@lexical/clipboard 0.7.6 → 0.7.8

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.
@@ -7,7 +7,6 @@
7
7
  'use strict';
8
8
 
9
9
  var html = require('@lexical/html');
10
- var list = require('@lexical/list');
11
10
  var selection = require('@lexical/selection');
12
11
  var utils = require('@lexical/utils');
13
12
  var lexical = require('lexical');
@@ -23,7 +22,9 @@ function $getHtmlContent(editor) {
23
22
  const selection = lexical.$getSelection();
24
23
 
25
24
  if (selection == null) {
26
- throw new Error('Expected valid LexicalSelection');
25
+ {
26
+ throw Error(`Expected valid LexicalSelection`);
27
+ }
27
28
  } // If we haven't selected anything
28
29
 
29
30
 
@@ -39,7 +40,9 @@ function $getLexicalContent(editor) {
39
40
  const selection = lexical.$getSelection();
40
41
 
41
42
  if (selection == null) {
42
- throw new Error('Expected valid LexicalSelection');
43
+ {
44
+ throw Error(`Expected valid LexicalSelection`);
45
+ }
43
46
  } // If we haven't selected anything
44
47
 
45
48
 
@@ -66,9 +69,9 @@ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
66
69
  if (payload.namespace === editor._config.namespace && Array.isArray(payload.nodes)) {
67
70
  const nodes = $generateNodesFromSerializedNodes(payload.nodes);
68
71
  return $insertGeneratedNodes(editor, nodes, selection);
69
- } // eslint-disable-next-line no-empty
70
-
71
- } catch {}
72
+ }
73
+ } catch {// Fail silently.
74
+ }
72
75
  }
73
76
 
74
77
  const htmlString = dataTransfer.getData('text/html');
@@ -78,8 +81,9 @@ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
78
81
  const parser = new DOMParser();
79
82
  const dom = parser.parseFromString(htmlString, 'text/html');
80
83
  const nodes = html.$generateNodesFromDOM(editor, dom);
81
- return $insertGeneratedNodes(editor, nodes, selection); // eslint-disable-next-line no-empty
82
- } catch {}
84
+ return $insertGeneratedNodes(editor, nodes, selection);
85
+ } catch {// Fail silently.
86
+ }
83
87
  } // Multi-line plain text in rich text mode pasted as separate paragraphs
84
88
  // instead of single paragraph with linebreaks.
85
89
 
