@lexical/clipboard 0.11.1 → 0.11.2

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.
@@ -18,6 +18,28 @@ var lexical = require('lexical');
18
18
  * LICENSE file in the root directory of this source tree.
19
19
  *
20
20
  */
21
+ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
22
+
23
+ /**
24
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
25
+ *
26
+ * This source code is licensed under the MIT license found in the
27
+ * LICENSE file in the root directory of this source tree.
28
+ *
29
+ */
30
+
31
+ const getDOMSelection = targetWindow => CAN_USE_DOM ? (targetWindow || window).getSelection() : null;
32
+ /**
33
+ * Returns the *currently selected* Lexical content as an HTML string, relying on the
34
+ * logic defined in the exportDOM methods on the LexicalNode classes. Note that
35
+ * this will not return the HTML content of the entire editor (unless all the content is included
36
+ * in the current selection).
37
+ *
38
+ * @param editor - LexicalEditor instance to get HTML content from
39
+ * @returns a string of HTML content
40
+ */
41
+
42
+
21
43
  function $getHtmlContent(editor) {
22
44
  const selection = lexical.$getSelection();
23
45
 
@@ -33,8 +55,16 @@ function $getHtmlContent(editor) {
33
55
  }
34
56
 
35
57
  return html.$generateHtmlFromNodes(editor, selection);
36
- } // TODO 0.6.0 Return a blank string instead
37
- // TODO 0.6.0 Rename to $getJSON
58
+ }
59
+ /**
60
+ * Returns the *currently selected* Lexical content as a JSON string, relying on the
61
+ * logic defined in the exportJSON methods on the LexicalNode classes. Note that
62
+ * this will not return the JSON content of the entire editor (unless all the content is included
63
+ * in the current selection).
64
+ *
65
+ * @param editor - LexicalEditor instance to get the JSON content from
66
+ * @returns
67
+ */
38
68
 
39
69
  function $getLexicalContent(editor) {
40
70
  const selection = lexical.$getSelection();
@@ -52,6 +82,15 @@ function $getLexicalContent(editor) {
52
82
 
53
83
  return JSON.stringify($generateJSONFromSelectedNodes(editor, selection));
54
84
  }
85
+ /**
86
+ * Attempts to insert content of the mime-types text/plain or text/uri-list from
87
+ * the provided DataTransfer object into the editor at the provided selection.
88
+ * text/uri-list is only used if text/plain is not also provided.
89
+ *
90
+ * @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
91
+ * @param selection the selection to use as the insertion point for the content in the DataTransfer object
92
+ */
93
+
55
94
  function $insertDataTransferForPlainText(dataTransfer, selection) {
56
95
  const text = dataTransfer.getData('text/plain') || dataTransfer.getData('text/uri-list');
57
96
 
@@ -59,6 +98,16 @@ function $insertDataTransferForPlainText(dataTransfer, selection) {
59
98
  selection.insertRawText(text);
60
99
  }
61
100
  }
101
+ /**
102
+ * Attempts to insert content of the mime-types application/x-lexical-editor, text/html,
103
+ * text/plain, or text/uri-list (in descending order of priority) from the provided DataTransfer
104
+ * object into the editor at the provided selection.
105
+ *
106
+ * @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
107
+ * @param selection the selection to use as the insertion point for the content in the DataTransfer object
108
+ * @param editor the LexicalEditor the content is being inserted into.
109
+ */
110
+
62
111
  function $insertDataTransferForRichText(dataTransfer, selection, editor) {
63
112
  const lexicalString = dataTransfer.getData('application/x-lexical-editor');
64
113
 
@@ -112,6 +161,17 @@ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
112
161
  }
113
162
  }
114
163
  }
164
+ /**
165
+ * Inserts Lexical nodes into the editor using different strategies depending on
166
+ * some simple selection-based heuristics. If you're looking for a generic way to
167
+ * to insert nodes into the editor at a specific selection point, you probably want
168
+ * {@link lexical.$insertNodes}
169
+ *
170
+ * @param editor LexicalEditor instance to insert the nodes into.
171
+ * @param nodes The nodes to insert.
172
+ * @param selection The selection to insert the nodes into.
173
+ */
174
+
115
175
  function $insertGeneratedNodes(editor, nodes, selection) {
116
176
  const isSelectionInsideOfGrid = lexical.DEPRECATED_$isGridSelection(selection) || utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.DEPRECATED_$isGridCellNode(n)) !== null && utils.$findMatchingParent(selection.focus.getNode(), n => lexical.DEPRECATED_$isGridCellNode(n)) !== null;
