@lexical/dragon 0.12.2 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LexicalDragon.dev.js +7 -21
- package/package.json +2 -2
package/LexicalDragon.dev.js
CHANGED
|
@@ -11,73 +11,60 @@ var lexical = require('lexical');
|
|
|
11
11
|
/** @module @lexical/dragon */
|
|
12
12
|
function registerDragonSupport(editor) {
|
|
13
13
|
const origin = window.location.origin;
|
|
14
|
-
|
|
15
14
|
const handler = event => {
|
|
16
15
|
if (event.origin !== origin) {
|
|
17
16
|
return;
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
const rootElement = editor.getRootElement();
|
|
21
|
-
|
|
22
19
|
if (document.activeElement !== rootElement) {
|
|
23
20
|
return;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
const data = event.data;
|
|
27
|
-
|
|
28
23
|
if (typeof data === 'string') {
|
|
29
24
|
let parsedData;
|
|
30
|
-
|
|
31
25
|
try {
|
|
32
26
|
parsedData = JSON.parse(data);
|
|
33
27
|
} catch (e) {
|
|
34
28
|
return;
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
if (parsedData && parsedData.protocol === 'nuanria_messaging' && parsedData.type === 'request') {
|
|
38
31
|
const payload = parsedData.payload;
|
|
39
|
-
|
|
40
32
|
if (payload && payload.functionId === 'makeChanges') {
|
|
41
33
|
const args = payload.args;
|
|
42
|
-
|
|
43
34
|
if (args) {
|
|
44
|
-
const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args;
|
|
35
|
+
const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args;
|
|
45
36
|
editor.update(() => {
|
|
46
37
|
const selection = lexical.$getSelection();
|
|
47
|
-
|
|
48
38
|
if (lexical.$isRangeSelection(selection)) {
|
|
49
39
|
const anchor = selection.anchor;
|
|
50
40
|
let anchorNode = anchor.getNode();
|
|
51
41
|
let setSelStart = 0;
|
|
52
42
|
let setSelEnd = 0;
|
|
53
|
-
|
|
54
43
|
if (lexical.$isTextNode(anchorNode)) {
|
|
55
44
|
// set initial selection
|
|
56
45
|
if (elementStart >= 0 && elementLength >= 0) {
|
|
57
46
|
setSelStart = elementStart;
|
|
58
|
-
setSelEnd = elementStart + elementLength;
|
|
59
|
-
|
|
47
|
+
setSelEnd = elementStart + elementLength;
|
|
48
|
+
// If the offset is more than the end, make it the end
|
|
60
49
|
selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
|
|
61
50
|
}
|
|
62
51
|
}
|
|
63
|
-
|
|
64
52
|
if (setSelStart !== setSelEnd || text !== '') {
|
|
65
53
|
selection.insertRawText(text);
|
|
66
54
|
anchorNode = anchor.getNode();
|
|
67
55
|
}
|
|
68
|
-
|
|
69
56
|
if (lexical.$isTextNode(anchorNode)) {
|
|
70
57
|
// set final selection
|
|
71
58
|
setSelStart = selStart;
|
|
72
59
|
setSelEnd = selStart + selLength;
|
|
73
|
-
const anchorNodeTextLength = anchorNode.getTextContentSize();
|
|
74
|
-
|
|
60
|
+
const anchorNodeTextLength = anchorNode.getTextContentSize();
|
|
61
|
+
// If the offset is more than the end, make it the end
|
|
75
62
|
setSelStart = setSelStart > anchorNodeTextLength ? anchorNodeTextLength : setSelStart;
|
|
76
63
|
setSelEnd = setSelEnd > anchorNodeTextLength ? anchorNodeTextLength : setSelEnd;
|
|
77
64
|
selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
|
|
78
|
-
}
|
|
79
|
-
|
|
65
|
+
}
|
|
80
66
|
|
|
67
|
+
// block the chrome extension from handling this event
|
|
81
68
|
event.stopImmediatePropagation();
|
|
82
69
|
}
|
|
83
70
|
});
|
|
@@ -86,7 +73,6 @@ function registerDragonSupport(editor) {
|
|
|
86
73
|
}
|
|
87
74
|
}
|
|
88
75
|
};
|
|
89
|
-
|
|
90
76
|
window.addEventListener('message', handler, true);
|
|
91
77
|
return () => {
|
|
92
78
|
window.removeEventListener('message', handler, true);
|
package/package.json
CHANGED