@@ -119,34 +123,14 @@ function $basicInsertStrategy(nodes, selection) {
119
123
  // Wrap text and inline nodes in paragraph nodes so we have all blocks at the top-level
120
124
  const topLevelBlocks = [];
121
125
  let currentBlock = null;
122
- let list$1 = null;
123
126
 
124
127
  for (let i = 0; i < nodes.length; i++) {
125
128
  const node = nodes[i];
126
- /**
127
- * There's no good way to add this to importDOM or importJSON directly,
128
- * so this is here in order to safely correct faulty clipboard data
129
- * that we can't control and avoid crashing the app.
130
- * https://github.com/facebook/lexical/issues/2405
131
- */
132
-
133
- if (list.$isListItemNode(node)) {
134
- if (list$1 == null) {
135
- list$1 = list.$createListNode('bullet');
136
- topLevelBlocks.push(list$1);
137
- }
138
-
139
- list$1.append(node);
140
- continue;
141
- } else if (list$1 != null) {
142
- list$1 = null;
143
- }
144
-
145
129
  const isLineBreakNode = lexical.$isLineBreakNode(node);
146
130
 
147
- if (isLineBreakNode || lexical.$isDecoratorNode(node) && node.isInline() || lexical.$isElementNode(node) && node.isInline() || lexical.$isTextNode(node)) {
131
+ if (isLineBreakNode || lexical.$isDecoratorNode(node) && node.isInline() || lexical.$isElementNode(node) && node.isInline() || lexical.$isTextNode(node) || node.isParentRequired()) {
148
132
  if (currentBlock === null) {
149
- currentBlock = lexical.$createParagraphNode();
133
+ currentBlock = node.createParentElementNode();
150
134
  topLevelBlocks.push(currentBlock); // In the case of LineBreakNode, we just need to
151
135
  // add an empty ParagraphNode to the topLevelBlocks.
152
136
 
@@ -308,7 +292,7 @@ function exportNodeToJSON(node) {
308
292
  }
309
293
 
310
294
  function $appendNodesToJSON(editor, selection$1, currentNode, targetArray = []) {
311
- let shouldInclude = selection$1 != null ? currentNode.isSelected() : true;
295
+ let shouldInclude = selection$1 != null ? currentNode.isSelected(selection$1) : true;
312
296
  const shouldExclude = lexical.$isElementNode(currentNode) && currentNode.excludeFromCopy('html');
313
297
  let target = currentNode;
314
298
 
@@ -327,7 +311,15 @@ function $appendNodesToJSON(editor, selection$1, currentNode, targetArray = [])
327
311
  // until then this hack will work for the selected text extract use case.
328
312
 
329
313
  if (lexical.$isTextNode(target)) {
330
- serializedNode.text = target.__text;
314
+ const text = target.__text; // If an uncollapsed selection ends or starts at the end of a line of specialized,
315
+ // TextNodes, such as code tokens, we will get a 'blank' TextNode here, i.e., one
316
+ // with text of length 0. We don't want this, it makes a confusing mess. Reset!
317
+
318
+ if (text.length > 0) {
319
+ serializedNode.text = text;
320
+ } else {
321
+ shouldInclude = false;
322
+ }
331
323
  }
332
324
 
333
325
  for (let i = 0; i < children.length; i++) {
@@ -447,14 +439,27 @@ async function copyToClipboard__EXPERIMENTAL(editor, event) {
447
439
  } // TODO shouldn't pass editor (pass namespace directly)
448
440
 
449
441
  function $copyToClipboardEvent(editor, event) {
442
+ const domSelection = window.getSelection();
443
+
444
+ if (!domSelection) {
445
+ return false;
446
+ }
447
+
448
+ const anchorDOM = domSelection.anchorNode;
449
+ const focusDOM = domSelection.focusNode;
450
+
451
+ if (anchorDOM !== null && focusDOM !== null && !lexical.isSelectionWithinEditor(editor, anchorDOM, focusDOM)) {
452
+ return false;
453
+ }
454
+
450
455
  event.preventDefault();
451
456
  const clipboardData = event.clipboardData;
457
+ const selection = lexical.$getSelection();
452
458
 
453
- if (clipboardData === null) {
459
+ if (clipboardData === null || selection === null) {
454
460
  return false;
455
461
  }
456
462
 
457
- const selection = lexical.$getSelection();
458
463
  const htmlString = $getHtmlContent(editor);
459
464
  const lexicalString = $getLexicalContent(editor);
460
465
  let plainString = '';
@@ -4,19 +4,18 @@
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
- 'use strict';var d=require("@lexical/html"),n=require("@lexical/list"),r=require("@lexical/selection"),t=require("@lexical/utils"),z=require("lexical");function A(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
- function B(a){let b=z.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return z.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?"":d.$generateHtmlFromNodes(a,b)}function C(a){let b=z.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return z.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?null:JSON.stringify(D(a,b))}
9
- function E(a,b,c){(z.DEPRECATED_$isGridSelection(c)||null!==t.$findMatchingParent(c.anchor.getNode(),g=>z.DEPRECATED_$isGridCellNode(g))&&null!==t.$findMatchingParent(c.focus.getNode(),g=>z.DEPRECATED_$isGridCellNode(g)))&&1===b.length&&z.DEPRECATED_$isGridNode(b[0])?F(b,c,!1,a):I(b,c)}
10
- function I(a,b){let c=[],g=null,e=null;for(let k=0;k<a.length;k++){let f=a[k];if(n.$isListItemNode(f)){null==e&&(e=n.$createListNode("bullet"),c.push(e));e.append(f);continue}else null!=e&&(e=null);let h=z.$isLineBreakNode(f);if(h||z.$isDecoratorNode(f)&&f.isInline()||z.$isElementNode(f)&&f.isInline()||z.$isTextNode(f)){if(null===g&&(g=z.$createParagraphNode(),c.push(g),h))continue;null!==g&&g.append(f)}else c.push(f),g=null}z.$isRangeSelection(b)?b.insertNodes(c):z.DEPRECATED_$isGridSelection(b)&&
11
- (a=b.anchor.getNode(),z.DEPRECATED_$isGridCellNode(a)||A(41),a.append(...c))}
12
- function F(a,b,c,g){1===a.length&&z.DEPRECATED_$isGridNode(a[0])||A(42);var e=a[0];a=e.getChildren();c=e.getFirstChildOrThrow().getChildrenSize();var k=e.getChildrenSize(),f=t.$findMatchingParent(b.anchor.getNode(),l=>z.DEPRECATED_$isGridCellNode(l));b=(e=f&&t.$findMatchingParent(f,l=>z.DEPRECATED_$isGridRowNode(l)))&&t.$findMatchingParent(e,l=>z.DEPRECATED_$isGridNode(l));z.DEPRECATED_$isGridCellNode(f)&&z.DEPRECATED_$isGridRowNode(e)&&z.DEPRECATED_$isGridNode(b)||A(43);var h=e.getIndexWithinParent(),
13
- p=Math.min(b.getChildrenSize()-1,h+k-1);k=f.getIndexWithinParent();f=Math.min(e.getChildrenSize()-1,k+c-1);c=Math.min(k,f);e=Math.min(h,p);k=Math.max(k,f);h=Math.max(h,p);p=b.getChildren();f=0;let m,q;for(let l=e;l<=h;l++){var x=p[l];z.DEPRECATED_$isGridRowNode(x)||A(24);var y=a[f];z.DEPRECATED_$isGridRowNode(y)||A(24);x=x.getChildren();y=y.getChildren();let G=0;for(let u=c;u<=k;u++){let v=x[u];z.DEPRECATED_$isGridCellNode(v)||A(25);let H=y[G];z.DEPRECATED_$isGridCellNode(H)||A(25);l===e&&u===c?m=
14
- v.getKey():l===h&&u===k&&(q=v.getKey());let N=v.getChildren();H.getChildren().forEach(w=>{z.$isTextNode(w)&&z.$createParagraphNode().append(w);v.append(w)});N.forEach(w=>w.remove());G++}f++}m&&q&&(a=z.DEPRECATED_$createGridSelection(),a.set(b.getKey(),m,q),z.$setSelection(a),g.dispatchCommand(z.SELECTION_CHANGE_COMMAND,void 0))}
15
- function J(a,b,c,g=[]){let e=null!=b?c.isSelected():!0,k=z.$isElementNode(c)&&c.excludeFromCopy("html");var f=c;if(null!==b){var h=r.$cloneWithProperties(c);f=h=z.$isTextNode(h)&&null!=b?r.$sliceSelectedTextNodeContent(b,h):h}let p=z.$isElementNode(f)?f.getChildren():[];var m=f;h=m.exportJSON();h.type!==m.constructor.getType()&&A(58);var q=h.children;z.$isElementNode(m)&&(Array.isArray(q)||A(59));z.$isTextNode(f)&&(h.text=f.__text);for(f=0;f<p.length;f++)m=p[f],q=J(a,b,m,h.children),!e&&z.$isElementNode(c)&&
16
- q&&c.extractWithChild(m,b,"clone")&&(e=!0);if(e&&!k)g.push(h);else if(Array.isArray(h.children))for(a=0;a<h.children.length;a++)g.push(h.children[a]);return e}function D(a,b){let c=[],g=z.$getRoot().getChildren();for(let e=0;e<g.length;e++)J(a,b,g[e],c);return{namespace:a._config.namespace,nodes:c}}function K(a){let b=[];for(let c=0;c<a.length;c++){let g=z.$parseSerializedNode(a[c]);z.$isTextNode(g)&&r.$addNodeStyle(g);b.push(g)}return b}let L=null;
17
- function M(a,b){b.preventDefault();b=b.clipboardData;if(null===b)return!1;let c=z.$getSelection(),g=B(a);a=C(a);let e="";null!==c&&(e=c.getTextContent());null!==g&&b.setData("text/html",g);null!==a&&b.setData("application/x-lexical-editor",a);b.setData("text/plain",e);return!0}exports.$generateJSONFromSelectedNodes=D;exports.$generateNodesFromSerializedNodes=K;exports.$getHtmlContent=B;exports.$getLexicalContent=C;
18
- exports.$insertDataTransferForPlainText=function(a,b){a=a.getData("text/plain");null!=a&&b.insertRawText(a)};
19
- exports.$insertDataTransferForRichText=function(a,b,c){var g=a.getData("application/x-lexical-editor");if(g)try{let k=JSON.parse(g);if(k.namespace===c._config.namespace&&Array.isArray(k.nodes)){let f=K(k.nodes);return E(c,f,b)}}catch{}if(g=a.getData("text/html"))try{var e=(new DOMParser).parseFromString(g,"text/html");let k=d.$generateNodesFromDOM(c,e);return E(c,k,b)}catch{}a=a.getData("text/plain");if(null!=a)if(z.$isRangeSelection(b))for(a=a.split(/\r?\n/),c=a.length,e=0;e<c;e++)b.insertText(a[e]),
20
- e<c-1&&b.insertParagraph();else b.insertRawText(a)};exports.$insertGeneratedNodes=E;
21
- exports.copyToClipboard__EXPERIMENTAL=async function(a,b){if(null!==L)return!1;if(null!==b)return new Promise(k=>{a.update(()=>{k(M(a,b))})});var c=a.getRootElement();let g=document.getSelection();if(null===c||null===g)return!1;let e=document.createElement("span");e.style.cssText="position: fixed; top: -1000px;";e.append(document.createTextNode("#"));c.append(e);c=new Range;c.setStart(e,0);c.setEnd(e,1);g.removeAllRanges();g.addRange(c);return new Promise(k=>{let f=a.registerCommand(z.COPY_COMMAND,
22
- h=>{h instanceof ClipboardEvent&&(f(),null!==L&&(window.clearTimeout(L),L=null),k(M(a,h)));return!0},z.COMMAND_PRIORITY_CRITICAL);L=window.setTimeout(()=>{f();L=null;k(!1)},50);document.execCommand("copy");e.remove()})}
7
+ 'use strict';var d=require("@lexical/html"),n=require("@lexical/selection"),r=require("@lexical/utils"),t=require("lexical");function z(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
+ function A(a){let b=t.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return t.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?"":d.$generateHtmlFromNodes(a,b)}function B(a){let b=t.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return t.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?null:JSON.stringify(C(a,b))}
9
+ function D(a,b,c){(t.DEPRECATED_$isGridSelection(c)||null!==r.$findMatchingParent(c.anchor.getNode(),f=>t.DEPRECATED_$isGridCellNode(f))&&null!==r.$findMatchingParent(c.focus.getNode(),f=>t.DEPRECATED_$isGridCellNode(f)))&&1===b.length&&t.DEPRECATED_$isGridNode(b[0])?E(b,c,!1,a):H(b,c)}
10
+ function H(a,b){let c=[],f=null;for(let e=0;e<a.length;e++){let g=a[e],h=t.$isLineBreakNode(g);if(h||t.$isDecoratorNode(g)&&g.isInline()||t.$isElementNode(g)&&g.isInline()||t.$isTextNode(g)||g.isParentRequired()){if(null===f&&(f=g.createParentElementNode(),c.push(f),h))continue;null!==f&&f.append(g)}else c.push(g),f=null}t.$isRangeSelection(b)?b.insertNodes(c):t.DEPRECATED_$isGridSelection(b)&&(a=b.anchor.getNode(),t.DEPRECATED_$isGridCellNode(a)||z(41),a.append(...c))}
11
+ function E(a,b,c,f){1===a.length&&t.DEPRECATED_$isGridNode(a[0])||z(42);var e=a[0];a=e.getChildren();c=e.getFirstChildOrThrow().getChildrenSize();var g=e.getChildrenSize(),h=r.$findMatchingParent(b.anchor.getNode(),l=>t.DEPRECATED_$isGridCellNode(l));b=(e=h&&r.$findMatchingParent(h,l=>t.DEPRECATED_$isGridRowNode(l)))&&r.$findMatchingParent(e,l=>t.DEPRECATED_$isGridNode(l));t.DEPRECATED_$isGridCellNode(h)&&t.DEPRECATED_$isGridRowNode(e)&&t.DEPRECATED_$isGridNode(b)||z(43);var k=e.getIndexWithinParent(),
12
+ p=Math.min(b.getChildrenSize()-1,k+g-1);g=h.getIndexWithinParent();h=Math.min(e.getChildrenSize()-1,g+c-1);c=Math.min(g,h);e=Math.min(k,p);g=Math.max(g,h);k=Math.max(k,p);p=b.getChildren();h=0;let m,q;for(let l=e;l<=k;l++){var x=p[l];t.DEPRECATED_$isGridRowNode(x)||z(24);var y=a[h];t.DEPRECATED_$isGridRowNode(y)||z(24);x=x.getChildren();y=y.getChildren();let F=0;for(let u=c;u<=g;u++){let v=x[u];t.DEPRECATED_$isGridCellNode(v)||z(25);let G=y[F];t.DEPRECATED_$isGridCellNode(G)||z(25);l===e&&u===c?m=
13
+ v.getKey():l===k&&u===g&&(q=v.getKey());let M=v.getChildren();G.getChildren().forEach(w=>{t.$isTextNode(w)&&t.$createParagraphNode().append(w);v.append(w)});M.forEach(w=>w.remove());F++}h++}m&&q&&(a=t.DEPRECATED_$createGridSelection(),a.set(b.getKey(),m,q),t.$setSelection(a),f.dispatchCommand(t.SELECTION_CHANGE_COMMAND,void 0))}
14
+ function I(a,b,c,f=[]){let e=null!=b?c.isSelected(b):!0,g=t.$isElementNode(c)&&c.excludeFromCopy("html");var h=c;if(null!==b){var k=n.$cloneWithProperties(c);h=k=t.$isTextNode(k)&&null!=b?n.$sliceSelectedTextNodeContent(b,k):k}let p=t.$isElementNode(h)?h.getChildren():[];var m=h;k=m.exportJSON();k.type!==m.constructor.getType()&&z(58);var q=k.children;t.$isElementNode(m)&&(Array.isArray(q)||z(59));t.$isTextNode(h)&&(h=h.__text,0<h.length?k.text=h:e=!1);for(h=0;h<p.length;h++)m=p[h],q=I(a,b,m,k.children),
15
+ !e&&t.$isElementNode(c)&&q&&c.extractWithChild(m,b,"clone")&&(e=!0);if(e&&!g)f.push(k);else if(Array.isArray(k.children))for(a=0;a<k.children.length;a++)f.push(k.children[a]);return e}function C(a,b){let c=[],f=t.$getRoot().getChildren();for(let e=0;e<f.length;e++)I(a,b,f[e],c);return{namespace:a._config.namespace,nodes:c}}function J(a){let b=[];for(let c=0;c<a.length;c++){let f=t.$parseSerializedNode(a[c]);t.$isTextNode(f)&&n.$addNodeStyle(f);b.push(f)}return b}let K=null;
16
+ function L(a,b){var c=window.getSelection();if(!c)return!1;var f=c.anchorNode;c=c.focusNode;if(null!==f&&null!==c&&!t.isSelectionWithinEditor(a,f,c))return!1;b.preventDefault();b=b.clipboardData;f=t.$getSelection();if(null===b||null===f)return!1;c=A(a);a=B(a);let e="";null!==f&&(e=f.getTextContent());null!==c&&b.setData("text/html",c);null!==a&&b.setData("application/x-lexical-editor",a);b.setData("text/plain",e);return!0}exports.$generateJSONFromSelectedNodes=C;
17
+ exports.$generateNodesFromSerializedNodes=J;exports.$getHtmlContent=A;exports.$getLexicalContent=B;exports.$insertDataTransferForPlainText=function(a,b){a=a.getData("text/plain");null!=a&&b.insertRawText(a)};
18
+ exports.$insertDataTransferForRichText=function(a,b,c){var f=a.getData("application/x-lexical-editor");if(f)try{let g=JSON.parse(f);if(g.namespace===c._config.namespace&&Array.isArray(g.nodes)){let h=J(g.nodes);return D(c,h,b)}}catch{}if(f=a.getData("text/html"))try{var e=(new DOMParser).parseFromString(f,"text/html");let g=d.$generateNodesFromDOM(c,e);return D(c,g,b)}catch{}a=a.getData("text/plain");if(null!=a)if(t.$isRangeSelection(b))for(a=a.split(/\r?\n/),c=a.length,e=0;e<c;e++)b.insertText(a[e]),
19
+ e<c-1&&b.insertParagraph();else b.insertRawText(a)};exports.$insertGeneratedNodes=D;
20
+ exports.copyToClipboard__EXPERIMENTAL=async function(a,b){if(null!==K)return!1;if(null!==b)return new Promise(g=>{a.update(()=>{g(L(a,b))})});var c=a.getRootElement();let f=document.getSelection();if(null===c||null===f)return!1;let e=document.createElement("span");e.style.cssText="position: fixed; top: -1000px;";e.append(document.createTextNode("#"));c.append(e);c=new Range;c.setStart(e,0);c.setEnd(e,1);f.removeAllRanges();f.addRange(c);return new Promise(g=>{let h=a.registerCommand(t.COPY_COMMAND,
21
+ k=>{k instanceof ClipboardEvent&&(h(),null!==K&&(window.clearTimeout(K),K=null),g(L(a,k)));return!0},t.COMMAND_PRIORITY_CRITICAL);K=window.setTimeout(()=>{h();K=null;g(!1)},50);document.execCommand("copy");e.remove()})}
package/package.json CHANGED
@@ -9,16 +9,16 @@
9
9
  "paste"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.7.6",
12
+ "version": "0.7.8",
13
13
  "main": "LexicalClipboard.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.7.6"
15
+ "lexical": "0.7.8"
16
16
  },
17
17
  "dependencies": {
18
- "@lexical/utils": "0.7.6",
19
- "@lexical/list": "0.7.6",
20
- "@lexical/selection": "0.7.6",
21
- "@lexical/html": "0.7.6"
18
+ "@lexical/utils": "0.7.8",
19
+ "@lexical/list": "0.7.8",
20
+ "@lexical/selection": "0.7.8",
21
+ "@lexical/html": "0.7.8"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",