@lexical/clipboard 0.1.19 → 0.2.0

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.
@@ -9,18 +9,6 @@
9
9
  var selection = require('@lexical/selection');
10
10
  var lexical = require('lexical');
11
11
 
12
- /**
13
- * Copyright (c) Meta Platforms, Inc. and affiliates.
14
- *
15
- * This source code is licensed under the MIT license found in the
16
- * LICENSE file in the root directory of this source tree.
17
- *
18
- *
19
- */
20
- const getSelection = () => window.getSelection();
21
-
22
- var getDOMSelection = getSelection;
23
-
24
12
  /**
25
13
  * Copyright (c) Meta Platforms, Inc. and affiliates.
26
14
  *
@@ -31,22 +19,93 @@ var getDOMSelection = getSelection;
31
19
  */
32
20
  const IGNORE_TAGS = new Set(['STYLE']);
33
21
  function getHtmlContent(editor) {
34
- const domSelection = getDOMSelection(); // If we haven't selected a range, then don't copy anything
22
+ const selection$1 = lexical.$getSelection();
23
+
24
+ if (selection$1 == null) {
25
+ throw new Error('Expected valid LexicalSelection');
26
+ } // If we haven't selected anything
27
+
35
28
 
36
- if (domSelection.isCollapsed) {
29
+ if (lexical.$isRangeSelection(selection$1) && selection$1.isCollapsed() || selection$1.getNodes().length === 0) {
37
30
  return null;
38
31
  }
39
32
 
40
- const range = domSelection.getRangeAt(0);
33
+ const state = selection.$cloneContents(selection$1);
34
+ return $convertSelectedLexicalContentToHtml(editor, selection$1, state);
35
+ }
36
+ function $convertSelectedLexicalNodeToHTMLElement(editor, selection, node) {
37
+ let nodeToConvert = node;
38
+
39
+ if (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection)) {
40
+ const anchor = selection.anchor.getNode();
41
+ const focus = selection.focus.getNode();
42
+ const isAnchor = node.is(anchor);
43
+ const isFocus = node.is(focus);
44
+
45
+ if (lexical.$isTextNode(node) && (isAnchor || isFocus)) {
46
+ const anchorOffset = selection.anchor.getCharacterOffset();
47
+ const focusOffset = selection.focus.getCharacterOffset();
48
+ const isBackward = selection.isBackward();
49
+ const isSame = anchor.is(focus);
50
+ const isFirst = node.is(isBackward ? focus : anchor);
51
+ const nodeText = node.getTextContent();
52
+ const nodeTextLength = nodeText.length;
53
+
54
+ if (isSame) {
55
+ const startOffset = anchorOffset > focusOffset ? focusOffset : anchorOffset;
56
+ const endOffset = anchorOffset > focusOffset ? anchorOffset : focusOffset;
57
+ const splitNodes = node.splitText(startOffset, endOffset);
58
+ nodeToConvert = startOffset === 0 ? splitNodes[0] : splitNodes[1];
59
+ } else {
60
+ let endOffset;
61
+
62
+ if (isFirst) {
63
+ endOffset = isBackward ? focusOffset : anchorOffset;
64
+ } else {
65
+ endOffset = isBackward ? anchorOffset : focusOffset;
66
+ }
67
+
68
+ if (!isBackward && endOffset === 0) {
69
+ return null;
70
+ } else if (endOffset !== nodeTextLength) {
71
+ nodeToConvert = node.splitText(endOffset)[isFirst && endOffset !== 0 ? 1 : 0];
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ const {
78
+ element,
79
+ after
80
+ } = nodeToConvert.exportDOM(editor);
81
+ if (!element) return null;
82
+ const children = lexical.$isElementNode(nodeToConvert) ? nodeToConvert.getChildren() : [];
83
+
84
+ for (let i = 0; i < children.length; i++) {
85
+ const childNode = children[i];
41
86
 
42
- if (range) {
43
- const container = document.createElement('div');
44
- const frag = range.cloneContents();
45
- container.appendChild(frag);
46
- return container.innerHTML;
87
+ if (childNode.isSelected()) {
88
+ const newElement = $convertSelectedLexicalNodeToHTMLElement(editor, selection, childNode);
89
+ if (newElement) element.append(newElement);
90
+ }
47
91
  }
48
92
 
49
- return null;
93
+ return after ? after.call(nodeToConvert, element) : element;
94
+ }
95
+ function $convertSelectedLexicalContentToHtml(editor, selection, state) {
96
+ const container = document.createElement('div');
97
+
98
+ for (let i = 0; i < state.range.length; i++) {
99
+ const nodeKey = state.range[i];
100
+ const node = lexical.$getNodeByKey(nodeKey);
101
+
102
+ if (node && node.isSelected()) {
103
+ const element = $convertSelectedLexicalNodeToHTMLElement(editor, selection, node);
104
+ if (element) container.append(element);
105
+ }
106
+ }
107
+
108
+ return container.innerHTML;
50
109
  }
51
110
  function $getLexicalContent(editor) {
52
111
  const selection$1 = lexical.$getSelection();
@@ -4,8 +4,11 @@
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
- var l=require("@lexical/selection"),m=require("lexical");const n=new Set(["STYLE"]);function p(a,d){a=a.getData("text/plain");null!=a&&d.insertRawText(a)}function r(a,d){const {nodeName:h}=a;d=d._htmlConversions.get(h.toLowerCase());let c=null;void 0!==d&&d.forEach(e=>{e=e(a);null!==e&&(null===c||c.priority<e.priority)&&(c=e)});return null!==c?c.conversion:null}
8
- function t(a,d,h=new Map,c){let e=[];if(n.has(a.nodeName))return e;let b=null;var g=r(a,d);const f=g?g(a):null;g=null;if(null!==f){g=f.after;b=f.node;if(null!==b){for(var [,k]of h)if(b=k(b,c),!b)break;b&&e.push(b)}null!=f.forChild&&h.set(a.nodeName,f.forChild)}a=a.childNodes;c=[];for(k=0;k<a.length;k++)c.push(...t(a[k],d,h,b));null!=g&&(c=g(c));null==b?e=e.concat(c):m.$isElementNode(b)&&b.append(...c);return e}
9
- exports.$getLexicalContent=function(a){var d=m.$getSelection();return null!==d?(a=a._config.namespace,d=l.$cloneContents(d),JSON.stringify({namespace:a,state:d})):null};exports.$insertDataTransferForPlainText=p;
10
- exports.$insertDataTransferForRichText=function(a,d,h){var c=a.getData("application/x-lexical-editor");if(c){var e=h._config.namespace;try{const k=JSON.parse(c);if(k.namespace===e){const {range:q,nodeMap:u}=k.state;var b=new Map(u);c=[];for(e=0;e<q.length;e++){var g=b.get(q[e]);if(void 0!==g){var f=m.$createNodeFromParse(g,b);c.push(f)}}d.insertNodes(c);return}}catch(k){}}if(b=a.getData("text/html")){b=(new DOMParser).parseFromString(b,"text/html");a=[];b=b.body?Array.from(b.body.childNodes):[];g=
11
- b.length;for(f=0;f<g;f++)c=b[f],n.has(c.nodeName)||(c=t(c,h),null!==c&&(a=a.concat(c)));h=a;a=[];b=null;for(g=0;g<h.length;g++)f=h[g],!m.$isElementNode(f)||f.isInline()?(null===b&&(b=m.$createParagraphNode(),a.push(b)),null!==b&&b.append(f)):(a.push(f),b=null);d.insertNodes(a)}else p(a,d)};exports.getHtmlContent=function(){var a=window.getSelection();if(a.isCollapsed)return null;var d=a.getRangeAt(0);return d?(a=document.createElement("div"),d=d.cloneContents(),a.appendChild(d),a.innerHTML):null};
7
+ var l=require("@lexical/selection"),p=require("lexical");const q=new Set(["STYLE"]);
8
+ function r(c,d,h){var b=h;if(p.$isRangeSelection(d)||p.$isGridSelection(d)){var f=d.anchor.getNode(),a=d.focus.getNode(),e=h.is(f),g=h.is(a);if(p.$isTextNode(h)&&(e||g)){e=d.anchor.getCharacterOffset();g=d.focus.getCharacterOffset();const m=d.isBackward(),v=f.is(a);f=h.is(m?a:f);a=h.getTextContent().length;if(v)b=e>g?g:e,h=h.splitText(b,e>g?e:g),b=0===b?h[0]:h[1];else if(e=f?m?g:e:m?e:g,m||0!==e)e!==a&&(b=h.splitText(e)[f&&0!==e?1:0]);else return null}}const {element:k,after:n}=b.exportDOM(c);if(!k)return null;
9
+ h=p.$isElementNode(b)?b.getChildren():[];for(e=0;e<h.length;e++)g=h[e],g.isSelected()&&(g=r(c,d,g))&&k.append(g);return n?n.call(b,k):k}function t(c,d){c=c.getData("text/plain");null!=c&&d.insertRawText(c)}function u(c,d){const {nodeName:h}=c;d=d._htmlConversions.get(h.toLowerCase());let b=null;void 0!==d&&d.forEach(f=>{f=f(c);null!==f&&(null===b||b.priority<f.priority)&&(b=f)});return null!==b?b.conversion:null}
10
+ function w(c,d,h=new Map,b){let f=[];if(q.has(c.nodeName))return f;let a=null;var e=u(c,d);const g=e?e(c):null;e=null;if(null!==g){e=g.after;a=g.node;if(null!==a){for(var [,k]of h)if(a=k(a,b),!a)break;a&&f.push(a)}null!=g.forChild&&h.set(c.nodeName,g.forChild)}c=c.childNodes;b=[];for(k=0;k<c.length;k++)b.push(...w(c[k],d,h,a));null!=e&&(b=e(b));null==a?f=f.concat(b):p.$isElementNode(a)&&a.append(...b);return f}
11
+ exports.$getLexicalContent=function(c){var d=p.$getSelection();return null!==d?(c=c._config.namespace,d=l.$cloneContents(d),JSON.stringify({namespace:c,state:d})):null};exports.$insertDataTransferForPlainText=t;
12
+ exports.$insertDataTransferForRichText=function(c,d,h){var b=c.getData("application/x-lexical-editor");if(b){var f=h._config.namespace;try{const k=JSON.parse(b);if(k.namespace===f){const {range:n,nodeMap:m}=k.state;var a=new Map(m);b=[];for(f=0;f<n.length;f++){var e=a.get(n[f]);if(void 0!==e){var g=p.$createNodeFromParse(e,a);b.push(g)}}d.insertNodes(b);return}}catch(k){}}if(a=c.getData("text/html")){a=(new DOMParser).parseFromString(a,"text/html");c=[];a=a.body?Array.from(a.body.childNodes):[];e=
13
+ a.length;for(g=0;g<e;g++)b=a[g],q.has(b.nodeName)||(b=w(b,h),null!==b&&(c=c.concat(b)));h=c;c=[];a=null;for(e=0;e<h.length;e++)g=h[e],!p.$isElementNode(g)||g.isInline()?(null===a&&(a=p.$createParagraphNode(),c.push(a)),null!==a&&a.append(g)):(c.push(g),a=null);d.insertNodes(c)}else t(c,d)};
14
+ exports.getHtmlContent=function(c){const d=p.$getSelection();if(null==d)throw Error("Expected valid LexicalSelection");if(p.$isRangeSelection(d)&&d.isCollapsed()||0===d.getNodes().length)return null;var h=l.$cloneContents(d);const b=document.createElement("div");for(let a=0;a<h.range.length;a++){var f=p.$getNodeByKey(h.range[a]);f&&f.isSelected()&&(f=r(c,d,f))&&b.append(f)}return b.innerHTML};
package/package.json CHANGED
@@ -9,14 +9,14 @@
9
9
  "paste"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.1.19",
12
+ "version": "0.2.0",
13
13
  "main": "LexicalClipboard.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.1.19"
15
+ "lexical": "0.2.0"
16
16
  },
17
17
  "dependencies": {
18
- "@lexical/utils": "0.1.19",
19
- "@lexical/selection": "0.1.19"
18
+ "@lexical/utils": "0.2.0",
19
+ "@lexical/selection": "0.2.0"
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",