@lexical/code-core 0.51.1-nightly.20260922.0 → 0.51.1-nightly.20260924.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import { type LexicalEditor } from 'lexical';
9
+ /**
10
+ * @internal
11
+ * Keep the `data-gutter` attribute of every {@link CodeNode} in sync with its
12
+ * line count, so a theme can render line numbers with
13
+ * `content: attr(data-gutter)`.
14
+ *
15
+ * Both `@lexical/code-prism` and `@lexical/code-shiki` register this. In
16
+ * headless mode there is no DOM to write to, so registration is skipped and
17
+ * the returned teardown does nothing.
18
+ *
19
+ * @param editor The editor whose code blocks should carry a gutter.
20
+ * @returns A teardown that removes the listener.
21
+ */
22
+ export declare function registerCodeGutter(editor: LexicalEditor): () => void;
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import { sel, ImportOverlays, defineOverlayRules, CoreImportExtension, DOMImportExtension } from '@lexical/html';
10
- import { getTextDirection, $isElementNode, $isTabNode, $isLineBreakNode, tokenizeRawText, $createTabNode, $createLineBreakNode, $getSiblingCaret, getterTableOf, setterTableOf, setterDefaultOf, aliasTableOf, $create, ElementNode, $getDocument, addClassNamesToElement, setDOMStyleFromCSS, $getEditor, $isTextNode, $createParagraphNode, nodeSchema, withAccessors, optional, stringValue, nullable, isHTMLElement, $applyNodeReplacement, TextNode, removeClassNamesFromElement, isDOMDocumentNode, isDOMTextNode, $generateNodesFromRawText, mergeRegister, KEY_ENTER_COMMAND, $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, TabNode, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_RIGHT_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ARROW_LEFT_COMMAND, KEY_TAB_COMMAND, INSERT_TAB_COMMAND, $insertNodes, INDENT_CONTENT_COMMAND, OUTDENT_CONTENT_COMMAND, MOVE_TO_START, MOVE_TO_END, $createPoint, $setSelectionFromCaretRange, $getCaretRangeInDirection, $getCaretRange, $getTextPointCaret, $normalizeCaret } from 'lexical';
10
+ import { getTextDirection, $isElementNode, $isTabNode, $isLineBreakNode, tokenizeRawText, $createTabNode, $createLineBreakNode, $getSiblingCaret, getterTableOf, setterTableOf, setterDefaultOf, aliasTableOf, $create, ElementNode, $getDocument, addClassNamesToElement, setDOMStyleFromCSS, $getEditor, $isTextNode, $createParagraphNode, nodeSchema, withAccessors, optional, stringValue, nullable, isHTMLElement, $applyNodeReplacement, TextNode, removeClassNamesFromElement, isDOMDocumentNode, isDOMTextNode, $generateNodesFromRawText, mergeRegister, KEY_ENTER_COMMAND, $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, TabNode, $getNodeByKey, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_RIGHT_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ARROW_LEFT_COMMAND, KEY_TAB_COMMAND, INSERT_TAB_COMMAND, $insertNodes, INDENT_CONTENT_COMMAND, OUTDENT_CONTENT_COMMAND, MOVE_TO_START, MOVE_TO_END, $createPoint, $setSelectionFromCaretRange, $getCaretRangeInDirection, $getCaretRange, $getTextPointCaret, $normalizeCaret } from 'lexical';
11
11
  import { getPeerDependencyFromEditor } from '@lexical/extension/getPeerDependencyFromEditor';
12
12
  import { effect } from '@lexical/extension/signals';
13
13
  import { namedSignals } from '@lexical/extension/namedSignals';
@@ -1355,6 +1355,81 @@ const CodeImportExtension = {
1355
1355
  name: '@lexical/code/Import'
1356
1356
  };
1357
1357
 
