/** * 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{$getNearestNodeOfType as t,removeClassNamesFromElement as e,addClassNamesToElement as n,isHTMLElement as r,mergeRegister as i,$findMatchingParent as s,calculateZoomLevel as o}from"@lexical/utils";import{$getSelection as l,$isRangeSelection as c,$isRootOrShadowRoot as a,$createParagraphNode as g,$isElementNode as u,$isLeafNode as h,$setPointFromCaret as f,$normalizeCaret as d,$getChildCaret as p,ElementNode as m,buildImportMap as _,$isParagraphNode as y,$applyNodeReplacement as C,$createTextNode as T,createCommand as S,COMMAND_PRIORITY_LOW as v,KEY_ARROW_DOWN_COMMAND as x,KEY_ARROW_UP_COMMAND as k,KEY_ESCAPE_COMMAND as b,KEY_SPACE_COMMAND as N,$getNearestNodeFromDOMNode as L,KEY_ARROW_LEFT_COMMAND as F,getNearestEditorFromDOMNode as P,$getNodeByKey as A,INSERT_PARAGRAPH_COMMAND as O,$isTextNode as E,TextNode as w}from"lexical";import{getStyleObjectFromCSS as D}from"@lexical/selection";function I(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 M(t){let e=1,n=t.getParent();for(;null!=n;){if(nt(n)){const t=n.getParent();if(at(t)){e++,n=t.getParent();continue}I(40)}return e}return e}function R(t){let e=t.getParent();at(e)||I(40);let n=e;for(;null!==n;)n=n.getParent(),at(n)&&(e=n);return e}function B(t){let e=[];const n=t.getChildren().filter(nt);for(let t=0;t0&&t.append(...i),e.remove()}function q(){const e=l();if(c(e)){const n=new Set,r=e.getNodes(),i=e.anchor.getNode();if(J(i,r))n.add(R(i));else for(let e=0;es.append(t)));const o=et(),l=ct(r);o.append(l),U(l,t.getNextSiblings()),n.insertBefore(i),n.insertAfter(o),n.replace(t)}}}function G(){const t=l();if(!c(t)||!t.isCollapsed())return!1;const e=t.anchor.getNode();if(!nt(e)||0!==e.getChildrenSize())return!1;const n=R(e),r=e.getParent();at(r)||I(40);const i=r.getParent();let s;if(a(i))s=g(),n.insertAfter(s);else{if(!nt(i))return!1;s=et(),i.insertAfter(s)}s.setTextStyle(t.style).setTextFormat(t.format).select();const o=e.getNextSiblings();if(o.length>0){const t=ct(r.getListType());if(nt(s)){const e=et();e.append(t),s.insertAfter(e)}else s.insertAfter(t);t.append(...o)}return function(t){let e=t;for(;null==e.getNextSibling()&&null==e.getPreviousSibling();){const t=e.getParent();if(null==t||!nt(t)&&!at(t))break;e=t}e.remove()}(e),!0}function Q(...t){const e=[];for(const n of t)if(n&&"string"==typeof n)for(const[t]of n.matchAll(/\S+/g))e.push(t);return e}class Y extends m{$config(){return this.config("listitem",{$transform:t=>{if(null==t.__checked)return;const e=t.getParent();at(e)&&"check"!==e.getListType()&&null!=t.getChecked()&&t.setChecked(void 0)},extends:m,importDOM:_({li:()=>({conversion:Z,priority:0})})})}constructor(t=1,e=void 0,n){super(n),this.__value=void 0===t?1:t,this.__checked=e}afterCloneFrom(t){super.afterCloneFrom(t),this.__value=t.__value,this.__checked=t.__checked}createDOM(t){const e=document.createElement("li");return this.updateListItemDOM(null,e,t),e}updateListItemDOM(t,r,i){const s=this.getParent();at(s)&&"check"===s.getListType()&&function(t,e,n,r){at(e.getFirstChild())?(t.removeAttribute("role"),t.removeAttribute("tabIndex"),t.removeAttribute("aria-checked")):(t.setAttribute("role","checkbox"),t.setAttribute("tabIndex","-1"),n&&e.__checked===n.__checked||t.setAttribute("aria-checked",e.getChecked()?"true":"false"))}(r,this,t),r.value=this.__value,function(t,r,i){const s=[],o=[],l=r.list,c=l?l.listitem:void 0;let a;l&&l.nested&&(a=l.nested.listitem);void 0!==c&&s.push(...Q(c));if(l){const t=i.getParent(),e=at(t)&&"check"===t.getListType(),n=i.getChecked();e&&!n||o.push(l.listitemUnchecked),e&&n||o.push(l.listitemChecked),e&&s.push(n?l.listitemChecked:l.listitemUnchecked)}if(void 0!==a){const t=Q(a);i.getChildren().some((t=>at(t)))?s.push(...t):o.push(...t)}o.length>0&&e(t,...o);s.length>0&&n(t,...s)}(r,i.theme,this);const o=t?t.__style:"",l=this.__style;o!==l&&(""===l?r.removeAttribute("style"):r.style.cssText=l),function(t,e,n){const r=D(e.__textStyle);for(const e in r)t.style.setProperty(`--listitem-marker-${e}`,r[e]);if(n)for(const e in D(n.__textStyle))e in r||t.style.removeProperty(`--listitem-marker-${e}`)}(r,this,t)}updateDOM(t,e,n){const r=e;return this.updateListItemDOM(t,r,n),!1}updateFromJSON(t){return super.updateFromJSON(t).setValue(t.value).setChecked(t.checked)}exportDOM(t){const e=this.createDOM(t._config),n=this.getFormatType();n&&(e.style.textAlign=n);const r=this.getDirection();return r&&(e.dir=r),{element:e}}exportJSON(){return{...super.exportJSON(),checked:this.getChecked(),value:this.getValue()}}append(...t){for(let e=0;e{t.append(e)}))),this.remove(),0===n.getChildrenSize()&&n.remove(),t}insertAfter(t,e=!0){const n=this.getParentOrThrow();if(at(n)||I(39),nt(t))return super.insertAfter(t,e);const r=this.getNextSiblings();if(n.insertAfter(t,e),0!==r.length){const i=ct(n.getListType());r.forEach((t=>i.append(t))),t.insertAfter(i,e)}return t}remove(t){const e=this.getPreviousSibling(),n=this.getNextSibling();super.remove(t),e&&n&&K(e)&&K(n)&&(z(e.getFirstChild(),n.getFirstChild()),n.remove())}insertNewAfter(t,e=!0){const n=et().updateFromJSON(this.exportJSON()).setChecked(!this.getChecked()&&void 0);return this.insertAfter(n,e),n}collapseAtStart(t){const e=g();this.getChildren().forEach((t=>e.append(t)));const n=this.getParentOrThrow(),r=n.getParentOrThrow(),i=nt(r);if(1===n.getChildrenSize())if(i)n.remove(),r.select();else{n.insertBefore(e),n.remove();const r=t.anchor,i=t.focus,s=e.getKey();"element"===r.type&&r.getNode().is(this)&&r.set(s,r.offset,"element"),"element"===i.type&&i.getNode().is(this)&&i.set(s,i.offset,"element")}else n.insertBefore(e),this.remove();return!0}getValue(){return this.getLatest().__value}setValue(t){const e=this.getWritable();return e.__value=t,e}getChecked(){const t=this.getLatest();let e;const n=this.getParent();return at(n)&&(e=n.getListType()),"check"===e?Boolean(t.__checked):void 0}setChecked(t){const e=this.getWritable();return e.__checked=t,e}toggleChecked(){const t=this.getWritable();return t.setChecked(!t.__checked)}getIndent(){const t=this.getParent();if(null===t||!this.isAttached())return this.getLatest().__indent;let e=t.getParentOrThrow(),n=0;for(;nt(e);)e=e.getParentOrThrow().getParentOrThrow(),n++;return n}setIndent(t){"number"!=typeof t&&I(117),(t=Math.floor(t))>=0||I(199);let e=this.getIndent();for(;e!==t;)e{!function(t){const e=t.getNextSibling();at(e)&&t.getListType()===e.getListType()&&z(t,e)}(t),H(t)},extends:m,importDOM:_({ol:()=>({conversion:ot,priority:0}),ul:()=>({conversion:ot,priority:0})})})}constructor(t="number",e=1,n){super(n);const r=lt[t]||t;this.__listType=r,this.__tag="number"===r?"ol":"ul",this.__start=e}afterCloneFrom(t){super.afterCloneFrom(t),this.__listType=t.__listType,this.__tag=t.__tag,this.__start=t.__start}getTag(){return this.getLatest().__tag}setListType(t){const e=this.getWritable();return e.__listType=t,e.__tag="number"===t?"ol":"ul",e}getListType(){return this.getLatest().__listType}getStart(){return this.getLatest().__start}setStart(t){const e=this.getWritable();return e.__start=t,e}createDOM(t,e){const n=this.__tag,r=document.createElement(n);return 1!==this.__start&&r.setAttribute("start",String(this.__start)),r.__lexicalListType=this.__listType,it(r,t.theme,this),r}updateDOM(t,e,n){return t.__tag!==this.__tag||(it(e,n.theme,this),!1)}updateFromJSON(t){return super.updateFromJSON(t).setListType(t.listType).setStart(t.start)}exportDOM(t){const e=this.createDOM(t._config,t);return r(e)&&(1!==this.__start&&e.setAttribute("start",String(this.__start)),"check"===this.__listType&&e.setAttribute("__lexicalListType","check")),{element:e}}exportJSON(){return{...super.exportJSON(),listType:this.getListType(),start:this.getStart(),tag:this.getTag()}}canBeEmpty(){return!1}canIndent(){return!1}splice(t,e,n){let r=n;for(let t=0;t1?s.push(...t):o.push(...t)}}o.length>0&&e(t,...o),s.length>0&&n(t,...s)}function st(t){const e=[];for(let n=0;n1&&t.forEach((t=>{at(t)&&e.push(W(t))}))}else e.push(W(r))}return e}function ot(t){const e=t.nodeName.toLowerCase();let n=null;if("ol"===e){n=ct("number",t.start)}else"ul"===e&&(n=function(t){if("check"===t.getAttribute("__lexicallisttype")||t.classList.contains("contains-task-list"))return!0;for(const e of t.childNodes)if(r(e)&&e.hasAttribute("aria-checked"))return!0;return!1}(t)?ct("check"):ct("bullet"));return{after:st,node:n}}const lt={ol:"number",ul:"bullet"};function ct(t="number",e=1){return C(new rt(t,e))}function at(t){return t instanceof rt}const gt=S("INSERT_CHECK_LIST_COMMAND");function ut(t){return i(t.registerCommand(gt,(()=>($("check"),!0)),v),t.registerCommand(x,(e=>mt(e,t,!1)),v),t.registerCommand(k,(e=>mt(e,t,!0)),v),t.registerCommand(b,(()=>{if(null!=pt()){const e=t.getRootElement();return null!=e&&e.focus(),!0}return!1}),v),t.registerCommand(N,(e=>{const n=pt();return!(null==n||!t.isEditable())&&(t.update((()=>{const t=L(n);nt(t)&&(e.preventDefault(),t.toggleChecked())})),!0)}),v),t.registerCommand(F,(e=>t.getEditorState().read((()=>{const n=l();if(c(n)&&n.isCollapsed()){const{anchor:r}=n,i="element"===r.type;if(i||0===r.offset){const n=r.getNode(),o=s(n,(t=>u(t)&&!t.isInline()));if(nt(o)){const r=o.getParent();if(at(r)&&"check"===r.getListType()&&(i||o.getFirstDescendant()===n)){const n=t.getElementByKey(o.__key);if(null!=n&&document.activeElement!==n)return n.focus(),e.preventDefault(),!0}}}}return!1}))),v),t.registerRootListener(((t,e)=>{null!==t&&(t.addEventListener("click",ft),t.addEventListener("pointerdown",dt)),null!==e&&(e.removeEventListener("click",ft),e.removeEventListener("pointerdown",dt))})))}function ht(t,e){const n=t.target;if(!r(n))return;const i=n.firstChild;if(r(i)&&("UL"===i.tagName||"OL"===i.tagName))return;const s=n.parentNode;if(!s||"check"!==s.__lexicalListType)return;const l=n.getBoundingClientRect(),c=o(n),a=t.clientX/c,g=window.getComputedStyle?window.getComputedStyle(n,"::before"):{width:"0px"},u=parseFloat(g.width),h="touch"===t.pointerType?32:0;("rtl"===n.dir?al.right-u-h:a>l.left-h&&a{if(r(t.target)){const e=t.target,n=P(e);null!=n&&n.isEditable()&&n.update((()=>{const t=L(e);nt(t)&&(e.focus(),t.toggleChecked())}))}}))}function dt(t){ht(t,(()=>{t.preventDefault()}))}function pt(){const t=document.activeElement;return r(t)&&"LI"===t.tagName&&null!=t.parentNode&&"check"===t.parentNode.__lexicalListType?t:null}function mt(t,e,n){const r=pt();return null!=r&&e.update((()=>{const i=L(r);if(!nt(i))return;const s=function(t,e){let n=e?t.getPreviousSibling():t.getNextSibling(),r=t;for(;null==n&&nt(r);)r=r.getParentOrThrow().getParent(),null!=r&&(n=e?r.getPreviousSibling():r.getNextSibling());for(;nt(n);){const t=e?n.getLastChild():n.getFirstChild();if(!at(t))return n;n=e?t.getLastChild():t.getFirstChild()}return null}(i,n);if(null!=s){s.selectStart();const n=e.getElementByKey(s.__key);null!=n&&(t.preventDefault(),setTimeout((()=>{n.focus()}),0))}})),!1}const _t=S("UPDATE_LIST_START_COMMAND"),yt=S("INSERT_UNORDERED_LIST_COMMAND"),Ct=S("INSERT_ORDERED_LIST_COMMAND"),Tt=S("REMOVE_LIST_COMMAND");function St(t){return i(t.registerCommand(Ct,(()=>($("number"),!0)),v),t.registerCommand(_t,(t=>{const{listNodeKey:e,newStart:n}=t,r=A(e);return!!at(r)&&("number"===r.getListType()&&(r.setStart(n),H(r)),!0)}),v),t.registerCommand(yt,(()=>($("bullet"),!0)),v),t.registerCommand(Tt,(()=>(q(),!0)),v),t.registerCommand(O,(()=>G()),v),t.registerNodeTransform(Y,(t=>{const e=t.getFirstChild();if(e){if(E(e)){const n=e.getStyle(),r=e.getFormat();t.getTextStyle()!==n&&t.setTextStyle(n),t.getTextFormat()!==r&&t.setTextFormat(r)}}else{const e=l();c(e)&&(e.style!==t.getTextStyle()||e.format!==t.getTextFormat())&&e.isCollapsed()&&t.is(e.anchor.getNode())&&t.setTextStyle(e.style).setTextFormat(e.format)}})),t.registerNodeTransform(w,(t=>{const e=t.getParent();if(nt(e)&&t.is(e.getFirstChild())){const n=t.getStyle(),r=t.getFormat();n===e.getTextStyle()&&r===e.getTextFormat()||e.setTextStyle(n).setTextFormat(r)}})))}function vt(t){const e=t=>{const e=t.getParent();if(at(t.getFirstChild())||!at(e))return;const n=s(t,(t=>nt(t)&&at(t.getParent())&&nt(t.getPreviousSibling())));if(null===n&&t.getIndent()>0)t.setIndent(0);else if(nt(n)){const r=n.getPreviousSibling();if(nt(r)){const n=function(t){let e=t,n=e.getFirstChild();for(;at(n);){const t=n.getLastChild();if(!nt(t))break;e=t,n=e.getFirstChild()}return e}(r),i=n.getParent();if(at(i)){const n=M(i);n+1{const n=[t];for(;n.length>0;){const t=n.shift();if(at(t))for(const r of t.getChildren())if(nt(r)){e(r);const t=r.getFirstChild();at(t)&&n.push(t)}}}))}function xt(t,e){t.update((()=>$(e)))}function kt(t){t.update((()=>q()))}export{et as $createListItemNode,ct as $createListNode,M as $getListDepth,G as $handleListInsertParagraph,$ as $insertList,nt as $isListItemNode,at as $isListNode,q as $removeList,gt as INSERT_CHECK_LIST_COMMAND,Ct as INSERT_ORDERED_LIST_COMMAND,yt as INSERT_UNORDERED_LIST_COMMAND,Y as ListItemNode,rt as ListNode,Tt as REMOVE_LIST_COMMAND,_t as UPDATE_LIST_START_COMMAND,xt as insertList,ut as registerCheckList,St as registerList,vt as registerListStrictIndentTransform,kt as removeList};