-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
inline-nodes-cursor.ts
66 lines (61 loc) 路 2.25 KB
/
inline-nodes-cursor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Copyright 2021, Milkdown by Mirone. */
import { Plugin, PluginKey } from '@milkdown/prose/state';
import { Decoration, DecorationSet } from '@milkdown/prose/view';
const inlineNodesCursorPluginKey = new PluginKey('MILKDOWN_INLINE_NODES_CURSOR');
/**
* This plugin is to solve the chrome 98 bug:
* https://discuss.prosemirror.net/t/cursor-jumps-at-the-end-of-line-when-it-betweens-two-inline-nodes/4641
*/
export const inlineNodesCursorPlugin: Plugin = new Plugin({
key: inlineNodesCursorPluginKey,
state: {
init() {
return false;
},
apply(tr) {
if (!tr.selection.empty) {
return false;
}
const pos = tr.selection.$from;
const left = pos.nodeBefore;
const right = pos.nodeAfter;
if (left && right && left.isInline && !left.isText && right.isInline && !right.isText) {
return true;
}
return false;
},
},
props: {
handleDOMEvents: {
beforeinput: (view, e) => {
const active = inlineNodesCursorPlugin.getState(view.state);
if (active) {
const from = view.state.selection.from;
e.preventDefault();
view.dispatch(view.state.tr.insertText(e.data || '', from));
return true;
}
return false;
},
},
decorations(state) {
const active = inlineNodesCursorPlugin.getState(state);
if (active) {
const pos = state.selection.$from;
const position = pos.pos;
const left = document.createElement('span');
const leftDec = Decoration.widget(position, left, {
side: -1,
});
const right = document.createElement('span');
const rightDec = Decoration.widget(position, right);
setTimeout(() => {
left.contentEditable = 'true';
right.contentEditable = 'true';
});
return DecorationSet.create(state.doc, [leftDec, rightDec]);
}
return DecorationSet.empty;
},
},
});