@lexical/clipboard 0.12.5 → 0.12.6

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.
@@ -164,116 +164,24 @@ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
164
164
  * @param selection The selection to insert the nodes into.
165
165
  */
166
166
  function $insertGeneratedNodes(editor, nodes, selection) {
167
- const isGridSelection = lexical.DEPRECATED_$isGridSelection(selection);
168
- const isRangeSelection = lexical.$isRangeSelection(selection);
169
- const isSelectionInsideOfGrid = isGridSelection || isRangeSelection && utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.DEPRECATED_$isGridCellNode(n)) !== null && utils.$findMatchingParent(selection.focus.getNode(), n => lexical.DEPRECATED_$isGridCellNode(n)) !== null;
170
- if (isSelectionInsideOfGrid && nodes.length === 1 && lexical.DEPRECATED_$isGridNode(nodes[0])) {
171
- $mergeGridNodesStrategy(nodes, selection, false, editor);
172
- return;
167
+ if (!editor.dispatchCommand(lexical.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, {
168
+ nodes,
169
+ selection
170
+ })) {
171
+ selection.insertNodes(nodes);
173
172
  }
174
- selection.insertNodes(nodes);
175
173
  return;
176
174
  }
177
- function $mergeGridNodesStrategy(nodes, selection, isFromLexical, editor) {
178
- if (nodes.length !== 1 || !lexical.DEPRECATED_$isGridNode(nodes[0])) {
179
- {
180
- throw Error(`$mergeGridNodesStrategy: Expected Grid insertion.`);
181
- }
182
- }
183
- const newGrid = nodes[0];
184
- const newGridRows = newGrid.getChildren();
185
- const newColumnCount = newGrid.getFirstChildOrThrow().getChildrenSize();
186
- const newRowCount = newGrid.getChildrenSize();
187
- const gridCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.DEPRECATED_$isGridCellNode(n));
188
- const gridRowNode = gridCellNode && utils.$findMatchingParent(gridCellNode, n => lexical.DEPRECATED_$isGridRowNode(n));
189
- const gridNode = gridRowNode && utils.$findMatchingParent(gridRowNode, n => lexical.DEPRECATED_$isGridNode(n));
190
- if (!lexical.DEPRECATED_$isGridCellNode(gridCellNode) || !lexical.DEPRECATED_$isGridRowNode(gridRowNode) || !lexical.DEPRECATED_$isGridNode(gridNode)) {
191
- {
192
- throw Error(`$mergeGridNodesStrategy: Expected selection to be inside of a Grid.`);
193
- }
194
- }
195
- const startY = gridRowNode.getIndexWithinParent();
196
- const stopY = Math.min(gridNode.getChildrenSize() - 1, startY + newRowCount - 1);
197
- const startX = gridCellNode.getIndexWithinParent();
198
- const stopX = Math.min(gridRowNode.getChildrenSize() - 1, startX + newColumnCount - 1);
199
- const fromX = Math.min(startX, stopX);
200
- const fromY = Math.min(startY, stopY);
201
- const toX = Math.max(startX, stopX);
202
- const toY = Math.max(startY, stopY);
203
- const gridRowNodes = gridNode.getChildren();
204
- let newRowIdx = 0;
205
- let newAnchorCellKey;
206
- let newFocusCellKey;
207
- for (let r = fromY; r <= toY; r++) {
208
- const currentGridRowNode = gridRowNodes[r];
209
- if (!lexical.DEPRECATED_$isGridRowNode(currentGridRowNode)) {
210
- {
211
- throw Error(`getNodes: expected to find GridRowNode`);
212
- }
213
- }
214
- const newGridRowNode = newGridRows[newRowIdx];
215
- if (!lexical.DEPRECATED_$isGridRowNode(newGridRowNode)) {
216
- {
217
- throw Error(`getNodes: expected to find GridRowNode`);
218
- }
219
- }
220
- const gridCellNodes = currentGridRowNode.getChildren();
221
- const newGridCellNodes = newGridRowNode.getChildren();
222
- let newColumnIdx = 0;
223
- for (let c = fromX; c <= toX; c++) {
224
- const currentGridCellNode = gridCellNodes[c];
225
- if (!lexical.DEPRECATED_$isGridCellNode(currentGridCellNode)) {
226
- {
227
- throw Error(`getNodes: expected to find GridCellNode`);
228
- }
229
- }
230
- const newGridCellNode = newGridCellNodes[newColumnIdx];
231
- if (!lexical.DEPRECATED_$isGridCellNode(newGridCellNode)) {
232
- {
233
- throw Error(`getNodes: expected to find GridCellNode`);
234
- }
235
- }
236
- if (r === fromY && c === fromX) {
237
- newAnchorCellKey = currentGridCellNode.getKey();
238
- } else if (r === toY && c === toX) {
239
- newFocusCellKey = currentGridCellNode.getKey();
240
- }
241
- const originalChildren = currentGridCellNode.getChildren();
242
- newGridCellNode.getChildren().forEach(child => {
243
- if (lexical.$isTextNode(child)) {
244
- const paragraphNode = lexical.$createParagraphNode();
245
- paragraphNode.append(child);
246
- currentGridCellNode.append(child);
247
- } else {
248
- currentGridCellNode.append(child);
249
- }
250
- });
251
- originalChildren.forEach(n => n.remove());
252
- newColumnIdx++;
253
- }
254
- newRowIdx++;
255
- }
256
- if (newAnchorCellKey && newFocusCellKey) {
257
- const newGridSelection = lexical.DEPRECATED_$createGridSelection();
258
- newGridSelection.set(gridNode.getKey(), newAnchorCellKey, newFocusCellKey);
259
- lexical.$setSelection(newGridSelection);
260
- editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
261
- }
262
- }
263
175
  function exportNodeToJSON(node) {
264
176
  const serializedNode = node.exportJSON();
265
177
  const nodeClass = node.constructor;
266
-
267
- // @ts-expect-error TODO Replace Class utility type with InstanceType
268
178
  if (serializedNode.type !== nodeClass.getType()) {
269
179
  {
270
180
  throw Error(`LexicalNode: Node ${nodeClass.name} does not implement .exportJSON().`);
271
181
  }
272
182
  }
273
-
274
- // @ts-expect-error TODO Replace Class utility type with InstanceType
275
- const serializedChildren = serializedNode.children;
276
183
  if (lexical.$isElementNode(node)) {
184
+ const serializedChildren = serializedNode.children;
277
185
  if (!Array.isArray(serializedChildren)) {
278
186
  {
279
187
  throw Error(`LexicalNode: Node ${nodeClass.name} is an element but .exportJSON() does not have a children array.`);
@@ -11,7 +11,6 @@ import type {
11
11
  BaseSelection,
12
12
  LexicalEditor,
13
13
  INTERNAL_PointSelection,
14
- GridSelection,
15
14
  RangeSelection,
16
15
  LexicalNode,
17
16
  } from 'lexical';
@@ -4,18 +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 d=require("@lexical/html"),q=require("@lexical/selection"),r=require("@lexical/utils"),u=require("lexical");function z(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
- let A="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement;function B(a){let b=u.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return u.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?"":d.$generateHtmlFromNodes(a,b)}
9
- function C(a){let b=u.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return u.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?null:JSON.stringify(D(a,b))}
10
- function E(a,b,c){let g=u.DEPRECATED_$isGridSelection(c),e=u.$isRangeSelection(c);(g||e&&null!==r.$findMatchingParent(c.anchor.getNode(),h=>u.DEPRECATED_$isGridCellNode(h))&&null!==r.$findMatchingParent(c.focus.getNode(),h=>u.DEPRECATED_$isGridCellNode(h)))&&1===b.length&&u.DEPRECATED_$isGridNode(b[0])?F(b,c,!1,a):c.insertNodes(b)}
11
- function F(a,b,c,g){1===a.length&&u.DEPRECATED_$isGridNode(a[0])||z(42);var e=a[0];a=e.getChildren();c=e.getFirstChildOrThrow().getChildrenSize();var h=e.getChildrenSize(),f=r.$findMatchingParent(b.anchor.getNode(),l=>u.DEPRECATED_$isGridCellNode(l));b=(e=f&&r.$findMatchingParent(f,l=>u.DEPRECATED_$isGridRowNode(l)))&&r.$findMatchingParent(e,l=>u.DEPRECATED_$isGridNode(l));u.DEPRECATED_$isGridCellNode(f)&&u.DEPRECATED_$isGridRowNode(e)&&u.DEPRECATED_$isGridNode(b)||z(43);var k=e.getIndexWithinParent(),
12
- m=Math.min(b.getChildrenSize()-1,k+h-1);h=f.getIndexWithinParent();f=Math.min(e.getChildrenSize()-1,h+c-1);c=Math.min(h,f);e=Math.min(k,m);h=Math.max(h,f);k=Math.max(k,m);m=b.getChildren();f=0;let n,p;for(let l=e;l<=k;l++){var t=m[l];u.DEPRECATED_$isGridRowNode(t)||z(24);var y=a[f];u.DEPRECATED_$isGridRowNode(y)||z(24);t=t.getChildren();y=y.getChildren();let G=0;for(let v=c;v<=h;v++){let w=t[v];u.DEPRECATED_$isGridCellNode(w)||z(25);let H=y[G];u.DEPRECATED_$isGridCellNode(H)||z(25);l===e&&v===c?n=
13
- w.getKey():l===k&&v===h&&(p=w.getKey());let M=w.getChildren();H.getChildren().forEach(x=>{u.$isTextNode(x)&&u.$createParagraphNode().append(x);w.append(x)});M.forEach(x=>x.remove());G++}f++}n&&p&&(a=u.DEPRECATED_$createGridSelection(),a.set(b.getKey(),n,p),u.$setSelection(a),g.dispatchCommand(u.SELECTION_CHANGE_COMMAND,void 0))}
14
- function I(a,b,c,g=[]){let e=null!=b?c.isSelected(b):!0,h=u.$isElementNode(c)&&c.excludeFromCopy("html");var f=c;if(null!==b){var k=q.$cloneWithProperties(c);f=k=u.$isTextNode(k)&&null!=b?q.$sliceSelectedTextNodeContent(b,k):k}let m=u.$isElementNode(f)?f.getChildren():[];var n=f;k=n.exportJSON();var p=n.constructor;k.type!==p.getType()&&z(58,p.name);let t=k.children;u.$isElementNode(n)&&(Array.isArray(t)||z(59,p.name));u.$isTextNode(f)&&(f=f.__text,0<f.length?k.text=f:e=!1);for(f=0;f<m.length;f++)n=
15
- m[f],p=I(a,b,n,k.children),!e&&u.$isElementNode(c)&&p&&c.extractWithChild(n,b,"clone")&&(e=!0);if(e&&!h)g.push(k);else if(Array.isArray(k.children))for(a=0;a<k.children.length;a++)g.push(k.children[a]);return e}function D(a,b){let c=[],g=u.$getRoot().getChildren();for(let e=0;e<g.length;e++)I(a,b,g[e],c);return{namespace:a._config.namespace,nodes:c}}function J(a){let b=[];for(let c=0;c<a.length;c++){let g=u.$parseSerializedNode(a[c]);u.$isTextNode(g)&&q.$addNodeStyle(g);b.push(g)}return b}let K=null;
16
- function L(a,b){var c=A?(a._window||window).getSelection():null;if(!c)return!1;var g=c.anchorNode;c=c.focusNode;if(null!==g&&null!==c&&!u.isSelectionWithinEditor(a,g,c))return!1;b.preventDefault();b=b.clipboardData;g=u.$getSelection();if(null===b||null===g)return!1;c=B(a);a=C(a);let e="";null!==g&&(e=g.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=D;
17
- exports.$generateNodesFromSerializedNodes=J;exports.$getHtmlContent=B;exports.$getLexicalContent=C;exports.$insertDataTransferForPlainText=function(a,b){a=a.getData("text/plain")||a.getData("text/uri-list");null!=a&&b.insertRawText(a)};
18
- exports.$insertDataTransferForRichText=function(a,b,c){var g=a.getData("application/x-lexical-editor");if(g)try{let h=JSON.parse(g);if(h.namespace===c._config.namespace&&Array.isArray(h.nodes)){let f=J(h.nodes);return E(c,f,b)}}catch(h){}if(g=a.getData("text/html"))try{var e=(new DOMParser).parseFromString(g,"text/html");let h=d.$generateNodesFromDOM(c,e);return E(c,h,b)}catch(h){}a=a.getData("text/plain")||a.getData("text/uri-list");if(null!=a)if(u.$isRangeSelection(b))for(a=a.split(/(\r?\n|\t)/),
19
- ""===a[a.length-1]&&a.pop(),c=0;c<a.length;c++)e=a[c],"\n"===e||"\r\n"===e?b.insertParagraph():"\t"===e?b.insertNodes([u.$createTabNode()]):b.insertText(e);else b.insertRawText(a)};exports.$insertGeneratedNodes=E;
20
- exports.copyToClipboard=async function(a,b){if(null!==K)return!1;if(null!==b)return new Promise(f=>{a.update(()=>{f(L(a,b))})});var c=a.getRootElement();let g=null==a._window?window.document:a._window.document,e=A?(a._window||window).getSelection():null;if(null===c||null===e)return!1;let h=g.createElement("span");h.style.cssText="position: fixed; top: -1000px;";h.append(g.createTextNode("#"));c.append(h);c=new Range;c.setStart(h,0);c.setEnd(h,1);e.removeAllRanges();e.addRange(c);return new Promise(f=>
21
- {let k=a.registerCommand(u.COPY_COMMAND,m=>{r.objectKlassEquals(m,ClipboardEvent)&&(k(),null!==K&&(window.clearTimeout(K),K=null),f(L(a,m)));return!0},u.COMMAND_PRIORITY_CRITICAL);K=window.setTimeout(()=>{k();K=null;f(!1)},50);g.execCommand("copy");h.remove()})}
7
+ 'use strict';var f=require("@lexical/html"),m=require("@lexical/selection"),q=require("@lexical/utils"),r=require("lexical");function t(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
+ let u="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement;function v(a){let b=r.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return r.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?"":f.$generateHtmlFromNodes(a,b)}
9
+ function w(a){let b=r.$getSelection();if(null==b)throw Error("Expected valid LexicalSelection");return r.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?null:JSON.stringify(x(a,b))}function y(a,b,c){a.dispatchCommand(r.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND,{nodes:b,selection:c})||c.insertNodes(b)}
10
+ function z(a,b,c,d=[]){let e=null!=b?c.isSelected(b):!0,h=r.$isElementNode(c)&&c.excludeFromCopy("html");var g=c;if(null!==b){var k=m.$cloneWithProperties(c);g=k=r.$isTextNode(k)&&null!=b?m.$sliceSelectedTextNodeContent(b,k):k}let n=r.$isElementNode(g)?g.getChildren():[];var l=g;k=l.exportJSON();var p=l.constructor;k.type!==p.getType()&&t(58,p.name);r.$isElementNode(l)&&(Array.isArray(k.children)||t(59,p.name));r.$isTextNode(g)&&(g=g.__text,0<g.length?k.text=g:e=!1);for(g=0;g<n.length;g++)l=n[g],
11
+ p=z(a,b,l,k.children),!e&&r.$isElementNode(c)&&p&&c.extractWithChild(l,b,"clone")&&(e=!0);if(e&&!h)d.push(k);else if(Array.isArray(k.children))for(a=0;a<k.children.length;a++)d.push(k.children[a]);return e}function x(a,b){let c=[],d=r.$getRoot().getChildren();for(let e=0;e<d.length;e++)z(a,b,d[e],c);return{namespace:a._config.namespace,nodes:c}}function A(a){let b=[];for(let c=0;c<a.length;c++){let d=r.$parseSerializedNode(a[c]);r.$isTextNode(d)&&m.$addNodeStyle(d);b.push(d)}return b}let B=null;
12
+ function C(a,b){var c=u?(a._window||window).getSelection():null;if(!c)return!1;var d=c.anchorNode;c=c.focusNode;if(null!==d&&null!==c&&!r.isSelectionWithinEditor(a,d,c))return!1;b.preventDefault();b=b.clipboardData;d=r.$getSelection();if(null===b||null===d)return!1;c=v(a);a=w(a);let e="";null!==d&&(e=d.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=x;
13
+ exports.$generateNodesFromSerializedNodes=A;exports.$getHtmlContent=v;exports.$getLexicalContent=w;exports.$insertDataTransferForPlainText=function(a,b){a=a.getData("text/plain")||a.getData("text/uri-list");null!=a&&b.insertRawText(a)};
14
+ exports.$insertDataTransferForRichText=function(a,b,c){var d=a.getData("application/x-lexical-editor");if(d)try{let h=JSON.parse(d);if(h.namespace===c._config.namespace&&Array.isArray(h.nodes)){let g=A(h.nodes);return y(c,g,b)}}catch(h){}if(d=a.getData("text/html"))try{var e=(new DOMParser).parseFromString(d,"text/html");let h=f.$generateNodesFromDOM(c,e);return y(c,h,b)}catch(h){}a=a.getData("text/plain")||a.getData("text/uri-list");if(null!=a)if(r.$isRangeSelection(b))for(a=a.split(/(\r?\n|\t)/),
15
+ ""===a[a.length-1]&&a.pop(),c=0;c<a.length;c++)e=a[c],"\n"===e||"\r\n"===e?b.insertParagraph():"\t"===e?b.insertNodes([r.$createTabNode()]):b.insertText(e);else b.insertRawText(a)};exports.$insertGeneratedNodes=y;
16
+ exports.copyToClipboard=async function(a,b){if(null!==B)return!1;if(null!==b)return new Promise(g=>{a.update(()=>{g(C(a,b))})});var c=a.getRootElement();let d=null==a._window?window.document:a._window.document,e=u?(a._window||window).getSelection():null;if(null===c||null===e)return!1;let h=d.createElement("span");h.style.cssText="position: fixed; top: -1000px;";h.append(d.createTextNode("#"));c.append(h);c=new Range;c.setStart(h,0);c.setEnd(h,1);e.removeAllRanges();e.addRange(c);return new Promise(g=>
17
+ {let k=a.registerCommand(r.COPY_COMMAND,n=>{q.objectKlassEquals(n,ClipboardEvent)&&(k(),null!==B&&(window.clearTimeout(B),B=null),g(C(a,n)));return!0},r.COMMAND_PRIORITY_CRITICAL);B=window.setTimeout(()=>{k();B=null;g(!1)},50);d.execCommand("copy");h.remove()})}
package/package.json CHANGED
@@ -9,16 +9,16 @@
9
9
  "paste"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.12.5",
12
+ "version": "0.12.6",
13
13
  "main": "LexicalClipboard.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.12.5"
15
+ "lexical": "0.12.6"
16
16
  },
17
17
  "dependencies": {
18
- "@lexical/utils": "0.12.5",
19
- "@lexical/list": "0.12.5",
20
- "@lexical/selection": "0.12.5",
21
- "@lexical/html": "0.12.5"
18
+ "@lexical/utils": "0.12.6",
19
+ "@lexical/list": "0.12.6",
20
+ "@lexical/selection": "0.12.6",
21
+ "@lexical/html": "0.12.6"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",