@lexical/clipboard 0.1.14 → 0.1.15

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.
@@ -6,199 +6,9 @@
6
6
  */
7
7
  'use strict';
8
8
 
9
+ var selection = require('@lexical/selection');
9
10
  var lexical = require('lexical');
10
11
 
11
- /**
12
- * Copyright (c) Meta Platforms, Inc. and affiliates.
13
- *
14
- * This source code is licensed under the MIT license found in the
15
- * LICENSE file in the root directory of this source tree.
16
- *
17
- *
18
- */
19
-
20
- function $cloneWithProperties(node) {
21
- const latest = node.getLatest();
22
- const constructor = latest.constructor;
23
- const clone = constructor.clone(latest);
24
- clone.__parent = latest.__parent;
25
-
26
- if (lexical.$isElementNode(latest) && lexical.$isElementNode(clone)) {
27
- clone.__children = Array.from(latest.__children);
28
- clone.__format = latest.__format;
29
- clone.__indent = latest.__indent;
30
- clone.__dir = latest.__dir;
31
- } else if (lexical.$isTextNode(latest) && lexical.$isTextNode(clone)) {
32
- clone.__format = latest.__format;
33
- clone.__style = latest.__style;
34
- clone.__mode = latest.__mode;
35
- clone.__detail = latest.__detail;
36
- } else if (lexical.$isDecoratorNode(latest) && lexical.$isDecoratorNode(clone)) {
37
- clone.__state = latest.__state;
38
- } // $FlowFixMe
39
-
40
-
41
- return clone;
42
- }
43
-
44
- function $getIndexFromPossibleClone(node, parent, nodeMap) {
45
- const parentClone = nodeMap.get(parent.getKey());
46
-
47
- if (lexical.$isElementNode(parentClone)) {
48
- return parentClone.__children.indexOf(node.getKey());
49
- }
50
-
51
- return node.getIndexWithinParent();
52
- }
53
-
54
- function $getParentAvoidingExcludedElements(node) {
55
- let parent = node.getParent();
56
-
57
- while (parent !== null && parent.excludeFromCopy()) {
58
- parent = parent.getParent();
59
- }
60
-
61
- return parent;
62
- }
63
-
64
- function $copyLeafNodeBranchToRoot(leaf, startingOffset, isLeftSide, range, nodeMap) {
65
- let node = leaf;
66
- let offset = startingOffset;
67
-
68
- while (node !== null) {
69
- const parent = $getParentAvoidingExcludedElements(node);
70
-
71
- if (parent === null) {
72
- break;
73
- }
74
-
75
- if (!lexical.$isElementNode(node) || !node.excludeFromCopy()) {
76
- const key = node.getKey();
77
- let clone = nodeMap.get(key);
78
- const needsClone = clone === undefined;
79
-
80
- if (needsClone) {
81
- clone = $cloneWithProperties(node);
82
- nodeMap.set(key, clone);
83
- }
84
-
85
- if (lexical.$isTextNode(clone) && !clone.isSegmented() && !clone.isToken()) {
86
- clone.__text = clone.__text.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : offset);
87
- } else if (lexical.$isElementNode(clone)) {
88
- clone.__children = clone.__children.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : offset + 1);
89
- }
90
-
91
- if (lexical.$isRootNode(parent)) {
92
- if (needsClone) {
93
- // We only want to collect a range of top level nodes.
94
- // So if the parent is the root, we know this is a top level.
95
- range.push(key);
96
- }
97
-
98
- break;
99
- }
100
- }
101
-
102
- offset = $getIndexFromPossibleClone(node, parent, nodeMap);
103
- node = parent;
104
- }
105
- }
106
-
107
- function $cloneContents(selection) {
108
- if (!lexical.$isRangeSelection(selection)) {
109
- {
110
- throw Error(`TODO`);
111
- }
112
- }
113
-
114
- const anchor = selection.anchor;
115
- const focus = selection.focus;
116
- const anchorOffset = anchor.getCharacterOffset();
117
- const focusOffset = focus.getCharacterOffset();
118
- const anchorNode = anchor.getNode();
119
- const focusNode = focus.getNode();
120
- const anchorNodeParent = anchorNode.getParentOrThrow(); // Handle a single text node extraction
121
-
122
- if (anchorNode === focusNode && lexical.$isTextNode(anchorNode) && (anchorNodeParent.canBeEmpty() || anchorNodeParent.getChildrenSize() > 1)) {
123
- const clonedFirstNode = $cloneWithProperties(anchorNode);
124
- const isBefore = focusOffset > anchorOffset;
125
- const startOffset = isBefore ? anchorOffset : focusOffset;
126
- const endOffset = isBefore ? focusOffset : anchorOffset;
127
- clonedFirstNode.__text = clonedFirstNode.__text.slice(startOffset, endOffset);
128
- const key = clonedFirstNode.getKey();
129
- return {
130
- nodeMap: [[key, clonedFirstNode]],
131
- range: [key]
132
- };
133
- }
134
-
135
- const nodes = selection.getNodes();
136
-
137
- if (nodes.length === 0) {
138
- return {
139
- nodeMap: [],
140
- range: []
141
- };
142
- } // Check if we can use the parent of the nodes, if the
143
- // parent can't be empty, then it's important that we
144
- // also copy that element node along with its children.
145
-
146
-
147
- let nodesLength = nodes.length;
148
- const firstNode = nodes[0];
149
- const firstNodeParent = firstNode.getParent();
150
-
151
- if (firstNodeParent !== null && (!firstNodeParent.canBeEmpty() || lexical.$isRootNode(firstNodeParent))) {
152
- const parentChildren = firstNodeParent.__children;
153
- const parentChildrenLength = parentChildren.length;
154
-
155
- if (parentChildrenLength === nodesLength) {
156
- let areTheSame = true;
157
-
158
- for (let i = 0; i < parentChildren.length; i++) {
159
- if (parentChildren[i] !== nodes[i].__key) {
160
- areTheSame = false;
161
- break;
162
- }
163
- }
164
-
165
- if (areTheSame) {
166
- nodesLength++;
167
- nodes.push(firstNodeParent);
168
- }
169
- }
170
- }
171
-
172
- const lastNode = nodes[nodesLength - 1];
173
- const isBefore = anchor.isBefore(focus);
174
- const nodeMap = new Map();
175
- const range = []; // Do first node to root
176
-
177
- $copyLeafNodeBranchToRoot(firstNode, isBefore ? anchorOffset : focusOffset, true, range, nodeMap); // Copy all nodes between
178
-
179
- for (let i = 0; i < nodesLength; i++) {
180
- const node = nodes[i];
181
- const key = node.getKey();
182
-
183
- if (!nodeMap.has(key) && (!lexical.$isElementNode(node) || !node.excludeFromCopy())) {
184
- const clone = $cloneWithProperties(node);
185
-
186
- if (lexical.$isRootNode(node.getParent())) {
187
- range.push(node.getKey());
188
- }
189
-
190
- nodeMap.set(key, clone);
191
- }
192
- } // Do last node to root
193
-
194
-
195
- $copyLeafNodeBranchToRoot(lastNode, isBefore ? focusOffset : anchorOffset, false, range, nodeMap);
196
- return {
197
- nodeMap: Array.from(nodeMap.entries()),
198
- range
199
- };
200
- }
201
-
202
12
  /**
203
13
  * Copyright (c) Meta Platforms, Inc. and affiliates.
204
14
  *
@@ -238,13 +48,13 @@ function getHtmlContent(editor) {
238
48
  return null;
239
49
  }
240
50
  function $getLexicalContent(editor) {
241
- const selection = lexical.$getSelection();
51
+ const selection$1 = lexical.$getSelection();
242
52
 
243
- if (selection !== null) {
53
+ if (selection$1 !== null) {
244
54
  const namespace = editor._config.namespace;
245
55
  return JSON.stringify({
246
56
  namespace,
247
- state: $cloneContents(selection)
57
+ state: selection.$cloneContents(selection$1)
248
58
  });
249
59
  }
250
60
 
@@ -4,12 +4,8 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var h=require("lexical");function r(a){a=a.getLatest();const b=a.constructor.clone(a);b.__parent=a.__parent;h.$isElementNode(a)&&h.$isElementNode(b)?(b.__children=Array.from(a.__children),b.__format=a.__format,b.__indent=a.__indent,b.__dir=a.__dir):h.$isTextNode(a)&&h.$isTextNode(b)?(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail):h.$isDecoratorNode(a)&&h.$isDecoratorNode(b)&&(b.__state=a.__state);return b}
8
- function t(a,b,l,g,f){for(var c=b;null!==a;){for(b=a.getParent();null!==b&&b.excludeFromCopy();)b=b.getParent();if(null===b)break;if(!h.$isElementNode(a)||!a.excludeFromCopy()){const e=a.getKey();let d=f.get(e);const k=void 0===d;k&&(d=r(a),f.set(e,d));!h.$isTextNode(d)||d.isSegmented()||d.isToken()?h.$isElementNode(d)&&(d.__children=d.__children.slice(l?c:0,l?void 0:c+1)):d.__text=d.__text.slice(l?c:0,l?void 0:c);if(h.$isRootNode(b)){k&&g.push(e);break}}c=f.get(b.getKey());c=h.$isElementNode(c)?
9
- c.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=b}}function u(a,b){a=a.getData("text/plain");null!=a&&b.insertRawText(a)}function v(a,b){const {nodeName:l}=a;b=b._htmlConversions.get(l.toLowerCase());let g=null;void 0!==b&&b.forEach(f=>{f=f(a);null!==f&&(null===g||g.priority<f.priority)&&(g=f)});return null!==g?g.conversion:null}
10
- function w(a,b,l=new Map){let g=[],f=null;var c=v(a,b),e=c?c(a):null;c=null;if(null!==e){c=e.after;f=e.node;if(null!==f){g.push(f);var d=Array.from(l.values());for(let k=0;k<d.length;k++)d[k](f)}null!=e.forChild&&l.set(a.nodeName,e.forChild)}a=a.childNodes;e=[];for(d=0;d<a.length;d++)e.push(...w(a[d],b,l));null!=c&&(e=c(e));null==f?g=g.concat(e):h.$isElementNode(f)&&f.append(...e);return g}
11
- exports.$getLexicalContent=function(a){var b=h.$getSelection();if(null!==b){const x=a._config.namespace;a=JSON;var l=a.stringify;{if(!h.$isRangeSelection(b))throw Error("Minified Lexical error #68; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");var g=b.anchor,f=b.focus;var c=g.getCharacterOffset();const q=f.getCharacterOffset();var e=g.getNode(),d=f.getNode(),k=e.getParentOrThrow();if(e===d&&h.$isTextNode(e)&&(k.canBeEmpty()||
12
- 1<k.getChildrenSize()))b=r(e),e=q>c,b.__text=b.__text.slice(e?c:q,e?q:c),c=b.getKey(),c={nodeMap:[[c,b]],range:[c]};else if(b=b.getNodes(),0===b.length)c={nodeMap:[],range:[]};else{e=b.length;d=b[0];k=d.getParent();if(null!==k&&(!k.canBeEmpty()||h.$isRootNode(k))){var m=k.__children;if(m.length===e){var n=!0;for(var p=0;p<m.length;p++)if(m[p]!==b[p].__key){n=!1;break}n&&(e++,b.push(k))}}k=b[e-1];g=g.isBefore(f);f=new Map;m=[];t(d,g?c:q,!0,m,f);for(d=0;d<e;d++)if(n=b[d],p=n.getKey(),!(f.has(p)||h.$isElementNode(n)&&
13
- n.excludeFromCopy())){const y=r(n);h.$isRootNode(n.getParent())&&m.push(n.getKey());f.set(p,y)}t(k,g?q:c,!1,m,f);c={nodeMap:Array.from(f.entries()),range:m}}}return l.call(a,{namespace:x,state:c})}return null};exports.$insertDataTransferForPlainText=u;
14
- exports.$insertDataTransferForRichText=function(a,b,l){var g=a.getData("application/x-lexical-editor");if(g){var f=l._config.namespace;try{const k=JSON.parse(g);if(k.namespace===f){const {range:m,nodeMap:n}=k.state;var c=new Map(n);g=[];for(f=0;f<m.length;f++){var e=c.get(m[f]);if(void 0!==e){var d=h.$createNodeFromParse(e,c);g.push(d)}}b.insertNodes(g);return}}catch(k){}}if(c=a.getData("text/html")){c=(new DOMParser).parseFromString(c,"text/html");a=[];c=c.body?Array.from(c.body.childNodes):[];e=
15
- c.length;for(d=0;d<e;d++)g=w(c[d],l),null!==g&&(a=a.concat(g));l=a;a=[];c=null;for(e=0;e<l.length;e++)d=l[e],!h.$isElementNode(d)||d.isInline()?(null===c&&(c=h.$createParagraphNode(),a.push(c)),null!==c&&c.append(d)):(a.push(d),c=null);b.insertNodes(a)}else u(a,b)};exports.getHtmlContent=function(){var a=window.getSelection();if(a.isCollapsed)return null;var b=a.getRangeAt(0);return b?(a=document.createElement("div"),b=b.cloneContents(),a.appendChild(b),a.innerHTML):null};
7
+ var l=require("@lexical/selection"),m=require("lexical");function n(a,c){a=a.getData("text/plain");null!=a&&c.insertRawText(a)}function q(a,c){const {nodeName:h}=a;c=c._htmlConversions.get(h.toLowerCase());let e=null;void 0!==c&&c.forEach(f=>{f=f(a);null!==f&&(null===e||e.priority<f.priority)&&(e=f)});return null!==e?e.conversion:null}
8
+ function r(a,c,h=new Map){let e=[],f=null;var b=q(a,c),d=b?b(a):null;b=null;if(null!==d){b=d.after;f=d.node;if(null!==f){e.push(f);var g=Array.from(h.values());for(let k=0;k<g.length;k++)g[k](f)}null!=d.forChild&&h.set(a.nodeName,d.forChild)}a=a.childNodes;d=[];for(g=0;g<a.length;g++)d.push(...r(a[g],c,h));null!=b&&(d=b(d));null==f?e=e.concat(d):m.$isElementNode(f)&&f.append(...d);return e}
9
+ exports.$getLexicalContent=function(a){const c=m.$getSelection();return null!==c?JSON.stringify({namespace:a._config.namespace,state:l.$cloneContents(c)}):null};exports.$insertDataTransferForPlainText=n;
10
+ exports.$insertDataTransferForRichText=function(a,c,h){var e=a.getData("application/x-lexical-editor");if(e){var f=h._config.namespace;try{const k=JSON.parse(e);if(k.namespace===f){const {range:p,nodeMap:t}=k.state;var b=new Map(t);e=[];for(f=0;f<p.length;f++){var d=b.get(p[f]);if(void 0!==d){var g=m.$createNodeFromParse(d,b);e.push(g)}}c.insertNodes(e);return}}catch(k){}}if(b=a.getData("text/html")){b=(new DOMParser).parseFromString(b,"text/html");a=[];b=b.body?Array.from(b.body.childNodes):[];d=
11
+ b.length;for(g=0;g<d;g++)e=r(b[g],h),null!==e&&(a=a.concat(e));h=a;a=[];b=null;for(d=0;d<h.length;d++)g=h[d],!m.$isElementNode(g)||g.isInline()?(null===b&&(b=m.$createParagraphNode(),a.push(b)),null!==b&&b.append(g)):(a.push(g),b=null);c.insertNodes(a)}else n(a,c)};exports.getHtmlContent=function(){var a=window.getSelection();if(a.isCollapsed)return null;var c=a.getRangeAt(0);return c?(a=document.createElement("div"),c=c.cloneContents(),a.appendChild(c),a.innerHTML):null};
package/package.json CHANGED
@@ -13,13 +13,14 @@
13
13
  "paste"
14
14
  ],
15
15
  "license": "MIT",
16
- "version": "0.1.14",
16
+ "version": "0.1.15",
17
17
  "main": "LexicalClipboard.js",
18
18
  "peerDependencies": {
19
- "lexical": "0.1.14"
19
+ "lexical": "0.1.15"
20
20
  },
21
21
  "dependencies": {
22
- "@lexical/helpers": "0.1.14"
22
+ "@lexical/utils": "0.1.15",
23
+ "@lexical/selection": "0.1.15"
23
24
  },
24
25
  "repository": {
25
26
  "type": "git",