117
177
 
@@ -348,6 +408,14 @@ function $appendNodesToJSON(editor, selection$1, currentNode, targetArray = [])
348
408
  return shouldInclude;
349
409
  } // TODO why $ function with Editor instance?
350
410
 
411
+ /**
412
+ * Gets the Lexical JSON of the nodes inside the provided Selection.
413
+ *
414
+ * @param editor LexicalEditor to get the JSON content from.
415
+ * @param selection Selection to get the JSON content from.
416
+ * @returns an object with the editor namespace and a list of serializable nodes as JavaScript objects.
417
+ */
418
+
351
419
 
352
420
  function $generateJSONFromSelectedNodes(editor, selection) {
353
421
  const nodes = [];
@@ -364,6 +432,15 @@ function $generateJSONFromSelectedNodes(editor, selection) {
364
432
  nodes
365
433
  };
366
434
  }
435
+ /**
436
+ * This method takes an array of objects conforming to the BaseSeralizedNode interface and returns
437
+ * an Array containing instances of the corresponding LexicalNode classes registered on the editor.
438
+ * Normally, you'd get an Array of BaseSerialized nodes from {@link $generateJSONFromSelectedNodes}
439
+ *
440
+ * @param serializedNodes an Array of objects conforming to the BaseSerializedNode interface.
441
+ * @returns an Array of Lexical Node objects.
442
+ */
443
+
367
444
  function $generateNodesFromSerializedNodes(serializedNodes) {
368
445
  const nodes = [];
369
446
 
@@ -384,6 +461,16 @@ const EVENT_LATENCY = 50;
384
461
  let clipboardEventTimeout = null; // TODO custom selection
385
462
  // TODO potentially have a node customizable version for plain text
386
463
 
464
+ /**
465
+ * Copies the content of the current selection to the clipboard in
466
+ * text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
467
+ * formats.
468
+ *
469
+ * @param editor the LexicalEditor instance to copy content from
470
+ * @param event the native browser ClipboardEvent to add the content to.
471
+ * @returns
472
+ */
473
+
387
474
  async function copyToClipboard(editor, event) {
388
475
  if (clipboardEventTimeout !== null) {
389
476
  // Prevent weird race conditions that can happen when this function is run multiple times
@@ -400,15 +487,16 @@ async function copyToClipboard(editor, event) {
400
487
  }
401
488
 
402
489
  const rootElement = editor.getRootElement();
403
- const domSelection = document.getSelection();
490
+ const windowDocument = editor._window == null ? window.document : editor._window.document;
491
+ const domSelection = getDOMSelection(editor._window);
404
492
 
405
493
  if (rootElement === null || domSelection === null) {
406
494
  return false;
407
495
  }
408
496
 
409
- const element = document.createElement('span');
497
+ const element = windowDocument.createElement('span');
410
498
  element.style.cssText = 'position: fixed; top: -1000px;';
411
- element.append(document.createTextNode('#'));
499
+ element.append(windowDocument.createTextNode('#'));
412
500
  rootElement.append(element);
413
501
  const range = new Range();
414
502
  range.setStart(element, 0);
@@ -417,7 +505,7 @@ async function copyToClipboard(editor, event) {
417
505
  domSelection.addRange(range);
418
506
  return new Promise((resolve, reject) => {
419
507
  const removeListener = editor.registerCommand(lexical.COPY_COMMAND, secondEvent => {
420
- if (secondEvent instanceof ClipboardEvent) {
508
+ if (utils.objectKlassEquals(secondEvent, ClipboardEvent)) {
421
509
  removeListener();
422
510
 
423
511
  if (clipboardEventTimeout !== null) {
@@ -438,13 +526,13 @@ async function copyToClipboard(editor, event) {
438
526
  clipboardEventTimeout = null;
439
527
  resolve(false);
440
528
  }, EVENT_LATENCY);
441
- document.execCommand('copy');
529
+ windowDocument.execCommand('copy');
442
530
  element.remove();
443
531
  });
444
532
  } // TODO shouldn't pass editor (pass namespace directly)
445
533
 
446
534
  function $copyToClipboardEvent(editor, event) {
447
- const domSelection = window.getSelection();
535
+ const domSelection = getDOMSelection(editor._window);
448
536
 
449
537
  if (!domSelection) {
450
538
  return false;
@@ -4,18 +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/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 h=a[e],g=t.$isLineBreakNode(h);if(g||t.$isDecoratorNode(h)&&h.isInline()||t.$isElementNode(h)&&h.isInline()||t.$isTextNode(h)||h.isParentRequired()){if(null===f&&(f=h.createParentElementNode(),c.push(f),g))continue;null!==f&&f.append(h)}else c.push(h),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 h=e.getChildrenSize(),g=r.$findMatchingParent(b.anchor.getNode(),l=>t.DEPRECATED_$isGridCellNode(l));b=(e=g&&r.$findMatchingParent(g,l=>t.DEPRECATED_$isGridRowNode(l)))&&r.$findMatchingParent(e,l=>t.DEPRECATED_$isGridNode(l));t.DEPRECATED_$isGridCellNode(g)&&t.DEPRECATED_$isGridRowNode(e)&&t.DEPRECATED_$isGridNode(b)||z(43);var k=e.getIndexWithinParent(),
12
- p=Math.min(b.getChildrenSize()-1,k+h-1);h=g.getIndexWithinParent();g=Math.min(e.getChildrenSize()-1,h+c-1);c=Math.min(h,g);e=Math.min(k,p);h=Math.max(h,g);k=Math.max(k,p);p=b.getChildren();g=0;let m,q;for(let l=e;l<=k;l++){var x=p[l];t.DEPRECATED_$isGridRowNode(x)||z(24);var y=a[g];t.DEPRECATED_$isGridRowNode(y)||z(24);x=x.getChildren();y=y.getChildren();let F=0;for(let u=c;u<=h;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===h&&(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++}g++}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,h=t.$isElementNode(c)&&c.excludeFromCopy("html");var g=c;if(null!==b){var k=n.$cloneWithProperties(c);g=k=t.$isTextNode(k)&&null!=b?n.$sliceSelectedTextNodeContent(b,k):k}let p=t.$isElementNode(g)?g.getChildren():[];var m=g;k=m.exportJSON();k.type!==m.constructor.getType()&&z(58);var q=k.children;t.$isElementNode(m)&&(Array.isArray(q)||z(59));t.$isTextNode(g)&&(g=g.__text,0<g.length?k.text=g:e=!1);for(g=0;g<p.length;g++)m=p[g],q=I(a,b,m,k.children),
15
- !e&&t.$isElementNode(c)&&q&&c.extractWithChild(m,b,"clone")&&(e=!0);if(e&&!h)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")||a.getData("text/uri-list");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 k=J(g.nodes);return D(c,k,b)}}catch{}if(f=a.getData("text/html"))try{var e=(new DOMParser).parseFromString(f,"text/html"),h=d.$generateNodesFromDOM(c,e);return D(c,h,b)}catch{}a=a.getData("text/plain")||a.getData("text/uri-list");if(null!=a)if(t.$isRangeSelection(b))for(a=a.split(/(\r?\n|\t)/),c=a.length,
19
- e=0;e<c;e++)h=a[e],"\n"===h||"\r\n"===h?b.insertParagraph():"\t"===h?b.insertNodes([t.$createTabNode()]):b.insertText(h);else b.insertRawText(a)};exports.$insertGeneratedNodes=D;
20
- exports.copyToClipboard=async function(a,b){if(null!==K)return!1;if(null!==b)return new Promise(h=>{a.update(()=>{h(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(h=>{let g=a.registerCommand(t.COPY_COMMAND,k=>{k instanceof
21
- ClipboardEvent&&(g(),null!==K&&(window.clearTimeout(K),K=null),h(L(a,k)));return!0},t.COMMAND_PRIORITY_CRITICAL);K=window.setTimeout(()=>{g();K=null;h(!1)},50);document.execCommand("copy");e.remove()})}
7
+ 'use strict';var d=require("@lexical/html"),p=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))}function E(a,b,c){(u.DEPRECATED_$isGridSelection(c)||null!==r.$findMatchingParent(c.anchor.getNode(),e=>u.DEPRECATED_$isGridCellNode(e))&&null!==r.$findMatchingParent(c.focus.getNode(),e=>u.DEPRECATED_$isGridCellNode(e)))&&1===b.length&&u.DEPRECATED_$isGridNode(b[0])?F(b,c,!1,a):I(b,c)}
10
+ function I(a,b){let c=[],e=null;for(let f=0;f<a.length;f++){let h=a[f],g=u.$isLineBreakNode(h);if(g||u.$isDecoratorNode(h)&&h.isInline()||u.$isElementNode(h)&&h.isInline()||u.$isTextNode(h)||h.isParentRequired()){if(null===e&&(e=h.createParentElementNode(),c.push(e),g))continue;null!==e&&e.append(h)}else c.push(h),e=null}u.$isRangeSelection(b)?b.insertNodes(c):u.DEPRECATED_$isGridSelection(b)&&(a=b.anchor.getNode(),u.DEPRECATED_$isGridCellNode(a)||z(41),a.append(...c))}
11
+ function F(a,b,c,e){1===a.length&&u.DEPRECATED_$isGridNode(a[0])||z(42);var f=a[0];a=f.getChildren();c=f.getFirstChildOrThrow().getChildrenSize();var h=f.getChildrenSize(),g=r.$findMatchingParent(b.anchor.getNode(),l=>u.DEPRECATED_$isGridCellNode(l));b=(f=g&&r.$findMatchingParent(g,l=>u.DEPRECATED_$isGridRowNode(l)))&&r.$findMatchingParent(f,l=>u.DEPRECATED_$isGridNode(l));u.DEPRECATED_$isGridCellNode(g)&&u.DEPRECATED_$isGridRowNode(f)&&u.DEPRECATED_$isGridNode(b)||z(43);var k=f.getIndexWithinParent(),
12
+ m=Math.min(b.getChildrenSize()-1,k+h-1);h=g.getIndexWithinParent();g=Math.min(f.getChildrenSize()-1,h+c-1);c=Math.min(h,g);f=Math.min(k,m);h=Math.max(h,g);k=Math.max(k,m);m=b.getChildren();g=0;let n,q;for(let l=f;l<=k;l++){var t=m[l];u.DEPRECATED_$isGridRowNode(t)||z(24);var y=a[g];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===f&&v===c?n=
13
+ w.getKey():l===k&&v===h&&(q=w.getKey());let N=w.getChildren();H.getChildren().forEach(x=>{u.$isTextNode(x)&&u.$createParagraphNode().append(x);w.append(x)});N.forEach(x=>x.remove());G++}g++}n&&q&&(a=u.DEPRECATED_$createGridSelection(),a.set(b.getKey(),n,q),u.$setSelection(a),e.dispatchCommand(u.SELECTION_CHANGE_COMMAND,void 0))}
14
+ function J(a,b,c,e=[]){let f=null!=b?c.isSelected(b):!0,h=u.$isElementNode(c)&&c.excludeFromCopy("html");var g=c;if(null!==b){var k=p.$cloneWithProperties(c);g=k=u.$isTextNode(k)&&null!=b?p.$sliceSelectedTextNodeContent(b,k):k}let m=u.$isElementNode(g)?g.getChildren():[];var n=g;k=n.exportJSON();var q=n.constructor;k.type!==q.getType()&&z(58,q.name);let t=k.children;u.$isElementNode(n)&&(Array.isArray(t)||z(59,q.name));u.$isTextNode(g)&&(g=g.__text,0<g.length?k.text=g:f=!1);for(g=0;g<m.length;g++)n=
15
+ m[g],q=J(a,b,n,k.children),!f&&u.$isElementNode(c)&&q&&c.extractWithChild(n,b,"clone")&&(f=!0);if(f&&!h)e.push(k);else if(Array.isArray(k.children))for(a=0;a<k.children.length;a++)e.push(k.children[a]);return f}function D(a,b){let c=[],e=u.$getRoot().getChildren();for(let f=0;f<e.length;f++)J(a,b,e[f],c);return{namespace:a._config.namespace,nodes:c}}function K(a){let b=[];for(let c=0;c<a.length;c++){let e=u.$parseSerializedNode(a[c]);u.$isTextNode(e)&&p.$addNodeStyle(e);b.push(e)}return b}let L=null;
16
+ function M(a,b){var c=A?(a._window||window).getSelection():null;if(!c)return!1;var e=c.anchorNode;c=c.focusNode;if(null!==e&&null!==c&&!u.isSelectionWithinEditor(a,e,c))return!1;b.preventDefault();b=b.clipboardData;e=u.$getSelection();if(null===b||null===e)return!1;c=B(a);a=C(a);let f="";null!==e&&(f=e.getTextContent());null!==c&&b.setData("text/html",c);null!==a&&b.setData("application/x-lexical-editor",a);b.setData("text/plain",f);return!0}exports.$generateJSONFromSelectedNodes=D;
17
+ exports.$generateNodesFromSerializedNodes=K;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 e=a.getData("application/x-lexical-editor");if(e)try{let g=JSON.parse(e);if(g.namespace===c._config.namespace&&Array.isArray(g.nodes)){let k=K(g.nodes);return E(c,k,b)}}catch{}if(e=a.getData("text/html"))try{var f=(new DOMParser).parseFromString(e,"text/html"),h=d.$generateNodesFromDOM(c,f);return E(c,h,b)}catch{}a=a.getData("text/plain")||a.getData("text/uri-list");if(null!=a)if(u.$isRangeSelection(b))for(a=a.split(/(\r?\n|\t)/),c=a.length,
19
+ f=0;f<c;f++)h=a[f],"\n"===h||"\r\n"===h?b.insertParagraph():"\t"===h?b.insertNodes([u.$createTabNode()]):b.insertText(h);else b.insertRawText(a)};exports.$insertGeneratedNodes=E;
20
+ exports.copyToClipboard=async function(a,b){if(null!==L)return!1;if(null!==b)return new Promise(g=>{a.update(()=>{g(M(a,b))})});var c=a.getRootElement();let e=null==a._window?window.document:a._window.document,f=A?(a._window||window).getSelection():null;if(null===c||null===f)return!1;let h=e.createElement("span");h.style.cssText="position: fixed; top: -1000px;";h.append(e.createTextNode("#"));c.append(h);c=new Range;c.setStart(h,0);c.setEnd(h,1);f.removeAllRanges();f.addRange(c);return new Promise(g=>
21
+ {let k=a.registerCommand(u.COPY_COMMAND,m=>{r.objectKlassEquals(m,ClipboardEvent)&&(k(),null!==L&&(window.clearTimeout(L),L=null),g(M(a,m)));return!0},u.COMMAND_PRIORITY_CRITICAL);L=window.setTimeout(()=>{k();L=null;g(!1)},50);e.execCommand("copy");h.remove()})}
package/clipboard.d.ts CHANGED
@@ -6,19 +6,88 @@
6
6
  *
7
7
  */
8
8
  import { GridSelection, LexicalEditor, LexicalNode, NodeSelection, RangeSelection } from 'lexical';
9
+ /**
10
+ * Returns the *currently selected* Lexical content as an HTML string, relying on the
11
+ * logic defined in the exportDOM methods on the LexicalNode classes. Note that
12
+ * this will not return the HTML content of the entire editor (unless all the content is included
13
+ * in the current selection).
14
+ *
15
+ * @param editor - LexicalEditor instance to get HTML content from
16
+ * @returns a string of HTML content
17
+ */
9
18
  export declare function $getHtmlContent(editor: LexicalEditor): string;
19
+ /**
20
+ * Returns the *currently selected* Lexical content as a JSON string, relying on the
21
+ * logic defined in the exportJSON methods on the LexicalNode classes. Note that
22
+ * this will not return the JSON content of the entire editor (unless all the content is included
23
+ * in the current selection).
24
+ *
25
+ * @param editor - LexicalEditor instance to get the JSON content from
26
+ * @returns
27
+ */
10
28
  export declare function $getLexicalContent(editor: LexicalEditor): null | string;
29
+ /**
30
+ * Attempts to insert content of the mime-types text/plain or text/uri-list from
31
+ * the provided DataTransfer object into the editor at the provided selection.
32
+ * text/uri-list is only used if text/plain is not also provided.
33
+ *
34
+ * @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
35
+ * @param selection the selection to use as the insertion point for the content in the DataTransfer object
36
+ */
11
37
  export declare function $insertDataTransferForPlainText(dataTransfer: DataTransfer, selection: RangeSelection | GridSelection): void;
38
+ /**
39
+ * Attempts to insert content of the mime-types application/x-lexical-editor, text/html,
40
+ * text/plain, or text/uri-list (in descending order of priority) from the provided DataTransfer
41
+ * object into the editor at the provided selection.
42
+ *
43
+ * @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
44
+ * @param selection the selection to use as the insertion point for the content in the DataTransfer object
45
+ * @param editor the LexicalEditor the content is being inserted into.
46
+ */
12
47
  export declare function $insertDataTransferForRichText(dataTransfer: DataTransfer, selection: RangeSelection | GridSelection, editor: LexicalEditor): void;
48
+ /**
49
+ * Inserts Lexical nodes into the editor using different strategies depending on
50
+ * some simple selection-based heuristics. If you're looking for a generic way to
51
+ * to insert nodes into the editor at a specific selection point, you probably want
52
+ * {@link lexical.$insertNodes}
53
+ *
54
+ * @param editor LexicalEditor instance to insert the nodes into.
55
+ * @param nodes The nodes to insert.
56
+ * @param selection The selection to insert the nodes into.
57
+ */
13
58
  export declare function $insertGeneratedNodes(editor: LexicalEditor, nodes: Array<LexicalNode>, selection: RangeSelection | GridSelection): void;
14
59
  export interface BaseSerializedNode {
15
60
  children?: Array<BaseSerializedNode>;
16
61
  type: string;
17
62
  version: number;
18
63
  }
64
+ /**
65
+ * Gets the Lexical JSON of the nodes inside the provided Selection.
66
+ *
67
+ * @param editor LexicalEditor to get the JSON content from.
68
+ * @param selection Selection to get the JSON content from.
69
+ * @returns an object with the editor namespace and a list of serializable nodes as JavaScript objects.
70
+ */
19
71
  export declare function $generateJSONFromSelectedNodes<SerializedNode extends BaseSerializedNode>(editor: LexicalEditor, selection: RangeSelection | NodeSelection | GridSelection | null): {
20
72
  namespace: string;
21
73
  nodes: Array<SerializedNode>;
22
74
  };
75
+ /**
76
+ * This method takes an array of objects conforming to the BaseSeralizedNode interface and returns
77
+ * an Array containing instances of the corresponding LexicalNode classes registered on the editor.
78
+ * Normally, you'd get an Array of BaseSerialized nodes from {@link $generateJSONFromSelectedNodes}
79
+ *
80
+ * @param serializedNodes an Array of objects conforming to the BaseSerializedNode interface.
81
+ * @returns an Array of Lexical Node objects.
82
+ */
23
83
  export declare function $generateNodesFromSerializedNodes(serializedNodes: Array<BaseSerializedNode>): Array<LexicalNode>;
84
+ /**
85
+ * Copies the content of the current selection to the clipboard in
86
+ * text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
87
+ * formats.
88
+ *
89
+ * @param editor the LexicalEditor instance to copy content from
90
+ * @param event the native browser ClipboardEvent to add the content to.
91
+ * @returns
92
+ */
24
93
  export declare function copyToClipboard(editor: LexicalEditor, event: null | ClipboardEvent): Promise<boolean>;
package/package.json CHANGED
@@ -9,16 +9,16 @@
9
9
  "paste"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.11.1",
12
+ "version": "0.11.2",
13
13
  "main": "LexicalClipboard.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.11.1"
15
+ "lexical": "0.11.2"
16
16
  },
17
17
  "dependencies": {
18
- "@lexical/utils": "0.11.1",
19
- "@lexical/list": "0.11.1",
20
- "@lexical/selection": "0.11.1",
21
- "@lexical/html": "0.11.1"
18
+ "@lexical/utils": "0.11.2",
19
+ "@lexical/list": "0.11.2",
20
+ "@lexical/selection": "0.11.2",
21
+ "@lexical/html": "0.11.2"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",