/** * 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{$generateHtmlFromNodes as e}from"@lexical/html";import{$isLinkNode as t}from"@lexical/link";import{$isMarkNode as n}from"@lexical/mark";import{$isTableSelection as r}from"@lexical/table";import{$getSelection as o,$getRoot as i,$isRangeSelection as l,$isNodeSelection as s,$isElementNode as a,$isTextNode as c,$isParagraphNode as u,COMMAND_PRIORITY_CRITICAL as f}from"lexical";import{forwardRef as d,useState as h,useRef as m,useCallback as g,useEffect as p,useMemo as y}from"react";import{jsxs as $,jsx as b}from"react/jsx-runtime";const x=Object.freeze({"\t":"\\t","\n":"\\n"}),C=new RegExp(Object.keys(x).join("|"),"g"),S=Object.freeze({ancestorHasNextSibling:"|",ancestorIsLastChild:" ",hasNextSibling:"├",isLastChild:"└",selectedChar:"^",selectedLine:">"}),N=[e=>e.hasFormat("bold")&&"Bold",e=>e.hasFormat("code")&&"Code",e=>e.hasFormat("italic")&&"Italic",e=>e.hasFormat("strikethrough")&&"Strikethrough",e=>e.hasFormat("subscript")&&"Subscript",e=>e.hasFormat("superscript")&&"Superscript",e=>e.hasFormat("underline")&&"Underline",e=>e.hasFormat("highlight")&&"Highlight"],T=[e=>e.hasTextFormat("bold")&&"Bold",e=>e.hasTextFormat("code")&&"Code",e=>e.hasTextFormat("italic")&&"Italic",e=>e.hasTextFormat("strikethrough")&&"Strikethrough",e=>e.hasTextFormat("subscript")&&"Subscript",e=>e.hasTextFormat("superscript")&&"Superscript",e=>e.hasTextFormat("underline")&&"Underline",e=>e.hasTextFormat("highlight")&&"Highlight"],k=[e=>e.isDirectionless()&&"Directionless",e=>e.isUnmergeable()&&"Unmergeable"],j=[e=>e.isToken()&&"Token",e=>e.isSegmented()&&"Segmented"];function v(f,d,h,m,g=!1){const p=f.getEditorState(),y=f._config,$=f._compositionKey,b=f._editable;if(h){let t="";return p.read((()=>{t=function(e){const t=document.createElement("div");return t.innerHTML=e.trim(),K(t,0).innerHTML}(e(f))})),t}let x=" root\n";const N=p.read((()=>{const e=o();return L(i(),((r,o)=>{const i=`(${r.getKey()})`,f=r.getType()||"",d=r.isSelected();x+=`${d?S.selectedLine:" "} ${o.join(" ")} ${i} ${f} ${function(e,r,o=!1){const i=r?r(e,o):void 0;if(void 0!==i&&i.length>0)return i;if(c(e)){const t=e.getTextContent(),n=0===t.length?"(empty)":`"${B(t,o)}"`,r=function(e){return[w(e),E(e),F(e),H(e)].filter(Boolean).join(", ")}(e);return[n,0!==r.length?`{ ${r} }`:null].filter(Boolean).join(" ").trim()}if(t(e)){const t=e.getURL(),n=0===t.length?"(empty)":`"${B(t,o)}"`,r=function(e){return[_(e),D(e),O(e),H(e)].filter(Boolean).join(", ")}(e);return[n,0!==r.length?`{ ${r} }`:null].filter(Boolean).join(" ").trim()}if(n(e))return`ids: [ ${e.getIDs().join(", ")} ]`;if(u(e)){const t=function(e){let t=T.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();""!==t&&(t="format: "+t);return t}(e);let n=""!==t?`{ ${t} }`:"";return n+=e.__style?`(${e.__style})`:"",n}return""}(r,m,g)}\n`,x+=function({indent:e,isSelected:t,node:n,nodeKeyDisplay:r,selection:o,typeDisplay:i}){if(!c(n)||!l(o)||!t||a(n))return"";const u=o.anchor,f=o.focus;if(""===n.getTextContent()||u.getNode()===o.focus.getNode()&&u.offset===f.offset)return"";const[d,h]=function(e,t){const n=t.getStartEndPoints();if(s(t)||null===n)return[-1,-1];const[r,o]=n,i=e.getTextContent(),l=i.length;let a=-1,c=-1;if("text"===r.type&&"text"===o.type){const t=r.getNode(),n=o.getNode();t===n&&e===t&&r.offset!==o.offset?[a,c]=r.offset{t(e,n.concat(r===o-1?S.isLastChild:S.hasNextSibling)),a(e)&&L(e,t,n.concat(r===o-1?S.ancestorIsLastChild:S.ancestorHasNextSibling))}))}function B(e,t=!1){const n=Object.entries(x).reduce(((e,[t,n])=>e.replace(new RegExp(t,"g"),String(n))),e);return t?n.replace(/[^\s]/g,"*"):n}function E(e){let t=k.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();return""!==t&&(t="detail: "+t),t}function F(e){let t=j.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();return""!==t&&(t="mode: "+t),t}function w(e){let t=N.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();return""!==t&&(t="format: "+t),t}function _(e){let t=e.getTarget();return null!=t&&(t="target: "+t),t}function D(e){let t=e.getRel();return null!=t&&(t="rel: "+t),t}function O(e){let t=e.getTitle();return null!=t&&(t="title: "+t),t}function H(e){if(!e.__state)return!1;const t=[];for(const[n,r]of e.__state.knownState.entries()){if(n.isEqual(r,n.defaultValue))continue;const e=JSON.stringify(n.unparse(r));t.push(`[${n.key}: ${e}]`)}let n=t.join(",");return""!==n&&(n="state: "+n),n}function K(e,t){const n=new Array(1+t++).join(" "),r=new Array(t-1).join(" ");let o;for(let i=0;i{const t=++H.current;c(e).then((e=>{t===H.current&&C(e)})).catch((e=>{t===H.current&&C(`Error rendering tree: ${e.message}\n\nStack:\n${e.stack}`)}))}),[c]);p((()=>{if(!w&&l._nodeMap.size>1e3&&(F(!0),!w))return;if(D.current!==l||O.current!==u){const e=D.current!==l;D.current=l,O.current=u,K(T),!S&&e&&y((e=>[...e,[Date.now(),l]]))}}),[l,K,T,w,S,u]);const A=d.length;p((()=>{if(L){let e;const t=()=>{const n=j.current;if(n===A-1)return void B(!1);const r=d[n][0],o=d[n+1][0];e=setTimeout((()=>{j.current++;const e=j.current,n=v.current;null!==n&&(n.value=String(e)),s(d[e][1]),t()}),o-r)};return t(),()=>{clearTimeout(e)}}}),[d,L,A,s]);return $("div",{className:o,children:[!w&&E?$("div",{style:{padding:20},children:[b("span",{style:{marginRight:20},children:"Detected large EditorState, this can impact debugging performance."}),b("button",{onClick:()=>{_(!0)},style:{background:"transparent",border:"1px solid white",color:"white",cursor:"pointer",padding:5},children:"Show full tree"})]}):null,w?null:b("button",{onClick:()=>(K(!T),void k(!T)),className:e,type:"button",children:T?"Tree":"Export DOM"}),!S&&(w||!E)&&A>2&&b("button",{onClick:()=>{a(!0),j.current=A-1,N(!0)},className:t,type:"button",children:"Time Travel"}),(w||!E)&&b("pre",{ref:f,children:x}),S&&(w||!E)&&$("div",{className:i,children:[b("button",{className:r,onClick:()=>{j.current===A-1&&(j.current=1),B(!L)},type:"button",children:L?"Pause":"Play"}),b("input",{className:n,ref:v,onChange:e=>{const t=Number(e.target.value),n=d[t];n&&(j.current=t,s(n[1]))},type:"range",min:"1",max:A-1}),b("button",{className:r,onClick:()=>{a(!1);const e=d.length-1,t=d[e];s(t[1]);const n=v.current;null!==n&&(n.value=String(e)),N(!1),B(!1)},type:"button",children:"Exit"})]})]})}));function I(e,t){const n=new Set;let r=0;for(const[o]of e._commands)n.add(e.registerCommand(o,(e=>(t((t=>{r+=1;const n=[...t];return n.push({index:r,payload:e,type:o.type?o.type:"UNKNOWN"}),n.length>10&&n.shift(),n})),!1)),f));return()=>n.forEach((e=>e()))}function P(e){const[t,n]=h([]);return p((()=>I(e,n)),[e]),y((()=>t),[t])}export{A as TreeView,v as generateContent,I as registerLexicalCommandLogger,P as useLexicalCommandsLog};