Skip to content

Commit

Permalink
fix(Cursor): input-rules does not work when cursor in virtual selecti…
Browse files Browse the repository at this point in the history
…on (GapCursorSelection) (#515)
  • Loading branch information
d3m1d0v authored Dec 5, 2024
1 parent 8a8fce8 commit 9126756
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/extensions/behavior/Cursor/gapcursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {DOMSerializer} from 'prosemirror-model';
import {EditorState, Plugin, PluginKey} from 'prosemirror-state';
import {Decoration, DecorationSet, EditorView} from 'prosemirror-view';
import {type EditorState, Plugin, PluginKey, TextSelection} from 'prosemirror-state';
import {Decoration, DecorationSet, type EditorView} from 'prosemirror-view';

import {isNodeSelection} from '../../../utils/selection';
import {pType} from '../../base/BaseSchema';
Expand Down Expand Up @@ -32,6 +32,21 @@ export const gapCursor = () =>
};
},
props: {
handleKeyPress(view) {
const {
state,
state: {selection: sel},
} = view;
if (isGapCursorSelection(sel)) {
// Replace GapCursorSelection with empty textblock before run all other handlers.
// This should be done before all inputRules and other handlers, that handle text input.
// Thus, entering text into a native textblock and into a "virtual" one – GapCursor – will be the same.
const tr = state.tr.replaceSelectionWith(pType(state.schema).create());
tr.setSelection(TextSelection.create(tr.doc, sel.pos + 1));
view.dispatch(tr.scrollIntoView());
}
return false;
},
decorations: ({doc, selection}: EditorState) => {
if (isGapCursorSelection(selection)) {
const position = selection.head;
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/behavior/Cursor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export type CursorOptions = {
};

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

0 comments on commit 9126756

Please sign in to comment.