@lexical/clipboard 0.3.3 → 0.3.4

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,6 +7,7 @@
7
7
  'use strict';
8
8
 
9
9
  var html = require('@lexical/html');
10
+ var list = require('@lexical/list');
10
11
  var selection = require('@lexical/selection');
11
12
  var utils = require('@lexical/utils');
12
13
  var lexical = require('lexical');
@@ -88,43 +89,54 @@ function $insertGeneratedNodes(editor, nodes, selection) {
88
89
  return;
89
90
  }
90
91
 
91
- $basicInsertStrategy(nodes, selection, false);
92
+ $basicInsertStrategy(nodes, selection);
92
93
  return;
93
94
  }
94
95
 
95
- function $basicInsertStrategy(nodes, selection, isFromLexical) {
96
- let nodesToInsert;
97
-
98
- if (!isFromLexical) {
99
- // Wrap text and inline nodes in paragraph nodes so we have all blocks at the top-level
100
- const topLevelBlocks = [];
101
- let currentBlock = null;
96
+ function $basicInsertStrategy(nodes, selection) {
97
+ // Wrap text and inline nodes in paragraph nodes so we have all blocks at the top-level
98
+ const topLevelBlocks = [];
99
+ let currentBlock = null;
100
+ let list$1 = null;
101
+
102
+ for (let i = 0; i < nodes.length; i++) {
103
+ const node = nodes[i];
104
+ /**
105
+ * There's no good way to add this to importDOM or importJSON directly,
106
+ * so this is here in order to safely correct faulty clipboard data
107
+ * that we can't control and avoid crashing the app.
108
+ * https://github.com/facebook/lexical/issues/2405
109
+ */
110
+
111
+ if (list.$isListItemNode(node)) {
112
+ if (list$1 == null) {
113
+ list$1 = list.$createListNode('bullet');
114
+ topLevelBlocks.push(list$1);
115
+ }
102
116
 
103
- for (let i = 0; i < nodes.length; i++) {
104
- const node = nodes[i];
117
+ list$1.append(node);
118
+ continue;
119
+ } else if (list$1 != null) {
120
+ list$1 = null;
121
+ }
105
122
 
106
- if (lexical.$isDecoratorNode(node) && !node.isTopLevel() || lexical.$isElementNode(node) && node.isInline() || lexical.$isTextNode(node) || lexical.$isLineBreakNode(node)) {
107
- if (currentBlock === null) {
108
- currentBlock = lexical.$createParagraphNode();
109
- topLevelBlocks.push(currentBlock);
110
- }
123
+ if (lexical.$isDecoratorNode(node) && !node.isTopLevel() || lexical.$isElementNode(node) && node.isInline() || lexical.$isTextNode(node) || lexical.$isLineBreakNode(node)) {
124
+ if (currentBlock === null) {
125
+ currentBlock = lexical.$createParagraphNode();
126
+ topLevelBlocks.push(currentBlock);
127
+ }
111
128
 
112
- if (currentBlock !== null) {
113
- currentBlock.append(node);
114
- }
115
- } else {
116
- topLevelBlocks.push(node);
117
- currentBlock = null;
129
+ if (currentBlock !== null) {
130
+ currentBlock.append(node);
118
131
  }
132
+ } else {
133
+ topLevelBlocks.push(node);
134
+ currentBlock = null;
119
135
  }
120
-
121
- nodesToInsert = topLevelBlocks;
122
- } else {
123
- nodesToInsert = nodes;
124
136
  }
125
137
 
126
138
  if (lexical.$isRangeSelection(selection)) {
127
- selection.insertNodes(nodesToInsert);
139
+ selection.insertNodes(topLevelBlocks);
128
140
  } else if (lexical.$isGridSelection(selection)) {
129
141
  // If there's an active grid selection and a non grid is pasted, add to the anchor.
130
142
  const anchorCell = selection.anchor.getNode();
@@ -135,7 +147,7 @@ function $basicInsertStrategy(nodes, selection, isFromLexical) {
135
147
  }
136
148
  }
137
149
 
138
- anchorCell.append(...nodesToInsert);
150
+ anchorCell.append(...topLevelBlocks);
139
151
  }
140
152
  }
141
153
 
@@ -325,7 +337,13 @@ function $generateNodesFromSerializedNodes(serializedNodes) {
325
337
 
326
338
  for (let i = 0; i < serializedNodes.length; i++) {
327
339
  const serializedNode = serializedNodes[i];
328
- nodes.push(lexical.$parseSerializedNode(serializedNode));
340
+ const node = lexical.$parseSerializedNode(serializedNode);
341
+
342
+ if (lexical.$isTextNode(node)) {
343
+ selection.$addNodeStyle(node);
344
+ }
345
+
346
+ nodes.push(node);
329
347
  }
330
348
 
331
349
  return nodes;
@@ -20,7 +20,7 @@ import type {
20
20
 
21
21
  declare export function $insertDataTransferForRichText(
22
22
  dataTransfer: DataTransfer,
23
- selection: RangeSelection | GridSelection | NodeSelection,
23
+ selection: RangeSelection | GridSelection,
24
24
  editor: LexicalEditor,
25
25
  ): void;
26
26
 
@@ -4,13 +4,14 @@
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/selection"),t=require("@lexical/utils"),y=require("lexical");function z(c){throw Error(`Minified Lexical error #${c}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function A(c,a){c=c.getData("text/plain");null!=c&&a.insertRawText(c)}
8
- function B(c,a,d){(y.$isGridSelection(d)||null!==t.$findMatchingParent(d.anchor.getNode(),h=>y.$isGridCellNode(h))&&null!==t.$findMatchingParent(d.focus.getNode(),h=>y.$isGridCellNode(h)))&&1===a.length&&y.$isGridNode(a[0])?E(a,d,!1,c):F(a,d,!1)}
9
- function F(c,a,d){if(!d){d=[];let h=null;for(let g=0;g<c.length;g++){let f=c[g];y.$isDecoratorNode(f)&&!f.isTopLevel()||y.$isElementNode(f)&&f.isInline()||y.$isTextNode(f)||y.$isLineBreakNode(f)?(null===h&&(h=y.$createParagraphNode(),d.push(h)),null!==h&&h.append(f)):(d.push(f),h=null)}c=d}y.$isRangeSelection(a)?a.insertNodes(c):y.$isGridSelection(a)&&(a=a.anchor.getNode(),y.$isGridCellNode(a)||z(41),a.append(...c))}
10
- function E(c,a,d,h){1===c.length&&y.$isGridNode(c[0])||z(42);var g=c[0];c=g.getChildren();d=g.getFirstChildOrThrow().getChildrenSize();var f=g.getChildrenSize(),e=t.$findMatchingParent(a.anchor.getNode(),l=>y.$isGridCellNode(l));a=(g=e&&t.$findMatchingParent(e,l=>y.$isGridRowNode(l)))&&t.$findMatchingParent(g,l=>y.$isGridNode(l));y.$isGridCellNode(e)&&y.$isGridRowNode(g)&&y.$isGridNode(a)||z(43);var m=g.getIndexWithinParent(),n=Math.min(a.getChildrenSize()-1,m+f-1);f=e.getIndexWithinParent();e=Math.min(g.getChildrenSize()-
11
- 1,f+d-1);d=Math.min(f,e);g=Math.min(m,n);f=Math.max(f,e);m=Math.max(m,n);n=a.getChildren();e=0;let k,q;for(let l=g;l<=m;l++){var r=n[l];y.$isGridRowNode(r)||z(24);var x=c[e];y.$isGridRowNode(x)||z(24);r=r.getChildren();x=x.getChildren();let C=0;for(let u=d;u<=f;u++){let v=r[u];y.$isGridCellNode(v)||z(25);let D=x[C];y.$isGridCellNode(D)||z(25);l===g&&u===d?k=v.getKey():l===m&&u===f&&(q=v.getKey());let H=v.getChildren();D.getChildren().forEach(w=>{y.$isTextNode(w)&&y.$createParagraphNode().append(w);
12
- v.append(w)});H.forEach(w=>w.remove());C++}e++}k&&q&&(c=y.$createGridSelection(),c.set(a.getKey(),k,q),y.$setSelection(c),h.dispatchCommand(y.SELECTION_CHANGE_COMMAND,void 0))}
13
- function G(c,a,d,h){let g=null!=a?d.isSelected():!0,f=y.$isElementNode(d)&&d.excludeFromCopy("html");var e=p.$cloneWithProperties(d);e=y.$isTextNode(e)&&null!=a?p.$sliceSelectedTextNodeContent(a,e):e;let m=y.$isElementNode(e)?e.getChildren():[];var n=e;let k=n.exportJSON();var q=n.constructor;k.type!==q.getType()&&z(58,q.name);let r=k.children;y.$isElementNode(n)&&(Array.isArray(r)||z(59,q.name));y.$isTextNode(e)&&(k.text=e.__text);for(e=0;e<m.length;e++)n=m[e],q=G(c,a,n,k.children),!g&&y.$isElementNode(d)&&
14
- q&&d.extractWithChild(n,a,"clone")&&(g=!0);if(g&&!f)h.push(k);else if(Array.isArray(k.children))for(c=0;c<k.children.length;c++)h.push(k.children[c]);return g}exports.$getHtmlContent=function(c){let a=y.$getSelection();if(null==a)throw Error("Expected valid LexicalSelection");return y.$isRangeSelection(a)&&a.isCollapsed()||0===a.getNodes().length?null:b.$generateHtmlFromNodes(c,a)};
15
- exports.$getLexicalContent=function(c){let a=y.$getSelection();if(null==a)throw Error("Expected valid LexicalSelection");if(y.$isRangeSelection(a)&&a.isCollapsed()||0===a.getNodes().length)return null;var d=JSON,h=d.stringify;let g=[],f=y.$getRoot().getChildren();for(let e=0;e<f.length;e++)G(c,a,f[e],g);return h.call(d,{namespace:c._config.namespace,nodes:g})};exports.$insertDataTransferForPlainText=A;
16
- exports.$insertDataTransferForRichText=function(c,a,d){let h=c.getData("text/html");var g=c.getData("application/x-lexical-editor");if(g)try{var f=JSON.parse(g);if(f.namespace===d._config.namespace&&Array.isArray(f.nodes)){var e=f.nodes;g=[];for(f=0;f<e.length;f++)g.push(y.$parseSerializedNode(e[f]));return B(d,g,a)}}catch{}if(h)try{let m=(new DOMParser).parseFromString(h,"text/html");return B(d,b.$generateNodesFromDOM(d,m),a)}catch{}A(c,a)}
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)}
package/clipboard.d.ts ADDED
@@ -0,0 +1,23 @@
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
+ import type { GridSelection, LexicalEditor, LexicalNode, NodeSelection, RangeSelection } from 'lexical';
9
+ export declare function $getHtmlContent(editor: LexicalEditor): string | null;
10
+ export declare function $getLexicalContent(editor: LexicalEditor): string | null;
11
+ export declare function $insertDataTransferForPlainText(dataTransfer: DataTransfer, selection: RangeSelection | GridSelection): void;
12
+ export declare function $insertDataTransferForRichText(dataTransfer: DataTransfer, selection: RangeSelection | GridSelection, editor: LexicalEditor): void;
13
+ interface BaseSerializedNode {
14
+ children?: Array<BaseSerializedNode>;
15
+ type: string;
16
+ version: number;
17
+ }
18
+ export declare function $generateJSONFromSelectedNodes<SerializedNode>(editor: LexicalEditor, selection: RangeSelection | NodeSelection | GridSelection | null): {
19
+ namespace: string;
20
+ nodes: Array<SerializedNode>;
21
+ };
22
+ export declare function $generateNodesFromSerializedNodes(serializedNodes: Array<BaseSerializedNode>): Array<LexicalNode>;
23
+ export {};
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
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
+ import { $getHtmlContent, $getLexicalContent, $insertDataTransferForPlainText, $insertDataTransferForRichText } from './clipboard';
9
+ export { $getHtmlContent, $getLexicalContent, $insertDataTransferForPlainText, $insertDataTransferForRichText, };
package/package.json CHANGED
@@ -9,15 +9,15 @@
9
9
  "paste"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.3.3",
12
+ "version": "0.3.4",
13
13
  "main": "LexicalClipboard.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.3.3"
15
+ "lexical": "0.3.4"
16
16
  },
17
17
  "dependencies": {
18
- "@lexical/utils": "0.3.3",
19
- "@lexical/selection": "0.3.3",
20
- "@lexical/html": "0.3.3"
18
+ "@lexical/utils": "0.3.4",
19
+ "@lexical/selection": "0.3.4",
20
+ "@lexical/html": "0.3.4"
21
21
  },
22
22
  "repository": {
23
23
  "type": "git",
@@ -1,35 +0,0 @@
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 {
10
- GridSelection,
11
- LexicalEditor,
12
- NodeSelection,
13
- RangeSelection,
14
- } from 'lexical';
15
-
16
- /*
17
- * Rich Text
18
- */
19
-
20
- export function $insertDataTransferForRichText(
21
- dataTransfer: DataTransfer,
22
- selection: RangeSelection | GridSelection | NodeSelection,
23
- editor: LexicalEditor,
24
- ): void;
25
-
26
- export function $getHtmlContent(editor: LexicalEditor): string | null;
27
- export function $getLexicalContent(editor: LexicalEditor): string | null;
28
- /*
29
- * Plain Text
30
- */
31
-
32
- export function $insertDataTransferForPlainText(
33
- dataTransfer: DataTransfer,
34
- selection: RangeSelection,
35
- ): void;