Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lexical/code

Package Overview
Dependencies
Maintainers
6
Versions
221
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lexical/code - npm Package Compare versions

Comparing version 0.7.6 to 0.7.7

4

CodeHighlightNode.d.ts

@@ -8,3 +8,3 @@ /**

*/
import { type EditorConfig, type LexicalNode, type NodeKey, type SerializedTextNode, type Spread, TextNode } from 'lexical';
import { type EditorConfig, type LexicalNode, type NodeKey, type SerializedTextNode, type Spread, TextNode, ElementNode } from 'lexical';
import 'prismjs/components/prism-clike';

@@ -47,2 +47,4 @@ import 'prismjs/components/prism-javascript';

setFormat(format: number): this;
isParentRequired(): true;
createParentElementNode(): ElementNode;
}

@@ -49,0 +51,0 @@ export declare function $createCodeHighlightNode(text: string, highlightType?: string | null | undefined): CodeHighlightNode;

@@ -36,3 +36,3 @@ /**

createDOM(config: EditorConfig): HTMLElement;
updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
updateDOM(prevNode: CodeNode, dom: HTMLElement, config: EditorConfig): boolean;
static importDOM(): DOMConversionMap | null;

@@ -44,3 +44,3 @@ static importJSON(serializedNode: SerializedCodeNode): CodeNode;

canIndent(): false;
collapseAtStart(): true;
collapseAtStart(): boolean;
setLanguage(language: string): void;

@@ -47,0 +47,0 @@ getLanguage(): string | null | undefined;

@@ -32,167 +32,3 @@ /**

*/
const DEFAULT_CODE_LANGUAGE = 'javascript';
const CODE_LANGUAGE_FRIENDLY_NAME_MAP = {
c: 'C',
clike: 'C-like',
css: 'CSS',
html: 'HTML',
js: 'JavaScript',
markdown: 'Markdown',
objc: 'Objective-C',
plain: 'Plain Text',
py: 'Python',
rust: 'Rust',
sql: 'SQL',
swift: 'Swift',
typescript: 'TypeScript',
xml: 'XML'
};
const CODE_LANGUAGE_MAP = {
javascript: 'js',
md: 'markdown',
plaintext: 'plain',
python: 'py',
text: 'plain',
ts: 'typescript'
};
function normalizeCodeLang(lang) {
return CODE_LANGUAGE_MAP[lang] || lang;
}
function getLanguageFriendlyName(lang) {
const _lang = normalizeCodeLang(lang);
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
}
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
const getCodeLanguages = () => Object.keys(Prism.languages).filter( // Prism has several language helpers mixed into languages object
// so filtering them out here to get langs list
language => typeof Prism.languages[language] !== 'function').sort();
/** @noInheritDoc */
class CodeHighlightNode extends lexical.TextNode {
/** @internal */
constructor(text, highlightType, key) {
super(text, key);
this.__highlightType = highlightType;
}
static getType() {
return 'code-highlight';
}
static clone(node) {
return new CodeHighlightNode(node.__text, node.__highlightType || undefined, node.__key);
}
getHighlightType() {
const self = this.getLatest();
return self.__highlightType;
}
createDOM(config) {
const element = super.createDOM(config);
const className = getHighlightThemeClass(config.theme, this.__highlightType);
utils.addClassNamesToElement(element, className);
return element;
}
updateDOM(prevNode, dom, config) {
const update = super.updateDOM(prevNode, dom, config);
const prevClassName = getHighlightThemeClass(config.theme, prevNode.__highlightType);
const nextClassName = getHighlightThemeClass(config.theme, this.__highlightType);
if (prevClassName !== nextClassName) {
if (prevClassName) {
utils.removeClassNamesFromElement(dom, prevClassName);
}
if (nextClassName) {
utils.addClassNamesToElement(dom, nextClassName);
}
}
return update;
}
static importJSON(serializedNode) {
const node = $createCodeHighlightNode(serializedNode.text, 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',
version: 1
};
} // Prevent formatting (bold, underline, etc)
setFormat(format) {
return this;
}
}
function getHighlightThemeClass(theme, highlightType) {
return highlightType && theme && theme.codeHighlight && theme.codeHighlight[highlightType];
}
function $createCodeHighlightNode(text, highlightType) {
return lexical.$applyNodeReplacement(new CodeHighlightNode(text, highlightType));
}
function $isCodeHighlightNode(node) {
return node instanceof CodeHighlightNode;
}
function getFirstCodeHighlightNodeOfLine(anchor) {
let currentNode = null;
const previousSiblings = anchor.getPreviousSiblings();
previousSiblings.push(anchor);
while (previousSiblings.length > 0) {
const node = previousSiblings.pop();
if ($isCodeHighlightNode(node)) {
currentNode = node;
}
if (lexical.$isLineBreakNode(node)) {
break;
}
}
return currentNode;
}
function getLastCodeHighlightNodeOfLine(anchor) {
let currentNode = null;
const nextSiblings = anchor.getNextSiblings();
nextSiblings.unshift(anchor);
while (nextSiblings.length > 0) {
const node = nextSiblings.shift();
if ($isCodeHighlightNode(node)) {
currentNode = node;
}
if (lexical.$isLineBreakNode(node)) {
break;
}
}
return currentNode;
}
/**
* 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.
*
*/
const mapToPrismLanguage = language => {

@@ -247,3 +83,3 @@ // eslint-disable-next-line no-prototype-builtins

updateDOM(prevNode, dom) {
updateDOM(prevNode, dom, config) {
const language = this.__language;

@@ -267,3 +103,3 @@ const prevLanguage = prevNode.__language;

// but if it's a multi line <code> we'll create a block. Pass through to
// inline format handled by TextNode otherwise
// inline format handled by TextNode otherwise.
code: node => {

@@ -290,3 +126,3 @@ const isMultiLine = node.textContent != null && (/\r?\n/.test(node.textContent) || hasChildDOMNodeTag(node, 'BR'));

conversion: convertTableElement,
priority: 4
priority: 3
};

@@ -305,3 +141,3 @@ }

conversion: convertTableCellElement,
priority: 4
priority: 3
};

@@ -315,3 +151,3 @@ }

