Set Cursor Position in Plugin

Hello, i am very new to Prosemirror. i tryed to update the cursor position using a plugin. i tryed a bunch of different ways but it does not work the way i expect. Could you give me a hint what’s wrong?

let plugin = new Plugin({
    view: (editor) => {
        return {
            update: (view, old) => {
                if (old.doc.content.size % 5 === 0 && old.doc.content.size) {
                    const tr = new Transaction(view.state.doc);
                    const toStart = tr.setSelection(TextSelection.atStart(view.state.doc));
                    view.updateState(view.state.apply(toStart));
                }
            }
        }
    }
})

A view plugin update method is called as part of a view update, and as such it should not initiate another view update. You may be looking for appendTransaction instead.

Thanks for the hint. i tryed a few different approaches with appendTransaction but i am not able to move the cursor.

let plugin = new Plugin({
    appendTransaction: (transactions: readonly Transaction[], oldState: EditorState, newState: EditorState) : Transaction | null | undefined => {
        let newSelection = TextSelection.create(newState.doc, 0);
        return newState.tr.setSelection(newSelection);
    },
})
1 Like