ueberdosis / tiptap

The headless rich text editor framework for web artisans.
https://tiptap.dev
MIT License
27.52k stars 2.29k forks source link

[Bug]: Table extended bug #4017

Open Liszt-Fly opened 1 year ago

Liszt-Fly commented 1 year ago

Which packages did you experience the bug in?

extension-table

What Tiptap version are you using?

2.0.3

What’s the bug you are facing?

I try to extend table, and I want to use addCommand

export const CaptionedTable = Table.extend({
    name: "captionedTable",

    addKeyboardShortcuts() {
        return {
            [insert_table]: () => this.editor.commands.insertTable(),
            "Tab": () => this.editor.commands.goToNextCell(),

        }
    },
    addInputRules() {
        return [
            nodeInputRuleWithContent({
                find: /\|\|\s/,
                type: this.type,
                content: createTableRow(this.editor, false, 2, 1)
            })
        ]
    },
    addCommands() {
        return {

        }
    }
})

but if I use the addCommand method, the default method cannot be used, for example, I want to use tab to go to next cell, but it failed.

image

add this is the custom nodeInput method:

import { ExtendedRegExpMatchArray, InputRule, InputRuleFinder, callOrReturn, nodeInputRule } from "@tiptap/core"
import { TextSelection } from "@tiptap/pm/state"
import { Node, NodeType } from "prosemirror-model"

export function nodeInputRuleWithContent(config: {
    find: InputRuleFinder
    type: NodeType
    content: Node[]
    getAttributes?:
    | Record<string, any>
    | ((match: ExtendedRegExpMatchArray) => Record<string, any>)
    | false
    | null
}) {
    return new InputRule({
        find: config.find,
        handler: ({ state, range, match }) => {
            const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
            const { tr } = state
            const start = range.from
            let end = range.to

            console.log('match', match)
            if (match[1]) {
                const offset = match[0].lastIndexOf(match[1])
                let matchStart = start + offset

                if (matchStart > end) {
                    matchStart = end
                } else {
                    end = matchStart + match[1].length
                }

                // insert last typed character
                const lastChar = match[0][match[0].length - 1]

                tr.insertText(lastChar, start + match[0].length - 1)

                // insert node from input rule
                tr.replaceWith(matchStart, end, config.type.create(attributes, config.content)).scrollIntoView()
                    .setSelection(TextSelection.near(tr.doc.resolve(offset)))
                alert(6)
            } else if (match[0]) {
                const offset = tr.selection.anchor + 1
                tr.replaceWith(start, end, config.type.create(attributes, config.content)).scrollIntoView()
                    .setSelection(TextSelection.near(tr.doc.resolve(offset)))
            }

        },

    })
}

What browser are you using?

Chrome

Code example

No response

What did you expect to happen?

I hope when I use addCommand method, these default method can be used. Everything works for custom-header plugin, but the table doesn't

Anything to add? (optional)

No response

Did you update your dependencies?

Are you sponsoring us?

vinoddotcom commented 1 year ago

We have encountered an issue while integrating @tiptap-pro/extension-mathematics katex with @tiptap/extension-table @tiptap/extension-table-row @tiptap/extension-table-header @tiptap/extension-table-cell. While each extension works individually, when integrated into the same editor, we receive an error when attempting to insert or manipulate a table.

image

We are currently experiencing this issue while using the latest version of Chrome, and have commented on an existing GitHub issue related to this problem. If possible, we would greatly appreciate any assistance in resolving this issue.

image

rezaffm commented 1 year ago

not sure if it helps, but I also tried to change my Tables a bit by adding a width attribute: https://gist.github.com/hanspagel/de6cd0c30ece7afb3f2ba663768f373f

However, once I used "configure" on the extension, it kind of "resetted" the extension, if just using it without, so just doing "extend" lead to the table not being wrapped in a div anymore.

dan-cooke commented 2 months ago

I have encountered this before, it seems addCommands is not combined with the base extension commands

I've got round it in the past by doing this:


    addCommands() {
        return {
+++                     ...Table.config.addCommands()

        }
    }

You'll get type errors but it works