@lexical/clipboard 0.1.16 → 0.1.19

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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Dominic Gannaway
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,31 @@
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 {LexicalEditor, RangeSelection} from 'lexical';
10
+
11
+ /*
12
+ * Rich Text
13
+ */
14
+
15
+ export function $insertDataTransferForRichText(
16
+ dataTransfer: DataTransfer,
17
+ selection: RangeSelection,
18
+ editor: LexicalEditor,
19
+ ): void;
20
+
21
+ export function getHtmlContent(editor: LexicalEditor): string;
22
+ export function $getLexicalContent(editor: LexicalEditor): string;
23
+
24
+ /*
25
+ * Plain Text
26
+ */
27
+
28
+ export function $insertDataTransferForPlainText(
29
+ dataTransfer: DataTransfer,
30
+ selection: RangeSelection,
31
+ ): void;
@@ -29,6 +29,7 @@ var getDOMSelection = getSelection;
29
29
  *
30
30
  *
31
31
  */
32
+ const IGNORE_TAGS = new Set(['STYLE']);
32
33
  function getHtmlContent(editor) {
33
34
  const domSelection = getDOMSelection(); // If we haven't selected a range, then don't copy anything
34
35
 
@@ -52,9 +53,10 @@ function $getLexicalContent(editor) {
52
53
 
53
54
  if (selection$1 !== null) {
54
55
  const namespace = editor._config.namespace;
56
+ const state = selection.$cloneContents(selection$1);
55
57
  return JSON.stringify({
56
58
  namespace,
57
- state: selection.$cloneContents(selection$1)
59
+ state
58
60
  });
59
61
  }
60
62
 
@@ -167,8 +169,13 @@ function getConversionFunction(domNode, editor) {
167
169
  return currentConversion !== null ? currentConversion.conversion : null;
168
170
  }
169
171
 
170
- function $createNodesFromDOM(node, editor, forChildMap = new Map()) {
172
+ function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexicalNode) {
171
173
  let lexicalNodes = [];
174
+
175
+ if (IGNORE_TAGS.has(node.nodeName)) {
176
+ return lexicalNodes;
177
+ }
178
+
172
179
  let currentLexicalNode = null;
173
180
  const transformFunction = getConversionFunction(node, editor);
174
181
  const transformOutput = transformFunction ? transformFunction(node) : null;
@@ -179,11 +186,16 @@ function $createNodesFromDOM(node, editor, forChildMap = new Map()) {
179
186
  currentLexicalNode = transformOutput.node;
180
187
 
181
188
  if (currentLexicalNode !== null) {
182
- lexicalNodes.push(currentLexicalNode);
183
- const forChildFunctions = Array.from(forChildMap.values());
189
+ for (const [, forChildFunction] of forChildMap) {
190
+ currentLexicalNode = forChildFunction(currentLexicalNode, parentLexicalNode);
191
+
192
+ if (!currentLexicalNode) {
193
+ break;
194
+ }
195
+ }
184
196
 
185
- for (let i = 0; i < forChildFunctions.length; i++) {
186
- forChildFunctions[i](currentLexicalNode);
197
+ if (currentLexicalNode) {
198
+ lexicalNodes.push(currentLexicalNode);
187
199
  }
188
200
  }
189
201
 
@@ -198,7 +210,7 @@ function $createNodesFromDOM(node, editor, forChildMap = new Map()) {
198
210
  let childLexicalNodes = [];
199
211
 
200
212
  for (let i = 0; i < children.length; i++) {
201
- childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, forChildMap));
213
+ childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, forChildMap, currentLexicalNode));
202
214
  }
203
215
 
204
216
  if (postTransform != null) {
@@ -226,10 +238,14 @@ function $generateNodesFromDOM(dom, editor) {
226
238
  const elementsLength = elements.length;
227
239
 
228
240
  for (let i = 0; i < elementsLength; i++) {
229
- const lexicalNode = $createNodesFromDOM(elements[i], editor);
241
+ const element = elements[i];
230
242
 
231
- if (lexicalNode !== null) {
232
- lexicalNodes = lexicalNodes.concat(lexicalNode);
243
+ if (!IGNORE_TAGS.has(element.nodeName)) {
244
+ const lexicalNode = $createNodesFromDOM(element, editor);
245
+
246
+ if (lexicalNode !== null) {
247
+ lexicalNodes = lexicalNodes.concat(lexicalNode);
248
+ }
233
249
  }
234
250
  }
235
251
 
@@ -0,0 +1,32 @@
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
+ * @flow strict
8
+ */
9
+
10
+ import type {LexicalEditor, RangeSelection} from 'lexical';
11
+
12
+ /*
13
+ * Rich Text
14
+ */
15
+
16
+ declare export function $insertDataTransferForRichText(
17
+ dataTransfer: DataTransfer,
18
+ selection: RangeSelection,
19
+ editor: LexicalEditor,
20
+ ): void;
21
+
22
+ declare export function getHtmlContent(editor: LexicalEditor): string;
23
+ declare export function $getLexicalContent(editor: LexicalEditor): string;
24
+
25
+ /*
26
+ * Plain Text
27
+ */
28
+
29
+ declare export function $insertDataTransferForPlainText(
30
+ dataTransfer: DataTransfer,
31
+ selection: RangeSelection,
32
+ ): void;
@@ -4,8 +4,8 @@
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");function n(a,c){a=a.getData("text/plain");null!=a&&c.insertRawText(a)}function q(a,c){const {nodeName:h}=a;c=c._htmlConversions.get(h.toLowerCase());let e=null;void 0!==c&&c.forEach(f=>{f=f(a);null!==f&&(null===e||e.priority<f.priority)&&(e=f)});return null!==e?e.conversion:null}
8
- function r(a,c,h=new Map){let e=[],f=null;var b=q(a,c),d=b?b(a):null;b=null;if(null!==d){b=d.after;f=d.node;if(null!==f){e.push(f);var g=Array.from(h.values());for(let k=0;k<g.length;k++)g[k](f)}null!=d.forChild&&h.set(a.nodeName,d.forChild)}a=a.childNodes;d=[];for(g=0;g<a.length;g++)d.push(...r(a[g],c,h));null!=b&&(d=b(d));null==f?e=e.concat(d):m.$isElementNode(f)&&f.append(...d);return e}
9
- exports.$getLexicalContent=function(a){const c=m.$getSelection();return null!==c?JSON.stringify({namespace:a._config.namespace,state:l.$cloneContents(c)}):null};exports.$insertDataTransferForPlainText=n;
10
- exports.$insertDataTransferForRichText=function(a,c,h){var e=a.getData("application/x-lexical-editor");if(e){var f=h._config.namespace;try{const k=JSON.parse(e);if(k.namespace===f){const {range:p,nodeMap:t}=k.state;var b=new Map(t);e=[];for(f=0;f<p.length;f++){var d=b.get(p[f]);if(void 0!==d){var g=m.$createNodeFromParse(d,b);e.push(g)}}c.insertNodes(e);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):[];d=
11
- b.length;for(g=0;g<d;g++)e=r(b[g],h),null!==e&&(a=a.concat(e));h=a;a=[];b=null;for(d=0;d<h.length;d++)g=h[d],!m.$isElementNode(g)||g.isInline()?(null===b&&(b=m.$createParagraphNode(),a.push(b)),null!==b&&b.append(g)):(a.push(g),b=null);c.insertNodes(a)}else n(a,c)};exports.getHtmlContent=function(){var a=window.getSelection();if(a.isCollapsed)return null;var c=a.getRangeAt(0);return c?(a=document.createElement("div"),c=c.cloneContents(),a.appendChild(c),a.innerHTML):null};
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};
package/package.json CHANGED
@@ -1,9 +1,5 @@
1
1
  {
2
2
  "name": "@lexical/clipboard",
3
- "author": {
4
- "name": "Dominic Gannaway",
5
- "email": "dg@domgan.com"
6
- },
7
3
  "description": "This package provides the copy/paste functionality for Lexical.",
8
4
  "keywords": [
9
5
  "lexical",
@@ -13,14 +9,14 @@
13
9
  "paste"
14
10
  ],
15
11
  "license": "MIT",
16
- "version": "0.1.16",
12
+ "version": "0.1.19",
17
13
  "main": "LexicalClipboard.js",
18
14
  "peerDependencies": {
19
- "lexical": "0.1.16"
15
+ "lexical": "0.1.19"
20
16
  },
21
17
  "dependencies": {
22
- "@lexical/utils": "0.1.16",
23
- "@lexical/selection": "0.1.16"
18
+ "@lexical/utils": "0.1.19",
19
+ "@lexical/selection": "0.1.19"
24
20
  },
25
21
  "repository": {
26
22
  "type": "git",