@lexical/clipboard
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -26,4 +26,4 @@ /** | ||
export function $getHtmlContent(editor: LexicalEditor): string; | ||
export function $getHtmlContent(editor: LexicalEditor): string | null; | ||
export function $getLexicalContent(editor: LexicalEditor): string | null; | ||
/* | ||
@@ -30,0 +30,0 @@ * Plain Text |
@@ -10,2 +10,3 @@ /** | ||
var html = require('@lexical/html'); | ||
var selection = require('@lexical/selection'); | ||
var utils = require('@lexical/utils'); | ||
@@ -35,2 +36,16 @@ var lexical = require('lexical'); | ||
} | ||
function $getLexicalContent(editor) { | ||
const selection = lexical.$getSelection(); | ||
if (selection == null) { | ||
throw new Error('Expected valid LexicalSelection'); | ||
} // If we haven't selected anything | ||
if (lexical.$isRangeSelection(selection) && selection.isCollapsed() || selection.getNodes().length === 0) { | ||
return null; | ||
} | ||
return JSON.stringify($generateJSONFromSelectedNodes(editor, selection)); | ||
} | ||
function $insertDataTransferForPlainText(dataTransfer, selection) { | ||
@@ -44,21 +59,38 @@ const text = dataTransfer.getData('text/plain'); | ||
function $insertDataTransferForRichText(dataTransfer, selection, editor) { | ||
const isSelectionInsideOfGrid = lexical.$isGridSelection(selection) || utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isGridCellNode(n)) !== null && utils.$findMatchingParent(selection.focus.getNode(), n => lexical.$isGridCellNode(n)) !== null; | ||
const textHtmlMimeType = 'text/html'; | ||
const htmlString = dataTransfer.getData(textHtmlMimeType); | ||
const htmlString = dataTransfer.getData('text/html'); | ||
const lexicalString = dataTransfer.getData('application/x-lexical-editor'); | ||
if (lexicalString) { | ||
try { | ||
const payload = JSON.parse(lexicalString); | ||
if (payload.namespace === editor._config.namespace && Array.isArray(payload.nodes)) { | ||
const nodes = $generateNodesFromSerializedNodes(payload.nodes); | ||
return $insertGeneratedNodes(editor, nodes, selection); | ||
} // eslint-disable-next-line no-empty | ||
} catch {} | ||
} | ||
if (htmlString) { | ||
const parser = new DOMParser(); | ||
const dom = parser.parseFromString(htmlString, textHtmlMimeType); | ||
const nodes = html.$generateNodesFromDOM(editor, dom); | ||
try { | ||
const parser = new DOMParser(); | ||
const dom = parser.parseFromString(htmlString, 'text/html'); | ||
return $insertGeneratedNodes(editor, html.$generateNodesFromDOM(editor, dom), selection); // eslint-disable-next-line no-empty | ||
} catch {} | ||
} | ||
if (isSelectionInsideOfGrid && nodes.length === 1 && lexical.$isGridNode(nodes[0])) { | ||
$mergeGridNodesStrategy(nodes, selection, false, editor); | ||
return; | ||
} | ||
$insertDataTransferForPlainText(dataTransfer, selection); | ||
} | ||
$basicInsertStrategy(nodes, selection, false); | ||
function $insertGeneratedNodes(editor, nodes, selection) { | ||
const isSelectionInsideOfGrid = lexical.$isGridSelection(selection) || utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isGridCellNode(n)) !== null && utils.$findMatchingParent(selection.focus.getNode(), n => lexical.$isGridCellNode(n)) !== null; | ||
if (isSelectionInsideOfGrid && nodes.length === 1 && lexical.$isGridNode(nodes[0])) { | ||
$mergeGridNodesStrategy(nodes, selection, false, editor); | ||
return; | ||
} | ||
$insertDataTransferForPlainText(dataTransfer, selection); | ||
$basicInsertStrategy(nodes, selection, false); | ||
return; | ||
} | ||
@@ -216,4 +248,94 @@ | ||
function exportNodeToJSON(node) { | ||
const serializedNode = node.exportJSON(); | ||
const nodeClass = node.constructor; // @ts-expect-error TODO Replace Class utility type with InstanceType | ||
if (serializedNode.type !== nodeClass.getType()) { | ||
{ | ||
throw Error(`LexicalNode: Node ${nodeClass.name} does not implement .exportJSON().`); | ||
} | ||
} // @ts-expect-error TODO Replace Class utility type with InstanceType | ||
const serializedChildren = serializedNode.children; | ||
if (lexical.$isElementNode(node)) { | ||
if (!Array.isArray(serializedChildren)) { | ||
{ | ||
throw Error(`LexicalNode: Node ${nodeClass.name} is an element but .exportJSON() does not have a children array.`); | ||
} | ||
} | ||
} | ||
return serializedNode; | ||
} | ||
function $appendNodesToJSON(editor, selection$1, currentNode, targetArray) { | ||
let shouldInclude = selection$1 != null ? currentNode.isSelected() : true; | ||
const shouldExclude = lexical.$isElementNode(currentNode) && currentNode.excludeFromCopy('html'); | ||
let clone = selection.$cloneWithProperties(currentNode); | ||
clone = lexical.$isTextNode(clone) && selection$1 != null ? selection.$sliceSelectedTextNodeContent(selection$1, clone) : clone; | ||
const children = lexical.$isElementNode(clone) ? clone.getChildren() : []; | ||
const serializedNode = exportNodeToJSON(clone); // TODO: TextNode calls getTextContent() (NOT node.__text) within it's exportJSON method | ||
// which uses getLatest() to get the text from the original node with the same key. | ||
// This is a deeper issue with the word "clone" here, it's still a reference to the | ||
// same node as far as the LexicalEditor is concerned since it shares a key. | ||
// We need a way to create a clone of a Node in memory with it's own key, but | ||
// until then this hack will work for the selected text extract use case. | ||
if (lexical.$isTextNode(clone)) { | ||
// @ts-ignore | ||
serializedNode.text = clone.__text; | ||
} | ||
for (let i = 0; i < children.length; i++) { | ||
const childNode = children[i]; | ||
const shouldIncludeChild = $appendNodesToJSON(editor, selection$1, childNode, serializedNode.children); | ||
if (!shouldInclude && lexical.$isElementNode(currentNode) && shouldIncludeChild && currentNode.extractWithChild(childNode, selection$1, 'clone')) { | ||
shouldInclude = true; | ||
} | ||
} | ||
if (shouldInclude && !shouldExclude) { | ||
targetArray.push(serializedNode); | ||
} else if (Array.isArray(serializedNode.children)) { | ||
for (let i = 0; i < serializedNode.children.length; i++) { | ||
const serializedChildNode = serializedNode.children[i]; | ||
targetArray.push(serializedChildNode); | ||
} | ||
} | ||
return shouldInclude; | ||
} | ||
function $generateJSONFromSelectedNodes(editor, selection) { | ||
const nodes = []; | ||
const root = lexical.$getRoot(); | ||
const topLevelChildren = root.getChildren(); | ||
for (let i = 0; i < topLevelChildren.length; i++) { | ||
const topLevelNode = topLevelChildren[i]; | ||
$appendNodesToJSON(editor, selection, topLevelNode, nodes); | ||
} | ||
return { | ||
namespace: editor._config.namespace, | ||
nodes | ||
}; | ||
} | ||
function $generateNodesFromSerializedNodes(serializedNodes) { | ||
const nodes = []; | ||
for (let i = 0; i < serializedNodes.length; i++) { | ||
const serializedNode = serializedNodes[i]; | ||
nodes.push(lexical.$parseSerializedNode(serializedNode)); | ||
} | ||
return nodes; | ||
} | ||
exports.$getHtmlContent = $getHtmlContent; | ||
exports.$getLexicalContent = $getLexicalContent; | ||
exports.$insertDataTransferForPlainText = $insertDataTransferForPlainText; | ||
exports.$insertDataTransferForRichText = $insertDataTransferForRichText; |
@@ -7,7 +7,11 @@ /** | ||
*/ | ||
'use strict';var b=require("@lexical/html"),l=require("@lexical/utils"),n=require("lexical");function y(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function z(a,c){a=a.getData("text/plain");null!=a&&c.insertRawText(a)} | ||
function C(a,c,f){if(!f){f=[];let h=null;for(let e=0;e<a.length;e++){let d=a[e];n.$isDecoratorNode(d)&&!d.isTopLevel()||n.$isElementNode(d)&&d.isInline()||n.$isTextNode(d)||n.$isLineBreakNode(d)?(null===h&&(h=n.$createParagraphNode(),f.push(h)),null!==h&&h.append(d)):(f.push(d),h=null)}a=f}n.$isRangeSelection(c)?c.insertNodes(a):n.$isGridSelection(c)&&(c=c.anchor.getNode(),n.$isGridCellNode(c)||y(41),c.append(...a))} | ||
function D(a,c,f,h){1===a.length&&n.$isGridNode(a[0])||y(42);var e=a[0];a=e.getChildren();f=e.getFirstChildOrThrow().getChildrenSize();var d=e.getChildrenSize(),k=l.$findMatchingParent(c.anchor.getNode(),g=>n.$isGridCellNode(g));c=(e=k&&l.$findMatchingParent(k,g=>n.$isGridRowNode(g)))&&l.$findMatchingParent(e,g=>n.$isGridNode(g));n.$isGridCellNode(k)&&n.$isGridRowNode(e)&&n.$isGridNode(c)||y(43);var m=e.getIndexWithinParent(),t=Math.min(c.getChildrenSize()-1,m+d-1);d=k.getIndexWithinParent();k=Math.min(e.getChildrenSize()- | ||
1,d+f-1);f=Math.min(d,k);e=Math.min(m,t);d=Math.max(d,k);m=Math.max(m,t);t=c.getChildren();k=0;let w,x;for(let g=e;g<=m;g++){var u=t[g];n.$isGridRowNode(u)||y(24);var v=a[k];n.$isGridRowNode(v)||y(24);u=u.getChildren();v=v.getChildren();let A=0;for(let p=f;p<=d;p++){let q=u[p];n.$isGridCellNode(q)||y(25);let B=v[A];n.$isGridCellNode(B)||y(25);g===e&&p===f?w=q.getKey():g===m&&p===d&&(x=q.getKey());let E=q.getChildren();B.getChildren().forEach(r=>{n.$isTextNode(r)&&n.$createParagraphNode().append(r); | ||
q.append(r)});E.forEach(r=>r.remove());A++}k++}w&&x&&(a=n.$createGridSelection(),a.set(c.getKey(),w,x),n.$setSelection(a),h.dispatchCommand(n.SELECTION_CHANGE_COMMAND,void 0))}exports.$getHtmlContent=function(a){let c=n.$getSelection();if(null==c)throw Error("Expected valid LexicalSelection");return n.$isRangeSelection(c)&&c.isCollapsed()||0===c.getNodes().length?null:b.$generateHtmlFromNodes(a,c)};exports.$insertDataTransferForPlainText=z; | ||
exports.$insertDataTransferForRichText=function(a,c,f){let h=n.$isGridSelection(c)||null!==l.$findMatchingParent(c.anchor.getNode(),d=>n.$isGridCellNode(d))&&null!==l.$findMatchingParent(c.focus.getNode(),d=>n.$isGridCellNode(d)),e=a.getData("text/html");e?(a=(new DOMParser).parseFromString(e,"text/html"),a=b.$generateNodesFromDOM(f,a),h&&1===a.length&&n.$isGridNode(a[0])?D(a,c,!1,f):C(a,c,!1)):z(a,c)} | ||
'use strict';var b=require("@lexical/html"),p=require("@lexical/selection"),t=require("@lexical/utils"),y=require("lexical");function z(c){throw Error(`Minified Lexical error #${c}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function A(c,a){c=c.getData("text/plain");null!=c&&a.insertRawText(c)} | ||
function B(c,a,d){(y.$isGridSelection(d)||null!==t.$findMatchingParent(d.anchor.getNode(),h=>y.$isGridCellNode(h))&&null!==t.$findMatchingParent(d.focus.getNode(),h=>y.$isGridCellNode(h)))&&1===a.length&&y.$isGridNode(a[0])?E(a,d,!1,c):F(a,d,!1)} | ||
function F(c,a,d){if(!d){d=[];let h=null;for(let g=0;g<c.length;g++){let f=c[g];y.$isDecoratorNode(f)&&!f.isTopLevel()||y.$isElementNode(f)&&f.isInline()||y.$isTextNode(f)||y.$isLineBreakNode(f)?(null===h&&(h=y.$createParagraphNode(),d.push(h)),null!==h&&h.append(f)):(d.push(f),h=null)}c=d}y.$isRangeSelection(a)?a.insertNodes(c):y.$isGridSelection(a)&&(a=a.anchor.getNode(),y.$isGridCellNode(a)||z(41),a.append(...c))} | ||
function E(c,a,d,h){1===c.length&&y.$isGridNode(c[0])||z(42);var g=c[0];c=g.getChildren();d=g.getFirstChildOrThrow().getChildrenSize();var f=g.getChildrenSize(),e=t.$findMatchingParent(a.anchor.getNode(),l=>y.$isGridCellNode(l));a=(g=e&&t.$findMatchingParent(e,l=>y.$isGridRowNode(l)))&&t.$findMatchingParent(g,l=>y.$isGridNode(l));y.$isGridCellNode(e)&&y.$isGridRowNode(g)&&y.$isGridNode(a)||z(43);var m=g.getIndexWithinParent(),n=Math.min(a.getChildrenSize()-1,m+f-1);f=e.getIndexWithinParent();e=Math.min(g.getChildrenSize()- | ||
1,f+d-1);d=Math.min(f,e);g=Math.min(m,n);f=Math.max(f,e);m=Math.max(m,n);n=a.getChildren();e=0;let k,q;for(let l=g;l<=m;l++){var r=n[l];y.$isGridRowNode(r)||z(24);var x=c[e];y.$isGridRowNode(x)||z(24);r=r.getChildren();x=x.getChildren();let C=0;for(let u=d;u<=f;u++){let v=r[u];y.$isGridCellNode(v)||z(25);let D=x[C];y.$isGridCellNode(D)||z(25);l===g&&u===d?k=v.getKey():l===m&&u===f&&(q=v.getKey());let H=v.getChildren();D.getChildren().forEach(w=>{y.$isTextNode(w)&&y.$createParagraphNode().append(w); | ||
v.append(w)});H.forEach(w=>w.remove());C++}e++}k&&q&&(c=y.$createGridSelection(),c.set(a.getKey(),k,q),y.$setSelection(c),h.dispatchCommand(y.SELECTION_CHANGE_COMMAND,void 0))} | ||
function G(c,a,d,h){let g=null!=a?d.isSelected():!0,f=y.$isElementNode(d)&&d.excludeFromCopy("html");var e=p.$cloneWithProperties(d);e=y.$isTextNode(e)&&null!=a?p.$sliceSelectedTextNodeContent(a,e):e;let m=y.$isElementNode(e)?e.getChildren():[];var n=e;let k=n.exportJSON();var q=n.constructor;k.type!==q.getType()&&z(58,q.name);let r=k.children;y.$isElementNode(n)&&(Array.isArray(r)||z(59,q.name));y.$isTextNode(e)&&(k.text=e.__text);for(e=0;e<m.length;e++)n=m[e],q=G(c,a,n,k.children),!g&&y.$isElementNode(d)&& | ||
q&&d.extractWithChild(n,a,"clone")&&(g=!0);if(g&&!f)h.push(k);else if(Array.isArray(k.children))for(c=0;c<k.children.length;c++)h.push(k.children[c]);return g}exports.$getHtmlContent=function(c){let a=y.$getSelection();if(null==a)throw Error("Expected valid LexicalSelection");return y.$isRangeSelection(a)&&a.isCollapsed()||0===a.getNodes().length?null:b.$generateHtmlFromNodes(c,a)}; | ||
exports.$getLexicalContent=function(c){let a=y.$getSelection();if(null==a)throw Error("Expected valid LexicalSelection");if(y.$isRangeSelection(a)&&a.isCollapsed()||0===a.getNodes().length)return null;var d=JSON,h=d.stringify;let g=[],f=y.$getRoot().getChildren();for(let e=0;e<f.length;e++)G(c,a,f[e],g);return h.call(d,{namespace:c._config.namespace,nodes:g})};exports.$insertDataTransferForPlainText=A; | ||
exports.$insertDataTransferForRichText=function(c,a,d){let h=c.getData("text/html");var g=c.getData("application/x-lexical-editor");if(g)try{var f=JSON.parse(g);if(f.namespace===d._config.namespace&&Array.isArray(f.nodes)){var e=f.nodes;g=[];for(f=0;f<e.length;f++)g.push(y.$parseSerializedNode(e[f]));return B(d,g,a)}}catch{}if(h)try{let m=(new DOMParser).parseFromString(h,"text/html");return B(d,b.$generateNodesFromDOM(d,m),a)}catch{}A(c,a)} |
@@ -12,11 +12,11 @@ { | ||
"license": "MIT", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"main": "LexicalClipboard.js", | ||
"peerDependencies": { | ||
"lexical": "0.3.2" | ||
"lexical": "0.3.3" | ||
}, | ||
"dependencies": { | ||
"@lexical/utils": "0.3.2", | ||
"@lexical/selection": "0.3.2", | ||
"@lexical/html": "0.3.2" | ||
"@lexical/utils": "0.3.3", | ||
"@lexical/selection": "0.3.3", | ||
"@lexical/html": "0.3.3" | ||
}, | ||
@@ -23,0 +23,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19521
337
+ Added@lexical/html@0.3.3(transitive)
+ Added@lexical/list@0.3.3(transitive)
+ Added@lexical/selection@0.3.3(transitive)
+ Added@lexical/table@0.3.3(transitive)
+ Added@lexical/utils@0.3.3(transitive)
+ Addedlexical@0.3.3(transitive)
- Removed@lexical/html@0.3.2(transitive)
- Removed@lexical/list@0.3.2(transitive)
- Removed@lexical/selection@0.3.2(transitive)
- Removed@lexical/table@0.3.2(transitive)
- Removed@lexical/utils@0.3.2(transitive)
- Removedlexical@0.3.2(transitive)
Updated@lexical/html@0.3.3
Updated@lexical/selection@0.3.3
Updated@lexical/utils@0.3.3