@lexical/code 0.2.4 → 0.2.7

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.
package/LexicalCode.d.ts CHANGED
@@ -4,7 +4,6 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
- * @flow strict
8
7
  */
9
8
 
10
9
  import type {
@@ -23,17 +22,17 @@ declare class CodeNode extends ElementNode {
23
22
  static getType(): string;
24
23
  static clone(node: CodeNode): CodeNode;
25
24
  constructor(key?: NodeKey);
26
- createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
25
+ createDOM(config: EditorConfig): HTMLElement;
27
26
  updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
28
27
  insertNewAfter(
29
28
  selection: RangeSelection,
30
29
  ): null | ParagraphNode | CodeHighlightNode;
31
- canInsertTab(): true;
30
+ canInsertTab(): boolean;
32
31
  collapseAtStart(): true;
33
32
  setLanguage(language: string): void;
34
33
  getLanguage(): string | void;
35
34
  }
36
- declare function $createCodeNode(): CodeNode;
35
+ declare function $createCodeNode(language?: string): CodeNode;
37
36
  declare function $isCodeNode(
38
37
  node: null | undefined | LexicalNode,
39
38
  ): node is CodeNode;
@@ -54,12 +53,11 @@ declare class CodeHighlightNode extends TextNode {
54
53
  constructor(text: string, highlightType?: string, key?: NodeKey);
55
54
  static getType(): string;
56
55
  static clone(node: CodeHighlightNode): CodeHighlightNode;
57
- createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
58
- updateDOM<EditorContext>(
59
- // $FlowFixMe
56
+ createDOM(config: EditorConfig): HTMLElement;
57
+ updateDOM(
60
58
  prevNode: CodeHighlightNode,
61
59
  dom: HTMLElement,
62
- config: EditorConfig<EditorContext>,
60
+ config: EditorConfig,
63
61
  ): boolean;
64
62
  setFormat(format: number): this;
65
63
  }
@@ -72,7 +70,7 @@ declare function $createCodeHighlightNode(
72
70
  highlightType?: string,
73
71
  ): CodeHighlightNode;
74
72
  declare function $isCodeHighlightNode(
75
- node: ?LexicalNode,
73
+ node: LexicalNode | null | undefined,
76
74
  ): node is CodeHighlightNode;
77
75
 
78
76
  declare function registerCodeHighlighting(editor: LexicalEditor): () => void;
@@ -6,7 +6,7 @@
6
6
  */
7
7
  'use strict';
8
8
 
9
- var Prism = require('prismjs/components/prism-core');
9
+ var Prism = require('prismjs');
10
10
  require('prismjs/components/prism-clike');
11
11
  require('prismjs/components/prism-javascript');
12
12
  require('prismjs/components/prism-markup');
@@ -27,9 +27,14 @@ var lexical = require('lexical');
27
27
  * This source code is licensed under the MIT license found in the
28
28
  * LICENSE file in the root directory of this source tree.
29
29
  *
30
- *
31
30
  */
32
31
  const DEFAULT_CODE_LANGUAGE = 'javascript';
32
+
33
+ const mapToPrismLanguage = language => {
34
+ // eslint-disable-next-line no-prototype-builtins
35
+ return language != null && Prism.languages.hasOwnProperty(language) ? language : undefined;
36
+ };
37
+
33
38
  const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
34
39
  const getCodeLanguages = () => Object.keys(Prism.languages).filter( // Prism has several language helpers mixed into languages object
35
40
  // so filtering them out here to get langs list
@@ -76,7 +81,7 @@ class CodeHighlightNode extends lexical.TextNode {
76
81
 
77
82
 
78
83
  setFormat(format) {
79
- return this.getWritable();
84
+ return this;
80
85
  }
81
86
 
82
87
  }
@@ -103,7 +108,7 @@ class CodeNode extends lexical.ElementNode {
103
108
 
104
109
  constructor(language, key) {
105
110
  super(key);
106
- this.__language = language;
111
+ this.__language = mapToPrismLanguage(language);
107
112
  } // View
108
113
 
109
114
 
@@ -146,8 +151,7 @@ class CodeNode extends lexical.ElementNode {
146
151
  priority: 0
147
152
  }),
148
153
  table: node => {
149
- // $FlowFixMe[incompatible-type] domNode is a <table> since we matched it by nodeName
150
- const table = node;
154
+ const table = node; // domNode is a <table> since we matched it by nodeName
151
155
 
152
156
  if (isGitHubCodeTable(table)) {
153
157
  return {
@@ -159,9 +163,8 @@ class CodeNode extends lexical.ElementNode {
159
163
  return null;
160
164
  },
161
165
  td: node => {
162
- // $FlowFixMe[incompatible-type] element is a <td> since we matched it by nodeName
163
- const td = node; // $FlowFixMe[incompatible-type] we know this will be a table, or null.
164
-
166
+ // element is a <td> since we matched it by nodeName
167
+ const td = node;
165
168
  const table = td.closest('table');
166
169
 
167
170
  if (isGitHubCodeCell(td)) {
@@ -183,9 +186,8 @@ class CodeNode extends lexical.ElementNode {
183
186
  return null;
184
187
  },
185
188
  tr: node => {
186
- // $FlowFixMe[incompatible-type] element is a <tr> since we matched it by nodeName
187
- const tr = node; // $FlowFixMe[incompatible-type] we know this will be a table, or null.
188
-
189
+ // element is a <tr> since we matched it by nodeName
190
+ const tr = node;
189
191
  const table = tr.closest('table');
190
192
 
191
193
  if (table && isGitHubCodeTable(table)) {
@@ -241,9 +243,19 @@ class CodeNode extends lexical.ElementNode {
241
243
  }
242
244
 
243
245
  canInsertTab() {
246
+ const selection = lexical.$getSelection();
247
+
248
+ if (!lexical.$isRangeSelection(selection) || !selection.isCollapsed()) {
249
+ return false;
250
+ }
251
+
244
252
  return true;
245
253
  }
246
254
 
255
+ canIndent() {
256
+ return false;
257
+ }
258
+
247
259
  collapseAtStart() {
248
260
  const paragraph = lexical.$createParagraphNode();
249
261
  const children = this.getChildren();
@@ -254,7 +266,7 @@ class CodeNode extends lexical.ElementNode {
254
266
 
255
267
  setLanguage(language) {
256
268
  const writable = this.getWritable();
257
- writable.__language = language;
269
+ writable.__language = mapToPrismLanguage(language);
258
270
  }
259
271
 
260
272
  getLanguage() {
@@ -314,7 +326,7 @@ function convertPreElement(domNode) {
314
326
  }
315
327
 
316
328
  function convertDivElement(domNode) {
317
- // $FlowFixMe[incompatible-type] domNode is a <div> since we matched it by nodeName
329
+ // domNode is a <div> since we matched it by nodeName
318
330
  const div = domNode;
319
331
  return {
320
332
  after: childLexicalNodes => {
@@ -343,7 +355,7 @@ function convertCodeNoop() {
343
355
  }
344
356
 
345
357
  function convertTableCellElement(domNode) {
346
- // $FlowFixMe[incompatible-type] domNode is a <td> since we matched it by nodeName
358
+ // domNode is a <td> since we matched it by nodeName
347
359
  const cell = domNode;
348
360
  return {
349
361
  after: childLexicalNodes => {
@@ -392,12 +404,12 @@ function updateCodeGutter(node, editor) {
392
404
  }
393
405
 
394
406
  const children = node.getChildren();
395
- const childrenLength = children.length; // $FlowFixMe: internal field
407
+ const childrenLength = children.length; // @ts-ignore: internal field
396
408
 
397
409
  if (childrenLength === codeElement.__cachedChildrenLength) {
398
410
  // Avoid updating the attribute if the children length hasn't changed.
399
411
  return;
400
- } // $FlowFixMe: internal field
412
+ } // @ts-ignore:: internal field
401
413
 
402
414
 
403
415
  codeElement.__cachedChildrenLength = childrenLength;
@@ -487,9 +499,9 @@ function getHighlightNodes(tokens) {
487
499
 
488
500
  if (typeof content === 'string') {
489
501
  nodes.push($createCodeHighlightNode(content, token.type));
490
- } else if (content.length === 1 && typeof content[0] === 'string') {
502
+ } else if (Array.isArray(content) && content.length === 1 && typeof content[0] === 'string') {
491
503
  nodes.push($createCodeHighlightNode(content[0], token.type));
492
- } else {
504
+ } else if (Array.isArray(content)) {
493
505
  nodes.push(...getHighlightNodes(content));
494
506
  }
495
507
  }
@@ -23,17 +23,17 @@ declare export class CodeNode extends ElementNode {
23
23
  static getType(): string;
24
24
  static clone(node: CodeNode): CodeNode;
25
25
  constructor(key?: NodeKey): void;
26
- createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
26
+ createDOM(config: EditorConfig): HTMLElement;
27
27
  updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
28
28
  insertNewAfter(
29
29
  selection: RangeSelection,
30
30
  ): null | ParagraphNode | CodeHighlightNode;
31
- canInsertTab(): true;
31
+ canInsertTab(): boolean;
32
32
  collapseAtStart(): true;
33
33
  setLanguage(language: string): void;
34
34
  getLanguage(): string | void;
35
35
  }
36
- declare export function $createCodeNode(): CodeNode;
36
+ declare export function $createCodeNode(language?: string): CodeNode;
37
37
  declare export function $isCodeNode(
38
38
  node: ?LexicalNode,
39
39
  ): boolean %checks(node instanceof CodeNode);
@@ -54,12 +54,12 @@ declare export class CodeHighlightNode extends TextNode {
54
54
  constructor(text: string, highlightType?: string, key?: NodeKey): void;
55
55
  static getType(): string;
56
56
  static clone(node: CodeHighlightNode): CodeHighlightNode;
57
- createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
58
- updateDOM<EditorContext>(
57
+ createDOM(config: EditorConfig): HTMLElement;
58
+ updateDOM(
59
59
  // $FlowFixMe
60
60
  prevNode: CodeHighlightNode,
61
61
  dom: HTMLElement,
62
- config: EditorConfig<EditorContext>,
62
+ config: EditorConfig,
63
63
  ): boolean;
64
64
  setFormat(format: number): this;
65
65
  }
@@ -4,25 +4,25 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var e=require("prismjs/components/prism-core");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
- var n=require("@lexical/utils"),r=require("lexical");
9
- class t extends r.TextNode{constructor(a,c,b){super(a,b);this.__highlightType=c}static getType(){return"code-highlight"}static clone(a){return new t(a.__text,a.__highlightType||void 0,a.__key)}createDOM(a){const c=super.createDOM(a);a=u(a.theme,this.__highlightType);n.addClassNamesToElement(c,a);return c}updateDOM(a,c,b){const d=super.updateDOM(a,c,b);a=u(b.theme,a.__highlightType);b=u(b.theme,this.__highlightType);a!==b&&(a&&n.removeClassNamesFromElement(c,a),b&&n.addClassNamesToElement(c,b));return d}setFormat(){return this.getWritable()}}
10
- function u(a,c){return c&&a&&a.codeHighlight&&a.codeHighlight[c]}function x(a,c){return new t(a,c)}function y(a){return a instanceof t}
11
- class z extends r.ElementNode{static getType(){return"code"}static clone(a){return new z(a.__language,a.__key)}constructor(a,c){super(c);this.__language=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");
12
- return!1}static importDOM(){return{div:()=>({conversion:B,priority:1}),pre:()=>({conversion:C,priority:0}),table:a=>D(a)?{conversion:E,priority:4}:null,td:a=>{const c=a.closest("table");return a.classList.contains("js-file-line")?{conversion:F,priority:4}:c&&D(c)?{conversion:G,priority:4}:null},tr:a=>(a=a.closest("table"))&&D(a)?{conversion:G,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()&&
13
- 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=H(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=x(b),c.insertAfter(b),a.insertNodes([r.$createLineBreakNode()]),b.select(),b}return null}canInsertTab(){return!0}collapseAtStart(){const a=r.$createParagraphNode();this.getChildren().forEach(c=>a.append(c));this.replace(a);
14
- return!0}setLanguage(a){this.getWritable().__language=a}getLanguage(){return this.getLatest().__language}}function I(a){return new z(a)}function J(a){return a instanceof z}function H(a){let c=null;const b=a.getPreviousSiblings();for(b.push(a);0<b.length&&(a=b.pop(),y(a)&&(c=a),!r.$isLineBreakNode(a)););return c}function K(a){let c=null;const b=a.getNextSiblings();for(b.unshift(a);0<b.length&&(a=b.shift(),y(a)&&(c=a),!r.$isLineBreakNode(a)););return c}function C(){return{node:I()}}
15
- function B(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")?I():null}}function E(){return{node:I()}}function G(){return{node:null}}function F(a){return{after:c=>{a.parentNode&&a.parentNode.nextSibling&&c.push(r.$createLineBreakNode());return c},node:null}}function D(a){return a.classList.contains("js-file-line-container")}
16
- function L(a,c){const b=a.getParent();J(b)?M(b,c):y(a)&&a.replace(r.$createTextNode(a.__text))}let N=!1;
17
- function M(a,c){N||(N=!0,void 0===a.getLanguage()&&a.setLanguage("javascript"),c.update(()=>{O(a,()=>{var b=a.getTextContent();b=e.tokenize(b,e.languages[a.getLanguage()||""]||e.languages.javascript);b=Q(b);var d=a.getChildren();let f=0;for(;f<d.length&&R(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++,!R(d[h-k],b[m-k])){k--;break}d=f;h-=k;b=b.slice(f,m-k);const {from:l,to:p,nodesForReplacement:v}={from:d,nodesForReplacement:b,to:h};return l!==p||v.length?
18
- (a.splice(l,p-l,v),!0):!1})},{onUpdate:()=>{N=!1},skipTransforms:!0}))}function Q(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(x(f));d<b.length-1&&c.push(r.$createLineBreakNode())}}else({content:d}=b),"string"===typeof d?c.push(x(d,b.type)):1===d.length&&"string"===typeof d[0]?c.push(x(d[0],b.type)):c.push(...Q(d))});return c}
19
- function O(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}))}}
20
- function R(a,c){return y(a)&&y(c)?a.__text===c.__text&&a.__highlightType===c.__highlightType:r.$isLineBreakNode(a)&&r.$isLineBreakNode(c)?!0:!1}function S(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(!y(d)&&!r.$isLineBreakNode(d))return!1}b=H(c[0]);null!=b&&T(b,a);for(b=1;b<c.length;b++)d=c[b],r.$isLineBreakNode(c[b-1])&&y(d)&&T(d,a);return!0}
21
- function T(a,c){const b=a.getTextContent();c===r.INDENT_CONTENT_COMMAND?0<b.length&&/\s/.test(b[0])?a.setTextContent("\t"+b):(c=x("\t"),a.insertBefore(c)):0===b.indexOf("\t")&&(1===b.length?a.remove():a.setTextContent(b.substring(1)))}
22
- function U(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(!y(g)||!y(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(),
23
- c.preventDefault(),!0;return!1}var p=H(g);const v=K(k);if(null==p||null==v)return!1;const A=p.getNodesBetween(v);for(let q=0;q<A.length;q++){const P=A[q];if(!y(P)&&!r.$isLineBreakNode(P))return!1}c.preventDefault();c.stopPropagation();c=l?p.getPreviousSibling():v.getNextSibling();if(!r.$isLineBreakNode(c))return!0;p=l?c.getPreviousSibling():c.getNextSibling();if(null==p)return!0;l=l?H(p):K(p);let w=null!=l?l:p;c.remove();A.forEach(q=>q.remove());a===r.KEY_ARROW_UP_COMMAND?(A.forEach(q=>w.insertBefore(q)),
24
- w.insertBefore(c)):(w.insertAfter(c),w=c,A.forEach(q=>{w.insertAfter(q);w=q}));b.setTextNodeRange(g,h,k,m);return!0}exports.$createCodeHighlightNode=x;exports.$createCodeNode=I;exports.$isCodeHighlightNode=y;exports.$isCodeNode=J;exports.CodeHighlightNode=t;exports.CodeNode=z;exports.getCodeLanguages=()=>Object.keys(e.languages).filter(a=>"function"!==typeof e.languages[a]).sort();exports.getDefaultCodeLanguage=()=>"javascript";exports.getFirstCodeHighlightNodeOfLine=H;
25
- exports.getLastCodeHighlightNodeOfLine=K;
26
- exports.registerCodeHighlighting=function(a){if(!a.hasNodes([z,t]))throw Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return n.mergeRegister(a.registerMutationListener(z,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])&&
27
- (g+="\n"+ ++k);b.setAttribute("data-gutter",g)}}})}),a.registerNodeTransform(z,c=>M(c,a)),a.registerNodeTransform(r.TextNode,c=>L(c,a)),a.registerNodeTransform(t,c=>L(c,a)),a.registerCommand(r.INDENT_CONTENT_COMMAND,()=>S(r.INDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.OUTDENT_CONTENT_COMMAND,()=>S(r.OUTDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_UP_COMMAND,c=>U(r.KEY_ARROW_UP_COMMAND,c),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_DOWN_COMMAND,
28
- c=>U(r.KEY_ARROW_DOWN_COMMAND,c),r.COMMAND_PRIORITY_LOW))};
7
+ 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
+ var n=require("@lexical/utils"),r=require("lexical");const t=a=>null!=a&&e.languages.hasOwnProperty(a)?a:void 0;
9
+ 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}}
10
+ 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}
11
+ 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");
12
+ 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()&&
13
+ 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=
14
+ 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}
15
+ 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}}
16
+ 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;
17
+ 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?
18
+ (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}
19
+ 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}))}}
20
+ 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}
21
+ 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)))}
22
+ 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(),
23
+ 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)),
24
+ 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;
25
+ exports.getLastCodeHighlightNodeOfLine=L;
26
+ 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])&&
27
+ (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,
28
+ c=>V(r.KEY_ARROW_DOWN_COMMAND,c),r.COMMAND_PRIORITY_LOW))};
package/package.json CHANGED
@@ -8,18 +8,21 @@
8
8
  "code"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.2.4",
11
+ "version": "0.2.7",
12
12
  "main": "LexicalCode.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.2.4"
14
+ "lexical": "0.2.7"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.2.4",
17
+ "@lexical/utils": "0.2.7",
18
18
  "prismjs": "^1.27.0"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",
22
22
  "url": "https://github.com/facebook/lexical",
23
23
  "directory": "packages/lexical-code"
24
+ },
25
+ "devDependencies": {
26
+ "@types/prismjs": "^1.26.0"
24
27
  }
25
28
  }