10 lines
18 KiB
JavaScript
10 lines
18 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
*/
|
|
|
|
import{isHTMLElement as t,addClassNamesToElement as e,removeClassNamesFromElement as n,$getAdjacentCaret as r,mergeRegister as i}from"@lexical/utils";import{ElementNode as o,$createParagraphNode as s,$isTextNode as u,$isTabNode as l,$createTabNode as c,$createLineBreakNode as a,$create as g,TextNode as f,$applyNodeReplacement as p,$getSiblingCaret as h,$isLineBreakNode as d,$createTextNode as m,$getNodeByKey as y,$getSelection as x,$isRangeSelection as S,$createPoint as _,INDENT_CONTENT_COMMAND as v,OUTDENT_CONTENT_COMMAND as T,INSERT_TAB_COMMAND as b,$setSelectionFromCaretRange as C,$getCaretRangeInDirection as N,$getCaretRange as j,$getTextPointCaret as w,$normalizeCaret as k,KEY_ARROW_UP_COMMAND as A,MOVE_TO_START as P,KEY_TAB_COMMAND as L,COMMAND_PRIORITY_LOW as O,$insertNodes as H,KEY_ARROW_DOWN_COMMAND as z,MOVE_TO_END as E}from"lexical";import"prismjs";import"prismjs/components/prism-clike.js";import"prismjs/components/prism-javascript.js";import"prismjs/components/prism-markup.js";import"prismjs/components/prism-markdown.js";import"prismjs/components/prism-c.js";import"prismjs/components/prism-css.js";import"prismjs/components/prism-objectivec.js";import"prismjs/components/prism-sql.js";import"prismjs/components/prism-powershell.js";import"prismjs/components/prism-python.js";import"prismjs/components/prism-rust.js";import"prismjs/components/prism-swift.js";import"prismjs/components/prism-typescript.js";import"prismjs/components/prism-java.js";import"prismjs/components/prism-cpp.js";function B(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.`)}const D="javascript",F=()=>D;function I(e,n){for(const r of e.childNodes){if(t(r)&&r.tagName===n)return!0;I(r,n)}return!1}const M="data-language",J="data-highlight-language",R="data-theme";class K extends o{static getType(){return"code"}static clone(t){return new K(t.__language,t.__key)}constructor(t,e){super(e),this.__language=t||void 0,this.__isSyntaxHighlightSupported=!1,this.__theme=void 0}afterCloneFrom(t){super.afterCloneFrom(t),this.__language=t.__language,this.__theme=t.__theme,this.__isSyntaxHighlightSupported=t.__isSyntaxHighlightSupported}createDOM(t){const n=document.createElement("code");e(n,t.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();r&&(n.setAttribute(M,r),this.getIsSyntaxHighlightSupported()&&n.setAttribute(J,r));const i=this.getTheme();i&&n.setAttribute(R,i);const o=this.getStyle();return o&&n.setAttribute("style",o),n}updateDOM(t,e,n){const r=this.__language,i=t.__language;r?r!==i&&e.setAttribute(M,r):i&&e.removeAttribute(M);const o=this.__isSyntaxHighlightSupported;t.__isSyntaxHighlightSupported&&i?o&&r?r!==i&&e.setAttribute(J,r):e.removeAttribute(J):o&&r&&e.setAttribute(J,r);const s=this.__theme,u=t.__theme;s?s!==u&&e.setAttribute(R,s):u&&e.removeAttribute(R);const l=this.__style,c=t.__style;return l?l!==c&&e.setAttribute("style",l):c&&e.removeAttribute("style"),!1}exportDOM(t){const n=document.createElement("pre");e(n,t._config.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();r&&(n.setAttribute(M,r),this.getIsSyntaxHighlightSupported()&&n.setAttribute(J,r));const i=this.getTheme();i&&n.setAttribute(R,i);const o=this.getStyle();return o&&n.setAttribute("style",o),{element:n}}static importDOM(){return{code:t=>null!=t.textContent&&(/\r?\n/.test(t.textContent)||I(t,"BR"))?{conversion:q,priority:1}:null,div:()=>({conversion:U,priority:1}),pre:()=>({conversion:q,priority:0}),table:t=>V(t)?{conversion:X,priority:3}:null,td:t=>{const e=t,n=e.closest("table");return e.classList.contains("js-file-line")||n&&V(n)?{conversion:Q,priority:3}:null},tr:t=>{const e=t.closest("table");return e&&V(e)?{conversion:Q,priority:3}:null}}}static importJSON(t){return $().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setLanguage(t.language).setTheme(t.theme)}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),theme:this.getTheme()}}insertNewAfter(t,e=!0){const n=this.getChildren(),r=n.length;if(r>=2&&"\n"===n[r-1].getTextContent()&&"\n"===n[r-2].getTextContent()&&t.isCollapsed()&&t.anchor.key===this.__key&&t.anchor.offset===r){n[r-1].remove(),n[r-2].remove();const t=s();return this.insertAfter(t,e),t}const{anchor:i,focus:o}=t,g=(i.isBefore(o)?i:o).getNode();if(u(g)){let t=rt(g);const e=[];for(;;)if(l(t))e.push(c()),t=t.getNextSibling();else{if(!et(t))break;{let n=0;const r=t.getTextContent(),i=t.getTextContentSize();for(;n<i&&" "===r[n];)n++;if(0!==n&&e.push(tt(" ".repeat(n))),n!==i)break;t=t.getNextSibling()}}const n=g.splitText(i.offset)[0],r=0===i.offset?0:1,o=n.getIndexWithinParent()+r,s=g.getParentOrThrow(),u=[a(),...e];s.splice(o,0,u);const f=e[e.length-1];f?f.select():0===i.offset?n.selectPrevious():n.getNextSibling().selectNext(0,0)}if(W(g)){const{offset:e}=t.anchor;g.splice(e,0,[a()]),g.select(e+1,e+1)}return null}canIndent(){return!1}collapseAtStart(){const t=s();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 $(t,e){return g(K).setLanguage(t).setTheme(e)}function W(t){return t instanceof K}function q(t){return{node:$(t.getAttribute(M))}}function U(t){const e=t,n=G(e);return n||function(t){let e=t.parentElement;for(;null!==e;){if(G(e))return!0;e=e.parentElement}return!1}(e)?{node:n?$():null}:{node:null}}function X(){return{node:$()}}function Q(){return{node:null}}function G(t){return null!==t.style.fontFamily.match("monospace")}function V(t){return t.classList.contains("js-file-line-container")}class Y extends f{constructor(t="",e,n){super(t,n),this.__highlightType=e}static getType(){return"code-highlight"}static clone(t){return new Y(t.__text,t.__highlightType||void 0,t.__key)}getHighlightType(){return this.getLatest().__highlightType}setHighlightType(t){const e=this.getWritable();return e.__highlightType=t||void 0,e}canHaveFormat(){return!1}createDOM(t){const n=super.createDOM(t),r=Z(t.theme,this.__highlightType);return e(n,r),n}updateDOM(t,r,i){const o=super.updateDOM(t,r,i),s=Z(i.theme,t.__highlightType),u=Z(i.theme,this.__highlightType);return s!==u&&(s&&n(r,s),u&&e(r,u)),o}static importJSON(t){return tt().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setHighlightType(t.highlightType)}exportJSON(){return{...super.exportJSON(),highlightType:this.getHighlightType()}}setFormat(t){return this}isParentRequired(){return!0}createParentElementNode(){return $()}}function Z(t,e){return e&&t&&t.codeHighlight&&t.codeHighlight[e]}function tt(t="",e){return p(new Y(t,e))}function et(t){return t instanceof Y}function nt(t,e){let n=t;for(let i=h(t,e);i&&(et(i.origin)||l(i.origin));i=r(i))n=i.origin;return n}function rt(t){return nt(t,"previous")}function it(t){return nt(t,"next")}function ot(t,e){let n=null,r=null,i=t,o=e,s=t.getTextContent();for(;;){if(0===o){if(i=i.getPreviousSibling(),null===i)break;if(et(i)||l(i)||d(i)||B(167),d(i)){n={node:i,offset:1};break}o=Math.max(0,i.getTextContentSize()-1),s=i.getTextContent()}else o--;const t=s[o];et(i)&&" "!==t&&(r={node:i,offset:o})}if(null!==r)return r;let u=null;if(e<t.getTextContentSize())et(t)&&(u=t.getTextContent()[e]);else{const e=t.getNextSibling();et(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(!et(n)||r===o){if(n=n.getNextSibling(),null===n||d(n))return null;et(n)&&(r=0,i=n.getTextContent(),o=n.getTextContentSize())}if(et(n)){if(" "!==i[r])return{node:n,offset:r};r++}}}(t,e);return null!==r?r:n}}function st(t){const e=it(t);return d(e)&&B(168),e}!function(t){t.languages.diff={coord:[/^(?:\*{3}|-{3}|\+{3}).*$/m,/^@@.*@@$/m,/^\d.*$/m]};var e={"deleted-sign":"-","deleted-arrow":"<","inserted-sign":"+","inserted-arrow":">",unchanged:" ",diff:"!"};Object.keys(e).forEach((function(n){var r=e[n],i=[];/^\w+$/.test(n)||i.push(/\w+/.exec(n)[0]),"diff"===n&&i.push("bold"),t.languages.diff[n]={pattern:RegExp("^(?:["+r+"].*(?:\r\n?|\n|(?![\\s\\S])))+","m"),alias:i,inside:{line:{pattern:/(.)(?=[\s\S]).*(?:\r\n?|\n)?/,lookbehind:!0},prefix:{pattern:/[\s\S]/,alias:/\w+/.exec(n)[0]}}}})),Object.defineProperty(t.languages.diff,"PREFIXES",{value:e})}(Prism);const ut=globalThis.Prism||window.Prism,lt={c:"C",clike:"C-like",cpp:"C++",css:"CSS",html:"HTML",java:"Java",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",powershell:"PowerShell",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",typescript:"TypeScript",xml:"XML"},ct={cpp:"cpp",java:"java",javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain",ts:"typescript"};function at(t){return ct[t]||t}function gt(t){const e=at(t);return lt[e]||e}const ft=()=>Object.keys(ut.languages).filter((t=>"function"!=typeof ut.languages[t])).sort();function pt(){const t=[];for(const[e,n]of Object.entries(lt))t.push([e,n]);return t}function ht(){return[]}function dt(t){return"string"==typeof t?t:Array.isArray(t)?t.map(dt).join(""):dt(t.content)}function mt(t,e){const n=/^diff-([\w-]+)/i.exec(e),r=t.getTextContent();let i=ut.tokenize(r,ut.languages[n?"diff":e]);return n&&(i=function(t,e){const n=e,r=ut.languages[n],i={tokens:t},o=ut.languages.diff.PREFIXES;for(const t of i.tokens){if("string"==typeof t||!(t.type in o)||!Array.isArray(t.content))continue;const e=t.type;let n=0;const i=()=>(n++,new ut.Token("prefix",o[e],e.replace(/^(\w+).*/,"$1"))),s=t.content.filter((t=>"string"==typeof t||"prefix"!==t.type)),u=t.content.length-s.length,l=ut.tokenize(dt(s),r);l.unshift(i());const c=/\r\n|\n/g,a=t=>{const e=[];c.lastIndex=0;let r,o=0;for(;n<u&&(r=c.exec(t));){const n=r.index+r[0].length;e.push(t.slice(o,n)),o=n,e.push(i())}if(0!==e.length)return o<t.length&&e.push(t.slice(o)),e},g=t=>{for(let e=0;e<t.length&&n<u;e++){const n=t[e];if("string"==typeof n){const r=a(n);r&&(t.splice(e,1,...r),e+=r.length-1)}else if("string"==typeof n.content){const t=a(n.content);t&&(n.content=t)}else Array.isArray(n.content)?g(n.content):g([n.content])}};g(l),n<u&&l.push(i()),t.content=l}return i.tokens}(i,n[1])),yt(i)}function yt(t,e){const n=[];for(const r of t)if("string"==typeof r){const t=r.split(/(\n|\t)/),i=t.length;for(let r=0;r<i;r++){const i=t[r];"\n"===i||"\r\n"===i?n.push(a()):"\t"===i?n.push(c()):i.length>0&&n.push(tt(i,e))}}else{const{content:t,alias:e}=r;"string"==typeof t?n.push(...yt([t],"prefix"===r.type&&"string"==typeof e?e:r.type)):Array.isArray(t)&&n.push(...yt(t,"unchanged"===r.type?void 0:r.type))}return n}const xt={$tokenize(t,e){return mt(t,e||this.defaultLanguage)},defaultLanguage:D,tokenize(t,e){return ut.tokenize(t,ut.languages[e||""]||ut.languages[this.defaultLanguage])}};function St(t,e,n){const r=t.getParent();W(r)?Tt(r,e,n):et(t)&&t.replace(m(t.__text))}function _t(t,e){const n=e.getElementByKey(t.getKey());if(null===n)return;const r=t.getChildren(),i=r.length;if(i===n.__cachedChildrenLength)return;n.__cachedChildrenLength=i;let o="1",s=1;for(let t=0;t<i;t++)d(r[t])&&(o+="\n"+ ++s);n.setAttribute("data-gutter",o)}const vt=new Set;function Tt(t,e,n){const r=t.getKey();void 0===t.getLanguage()&&t.setLanguage(n.defaultLanguage);const i=t.getLanguage()||n.defaultLanguage;if(!function(t){const e=function(t){const e=/^diff-([\w-]+)/i.exec(t);return e?e[1]:null}(t),n=e||t;try{return!!n&&ut.languages.hasOwnProperty(n)}catch(t){return!1}}(i))return t.getIsSyntaxHighlightSupported()&&t.setIsSyntaxHighlightSupported(!1),void async function(t,e,n){}();t.getIsSyntaxHighlightSupported()||t.setIsSyntaxHighlightSupported(!0),vt.has(r)||(vt.add(r),e.update((()=>{!function(t,e){const n=y(t);if(!W(n)||!n.isAttached())return;const r=x();if(!S(r))return void e();const i=r.anchor,o=i.offset,s="element"===i.type&&d(n.getChildAtIndex(i.offset-1));let l=0;if(!s){const t=i.getNode();l=o+t.getPreviousSiblings().reduce(((t,e)=>t+e.getTextContentSize()),0)}if(!e())return;if(s)return void i.getNode().select(o,o);n.getChildren().some((t=>{const e=u(t);if(e||d(t)){const n=t.getTextContentSize();if(e&&n>=l)return t.select(l,l),!0;l-=n}return!1}))}(r,(()=>{const e=y(r);if(!W(e)||!e.isAttached())return!1;const i=e.getLanguage()||n.defaultLanguage,o=n.$tokenize(e,i),s=function(t,e){let n=0;for(;n<t.length&&bt(t[n],e[n]);)n++;const r=t.length,i=e.length,o=Math.min(r,i)-n;let s=0;for(;s<o;)if(s++,!bt(t[r-s],e[i-s])){s--;break}const u=n,l=r-s,c=e.slice(n,i-s);return{from:u,nodesForReplacement:c,to:l}}(e.getChildren(),o),{from:u,to:l,nodesForReplacement:c}=s;return!(u===l&&!c.length)&&(t.splice(u,l-u,c),!0)}))}),{onUpdate:()=>{vt.delete(r)},skipTransforms:!0}))}function bt(t,e){return et(t)&&et(e)&&t.__text===e.__text&&t.__highlightType===e.__highlightType||l(t)&&l(e)||d(t)&&d(e)}function Ct(t){if(!S(t))return!1;const e=t.anchor.getNode(),n=W(e)?e:e.getParent(),r=t.focus.getNode(),i=W(r)?r:r.getParent();return W(n)&&n.is(i)}function Nt(t){const e=t.getNodes(),n=[];if(1===e.length&&W(e[0]))return n;let r=[];for(let t=0;t<e.length;t++){const i=e[t];et(i)||l(i)||d(i)||B(169),d(i)?r.length>0&&(n.push(r),r=[]):r.push(i)}if(r.length>0){const e=t.isBackward()?t.anchor:t.focus,i=_(r[0].getKey(),0,"text");e.is(i)||n.push(r)}return n}function jt(t){const e=x();if(!S(e)||!Ct(e))return!1;const n=Nt(e),r=n.length;if(0===r&&e.isCollapsed())return t===v&&e.insertNodes([c()]),!0;if(0===r&&t===v&&"\n"===e.getTextContent()){const t=c(),n=a(),r=e.isBackward()?"previous":"next";return e.insertNodes([t,n]),C(N(j(w(t,"next",0),k(h(n,"next"))),r)),!0}for(let i=0;i<r;i++){const r=n[i];if(r.length>0){let n=r[0];if(0===i&&(n=rt(n)),t===v){const t=c();if(n.insertBefore(t),0===i){const r=e.isBackward()?"focus":"anchor",i=_(n.getKey(),0,"text");e[r].is(i)&&e[r].set(t.getKey(),0,"text")}}else l(n)&&n.remove()}}return!0}function wt(t,e){const n=x();if(!S(n))return!1;const{anchor:r,focus:i}=n,o=r.offset,s=i.offset,u=r.getNode(),c=i.getNode(),a=t===A;if(!Ct(n)||!et(u)&&!l(u)||!et(c)&&!l(c))return!1;if(!e.altKey){if(n.isCollapsed()){const t=u.getParentOrThrow();if(a&&0===o&&null===u.getPreviousSibling()){if(null===t.getPreviousSibling())return t.selectPrevious(),e.preventDefault(),!0}else if(!a&&o===u.getTextContentSize()&&null===u.getNextSibling()){if(null===t.getNextSibling())return t.selectNext(),e.preventDefault(),!0}}return!1}let g,f;if(u.isBefore(c)?(g=rt(u),f=it(c)):(g=rt(c),f=it(u)),null==g||null==f)return!1;const p=g.getNodesBetween(f);for(let t=0;t<p.length;t++){const e=p[t];if(!et(e)&&!l(e)&&!d(e))return!1}e.preventDefault(),e.stopPropagation();const h=a?g.getPreviousSibling():f.getNextSibling();if(!d(h))return!0;const m=a?h.getPreviousSibling():h.getNextSibling();if(null==m)return!0;const y=et(m)||l(m)||d(m)?a?rt(m):it(m):null;let _=null!=y?y:m;return h.remove(),p.forEach((t=>t.remove())),t===A?(p.forEach((t=>_.insertBefore(t))),_.insertBefore(h)):(_.insertAfter(h),_=h,p.forEach((t=>{_.insertAfter(t),_=t}))),n.setTextNodeRange(u,o,c,s),!0}function kt(t,e){const n=x();if(!S(n))return!1;const{anchor:r,focus:i}=n,o=r.getNode(),s=i.getNode(),u=t===P;if(!Ct(n)||!et(o)&&!l(o)||!et(s)&&!l(s))return!1;if(u){const t=ot(s,i.offset);if(null!==t){const{node:e,offset:r}=t;d(e)?e.selectNext(0,0):n.setTextNodeRange(e,r,e,r)}else s.getParentOrThrow().selectStart()}else{st(s).select()}return e.preventDefault(),e.stopPropagation(),!0}function At(t,e){if(!t.hasNodes([K,Y]))throw new Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");null==e&&(e=xt);const n=[];return!0!==t._headless&&n.push(t.registerMutationListener(K,(e=>{t.update((()=>{for(const[n,r]of e)if("destroyed"!==r){const e=y(n);null!==e&&_t(e,t)}}))}),{skipInitialization:!1})),n.push(t.registerNodeTransform(K,(n=>Tt(n,t,e))),t.registerNodeTransform(f,(n=>St(n,t,e))),t.registerNodeTransform(Y,(n=>St(n,t,e))),t.registerCommand(L,(e=>{const n=function(t){const e=x();if(!S(e)||!Ct(e))return null;const n=t?T:v,r=t?T:b,i=e.anchor,o=e.focus;if(i.is(o))return r;const s=Nt(e);if(1!==s.length)return n;const u=s[0];let l,c;0===u.length&&B(285),e.isBackward()?(l=o,c=i):(l=i,c=o);const a=rt(u[0]),g=it(u[0]),f=_(a.getKey(),0,"text"),p=_(g.getKey(),g.getTextContentSize(),"text");return l.isBefore(f)||p.isBefore(c)?n:f.isBefore(l)||c.isBefore(p)?r:n}(e.shiftKey);return null!==n&&(e.preventDefault(),t.dispatchCommand(n,void 0),!0)}),O),t.registerCommand(b,(()=>!!Ct(x())&&(H([c()]),!0)),O),t.registerCommand(v,(t=>jt(v)),O),t.registerCommand(T,(t=>jt(T)),O),t.registerCommand(A,(t=>{const e=x();if(!S(e))return!1;const{anchor:n}=e,r=n.getNode();return!!Ct(e)&&(e.isCollapsed()&&0===n.offset&&null===r.getPreviousSibling()&&W(r.getParentOrThrow())?(t.preventDefault(),!0):wt(A,t))}),O),t.registerCommand(z,(t=>{const e=x();if(!S(e))return!1;const{anchor:n}=e,r=n.getNode();return!!Ct(e)&&(e.isCollapsed()&&n.offset===r.getTextContentSize()&&null===r.getNextSibling()&&W(r.getParentOrThrow())?(t.preventDefault(),!0):wt(z,t))}),O),t.registerCommand(P,(t=>kt(P,t)),O),t.registerCommand(E,(t=>kt(E,t)),O)),i(...n)}const Pt=rt,Lt=it,Ot=st,Ht=ot;export{tt as $createCodeHighlightNode,$ as $createCodeNode,st as $getEndOfCodeInLine,rt as $getFirstCodeNodeOfLine,it as $getLastCodeNodeOfLine,ot as $getStartOfCodeInLine,et as $isCodeHighlightNode,W as $isCodeNode,lt as CODE_LANGUAGE_FRIENDLY_NAME_MAP,ct as CODE_LANGUAGE_MAP,Y as CodeHighlightNode,K as CodeNode,D as DEFAULT_CODE_LANGUAGE,xt as PrismTokenizer,pt as getCodeLanguageOptions,ft as getCodeLanguages,ht as getCodeThemeOptions,F as getDefaultCodeLanguage,Ot as getEndOfCodeInLine,Pt as getFirstCodeNodeOfLine,gt as getLanguageFriendlyName,Lt as getLastCodeNodeOfLine,Ht as getStartOfCodeInLine,at as normalizeCodeLang,at as normalizeCodeLanguage,At as registerCodeHighlighting};
|