@lexical/code 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,7 +95,8 @@ class CodeHighlightNode extends lexical.TextNode {
95
95
  exportJSON() {
96
96
  return { ...super.exportJSON(),
97
97
  highlightType: this.getHighlightType(),
98
- type: 'code-highlight'
98
+ type: 'code-highlight',
99
+ version: 1
99
100
  };
100
101
  } // Prevent formatting (bold, underline, etc)
101
102
 
@@ -237,7 +238,8 @@ class CodeNode extends lexical.ElementNode {
237
238
  exportJSON() {
238
239
  return { ...super.exportJSON(),
239
240
  language: this.getLanguage(),
240
- type: 'code'
241
+ type: 'code',
242
+ version: 1
241
243
  };
242
244
  } // Mutation
243
245
 
@@ -7,12 +7,12 @@
7
7
  '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");
8
8
  var m=require("@lexical/utils"),r=require("lexical");let t=a=>null!=a&&e.languages.hasOwnProperty(a)?a:void 0;
9
9
  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,
10
- a),c&&m.addClassNamesToElement(b,c));return d}static importJSON(a){let b=y(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"}}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}
10
+ a),c&&m.addClassNamesToElement(b,c));return d}static importJSON(a){let b=y(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 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}
11
11
  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");
12
12
  return!1}static importDOM(){return{code:()=>({conversion:C,priority:0}),div:()=>({conversion:D,priority:1}),pre:()=>({conversion:C,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);
13
- 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,
14
- 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}
15
- 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}
13
+ 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=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=
14
+ 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)}
15
+ 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}
16
16
  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}}
17
17
  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 C(){return{node:I()}}
18
18
  function D(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")}
package/index.d.ts ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { DOMConversionMap, EditorConfig, LexicalEditor, LexicalNode, NodeKey, ParagraphNode, RangeSelection, SerializedElementNode, SerializedTextNode, Spread } from 'lexical';
9
+ import 'prismjs/components/prism-clike';
10
+ import 'prismjs/components/prism-javascript';
11
+ import 'prismjs/components/prism-markup';
12
+ import 'prismjs/components/prism-markdown';
13
+ import 'prismjs/components/prism-c';
14
+ import 'prismjs/components/prism-css';
15
+ import 'prismjs/components/prism-objectivec';
16
+ import 'prismjs/components/prism-sql';
17
+ import 'prismjs/components/prism-python';
18
+ import 'prismjs/components/prism-rust';
19
+ import 'prismjs/components/prism-swift';
20
+ import { ElementNode, TextNode } from 'lexical';
21
+ declare type SerializedCodeNode = Spread<{
22
+ language: string | null | undefined;
23
+ type: 'code';
24
+ version: 1;
25
+ }, SerializedElementNode>;
26
+ declare type SerializedCodeHighlightNode = Spread<{
27
+ highlightType: string | null | undefined;
28
+ type: 'code-highlight';
29
+ version: 1;
30
+ }, SerializedTextNode>;
31
+ export declare const getDefaultCodeLanguage: () => string;
32
+ export declare const getCodeLanguages: () => Array<string>;
33
+ export declare class CodeHighlightNode extends TextNode {
34
+ __highlightType: string | null | undefined;
35
+ constructor(text: string, highlightType?: string, key?: NodeKey);
36
+ static getType(): string;
37
+ static clone(node: CodeHighlightNode): CodeHighlightNode;
38
+ getHighlightType(): string | null | undefined;
39
+ createDOM(config: EditorConfig): HTMLElement;
40
+ updateDOM(prevNode: CodeHighlightNode, dom: HTMLElement, config: EditorConfig): boolean;
41
+ static importJSON(serializedNode: SerializedCodeHighlightNode): CodeHighlightNode;
42
+ exportJSON(): SerializedCodeHighlightNode;
43
+ setFormat(format: number): this;
44
+ }
45
+ export declare function $createCodeHighlightNode(text: string, highlightType?: string): CodeHighlightNode;
46
+ export declare function $isCodeHighlightNode(node: LexicalNode | CodeHighlightNode | null | undefined): node is CodeHighlightNode;
47
+ export declare class CodeNode extends ElementNode {
48
+ __language: string | null | undefined;
49
+ static getType(): string;
50
+ static clone(node: CodeNode): CodeNode;
51
+ constructor(language?: string | null | undefined, key?: NodeKey);
52
+ createDOM(config: EditorConfig): HTMLElement;
53
+ updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
54
+ static importDOM(): DOMConversionMap | null;
55
+ static importJSON(serializedNode: SerializedCodeNode): CodeNode;
56
+ exportJSON(): SerializedCodeNode;
57
+ insertNewAfter(selection: RangeSelection): null | ParagraphNode | CodeHighlightNode;
58
+ canInsertTab(): boolean;
59
+ canIndent(): false;
60
+ collapseAtStart(): true;
61
+ setLanguage(language: string): void;
62
+ getLanguage(): string | null | undefined;
63
+ }
64
+ export declare function $createCodeNode(language?: string): CodeNode;
65
+ export declare function $isCodeNode(node: LexicalNode | null | undefined): node is CodeNode;
66
+ export declare function getFirstCodeHighlightNodeOfLine(anchor: LexicalNode): CodeHighlightNode | null | undefined;
67
+ export declare function getLastCodeHighlightNodeOfLine(anchor: LexicalNode): CodeHighlightNode | null | undefined;
68
+ export declare function getStartOfCodeInLine(anchor: LexicalNode): {
69
+ node: TextNode | null;
70
+ offset: number;
71
+ };
72
+ export declare function getEndOfCodeInLine(anchor: LexicalNode): {
73
+ node: TextNode | null;
74
+ offset: number;
75
+ };
76
+ export declare function registerCodeHighlighting(editor: LexicalEditor): () => void;
77
+ export {};
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "code"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.3.3",
11
+ "version": "0.3.4",
12
12
  "main": "LexicalCode.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.3.3"
14
+ "lexical": "0.3.4"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.3.3",
17
+ "@lexical/utils": "0.3.4",
18
18
  "prismjs": "^1.27.0"
19
19
  },
