@lexical/clipboard 0.3.4 → 0.3.7

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.
@@ -55,7 +55,6 @@ function $insertDataTransferForPlainText(dataTransfer, selection) {
55
55
  }
56
56
  }
57
57
  function $insertDataTransferForRichText(dataTransfer, selection, editor) {
58
- const htmlString = dataTransfer.getData('text/html');
59
58
  const lexicalString = dataTransfer.getData('application/x-lexical-editor');
60
59
 
61
60
  if (lexicalString) {
@@ -70,15 +69,36 @@ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
70
69
  } catch {}
71
70
  }
72
71
 
72
+ const htmlString = dataTransfer.getData('text/html');
73
+
73
74
  if (htmlString) {
74
75
  try {
75
76
  const parser = new DOMParser();
76
77
  const dom = parser.parseFromString(htmlString, 'text/html');
77
78
  return $insertGeneratedNodes(editor, html.$generateNodesFromDOM(editor, dom), selection); // eslint-disable-next-line no-empty
78
79
  } catch {}
79
- }
80
+ } // Multi-line plain text in rich text mode pasted as separate paragrahs
81
+ // instead of single paragraph with linebreaks.
80
82
 
81
- $insertDataTransferForPlainText(dataTransfer, selection);
83
+
84
+ const text = dataTransfer.getData('text/plain');
85
+
86
+ if (text != null) {
87
+ if (lexical.$isRangeSelection(selection)) {
88
+ const lines = text.split(/\r?\n/);
89
+ const linesLength = lines.length;
90
+
91
+ for (let i = 0; i < linesLength; i++) {
92
+ selection.insertText(lines[i]);
93
+
94
+ if (i < linesLength - 1) {
95
+ selection.insertParagraph();
96
+ }
97
+ }
98
+ } else {
99
+ selection.insertRawText(text);
100
+ }
101
+ }
82
102
  }
83
103
 
