UNPKG

@lexical/list

Version:

This package provides the list feature for Lexical.

10 lines (8 loc) • 22.9 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/extension"),t=require("@lexical/utils"),n=require("lexical");function r(e,...t){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",e);for(const e of t)r.append("v",e);throw n.search=r.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(e){let t=1,n=e.getParent();for(;null!=n;){if(L(n)){const e=n.getParent();if(b(e)){t++,n=e.getParent();continue}r(40)}return t}return t}function s(e){let t=e.getParent();b(t)||r(40);let n=t;for(;null!==n;)n=n.getParent(),b(n)&&(t=n);return t}function o(e){let t=[];const n=e.getChildren().filter(L);for(let e=0;e<n.length;e++){const r=n[e],i=r.getFirstChild();b(i)?t=t.concat(o(i)):t.push(r)}return t}function l(e){return L(e)&&b(e.getFirstChild())}function a(e){return y().append(e)}function c(e,t){return L(e)&&(0===t.length||1===t.length&&e.is(t[0])&&0===e.getChildrenSize())}function d(e){const t=n.$getSelection();if(null!==t){let i=t.getNodes();if(n.$isRangeSelection(t)){const s=t.getStartEndPoints();null===s&&r(143);const[o]=s,l=o.getNode(),a=l.getParent();if(n.$isRootOrShadowRoot(l)){const e=l.getFirstChild();if(e)i=e.selectStart().getNodes();else{const e=n.$createParagraphNode();l.append(e),i=e.select().getNodes()}}else if(c(l,i)){const t=P(e);if(n.$isRootOrShadowRoot(a)){l.replace(t);const e=y();n.$isElementNode(l)&&(e.setFormat(l.getFormatType()),e.setIndent(l.getIndent())),t.append(e)}else if(L(l)){const e=l.getParentOrThrow();g(t,e.getChildren()),e.replace(t)}return}}const s=new Set;for(let t=0;t<i.length;t++){const r=i[t];if(n.$isElementNode(r)&&r.isEmpty()&&!L(r)&&!s.has(r.getKey())){u(r,e);continue}let o=n.$isLeafNode(r)?r.getParent():L(r)&&r.isEmpty()?r:null;for(;null!=o;){const t=o.getKey();if(b(o)){if(!s.has(t)){const n=P(e);g(n,o.getChildren()),o.replace(n),s.add(t)}break}{const r=o.getParent();if(n.$isRootOrShadowRoot(r)&&!s.has(t)){s.add(t),u(o,e);break}o=r}}}}}function g(e,t){e.splice(e.getChildrenSize(),0,t)}function u(e,t){if(b(e))return e;const r=e.getPreviousSibling(),i=e.getNextSibling(),s=y();let o;if(g(s,e.getChildren()),b(r)&&t===r.getListType())r.append(s),b(i)&&t===i.getListType()&&(g(r,i.getChildren()),i.remove()),o=r;else if(b(i)&&t===i.getListType())i.getFirstChildOrThrow().insertBefore(s),o=i;else{const n=P(t);n.append(s),e.replace(n),o=n}s.setFormat(e.getFormatType()),s.setIndent(e.getIndent());const l=n.$getSelection();return n.$isRangeSelection(l)&&(o.getKey()===l.anchor.key&&l.anchor.set(s.getKey(),l.anchor.offset,"element"),o.getKey()===l.focus.key&&l.focus.set(s.getKey(),l.focus.offset,"element")),e.remove(),o}function h(e,t){const n=e.getLastChild(),r=t.getFirstChild();n&&r&&l(n)&&l(r)&&(h(n.getFirstChild(),r.getFirstChild()),r.remove());const i=t.getChildren();i.length>0&&e.append(...i),t.remove()}function f(){const e=n.$getSelection();if(n.$isRangeSelection(e)){const r=new Set,i=e.getNodes(),l=e.anchor.getNode();if(c(l,i))r.add(s(l));else for(let e=0;e<i.length;e++){const o=i[e];if(n.$isLeafNode(o)){const e=t.$getNearestNodeOfType(o,N);null!=e&&r.add(s(e))}}for(const t of r){let r=t;const i=o(t);for(const t of i){const i=n.$createParagraphNode().setTextStyle(e.style).setTextFormat(e.format);g(i,t.getChildren()),r.insertAfter(i),r=i,t.__key===e.anchor.key&&n.$setPointFromCaret(e.anchor,n.$normalizeCaret(n.$getChildCaret(i,"next"))),t.__key===e.focus.key&&n.$setPointFromCaret(e.focus,n.$normalizeCaret(n.$getChildCaret(i,"next"))),t.remove()}t.remove()}}}function p(e){const t="check"!==e.getListType();let n=e.getStart();for(const r of e.getChildren())L(r)&&(r.getValue()!==n&&r.setValue(n),t&&null!=r.getLatest().__checked&&r.setChecked(void 0),b(r.getFirstChild())||n++)}function m(e){const t=new Set;if(l(e)||t.has(e.getKey()))return;const r=e.getParent(),i=e.getNextSibling(),s=e.getPreviousSibling();if(l(i)&&l(s)){const n=s.getFirstChild();if(b(n)){n.append(e);const r=i.getFirstChild();if(b(r)){g(n,r.getChildren()),i.remove(),t.add(i.getKey())}}}else if(l(i)){const t=i.getFirstChild();if(b(t)){const n=t.getFirstChild();null!==n&&n.insertBefore(e)}}else if(l(s)){const t=s.getFirstChild();b(t)&&t.append(e)}else if(b(r)){const t=n.$copyNode(e),o=n.$copyNode(r);t.append(o),o.append(e),s?s.insertAfter(t):i?i.insertBefore(t):r.append(t)}}function _(e){if(l(e))return;const t=e.getParent(),r=t?t.getParent():void 0;if(b(r?r.getParent():void 0)&&L(r)&&b(t)){const i=t?t.getFirstChild():void 0,s=t?t.getLastChild():void 0;if(e.is(i))r.insertBefore(e),t.isEmpty()&&r.remove();else if(e.is(s))r.insertAfter(e),t.isEmpty()&&r.remove();else{const i=n.$copyNode(e),s=n.$copyNode(t);i.append(s),e.getPreviousSiblings().forEach(e=>s.append(e));const o=n.$copyNode(e),l=n.$copyNode(t);o.append(l),g(l,e.getNextSiblings()),r.insertBefore(i),r.insertAfter(o),r.replace(e)}}}function C(e=!1){const t=n.$getSelection();if(!n.$isRangeSelection(t)||!t.isCollapsed())return!1;const i=t.anchor.getNode();let o=null;if(L(i)&&0===i.getChildrenSize())o=i;else if(n.$isTextNode(i)){const e=i.getParent();L(e)&&e.getChildren().every(e=>n.$isTextNode(e)&&""===e.getTextContent().trim())&&(o=e)}if(null===o)return!1;const l=s(o),a=o.getParent();b(a)||r(40);const c=a.getParent();let d;if(n.$isRootOrShadowRoot(c))d=n.$createParagraphNode(),l.insertAfter(d);else{if(!L(c))return!1;d=n.$copyNode(c),c.insertAfter(d)}d.setTextStyle(t.style).setTextFormat(t.format).select();const g=o.getNextSiblings();if(g.length>0){const t=e?function(e,t){return e.getStart()+t.getIndexWithinParent()}(a,o):1,r=n.$copyNode(a).setStart(t);if(L(d)){const e=n.$copyNode(d);e.append(r),d.insertAfter(e)}else d.insertAfter(r);r.append(...g)}return function(e){let t=e;for(;null==t.getNextSibling()&&null==t.getPreviousSibling();){const e=t.getParent();if(null==e||!L(e)&&!b(e))break;t=e}t.remove()}(o),!0}class N extends n.ElementNode{__value;__checked;$config(){return this.config("listitem",{$transform:e=>{const i=e.getParent();if(b(i))"check"!==i.getListType()&&null!=e.getChecked()&&e.setChecked(void 0);else if(i){const s=e.createParentElementNode();b(s)||r(340);const o=[e];for(const t of["previous","next"]){o.reverse();for(const{origin:r}of n.$getSiblingCaret(e,t)){if(!L(r))break;o.push(r)}}e.insertBefore(s),s.splice(0,0,o),n.$isRootOrShadowRoot(i)||(t.$insertNodeToNearestRootAtCaret(s,n.$rewindSiblingCaret(n.$getSiblingCaret(s,"next")),{$shouldSplit:()=>!1,removeEmptyDestination:!0}),i.isEmpty()&&i.isAttached()&&i.remove())}},extends:n.ElementNode,importDOM:n.buildImportMap({li:()=>({conversion:T,priority:0})})})}constructor(e=1,t=void 0,n){super(n),this.__value=void 0===e?1:e,this.__checked=t}afterCloneFrom(e){super.afterCloneFrom(e),this.__value=e.__value,this.__checked=e.__checked}createDOM(e){const t=document.createElement("li");return this.updateListItemDOM(null,t,e),t}updateListItemDOM(e,r,i){!function(e,t,n){const r=t.getParent();!b(r)||"check"!==r.getListType()||b(t.getFirstChild())?(e.removeAttribute("role"),e.removeAttribute("tabIndex"),e.removeAttribute("aria-checked")):(e.setAttribute("role","checkbox"),e.setAttribute("tabIndex","-1"),n&&t.__checked===n.__checked||e.setAttribute("aria-checked",t.getChecked()?"true":"false"))}(r,this,e),r.value=this.__value,function(e,r,i){const s=[],o=[],l=r.list,a=l?l.listitem:void 0;let c;l&&l.nested&&(c=l.nested.listitem);void 0!==a&&s.push(...n.normalizeClassNames(a));if(l){const e=i.getParent(),t=b(e)&&"check"===e.getListType(),n=i.getChecked();t&&!n||o.push(l.listitemUnchecked),t&&n||o.push(l.listitemChecked),t&&s.push(n?l.listitemChecked:l.listitemUnchecked)}if(void 0!==c){const e=n.normalizeClassNames(c);i.getChildren().some(e=>b(e))?s.push(...e):o.push(...e)}o.length>0&&t.removeClassNamesFromElement(e,...o);s.length>0&&t.addClassNamesToElement(e,...s)}(r,i.theme,this);const s=e?e.__style:"",o=this.__style;s!==o&&n.setDOMStyleFromCSS(r.style,o,s),function(e,t,r){const i=t.__textStyle,s=r?r.__textStyle:"";if(null!==r&&s===i)return;const o=n.getStyleObjectFromCSS(i);for(const t in o)e.style.setProperty(`--listitem-marker-${t}`,o[t]);if(""!==s)for(const t in n.getStyleObjectFromCSS(s))t in o||e.style.removeProperty(`--listitem-marker-${t}`)}(r,this,e)}updateDOM(e,t,n){const r=t;return this.updateListItemDOM(e,r,n),!1}updateFromJSON(e){return super.updateFromJSON(e).setValue(e.value).setChecked(e.checked)}exportDOM(e){const t=this.createDOM(e._config),r=this.getFormatType();r&&(t.style.textAlign=r);const i=this.getDirection();return i&&(t.dir=i),l(this)?{after(e){if(n.isHTMLElement(e)){const t=e.previousElementSibling;if(n.isHTMLElement(t)&&"LI"===t.nodeName){for(;e.firstChild;)t.append(e.firstChild);e.remove()}}return e},element:t}:{element:t}}exportJSON(){return{...super.exportJSON(),checked:this.getChecked(),value:this.getValue()}}append(...e){for(let t=0;t<e.length;t++){const r=e[t];if(n.$isElementNode(r)&&this.canMergeWith(r)){const e=r.getChildren();this.append(...e),r.remove()}else super.append(r)}return this}replace(e,t){if(L(e))return super.replace(e);this.setIndent(0);const i=this.getParentOrThrow();if(!b(i))return e;if(i.__first===this.getKey())i.insertBefore(e);else if(i.__last===this.getKey())i.insertAfter(e);else{const t=n.$copyNode(i);let r=this.getNextSibling();for(;r;){const e=r;r=r.getNextSibling(),t.append(e)}i.insertAfter(e),e.insertAfter(t)}return t&&(n.$isElementNode(e)||r(139),this.getChildren().forEach(t=>{e.append(t)})),this.remove(),0===i.getChildrenSize()&&i.remove(),e}insertAfter(e,t=!0){const i=this.getParentOrThrow();if(b(i)||r(39),L(e))return super.insertAfter(e,t);const s=this.getNextSiblings();if(i.insertAfter(e,t),0!==s.length){const r=n.$copyNode(i);s.forEach(e=>r.append(e)),e.insertAfter(r,t)}return e}remove(e){const t=this.getPreviousSibling(),n=this.getNextSibling();super.remove(e),t&&n&&l(t)&&l(n)&&(h(t.getFirstChild(),n.getFirstChild()),n.remove())}resetOnCopyNodeFrom(e){super.resetOnCopyNodeFrom(e),e.getChecked()&&this.setChecked(!1)}insertNewAfter(e,t=!0){const r=n.$copyNode(this);return this.insertAfter(r,t),r}collapseAtStart(e){const t=n.$createParagraphNode();this.getChildren().forEach(e=>t.append(e));const r=this.getParentOrThrow(),i=r.getParentOrThrow(),s=L(i);if(1===r.getChildrenSize())if(s)r.remove(),i.select();else{r.insertBefore(t),r.remove();const n=e.anchor,i=e.focus,s=t.getKey();"element"===n.type&&n.getNode().is(this)&&n.set(s,n.offset,"element"),"element"===i.type&&i.getNode().is(this)&&i.set(s,i.offset,"element")}else r.insertBefore(t),this.remove();return!0}getValue(){return this.getLatest().__value}setValue(e){const t=this.getWritable();return t.__value=e,t}getChecked(){const e=this.getLatest();let t;const n=this.getParent();return b(n)&&(t=n.getListType()),"check"===t?Boolean(e.__checked):void 0}setChecked(e){const t=this.getWritable();return t.__checked=e,t}toggleChecked(){const e=this.getWritable();return e.setChecked(!e.__checked)}getIndent(){const e=this.getParent();if(null===e||!this.isAttached())return this.getLatest().__indent;let t=e.getParentOrThrow(),n=0;for(;L(t);)t=t.getParentOrThrow().getParentOrThrow(),n++;return n}setIndent(e){"number"!=typeof e&&r(117),(e=Math.floor(e))>=0||r(199);let t=this.getIndent();for(;t!==e;)t<e?(m(this),t++):(_(this),t--);return this}canInsertAfter(e){return L(e)}canReplaceWith(e){return L(e)}canMergeWith(e){return L(e)||n.$isParagraphNode(e)}extractWithChild(e,t){if(!n.$isRangeSelection(t))return!1;const r=t.anchor.getNode(),i=t.focus.getNode();return this.isParentOf(r)&&this.isParentOf(i)&&this.getTextContent().length===t.getTextContent().length}isParentRequired(){return!0}createParentElementNode(){return P("bullet")}canMergeWhenEmpty(){return!0}}function T(e){if(e.classList.contains("task-list-item"))for(const t of e.children)if("INPUT"===t.tagName)return S(t);if(e.classList.contains("joplin-checkbox"))for(const t of e.children)if(t.classList.contains("checkbox-wrapper")&&t.children.length>0&&"INPUT"===t.children[0].tagName)return S(t.children[0]);const t=e.getAttribute("aria-checked");return{node:y("true"===t||"false"!==t&&void 0)}}function S(e){if(!("checkbox"===e.getAttribute("type")))return{node:null};return{node:y(e.hasAttribute("checked"))}}function y(e){return n.$applyNodeReplacement(new N(void 0,e))}function L(e){return e instanceof N}class O extends n.ElementNode{__tag;__start;__listType;$config(){return this.config("list",{$transform:e=>{!function(e){const t=e.getNextSibling();b(t)&&e.getListType()===t.getListType()&&h(e,t)}(e),p(e)},extends:n.ElementNode,importDOM:n.buildImportMap({ol:()=>({conversion:E,priority:0}),ul:()=>({conversion:E,priority:0})})})}constructor(e="number",t=1,n){super(n);const r=M[e]||e;this.__listType=r,this.__tag="number"===r?"ol":"ul",this.__start=t}afterCloneFrom(e){super.afterCloneFrom(e),this.__listType=e.__listType,this.__tag=e.__tag,this.__start=e.__start}getTag(){return this.getLatest().__tag}setListType(e){const t=this.getWritable();return t.__listType=e,t.__tag="number"===e?"ol":"ul",t}getListType(){return this.getLatest().__listType}getStart(){return this.getLatest().__start}setStart(e){const t=this.getWritable();return t.__start=e,t}createDOM(e,t){const n=this.__tag,r=document.createElement(n);return 1!==this.__start&&r.setAttribute("start",String(this.__start)),r.__lexicalListType=this.__listType,v(r,e.theme,this),r}updateDOM(e,t,n){return e.__tag!==this.__tag||e.__listType!==this.__listType||(v(t,n.theme,this),e.__start!==this.__start&&t.setAttribute("start",String(this.__start)),!1)}updateFromJSON(e){return super.updateFromJSON(e).setListType(e.listType).setStart(e.start)}exportDOM(e){const n=this.createDOM(e._config,e);return t.isHTMLElement(n)&&(1!==this.__start&&n.setAttribute("start",String(this.__start)),"check"===this.__listType&&n.setAttribute("__lexicalListType","check")),{element:n}}exportJSON(){return{...super.exportJSON(),listType:this.getListType(),start:this.getStart(),tag:this.getTag()}}canBeEmpty(){return!1}canIndent(){return!1}splice(e,t,r){const i=r.find(L)??this.getChildren().find(L),s=i?()=>n.$copyNode(i):y;let o=r;for(let e=0;e<r.length;e++){const t=r[e];L(t)||(o===r&&(o=[...r]),o[e]=s().append(!n.$isElementNode(t)||b(t)||t.isInline()?t:n.$createTextNode(t.getTextContent())))}return super.splice(e,t,o)}extractWithChild(e){return L(e)}}function v(e,r,s){const o=[],l=[],a=r.list;if(void 0!==a){const e=a[`${s.__tag}Depth`]||[],t=i(s)-1,r=t%e.length,c=e[r],d=a[s.__tag];let g;const u=a.nested,h=a.checklist;if(void 0!==u&&u.list&&(g=u.list),void 0!==d&&o.push(d),void 0!==h&&"check"===s.__listType&&o.push(h),void 0!==c){o.push(...n.normalizeClassNames(c));for(let t=0;t<e.length;t++)t!==r&&l.push(s.__tag+t)}if(void 0!==g){const e=n.normalizeClassNames(g);t>1?o.push(...e):l.push(...e)}}l.length>0&&t.removeClassNamesFromElement(e,...l),o.length>0&&t.addClassNamesToElement(e,...o)}function x(e){const t=[];for(let n=0;n<e.length;n++){const r=e[n];if(L(r)){t.push(r);const e=r.getChildren();e.length>1&&e.forEach(e=>{b(e)&&t.push(a(e))})}else t.push(a(r))}return t}function E(e){const n=e.nodeName.toLowerCase();let r=null;if("ol"===n){r=P("number",e.start)}else"ul"===n&&(r=function(e){if("check"===e.getAttribute("__lexicallisttype")||e.classList.contains("contains-task-list")||"1"===e.getAttribute("data-is-checklist"))return!0;for(const n of e.childNodes)if(t.isHTMLElement(n)&&n.hasAttribute("aria-checked"))return!0;return!1}(e)?P("check"):P("bullet"));return{after:x,node:r}}const M={ol:"number",ul:"bullet"};function P(e="number",t=1){return n.$applyNodeReplacement(new O(e,t))}function b(e){return e instanceof O}const $=n.createCommand("INSERT_CHECK_LIST_COMMAND");function k(e,r){const i=r&&r.disableTakeFocusOnClick||!1,s="boolean"==typeof i?()=>i:i.peek.bind(i),o=e=>{const n=e.target;if(!t.isHTMLElement(n))return!1;const r=n.__lexicalCheckListLastHandled;return void 0!==r&&e.timeStamp-r<500},l=e=>{const n=e.target;t.isHTMLElement(n)&&(n.__lexicalCheckListLastHandled=e.timeStamp)},a=e=>{o(e)||(l(e),R(e,s()))},c=e=>{"touch"===e.pointerType&&(o(e)||(l(e),R(e,s())))},g=e=>{!function(e,t){A(e,()=>{e.preventDefault(),t&&e.stopPropagation()})}(e,s())};return t.mergeRegister(e.registerCommand($,()=>(d("check"),!0),n.COMMAND_PRIORITY_LOW),e.registerCommand(n.KEY_ARROW_DOWN_COMMAND,t=>F(t,e,!1),n.COMMAND_PRIORITY_LOW),e.registerCommand(n.KEY_ARROW_UP_COMMAND,t=>F(t,e,!0),n.COMMAND_PRIORITY_LOW),e.registerCommand(n.KEY_ESCAPE_COMMAND,()=>{if(null!=I()){const t=e.getRootElement();return null!=t&&t.focus(),!0}return!1},n.COMMAND_PRIORITY_LOW),e.registerCommand(n.KEY_SPACE_COMMAND,t=>{const r=I();return!(null==r||!e.isEditable())&&(e.update(()=>{const e=n.$getNearestNodeFromDOMNode(r);L(e)&&(t.preventDefault(),e.toggleChecked())}),!0)},n.COMMAND_PRIORITY_LOW),e.registerCommand(n.KEY_ARROW_LEFT_COMMAND,r=>e.getEditorState().read(()=>{const i=n.$getSelection();if(n.$isRangeSelection(i)&&i.isCollapsed()){const{anchor:s}=i,o="element"===s.type;if(o||0===s.offset){const i=s.getNode(),l=t.$findMatchingParent(i,e=>n.$isElementNode(e)&&!e.isInline());if(L(l)){const t=l.getParent();if(b(t)&&"check"===t.getListType()&&(o||l.getFirstDescendant()===i)){const t=e.getElementByKey(l.__key);if(null!=t&&document.activeElement!==t)return t.focus(),r.preventDefault(),!0}}}}return!1}),n.COMMAND_PRIORITY_LOW),e.registerRootListener(e=>{if(null!==e)return e.addEventListener("click",a),e.addEventListener("pointerup",c),e.addEventListener("pointerdown",g,{capture:!0}),e.addEventListener("mousedown",g,{capture:!0}),e.addEventListener("touchstart",g,{capture:!0,passive:!1}),()=>{e.removeEventListener("click",a),e.removeEventListener("pointerup",c),e.removeEventListener("pointerdown",g,{capture:!0}),e.removeEventListener("mousedown",g,{capture:!0}),e.removeEventListener("touchstart",g,{capture:!0})}}))}function A(e,n){const r=e.target;if(!t.isHTMLElement(r))return;const i=r.firstChild;if(t.isHTMLElement(i)&&("UL"===i.tagName||"OL"===i.tagName))return;const s=r.parentNode;if(!s||"check"!==s.__lexicalListType)return;let o=null,l=null;if("clientX"in e)o=e.clientX;else if("touches"in e){const t=e.touches;t.length>0&&(o=t[0].clientX,l="touch")}if(null==o)return;const a=r.getBoundingClientRect(),c=o/t.calculateZoomLevel(r),d=window.getComputedStyle?window.getComputedStyle(r,"::before"):{width:"0px"},g=parseFloat(d.width),u="touch"===l||"pointerType"in e&&"touch"===e.pointerType?32:0;("rtl"===r.dir?c<a.right+u&&c>a.right-g-u:c>a.left-u&&c<a.left+g+u)&&n()}function R(e,r){A(e,()=>{if(t.isHTMLElement(e.target)){const t=e.target,i=n.getNearestEditorFromDOMNode(t);null!=i&&i.isEditable()&&i.update(()=>{const e=n.$getNearestNodeFromDOMNode(t);L(e)&&(r?(n.$addUpdateTag(n.SKIP_SELECTION_FOCUS_TAG),n.$addUpdateTag(n.SKIP_DOM_SELECTION_TAG)):t.focus(),e.toggleChecked())})}})}function I(){const e=document.activeElement;return t.isHTMLElement(e)&&"LI"===e.tagName&&null!=e.parentNode&&"check"===e.parentNode.__lexicalListType?e:null}function F(e,t,r){const i=I();return null!=i&&t.update(()=>{const s=n.$getNearestNodeFromDOMNode(i);if(!L(s))return;const o=function(e,t){let n=t?e.getPreviousSibling():e.getNextSibling(),r=e;for(;null==n&&L(r);)r=r.getParentOrThrow().getParent(),null!=r&&(n=t?r.getPreviousSibling():r.getNextSibling());for(;L(n);){const e=t?n.getLastChild():n.getFirstChild();if(!b(e))return n;n=t?e.getLastChild():e.getFirstChild()}return null}(s,r);if(null!=o){o.selectStart();const n=t.getElementByKey(o.__key);null!=n&&(e.preventDefault(),setTimeout(()=>{n.focus()},0))}}),!1}const D=n.createCommand("UPDATE_LIST_START_COMMAND"),w=n.createCommand("INSERT_UNORDERED_LIST_COMMAND"),W=n.createCommand("INSERT_ORDERED_LIST_COMMAND"),K=n.createCommand("REMOVE_LIST_COMMAND");function Y(e,r){return t.mergeRegister(e.registerCommand(W,()=>(d("number"),!0),n.COMMAND_PRIORITY_LOW),e.registerCommand(D,e=>{const{listNodeKey:t,newStart:r}=e,i=n.$getNodeByKey(t);return!!b(i)&&("number"===i.getListType()&&(i.setStart(r),p(i)),!0)},n.COMMAND_PRIORITY_LOW),e.registerCommand(w,()=>(d("bullet"),!0),n.COMMAND_PRIORITY_LOW),e.registerCommand(K,()=>(f(),!0),n.COMMAND_PRIORITY_LOW),e.registerCommand(n.INSERT_PARAGRAPH_COMMAND,()=>C(!!(r&&r.restoreNumbering)),n.COMMAND_PRIORITY_LOW),e.registerNodeTransform(N,e=>{const t=e.getFirstChild();if(t){if(n.$isTextNode(t)){const n=t.getStyle(),r=t.getFormat();e.getTextStyle()!==n&&e.setTextStyle(n),e.getTextFormat()!==r&&e.setTextFormat(r)}}else{const t=n.$getSelection();n.$isRangeSelection(t)&&(t.style!==e.getTextStyle()||t.format!==e.getTextFormat())&&t.isCollapsed()&&e.is(t.anchor.getNode())&&e.setTextStyle(t.style).setTextFormat(t.format)}}),e.registerNodeTransform(n.TextNode,e=>{const t=e.getParent();if(L(t)&&e.is(t.getFirstChild())){const n=e.getStyle(),r=e.getFormat();n===t.getTextStyle()&&r===t.getTextFormat()||t.setTextStyle(n).setTextFormat(r)}}))}function B(e){const n=e=>{const n=e.getParent();if(b(e.getFirstChild())||!b(n))return;const r=t.$findMatchingParent(e,e=>L(e)&&b(e.getParent())&&L(e.getPreviousSibling()));if(null===r&&e.getIndent()>0)e.setIndent(0);else if(L(r)){const t=r.getPreviousSibling();if(L(t)){const r=function(e){let t=e,n=t.getFirstChild();for(;b(n);){const e=n.getLastChild();if(!L(e))break;t=e,n=t.getFirstChild()}return t}(t),s=r.getParent();if(b(s)){const t=i(s);t+1<i(n)&&e.setIndent(t)}}}};return e.registerNodeTransform(O,e=>{const t=[e];for(;t.length>0;){const e=t.shift();if(b(e))for(const r of e.getChildren())if(L(r)){n(r);const e=r.getFirstChild();b(e)&&t.push(e)}}})}const H=n.defineExtension({build:(t,n,r)=>e.namedSignals(n),config:n.safeCast({hasStrictIndent:!1,shouldPreserveNumbering:!1}),name:"@lexical/list/List",nodes:()=>[O,N],register(n,r,i){const s=i.getOutput();return t.mergeRegister(e.effect(()=>Y(n,{restoreNumbering:s.shouldPreserveNumbering.value})),e.effect(()=>s.hasStrictIndent.value?B(n):void 0))}}),U=n.defineExtension({build:(t,n)=>e.namedSignals(n),config:n.safeCast({disableTakeFocusOnClick:!1}),dependencies:[H],name:"@lexical/list/CheckList",register:(e,t,n)=>k(e,n.getOutput())});exports.$createListItemNode=y,exports.$createListNode=P,exports.$getListDepth=i,exports.$handleListInsertParagraph=C,exports.$insertList=d,exports.$isListItemNode=L,exports.$isListNode=b,exports.$removeList=f,exports.CheckListExtension=U,exports.INSERT_CHECK_LIST_COMMAND=$,exports.INSERT_ORDERED_LIST_COMMAND=W,exports.INSERT_UNORDERED_LIST_COMMAND=w,exports.ListExtension=H,exports.ListItemNode=N,exports.ListNode=O,exports.REMOVE_LIST_COMMAND=K,exports.UPDATE_LIST_START_COMMAND=D,exports.insertList=function(e,t){e.update(()=>d(t))},exports.registerCheckList=k,exports.registerList=Y,exports.registerListStrictIndentTransform=B,exports.removeList=function(e){e.update(()=>f())};