@lexical/code
Advanced tools
Comparing version 0.6.5 to 0.7.0
@@ -28,2 +28,3 @@ /** | ||
export interface Tokenizer { | ||
defaultLanguage: string; | ||
tokenize(code: string, language?: string): (string | Token)[]; | ||
@@ -30,0 +31,0 @@ } |
@@ -40,3 +40,3 @@ /** | ||
exportJSON(): SerializedCodeNode; | ||
insertNewAfter(selection: RangeSelection): null | ParagraphNode | CodeHighlightNode; | ||
insertNewAfter(selection: RangeSelection, restoreSelection?: boolean): null | ParagraphNode | CodeHighlightNode; | ||
canInsertTab(): boolean; | ||
@@ -43,0 +43,0 @@ canIndent(): false; |
@@ -350,3 +350,3 @@ /** | ||
insertNewAfter(selection) { | ||
insertNewAfter(selection, restoreSelection = true) { | ||
const children = this.getChildren(); | ||
@@ -359,3 +359,3 @@ const childrenLength = children.length; | ||
const newElement = lexical.$createParagraphNode(); | ||
this.insertAfter(newElement); | ||
this.insertAfter(newElement, restoreSelection); | ||
return newElement; | ||
@@ -526,4 +526,6 @@ } // If the selection is within the codeblock, find all leading tabs and | ||
const PrismTokenizer = { | ||
defaultLanguage: DEFAULT_CODE_LANGUAGE, | ||
tokenize(code, language) { | ||
return Prism.tokenize(code, Prism.languages[language || ''] || Prism.languages[DEFAULT_CODE_LANGUAGE]); | ||
return Prism.tokenize(code, Prism.languages[language || ''] || Prism.languages[this.defaultLanguage]); | ||
} | ||
@@ -731,3 +733,3 @@ | ||
if (node.getLanguage() === undefined) { | ||
node.setLanguage(DEFAULT_CODE_LANGUAGE); | ||
node.setLanguage(tokenizer.defaultLanguage); | ||
} // Using nested update call to pass `skipTransforms` since we don't want | ||
@@ -747,3 +749,3 @@ // each individual codehighlight node to be transformed again as it's already | ||
const code = currentNode.getTextContent(); | ||
const tokens = tokenizer.tokenize(code, currentNode.getLanguage() || DEFAULT_CODE_LANGUAGE); | ||
const tokens = tokenizer.tokenize(code, currentNode.getLanguage() || tokenizer.defaultLanguage); | ||
const highlightNodes = getHighlightNodes(tokens); | ||
@@ -750,0 +752,0 @@ const diffRange = getDiffRange(currentNode.getChildren(), highlightNodes); |
@@ -14,11 +14,12 @@ /** | ||
return!1}static importDOM(){return{code:a=>null!=a.textContent&&(/\r?\n/.test(a.textContent)||G(a,"BR"))?{conversion:I,priority:1}:null,div:()=>({conversion:aa,priority:1}),pre:()=>({conversion:I,priority:0}),table:a=>J(a)?{conversion:ba,priority:4}:null,td:a=>{let b=a.closest("table");return a.classList.contains("js-file-line")?{conversion:ca,priority:4}:b&&J(b)?{conversion:K,priority:4}:null},tr:a=>(a=a.closest("table"))&&J(a)?{conversion:K,priority:4}:null}}static importJSON(a){let b=L(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",version:1}}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=u.$createParagraphNode(),this.insertAfter(a),a;b=a.anchor.getNode();var d=D(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=B(c),b.insertAfter(c),a.insertNodes([u.$createLineBreakNode()]),c.select(),c}return null}canInsertTab(){let a=u.$getSelection();return u.$isRangeSelection(a)&&a.isCollapsed()?!0:!1}canIndent(){return!1}collapseAtStart(){let a=u.$createParagraphNode();this.getChildren().forEach(b=>a.append(b));this.replace(a);return!0}setLanguage(a){this.getWritable().__language=F(a)}getLanguage(){return this.getLatest().__language}} | ||
b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);return b}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),type:"code",version:1}}insertNewAfter(a,b=!0){var c=this.getChildren(),d=c.length;if(2<=d&&"\n"===c[d-1].getTextContent()&&"\n"===c[d-2].getTextContent()&&a.isCollapsed()&&a.anchor.key===this.__key&&a.anchor.offset===d)return c[d-1].remove(),c[d-2].remove(),a=u.$createParagraphNode(),this.insertAfter(a,b),a;b=a.anchor.getNode();d=D(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=B(c),b.insertAfter(c),a.insertNodes([u.$createLineBreakNode()]),c.select(),c}return null}canInsertTab(){let a=u.$getSelection();return u.$isRangeSelection(a)&&a.isCollapsed()?!0:!1}canIndent(){return!1}collapseAtStart(){let a=u.$createParagraphNode();this.getChildren().forEach(b=>a.append(b));this.replace(a);return!0}setLanguage(a){this.getWritable().__language=F(a)}getLanguage(){return this.getLatest().__language}} | ||
function L(a){return u.$applyNodeReplacement(new H(a))}function M(a){return a instanceof H}function I(){return{node:L(),preformatted:!0}}function aa(a){let b=null!==a.style.fontFamily.match("monospace");return b||da(a)?{after:c=>{let d=a.parentNode;null!=d&&a!==d.lastChild&&c.push(u.$createLineBreakNode());return c},node:b?L():null,preformatted:b}:{node:null}}function ba(){return{node:L(),preformatted:!0}}function K(){return{node:null}} | ||
function ca(a){return{after:b=>{a.parentNode&&a.parentNode.nextSibling&&b.push(u.$createLineBreakNode());return b},node:null}}function da(a){for(a=a.parentElement;null!==a;){if(null!==a.style.fontFamily.match("monospace"))return!0;a=a.parentElement}return!1}function J(a){return a.classList.contains("js-file-line-container")}let N={tokenize(a,b){return e.tokenize(a,e.languages[b||""]||e.languages.javascript)}}; | ||
function ca(a){return{after:b=>{a.parentNode&&a.parentNode.nextSibling&&b.push(u.$createLineBreakNode());return b},node:null}}function da(a){for(a=a.parentElement;null!==a;){if(null!==a.style.fontFamily.match("monospace"))return!0;a=a.parentElement}return!1}function J(a){return a.classList.contains("js-file-line-container")}let N={defaultLanguage:"javascript",tokenize(a,b){return e.tokenize(a,e.languages[b||""]||e.languages[this.defaultLanguage])}}; | ||
function O(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 P(a){let b=null,c=-1;var d=a.getPreviousSiblings();for(d.push(a);0<d.length;){var f=d.pop();if(C(f)){var g=f.getTextContent();g=O(g,!0);-1!==g&&(b=f,c=g)}if(u.$isLineBreakNode(f))break}if(null===b)for(a=a.getNextSiblings();0<a.length;){d=a.shift();if(C(d)&&(f=d.getTextContent(),f=O(f,!0),-1!==f)){b=d;c=f;break}if(u.$isLineBreakNode(d))break}return{node:b,offset:c}} | ||
function Q(a){let b=null,c=-1;var d=a.getNextSiblings();for(d.unshift(a);0<d.length;){var f=d.shift();if(C(f)){var g=f.getTextContent();g=O(g,!1);-1!==g&&(b=f,c=g+1)}if(u.$isLineBreakNode(f))break}if(null===b)for(a=a.getPreviousSiblings();0<a.length;){d=a.pop();if(C(d)&&(f=d.getTextContent(),f=O(f,!1),-1!==f)){b=d;c=f+1;break}if(u.$isLineBreakNode(d))break}return{node:b,offset:c}}function R(a,b,c){let d=a.getParent();M(d)?S(d,b,c):C(a)&&a.replace(u.$createTextNode(a.__text))}let T=new Set; | ||
function S(a,b,c){let d=a.getKey();T.has(d)||(T.add(d),void 0===a.getLanguage()&&a.setLanguage("javascript"),b.update(()=>{ea(d,()=>{var f=u.$getNodeByKey(d);if(!M(f)||!f.isAttached())return!1;var g=f.getTextContent();g=c.tokenize(g,f.getLanguage()||"javascript");g=U(g);var k=f.getChildren();for(f=0;f<k.length&&V(k[f],g[f]);)f++;var h=k.length;let l=g.length,p=Math.min(h,l)-f,m=0;for(;m<p;)if(m++,!V(k[h-m],g[l-m])){m--;break}k=f;h-=m;g=g.slice(f,l-m);let {from:w,to:t,nodesForReplacement:r}={from:k, | ||
nodesForReplacement:g,to:h};return w!==t||r.length?(a.splice(w,t-w,r),!0):!1})},{onUpdate:()=>{T.delete(d)},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(B(f));d<c.length-1&&b.push(u.$createLineBreakNode())}}else({content:d}=c),"string"===typeof d?b.push(B(d,c.type)):Array.isArray(d)&&1===d.length&&"string"===typeof d[0]?b.push(B(d[0],c.type)):Array.isArray(d)&&b.push(...U(d))});return b} | ||
function S(a,b,c){let d=a.getKey();T.has(d)||(T.add(d),void 0===a.getLanguage()&&a.setLanguage(c.defaultLanguage),b.update(()=>{ea(d,()=>{var f=u.$getNodeByKey(d);if(!M(f)||!f.isAttached())return!1;var g=f.getTextContent();g=c.tokenize(g,f.getLanguage()||c.defaultLanguage);g=U(g);var k=f.getChildren();for(f=0;f<k.length&&V(k[f],g[f]);)f++;var h=k.length;let l=g.length,p=Math.min(h,l)-f,m=0;for(;m<p;)if(m++,!V(k[h-m],g[l-m])){m--;break}k=f;h-=m;g=g.slice(f,l-m);let {from:w,to:t,nodesForReplacement:r}= | ||
{from:k,nodesForReplacement:g,to:h};return w!==t||r.length?(a.splice(w,t-w,r),!0):!1})},{onUpdate:()=>{T.delete(d)},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(B(f));d<c.length-1&&b.push(u.$createLineBreakNode())}}else({content:d}=c),"string"===typeof d?b.push(B(d,c.type)):Array.isArray(d)&&1===d.length&&"string"===typeof d[0]?b.push(B(d[0],c.type)):Array.isArray(d)&&b.push(...U(d))});return b} | ||
function ea(a,b){a=u.$getNodeByKey(a);if(M(a)&&a.isAttached()){var c=u.$getSelection();if(u.$isRangeSelection(c)){c=c.anchor;var d=c.offset,f="element"===c.type&&u.$isLineBreakNode(a.getChildAtIndex(c.offset-1)),g=0;if(!f){let k=c.getNode();g=d+k.getPreviousSiblings().reduce((h,l)=>h+l.getTextContentSize(),0)}b()&&(f?c.getNode().select(d,d):a.getChildren().some(k=>{let h=u.$isTextNode(k);if(h||u.$isLineBreakNode(k)){let l=k.getTextContentSize();if(h&&l>=g)return k.select(g,g),!0;g-=l}return!1}))}else b()}} | ||
@@ -25,0 +26,0 @@ function V(a,b){return C(a)&&C(b)?a.__text===b.__text&&a.__highlightType===b.__highlightType:u.$isLineBreakNode(a)&&u.$isLineBreakNode(b)?!0:!1}function W(a){var b=u.$getSelection();if(!u.$isRangeSelection(b)||b.isCollapsed())return!1;b=b.getNodes();for(var c=0;c<b.length;c++){var d=b[c];if(!C(d)&&!u.$isLineBreakNode(d))return!1}c=D(b[0]);null!=c&&X(c,a);for(c=1;c<b.length;c++)d=b[c],u.$isLineBreakNode(b[c-1])&&C(d)&&X(d,a);return!0} |
@@ -11,9 +11,9 @@ { | ||
"license": "MIT", | ||
"version": "0.6.5", | ||
"version": "0.7.0", | ||
"main": "LexicalCode.js", | ||
"peerDependencies": { | ||
"lexical": "0.6.5" | ||
"lexical": "0.7.0" | ||
}, | ||
"dependencies": { | ||
"@lexical/utils": "0.6.5", | ||
"@lexical/utils": "0.7.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
57147
1174
+ Added@lexical/list@0.7.0(transitive)
+ Added@lexical/table@0.7.0(transitive)
+ Added@lexical/utils@0.7.0(transitive)
+ Addedlexical@0.7.0(transitive)
- Removed@lexical/list@0.6.5(transitive)
- Removed@lexical/table@0.6.5(transitive)
- Removed@lexical/utils@0.6.5(transitive)
- Removedlexical@0.6.5(transitive)
Updated@lexical/utils@0.7.0