@lexical/clipboard 0.1.14 → 0.1.17

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Dominic Gannaway
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import type {LexicalEditor, RangeSelection} from 'lexical';
10
+
11
+ /*
12
+ * Rich Text
13
+ */
14
+
15
+ export function $insertDataTransferForRichText(
16
+ dataTransfer: DataTransfer,
17
+ selection: RangeSelection,
18
+ editor: LexicalEditor,
19
+ ): void;
20
+
21
+ export function getHtmlContent(editor: LexicalEditor): string;
22
+ export function $getLexicalContent(editor: LexicalEditor): string;
23
+
24
+ /*
25
+ * Plain Text
26
+ */
27
+
28
+ export function $insertDataTransferForPlainText(
29
+ dataTransfer: DataTransfer,
30
+ selection: RangeSelection,
31
+ ): void;
@@ -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
  *
@@ -219,6 +29,7 @@ var getDOMSelection = getSelection;
219
29
  *
220
30
  *
221
31
  */
32
+ const IGNORE_TAGS = new Set(['STYLE']);
222
33
  function getHtmlContent(editor) {
223
34
  const domSelection = getDOMSelection(); // If we haven't selected a range, then don't copy anything
224
35
 
@@ -238,13 +49,14 @@ function getHtmlContent(editor) {
238
49
  return null;
239
50
  }
240
51
  function $getLexicalContent(editor) {
241
- const selection = lexical.$getSelection();
52
+ const selection$1 = lexical.$getSelection();
242
53
 
243
- if (selection !== null) {
54
+ if (selection$1 !== null) {
244
55
  const namespace = editor._config.namespace;
56
+ const state = selection.$cloneContents(selection$1);
245
57
  return JSON.stringify({
246
58
  namespace,
247
- state: $cloneContents(selection)
59
+ state
248
60
  });
249
61
  }
250
62
 
@@ -357,8 +169,13 @@ function getConversionFunction(domNode, editor) {
357
169
  return currentConversion !== null ? currentConversion.conversion : null;
358
170
  }
359
171
 
360
- function $createNodesFromDOM(node, editor, forChildMap = new Map()) {
172
+ function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexicalNode) {
361
173
  let lexicalNodes = [];
174
+
175
+ if (IGNORE_TAGS.has(node.nodeName)) {
176
+ return lexicalNodes;
177
+ }
178
+
362
179
  let currentLexicalNode = null;
363
180
  const transformFunction = getConversionFunction(node, editor);
364
181
  const transformOutput = transformFunction ? transformFunction(node) : null;
@@ -369,11 +186,16 @@ function $createNodesFromDOM(node, editor, forChildMap = new Map()) {
369
186
  currentLexicalNode = transformOutput.node;
370
187
 
371
188
  if (currentLexicalNode !== null) {
372
- lexicalNodes.push(currentLexicalNode);
373
- const forChildFunctions = Array.from(forChildMap.values());
189
+ for (const [, forChildFunction] of forChildMap) {
190
+ currentLexicalNode = forChildFunction(currentLexicalNode, parentLexicalNode);
374
191
 
375
- for (let i = 0; i < forChildFunctions.length; i++) {
376
- forChildFunctions[i](currentLexicalNode);
192
+ if (!currentLexicalNode) {
193
+ break;
194
+ }
195
+ }
196
+
197
+ if (currentLexicalNode) {
198
+ lexicalNodes.push(currentLexicalNode);
377
199
  }
378
200
  }
379
201
 
@@ -388,7 +210,7 @@ function $createNodesFromDOM(node, editor, forChildMap = new Map()) {
388
210
  let childLexicalNodes = [];
389
211
 
390
212
  for (let i = 0; i < children.length; i++) {
391
- childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, forChildMap));
213
+ childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, forChildMap, currentLexicalNode));
392
214
  }
393
215
 
394
216
  if (postTransform != null) {
@@ -416,10 +238,14 @@ function $generateNodesFromDOM(dom, editor) {
416
238
  const elementsLength = elements.length;
417
239
 
418
240
  for (let i = 0; i < elementsLength; i++) {
419
- const lexicalNode = $createNodesFromDOM(elements[i], editor);
241
+ const element = elements[i];
420
242
 
421
- if (lexicalNode !== null) {
422
- lexicalNodes = lexicalNodes.concat(lexicalNode);
243
+ if (!IGNORE_TAGS.has(element.nodeName)) {
244
+ const lexicalNode = $createNodesFromDOM(element, editor);
245
+
246
+ if (lexicalNode !== null) {
247
+ lexicalNodes = lexicalNodes.concat(lexicalNode);
248
+ }
423
249
  }
424
250
  }
425
251
 
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ import type {LexicalEditor, RangeSelection} from 'lexical';
11
+
12
+ /*
13
+ * Rich Text
14
+ */
15
+
16
+ declare export function $insertDataTransferForRichText(
17
+ dataTransfer: DataTransfer,
18
+ selection: RangeSelection,
19
+ editor: LexicalEditor,
20
+ ): void;
21
+
22
+ declare export function getHtmlContent(editor: LexicalEditor): string;
23
+ declare export function $getLexicalContent(editor: LexicalEditor): string;
24
+
25
+ /*
26
+ * Plain Text
27
+ */
28
+
29
+ declare export function $insertDataTransferForPlainText(
30
+ dataTransfer: DataTransfer,
31
+ selection: RangeSelection,
32
+ ): void;
@@ -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");const n=new Set(["STYLE"]);function p(a,d){a=a.getData("text/plain");null!=a&&d.insertRawText(a)}function r(a,d){const {nodeName:h}=a;d=d._htmlConversions.get(h.toLowerCase());let c=null;void 0!==d&&d.forEach(e=>{e=e(a);null!==e&&(null===c||c.priority<e.priority)&&(c=e)});return null!==c?c.conversion:null}
8
+ function t(a,d,h=new Map,c){let e=[];if(n.has(a.nodeName))return e;let b=null;var g=r(a,d);const f=g?g(a):null;g=null;if(null!==f){g=f.after;b=f.node;if(null!==b){for(var [,k]of h)if(b=k(b,c),!b)break;b&&e.push(b)}null!=f.forChild&&h.set(a.nodeName,f.forChild)}a=a.childNodes;c=[];for(k=0;k<a.length;k++)c.push(...t(a[k],d,h,b));null!=g&&(c=g(c));null==b?e=e.concat(c):m.$isElementNode(b)&&b.append(...c);return e}
9
+ exports.$getLexicalContent=function(a){var d=m.$getSelection();return null!==d?(a=a._config.namespace,d=l.$cloneContents(d),JSON.stringify({namespace:a,state:d})):null};exports.$insertDataTransferForPlainText=p;
10
+ exports.$insertDataTransferForRichText=function(a,d,h){var c=a.getData("application/x-lexical-editor");if(c){var e=h._config.namespace;try{const k=JSON.parse(c);if(k.namespace===e){const {range:q,nodeMap:u}=k.state;var b=new Map(u);c=[];for(e=0;e<q.length;e++){var g=b.get(q[e]);if(void 0!==g){var f=m.$createNodeFromParse(g,b);c.push(f)}}d.insertNodes(c);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):[];g=
11
+ b.length;for(f=0;f<g;f++)c=b[f],n.has(c.nodeName)||(c=t(c,h),null!==c&&(a=a.concat(c)));h=a;a=[];b=null;for(g=0;g<h.length;g++)f=h[g],!m.$isElementNode(f)||f.isInline()?(null===b&&(b=m.$createParagraphNode(),a.push(b)),null!==b&&b.append(f)):(a.push(f),b=null);d.insertNodes(a)}else p(a,d)};exports.getHtmlContent=function(){var a=window.getSelection();if(a.isCollapsed)return null;var d=a.getRangeAt(0);return d?(a=document.createElement("div"),d=d.cloneContents(),a.appendChild(d),a.innerHTML):null};
package/package.json CHANGED
@@ -1,9 +1,5 @@
1
1
  {
2
2
  "name": "@lexical/clipboard",
3
- "author": {
4
- "name": "Dominic Gannaway",
5
- "email": "dg@domgan.com"
6
- },
7
3
  "description": "This package provides the copy/paste functionality for Lexical.",
8
4
  "keywords": [
9
5
  "lexical",
@@ -13,13 +9,14 @@
13
9
  "paste"
14
10
  ],
15
11
  "license": "MIT",
16
- "version": "0.1.14",
12
+ "version": "0.1.17",
17
13
  "main": "LexicalClipboard.js",
18
14
  "peerDependencies": {
19
- "lexical": "0.1.14"
15
+ "lexical": "0.1.17"
20
16
  },
21
17
  "dependencies": {
22
- "@lexical/helpers": "0.1.14"
18
+ "@lexical/utils": "0.1.17",
19
+ "@lexical/selection": "0.1.17"
23
20
  },
24
21
  "repository": {
25
22
  "type": "git",