Tiptap: Easy way to get all content after current cursor position

Created on 13 Dec 2018  路  1Comment  路  Source: ueberdosis/tiptap

Hello. I need to get all content after current cursor position and when I press "ctrl-enter" create a new instance of tiptap editor with this content (also remove this content on the current instance).

I have created a custom plugin, using your examples, where i've track the keydown, but I can't find the method like "split" or something the same. Can you say me right way to make this feature? Thanks.

Most helpful comment

I've found a solution.

function splitBlock ({ onCtrlEnter }: { onCtrlEnter: Function }) {
    return new Plugin({
        key: new PluginKey('splitBlock'),
        // @ts-ignore next-line
        props: {
            handleKeyDown ({ state, dom }, event) {
                if (event.ctrlKey && event.keyCode === KeyboardKeys.ENTER) {
                    const pos = get(state, 'selection.$anchor.pos', true);
                    const { doc } = state;
                    const textBefore = doc.cut(0, pos).toJSON();
                    const textAfter = doc.cut(pos, doc.content.size).toJSON();

                    return onCtrlEnter({ dom, textBefore, textAfter });
                }
            }
        }
    });
}

Then in my vue component I've replace content using setContent(textBefore). And send an event with textAfter data to my Vuex.Store for creation a new editor instance.

>All comments

I've found a solution.

function splitBlock ({ onCtrlEnter }: { onCtrlEnter: Function }) {
    return new Plugin({
        key: new PluginKey('splitBlock'),
        // @ts-ignore next-line
        props: {
            handleKeyDown ({ state, dom }, event) {
                if (event.ctrlKey && event.keyCode === KeyboardKeys.ENTER) {
                    const pos = get(state, 'selection.$anchor.pos', true);
                    const { doc } = state;
                    const textBefore = doc.cut(0, pos).toJSON();
                    const textAfter = doc.cut(pos, doc.content.size).toJSON();

                    return onCtrlEnter({ dom, textBefore, textAfter });
                }
            }
        }
    });
}

Then in my vue component I've replace content using setContent(textBefore). And send an event with textAfter data to my Vuex.Store for creation a new editor instance.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glavdir picture glavdir  路  3Comments

afwn90cj93201nixr2e1re picture afwn90cj93201nixr2e1re  路  3Comments

Auxxxxlx picture Auxxxxlx  路  3Comments

agentq15 picture agentq15  路  3Comments

jetacpp picture jetacpp  路  3Comments