@lexical/dragon 0.8.1 → 0.9.1

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.
@@ -11,60 +11,73 @@ var lexical = require('lexical');
11
11
  /** @module @lexical/dragon */
12
12
  function registerDragonSupport(editor) {
13
13
  const origin = window.location.origin;
14
+
14
15
  const handler = event => {
15
16
  if (event.origin !== origin) {
16
17
  return;
17
18
  }
19
+
18
20
  const rootElement = editor.getRootElement();
21
+
19
22
  if (document.activeElement !== rootElement) {
20
23
  return;
21
24
  }
25
+
22
26
  const data = event.data;
27
+
23
28
  if (typeof data === 'string') {
24
29
  let parsedData;
30
+
25
31
  try {
26
32
  parsedData = JSON.parse(data);
27
33
  } catch (e) {
28
34
  return;
29
35
  }
36
+
30
37
  if (parsedData && parsedData.protocol === 'nuanria_messaging' && parsedData.type === 'request') {
31
38
  const payload = parsedData.payload;
39
+
32
40
  if (payload && payload.functionId === 'makeChanges') {
33
41
  const args = payload.args;
42
+
34
43
  if (args) {
35
- const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args;
44
+ const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args; // TODO: we should probably handle formatCommand somehow?
36
45
  editor.update(() => {
37
46
  const selection = lexical.$getSelection();
47
+
38
48
  if (lexical.$isRangeSelection(selection)) {
39
49
  const anchor = selection.anchor;
40
50
  let anchorNode = anchor.getNode();
41
51
  let setSelStart = 0;
42
52
  let setSelEnd = 0;
53
+
43
54
  if (lexical.$isTextNode(anchorNode)) {
44
55
  // set initial selection
45
56
  if (elementStart >= 0 && elementLength >= 0) {
46
57
  setSelStart = elementStart;
47
- setSelEnd = elementStart + elementLength;
48
- // If the offset is more than the end, make it the end
58
+ setSelEnd = elementStart + elementLength; // If the offset is more than the end, make it the end
59
+
49
60
  selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
50
61
  }
51
62
  }
63
+
52
64
  if (setSelStart !== setSelEnd || text !== '') {
53
65
  selection.insertRawText(text);
54
66
  anchorNode = anchor.getNode();
55
67
  }
68
+
56
69
  if (lexical.$isTextNode(anchorNode)) {
57
70
  // set final selection
58
71
  setSelStart = selStart;
59
72
  setSelEnd = selStart + selLength;
60
- const anchorNodeTextLength = anchorNode.getTextContentSize();
61
- // If the offset is more than the end, make it the end
73
+ const anchorNodeTextLength = anchorNode.getTextContentSize(); // If the offset is more than the end, make it the end
74
+
62
75
  setSelStart = setSelStart > anchorNodeTextLength ? anchorNodeTextLength : setSelStart;
63
76
  setSelEnd = setSelEnd > anchorNodeTextLength ? anchorNodeTextLength : setSelEnd;
64
77
  selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
65
- }
78
+ } // block the chrome extension from handling this event
79
+
66
80
 
67
- // block the chrome extension from handling this event
68
81
  event.stopImmediatePropagation();
69
82
  }
70
83
  });
@@ -73,6 +86,7 @@ function registerDragonSupport(editor) {
73
86
  }
74
87
  }
75
88
  };
89
+
76
90
  window.addEventListener('message', handler, true);
77
91
  return () => {
78
92
  window.removeEventListener('message', handler, true);
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "accessibility"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.8.1",
12
+ "version": "0.9.1",
13
13
  "main": "LexicalDragon.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.8.1"
15
+ "lexical": "0.9.1"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",