Skip to content

Commit 9126756

Browse files
authored
fix(Cursor): input-rules does not work when cursor in virtual selection (GapCursorSelection) (#515)
1 parent 8a8fce8 commit 9126756

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/extensions/behavior/Cursor/gapcursor.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {DOMSerializer} from 'prosemirror-model';
2-
import {EditorState, Plugin, PluginKey} from 'prosemirror-state';
3-
import {Decoration, DecorationSet, EditorView} from 'prosemirror-view';
2+
import {type EditorState, Plugin, PluginKey, TextSelection} from 'prosemirror-state';
3+
import {Decoration, DecorationSet, type EditorView} from 'prosemirror-view';
44

55
import {isNodeSelection} from '../../../utils/selection';
66
import {pType} from '../../base/BaseSchema';
@@ -32,6 +32,21 @@ export const gapCursor = () =>
3232
};
3333
},
3434
props: {
35+
handleKeyPress(view) {
36+
const {
37+
state,
38+
state: {selection: sel},
39+
} = view;
40+
if (isGapCursorSelection(sel)) {
41+
// Replace GapCursorSelection with empty textblock before run all other handlers.
42+
// This should be done before all inputRules and other handlers, that handle text input.
43+
// Thus, entering text into a native textblock and into a "virtual" one – GapCursor – will be the same.
44+
const tr = state.tr.replaceSelectionWith(pType(state.schema).create());
45+
tr.setSelection(TextSelection.create(tr.doc, sel.pos + 1));
46+
view.dispatch(tr.scrollIntoView());
47+
}
48+
return false;
49+
},
3550
decorations: ({doc, selection}: EditorState) => {
3651
if (isGapCursorSelection(selection)) {
3752
const position = selection.head;

src/extensions/behavior/Cursor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ export type CursorOptions = {
1111
};
1212

1313
export const Cursor: ExtensionAuto<CursorOptions> = (builder, opts) => {
14-
builder.addPlugin(() => gapCursor());
14+
builder.addPlugin(() => gapCursor(), builder.Priority.Highest);
1515
builder.addPlugin(() => dropCursor(opts.dropOptions));
1616
};

0 commit comments

Comments
 (0)