@lexical/code
Advanced tools
Comparing version 0.2.9 to 0.3.0
@@ -17,5 +17,8 @@ /** | ||
LexicalEditor, | ||
SerializedElementNode, | ||
SerializedTextNode, | ||
} from 'lexical'; | ||
import {ElementNode, TextNode} from 'lexical'; | ||
import {Spread} from 'libdefs/globals'; | ||
@@ -35,2 +38,4 @@ declare class CodeNode extends ElementNode { | ||
getLanguage(): string | void; | ||
importJSON(serializedNode: SerializedCodeNode): CodeNode; | ||
exportJSON(): SerializedElementNode; | ||
} | ||
@@ -79,1 +84,19 @@ declare function $createCodeNode(language?: string): CodeNode; | ||
declare function registerCodeHighlighting(editor: LexicalEditor): () => void; | ||
type SerializedCodeNode = Spread< | ||
{ | ||
language: string | null | undefined; | ||
type: 'code'; | ||
version: 1; | ||
}, | ||
SerializedElementNode | ||
>; | ||
type SerializedCodeHighlightNode = Spread< | ||
{ | ||
highlightType: string | null | undefined; | ||
type: 'code-highlight'; | ||
version: 1; | ||
}, | ||
SerializedTextNode | ||
>; |
@@ -56,2 +56,7 @@ /** | ||
getHighlightType() { | ||
const self = this.getLatest(); | ||
return self.__highlightType; | ||
} | ||
createDOM(config) { | ||
@@ -80,2 +85,18 @@ const element = super.createDOM(config); | ||
return update; | ||
} | ||
static importJSON(serializedNode) { | ||
const node = $createCodeHighlightNode(serializedNode.highlightType); | ||
node.setFormat(serializedNode.format); | ||
node.setDetail(serializedNode.detail); | ||
node.setMode(serializedNode.mode); | ||
node.setStyle(serializedNode.style); | ||
return node; | ||
} | ||
exportJSON() { | ||
return { ...super.exportJSON(), | ||
highlightType: this.getHighlightType(), | ||
type: 'code-highlight' | ||
}; | ||
} // Prevent formatting (bold, underline, etc) | ||
@@ -204,2 +225,17 @@ | ||
}; | ||
} | ||
static importJSON(serializedNode) { | ||
const node = $createCodeNode(serializedNode.language); | ||
node.setFormat(serializedNode.format); | ||
node.setIndent(serializedNode.indent); | ||
node.setDirection(serializedNode.direction); | ||
return node; | ||
} | ||
exportJSON() { | ||
return { ...super.exportJSON(), | ||
language: this.getLanguage(), | ||
type: 'code' | ||
}; | ||
} // Mutation | ||
@@ -324,2 +360,138 @@ | ||
function isSpaceOrTabChar(char) { | ||
return char === ' ' || char === '\t'; | ||
} | ||
function findFirstNotSpaceOrTabCharAtText(text, isForward) { | ||
const length = text.length; | ||
let offset = -1; | ||
if (isForward) { | ||
for (let i = 0; i < length; i++) { | ||
const char = text[i]; | ||
if (!isSpaceOrTabChar(char)) { | ||
offset = i; | ||
break; | ||
} | ||
} | ||
} else { | ||
for (let i = length - 1; i > -1; i--) { | ||
const char = text[i]; | ||
if (!isSpaceOrTabChar(char)) { | ||
offset = i; | ||
break; | ||
} | ||
} | ||
} | ||
return offset; | ||
} | ||
function getStartOfCodeInLine(anchor) { | ||
let currentNode = null; | ||
let currentNodeOffset = -1; | ||
const previousSiblings = anchor.getPreviousSiblings(); | ||
previousSiblings.push(anchor); | ||
while (previousSiblings.length > 0) { | ||
const node = previousSiblings.pop(); | ||
if ($isCodeHighlightNode(node)) { | ||
const text = node.getTextContent(); | ||
const offset = findFirstNotSpaceOrTabCharAtText(text, true); | ||
if (offset !== -1) { | ||
currentNode = node; | ||
currentNodeOffset = offset; | ||
} | ||
} | ||
if (lexical.$isLineBreakNode(node)) { | ||
break; | ||
} | ||
} | ||
if (currentNode === null) { | ||
const nextSiblings = anchor.getNextSiblings(); | ||
while (nextSiblings.length > 0) { | ||
const node = nextSiblings.shift(); | ||
if ($isCodeHighlightNode(node)) { | ||
const text = node.getTextContent(); | ||
const offset = findFirstNotSpaceOrTabCharAtText(text, true); | ||
if (offset !== -1) { | ||
currentNode = node; | ||
currentNodeOffset = offset; | ||
break; | ||
} | ||
} | ||
if (lexical.$isLineBreakNode(node)) { | ||
break; | ||
} | ||
} | ||
} | ||
return { | ||
node: currentNode, | ||
offset: currentNodeOffset | ||
}; | ||
} | ||
function getEndOfCodeInLine(anchor) { | ||
let currentNode = null; | ||
let currentNodeOffset = -1; | ||
const nextSiblings = anchor.getNextSiblings(); | ||
nextSiblings.unshift(anchor); | ||
while (nextSiblings.length > 0) { | ||
const node = nextSiblings.shift(); | ||
if ($isCodeHighlightNode(node)) { | ||
const text = node.getTextContent(); | ||
const offset = findFirstNotSpaceOrTabCharAtText(text, false); | ||
if (offset !== -1) { | ||
currentNode = node; | ||
currentNodeOffset = offset + 1; | ||
} | ||
} | ||
if (lexical.$isLineBreakNode(node)) { | ||
break; | ||
} | ||
} | ||
if (currentNode === null) { | ||
const previousSiblings = anchor.getPreviousSiblings(); | ||
while (previousSiblings.length > 0) { | ||
const node = previousSiblings.pop(); | ||
if ($isCodeHighlightNode(node)) { | ||
const text = node.getTextContent(); | ||
const offset = findFirstNotSpaceOrTabCharAtText(text, false); | ||
if (offset !== -1) { | ||
currentNode = node; | ||
currentNodeOffset = offset + 1; | ||
break; | ||
} | ||
} | ||
if (lexical.$isLineBreakNode(node)) { | ||
break; | ||
} | ||
} | ||
} | ||
return { | ||
node: currentNode, | ||
offset: currentNodeOffset | ||
}; | ||
} | ||
function convertPreElement(domNode) { | ||
@@ -790,2 +962,44 @@ return { | ||
function handleMoveTo(type, event) { | ||
const selection = lexical.$getSelection(); | ||
if (!lexical.$isRangeSelection(selection)) { | ||
return false; | ||
} | ||
const { | ||
anchor, | ||
focus | ||
} = selection; | ||
const anchorNode = anchor.getNode(); | ||
const focusNode = focus.getNode(); | ||
const isMoveToStart = type === lexical.MOVE_TO_START; | ||
if (!$isCodeHighlightNode(anchorNode) || !$isCodeHighlightNode(focusNode)) { | ||
return false; | ||
} | ||
let node; | ||
let offset; | ||
if (isMoveToStart) { | ||
({ | ||
node, | ||
offset | ||
} = getStartOfCodeInLine(focusNode)); | ||
} else { | ||
({ | ||
node, | ||
offset | ||
} = getEndOfCodeInLine(focusNode)); | ||
} | ||
if (node !== null && offset !== -1) { | ||
selection.setTextNodeRange(node, offset, node, offset); | ||
} | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
function registerCodeHighlighting(editor) { | ||
@@ -808,3 +1022,3 @@ if (!editor.hasNodes([CodeNode, CodeHighlightNode])) { | ||
}); | ||
}), editor.registerNodeTransform(CodeNode, node => codeNodeTransform(node, editor)), editor.registerNodeTransform(lexical.TextNode, node => textNodeTransform(node, editor)), editor.registerNodeTransform(CodeHighlightNode, node => textNodeTransform(node, editor)), editor.registerCommand(lexical.INDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.INDENT_CONTENT_COMMAND), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.OUTDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.OUTDENT_CONTENT_COMMAND), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_UP_COMMAND, payload), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_DOWN_COMMAND, payload), lexical.COMMAND_PRIORITY_LOW)); | ||
}), editor.registerNodeTransform(CodeNode, node => codeNodeTransform(node, editor)), editor.registerNodeTransform(lexical.TextNode, node => textNodeTransform(node, editor)), editor.registerNodeTransform(CodeHighlightNode, node => textNodeTransform(node, editor)), editor.registerCommand(lexical.INDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.INDENT_CONTENT_COMMAND), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.OUTDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.OUTDENT_CONTENT_COMMAND), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_UP_COMMAND, payload), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_DOWN_COMMAND, payload), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.MOVE_TO_END, payload => handleMoveTo(lexical.MOVE_TO_END, payload), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.MOVE_TO_START, payload => handleMoveTo(lexical.MOVE_TO_START, payload), lexical.COMMAND_PRIORITY_LOW)); | ||
} | ||
@@ -820,4 +1034,6 @@ | ||
exports.getDefaultCodeLanguage = getDefaultCodeLanguage; | ||
exports.getEndOfCodeInLine = getEndOfCodeInLine; | ||
exports.getFirstCodeHighlightNodeOfLine = getFirstCodeHighlightNodeOfLine; | ||
exports.getLastCodeHighlightNodeOfLine = getLastCodeHighlightNodeOfLine; | ||
exports.getStartOfCodeInLine = getStartOfCodeInLine; | ||
exports.registerCodeHighlighting = registerCodeHighlighting; |
@@ -7,23 +7,26 @@ /** | ||
*/ | ||
var e=require("prismjs");require("prismjs/components/prism-clike");require("prismjs/components/prism-javascript");require("prismjs/components/prism-markup");require("prismjs/components/prism-markdown");require("prismjs/components/prism-c");require("prismjs/components/prism-css");require("prismjs/components/prism-objectivec");require("prismjs/components/prism-sql");require("prismjs/components/prism-python");require("prismjs/components/prism-rust");require("prismjs/components/prism-swift"); | ||
var n=require("@lexical/utils"),r=require("lexical");const t=a=>null!=a&&e.languages.hasOwnProperty(a)?a:void 0; | ||
class u extends r.TextNode{constructor(a,c,b){super(a,b);this.__highlightType=c}static getType(){return"code-highlight"}static clone(a){return new u(a.__text,a.__highlightType||void 0,a.__key)}createDOM(a){const c=super.createDOM(a);a=v(a.theme,this.__highlightType);n.addClassNamesToElement(c,a);return c}updateDOM(a,c,b){const d=super.updateDOM(a,c,b);a=v(b.theme,a.__highlightType);b=v(b.theme,this.__highlightType);a!==b&&(a&&n.removeClassNamesFromElement(c,a),b&&n.addClassNamesToElement(c,b));return d}setFormat(){return this}} | ||
function v(a,c){return c&&a&&a.codeHighlight&&a.codeHighlight[c]}function y(a,c){return new u(a,c)}function z(a){return a instanceof u} | ||
class B extends r.ElementNode{static getType(){return"code"}static clone(a){return new B(a.__language,a.__key)}constructor(a,c){super(c);this.__language=t(a)}createDOM(a){const c=document.createElement("code");n.addClassNamesToElement(c,a.theme.code);c.setAttribute("spellcheck","false");(a=this.getLanguage())&&c.setAttribute("data-highlight-language",a);return c}updateDOM(a,c){const b=this.__language;a=a.__language;b?b!==a&&c.setAttribute("data-highlight-language",b):a&&c.removeAttribute("data-highlight-language"); | ||
return!1}static importDOM(){return{div:()=>({conversion:C,priority:1}),pre:()=>({conversion:D,priority:0}),table:a=>E(a)?{conversion:F,priority:4}:null,td:a=>{const c=a.closest("table");return a.classList.contains("js-file-line")?{conversion:G,priority:4}:c&&E(c)?{conversion:H,priority:4}:null},tr:a=>(a=a.closest("table"))&&E(a)?{conversion:H,priority:4}:null}}insertNewAfter(a){var c=this.getChildren(),b=c.length;if(2<=b&&"\n"===c[b-1].getTextContent()&&"\n"===c[b-2].getTextContent()&&a.isCollapsed()&& | ||
a.anchor.key===this.__key&&a.anchor.offset===b)return c[b-1].remove(),c[b-2].remove(),a=r.$createParagraphNode(),this.insertAfter(a),a;c=a.anchor.getNode();var d=I(c);if(null!=d){b=0;for(d=d.getTextContent();b<d.length&&/[\t ]/.test(d[b]);)b+=1;if(0<b)return b=d.substring(0,b),b=y(b),c.insertAfter(b),a.insertNodes([r.$createLineBreakNode()]),b.select(),b}return null}canInsertTab(){const a=r.$getSelection();return r.$isRangeSelection(a)&&a.isCollapsed()?!0:!1}canIndent(){return!1}collapseAtStart(){const a= | ||
r.$createParagraphNode();this.getChildren().forEach(c=>a.append(c));this.replace(a);return!0}setLanguage(a){this.getWritable().__language=t(a)}getLanguage(){return this.getLatest().__language}}function J(a){return new B(a)}function K(a){return a instanceof B}function I(a){let c=null;const b=a.getPreviousSiblings();for(b.push(a);0<b.length&&(a=b.pop(),z(a)&&(c=a),!r.$isLineBreakNode(a)););return c} | ||
function L(a){let c=null;const b=a.getNextSiblings();for(b.unshift(a);0<b.length&&(a=b.shift(),z(a)&&(c=a),!r.$isLineBreakNode(a)););return c}function D(){return{node:J()}}function C(a){return{after:c=>{const b=a.parentNode;null!=b&&a!==b.lastChild&&c.push(r.$createLineBreakNode());return c},node:null!==a.style.fontFamily.match("monospace")?J():null}}function F(){return{node:J()}}function H(){return{node:null}} | ||
function G(a){return{after:c=>{a.parentNode&&a.parentNode.nextSibling&&c.push(r.$createLineBreakNode());return c},node:null}}function E(a){return a.classList.contains("js-file-line-container")}function M(a,c){const b=a.getParent();K(b)?N(b,c):z(a)&&a.replace(r.$createTextNode(a.__text))}let O=!1; | ||
function N(a,c){O||(O=!0,void 0===a.getLanguage()&&a.setLanguage("javascript"),c.update(()=>{P(a,()=>{var b=a.getTextContent();b=e.tokenize(b,e.languages[a.getLanguage()||""]||e.languages.javascript);b=R(b);var d=a.getChildren();let f=0;for(;f<d.length&&S(d[f],b[f]);)f++;var h=d.length;const m=b.length,g=Math.min(h,m)-f;let k=0;for(;k<g;)if(k++,!S(d[h-k],b[m-k])){k--;break}d=f;h-=k;b=b.slice(f,m-k);const {from:l,to:p,nodesForReplacement:w}={from:d,nodesForReplacement:b,to:h};return l!==p||w.length? | ||
(a.splice(l,p-l,w),!0):!1})},{onUpdate:()=>{O=!1},skipTransforms:!0}))}function R(a){const c=[];a.forEach(b=>{if("string"===typeof b){b=b.split("\n");for(var d=0;d<b.length;d++){const f=b[d];f.length&&c.push(y(f));d<b.length-1&&c.push(r.$createLineBreakNode())}}else({content:d}=b),"string"===typeof d?c.push(y(d,b.type)):Array.isArray(d)&&1===d.length&&"string"===typeof d[0]?c.push(y(d[0],b.type)):Array.isArray(d)&&c.push(...R(d))});return c} | ||
function P(a,c){var b=r.$getSelection();if(r.$isRangeSelection(b)&&b.anchor){b=b.anchor;var d=b.offset,f="element"===b.type&&r.$isLineBreakNode(a.getChildAtIndex(b.offset-1)),h=0;if(!f){const m=b.getNode();h=d+m.getPreviousSiblings().reduce((g,k)=>g+(r.$isLineBreakNode(k)?0:k.getTextContentSize()),0)}c()&&(f?b.getNode().select(d,d):a.getChildren().some(m=>{if(r.$isTextNode(m)){const g=m.getTextContentSize();if(g>=h)return m.select(h,h),!0;h-=g}return!1}))}} | ||
function S(a,c){return z(a)&&z(c)?a.__text===c.__text&&a.__highlightType===c.__highlightType:r.$isLineBreakNode(a)&&r.$isLineBreakNode(c)?!0:!1}function T(a){var c=r.$getSelection();if(!r.$isRangeSelection(c)||c.isCollapsed())return!1;c=c.getNodes();for(var b=0;b<c.length;b++){var d=c[b];if(!z(d)&&!r.$isLineBreakNode(d))return!1}b=I(c[0]);null!=b&&U(b,a);for(b=1;b<c.length;b++)d=c[b],r.$isLineBreakNode(c[b-1])&&z(d)&&U(d,a);return!0} | ||
function U(a,c){const b=a.getTextContent();c===r.INDENT_CONTENT_COMMAND?0<b.length&&/\s/.test(b[0])?a.setTextContent("\t"+b):(c=y("\t"),a.insertBefore(c)):0===b.indexOf("\t")&&(1===b.length?a.remove():a.setTextContent(b.substring(1)))} | ||
function V(a,c){const b=r.$getSelection();if(!r.$isRangeSelection(b))return!1;const {anchor:d,focus:f}=b,h=d.offset,m=f.offset,g=d.getNode(),k=f.getNode();var l=a===r.KEY_ARROW_UP_COMMAND;if(!z(g)||!z(k))return!1;if(!c.altKey){if(b.isCollapsed())if(a=g.getParentOrThrow(),l&&0===h&&null===g.getPreviousSibling()){if(null===a.getPreviousSibling())return a.selectPrevious(),c.preventDefault(),!0}else if(!l&&h===g.getTextContentSize()&&null===g.getNextSibling()&&null===a.getNextSibling())return a.selectNext(), | ||
c.preventDefault(),!0;return!1}var p=I(g);const w=L(k);if(null==p||null==w)return!1;const A=p.getNodesBetween(w);for(let q=0;q<A.length;q++){const Q=A[q];if(!z(Q)&&!r.$isLineBreakNode(Q))return!1}c.preventDefault();c.stopPropagation();c=l?p.getPreviousSibling():w.getNextSibling();if(!r.$isLineBreakNode(c))return!0;p=l?c.getPreviousSibling():c.getNextSibling();if(null==p)return!0;l=l?I(p):L(p);let x=null!=l?l:p;c.remove();A.forEach(q=>q.remove());a===r.KEY_ARROW_UP_COMMAND?(A.forEach(q=>x.insertBefore(q)), | ||
x.insertBefore(c)):(x.insertAfter(c),x=c,A.forEach(q=>{x.insertAfter(q);x=q}));b.setTextNodeRange(g,h,k,m);return!0}exports.$createCodeHighlightNode=y;exports.$createCodeNode=J;exports.$isCodeHighlightNode=z;exports.$isCodeNode=K;exports.CodeHighlightNode=u;exports.CodeNode=B;exports.getCodeLanguages=()=>Object.keys(e.languages).filter(a=>"function"!==typeof e.languages[a]).sort();exports.getDefaultCodeLanguage=()=>"javascript";exports.getFirstCodeHighlightNodeOfLine=I; | ||
exports.getLastCodeHighlightNodeOfLine=L; | ||
exports.registerCodeHighlighting=function(a){if(!a.hasNodes([B,u]))throw Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return n.mergeRegister(a.registerMutationListener(B,c=>{a.update(()=>{for(const [f,h]of c)if("destroyed"!==h){var b=r.$getNodeByKey(f);if(null!==b)a:{var d=b;b=a.getElementByKey(d.getKey());if(null===b)break a;d=d.getChildren();const m=d.length;if(m===b.__cachedChildrenLength)break a;b.__cachedChildrenLength=m;let g="1",k=1;for(let l=0;l<m;l++)r.$isLineBreakNode(d[l])&& | ||
(g+="\n"+ ++k);b.setAttribute("data-gutter",g)}}})}),a.registerNodeTransform(B,c=>N(c,a)),a.registerNodeTransform(r.TextNode,c=>M(c,a)),a.registerNodeTransform(u,c=>M(c,a)),a.registerCommand(r.INDENT_CONTENT_COMMAND,()=>T(r.INDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.OUTDENT_CONTENT_COMMAND,()=>T(r.OUTDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_UP_COMMAND,c=>V(r.KEY_ARROW_UP_COMMAND,c),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_DOWN_COMMAND, | ||
c=>V(r.KEY_ARROW_DOWN_COMMAND,c),r.COMMAND_PRIORITY_LOW))}; | ||
'use strict';var e=require("prismjs");require("prismjs/components/prism-clike");require("prismjs/components/prism-javascript");require("prismjs/components/prism-markup");require("prismjs/components/prism-markdown");require("prismjs/components/prism-c");require("prismjs/components/prism-css");require("prismjs/components/prism-objectivec");require("prismjs/components/prism-sql");require("prismjs/components/prism-python");require("prismjs/components/prism-rust");require("prismjs/components/prism-swift"); | ||
var m=require("@lexical/utils"),r=require("lexical");let t=a=>null!=a&&e.languages.hasOwnProperty(a)?a:void 0; | ||
class u extends r.TextNode{constructor(a,b,c){super(a,c);this.__highlightType=b}static getType(){return"code-highlight"}static clone(a){return new u(a.__text,a.__highlightType||void 0,a.__key)}getHighlightType(){return this.getLatest().__highlightType}createDOM(a){let b=super.createDOM(a);a=v(a.theme,this.__highlightType);m.addClassNamesToElement(b,a);return b}updateDOM(a,b,c){let d=super.updateDOM(a,b,c);a=v(c.theme,a.__highlightType);c=v(c.theme,this.__highlightType);a!==c&&(a&&m.removeClassNamesFromElement(b, | ||
a),c&&m.addClassNamesToElement(b,c));return d}static importJSON(a){let b=y(a.highlightType);b.setFormat(a.format);b.setDetail(a.detail);b.setMode(a.mode);b.setStyle(a.style);return b}exportJSON(){return{...super.exportJSON(),highlightType:this.getHighlightType(),type:"code-highlight"}}setFormat(){return this}}function v(a,b){return b&&a&&a.codeHighlight&&a.codeHighlight[b]}function y(a,b){return new u(a,b)}function z(a){return a instanceof u} | ||
class A extends r.ElementNode{static getType(){return"code"}static clone(a){return new A(a.__language,a.__key)}constructor(a,b){super(b);this.__language=t(a)}createDOM(a){let b=document.createElement("code");m.addClassNamesToElement(b,a.theme.code);b.setAttribute("spellcheck","false");(a=this.getLanguage())&&b.setAttribute("data-highlight-language",a);return b}updateDOM(a,b){let c=this.__language;a=a.__language;c?c!==a&&b.setAttribute("data-highlight-language",c):a&&b.removeAttribute("data-highlight-language"); | ||
return!1}static importDOM(){return{div:()=>({conversion:C,priority:1}),pre:()=>({conversion:D,priority:0}),table:a=>E(a)?{conversion:F,priority:4}:null,td:a=>{let b=a.closest("table");return a.classList.contains("js-file-line")?{conversion:G,priority:4}:b&&E(b)?{conversion:H,priority:4}:null},tr:a=>(a=a.closest("table"))&&E(a)?{conversion:H,priority:4}:null}}static importJSON(a){let b=I(a.language);b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);return b}exportJSON(){return{...super.exportJSON(), | ||
language:this.getLanguage(),type:"code"}}insertNewAfter(a){var b=this.getChildren(),c=b.length;if(2<=c&&"\n"===b[c-1].getTextContent()&&"\n"===b[c-2].getTextContent()&&a.isCollapsed()&&a.anchor.key===this.__key&&a.anchor.offset===c)return b[c-1].remove(),b[c-2].remove(),a=r.$createParagraphNode(),this.insertAfter(a),a;b=a.anchor.getNode();var d=J(b);if(null!=d){c=0;for(d=d.getTextContent();c<d.length&&/[\t ]/.test(d[c]);)c+=1;if(0<c)return c=d.substring(0,c),c=y(c),b.insertAfter(c),a.insertNodes([r.$createLineBreakNode()]), | ||
c.select(),c}return null}canInsertTab(){let a=r.$getSelection();return r.$isRangeSelection(a)&&a.isCollapsed()?!0:!1}canIndent(){return!1}collapseAtStart(){let a=r.$createParagraphNode();this.getChildren().forEach(b=>a.append(b));this.replace(a);return!0}setLanguage(a){this.getWritable().__language=t(a)}getLanguage(){return this.getLatest().__language}}function I(a){return new A(a)}function K(a){return a instanceof A} | ||
function J(a){let b=null,c=a.getPreviousSiblings();for(c.push(a);0<c.length&&(a=c.pop(),z(a)&&(b=a),!r.$isLineBreakNode(a)););return b}function L(a){let b=null,c=a.getNextSiblings();for(c.unshift(a);0<c.length&&(a=c.shift(),z(a)&&(b=a),!r.$isLineBreakNode(a)););return b}function M(a,b){var c=a.length;let d=-1;if(b)for(b=0;b<c;b++){let f=a[b];if(" "!==f&&"\t"!==f){d=b;break}}else for(--c;-1<c;c--)if(b=a[c]," "!==b&&"\t"!==b){d=c;break}return d} | ||
function N(a){let b=null,c=-1;var d=a.getPreviousSiblings();for(d.push(a);0<d.length;){var f=d.pop();if(z(f)){var g=f.getTextContent();g=M(g,!0);-1!==g&&(b=f,c=g)}if(r.$isLineBreakNode(f))break}if(null===b)for(a=a.getNextSiblings();0<a.length;){d=a.shift();if(z(d)&&(f=d.getTextContent(),f=M(f,!0),-1!==f)){b=d;c=f;break}if(r.$isLineBreakNode(d))break}return{node:b,offset:c}} | ||
function O(a){let b=null,c=-1;var d=a.getNextSiblings();for(d.unshift(a);0<d.length;){var f=d.shift();if(z(f)){var g=f.getTextContent();g=M(g,!1);-1!==g&&(b=f,c=g+1)}if(r.$isLineBreakNode(f))break}if(null===b)for(a=a.getPreviousSiblings();0<a.length;){d=a.pop();if(z(d)&&(f=d.getTextContent(),f=M(f,!1),-1!==f)){b=d;c=f+1;break}if(r.$isLineBreakNode(d))break}return{node:b,offset:c}}function D(){return{node:I()}} | ||
function C(a){return{after:b=>{let c=a.parentNode;null!=c&&a!==c.lastChild&&b.push(r.$createLineBreakNode());return b},node:null!==a.style.fontFamily.match("monospace")?I():null}}function F(){return{node:I()}}function H(){return{node:null}}function G(a){return{after:b=>{a.parentNode&&a.parentNode.nextSibling&&b.push(r.$createLineBreakNode());return b},node:null}}function E(a){return a.classList.contains("js-file-line-container")} | ||
function P(a,b){let c=a.getParent();K(c)?Q(c,b):z(a)&&a.replace(r.$createTextNode(a.__text))}let R=!1; | ||
function Q(a,b){R||(R=!0,void 0===a.getLanguage()&&a.setLanguage("javascript"),b.update(()=>{S(a,()=>{var c=a.getTextContent();c=e.tokenize(c,e.languages[a.getLanguage()||""]||e.languages.javascript);c=U(c);var d=a.getChildren();let f=0;for(;f<d.length&&V(d[f],c[f]);)f++;var g=d.length;let l=c.length,h=Math.min(g,l)-f,k=0;for(;k<h;)if(k++,!V(d[g-k],c[l-k])){k--;break}d=f;g-=k;c=c.slice(f,l-k);let {from:n,to:p,nodesForReplacement:w}={from:d,nodesForReplacement:c,to:g};return n!==p||w.length?(a.splice(n, | ||
p-n,w),!0):!1})},{onUpdate:()=>{R=!1},skipTransforms:!0}))}function U(a){let b=[];a.forEach(c=>{if("string"===typeof c){c=c.split("\n");for(var d=0;d<c.length;d++){let f=c[d];f.length&&b.push(y(f));d<c.length-1&&b.push(r.$createLineBreakNode())}}else({content:d}=c),"string"===typeof d?b.push(y(d,c.type)):Array.isArray(d)&&1===d.length&&"string"===typeof d[0]?b.push(y(d[0],c.type)):Array.isArray(d)&&b.push(...U(d))});return b} | ||
function S(a,b){var c=r.$getSelection();if(r.$isRangeSelection(c)&&c.anchor){c=c.anchor;var d=c.offset,f="element"===c.type&&r.$isLineBreakNode(a.getChildAtIndex(c.offset-1)),g=0;if(!f){let l=c.getNode();g=d+l.getPreviousSiblings().reduce((h,k)=>h+(r.$isLineBreakNode(k)?0:k.getTextContentSize()),0)}b()&&(f?c.getNode().select(d,d):a.getChildren().some(l=>{if(r.$isTextNode(l)){let h=l.getTextContentSize();if(h>=g)return l.select(g,g),!0;g-=h}return!1}))}} | ||
function V(a,b){return z(a)&&z(b)?a.__text===b.__text&&a.__highlightType===b.__highlightType:r.$isLineBreakNode(a)&&r.$isLineBreakNode(b)?!0:!1}function W(a){var b=r.$getSelection();if(!r.$isRangeSelection(b)||b.isCollapsed())return!1;b=b.getNodes();for(var c=0;c<b.length;c++){var d=b[c];if(!z(d)&&!r.$isLineBreakNode(d))return!1}c=J(b[0]);null!=c&&X(c,a);for(c=1;c<b.length;c++)d=b[c],r.$isLineBreakNode(b[c-1])&&z(d)&&X(d,a);return!0} | ||
function X(a,b){let c=a.getTextContent();b===r.INDENT_CONTENT_COMMAND?0<c.length&&/\s/.test(c[0])?a.setTextContent("\t"+c):(b=y("\t"),a.insertBefore(b)):0===c.indexOf("\t")&&(1===c.length?a.remove():a.setTextContent(c.substring(1)))} | ||
function Y(a,b){let c=r.$getSelection();if(!r.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.offset,l=f.offset,h=d.getNode(),k=f.getNode();var n=a===r.KEY_ARROW_UP_COMMAND;if(!z(h)||!z(k))return!1;if(!b.altKey){if(c.isCollapsed())if(a=h.getParentOrThrow(),n&&0===g&&null===h.getPreviousSibling()){if(null===a.getPreviousSibling())return a.selectPrevious(),b.preventDefault(),!0}else if(!n&&g===h.getTextContentSize()&&null===h.getNextSibling()&&null===a.getNextSibling())return a.selectNext(), | ||
b.preventDefault(),!0;return!1}var p=J(h);let w=L(k);if(null==p||null==w)return!1;let B=p.getNodesBetween(w);for(let q=0;q<B.length;q++){let T=B[q];if(!z(T)&&!r.$isLineBreakNode(T))return!1}b.preventDefault();b.stopPropagation();b=n?p.getPreviousSibling():w.getNextSibling();if(!r.$isLineBreakNode(b))return!0;p=n?b.getPreviousSibling():b.getNextSibling();if(null==p)return!0;n=n?J(p):L(p);let x=null!=n?n:p;b.remove();B.forEach(q=>q.remove());a===r.KEY_ARROW_UP_COMMAND?(B.forEach(q=>x.insertBefore(q)), | ||
x.insertBefore(b)):(x.insertAfter(b),x=b,B.forEach(q=>{x.insertAfter(q);x=q}));c.setTextNodeRange(h,g,k,l);return!0}function Z(a,b){let c=r.$getSelection();if(!r.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.getNode(),l=f.getNode();a=a===r.MOVE_TO_START;if(!z(g)||!z(l))return!1;let h,k;a?{node:h,offset:k}=N(l):{node:h,offset:k}=O(l);null!==h&&-1!==k&&c.setTextNodeRange(h,k,h,k);b.preventDefault();b.stopPropagation()}exports.$createCodeHighlightNode=y;exports.$createCodeNode=I; | ||
exports.$isCodeHighlightNode=z;exports.$isCodeNode=K;exports.CodeHighlightNode=u;exports.CodeNode=A;exports.getCodeLanguages=()=>Object.keys(e.languages).filter(a=>"function"!==typeof e.languages[a]).sort();exports.getDefaultCodeLanguage=()=>"javascript";exports.getEndOfCodeInLine=O;exports.getFirstCodeHighlightNodeOfLine=J;exports.getLastCodeHighlightNodeOfLine=L;exports.getStartOfCodeInLine=N; | ||
exports.registerCodeHighlighting=function(a){if(!a.hasNodes([A,u]))throw Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return m.mergeRegister(a.registerMutationListener(A,b=>{a.update(()=>{for(let [f,g]of b)if("destroyed"!==g){var c=r.$getNodeByKey(f);if(null!==c)a:{var d=c;c=a.getElementByKey(d.getKey());if(null===c)break a;d=d.getChildren();let l=d.length;if(l===c.__cachedChildrenLength)break a;c.__cachedChildrenLength=l;let h="1",k=1;for(let n=0;n<l;n++)r.$isLineBreakNode(d[n])&& | ||
(h+="\n"+ ++k);c.setAttribute("data-gutter",h)}}})}),a.registerNodeTransform(A,b=>Q(b,a)),a.registerNodeTransform(r.TextNode,b=>P(b,a)),a.registerNodeTransform(u,b=>P(b,a)),a.registerCommand(r.INDENT_CONTENT_COMMAND,()=>W(r.INDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.OUTDENT_CONTENT_COMMAND,()=>W(r.OUTDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_UP_COMMAND,b=>Y(r.KEY_ARROW_UP_COMMAND,b),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_DOWN_COMMAND, | ||
b=>Y(r.KEY_ARROW_DOWN_COMMAND,b),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.MOVE_TO_END,b=>Z(r.MOVE_TO_END,b),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.MOVE_TO_START,b=>Z(r.MOVE_TO_START,b),r.COMMAND_PRIORITY_LOW))} |
@@ -11,9 +11,9 @@ { | ||
"license": "MIT", | ||
"version": "0.2.9", | ||
"version": "0.3.0", | ||
"main": "LexicalCode.js", | ||
"peerDependencies": { | ||
"lexical": "0.2.9" | ||
"lexical": "0.3.0" | ||
}, | ||
"dependencies": { | ||
"@lexical/utils": "0.2.9", | ||
"@lexical/utils": "0.3.0", | ||
"prismjs": "^1.27.0" | ||
@@ -20,0 +20,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
47406
987
0
+ Added@lexical/list@0.3.0(transitive)
+ Added@lexical/table@0.3.0(transitive)
+ Added@lexical/utils@0.3.0(transitive)
+ Addedlexical@0.3.0(transitive)
- Removed@lexical/list@0.2.9(transitive)
- Removed@lexical/table@0.2.9(transitive)
- Removed@lexical/utils@0.2.9(transitive)
- Removedlexical@0.2.9(transitive)
Updated@lexical/utils@0.3.0