@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.
- package/LexicalClipboard.dev.js +80 -21
- package/LexicalClipboard.prod.js +8 -5
- package/package.json +4 -4
package/LexicalClipboard.dev.js
CHANGED
|
@@ -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
|
|
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 (
|
|
29
|
+
if (lexical.$isRangeSelection(selection$1) && selection$1.isCollapsed() || selection$1.getNodes().length === 0) {
|
|
37
30
|
return null;
|
|
38
31
|
}
|
|
39
32
|
|
|
40
|
-
const
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
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();
|
package/LexicalClipboard.prod.js
CHANGED
|
@@ -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"),
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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.
|
|
12
|
+
"version": "0.2.0",
|
|
13
13
|
"main": "LexicalClipboard.js",
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"lexical": "0.
|
|
15
|
+
"lexical": "0.2.0"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@lexical/utils": "0.
|
|
19
|
-
"@lexical/selection": "0.
|
|
18
|
+
"@lexical/utils": "0.2.0",
|
|
19
|
+
"@lexical/selection": "0.2.0"
|
|
20
20
|
},
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|