84
104
  function $insertGeneratedNodes(editor, nodes, selection) {
@@ -278,7 +298,7 @@ function exportNodeToJSON(node) {
278
298
  return serializedNode;
279
299
  }
280
300
 
281
- function $appendNodesToJSON(editor, selection$1, currentNode, targetArray) {
301
+ function $appendNodesToJSON(editor, selection$1, currentNode, targetArray = []) {
282
302
  let shouldInclude = selection$1 != null ? currentNode.isSelected() : true;
283
303
  const shouldExclude = lexical.$isElementNode(currentNode) && currentNode.excludeFromCopy('html');
284
304
  let clone = selection.$cloneWithProperties(currentNode);
@@ -292,7 +312,6 @@ function $appendNodesToJSON(editor, selection$1, currentNode, targetArray) {
292
312
  // until then this hack will work for the selected text extract use case.
293
313
 
294
314
  if (lexical.$isTextNode(clone)) {
295
- // @ts-ignore
296
315
  serializedNode.text = clone.__text;
297
316
  }
298
317
 
@@ -12,6 +12,7 @@ import type {
12
12
  LexicalEditor,
13
13
  NodeSelection,
14
14
  RangeSelection,
15
+ LexicalNode,
15
16
  } from 'lexical';
16
17
 
17
18
  /*
@@ -32,7 +33,31 @@ declare export function $getLexicalContent(
32
33
  * Plain Text
33
34
  */
34
35
 
36
+ /*
37
+ * Export as JSON
38
+ */
39
+
35
40
  declare export function $insertDataTransferForPlainText(
36
41
  dataTransfer: DataTransfer,
37
42
  selection: RangeSelection,
38
43
  ): void;
44
+
45
+ interface BaseSerializedNode {
46
+ children?: Array<BaseSerializedNode>;
47
+ type: string;
48
+ version: number;
49
+ }
50
+
51
+ declare export function $generateJSONFromSelectedNodes<
52
+ SerializedNode: BaseSerializedNode,
53
+ >(
54
+ editor: LexicalEditor,
55
+ selection: RangeSelection | NodeSelection | GridSelection | null,
56
+ ): {
57
+ namespace: string,
58
+ nodes: Array<SerializedNode>,
59
+ };
60
+
61
+ declare export function $generateNodesFromSerializedNodes(
62
+ serializedNodes: Array<BaseSerializedNode>,
63
+ ): Array<LexicalNode>;
@@ -4,14 +4,15 @@
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 b=require("@lexical/html"),p=require("@lexical/list"),r=require("@lexical/selection"),y=require("@lexical/utils"),z=require("lexical");function A(d){throw Error(`Minified Lexical error #${d}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function B(d,c){d=d.getData("text/plain");null!=d&&c.insertRawText(d)}
8
- function C(d,c,e){(z.$isGridSelection(e)||null!==y.$findMatchingParent(e.anchor.getNode(),h=>z.$isGridCellNode(h))&&null!==y.$findMatchingParent(e.focus.getNode(),h=>z.$isGridCellNode(h)))&&1===c.length&&z.$isGridNode(c[0])?F(c,e,!1,d):G(c,e)}
9
- function G(d,c){let e=[],h=null,f=null;for(let g=0;g<d.length;g++){let a=d[g];p.$isListItemNode(a)?(null==f&&(f=p.$createListNode("bullet"),e.push(f)),f.append(a)):(null!=f&&(f=null),z.$isDecoratorNode(a)&&!a.isTopLevel()||z.$isElementNode(a)&&a.isInline()||z.$isTextNode(a)||z.$isLineBreakNode(a)?(null===h&&(h=z.$createParagraphNode(),e.push(h)),null!==h&&h.append(a)):(e.push(a),h=null))}z.$isRangeSelection(c)?c.insertNodes(e):z.$isGridSelection(c)&&(d=c.anchor.getNode(),z.$isGridCellNode(d)||A(41),
10
- d.append(...e))}
11
- function F(d,c,e,h){1===d.length&&z.$isGridNode(d[0])||A(42);var f=d[0];d=f.getChildren();e=f.getFirstChildOrThrow().getChildrenSize();var g=f.getChildrenSize(),a=y.$findMatchingParent(c.anchor.getNode(),m=>z.$isGridCellNode(m));c=(f=a&&y.$findMatchingParent(a,m=>z.$isGridRowNode(m)))&&y.$findMatchingParent(f,m=>z.$isGridNode(m));z.$isGridCellNode(a)&&z.$isGridRowNode(f)&&z.$isGridNode(c)||A(43);var k=f.getIndexWithinParent(),n=Math.min(c.getChildrenSize()-1,k+g-1);g=a.getIndexWithinParent();a=Math.min(f.getChildrenSize()-
12
- 1,g+e-1);e=Math.min(g,a);f=Math.min(k,n);g=Math.max(g,a);k=Math.max(k,n);n=c.getChildren();a=0;let l,q;for(let m=f;m<=k;m++){var w=n[m];z.$isGridRowNode(w)||A(24);var x=d[a];z.$isGridRowNode(x)||A(24);w=w.getChildren();x=x.getChildren();let D=0;for(let t=e;t<=g;t++){let u=w[t];z.$isGridCellNode(u)||A(25);let E=x[D];z.$isGridCellNode(E)||A(25);m===f&&t===e?l=u.getKey():m===k&&t===g&&(q=u.getKey());let I=u.getChildren();E.getChildren().forEach(v=>{z.$isTextNode(v)&&z.$createParagraphNode().append(v);
13
- u.append(v)});I.forEach(v=>v.remove());D++}a++}l&&q&&(d=z.$createGridSelection(),d.set(c.getKey(),l,q),z.$setSelection(d),h.dispatchCommand(z.SELECTION_CHANGE_COMMAND,void 0))}
14
- function H(d,c,e,h){let f=null!=c?e.isSelected():!0,g=z.$isElementNode(e)&&e.excludeFromCopy("html");var a=r.$cloneWithProperties(e);a=z.$isTextNode(a)&&null!=c?r.$sliceSelectedTextNodeContent(c,a):a;let k=z.$isElementNode(a)?a.getChildren():[];var n=a;let l=n.exportJSON();l.type!==n.constructor.getType()&&A(58);var q=l.children;z.$isElementNode(n)&&(Array.isArray(q)||A(59));z.$isTextNode(a)&&(l.text=a.__text);for(a=0;a<k.length;a++)n=k[a],q=H(d,c,n,l.children),!f&&z.$isElementNode(e)&&q&&e.extractWithChild(n,
15
- c,"clone")&&(f=!0);if(f&&!g)h.push(l);else if(Array.isArray(l.children))for(d=0;d<l.children.length;d++)h.push(l.children[d]);return f}exports.$getHtmlContent=function(d){let c=z.$getSelection();if(null==c)throw Error("Expected valid LexicalSelection");return z.$isRangeSelection(c)&&c.isCollapsed()||0===c.getNodes().length?null:b.$generateHtmlFromNodes(d,c)};
16
- exports.$getLexicalContent=function(d){let c=z.$getSelection();if(null==c)throw Error("Expected valid LexicalSelection");if(z.$isRangeSelection(c)&&c.isCollapsed()||0===c.getNodes().length)return null;var e=JSON,h=e.stringify;let f=[],g=z.$getRoot().getChildren();for(let a=0;a<g.length;a++)H(d,c,g[a],f);return h.call(e,{namespace:d._config.namespace,nodes:f})};exports.$insertDataTransferForPlainText=B;
17
- exports.$insertDataTransferForRichText=function(d,c,e){let h=d.getData("text/html");var f=d.getData("application/x-lexical-editor");if(f)try{var g=JSON.parse(f);if(g.namespace===e._config.namespace&&Array.isArray(g.nodes)){var a=g.nodes;f=[];for(g=0;g<a.length;g++){let k=z.$parseSerializedNode(a[g]);z.$isTextNode(k)&&r.$addNodeStyle(k);f.push(k)}return C(e,f,c)}}catch{}if(h)try{let k=(new DOMParser).parseFromString(h,"text/html");return C(e,b.$generateNodesFromDOM(e,k),c)}catch{}B(d,c)}
7
+ 'use strict';var d=require("@lexical/html"),p=require("@lexical/list"),r=require("@lexical/selection"),y=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,c,e){(z.$isGridSelection(e)||null!==y.$findMatchingParent(e.anchor.getNode(),g=>z.$isGridCellNode(g))&&null!==y.$findMatchingParent(e.focus.getNode(),g=>z.$isGridCellNode(g)))&&1===c.length&&z.$isGridNode(c[0])?E(c,e,!1,a):F(c,e)}
9
+ function F(a,c){let e=[],g=null,f=null;for(let h=0;h<a.length;h++){let b=a[h];p.$isListItemNode(b)?(null==f&&(f=p.$createListNode("bullet"),e.push(f)),f.append(b)):(null!=f&&(f=null),z.$isDecoratorNode(b)&&!b.isTopLevel()||z.$isElementNode(b)&&b.isInline()||z.$isTextNode(b)||z.$isLineBreakNode(b)?(null===g&&(g=z.$createParagraphNode(),e.push(g)),null!==g&&g.append(b)):(e.push(b),g=null))}z.$isRangeSelection(c)?c.insertNodes(e):z.$isGridSelection(c)&&(a=c.anchor.getNode(),z.$isGridCellNode(a)||A(41),
10
+ a.append(...e))}
11
+ function E(a,c,e,g){1===a.length&&z.$isGridNode(a[0])||A(42);var f=a[0];a=f.getChildren();e=f.getFirstChildOrThrow().getChildrenSize();var h=f.getChildrenSize(),b=y.$findMatchingParent(c.anchor.getNode(),m=>z.$isGridCellNode(m));c=(f=b&&y.$findMatchingParent(b,m=>z.$isGridRowNode(m)))&&y.$findMatchingParent(f,m=>z.$isGridNode(m));z.$isGridCellNode(b)&&z.$isGridRowNode(f)&&z.$isGridNode(c)||A(43);var k=f.getIndexWithinParent(),n=Math.min(c.getChildrenSize()-1,k+h-1);h=b.getIndexWithinParent();b=Math.min(f.getChildrenSize()-
12
+ 1,h+e-1);e=Math.min(h,b);f=Math.min(k,n);h=Math.max(h,b);k=Math.max(k,n);n=c.getChildren();b=0;let l,q;for(let m=f;m<=k;m++){var w=n[m];z.$isGridRowNode(w)||A(24);var x=a[b];z.$isGridRowNode(x)||A(24);w=w.getChildren();x=x.getChildren();let C=0;for(let t=e;t<=h;t++){let u=w[t];z.$isGridCellNode(u)||A(25);let D=x[C];z.$isGridCellNode(D)||A(25);m===f&&t===e?l=u.getKey():m===k&&t===h&&(q=u.getKey());let H=u.getChildren();D.getChildren().forEach(v=>{z.$isTextNode(v)&&z.$createParagraphNode().append(v);
13
+ u.append(v)});H.forEach(v=>v.remove());C++}b++}l&&q&&(a=z.$createGridSelection(),a.set(c.getKey(),l,q),z.$setSelection(a),g.dispatchCommand(z.SELECTION_CHANGE_COMMAND,void 0))}
14
+ function G(a,c,e,g=[]){let f=null!=c?e.isSelected():!0,h=z.$isElementNode(e)&&e.excludeFromCopy("html");var b=r.$cloneWithProperties(e);b=z.$isTextNode(b)&&null!=c?r.$sliceSelectedTextNodeContent(c,b):b;let k=z.$isElementNode(b)?b.getChildren():[];var n=b;let l=n.exportJSON();l.type!==n.constructor.getType()&&A(58);var q=l.children;z.$isElementNode(n)&&(Array.isArray(q)||A(59));z.$isTextNode(b)&&(l.text=b.__text);for(b=0;b<k.length;b++)n=k[b],q=G(a,c,n,l.children),!f&&z.$isElementNode(e)&&q&&e.extractWithChild(n,
15
+ c,"clone")&&(f=!0);if(f&&!h)g.push(l);else if(Array.isArray(l.children))for(a=0;a<l.children.length;a++)g.push(l.children[a]);return f}exports.$getHtmlContent=function(a){let c=z.$getSelection();if(null==c)throw Error("Expected valid LexicalSelection");return z.$isRangeSelection(c)&&c.isCollapsed()||0===c.getNodes().length?null:d.$generateHtmlFromNodes(a,c)};
16
+ exports.$getLexicalContent=function(a){let c=z.$getSelection();if(null==c)throw Error("Expected valid LexicalSelection");if(z.$isRangeSelection(c)&&c.isCollapsed()||0===c.getNodes().length)return null;var e=JSON,g=e.stringify;let f=[],h=z.$getRoot().getChildren();for(let b=0;b<h.length;b++)G(a,c,h[b],f);return g.call(e,{namespace:a._config.namespace,nodes:f})};exports.$insertDataTransferForPlainText=function(a,c){a=a.getData("text/plain");null!=a&&c.insertRawText(a)};
17
+ exports.$insertDataTransferForRichText=function(a,c,e){var g=a.getData("application/x-lexical-editor");if(g)try{var f=JSON.parse(g);if(f.namespace===e._config.namespace&&Array.isArray(f.nodes)){var h=f.nodes;g=[];for(f=0;f<h.length;f++){let k=z.$parseSerializedNode(h[f]);z.$isTextNode(k)&&r.$addNodeStyle(k);g.push(k)}return B(e,g,c)}}catch{}if(h=a.getData("text/html"))try{var b=(new DOMParser).parseFromString(h,"text/html");return B(e,d.$generateNodesFromDOM(e,b),c)}catch{}a=a.getData("text/plain");
18
+ if(null!=a)if(z.$isRangeSelection(c))for(a=a.split(/\r?\n/),e=a.length,b=0;b<e;b++)c.insertText(a[b]),b<e-1&&c.insertParagraph();else c.insertRawText(a)}
package/clipboard.d.ts CHANGED
@@ -15,7 +15,7 @@ interface BaseSerializedNode {
15
15
  type: string;
16
16
  version: number;
17
17
  }
18
- export declare function $generateJSONFromSelectedNodes<SerializedNode>(editor: LexicalEditor, selection: RangeSelection | NodeSelection | GridSelection | null): {
18
+ export declare function $generateJSONFromSelectedNodes<SerializedNode extends BaseSerializedNode>(editor: LexicalEditor, selection: RangeSelection | NodeSelection | GridSelection | null): {
19
19
  namespace: string;
20
20
  nodes: Array<SerializedNode>;
21
21
  };
package/package.json CHANGED
@@ -9,15 +9,16 @@
9
9
  "paste"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.3.4",
12
+ "version": "0.3.7",
13
13
  "main": "LexicalClipboard.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.3.4"
15
+ "lexical": "0.3.7"
16
16
  },
17
17
  "dependencies": {
18
- "@lexical/utils": "0.3.4",
19
- "@lexical/selection": "0.3.4",
20
- "@lexical/html": "0.3.4"
18
+ "@lexical/utils": "0.3.7",
19
+ "@lexical/list": "0.3.7",
20
+ "@lexical/selection": "0.3.7",
21
+ "@lexical/html": "0.3.7"
21
22
  },
22
23
  "repository": {
23
24
  "type": "git",