conversion: convertCodeNoop,
priority: 4
priority: 3
};

@@ -330,3 +166,3 @@ }

conversion: convertCodeNoop,
priority: 4
priority: 3
};

@@ -530,2 +366,174 @@ }

*/
const DEFAULT_CODE_LANGUAGE = 'javascript';
const CODE_LANGUAGE_FRIENDLY_NAME_MAP = {
c: 'C',
clike: 'C-like',
css: 'CSS',
html: 'HTML',
js: 'JavaScript',
markdown: 'Markdown',
objc: 'Objective-C',
plain: 'Plain Text',
py: 'Python',
rust: 'Rust',
sql: 'SQL',
swift: 'Swift',
typescript: 'TypeScript',
xml: 'XML'
};
const CODE_LANGUAGE_MAP = {
javascript: 'js',
md: 'markdown',
plaintext: 'plain',
python: 'py',
text: 'plain',
ts: 'typescript'
};
function normalizeCodeLang(lang) {
return CODE_LANGUAGE_MAP[lang] || lang;
}
function getLanguageFriendlyName(lang) {
const _lang = normalizeCodeLang(lang);
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
}
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
const getCodeLanguages = () => Object.keys(Prism.languages).filter( // Prism has several language helpers mixed into languages object
// so filtering them out here to get langs list
language => typeof Prism.languages[language] !== 'function').sort();
/** @noInheritDoc */
class CodeHighlightNode extends lexical.TextNode {
/** @internal */
constructor(text, highlightType, key) {
super(text, key);
this.__highlightType = highlightType;
}
static getType() {
return 'code-highlight';
}
static clone(node) {
return new CodeHighlightNode(node.__text, node.__highlightType || undefined, node.__key);
}
getHighlightType() {
const self = this.getLatest();
return self.__highlightType;
}
createDOM(config) {
const element = super.createDOM(config);
const className = getHighlightThemeClass(config.theme, this.__highlightType);
utils.addClassNamesToElement(element, className);
return element;
}
updateDOM(prevNode, dom, config) {
const update = super.updateDOM(prevNode, dom, config);
const prevClassName = getHighlightThemeClass(config.theme, prevNode.__highlightType);
const nextClassName = getHighlightThemeClass(config.theme, this.__highlightType);
if (prevClassName !== nextClassName) {
if (prevClassName) {
utils.removeClassNamesFromElement(dom, prevClassName);
}
if (nextClassName) {
utils.addClassNamesToElement(dom, nextClassName);
}
}
return update;
}
static importJSON(serializedNode) {
const node = $createCodeHighlightNode(serializedNode.text, 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',
version: 1
};
} // Prevent formatting (bold, underline, etc)
setFormat(format) {
return this;
}
isParentRequired() {
return true;
}
createParentElementNode() {
return $createCodeNode();
}
}
function getHighlightThemeClass(theme, highlightType) {
return highlightType && theme && theme.codeHighlight && theme.codeHighlight[highlightType];
}
function $createCodeHighlightNode(text, highlightType) {
return lexical.$applyNodeReplacement(new CodeHighlightNode(text, highlightType));
}
function $isCodeHighlightNode(node) {
return node instanceof CodeHighlightNode;
}
function getFirstCodeHighlightNodeOfLine(anchor) {
let currentNode = null;
const previousSiblings = anchor.getPreviousSiblings();
previousSiblings.push(anchor);
while (previousSiblings.length > 0) {
const node = previousSiblings.pop();
if ($isCodeHighlightNode(node)) {
currentNode = node;
}
if (lexical.$isLineBreakNode(node)) {
break;
}
}
return currentNode;
}
function getLastCodeHighlightNodeOfLine(anchor) {
let currentNode = null;
const nextSiblings = anchor.getNextSiblings();
nextSiblings.unshift(anchor);
while (nextSiblings.length > 0) {
const node = nextSiblings.shift();
if ($isCodeHighlightNode(node)) {
currentNode = node;
}
if (lexical.$isLineBreakNode(node)) {
break;
}
}
return currentNode;
}
/**
* 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.
*
*/
const PrismTokenizer = {

@@ -532,0 +540,0 @@ defaultLanguage: DEFAULT_CODE_LANGUAGE,

@@ -8,28 +8,29 @@ /**

'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");
require("prismjs/components/prism-typescript");var n=require("@lexical/utils"),u=require("lexical");let v={c:"C",clike:"C-like",css:"CSS",html:"HTML",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",typescript:"TypeScript",xml:"XML"},x={javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain",ts:"typescript"};function y(a){return x[a]||a}
class z extends u.TextNode{constructor(a,b,c){super(a,c);this.__highlightType=b}static getType(){return"code-highlight"}static clone(a){return new z(a.__text,a.__highlightType||void 0,a.__key)}getHighlightType(){return this.getLatest().__highlightType}createDOM(a){let b=super.createDOM(a);a=A(a.theme,this.__highlightType);n.addClassNamesToElement(b,a);return b}updateDOM(a,b,c){let d=super.updateDOM(a,b,c);a=A(c.theme,a.__highlightType);c=A(c.theme,this.__highlightType);a!==c&&(a&&n.removeClassNamesFromElement(b,
a),c&&n.addClassNamesToElement(b,c));return d}static importJSON(a){let b=B(a.text,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",version:1}}setFormat(){return this}}function A(a,b){return b&&a&&a.codeHighlight&&a.codeHighlight[b]}function B(a,b){return u.$applyNodeReplacement(new z(a,b))}function C(a){return a instanceof z}
function D(a){let b=null,c=a.getPreviousSiblings();for(c.push(a);0<c.length&&(a=c.pop(),C(a)&&(b=a),!u.$isLineBreakNode(a)););return b}function E(a){let b=null,c=a.getNextSiblings();for(c.unshift(a);0<c.length&&(a=c.shift(),C(a)&&(b=a),!u.$isLineBreakNode(a)););return b}let F=a=>null!=a&&e.languages.hasOwnProperty(a)?a:void 0;function G(a,b){for(let c of a.childNodes){if(c instanceof HTMLElement&&c.tagName===b)return!0;G(c,b)}return!1}
class H extends u.ElementNode{static getType(){return"code"}static clone(a){return new H(a.__language,a.__key)}constructor(a,b){super(b);this.__language=F(a)}createDOM(a){let b=document.createElement("code");n.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{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);
require("prismjs/components/prism-typescript");var n=require("@lexical/utils"),u=require("lexical");let v=a=>null!=a&&e.languages.hasOwnProperty(a)?a:void 0;function x(a,b){for(let c of a.childNodes){if(c instanceof HTMLElement&&c.tagName===b)return!0;x(c,b)}return!1}
class y extends u.ElementNode{static getType(){return"code"}static clone(a){return new y(a.__language,a.__key)}constructor(a,b){super(b);this.__language=v(a)}createDOM(a){let b=document.createElement("code");n.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{code:a=>null!=a.textContent&&(/\r?\n/.test(a.textContent)||x(a,"BR"))?{conversion:z,priority:1}:null,div:()=>({conversion:aa,priority:1}),pre:()=>({conversion:z,priority:0}),table:a=>A(a)?{conversion:ba,priority:3}:null,td:a=>{let b=a.closest("table");return a.classList.contains("js-file-line")?{conversion:ca,priority:3}:b&&A(b)?{conversion:B,priority:3}:null},tr:a=>(a=a.closest("table"))&&A(a)?{conversion:B,priority:3}:null}}static importJSON(a){let b=C(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,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={defaultLanguage:"javascript",tokenize(a,b){return e.tokenize(a,e.languages[b||""]||e.languages[this.defaultLanguage])}};
0;for(d=d.getTextContent();c<d.length&&/[\t ]/.test(d[c]);)c+=1;if(0<c)return c=d.substring(0,c),c=E(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=v(a)}getLanguage(){return this.getLatest().__language}}
function C(a){return u.$applyNodeReplacement(new y(a))}function F(a){return a instanceof y}function z(){return{node:C(),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?C():null,preformatted:b}:{node:null}}function ba(){return{node:C(),preformatted:!0}}function B(){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 A(a){return a.classList.contains("js-file-line-container")}
let G={c:"C",clike:"C-like",css:"CSS",html:"HTML",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",typescript:"TypeScript",xml:"XML"},H={javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain",ts:"typescript"};function I(a){return H[a]||a}
class J extends u.TextNode{constructor(a,b,c){super(a,c);this.__highlightType=b}static getType(){return"code-highlight"}static clone(a){return new J(a.__text,a.__highlightType||void 0,a.__key)}getHighlightType(){return this.getLatest().__highlightType}createDOM(a){let b=super.createDOM(a);a=K(a.theme,this.__highlightType);n.addClassNamesToElement(b,a);return b}updateDOM(a,b,c){let d=super.updateDOM(a,b,c);a=K(c.theme,a.__highlightType);c=K(c.theme,this.__highlightType);a!==c&&(a&&n.removeClassNamesFromElement(b,
a),c&&n.addClassNamesToElement(b,c));return d}static importJSON(a){let b=E(a.text,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",version:1}}setFormat(){return this}isParentRequired(){return!0}createParentElementNode(){return C()}}function K(a,b){return b&&a&&a.codeHighlight&&a.codeHighlight[b]}
function E(a,b){return u.$applyNodeReplacement(new J(a,b))}function L(a){return a instanceof J}function D(a){let b=null,c=a.getPreviousSiblings();for(c.push(a);0<c.length&&(a=c.pop(),L(a)&&(b=a),!u.$isLineBreakNode(a)););return b}function M(a){let b=null,c=a.getNextSiblings();for(c.unshift(a);0<c.length&&(a=c.shift(),L(a)&&(b=a),!u.$isLineBreakNode(a)););return b}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(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}=
function P(a){let b=null,c=-1;var d=a.getPreviousSiblings();for(d.push(a);0<d.length;){var f=d.pop();if(L(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(L(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(L(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(L(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();F(d)?S(d,b,c):L(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(c.defaultLanguage),b.update(()=>{ea(d,()=>{var f=u.$getNodeByKey(d);if(!F(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()}}
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}
function X(a,b){let c=a.getTextContent();b===u.INDENT_CONTENT_COMMAND?0<c.length&&/\s/.test(c[0])?a.setTextContent("\t"+c):(b=B("\t"),a.insertBefore(b)):0===c.indexOf("\t")&&(1===c.length?a.remove():a.setTextContent(c.substring(1)))}
function Y(a,b){let c=u.$getSelection();if(!u.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.offset,k=f.offset,h=d.getNode(),l=f.getNode();var p=a===u.KEY_ARROW_UP_COMMAND;if(!C(h)||!C(l))return!1;if(!b.altKey){if(c.isCollapsed())if(a=h.getParentOrThrow(),p&&0===g&&null===h.getPreviousSibling()){if(null===a.getPreviousSibling())return a.selectPrevious(),b.preventDefault(),!0}else if(!p&&g===h.getTextContentSize()&&null===h.getNextSibling()&&null===a.getNextSibling())return a.selectNext(),
b.preventDefault(),!0;return!1}var m=D(h);let w=E(l);if(null==m||null==w)return!1;let t=m.getNodesBetween(w);for(let q=0;q<t.length;q++){let Z=t[q];if(!C(Z)&&!u.$isLineBreakNode(Z))return!1}b.preventDefault();b.stopPropagation();b=p?m.getPreviousSibling():w.getNextSibling();if(!u.$isLineBreakNode(b))return!0;m=p?b.getPreviousSibling():b.getNextSibling();if(null==m)return!0;p=p?D(m):E(m);let r=null!=p?p:m;b.remove();t.forEach(q=>q.remove());a===u.KEY_ARROW_UP_COMMAND?(t.forEach(q=>r.insertBefore(q)),
r.insertBefore(b)):(r.insertAfter(b),r=b,t.forEach(q=>{r.insertAfter(q);r=q}));c.setTextNodeRange(h,g,l,k);return!0}function fa(a,b){let c=u.$getSelection();if(!u.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.getNode(),k=f.getNode();a=a===u.MOVE_TO_START;if(!C(g)||!C(k))return!1;let h,l;a?{node:h,offset:l}=P(k):{node:h,offset:l}=Q(k);null!==h&&-1!==l&&c.setTextNodeRange(h,l,h,l);b.preventDefault();b.stopPropagation();return!0}exports.$createCodeHighlightNode=B;
exports.$createCodeNode=L;exports.$isCodeHighlightNode=C;exports.$isCodeNode=M;exports.CODE_LANGUAGE_FRIENDLY_NAME_MAP=v;exports.CODE_LANGUAGE_MAP=x;exports.CodeHighlightNode=z;exports.CodeNode=H;exports.DEFAULT_CODE_LANGUAGE="javascript";exports.PrismTokenizer=N;exports.getCodeLanguages=()=>Object.keys(e.languages).filter(a=>"function"!==typeof e.languages[a]).sort();exports.getDefaultCodeLanguage=()=>"javascript";exports.getEndOfCodeInLine=Q;exports.getFirstCodeHighlightNodeOfLine=D;
exports.getLanguageFriendlyName=function(a){a=y(a);return v[a]||a};exports.getLastCodeHighlightNodeOfLine=E;exports.getStartOfCodeInLine=P;exports.normalizeCodeLang=y;
exports.registerCodeHighlighting=function(a,b){if(!a.hasNodes([H,z]))throw Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");null==b&&(b=N);return n.mergeRegister(a.registerMutationListener(H,c=>{a.update(()=>{for(let [g,k]of c)if("destroyed"!==k){var d=u.$getNodeByKey(g);if(null!==d)a:{var f=d;d=a.getElementByKey(f.getKey());if(null===d)break a;f=f.getChildren();let h=f.length;if(h===d.__cachedChildrenLength)break a;d.__cachedChildrenLength=h;let l="1",p=1;for(let m=
0;m<h;m++)u.$isLineBreakNode(f[m])&&(l+="\n"+ ++p);d.setAttribute("data-gutter",l)}}})}),a.registerNodeTransform(H,c=>S(c,a,b)),a.registerNodeTransform(u.TextNode,c=>R(c,a,b)),a.registerNodeTransform(z,c=>R(c,a,b)),a.registerCommand(u.INDENT_CONTENT_COMMAND,()=>W(u.INDENT_CONTENT_COMMAND),u.COMMAND_PRIORITY_LOW),a.registerCommand(u.OUTDENT_CONTENT_COMMAND,()=>W(u.OUTDENT_CONTENT_COMMAND),u.COMMAND_PRIORITY_LOW),a.registerCommand(u.KEY_ARROW_UP_COMMAND,c=>Y(u.KEY_ARROW_UP_COMMAND,c),u.COMMAND_PRIORITY_LOW),
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(E(f));d<c.length-1&&b.push(u.$createLineBreakNode())}}else({content:d}=c),"string"===typeof d?b.push(E(d,c.type)):Array.isArray(d)&&1===d.length&&"string"===typeof d[0]?b.push(E(d[0],c.type)):Array.isArray(d)&&b.push(...U(d))});return b}
function ea(a,b){a=u.$getNodeByKey(a);if(F(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()}}
function V(a,b){return L(a)&&L(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(!L(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])&&L(d)&&X(d,a);return!0}
function X(a,b){let c=a.getTextContent();b===u.INDENT_CONTENT_COMMAND?0<c.length&&/\s/.test(c[0])?a.setTextContent("\t"+c):(b=E("\t"),a.insertBefore(b)):0===c.indexOf("\t")&&(1===c.length?a.remove():a.setTextContent(c.substring(1)))}
function Y(a,b){let c=u.$getSelection();if(!u.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.offset,k=f.offset,h=d.getNode(),l=f.getNode();var p=a===u.KEY_ARROW_UP_COMMAND;if(!L(h)||!L(l))return!1;if(!b.altKey){if(c.isCollapsed())if(a=h.getParentOrThrow(),p&&0===g&&null===h.getPreviousSibling()){if(null===a.getPreviousSibling())return a.selectPrevious(),b.preventDefault(),!0}else if(!p&&g===h.getTextContentSize()&&null===h.getNextSibling()&&null===a.getNextSibling())return a.selectNext(),
b.preventDefault(),!0;return!1}var m=D(h);let w=M(l);if(null==m||null==w)return!1;let t=m.getNodesBetween(w);for(let q=0;q<t.length;q++){let Z=t[q];if(!L(Z)&&!u.$isLineBreakNode(Z))return!1}b.preventDefault();b.stopPropagation();b=p?m.getPreviousSibling():w.getNextSibling();if(!u.$isLineBreakNode(b))return!0;m=p?b.getPreviousSibling():b.getNextSibling();if(null==m)return!0;p=p?D(m):M(m);let r=null!=p?p:m;b.remove();t.forEach(q=>q.remove());a===u.KEY_ARROW_UP_COMMAND?(t.forEach(q=>r.insertBefore(q)),
r.insertBefore(b)):(r.insertAfter(b),r=b,t.forEach(q=>{r.insertAfter(q);r=q}));c.setTextNodeRange(h,g,l,k);return!0}function fa(a,b){let c=u.$getSelection();if(!u.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.getNode(),k=f.getNode();a=a===u.MOVE_TO_START;if(!L(g)||!L(k))return!1;let h,l;a?{node:h,offset:l}=P(k):{node:h,offset:l}=Q(k);null!==h&&-1!==l&&c.setTextNodeRange(h,l,h,l);b.preventDefault();b.stopPropagation();return!0}exports.$createCodeHighlightNode=E;
exports.$createCodeNode=C;exports.$isCodeHighlightNode=L;exports.$isCodeNode=F;exports.CODE_LANGUAGE_FRIENDLY_NAME_MAP=G;exports.CODE_LANGUAGE_MAP=H;exports.CodeHighlightNode=J;exports.CodeNode=y;exports.DEFAULT_CODE_LANGUAGE="javascript";exports.PrismTokenizer=N;exports.getCodeLanguages=()=>Object.keys(e.languages).filter(a=>"function"!==typeof e.languages[a]).sort();exports.getDefaultCodeLanguage=()=>"javascript";exports.getEndOfCodeInLine=Q;exports.getFirstCodeHighlightNodeOfLine=D;
exports.getLanguageFriendlyName=function(a){a=I(a);return G[a]||a};exports.getLastCodeHighlightNodeOfLine=M;exports.getStartOfCodeInLine=P;exports.normalizeCodeLang=I;
exports.registerCodeHighlighting=function(a,b){if(!a.hasNodes([y,J]))throw Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");null==b&&(b=N);return n.mergeRegister(a.registerMutationListener(y,c=>{a.update(()=>{for(let [g,k]of c)if("destroyed"!==k){var d=u.$getNodeByKey(g);if(null!==d)a:{var f=d;d=a.getElementByKey(f.getKey());if(null===d)break a;f=f.getChildren();let h=f.length;if(h===d.__cachedChildrenLength)break a;d.__cachedChildrenLength=h;let l="1",p=1;for(let m=
0;m<h;m++)u.$isLineBreakNode(f[m])&&(l+="\n"+ ++p);d.setAttribute("data-gutter",l)}}})}),a.registerNodeTransform(y,c=>S(c,a,b)),a.registerNodeTransform(u.TextNode,c=>R(c,a,b)),a.registerNodeTransform(J,c=>R(c,a,b)),a.registerCommand(u.INDENT_CONTENT_COMMAND,()=>W(u.INDENT_CONTENT_COMMAND),u.COMMAND_PRIORITY_LOW),a.registerCommand(u.OUTDENT_CONTENT_COMMAND,()=>W(u.OUTDENT_CONTENT_COMMAND),u.COMMAND_PRIORITY_LOW),a.registerCommand(u.KEY_ARROW_UP_COMMAND,c=>Y(u.KEY_ARROW_UP_COMMAND,c),u.COMMAND_PRIORITY_LOW),
a.registerCommand(u.KEY_ARROW_DOWN_COMMAND,c=>Y(u.KEY_ARROW_DOWN_COMMAND,c),u.COMMAND_PRIORITY_LOW),a.registerCommand(u.MOVE_TO_END,c=>fa(u.MOVE_TO_END,c),u.COMMAND_PRIORITY_LOW),a.registerCommand(u.MOVE_TO_START,c=>fa(u.MOVE_TO_START,c),u.COMMAND_PRIORITY_LOW))}

@@ -11,9 +11,9 @@ {

"license": "MIT",
"version": "0.7.6",
"version": "0.7.7",
"main": "LexicalCode.js",
"peerDependencies": {
"lexical": "0.7.6"
"lexical": "0.7.7"
},
"dependencies": {
"@lexical/utils": "0.7.6",
"@lexical/utils": "0.7.7",
"prismjs": "^1.27.0"

@@ -20,0 +20,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc