@lexical/clipboard
Version:
This package provides the copy/paste functionality for Lexical.
10 lines (8 loc) • 8.18 kB
JavaScript
/**
* 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.
*
*/
import{LexicalBuilder as t,getExtensionDependencyFromEditor as e}from"@lexical/extension";import{$generateHtmlFromNodes as n,$generateNodesFromDOM as r}from"@lexical/html";import{$addNodeStyle as o,$sliceSelectedTextNodeContent as i}from"@lexical/selection";import{objectKlassEquals as l}from"@lexical/utils";import{$isRangeSelection as c,$getSelection as s,$createTabNode as a,SELECTION_INSERT_CLIPBOARD_NODES_COMMAND as u,$caretFromPoint as f,$isTextPointCaret as d,$getCaretRange as p,$getChildCaret as m,$getRoot as g,$isTextNode as x,$isElementNode as y,$parseSerializedNode as h,getDOMSelection as w,COPY_COMMAND as T,COMMAND_PRIORITY_CRITICAL as v,isSelectionWithinEditor as D,$getEditor as C,defineExtension as b,shallowMergeConfig as P,safeCast as N,$splitAtPointCaretNext as S,$setSelectionFromCaretRange as E,$getCollapsedCaretRange as R,$getNearestNodeFromDOMNode as A,$getTextPointCaret as M,$getChildCaretAtIndex as F,$getCaretRangeInDirection as O,$caretRangeFromSelection as K,$comparePointCaretNext as $}from"lexical";function _(t,...e){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",t);for(const t of e)r.append("v",t);throw n.search=r.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function J(t,e=s()){return null==e&&_(166),c(e)&&e.isCollapsed()||0===e.getNodes().length?"":n(t,e)}function L(t,e=s()){return null==e&&_(166),c(e)&&e.isCollapsed()||0===e.getNodes().length?null:JSON.stringify(X(t,e))}function j(t,e){const n=t.getData("text/plain")||t.getData("text/uri-list");null!=n&&e.insertRawText(n)}function I(t,e,n){const o=t.getData("application/x-lexical-editor");if(o)try{const t=JSON.parse(o);if(t.namespace===n._config.namespace&&Array.isArray(t.nodes)){return z(n,Y(t.nodes),e)}}catch(t){console.error(t)}const i=t.getData("text/html"),l=t.getData("text/plain");if(i&&l!==i)try{const t=(new DOMParser).parseFromString(function(t){if(window.trustedTypes&&window.trustedTypes.createPolicy){return window.trustedTypes.createPolicy("lexical",{createHTML:t=>t}).createHTML(t)}return t}(i),"text/html");return z(n,r(n,t),e)}catch(t){console.error(t)}const u=l||t.getData("text/uri-list");if(null!=u)if(c(e)){const t=u.split(/(\r?\n|\t)/);""===t[t.length-1]&&t.pop();for(let e=0;e<t.length;e++){const n=s();if(c(n)){const r=t[e];"\n"===r||"\r\n"===r?n.insertParagraph():"\t"===r?n.insertNodes([a()]):n.insertText(r)}}}else e.insertRawText(u)}const k="application/x-lexical-drag";function B(t,e){const n={editorKey:e.getKey()};t.setData(k,JSON.stringify(n))}function H(t){const e=function(t,e){if(void 0!==document.caretRangeFromPoint){const n=document.caretRangeFromPoint(t,e);return null===n?null:{node:n.startContainer,offset:n.startOffset}}if("undefined"!==document.caretPositionFromPoint){const n=document.caretPositionFromPoint(t,e);return null===n?null:{node:n.offsetNode,offset:n.offset}}return null}(t.clientX,t.clientY);if(null===e)return null;const n=A(e.node);if(null===n)return null;if(x(n))return M(n,"next",e.offset);if(y(n))return F(n,e.offset,"next");const r=n.getParent();return null===r?null:F(r,n.getIndexWithinParent()+1,"next")}function U(t,e,n){const r=t.dataTransfer;if(null===r)return!1;const o=function(t){const e=t.getData(k);if(!e)return null;let n;try{n=JSON.parse(e)}catch(t){return null}return null!==(r=n)&&"object"==typeof r&&"editorKey"in r&&"string"==typeof r.editorKey?n:null;var r}(r);if(null===o)return!1;const i=H(t);if(null===i)return!1;const l=S(i);if(null===l)return!1;const a=o.editorKey===e.getKey(),u=s();if(a){if(!c(u)||u.isCollapsed())return!1;if(function(t,e){const{anchor:n,focus:r}=O(K(e),"next");return $(n,t)<0&&$(t,r)<0}(i,u))return t.preventDefault(),!0;u.removeText()}if(!l.origin.isAttached())return t.preventDefault(),!0;if(n(r,E(R(l)),e),!a){const t=e.getRootElement(),n=t?t.ownerDocument:null,r=n?function(t,e){const n=e.querySelectorAll('[data-lexical-editor="true"]');for(const e of Array.from(n)){const n=e.__lexicalEditor;if(n&&n.getKey()===t)return e}return null}(o.editorKey,n):null;null!==r&&r.dispatchEvent(new InputEvent("beforeinput",{bubbles:!0,cancelable:!0,inputType:"deleteByDrag"}))}return t.preventDefault(),!0}function W(t,e){return U(t,e,I)}function q(t,e){return U(t,e,(t,e)=>j(t,e))}function z(t,e,n){t.dispatchCommand(u,{nodes:e,selection:n})||(n.insertNodes(e),function(t){if(c(t)&&t.isCollapsed()){const e=t.anchor;let n=null;const r=f(e,"previous");if(r)if(d(r))n=r.origin;else{const t=p(r,m(g(),"next").getFlipped());for(const e of t){if(x(e.origin)){n=e.origin;break}if(y(e.origin)&&!e.origin.isInline())break}}if(n&&x(n)){const e=n.getFormat(),r=n.getStyle();t.format===e&&t.style===r||(t.format=e,t.style=r,t.dirty=!0)}}}(n))}function G(t,e,n,r=[]){let o=null===e||n.isSelected(e);const l=y(n)&&n.excludeFromCopy("html");let c=n;null!==e&&x(c)&&(c=i(e,c,"clone"));const s=y(c)?c.getChildren():[],a=function(t){const e=t.exportJSON(),n=t.constructor;if(e.type!==n.getType()&&_(58,n.name),y(t)){const t=e.children;Array.isArray(t)||_(59,n.name)}return e}(c);x(c)&&0===c.getTextContentSize()&&(o=!1);for(let r=0;r<s.length;r++){const i=s[r],l=G(t,e,i,a.children);!o&&y(n)&&l&&n.extractWithChild(i,e,"clone")&&(o=!0)}if(o&&!l)r.push(a);else if(Array.isArray(a.children))for(let t=0;t<a.children.length;t++){const e=a.children[t];r.push(e)}return o}function X(t,e){const n=[],r=g().getChildren();for(let o=0;o<r.length;o++){G(t,e,r[o],n)}return{namespace:t._config.namespace,nodes:n}}function Y(t){const e=[];for(let n=0;n<t.length;n++){const r=t[n],i=h(r);x(i)&&o(i),e.push(i)}return e}let Q=null;async function V(t,e,n){if(null!==Q)return!1;if(null!==e)return new Promise((r,o)=>{t.update(()=>{r(Z(t,e,n))})});const r=t.getRootElement(),o=t._window||window,i=o.document,c=w(o);if(null===r||null===c)return!1;const s=i.createElement("span");s.style.position="fixed",s.style.top="-1000px",s.append(i.createTextNode("#")),r.append(s);const a=new Range;return a.setStart(s,0),a.setEnd(s,1),c.removeAllRanges(),c.addRange(a),new Promise((e,r)=>{const c=t.registerCommand(T,r=>(l(r,ClipboardEvent)&&(c(),null!==Q&&(o.clearTimeout(Q),Q=null),e(Z(t,r,n))),!0),v);Q=o.setTimeout(()=>{c(),Q=null,e(!1)},50),i.execCommand("copy"),s.remove()})}function Z(t,e,n){if(void 0===n){const e=w(t._window),r=s();if(!r||r.isCollapsed())return!1;if(!e)return!1;const o=e.anchorNode,i=e.focusNode;if(null!==o&&null!==i&&!D(t,o,i))return!1;n=et(r)}e.preventDefault();const r=e.clipboardData;return null!==r&&(nt(r,n),!0)}const tt=[["text/html",J],["application/x-lexical-editor",L]];function et(n=s()){return function(t,e){const n={"text/plain":""};for(const[r,o]of Object.entries(t)){const t=ot(o,e);null!==t&&(n[r]=t)}return n}(function(){const n=C(),r=t.maybeFromEditor(n);if(r&&r.hasExtensionByName(it.name))return e(n,it).output;return rt}(),n)}function nt(t,e){for(const[n]of tt)void 0===e[n]&&t.setData(n,"");for(const n in e){const r=e[n];void 0!==r&&t.setData(n,r)}}const rt={"application/x-lexical-editor":[(t,e)=>t?L(C(),t):e()],"text/html":[(t,e)=>t?J(C(),t):e()],"text/plain":[(t,e)=>t?t.getTextContent():e()]};function ot(t,e){const n=r=>t[r]?t[r](e,n.bind(null,r-1)):null;return n(t.length-1)}const it=b({build:(t,e,n)=>e.$exportMimeType,config:N({$exportMimeType:rt}),mergeConfig(t,e){const n=P(t,e);if(e.$exportMimeType){const r={...t.$exportMimeType};for(const[t,n]of Object.entries(e.$exportMimeType))r[t]=[...r[t],...n];n.$exportMimeType=r}return n},name:"@lexical/clipboard/GetClipboardData"});export{X as $generateJSONFromSelectedNodes,Y as $generateNodesFromSerializedNodes,et as $getClipboardDataFromSelection,J as $getHtmlContent,L as $getLexicalContent,q as $handlePlainTextDrop,W as $handleRichTextDrop,j as $insertDataTransferForPlainText,I as $insertDataTransferForRichText,z as $insertGeneratedNodes,B as $writeDragSourceToDataTransfer,V as copyToClipboard,nt as setLexicalClipboardDataTransfer};