20
20
  "repository": {
package/LexicalCode.d.ts DELETED
@@ -1,99 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- import type {
10
- EditorConfig,
11
- LexicalNode,
12
- NodeKey,
13
- ParagraphNode,
14
- RangeSelection,
15
- EditorThemeClasses,
16
- LexicalEditor,
17
- SerializedElementNode,
18
- SerializedTextNode,
19
- } from 'lexical';
20
-
21
- import {ElementNode, TextNode} from 'lexical';
22
- import type {Spread} from 'lexical';
23
-
24
- declare class CodeNode extends ElementNode {
25
- static getType(): string;
26
- static clone(node: CodeNode): CodeNode;
27
- constructor(key?: NodeKey);
28
- createDOM(config: EditorConfig): HTMLElement;
29
- updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
30
- insertNewAfter(
31
- selection: RangeSelection,
32
- ): null | ParagraphNode | CodeHighlightNode;
33
- canInsertTab(): boolean;
34
- collapseAtStart(): true;
35
- setLanguage(language: string): void;
36
- getLanguage(): string | void;
37
- importJSON(serializedNode: SerializedCodeNode): CodeNode;
38
- exportJSON(): SerializedElementNode;
39
- }
40
- declare function $createCodeNode(language?: string): CodeNode;
41
- declare function $isCodeNode(
42
- node: null | undefined | LexicalNode,
43
- ): node is CodeNode;
44
-
45
- declare function getFirstCodeHighlightNodeOfLine(
46
- anchor: LexicalNode,
47
- ): null | undefined | CodeHighlightNode;
48
-
49
- declare function getLastCodeHighlightNodeOfLine(
50
- anchor: LexicalNode,
51
- ): null | undefined | CodeHighlightNode;
52
-
53
- declare function getDefaultCodeLanguage(): string;
54
- declare function getCodeLanguages(): Array<string>;
55
-
56
- declare class CodeHighlightNode extends TextNode {
57
- __highlightType: null | undefined | string;
58
- constructor(text: string, highlightType?: string, key?: NodeKey);
59
- static getType(): string;
60
- static clone(node: CodeHighlightNode): CodeHighlightNode;
61
- createDOM(config: EditorConfig): HTMLElement;
62
- updateDOM(
63
- prevNode: CodeHighlightNode,
64
- dom: HTMLElement,
65
- config: EditorConfig,
66
- ): boolean;
67
- setFormat(format: number): this;
68
- }
69
- declare function getHighlightThemeClass(
70
- theme: EditorThemeClasses,
71
- highlightType: null | undefined | string,
72
- ): null | undefined | string;
73
- declare function $createCodeHighlightNode(
74
- text: string,
75
- highlightType?: string,
76
- ): CodeHighlightNode;
77
- declare function $isCodeHighlightNode(
78
- node: LexicalNode | null | undefined,
79
- ): node is CodeHighlightNode;
80
-
81
- declare function registerCodeHighlighting(editor: LexicalEditor): () => void;
82
-
83
- type SerializedCodeNode = Spread<
84
- {
85
- language: string | null | undefined;
86
- type: 'code';
87
- version: 1;
88
- },
89
- SerializedElementNode
90
- >;
91
-
92
- type SerializedCodeHighlightNode = Spread<
93
- {
94
- highlightType: string | null | undefined;
95
- type: 'code-highlight';
96
- version: 1;
97
- },
98
- SerializedTextNode
99
- >;