1358
+ /**
1359
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
1360
+ *
1361
+ * This source code is licensed under the MIT license found in the
1362
+ * LICENSE file in the root directory of this source tree.
1363
+ *
1364
+ */
1365
+
1366
+
1367
+ /**
1368
+ * Write the line numbers of a {@link CodeNode} to the `data-gutter`
1369
+ * attribute of its DOM element as a newline separated list (`"1\n2\n3"`),
1370
+ * so a theme can render them with `content: attr(data-gutter)`. There is
1371
+ * one line per `LineBreakNode` child plus one. The attribute is only
1372
+ * rewritten when the number of children has changed since the last call.
1373
+ *
1374
+ * @param node The CodeNode whose gutter should be updated.
1375
+ */
1376
+ function $updateCodeGutter(node) {
1377
+ const codeElement = $getEditor().getElementByKey(node.getKey());
1378
+ if (codeElement === null) {
1379
+ return;
1380
+ }
1381
+ const children = node.getChildren();
1382
+ const childrenLength = children.length;
1383
+ // @ts-ignore: internal field
1384
+ if (childrenLength === codeElement.__cachedChildrenLength) {
1385
+ // Avoid updating the attribute if the children length hasn't changed.
1386
+ return;
1387
+ }
1388
+ // @ts-ignore:: internal field
1389
+ codeElement.__cachedChildrenLength = childrenLength;
1390
+ let gutter = '1';
1391
+ let count = 1;
1392
+ for (let i = 0; i < childrenLength; i++) {
1393
+ if ($isLineBreakNode(children[i])) {
1394
+ gutter += '\n' + ++count;
1395
+ }
1396
+ }
1397
+ codeElement.setAttribute('data-gutter', gutter);
1398
+ }
1399
+
1400
+ /**
1401
+ * @internal
1402
+ * Keep the `data-gutter` attribute of every {@link CodeNode} in sync with its
1403
+ * line count, so a theme can render line numbers with
1404
+ * `content: attr(data-gutter)`.
1405
+ *
1406
+ * Both `@lexical/code-prism` and `@lexical/code-shiki` register this. In
1407
+ * headless mode there is no DOM to write to, so registration is skipped and
1408
+ * the returned teardown does nothing.
1409
+ *
1410
+ * @param editor The editor whose code blocks should carry a gutter.
1411
+ * @returns A teardown that removes the listener.
1412
+ */
1413
+ function registerCodeGutter(editor) {
1414
+ if (editor._headless) {
1415
+ return () => {};
1416
+ }
1417
+ return editor.registerMutationListener(CodeNode, mutations => {
1418
+ editor.read('latest', () => {
1419
+ for (const [key, type] of mutations) {
1420
+ if (type !== 'destroyed') {
1421
+ const node = $getNodeByKey(key);
1422
+ if (node !== null) {
1423
+ $updateCodeGutter(node);
1424
+ }
1425
+ }
1426
+ }
1427
+ });
1428
+ }, {
1429
+ skipInitialization: false
1430
+ });
1431
+ }
1432
+
1358
1433
  function $isSelectionInCode(selection) {
1359
1434
  if (!$isRangeSelection(selection)) {
1360
1435
  return false;
@@ -1829,4 +1904,4 @@ const CodeIndentExtension = {
1829
1904
  }
1830
1905
  };
1831
1906
 
1832
- export { $createCodeHighlightNode, $createCodeNode, $getCodeLineDirection, $getEndOfCodeInLine, $getFirstCodeNodeOfLine, $getLastCodeNodeOfLine, $getStartOfCodeInLine, $isCodeHighlightNode, $isCodeNode, $outdentLeadingSpaces, $plainifyCodeContent, CodeExtension, CodeHighlightNode, CodeImportExtension, CodeImportRules, CodeIndentExtension, CodeNode, DEFAULT_CODE_LANGUAGE, getDefaultCodeLanguage, registerCodeIndentation };
1907
+ export { $createCodeHighlightNode, $createCodeNode, $getCodeLineDirection, $getEndOfCodeInLine, $getFirstCodeNodeOfLine, $getLastCodeNodeOfLine, $getStartOfCodeInLine, $isCodeHighlightNode, $isCodeNode, $outdentLeadingSpaces, $plainifyCodeContent, CodeExtension, CodeHighlightNode, CodeImportExtension, CodeImportRules, CodeIndentExtension, CodeNode, DEFAULT_CODE_LANGUAGE, getDefaultCodeLanguage, registerCodeGutter, registerCodeIndentation };
@@ -28,4 +28,5 @@ export const CodeIndentExtension = mod.CodeIndentExtension;
28
28
  export const CodeNode = mod.CodeNode;
29
29
  export const DEFAULT_CODE_LANGUAGE = mod.DEFAULT_CODE_LANGUAGE;
30
30
  export const getDefaultCodeLanguage = mod.getDefaultCodeLanguage;
31
+ export const registerCodeGutter = mod.registerCodeGutter;
31
32
  export const registerCodeIndentation = mod.registerCodeIndentation;
@@ -108,6 +108,12 @@ declare export class CodeNode extends ElementNode {
108
108
  getLanguage(): string | void;
109
109
  }
110
110
 
111
+ /**
112
+ * CodeGutter
113
+ */
114
+
115
+ declare export function registerCodeGutter(editor: LexicalEditor): () => void;
116
+
111
117
  declare export var CodeExtension: LexicalExtension<ExtensionConfigBase, "@lexical/code", unknown, unknown>;
112
118
 
113
119
  export type CodeIndentConfig = {
@@ -6,5 +6,5 @@
6
6
  *
7
7
  */
8
8
 
9
- import{sel as t,ImportOverlays as e,defineOverlayRules as n,CoreImportExtension as r,DOMImportExtension as i}from"@lexical/html";import{getTextDirection as o,$isElementNode as s,$isTabNode as l,$isLineBreakNode as c,tokenizeRawText as u,$createTabNode as a,$createLineBreakNode as g,$getSiblingCaret as f,getterTableOf as h,setterTableOf as d,setterDefaultOf as p,aliasTableOf as m,$create as _,ElementNode as x,$getDocument as y,addClassNamesToElement as S,setDOMStyleFromCSS as b,$getEditor as v,$isTextNode as C,$createParagraphNode as T,nodeSchema as N,withAccessors as A,optional as P,stringValue as F,nullable as w,isHTMLElement as O,$applyNodeReplacement as $,TextNode as D,removeClassNamesFromElement as E,isDOMDocumentNode as H,isDOMTextNode as B,$generateNodesFromRawText as L,mergeRegister as k,KEY_ENTER_COMMAND as z,$getSelection as M,$isRangeSelection as I,COMMAND_PRIORITY_LOW as K,TabNode as j,KEY_ARROW_DOWN_COMMAND as R,KEY_ARROW_RIGHT_COMMAND as W,KEY_ARROW_UP_COMMAND as J,KEY_ARROW_LEFT_COMMAND as q,KEY_TAB_COMMAND as U,INSERT_TAB_COMMAND as V,$insertNodes as G,INDENT_CONTENT_COMMAND as Q,OUTDENT_CONTENT_COMMAND as X,MOVE_TO_START as Y,MOVE_TO_END as Z,$createPoint as tt,$setSelectionFromCaretRange as et,$getCaretRangeInDirection as nt,$getCaretRange as rt,$getTextPointCaret as it,$normalizeCaret as ot}from"lexical";import{getPeerDependencyFromEditor as st}from"@lexical/extension/getPeerDependencyFromEditor";import{effect as lt}from"@lexical/extension/signals";import{namedSignals as ct}from"@lexical/extension/namedSignals";import{$onEscapeDown as ut,$onEscapeUp as at}from"@lexical/utils";
10
- /*@__INLINE__*/function gt(t){return()=>{}}function ft(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 ht(t,e){let n=t;for(let r=f(t,e);r&&(Vt(r.origin)||l(r.origin));r=r.getAdjacentCaret())n=r.origin;return n}function dt(t){return ht(t,"previous")}function pt(t){return ht(t,"next")}function mt(t){const e=dt(t),n=pt(t);let r=e;for(;null!==r;){if(Vt(r)){const t=o(r.getTextContent());if(null!==t)return t}if(r===n)break;r=r.getNextSibling()}const i=e.getParent();if(s(i)){const t=i.getDirection();if("ltr"===t||"rtl"===t)return t}return null}function _t(t,e){let n=null,r=null,i=t,o=e,s=t.getTextContent();for(;;){if(0===o){const t=i.getPreviousSibling();if(null===t){i=null;break}if(Vt(t)||l(t)||c(t)||ft(167),i=t,c(i)){n={node:i,offset:1};break}o=Math.max(0,i.getTextContentSize()-1),s=i.getTextContent()}else o--;const t=s[o];Vt(i)&&" "!==t&&(r={node:i,offset:o})}if(null!==r)return r;let u=null;if(e<t.getTextContentSize())Vt(t)&&(u=t.getTextContent()[e]);else{const e=t.getNextSibling();Vt(e)&&(u=e.getTextContent()[0])}if(null!==u&&" "!==u)return n;{const r=function(t,e){let n=t,r=e,i=t.getTextContent(),o=t.getTextContentSize();for(;;){if(!Vt(n)||r===o){if(n=n.getNextSibling(),null===n||c(n))return null;Vt(n)&&(r=0,i=n.getTextContent(),o=n.getTextContentSize())}if(Vt(n)){if(" "!==i[r])return{node:n,offset:r};r++}}}(t,e);return null!==r?r:n}}function xt(t){const e=pt(t);return c(e)&&ft(168),e}function yt(t){const e=[];return u(t,{linebreak:()=>e.push(g()),tab:()=>e.push(a()),text:t=>e.push(Ut(t))}),e}function St(t,e,n){if(!Number.isInteger(e)||e<=0)return!1;const r=t.getTextContent(),i=/^ +/.exec(r);if(!i)return!1;const o=Math.min(e,i[0].length),s=t.getKey(),l=n.anchor.key===s&&"text"===n.anchor.type?n.anchor.offset:null,c=n.focus.key===s&&"text"===n.focus.type?n.focus.offset:null;return t.spliceText(0,o,""),null!==l&&n.anchor.set(s,Math.max(0,l-o),"text"),null!==c&&n.focus.set(s,Math.max(0,c-o),"text"),!0}const bt=/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;function vt(t,e){if("number"==typeof t)return Number.isFinite(t)?t:e;if("string"!=typeof t||!bt.test(t))return e;const n=Number(t);return Number.isFinite(n)?n:e}const Ct=t=>{const e=h(t,"format"),n=d(t,"format"),r=p(t,"format");return{exportJSON:function(t){const n=t.__textFormat,r=t.__textStyle,i=(0!==n||""!==r)&&t.shouldSerializeTextStyles();return{children:[],language:t.__language,theme:t.__theme,direction:t.__dir,format:e[t.__format],indent:t.__indent,textFormat:0!==n&&i?n:void 0,textStyle:""!==r&&i?r:void 0,type:t.__type,version:1}},exportCompactJSON:function(t){const n=t.__textFormat,r=t.__textStyle,i=(0!==n||""!==r)&&t.shouldSerializeTextStyles(),o={type:t.__type,children:[]},s=t.__language;void 0!==s&&(o.language=s);const l=t.__theme;void 0!==l&&(o.theme=l);const c=t.__dir;null!=c&&(o.direction=c);const u=e[t.__format];void 0!==u&&""!==u&&(o.format=u);const a=t.__indent;return void 0!==a&&0!==a&&(o.indent=a),void 0!==n&&0!==n&&i&&(o.textFormat=n),void 0!==r&&""!==r&&i&&(o.textStyle=r),o},updateFromJSON:function(t,e){const i=e.direction;t.__dir=null===i||"ltr"===i||"rtl"===i?i:null;const o=e.format;t.__format="string"==typeof o&&o in n?n[o]:r,t.__indent=function(t){const e=vt(t,0);return e>=0&&e<=Infinity&&Number.isInteger(e)?e:0}(e.indent),t.__textFormat=vt(e.textFormat,0);const s=e.textStyle;t.__textStyle="string"==typeof s?s:"";const l=e.language;t.setLanguage(null==l||"string"==typeof l?l:"");const c=e.theme;return t.setTheme(void 0===c?void 0:"string"==typeof c?c:""),t}}};function Tt(t,e){t.__highlightType=e.__highlightType}const Nt=t=>{const e=h(t,"mode"),n=m(t,"detail",0),r=m(t,"format",0),i=d(t,"mode"),o=p(t,"mode");return{exportJSON:function(t){return{highlightType:t.__highlightType,detail:t.__detail,format:t.__format,mode:e[t.__mode],style:t.__style,text:t.__text,type:t.__type,version:1}},exportCompactJSON:function(t){const n={type:t.__type},r=t.__highlightType;void 0!==r&&(n.highlightType=r);const i=t.__detail;void 0!==i&&0!==i&&(n.detail=i);const o=t.__format;void 0!==o&&0!==o&&(n.format=o);const s=e[t.__mode];void 0!==s&&"normal"!==s&&(n.mode=s);const l=t.__style;void 0!==l&&""!==l&&(n.style=l);const c=t.__text;return void 0!==c&&""!==c&&(n.text=c),n},updateFromJSON:function(t,e){const s=e.detail;t.__detail="string"==typeof s&&s in n?n[s]:vt(s,0);const l=e.format;t.setFormat("string"==typeof l&&l in r?r[l]:vt(l,0));const c=e.mode;t.__mode="string"==typeof c&&c in i?i[c]:o;const u=e.style;t.__style="string"==typeof u?u:"";const a=e.text;t.__text="string"==typeof a?a:"";const g=e.highlightType;return t.setHighlightType(null==g||"string"==typeof g?g:""),t},afterCloneFrom:Tt}},At=/* @__PURE__ */N()({language:/* @__PURE__ */A(/* @__PURE__ */P(/* @__PURE__ */w(/* @__PURE__ */F())),{getter:{field:"__language"}}),theme:/* @__PURE__ */A(/* @__PURE__ */P(/* @__PURE__ */F()),{getter:{field:"__theme"}})}),Pt="javascript",Ft=()=>Pt;function wt(t,e){for(const n of t.childNodes){if(O(n)&&n.tagName===e)return!0;if(wt(n,e))return!0}return!1}const Ot="data-language",$t="data-highlight-language",Dt="data-theme",Et=/* @__PURE__ */gt();class Ht extends x{__language;__theme;__isSyntaxHighlightSupported;$config(){return this.config("code",{extends:x,generated:Ct,importDOM:{code:t=>null!=t.textContent&&(/\r?\n/.test(t.textContent)||wt(t,"BR"))?{conversion:kt,priority:1}:null,div:()=>({conversion:zt,priority:1}),pre:()=>({conversion:kt,priority:0}),table:t=>jt(t)?{conversion:Mt,priority:3}:null,td:t=>{const e=t,n=e.closest("table");return e.classList.contains("js-file-line")||n&&jt(n)?{conversion:It,priority:3}:null},tr:t=>{const e=t.closest("table");return e&&jt(e)?{conversion:It,priority:3}:null}},json:At})}constructor(t=void 0,e){super(e),this.__language=t||void 0,this.__isSyntaxHighlightSupported=!1,this.__theme=void 0}afterCloneFrom(t){super.afterCloneFrom(t),function(t,e){t.__language=e.__language,t.__theme=e.__theme}(this,t),this.__isSyntaxHighlightSupported=t.__isSyntaxHighlightSupported}createDOM(t){const e=y().createElement("code");S(e,t.theme.code),e.setAttribute("spellcheck","false");const n=this.getLanguage();n&&(e.setAttribute(Ot,n),this.getIsSyntaxHighlightSupported()&&e.setAttribute($t,n));const r=this.getTheme();r&&e.setAttribute(Dt,r);const i=this.getStyle();return i&&b(e.style,i),e}updateDOM(t,e,n){const r=this.__language,i=t.__language;r?r!==i&&e.setAttribute(Ot,r):i&&e.removeAttribute(Ot);const o=this.__isSyntaxHighlightSupported;t.__isSyntaxHighlightSupported&&i?o&&r?r!==i&&e.setAttribute($t,r):e.removeAttribute($t):o&&r&&e.setAttribute($t,r);const s=this.__theme,l=t.__theme;s?s!==l&&e.setAttribute(Dt,s):l&&e.removeAttribute(Dt);const c=this.__style,u=t.__style;return c!==u&&b(e.style,c,u),!1}exportDOM(t){const e=y().createElement("pre");S(e,t._config.theme.code),e.setAttribute("spellcheck","false");const n=this.getLanguage();n&&(e.setAttribute(Ot,n),this.getIsSyntaxHighlightSupported()&&e.setAttribute($t,n));const r=this.getTheme();r&&e.setAttribute(Dt,r);const i=this.getStyle();return i&&b(e.style,i),{element:e}}insertNewAfter(t,e=!0){if(!st(v(),"@lexical/code")){Et();const e=Rt(t);if(e)return e}const{anchor:n,focus:r}=t,i=(n.isBefore(r)?n:r).getNode();if(C(i)){let t=dt(i);const e=[];for(;;)if(l(t))e.push(a()),t=t.getNextSibling();else{if(!Vt(t))break;{let n=0;const r=t.getTextContent(),i=t.getTextContentSize();for(;n<i&&" "===r[n];)n++;if(0!==n&&e.push(Ut(" ".repeat(n))),n!==i)break;t=t.getNextSibling()}}const r=i.splitText(n.offset)[0],o=0===n.offset?0:1,s=r.getIndexWithinParent()+o,c=i.getParentOrThrow(),u=[g(),...e];c.splice(s,0,u);const f=e[e.length-1];f?f.select():0===n.offset?r.selectPrevious():r.getNextSibling().selectNext(0,0)}if(Lt(i)){const{offset:e}=t.anchor;i.splice(e,0,[g()]),i.select(e+1,e+1)}return null}canIndent(){return!1}collapseAtStart(){const t=T();return this.getChildren().forEach(e=>t.append(e)),this.replace(t),!0}setLanguage(t){const e=this.getWritable();return e.__language=t||void 0,e}getLanguage(){return this.getLatest().__language}setIsSyntaxHighlightSupported(t){const e=this.getWritable();return e.__isSyntaxHighlightSupported=t,e}getIsSyntaxHighlightSupported(){return this.getLatest().__isSyntaxHighlightSupported}setTheme(t){const e=this.getWritable();return e.__theme=t||void 0,e}getTheme(){return this.getLatest().__theme}}function Bt(t,e){return _(Ht).setLanguage(t).setTheme(e)}function Lt(t){return t instanceof Ht}function kt(t){return{node:Bt(t.getAttribute(Ot),t.getAttribute(Dt))}}function zt(t){const e=t,n=Kt(e);return n||function(t){let e=t.parentElement;for(;null!==e;){if(Kt(e))return!0;e=e.parentElement}return!1}(e)?{node:n?Bt():null}:{node:null}}function Mt(){return{node:Bt()}}function It(){return{node:null}}function Kt(t){return null!==t.style.fontFamily.match("monospace")}function jt(t){return t.classList.contains("js-file-line-container")}function Rt(t){const{anchor:e}=t;if(t.isCollapsed()&&"element"===e.type){const t=e.getNode();if(Lt(t)){const n=t.getChildrenSize();if(n>=2&&e.offset===n){const e=t.getLastChild();if(c(e)&&c(e.getPreviousSibling())){const e=T();return t.splice(n-2,2,[]).insertAfter(e,!1),e.select(),e}}}}return null}const Wt=/* @__PURE__ */N()({highlightType:/* @__PURE__ */A(/* @__PURE__ */P(/* @__PURE__ */w(/* @__PURE__ */F())),{getter:{field:"__highlightType"}})});class Jt extends D{__highlightType;constructor(t="",e,n){super(t,n),this.__highlightType=e}$config(){return this.config("code-highlight",{extends:D,generated:Nt,json:Wt})}getHighlightType(){return this.getLatest().__highlightType}setHighlightType(t){const e=this.getWritable();return e.__highlightType=t||void 0,e}canHaveFormat(){return!1}createDOM(t){const e=super.createDOM(t),n=qt(t.theme,this.__highlightType);return S(e,n),e}updateDOM(t,e,n){const r=super.updateDOM(t,e,n),i=qt(n.theme,t.__highlightType),o=qt(n.theme,this.__highlightType);return i!==o&&(i&&E(e,i),o&&S(e,o)),r}setFormat(t){return this}isParentRequired(){return!0}createParentElementNode(){return Bt()}}function qt(t,e){return e&&t&&t.codeHighlight&&t.codeHighlight[e]}function Ut(t="",e){return $(new Jt(t,e))}function Vt(t){return t instanceof Jt}const Gt="data-language",Qt="data-theme";function Xt(t){return null!==t.style.fontFamily.match("monospace")}function Yt(t){let e=t.parentElement;for(;null!==e;){if(Xt(e))return!0;e=e.parentElement}return!1}const Zt=/* @__PURE__ */n([{$import:(t,e)=>t.$importChildren(e),match:/* @__PURE__ */t.tag("tr","td"),name:"@lexical/code/github-code-table/unwrap"}]),te={$import:(t,e)=>[Bt(e.getAttribute(Gt),e.getAttribute(Qt)).splice(0,0,t.$importChildren(e))],match:/* @__PURE__ */t.tag("pre"),name:"@lexical/code/pre"},ee={$import:(t,e,n)=>{const r=e.textContent||"";return/\r?\n/.test(r)||null!==e.querySelector("br")?[Bt(e.getAttribute(Gt),e.getAttribute(Qt)).splice(0,0,t.$importChildren(e))]:n()},match:/* @__PURE__ */t.tag("code"),name:"@lexical/code/code-multiline"};function ne(t){if(!O(t))return!1;const e=t.style.fontFamily,n=t.style.whiteSpace;return"string"==typeof e&&/monospace/i.test(e)&&"string"==typeof n&&n.startsWith("pre")}function re(t){let e=!1;const n=[];let r="",i=!1;const o=()=>{i&&(n.push(r),r="",i=!1)};for(const s of Array.from(t.childNodes))if(O(s))"DIV"===s.tagName?(o(),n.push(s.textContent||""),e=!0):"BR"===s.tagName?(o(),n.push(""),e=!0):(r+=s.textContent||"",i=!0);else if(B(s)){const t=s.textContent||"";t.length>0&&(r+=t,i=!0)}return o(),e?n:null}function ie(t){for(const e of Array.from(t.children)){if(O(e)&&ne(e)){if(null!==re(e))return!0;const t=e.nextElementSibling;if(t&&ne(t))return!0;continue}if(ie(e))return!0}return!1}const oe=/* @__PURE__ */n([{$import:(t,e,n)=>{if(!ne(e)||Yt(e))return n();const r=re(e);return null===r||0===r.length?n():[Bt().splice(0,0,L(r.join("\n")))]},match:/* @__PURE__ */t.tag("div"),name:"@lexical/code/vscode-wrapper"},{$import:(t,e,n)=>{if(!ne(e)||Yt(e))return n();const r=e.previousElementSibling;if(r&&ne(r))return[];const i=[];let o=e;for(;o&&ne(o);)i.push("BR"===o.tagName?"":o.textContent||""),o=o.nextElementSibling;return i.length<2?n():[Bt().splice(0,0,L(i.join("\n")))]},match:/* @__PURE__ */t.tag("div","br"),name:"@lexical/code/vscode-line-run"}]),se={$import:(t,e,n)=>Xt(e)?[Bt().splice(0,0,t.$importChildren(e))]:Yt(e)?t.$importChildren(e):n(),match:/* @__PURE__ */t.tag("div"),name:"@lexical/code/div"},le=[{$import:(t,e)=>[Bt().splice(0,0,t.$importChildren(e,{rules:Zt}))],match:/* @__PURE__ */t.tag("table").classAll("js-file-line-container"),name:"@lexical/code/github-code-table"},{$import:(t,e)=>t.$importChildren(e),match:/* @__PURE__ */t.tag("td").classAll("js-file-line"),name:"@lexical/code/github-code-cell-by-class"},ee,te,se],ce={dependencies:[r,[i,{preprocess:[(t,n,r)=>{ie(H(t)?t.body:t)&&n.session.update(e,t=>[...t,oe]),r()}],rules:le}]],name:"@lexical/code",nodes:()=>[Ht,Jt],register:t=>k(t.registerCommand(z,t=>{const e=M();return!(!I(e)||!Rt(e)||(null!==t&&t.preventDefault(),0))},K),t.registerNodeTransform(j,t=>{0!==t.getFormat()&&Lt(t.getParent())&&t.setFormat(0)}))},ue={dependencies:[ce],name:"@lexical/code/Import"};function ae(t){if(!I(t))return!1;const e=t.anchor.getNode(),n=Lt(e)?e:e.getParent(),r=t.focus.getNode(),i=Lt(r)?r:r.getParent();return Lt(n)&&n.is(i)}function ge(t){const e=t.getNodes(),n=[];if(1===e.length&&Lt(e[0]))return n;let r=[];for(let t=0;t<e.length;t++){const i=e[t];Vt(i)||l(i)||c(i)||ft(169),c(i)?r.length>0&&(n.push(r),r=[]):r.push(i)}if(r.length>0){const e=t.isBackward()?t.anchor:t.focus,i=tt(r[0].getKey(),0,"text");e.is(i)||n.push(r)}return n}function fe(t,e){const n=M();if(!I(n)||!ae(n))return!1;const r=ge(n),i=r.length;if(0===i&&n.isCollapsed())return t===Q?n.insertNodes([a()]):function(t,e){const n=t.anchor.getNode();if(!Vt(n)&&!l(n))return;const r=dt(n);l(r)?r.remove():void 0!==e&&Vt(r)&&St(r,e,t)}(n,e),!0;if(0===i&&t===Q&&"\n"===n.getTextContent()){const t=a(),e=g(),r=n.isBackward()?"previous":"next";return n.insertNodes([t,e]),et(nt(rt(it(t,"next",0),ot(f(e,"next"))),r)),!0}for(let o=0;o<i;o++){const i=r[o];if(i.length>0){let r=i[0];if(0===o&&(r=dt(r)),t===Q){const t=a();if(r.insertBefore(t),0===o){const e=n.isBackward()?"focus":"anchor",i=tt(r.getKey(),0,"text");n[e].is(i)&&n[e].set(t.getKey(),0,"text")}}else l(r)?r.remove():void 0!==e&&Vt(r)&&St(r,e,n)}}return!0}function he(t,e){const n=M();if(!I(n))return!1;const{anchor:r,focus:i}=n,o=r.offset,s=i.offset,u=r.getNode(),a=i.getNode(),g=t===J;if(!ae(n)||!Vt(u)&&!l(u)||!Vt(a)&&!l(a))return!1;if(!e.altKey){if(n.isCollapsed()){const t=u.getParentOrThrow();if(g&&0===o&&null===u.getPreviousSibling()){if(null===t.getPreviousSibling())return t.selectPrevious(),e.preventDefault(),!1}else if(!g&&o===u.getTextContentSize()&&null===u.getNextSibling()&&null===t.getNextSibling())return t.selectNext(),e.preventDefault(),!1}return!1}let f,h;if(u.isBefore(a)?(f=dt(u),h=pt(a)):(f=dt(a),h=pt(u)),null==f||null==h)return!1;const d=f.getNodesBetween(h);for(let t=0;t<d.length;t++){const e=d[t];if(!Vt(e)&&!l(e)&&!c(e))return!1}e.preventDefault(),e.stopPropagation();const p=g?f.getPreviousSibling():h.getNextSibling();if(!c(p))return!0;const m=g?p.getPreviousSibling():p.getNextSibling();if(null==m)return!0;const _=c(m),x=_||!Vt(m)&&!l(m)?null:g?dt(m):pt(m);let y=null!=x?x:m;return p.remove(),d.forEach(t=>t.remove()),_?(d.forEach(t=>{y.insertAfter(t),y=t}),y.insertAfter(p)):t===J?(d.forEach(t=>y.insertBefore(t)),y.insertBefore(p)):(y.insertAfter(p),y=p,d.forEach(t=>{y.insertAfter(t),y=t})),n.setTextNodeRange(u,o,a,s),!0}function de(t,e){const n=M();if(!I(n))return!1;const{anchor:r,focus:i}=n,o=r.getNode(),s=i.getNode(),u=t===Y;if(!ae(n)||!Vt(o)&&!l(o)||!Vt(s)&&!l(s))return!1;const a=s,g="rtl"===mt(a)?!u:u,f=r.key,h=r.offset,d=r.type;if(g){const t=_t(a,i.offset);if(null!==t){const{node:e,offset:r}=t;c(e)?e.selectNext(0,0):n.setTextNodeRange(e,r,e,r)}else a.getParentOrThrow().selectStart()}else xt(a).select();return e.shiftKey&&n.anchor.set(f,h,d),e.preventDefault(),e.stopPropagation(),!0}function pe(t,e,n){return k(...n?[t.registerCommand(R,t=>!t.altKey&&ut(Lt,t),K),t.registerCommand(W,t=>ut(Lt,t),K),t.registerCommand(J,t=>!t.altKey&&at(Lt,t),K),t.registerCommand(q,t=>at(Lt,t),K)]:[],t.registerCommand(U,e=>{const n=function(t){const e=M();if(!I(e)||!ae(e))return null;const n=t?X:Q,r=t?X:V,i=e.anchor,o=e.focus;if(i.is(o))return r;const s=ge(e);if(1!==s.length)return n;const l=s[0];let c,u;0===l.length&&ft(285),e.isBackward()?(c=o,u=i):(c=i,u=o);const a=dt(l[0]),g=pt(l[0]),f=tt(a.getKey(),0,"text"),h=tt(g.getKey(),g.getTextContentSize(),"text");return c.isBefore(f)||h.isBefore(u)?n:f.isBefore(c)||u.isBefore(h)?r:n}(e.shiftKey);return null!==n&&(e.preventDefault(),t.dispatchCommand(n),!0)},K),t.registerCommand(V,()=>!!ae(M())&&(G([a()]),!0),K),t.registerCommand(Q,()=>fe(Q),K),t.registerCommand(X,()=>fe(X,e),K),t.registerCommand(J,t=>{const e=M();if(!I(e))return!1;const{anchor:n}=e,r=n.getNode();if(!ae(e))return!1;const i=r.getParent();return e.isCollapsed()&&0===n.offset&&null===r.getPreviousSibling()&&Lt(i)&&null===i.getPreviousSibling()?(t.preventDefault(),!0):he(J,t)},K),t.registerCommand(R,t=>{const e=M();if(!I(e))return!1;const{anchor:n}=e,r=n.getNode();return!!ae(e)&&(e.isCollapsed()&&n.offset===r.getTextContentSize()&&null===r.getNextSibling()&&Lt(r.getParentOrThrow())&&null===r.getParentOrThrow().getNextSibling()?(t.preventDefault(),!0):he(R,t))},K),t.registerCommand(Y,t=>de(Y,t),K),t.registerCommand(Z,t=>de(Z,t),K))}const me={build:(t,e)=>ct(e),config:{disabled:!1,escapeWithArrows:!1,tabSize:void 0},dependencies:[ce],name:"@lexical/code-indent",register:(t,e,n)=>{const r=n.getOutput();return lt(()=>{if(!r.disabled.value)return pe(t,r.tabSize.value,r.escapeWithArrows.value)})}};export{Ut as $createCodeHighlightNode,Bt as $createCodeNode,mt as $getCodeLineDirection,xt as $getEndOfCodeInLine,dt as $getFirstCodeNodeOfLine,pt as $getLastCodeNodeOfLine,_t as $getStartOfCodeInLine,Vt as $isCodeHighlightNode,Lt as $isCodeNode,St as $outdentLeadingSpaces,yt as $plainifyCodeContent,ce as CodeExtension,Jt as CodeHighlightNode,ue as CodeImportExtension,le as CodeImportRules,me as CodeIndentExtension,Ht as CodeNode,Pt as DEFAULT_CODE_LANGUAGE,Ft as getDefaultCodeLanguage,pe as registerCodeIndentation};
9
+ import{sel as t,ImportOverlays as e,defineOverlayRules as n,CoreImportExtension as r,DOMImportExtension as i}from"@lexical/html";import{getTextDirection as o,$isElementNode as s,$isTabNode as l,$isLineBreakNode as c,tokenizeRawText as u,$createTabNode as a,$createLineBreakNode as g,$getSiblingCaret as f,getterTableOf as h,setterTableOf as d,setterDefaultOf as p,aliasTableOf as m,$create as _,ElementNode as x,$getDocument as y,addClassNamesToElement as S,setDOMStyleFromCSS as b,$getEditor as v,$isTextNode as C,$createParagraphNode as T,nodeSchema as N,withAccessors as A,optional as P,stringValue as F,nullable as w,isHTMLElement as O,$applyNodeReplacement as $,TextNode as D,removeClassNamesFromElement as E,isDOMDocumentNode as L,isDOMTextNode as B,$generateNodesFromRawText as H,mergeRegister as k,KEY_ENTER_COMMAND as z,$getSelection as M,$isRangeSelection as K,COMMAND_PRIORITY_LOW as I,TabNode as j,$getNodeByKey as R,KEY_ARROW_DOWN_COMMAND as W,KEY_ARROW_RIGHT_COMMAND as J,KEY_ARROW_UP_COMMAND as q,KEY_ARROW_LEFT_COMMAND as U,KEY_TAB_COMMAND as V,INSERT_TAB_COMMAND as G,$insertNodes as Q,INDENT_CONTENT_COMMAND as X,OUTDENT_CONTENT_COMMAND as Y,MOVE_TO_START as Z,MOVE_TO_END as tt,$createPoint as et,$setSelectionFromCaretRange as nt,$getCaretRangeInDirection as rt,$getCaretRange as it,$getTextPointCaret as ot,$normalizeCaret as st}from"lexical";import{getPeerDependencyFromEditor as lt}from"@lexical/extension/getPeerDependencyFromEditor";import{effect as ct}from"@lexical/extension/signals";import{namedSignals as ut}from"@lexical/extension/namedSignals";import{$onEscapeDown as at,$onEscapeUp as gt}from"@lexical/utils";
10
+ /*@__INLINE__*/function ft(t){return()=>{}}function ht(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 dt(t,e){let n=t;for(let r=f(t,e);r&&(Gt(r.origin)||l(r.origin));r=r.getAdjacentCaret())n=r.origin;return n}function pt(t){return dt(t,"previous")}function mt(t){return dt(t,"next")}function _t(t){const e=pt(t),n=mt(t);let r=e;for(;null!==r;){if(Gt(r)){const t=o(r.getTextContent());if(null!==t)return t}if(r===n)break;r=r.getNextSibling()}const i=e.getParent();if(s(i)){const t=i.getDirection();if("ltr"===t||"rtl"===t)return t}return null}function xt(t,e){let n=null,r=null,i=t,o=e,s=t.getTextContent();for(;;){if(0===o){const t=i.getPreviousSibling();if(null===t){i=null;break}if(Gt(t)||l(t)||c(t)||ht(167),i=t,c(i)){n={node:i,offset:1};break}o=Math.max(0,i.getTextContentSize()-1),s=i.getTextContent()}else o--;const t=s[o];Gt(i)&&" "!==t&&(r={node:i,offset:o})}if(null!==r)return r;let u=null;if(e<t.getTextContentSize())Gt(t)&&(u=t.getTextContent()[e]);else{const e=t.getNextSibling();Gt(e)&&(u=e.getTextContent()[0])}if(null!==u&&" "!==u)return n;{const r=function(t,e){let n=t,r=e,i=t.getTextContent(),o=t.getTextContentSize();for(;;){if(!Gt(n)||r===o){if(n=n.getNextSibling(),null===n||c(n))return null;Gt(n)&&(r=0,i=n.getTextContent(),o=n.getTextContentSize())}if(Gt(n)){if(" "!==i[r])return{node:n,offset:r};r++}}}(t,e);return null!==r?r:n}}function yt(t){const e=mt(t);return c(e)&&ht(168),e}function St(t){const e=[];return u(t,{linebreak:()=>e.push(g()),tab:()=>e.push(a()),text:t=>e.push(Vt(t))}),e}function bt(t,e,n){if(!Number.isInteger(e)||e<=0)return!1;const r=t.getTextContent(),i=/^ +/.exec(r);if(!i)return!1;const o=Math.min(e,i[0].length),s=t.getKey(),l=n.anchor.key===s&&"text"===n.anchor.type?n.anchor.offset:null,c=n.focus.key===s&&"text"===n.focus.type?n.focus.offset:null;return t.spliceText(0,o,""),null!==l&&n.anchor.set(s,Math.max(0,l-o),"text"),null!==c&&n.focus.set(s,Math.max(0,c-o),"text"),!0}const vt=/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;function Ct(t,e){if("number"==typeof t)return Number.isFinite(t)?t:e;if("string"!=typeof t||!vt.test(t))return e;const n=Number(t);return Number.isFinite(n)?n:e}const Tt=t=>{const e=h(t,"format"),n=d(t,"format"),r=p(t,"format");return{exportJSON:function(t){const n=t.__textFormat,r=t.__textStyle,i=(0!==n||""!==r)&&t.shouldSerializeTextStyles();return{children:[],language:t.__language,theme:t.__theme,direction:t.__dir,format:e[t.__format],indent:t.__indent,textFormat:0!==n&&i?n:void 0,textStyle:""!==r&&i?r:void 0,type:t.__type,version:1}},exportCompactJSON:function(t){const n=t.__textFormat,r=t.__textStyle,i=(0!==n||""!==r)&&t.shouldSerializeTextStyles(),o={type:t.__type,children:[]},s=t.__language;void 0!==s&&(o.language=s);const l=t.__theme;void 0!==l&&(o.theme=l);const c=t.__dir;null!=c&&(o.direction=c);const u=e[t.__format];void 0!==u&&""!==u&&(o.format=u);const a=t.__indent;return void 0!==a&&0!==a&&(o.indent=a),void 0!==n&&0!==n&&i&&(o.textFormat=n),void 0!==r&&""!==r&&i&&(o.textStyle=r),o},updateFromJSON:function(t,e){const i=e.direction;t.__dir=null===i||"ltr"===i||"rtl"===i?i:null;const o=e.format;t.__format="string"==typeof o&&o in n?n[o]:r,t.__indent=function(t){const e=Ct(t,0);return e>=0&&e<=Infinity&&Number.isInteger(e)?e:0}(e.indent),t.__textFormat=Ct(e.textFormat,0);const s=e.textStyle;t.__textStyle="string"==typeof s?s:"";const l=e.language;t.setLanguage(null==l||"string"==typeof l?l:"");const c=e.theme;return t.setTheme(void 0===c?void 0:"string"==typeof c?c:""),t}}};function Nt(t,e){t.__highlightType=e.__highlightType}const At=t=>{const e=h(t,"mode"),n=m(t,"detail",0),r=m(t,"format",0),i=d(t,"mode"),o=p(t,"mode");return{exportJSON:function(t){return{highlightType:t.__highlightType,detail:t.__detail,format:t.__format,mode:e[t.__mode],style:t.__style,text:t.__text,type:t.__type,version:1}},exportCompactJSON:function(t){const n={type:t.__type},r=t.__highlightType;void 0!==r&&(n.highlightType=r);const i=t.__detail;void 0!==i&&0!==i&&(n.detail=i);const o=t.__format;void 0!==o&&0!==o&&(n.format=o);const s=e[t.__mode];void 0!==s&&"normal"!==s&&(n.mode=s);const l=t.__style;void 0!==l&&""!==l&&(n.style=l);const c=t.__text;return void 0!==c&&""!==c&&(n.text=c),n},updateFromJSON:function(t,e){const s=e.detail;t.__detail="string"==typeof s&&s in n?n[s]:Ct(s,0);const l=e.format;t.setFormat("string"==typeof l&&l in r?r[l]:Ct(l,0));const c=e.mode;t.__mode="string"==typeof c&&c in i?i[c]:o;const u=e.style;t.__style="string"==typeof u?u:"";const a=e.text;t.__text="string"==typeof a?a:"";const g=e.highlightType;return t.setHighlightType(null==g||"string"==typeof g?g:""),t},afterCloneFrom:Nt}},Pt=/* @__PURE__ */N()({language:/* @__PURE__ */A(/* @__PURE__ */P(/* @__PURE__ */w(/* @__PURE__ */F())),{getter:{field:"__language"}}),theme:/* @__PURE__ */A(/* @__PURE__ */P(/* @__PURE__ */F()),{getter:{field:"__theme"}})}),Ft="javascript",wt=()=>Ft;function Ot(t,e){for(const n of t.childNodes){if(O(n)&&n.tagName===e)return!0;if(Ot(n,e))return!0}return!1}const $t="data-language",Dt="data-highlight-language",Et="data-theme",Lt=/* @__PURE__ */ft();class Bt extends x{__language;__theme;__isSyntaxHighlightSupported;$config(){return this.config("code",{extends:x,generated:Tt,importDOM:{code:t=>null!=t.textContent&&(/\r?\n/.test(t.textContent)||Ot(t,"BR"))?{conversion:zt,priority:1}:null,div:()=>({conversion:Mt,priority:1}),pre:()=>({conversion:zt,priority:0}),table:t=>Rt(t)?{conversion:Kt,priority:3}:null,td:t=>{const e=t,n=e.closest("table");return e.classList.contains("js-file-line")||n&&Rt(n)?{conversion:It,priority:3}:null},tr:t=>{const e=t.closest("table");return e&&Rt(e)?{conversion:It,priority:3}:null}},json:Pt})}constructor(t=void 0,e){super(e),this.__language=t||void 0,this.__isSyntaxHighlightSupported=!1,this.__theme=void 0}afterCloneFrom(t){super.afterCloneFrom(t),function(t,e){t.__language=e.__language,t.__theme=e.__theme}(this,t),this.__isSyntaxHighlightSupported=t.__isSyntaxHighlightSupported}createDOM(t){const e=y().createElement("code");S(e,t.theme.code),e.setAttribute("spellcheck","false");const n=this.getLanguage();n&&(e.setAttribute($t,n),this.getIsSyntaxHighlightSupported()&&e.setAttribute(Dt,n));const r=this.getTheme();r&&e.setAttribute(Et,r);const i=this.getStyle();return i&&b(e.style,i),e}updateDOM(t,e,n){const r=this.__language,i=t.__language;r?r!==i&&e.setAttribute($t,r):i&&e.removeAttribute($t);const o=this.__isSyntaxHighlightSupported;t.__isSyntaxHighlightSupported&&i?o&&r?r!==i&&e.setAttribute(Dt,r):e.removeAttribute(Dt):o&&r&&e.setAttribute(Dt,r);const s=this.__theme,l=t.__theme;s?s!==l&&e.setAttribute(Et,s):l&&e.removeAttribute(Et);const c=this.__style,u=t.__style;return c!==u&&b(e.style,c,u),!1}exportDOM(t){const e=y().createElement("pre");S(e,t._config.theme.code),e.setAttribute("spellcheck","false");const n=this.getLanguage();n&&(e.setAttribute($t,n),this.getIsSyntaxHighlightSupported()&&e.setAttribute(Dt,n));const r=this.getTheme();r&&e.setAttribute(Et,r);const i=this.getStyle();return i&&b(e.style,i),{element:e}}insertNewAfter(t,e=!0){if(!lt(v(),"@lexical/code")){Lt();const e=Wt(t);if(e)return e}const{anchor:n,focus:r}=t,i=(n.isBefore(r)?n:r).getNode();if(C(i)){let t=pt(i);const e=[];for(;;)if(l(t))e.push(a()),t=t.getNextSibling();else{if(!Gt(t))break;{let n=0;const r=t.getTextContent(),i=t.getTextContentSize();for(;n<i&&" "===r[n];)n++;if(0!==n&&e.push(Vt(" ".repeat(n))),n!==i)break;t=t.getNextSibling()}}const r=i.splitText(n.offset)[0],o=0===n.offset?0:1,s=r.getIndexWithinParent()+o,c=i.getParentOrThrow(),u=[g(),...e];c.splice(s,0,u);const f=e[e.length-1];f?f.select():0===n.offset?r.selectPrevious():r.getNextSibling().selectNext(0,0)}if(kt(i)){const{offset:e}=t.anchor;i.splice(e,0,[g()]),i.select(e+1,e+1)}return null}canIndent(){return!1}collapseAtStart(){const t=T();return this.getChildren().forEach(e=>t.append(e)),this.replace(t),!0}setLanguage(t){const e=this.getWritable();return e.__language=t||void 0,e}getLanguage(){return this.getLatest().__language}setIsSyntaxHighlightSupported(t){const e=this.getWritable();return e.__isSyntaxHighlightSupported=t,e}getIsSyntaxHighlightSupported(){return this.getLatest().__isSyntaxHighlightSupported}setTheme(t){const e=this.getWritable();return e.__theme=t||void 0,e}getTheme(){return this.getLatest().__theme}}function Ht(t,e){return _(Bt).setLanguage(t).setTheme(e)}function kt(t){return t instanceof Bt}function zt(t){return{node:Ht(t.getAttribute($t),t.getAttribute(Et))}}function Mt(t){const e=t,n=jt(e);return n||function(t){let e=t.parentElement;for(;null!==e;){if(jt(e))return!0;e=e.parentElement}return!1}(e)?{node:n?Ht():null}:{node:null}}function Kt(){return{node:Ht()}}function It(){return{node:null}}function jt(t){return null!==t.style.fontFamily.match("monospace")}function Rt(t){return t.classList.contains("js-file-line-container")}function Wt(t){const{anchor:e}=t;if(t.isCollapsed()&&"element"===e.type){const t=e.getNode();if(kt(t)){const n=t.getChildrenSize();if(n>=2&&e.offset===n){const e=t.getLastChild();if(c(e)&&c(e.getPreviousSibling())){const e=T();return t.splice(n-2,2,[]).insertAfter(e,!1),e.select(),e}}}}return null}const Jt=/* @__PURE__ */N()({highlightType:/* @__PURE__ */A(/* @__PURE__ */P(/* @__PURE__ */w(/* @__PURE__ */F())),{getter:{field:"__highlightType"}})});class qt extends D{__highlightType;constructor(t="",e,n){super(t,n),this.__highlightType=e}$config(){return this.config("code-highlight",{extends:D,generated:At,json:Jt})}getHighlightType(){return this.getLatest().__highlightType}setHighlightType(t){const e=this.getWritable();return e.__highlightType=t||void 0,e}canHaveFormat(){return!1}createDOM(t){const e=super.createDOM(t),n=Ut(t.theme,this.__highlightType);return S(e,n),e}updateDOM(t,e,n){const r=super.updateDOM(t,e,n),i=Ut(n.theme,t.__highlightType),o=Ut(n.theme,this.__highlightType);return i!==o&&(i&&E(e,i),o&&S(e,o)),r}setFormat(t){return this}isParentRequired(){return!0}createParentElementNode(){return Ht()}}function Ut(t,e){return e&&t&&t.codeHighlight&&t.codeHighlight[e]}function Vt(t="",e){return $(new qt(t,e))}function Gt(t){return t instanceof qt}const Qt="data-language",Xt="data-theme";function Yt(t){return null!==t.style.fontFamily.match("monospace")}function Zt(t){let e=t.parentElement;for(;null!==e;){if(Yt(e))return!0;e=e.parentElement}return!1}const te=/* @__PURE__ */n([{$import:(t,e)=>t.$importChildren(e),match:/* @__PURE__ */t.tag("tr","td"),name:"@lexical/code/github-code-table/unwrap"}]),ee={$import:(t,e)=>[Ht(e.getAttribute(Qt),e.getAttribute(Xt)).splice(0,0,t.$importChildren(e))],match:/* @__PURE__ */t.tag("pre"),name:"@lexical/code/pre"},ne={$import:(t,e,n)=>{const r=e.textContent||"";return/\r?\n/.test(r)||null!==e.querySelector("br")?[Ht(e.getAttribute(Qt),e.getAttribute(Xt)).splice(0,0,t.$importChildren(e))]:n()},match:/* @__PURE__ */t.tag("code"),name:"@lexical/code/code-multiline"};function re(t){if(!O(t))return!1;const e=t.style.fontFamily,n=t.style.whiteSpace;return"string"==typeof e&&/monospace/i.test(e)&&"string"==typeof n&&n.startsWith("pre")}function ie(t){let e=!1;const n=[];let r="",i=!1;const o=()=>{i&&(n.push(r),r="",i=!1)};for(const s of Array.from(t.childNodes))if(O(s))"DIV"===s.tagName?(o(),n.push(s.textContent||""),e=!0):"BR"===s.tagName?(o(),n.push(""),e=!0):(r+=s.textContent||"",i=!0);else if(B(s)){const t=s.textContent||"";t.length>0&&(r+=t,i=!0)}return o(),e?n:null}function oe(t){for(const e of Array.from(t.children)){if(O(e)&&re(e)){if(null!==ie(e))return!0;const t=e.nextElementSibling;if(t&&re(t))return!0;continue}if(oe(e))return!0}return!1}const se=/* @__PURE__ */n([{$import:(t,e,n)=>{if(!re(e)||Zt(e))return n();const r=ie(e);return null===r||0===r.length?n():[Ht().splice(0,0,H(r.join("\n")))]},match:/* @__PURE__ */t.tag("div"),name:"@lexical/code/vscode-wrapper"},{$import:(t,e,n)=>{if(!re(e)||Zt(e))return n();const r=e.previousElementSibling;if(r&&re(r))return[];const i=[];let o=e;for(;o&&re(o);)i.push("BR"===o.tagName?"":o.textContent||""),o=o.nextElementSibling;return i.length<2?n():[Ht().splice(0,0,H(i.join("\n")))]},match:/* @__PURE__ */t.tag("div","br"),name:"@lexical/code/vscode-line-run"}]),le={$import:(t,e,n)=>Yt(e)?[Ht().splice(0,0,t.$importChildren(e))]:Zt(e)?t.$importChildren(e):n(),match:/* @__PURE__ */t.tag("div"),name:"@lexical/code/div"},ce=[{$import:(t,e)=>[Ht().splice(0,0,t.$importChildren(e,{rules:te}))],match:/* @__PURE__ */t.tag("table").classAll("js-file-line-container"),name:"@lexical/code/github-code-table"},{$import:(t,e)=>t.$importChildren(e),match:/* @__PURE__ */t.tag("td").classAll("js-file-line"),name:"@lexical/code/github-code-cell-by-class"},ne,ee,le],ue={dependencies:[r,[i,{preprocess:[(t,n,r)=>{oe(L(t)?t.body:t)&&n.session.update(e,t=>[...t,se]),r()}],rules:ce}]],name:"@lexical/code",nodes:()=>[Bt,qt],register:t=>k(t.registerCommand(z,t=>{const e=M();return!(!K(e)||!Wt(e)||(null!==t&&t.preventDefault(),0))},I),t.registerNodeTransform(j,t=>{0!==t.getFormat()&&kt(t.getParent())&&t.setFormat(0)}))},ae={dependencies:[ue],name:"@lexical/code/Import"};function ge(t){const e=v().getElementByKey(t.getKey());if(null===e)return;const n=t.getChildren(),r=n.length;if(r===e.__cachedChildrenLength)return;e.__cachedChildrenLength=r;let i="1",o=1;for(let t=0;t<r;t++)c(n[t])&&(i+="\n"+ ++o);e.setAttribute("data-gutter",i)}function fe(t){return t._headless?()=>{}:t.registerMutationListener(Bt,e=>{t.read("latest",()=>{for(const[t,n]of e)if("destroyed"!==n){const e=R(t);null!==e&&ge(e)}})},{skipInitialization:!1})}function he(t){if(!K(t))return!1;const e=t.anchor.getNode(),n=kt(e)?e:e.getParent(),r=t.focus.getNode(),i=kt(r)?r:r.getParent();return kt(n)&&n.is(i)}function de(t){const e=t.getNodes(),n=[];if(1===e.length&&kt(e[0]))return n;let r=[];for(let t=0;t<e.length;t++){const i=e[t];Gt(i)||l(i)||c(i)||ht(169),c(i)?r.length>0&&(n.push(r),r=[]):r.push(i)}if(r.length>0){const e=t.isBackward()?t.anchor:t.focus,i=et(r[0].getKey(),0,"text");e.is(i)||n.push(r)}return n}function pe(t,e){const n=M();if(!K(n)||!he(n))return!1;const r=de(n),i=r.length;if(0===i&&n.isCollapsed())return t===X?n.insertNodes([a()]):function(t,e){const n=t.anchor.getNode();if(!Gt(n)&&!l(n))return;const r=pt(n);l(r)?r.remove():void 0!==e&&Gt(r)&&bt(r,e,t)}(n,e),!0;if(0===i&&t===X&&"\n"===n.getTextContent()){const t=a(),e=g(),r=n.isBackward()?"previous":"next";return n.insertNodes([t,e]),nt(rt(it(ot(t,"next",0),st(f(e,"next"))),r)),!0}for(let o=0;o<i;o++){const i=r[o];if(i.length>0){let r=i[0];if(0===o&&(r=pt(r)),t===X){const t=a();if(r.insertBefore(t),0===o){const e=n.isBackward()?"focus":"anchor",i=et(r.getKey(),0,"text");n[e].is(i)&&n[e].set(t.getKey(),0,"text")}}else l(r)?r.remove():void 0!==e&&Gt(r)&&bt(r,e,n)}}return!0}function me(t,e){const n=M();if(!K(n))return!1;const{anchor:r,focus:i}=n,o=r.offset,s=i.offset,u=r.getNode(),a=i.getNode(),g=t===q;if(!he(n)||!Gt(u)&&!l(u)||!Gt(a)&&!l(a))return!1;if(!e.altKey){if(n.isCollapsed()){const t=u.getParentOrThrow();if(g&&0===o&&null===u.getPreviousSibling()){if(null===t.getPreviousSibling())return t.selectPrevious(),e.preventDefault(),!1}else if(!g&&o===u.getTextContentSize()&&null===u.getNextSibling()&&null===t.getNextSibling())return t.selectNext(),e.preventDefault(),!1}return!1}let f,h;if(u.isBefore(a)?(f=pt(u),h=mt(a)):(f=pt(a),h=mt(u)),null==f||null==h)return!1;const d=f.getNodesBetween(h);for(let t=0;t<d.length;t++){const e=d[t];if(!Gt(e)&&!l(e)&&!c(e))return!1}e.preventDefault(),e.stopPropagation();const p=g?f.getPreviousSibling():h.getNextSibling();if(!c(p))return!0;const m=g?p.getPreviousSibling():p.getNextSibling();if(null==m)return!0;const _=c(m),x=_||!Gt(m)&&!l(m)?null:g?pt(m):mt(m);let y=null!=x?x:m;return p.remove(),d.forEach(t=>t.remove()),_?(d.forEach(t=>{y.insertAfter(t),y=t}),y.insertAfter(p)):t===q?(d.forEach(t=>y.insertBefore(t)),y.insertBefore(p)):(y.insertAfter(p),y=p,d.forEach(t=>{y.insertAfter(t),y=t})),n.setTextNodeRange(u,o,a,s),!0}function _e(t,e){const n=M();if(!K(n))return!1;const{anchor:r,focus:i}=n,o=r.getNode(),s=i.getNode(),u=t===Z;if(!he(n)||!Gt(o)&&!l(o)||!Gt(s)&&!l(s))return!1;const a=s,g="rtl"===_t(a)?!u:u,f=r.key,h=r.offset,d=r.type;if(g){const t=xt(a,i.offset);if(null!==t){const{node:e,offset:r}=t;c(e)?e.selectNext(0,0):n.setTextNodeRange(e,r,e,r)}else a.getParentOrThrow().selectStart()}else yt(a).select();return e.shiftKey&&n.anchor.set(f,h,d),e.preventDefault(),e.stopPropagation(),!0}function xe(t,e,n){return k(...n?[t.registerCommand(W,t=>!t.altKey&&at(kt,t),I),t.registerCommand(J,t=>at(kt,t),I),t.registerCommand(q,t=>!t.altKey&&gt(kt,t),I),t.registerCommand(U,t=>gt(kt,t),I)]:[],t.registerCommand(V,e=>{const n=function(t){const e=M();if(!K(e)||!he(e))return null;const n=t?Y:X,r=t?Y:G,i=e.anchor,o=e.focus;if(i.is(o))return r;const s=de(e);if(1!==s.length)return n;const l=s[0];let c,u;0===l.length&&ht(285),e.isBackward()?(c=o,u=i):(c=i,u=o);const a=pt(l[0]),g=mt(l[0]),f=et(a.getKey(),0,"text"),h=et(g.getKey(),g.getTextContentSize(),"text");return c.isBefore(f)||h.isBefore(u)?n:f.isBefore(c)||u.isBefore(h)?r:n}(e.shiftKey);return null!==n&&(e.preventDefault(),t.dispatchCommand(n),!0)},I),t.registerCommand(G,()=>!!he(M())&&(Q([a()]),!0),I),t.registerCommand(X,()=>pe(X),I),t.registerCommand(Y,()=>pe(Y,e),I),t.registerCommand(q,t=>{const e=M();if(!K(e))return!1;const{anchor:n}=e,r=n.getNode();if(!he(e))return!1;const i=r.getParent();return e.isCollapsed()&&0===n.offset&&null===r.getPreviousSibling()&&kt(i)&&null===i.getPreviousSibling()?(t.preventDefault(),!0):me(q,t)},I),t.registerCommand(W,t=>{const e=M();if(!K(e))return!1;const{anchor:n}=e,r=n.getNode();return!!he(e)&&(e.isCollapsed()&&n.offset===r.getTextContentSize()&&null===r.getNextSibling()&&kt(r.getParentOrThrow())&&null===r.getParentOrThrow().getNextSibling()?(t.preventDefault(),!0):me(W,t))},I),t.registerCommand(Z,t=>_e(Z,t),I),t.registerCommand(tt,t=>_e(tt,t),I))}const ye={build:(t,e)=>ut(e),config:{disabled:!1,escapeWithArrows:!1,tabSize:void 0},dependencies:[ue],name:"@lexical/code-indent",register:(t,e,n)=>{const r=n.getOutput();return ct(()=>{if(!r.disabled.value)return xe(t,r.tabSize.value,r.escapeWithArrows.value)})}};export{Vt as $createCodeHighlightNode,Ht as $createCodeNode,_t as $getCodeLineDirection,yt as $getEndOfCodeInLine,pt as $getFirstCodeNodeOfLine,mt as $getLastCodeNodeOfLine,xt as $getStartOfCodeInLine,Gt as $isCodeHighlightNode,kt as $isCodeNode,bt as $outdentLeadingSpaces,St as $plainifyCodeContent,ue as CodeExtension,qt as CodeHighlightNode,ae as CodeImportExtension,ce as CodeImportRules,ye as CodeIndentExtension,Bt as CodeNode,Ft as DEFAULT_CODE_LANGUAGE,wt as getDefaultCodeLanguage,fe as registerCodeGutter,xe as registerCodeIndentation};
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  *
7
7
  */
8
8
  export { CodeExtension, CodeImportExtension } from './CodeExtension.js';
9
+ export { registerCodeGutter } from './CodeGutter.js';
9
10
  export { $createCodeHighlightNode, $isCodeHighlightNode, CodeHighlightNode, } from './CodeHighlightNode.js';
10
11
  export { CodeImportRules } from './CodeImportExtension.js';
11
12
  export { type CodeIndentConfig, CodeIndentExtension, registerCodeIndentation, } from './CodeIndentation.js';
package/package.json CHANGED
@@ -8,15 +8,15 @@
8
8
  "code"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.51.1-nightly.20260922.0",
11
+ "version": "0.51.1-nightly.20260924.0",
12
12
  "main": "./dist/LexicalCodeCore.js",
13
13
  "types": "./dist/typescript-too-old.d.ts",
14
14
  "dependencies": {
15
- "@lexical/extension": "0.51.1-nightly.20260922.0",
16
- "@lexical/html": "0.51.1-nightly.20260922.0",
17
- "@lexical/internal": "0.51.1-nightly.20260922.0",
18
- "@lexical/utils": "0.51.1-nightly.20260922.0",
19
- "lexical": "0.51.1-nightly.20260922.0"
15
+ "@lexical/extension": "0.51.1-nightly.20260924.0",
16
+ "@lexical/html": "0.51.1-nightly.20260924.0",
17
+ "@lexical/internal": "0.51.1-nightly.20260924.0",
18
+ "@lexical/utils": "0.51.1-nightly.20260924.0",
19
+ "lexical": "0.51.1-nightly.20260924.0"
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import {
10
+ $getEditor,
11
+ $getNodeByKey,
12
+ $isLineBreakNode,
13
+ type LexicalEditor,
14
+ } from 'lexical';
15
+
16
+ import {CodeNode} from './CodeNode';
17
+
18
+ /**
19
+ * Write the line numbers of a {@link CodeNode} to the `data-gutter`
20
+ * attribute of its DOM element as a newline separated list (`"1\n2\n3"`),
21
+ * so a theme can render them with `content: attr(data-gutter)`. There is
22
+ * one line per `LineBreakNode` child plus one. The attribute is only
23
+ * rewritten when the number of children has changed since the last call.
24
+ *
25
+ * @param node The CodeNode whose gutter should be updated.
26
+ */
27
+ function $updateCodeGutter(node: CodeNode): void {
28
+ const codeElement = $getEditor().getElementByKey(node.getKey());
29
+ if (codeElement === null) {
30
+ return;
31
+ }
32
+ const children = node.getChildren();
33
+ const childrenLength = children.length;
34
+ // @ts-ignore: internal field
35
+ if (childrenLength === codeElement.__cachedChildrenLength) {
36
+ // Avoid updating the attribute if the children length hasn't changed.
37
+ return;
38
+ }
39
+ // @ts-ignore:: internal field
40
+ codeElement.__cachedChildrenLength = childrenLength;
41
+ let gutter = '1';
42
+ let count = 1;
43
+ for (let i = 0; i < childrenLength; i++) {
44
+ if ($isLineBreakNode(children[i])) {
45
+ gutter += '\n' + ++count;
46
+ }
47
+ }
48
+ codeElement.setAttribute('data-gutter', gutter);
49
+ }
50
+
51
+ /**
52
+ * @internal
53
+ * Keep the `data-gutter` attribute of every {@link CodeNode} in sync with its
54
+ * line count, so a theme can render line numbers with
55
+ * `content: attr(data-gutter)`.
56
+ *
57
+ * Both `@lexical/code-prism` and `@lexical/code-shiki` register this. In
58
+ * headless mode there is no DOM to write to, so registration is skipped and
59
+ * the returned teardown does nothing.
60
+ *
61
+ * @param editor The editor whose code blocks should carry a gutter.
62
+ * @returns A teardown that removes the listener.
63
+ */
64
+ export function registerCodeGutter(editor: LexicalEditor): () => void {
65
+ if (editor._headless) {
66
+ return () => {};
67
+ }
68
+ return editor.registerMutationListener(
69
+ CodeNode,
70
+ mutations => {
71
+ editor.read('latest', () => {
72
+ for (const [key, type] of mutations) {
73
+ if (type !== 'destroyed') {
74
+ const node = $getNodeByKey(key);
75
+ if (node !== null) {
76
+ $updateCodeGutter(node as CodeNode);
77
+ }
78
+ }
79
+ }
80
+ });
81
+ },
82
+ {skipInitialization: false},
83
+ );
84
+ }
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  export {CodeExtension, CodeImportExtension} from './CodeExtension';
10
+ export {registerCodeGutter} from './CodeGutter';
10
11
  export {
11
12
  $createCodeHighlightNode,
12
13
  $isCodeHighlightNode,