UNPKG

@lexical/clipboard

Version:

This package provides the copy/paste functionality for Lexical.

10 lines (8 loc) 4.89 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ "use strict";var e=require("@lexical/html"),t=require("@lexical/selection"),n=require("@lexical/utils"),o=require("lexical");function r(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function i(t,n=o.$getSelection()){return null==n&&r(166),o.$isRangeSelection(n)&&n.isCollapsed()||0===n.getNodes().length?"":e.$generateHtmlFromNodes(t,n)}function l(e,t=o.$getSelection()){return null==t&&r(166),o.$isRangeSelection(t)&&t.isCollapsed()||0===t.getNodes().length?null:JSON.stringify(c(e,t))}function s(e,t,n){e.dispatchCommand(o.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND,{nodes:t,selection:n})||n.insertNodes(t)}function a(e,n,i,l=[]){let s=null===n||i.isSelected(n);const c=o.$isElementNode(i)&&i.excludeFromCopy("html");let d=i;if(null!==n){let e=o.$cloneWithProperties(i);e=o.$isTextNode(e)&&null!==n?t.$sliceSelectedTextNodeContent(n,e):e,d=e}const u=o.$isElementNode(d)?d.getChildren():[],p=function(e){const t=e.exportJSON(),n=e.constructor;if(t.type!==n.getType()&&r(58,n.name),o.$isElementNode(e)){const e=t.children;Array.isArray(e)||r(59,n.name)}return t}(d);if(o.$isTextNode(d)){const e=d.__text;e.length>0?p.text=e:s=!1}for(let t=0;t<u.length;t++){const r=u[t],l=a(e,n,r,p.children);!s&&o.$isElementNode(i)&&l&&i.extractWithChild(r,n,"clone")&&(s=!0)}if(s&&!c)l.push(p);else if(Array.isArray(p.children))for(let e=0;e<p.children.length;e++){const t=p.children[e];l.push(t)}return s}function c(e,t){const n=[],r=o.$getRoot().getChildren();for(let o=0;o<r.length;o++){a(e,t,r[o],n)}return{namespace:e._config.namespace,nodes:n}}function d(e){const n=[];for(let r=0;r<e.length;r++){const i=e[r],l=o.$parseSerializedNode(i);o.$isTextNode(l)&&t.$addNodeStyle(l),n.push(l)}return n}let u=null;function p(e,t,n){if(void 0===n){const t=o.getDOMSelection(e._window);if(!t)return!1;const r=t.anchorNode,i=t.focusNode;if(null!==r&&null!==i&&!o.isSelectionWithinEditor(e,r,i))return!1;const l=o.$getSelection();if(null===l)return!1;n=g(l)}t.preventDefault();const r=t.clipboardData;return null!==r&&(x(r,n),!0)}const f=[["text/html",i],["application/x-lexical-editor",l]];function g(e=o.$getSelection()){const t={"text/plain":e?e.getTextContent():""};if(e){const n=o.$getEditor();for(const[o,r]of f){const i=r(n,e);null!==i&&(t[o]=i)}}return t}function x(e,t){for(const n in t){const o=t[n];void 0!==o&&e.setData(n,o)}}exports.$generateJSONFromSelectedNodes=c,exports.$generateNodesFromSerializedNodes=d,exports.$getClipboardDataFromSelection=g,exports.$getHtmlContent=i,exports.$getLexicalContent=l,exports.$insertDataTransferForPlainText=function(e,t){const n=e.getData("text/plain")||e.getData("text/uri-list");null!=n&&t.insertRawText(n)},exports.$insertDataTransferForRichText=function(t,n,r){const i=t.getData("application/x-lexical-editor");if(i)try{const e=JSON.parse(i);if(e.namespace===r._config.namespace&&Array.isArray(e.nodes)){return s(r,d(e.nodes),n)}}catch(e){}const l=t.getData("text/html"),a=t.getData("text/plain");if(l&&a!==l)try{const t=(new DOMParser).parseFromString(function(e){if(window.trustedTypes&&window.trustedTypes.createPolicy){return window.trustedTypes.createPolicy("lexical",{createHTML:e=>e}).createHTML(e)}return e}(l),"text/html");return s(r,e.$generateNodesFromDOM(r,t),n)}catch(e){}const c=a||t.getData("text/uri-list");if(null!=c)if(o.$isRangeSelection(n)){const e=c.split(/(\r?\n|\t)/);""===e[e.length-1]&&e.pop();for(let t=0;t<e.length;t++){const n=o.$getSelection();if(o.$isRangeSelection(n)){const r=e[t];"\n"===r||"\r\n"===r?n.insertParagraph():"\t"===r?n.insertNodes([o.$createTabNode()]):n.insertText(r)}}}else n.insertRawText(c)},exports.$insertGeneratedNodes=s,exports.copyToClipboard=async function(e,t,r){if(null!==u)return!1;if(null!==t)return new Promise(((n,o)=>{e.update((()=>{n(p(e,t,r))}))}));const i=e.getRootElement(),l=e._window||window,s=window.document,a=o.getDOMSelection(l);if(null===i||null===a)return!1;const c=s.createElement("span");c.style.cssText="position: fixed; top: -1000px;",c.append(s.createTextNode("#")),i.append(c);const d=new Range;return d.setStart(c,0),d.setEnd(c,1),a.removeAllRanges(),a.addRange(d),new Promise(((t,i)=>{const l=e.registerCommand(o.COPY_COMMAND,(o=>(n.objectKlassEquals(o,ClipboardEvent)&&(l(),null!==u&&(window.clearTimeout(u),u=null),t(p(e,o,r))),!0)),o.COMMAND_PRIORITY_CRITICAL);u=window.setTimeout((()=>{l(),u=null,t(!1)}),50),s.execCommand("copy"),c.remove()}))},exports.setLexicalClipboardDataTransfer=x;