@lexical/code-core 0.44.1-nightly.20260518.0 → 0.45.0
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/dist/CodeImportExtension.d.ts +50 -0
- package/{CodeNode.d.ts → dist/CodeNode.d.ts} +1 -0
- package/{FlatStructureUtils.d.ts → dist/FlatStructureUtils.d.ts} +10 -1
- package/{LexicalCodeCore.dev.js → dist/LexicalCodeCore.dev.js} +377 -6
- package/{LexicalCodeCore.dev.mjs → dist/LexicalCodeCore.dev.mjs} +376 -8
- package/{LexicalCodeCore.js.flow → dist/LexicalCodeCore.js.flow} +2 -0
- package/{LexicalCodeCore.mjs → dist/LexicalCodeCore.mjs} +3 -0
- package/{LexicalCodeCore.node.mjs → dist/LexicalCodeCore.node.mjs} +3 -0
- package/dist/LexicalCodeCore.prod.js +9 -0
- package/dist/LexicalCodeCore.prod.mjs +9 -0
- package/{index.d.ts → dist/index.d.ts} +2 -1
- package/package.json +32 -16
- package/src/CodeExtension.ts +40 -0
- package/src/CodeHighlightNode.ts +171 -0
- package/src/CodeImportExtension.ts +403 -0
- package/src/CodeIndentation.ts +654 -0
- package/src/CodeNode.ts +503 -0
- package/src/FlatStructureUtils.ts +311 -0
- package/src/index.ts +37 -0
- package/LexicalCodeCore.prod.js +0 -9
- package/LexicalCodeCore.prod.mjs +0 -9
- /package/{CodeExtension.d.ts → dist/CodeExtension.d.ts} +0 -0
- /package/{CodeHighlightNode.d.ts → dist/CodeHighlightNode.d.ts} +0 -0
- /package/{CodeIndentation.d.ts → dist/CodeIndentation.d.ts} +0 -0
- /package/{LexicalCodeCore.js → dist/LexicalCodeCore.js} +0 -0
|
@@ -0,0 +1,311 @@
|
|
|
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 {CodeHighlightNode} from './CodeHighlightNode';
|
|
10
|
+
import type {
|
|
11
|
+
CaretDirection,
|
|
12
|
+
LexicalNode,
|
|
13
|
+
LineBreakNode,
|
|
14
|
+
RangeSelection,
|
|
15
|
+
SiblingCaret,
|
|
16
|
+
TabNode,
|
|
17
|
+
} from 'lexical';
|
|
18
|
+
|
|
19
|
+
import invariant from '@lexical/internal/invariant';
|
|
20
|
+
import {
|
|
21
|
+
$createLineBreakNode,
|
|
22
|
+
$createTabNode,
|
|
23
|
+
$getSiblingCaret,
|
|
24
|
+
$isElementNode,
|
|
25
|
+
$isLineBreakNode,
|
|
26
|
+
$isTabNode,
|
|
27
|
+
getTextDirection,
|
|
28
|
+
} from 'lexical';
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
$createCodeHighlightNode,
|
|
32
|
+
$isCodeHighlightNode,
|
|
33
|
+
} from './CodeHighlightNode';
|
|
34
|
+
|
|
35
|
+
function $getLastMatchingCodeNode<D extends CaretDirection>(
|
|
36
|
+
anchor: CodeHighlightNode | TabNode | LineBreakNode,
|
|
37
|
+
direction: D,
|
|
38
|
+
): CodeHighlightNode | TabNode | LineBreakNode {
|
|
39
|
+
let matchingNode: CodeHighlightNode | TabNode | LineBreakNode = anchor;
|
|
40
|
+
for (
|
|
41
|
+
let caret: null | SiblingCaret<LexicalNode, D> = $getSiblingCaret(
|
|
42
|
+
anchor,
|
|
43
|
+
direction,
|
|
44
|
+
);
|
|
45
|
+
caret && ($isCodeHighlightNode(caret.origin) || $isTabNode(caret.origin));
|
|
46
|
+
caret = caret.getAdjacentCaret()
|
|
47
|
+
) {
|
|
48
|
+
matchingNode = caret.origin;
|
|
49
|
+
}
|
|
50
|
+
return matchingNode;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function $getFirstCodeNodeOfLine(
|
|
54
|
+
anchor: CodeHighlightNode | TabNode | LineBreakNode,
|
|
55
|
+
): CodeHighlightNode | TabNode | LineBreakNode {
|
|
56
|
+
return $getLastMatchingCodeNode(anchor, 'previous');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function $getLastCodeNodeOfLine(
|
|
60
|
+
anchor: CodeHighlightNode | TabNode | LineBreakNode,
|
|
61
|
+
): CodeHighlightNode | TabNode | LineBreakNode {
|
|
62
|
+
return $getLastMatchingCodeNode(anchor, 'next');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Determines the visual writing direction of a code line.
|
|
67
|
+
*
|
|
68
|
+
* Scans the line segments (CodeHighlightNode/TabNode) from start to end
|
|
69
|
+
* and returns the first strong direction found ("ltr" or "rtl").
|
|
70
|
+
* If no strong character is found, falls back to the parent element's
|
|
71
|
+
* direction. Returns null if indeterminate.
|
|
72
|
+
*/
|
|
73
|
+
export function $getCodeLineDirection(
|
|
74
|
+
anchor: CodeHighlightNode | TabNode | LineBreakNode,
|
|
75
|
+
): 'ltr' | 'rtl' | null {
|
|
76
|
+
const start = $getFirstCodeNodeOfLine(anchor);
|
|
77
|
+
const end = $getLastCodeNodeOfLine(anchor);
|
|
78
|
+
let node: null | LexicalNode = start;
|
|
79
|
+
|
|
80
|
+
while (node !== null) {
|
|
81
|
+
if ($isCodeHighlightNode(node)) {
|
|
82
|
+
const direction = getTextDirection(node.getTextContent());
|
|
83
|
+
if (direction !== null) {
|
|
84
|
+
return direction;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (node === end) {
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
node = node.getNextSibling();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const parent = start.getParent();
|
|
96
|
+
if ($isElementNode(parent)) {
|
|
97
|
+
const parentDirection = parent.getDirection();
|
|
98
|
+
if (parentDirection === 'ltr' || parentDirection === 'rtl') {
|
|
99
|
+
return parentDirection;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function $getStartOfCodeInLine(
|
|
107
|
+
anchor: CodeHighlightNode | TabNode,
|
|
108
|
+
offset: number,
|
|
109
|
+
): null | {
|
|
110
|
+
node: CodeHighlightNode | TabNode | LineBreakNode;
|
|
111
|
+
offset: number;
|
|
112
|
+
} {
|
|
113
|
+
let last: null | {
|
|
114
|
+
node: CodeHighlightNode | TabNode | LineBreakNode;
|
|
115
|
+
offset: number;
|
|
116
|
+
} = null;
|
|
117
|
+
let lastNonBlank: null | {node: CodeHighlightNode; offset: number} = null;
|
|
118
|
+
let node: null | CodeHighlightNode | TabNode | LineBreakNode = anchor;
|
|
119
|
+
let nodeOffset = offset;
|
|
120
|
+
let nodeTextContent = anchor.getTextContent();
|
|
121
|
+
|
|
122
|
+
while (true) {
|
|
123
|
+
if (nodeOffset === 0) {
|
|
124
|
+
node = node.getPreviousSibling();
|
|
125
|
+
if (node === null) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
invariant(
|
|
129
|
+
$isCodeHighlightNode(node) ||
|
|
130
|
+
$isTabNode(node) ||
|
|
131
|
+
$isLineBreakNode(node),
|
|
132
|
+
'Expected a valid Code Node: CodeHighlightNode, TabNode, LineBreakNode',
|
|
133
|
+
);
|
|
134
|
+
if ($isLineBreakNode(node)) {
|
|
135
|
+
last = {
|
|
136
|
+
node,
|
|
137
|
+
offset: 1,
|
|
138
|
+
};
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
nodeOffset = Math.max(0, node.getTextContentSize() - 1);
|
|
142
|
+
nodeTextContent = node.getTextContent();
|
|
143
|
+
} else {
|
|
144
|
+
nodeOffset--;
|
|
145
|
+
}
|
|
146
|
+
const character = nodeTextContent[nodeOffset];
|
|
147
|
+
if ($isCodeHighlightNode(node) && character !== ' ') {
|
|
148
|
+
lastNonBlank = {
|
|
149
|
+
node,
|
|
150
|
+
offset: nodeOffset,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// lastNonBlank !== null: anchor in the middle of code; move to line beginning
|
|
155
|
+
if (lastNonBlank !== null) {
|
|
156
|
+
return lastNonBlank;
|
|
157
|
+
}
|
|
158
|
+
// Spaces, tabs or nothing ahead of anchor
|
|
159
|
+
let codeCharacterAtAnchorOffset = null;
|
|
160
|
+
if (offset < anchor.getTextContentSize()) {
|
|
161
|
+
if ($isCodeHighlightNode(anchor)) {
|
|
162
|
+
codeCharacterAtAnchorOffset = anchor.getTextContent()[offset];
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
const nextSibling = anchor.getNextSibling();
|
|
166
|
+
if ($isCodeHighlightNode(nextSibling)) {
|
|
167
|
+
codeCharacterAtAnchorOffset = nextSibling.getTextContent()[0];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (
|
|
171
|
+
codeCharacterAtAnchorOffset !== null &&
|
|
172
|
+
codeCharacterAtAnchorOffset !== ' '
|
|
173
|
+
) {
|
|
174
|
+
// Borderline whitespace and code, move to line beginning
|
|
175
|
+
return last;
|
|
176
|
+
} else {
|
|
177
|
+
const nextNonBlank = findNextNonBlankInLine(anchor, offset);
|
|
178
|
+
if (nextNonBlank !== null) {
|
|
179
|
+
return nextNonBlank;
|
|
180
|
+
} else {
|
|
181
|
+
return last;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function findNextNonBlankInLine(
|
|
187
|
+
anchor: LexicalNode,
|
|
188
|
+
offset: number,
|
|
189
|
+
): null | {node: CodeHighlightNode; offset: number} {
|
|
190
|
+
let node: null | LexicalNode = anchor;
|
|
191
|
+
let nodeOffset = offset;
|
|
192
|
+
let nodeTextContent = anchor.getTextContent();
|
|
193
|
+
let nodeTextContentSize = anchor.getTextContentSize();
|
|
194
|
+
|
|
195
|
+
while (true) {
|
|
196
|
+
if (!$isCodeHighlightNode(node) || nodeOffset === nodeTextContentSize) {
|
|
197
|
+
node = node.getNextSibling();
|
|
198
|
+
if (node === null || $isLineBreakNode(node)) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
if ($isCodeHighlightNode(node)) {
|
|
202
|
+
nodeOffset = 0;
|
|
203
|
+
nodeTextContent = node.getTextContent();
|
|
204
|
+
nodeTextContentSize = node.getTextContentSize();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if ($isCodeHighlightNode(node)) {
|
|
208
|
+
if (nodeTextContent[nodeOffset] !== ' ') {
|
|
209
|
+
return {
|
|
210
|
+
node,
|
|
211
|
+
offset: nodeOffset,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
nodeOffset++;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function $getEndOfCodeInLine(
|
|
220
|
+
anchor: CodeHighlightNode | TabNode,
|
|
221
|
+
): CodeHighlightNode | TabNode {
|
|
222
|
+
const lastNode = $getLastCodeNodeOfLine(anchor);
|
|
223
|
+
invariant(
|
|
224
|
+
!$isLineBreakNode(lastNode),
|
|
225
|
+
'Unexpected lineBreakNode in getEndOfCodeInLine',
|
|
226
|
+
);
|
|
227
|
+
return lastNode;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Plain split of code text into CodeHighlightNodes (with no highlight
|
|
232
|
+
* type) + LineBreakNodes + TabNodes. Used when the tokenizer opts out
|
|
233
|
+
* of a default language so a previously highlighted block still
|
|
234
|
+
* renders its `\n` / `\t` as real line breaks / tabs, while staying
|
|
235
|
+
* compatible with the indent / shift-lines handlers that only accept
|
|
236
|
+
* CodeHighlightNode + TabNode + LineBreakNode inside a CodeNode.
|
|
237
|
+
*/
|
|
238
|
+
export function $plainifyCodeContent(text: string): LexicalNode[] {
|
|
239
|
+
const out: LexicalNode[] = [];
|
|
240
|
+
const lines = text.split('\n');
|
|
241
|
+
lines.forEach((line, lineIdx) => {
|
|
242
|
+
if (lineIdx > 0) {
|
|
243
|
+
out.push($createLineBreakNode());
|
|
244
|
+
}
|
|
245
|
+
const tabParts = line.split('\t');
|
|
246
|
+
tabParts.forEach((part, partIdx) => {
|
|
247
|
+
if (partIdx > 0) {
|
|
248
|
+
out.push($createTabNode());
|
|
249
|
+
}
|
|
250
|
+
if (part.length > 0) {
|
|
251
|
+
out.push($createCodeHighlightNode(part));
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
return out;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Strip up to `tabSize` leading spaces from a {@link CodeHighlightNode} that
|
|
260
|
+
* starts a code line, to support outdenting space-indented code lines (e.g.
|
|
261
|
+
* code formatted with prettier). Returns true if any spaces were stripped.
|
|
262
|
+
*
|
|
263
|
+
* Best-effort: a line with fewer than `tabSize` leading spaces has all of
|
|
264
|
+
* them stripped, matching VS Code / IntelliJ behavior.
|
|
265
|
+
*
|
|
266
|
+
* Selection is preserved relative to line content. Anchor/focus offsets
|
|
267
|
+
* pointing into `node` shift left by the number of stripped characters
|
|
268
|
+
* (clamped to 0). The underlying TextNode mutation does not adjust
|
|
269
|
+
* selection offsets that already point into the old text, so we patch
|
|
270
|
+
* them up explicitly.
|
|
271
|
+
*/
|
|
272
|
+
export function $outdentLeadingSpaces(
|
|
273
|
+
node: CodeHighlightNode,
|
|
274
|
+
tabSize: number,
|
|
275
|
+
selection: RangeSelection,
|
|
276
|
+
): boolean {
|
|
277
|
+
if (!Number.isInteger(tabSize) || tabSize <= 0) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
const text = node.getTextContent();
|
|
281
|
+
const leading = /^ +/.exec(text);
|
|
282
|
+
if (!leading) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
const stripCount = Math.min(tabSize, leading[0].length);
|
|
286
|
+
const lineKey = node.getKey();
|
|
287
|
+
const oldAnchorOffset =
|
|
288
|
+
selection.anchor.key === lineKey && selection.anchor.type === 'text'
|
|
289
|
+
? selection.anchor.offset
|
|
290
|
+
: null;
|
|
291
|
+
const oldFocusOffset =
|
|
292
|
+
selection.focus.key === lineKey && selection.focus.type === 'text'
|
|
293
|
+
? selection.focus.offset
|
|
294
|
+
: null;
|
|
295
|
+
node.spliceText(0, stripCount, '');
|
|
296
|
+
if (oldAnchorOffset !== null) {
|
|
297
|
+
selection.anchor.set(
|
|
298
|
+
lineKey,
|
|
299
|
+
Math.max(0, oldAnchorOffset - stripCount),
|
|
300
|
+
'text',
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
if (oldFocusOffset !== null) {
|
|
304
|
+
selection.focus.set(
|
|
305
|
+
lineKey,
|
|
306
|
+
Math.max(0, oldFocusOffset - stripCount),
|
|
307
|
+
'text',
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
return true;
|
|
311
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
export {CodeExtension} from './CodeExtension';
|
|
10
|
+
export {
|
|
11
|
+
$createCodeHighlightNode,
|
|
12
|
+
$isCodeHighlightNode,
|
|
13
|
+
CodeHighlightNode,
|
|
14
|
+
} from './CodeHighlightNode';
|
|
15
|
+
export {CodeImportExtension, CodeImportRules} from './CodeImportExtension';
|
|
16
|
+
export {
|
|
17
|
+
type CodeIndentConfig,
|
|
18
|
+
CodeIndentExtension,
|
|
19
|
+
registerCodeIndentation,
|
|
20
|
+
} from './CodeIndentation';
|
|
21
|
+
export type {SerializedCodeNode} from './CodeNode';
|
|
22
|
+
export {
|
|
23
|
+
$createCodeNode,
|
|
24
|
+
$isCodeNode,
|
|
25
|
+
CodeNode,
|
|
26
|
+
DEFAULT_CODE_LANGUAGE,
|
|
27
|
+
getDefaultCodeLanguage,
|
|
28
|
+
} from './CodeNode';
|
|
29
|
+
export {
|
|
30
|
+
$getCodeLineDirection,
|
|
31
|
+
$getEndOfCodeInLine,
|
|
32
|
+
$getFirstCodeNodeOfLine,
|
|
33
|
+
$getLastCodeNodeOfLine,
|
|
34
|
+
$getStartOfCodeInLine,
|
|
35
|
+
$outdentLeadingSpaces,
|
|
36
|
+
$plainifyCodeContent,
|
|
37
|
+
} from './FlatStructureUtils';
|
package/LexicalCodeCore.prod.js
DELETED
|
@@ -1,9 +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
|
-
"use strict";var e=require("lexical"),t=require("@lexical/extension");function n(e,...t){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",e);for(const e of t)r.append("v",e);throw n.search=r.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function r(t,n){let r=t;for(let i=e.$getSiblingCaret(t,n);i&&(A(i.origin)||e.$isTabNode(i.origin));i=i.getAdjacentCaret())r=i.origin;return r}function i(e){return r(e,"previous")}function o(e){return r(e,"next")}function s(t){const n=i(t),r=o(t);let s=n;for(;null!==s;){if(A(s)){const t=e.getTextDirection(s.getTextContent());if(null!==t)return t}if(s===r)break;s=s.getNextSibling()}const l=n.getParent();if(e.$isElementNode(l)){const e=l.getDirection();if("ltr"===e||"rtl"===e)return e}return null}function l(t,r){let i=null,o=null,s=t,l=r,a=t.getTextContent();for(;;){if(0===l){if(s=s.getPreviousSibling(),null===s)break;if(A(s)||e.$isTabNode(s)||e.$isLineBreakNode(s)||n(167),e.$isLineBreakNode(s)){i={node:s,offset:1};break}l=Math.max(0,s.getTextContentSize()-1),a=s.getTextContent()}else l--;const t=a[l];A(s)&&" "!==t&&(o={node:s,offset:l})}if(null!==o)return o;let g=null;if(r<t.getTextContentSize())A(t)&&(g=t.getTextContent()[r]);else{const e=t.getNextSibling();A(e)&&(g=e.getTextContent()[0])}if(null!==g&&" "!==g)return i;{const n=function(t,n){let r=t,i=n,o=t.getTextContent(),s=t.getTextContentSize();for(;;){if(!A(r)||i===s){if(r=r.getNextSibling(),null===r||e.$isLineBreakNode(r))return null;A(r)&&(i=0,o=r.getTextContent(),s=r.getTextContentSize())}if(A(r)){if(" "!==o[i])return{node:r,offset:i};i++}}}(t,r);return null!==n?n:i}}function a(t){const r=o(t);return e.$isLineBreakNode(r)&&n(168),r}function g(e,t,n){if(!Number.isInteger(t)||t<=0)return!1;const r=e.getTextContent(),i=/^ +/.exec(r);if(!i)return!1;const o=Math.min(t,i[0].length),s=e.getKey(),l=n.anchor.key===s&&"text"===n.anchor.type?n.anchor.offset:null,a=n.focus.key===s&&"text"===n.focus.type?n.focus.offset:null;return e.spliceText(0,o,""),null!==l&&n.anchor.set(s,Math.max(0,l-o),"text"),null!==a&&n.focus.set(s,Math.max(0,a-o),"text"),!0}const u="javascript";function c(t,n){for(const r of t.childNodes){if(e.isHTMLElement(r)&&r.tagName===n)return!0;if(c(r,n))return!0}return!1}const d="data-language",h="data-highlight-language",f="data-theme",N=()=>{};class _ extends e.ElementNode{__language;__theme;__isSyntaxHighlightSupported;static getType(){return"code"}static clone(e){return new _(e.__language,e.__key)}constructor(e,t){super(t),this.__language=e||void 0,this.__isSyntaxHighlightSupported=!1,this.__theme=void 0}afterCloneFrom(e){super.afterCloneFrom(e),this.__language=e.__language,this.__theme=e.__theme,this.__isSyntaxHighlightSupported=e.__isSyntaxHighlightSupported}createDOM(t){const n=document.createElement("code");e.addClassNamesToElement(n,t.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();r&&(n.setAttribute(d,r),this.getIsSyntaxHighlightSupported()&&n.setAttribute(h,r));const i=this.getTheme();i&&n.setAttribute(f,i);const o=this.getStyle();return o&&e.setDOMStyleFromCSS(n.style,o),n}updateDOM(t,n,r){const i=this.__language,o=t.__language;i?i!==o&&n.setAttribute(d,i):o&&n.removeAttribute(d);const s=this.__isSyntaxHighlightSupported;t.__isSyntaxHighlightSupported&&o?s&&i?i!==o&&n.setAttribute(h,i):n.removeAttribute(h):s&&i&&n.setAttribute(h,i);const l=this.__theme,a=t.__theme;l?l!==a&&n.setAttribute(f,l):a&&n.removeAttribute(f);const g=this.__style,u=t.__style;return g!==u&&e.setDOMStyleFromCSS(n.style,g,u),!1}exportDOM(t){const n=document.createElement("pre");e.addClassNamesToElement(n,t._config.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();r&&(n.setAttribute(d,r),this.getIsSyntaxHighlightSupported()&&n.setAttribute(h,r));const i=this.getTheme();i&&n.setAttribute(f,i);const o=this.getStyle();return o&&e.setDOMStyleFromCSS(n.style,o),{element:n}}static importDOM(){return{code:e=>null!=e.textContent&&(/\r?\n/.test(e.textContent)||c(e,"BR"))?{conversion:C,priority:1}:null,div:()=>({conversion:O,priority:1}),pre:()=>({conversion:C,priority:0}),table:e=>M(e)?{conversion:m,priority:3}:null,td:e=>{const t=e,n=t.closest("table");return t.classList.contains("js-file-line")||n&&M(n)?{conversion:x,priority:3}:null},tr:e=>{const t=e.closest("table");return t&&M(t)?{conversion:x,priority:3}:null}}}static importJSON(e){return p().updateFromJSON(e)}updateFromJSON(e){return super.updateFromJSON(e).setLanguage(e.language).setTheme(e.theme)}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),theme:this.getTheme()}}insertNewAfter(n,r=!0){if(!t.getPeerDependencyFromEditor(e.$getEditor(),"@lexical/code")){N();const e=$(n);if(e)return e}const{anchor:o,focus:s}=n,l=(o.isBefore(s)?o:s).getNode();if(e.$isTextNode(l)){let t=i(l);const n=[];for(;;)if(e.$isTabNode(t))n.push(e.$createTabNode()),t=t.getNextSibling();else{if(!A(t))break;{let e=0;const r=t.getTextContent(),i=t.getTextContentSize();for(;e<i&&" "===r[e];)e++;if(0!==e&&n.push(E(" ".repeat(e))),e!==i)break;t=t.getNextSibling()}}const r=l.splitText(o.offset)[0],s=0===o.offset?0:1,a=r.getIndexWithinParent()+s,g=l.getParentOrThrow(),u=[e.$createLineBreakNode(),...n];g.splice(a,0,u);const c=n[n.length-1];c?c.select():0===o.offset?r.selectPrevious():r.getNextSibling().selectNext(0,0)}if(T(l)){const{offset:t}=n.anchor;l.splice(t,0,[e.$createLineBreakNode()]),l.select(t+1,t+1)}return null}canIndent(){return!1}collapseAtStart(){const t=e.$createParagraphNode();return this.getChildren().forEach(e=>t.append(e)),this.replace(t),!0}setLanguage(e){const t=this.getWritable();return t.__language=e||void 0,t}getLanguage(){return this.getLatest().__language}setIsSyntaxHighlightSupported(e){const t=this.getWritable();return t.__isSyntaxHighlightSupported=e,t}getIsSyntaxHighlightSupported(){return this.getLatest().__isSyntaxHighlightSupported}setTheme(e){const t=this.getWritable();return t.__theme=e||void 0,t}getTheme(){return this.getLatest().__theme}}function p(t,n){return e.$create(_).setLanguage(t).setTheme(n)}function T(e){return e instanceof _}function C(e){return{node:p(e.getAttribute(d))}}function O(e){const t=e,n=S(t);return n||function(e){let t=e.parentElement;for(;null!==t;){if(S(t))return!0;t=t.parentElement}return!1}(t)?{node:n?p():null}:{node:null}}function m(){return{node:p()}}function x(){return{node:null}}function S(e){return null!==e.style.fontFamily.match("monospace")}function M(e){return e.classList.contains("js-file-line-container")}function $(t){const{anchor:n}=t;if(t.isCollapsed()&&"element"===n.type){const t=n.getNode();if(T(t)){const r=t.getChildrenSize();if(r>=2&&n.offset===r){const n=t.getLastChild();if(e.$isLineBreakNode(n)&&e.$isLineBreakNode(n.getPreviousSibling())){const n=e.$createParagraphNode();return t.splice(r-2,2,[]).insertAfter(n,!1),n.select(),n}}}}return null}class b extends e.TextNode{__highlightType;constructor(e="",t,n){super(e,n),this.__highlightType=t}static getType(){return"code-highlight"}static clone(e){return new b(e.__text,e.__highlightType||void 0,e.__key)}afterCloneFrom(e){super.afterCloneFrom(e),this.__highlightType=e.__highlightType}getHighlightType(){return this.getLatest().__highlightType}setHighlightType(e){const t=this.getWritable();return t.__highlightType=e||void 0,t}canHaveFormat(){return!1}createDOM(t){const n=super.createDOM(t),r=D(t.theme,this.__highlightType);return e.addClassNamesToElement(n,r),n}updateDOM(t,n,r){const i=super.updateDOM(t,n,r),o=D(r.theme,t.__highlightType),s=D(r.theme,this.__highlightType);return o!==s&&(o&&e.removeClassNamesFromElement(n,o),s&&e.addClassNamesToElement(n,s)),i}static importJSON(e){return E().updateFromJSON(e)}updateFromJSON(e){return super.updateFromJSON(e).setHighlightType(e.highlightType)}exportJSON(){return{...super.exportJSON(),highlightType:this.getHighlightType()}}setFormat(e){return this}isParentRequired(){return!0}createParentElementNode(){return p()}}function D(e,t){return t&&e&&e.codeHighlight&&e.codeHighlight[t]}function E(t="",n){return e.$applyNodeReplacement(new b(t,n))}function A(e){return e instanceof b}const y=e.defineExtension({name:"@lexical/code",nodes:()=>[_,b],register:t=>t.registerCommand(e.KEY_ENTER_COMMAND,t=>{const n=e.$getSelection();return!(!e.$isRangeSelection(n)||!$(n))&&(t.preventDefault(),!0)},e.COMMAND_PRIORITY_LOW)});function R(t){if(!e.$isRangeSelection(t))return!1;const n=t.anchor.getNode(),r=T(n)?n:n.getParent(),i=t.focus.getNode(),o=T(i)?i:i.getParent();return T(r)&&r.is(o)}function v(t){const r=t.getNodes(),i=[];if(1===r.length&&T(r[0]))return i;let o=[];for(let t=0;t<r.length;t++){const s=r[t];A(s)||e.$isTabNode(s)||e.$isLineBreakNode(s)||n(169),e.$isLineBreakNode(s)?o.length>0&&(i.push(o),o=[]):o.push(s)}if(o.length>0){const n=t.isBackward()?t.anchor:t.focus,r=e.$createPoint(o[0].getKey(),0,"text");n.is(r)||i.push(o)}return i}function L(t,n){const r=e.$getSelection();if(!e.$isRangeSelection(r)||!R(r))return!1;const o=v(r),s=o.length;if(0===s&&r.isCollapsed())return t===e.INDENT_CONTENT_COMMAND&&r.insertNodes([e.$createTabNode()]),!0;if(0===s&&t===e.INDENT_CONTENT_COMMAND&&"\n"===r.getTextContent()){const t=e.$createTabNode(),n=e.$createLineBreakNode(),i=r.isBackward()?"previous":"next";return r.insertNodes([t,n]),e.$setSelectionFromCaretRange(e.$getCaretRangeInDirection(e.$getCaretRange(e.$getTextPointCaret(t,"next",0),e.$normalizeCaret(e.$getSiblingCaret(n,"next"))),i)),!0}for(let l=0;l<s;l++){const s=o[l];if(s.length>0){let o=s[0];if(0===l&&(o=i(o)),t===e.INDENT_CONTENT_COMMAND){const t=e.$createTabNode();if(o.insertBefore(t),0===l){const n=r.isBackward()?"focus":"anchor",i=e.$createPoint(o.getKey(),0,"text");r[n].is(i)&&r[n].set(t.getKey(),0,"text")}}else e.$isTabNode(o)?o.remove():void 0!==n&&A(o)&&g(o,n,r)}}return!0}function P(t,n){const r=e.$getSelection();if(!e.$isRangeSelection(r))return!1;const{anchor:s,focus:l}=r,a=s.offset,g=l.offset,u=s.getNode(),c=l.getNode(),d=t===e.KEY_ARROW_UP_COMMAND;if(!R(r)||!A(u)&&!e.$isTabNode(u)||!A(c)&&!e.$isTabNode(c))return!1;if(!n.altKey){if(r.isCollapsed()){const e=u.getParentOrThrow();if(d&&0===a&&null===u.getPreviousSibling()){if(null===e.getPreviousSibling())return e.selectPrevious(),n.preventDefault(),!0}else if(!d&&a===u.getTextContentSize()&&null===u.getNextSibling()){if(null===e.getNextSibling())return e.selectNext(),n.preventDefault(),!0}}return!1}let h,f;if(u.isBefore(c)?(h=i(u),f=o(c)):(h=i(c),f=o(u)),null==h||null==f)return!1;const N=h.getNodesBetween(f);for(let t=0;t<N.length;t++){const n=N[t];if(!A(n)&&!e.$isTabNode(n)&&!e.$isLineBreakNode(n))return!1}n.preventDefault(),n.stopPropagation();const _=d?h.getPreviousSibling():f.getNextSibling();if(!e.$isLineBreakNode(_))return!0;const p=d?_.getPreviousSibling():_.getNextSibling();if(null==p)return!0;const T=A(p)||e.$isTabNode(p)||e.$isLineBreakNode(p)?d?i(p):o(p):null;let C=null!=T?T:p;return _.remove(),N.forEach(e=>e.remove()),t===e.KEY_ARROW_UP_COMMAND?(N.forEach(e=>C.insertBefore(e)),C.insertBefore(_)):(C.insertAfter(_),C=_,N.forEach(e=>{C.insertAfter(e),C=e})),r.setTextNodeRange(u,a,c,g),!0}function I(t,n){const r=e.$getSelection();if(!e.$isRangeSelection(r))return!1;const{anchor:i,focus:o}=r,g=i.getNode(),u=o.getNode(),c=t===e.MOVE_TO_START;if(!R(r)||!A(g)&&!e.$isTabNode(g)||!A(u)&&!e.$isTabNode(u))return!1;const d=u;if("rtl"===s(d)?!c:c){const t=l(d,o.offset);if(null!==t){const{node:n,offset:i}=t;e.$isLineBreakNode(n)?n.selectNext(0,0):r.setTextNodeRange(n,i,n,i)}else d.getParentOrThrow().selectStart()}else{a(d).select()}return n.preventDefault(),n.stopPropagation(),!0}function B(t,r){return e.mergeRegister(t.registerCommand(e.KEY_TAB_COMMAND,r=>{const s=function(t){const r=e.$getSelection();if(!e.$isRangeSelection(r)||!R(r))return null;const s=t?e.OUTDENT_CONTENT_COMMAND:e.INDENT_CONTENT_COMMAND,l=t?e.OUTDENT_CONTENT_COMMAND:e.INSERT_TAB_COMMAND,a=r.anchor,g=r.focus;if(a.is(g))return l;const u=v(r);if(1!==u.length)return s;const c=u[0];let d,h;0===c.length&&n(285),r.isBackward()?(d=g,h=a):(d=a,h=g);const f=i(c[0]),N=o(c[0]),_=e.$createPoint(f.getKey(),0,"text"),p=e.$createPoint(N.getKey(),N.getTextContentSize(),"text");return d.isBefore(_)||p.isBefore(h)?s:_.isBefore(d)||h.isBefore(p)?l:s}(r.shiftKey);return null!==s&&(r.preventDefault(),t.dispatchCommand(s,void 0),!0)},e.COMMAND_PRIORITY_LOW),t.registerCommand(e.INSERT_TAB_COMMAND,()=>!!R(e.$getSelection())&&(e.$insertNodes([e.$createTabNode()]),!0),e.COMMAND_PRIORITY_LOW),t.registerCommand(e.INDENT_CONTENT_COMMAND,()=>L(e.INDENT_CONTENT_COMMAND),e.COMMAND_PRIORITY_LOW),t.registerCommand(e.OUTDENT_CONTENT_COMMAND,()=>L(e.OUTDENT_CONTENT_COMMAND,r),e.COMMAND_PRIORITY_LOW),t.registerCommand(e.KEY_ARROW_UP_COMMAND,t=>{const n=e.$getSelection();if(!e.$isRangeSelection(n))return!1;const{anchor:r}=n,i=r.getNode();return!!R(n)&&(n.isCollapsed()&&0===r.offset&&null===i.getPreviousSibling()&&T(i.getParentOrThrow())?(t.preventDefault(),!0):P(e.KEY_ARROW_UP_COMMAND,t))},e.COMMAND_PRIORITY_LOW),t.registerCommand(e.KEY_ARROW_DOWN_COMMAND,t=>{const n=e.$getSelection();if(!e.$isRangeSelection(n))return!1;const{anchor:r}=n,i=r.getNode();return!!R(n)&&(n.isCollapsed()&&r.offset===i.getTextContentSize()&&null===i.getNextSibling()&&T(i.getParentOrThrow())?(t.preventDefault(),!0):P(e.KEY_ARROW_DOWN_COMMAND,t))},e.COMMAND_PRIORITY_LOW),t.registerCommand(e.MOVE_TO_START,t=>I(e.MOVE_TO_START,t),e.COMMAND_PRIORITY_LOW),t.registerCommand(e.MOVE_TO_END,t=>I(e.MOVE_TO_END,t),e.COMMAND_PRIORITY_LOW))}const k=e.defineExtension({build:(e,n)=>t.namedSignals(n),config:e.safeCast({disabled:!1,tabSize:void 0}),dependencies:[y],name:"@lexical/code-indent",register:(e,n,r)=>{const i=r.getOutput();return t.effect(()=>{if(!i.disabled.value)return B(e,i.tabSize.value)})}});exports.$createCodeHighlightNode=E,exports.$createCodeNode=p,exports.$getCodeLineDirection=s,exports.$getEndOfCodeInLine=a,exports.$getFirstCodeNodeOfLine=i,exports.$getLastCodeNodeOfLine=o,exports.$getStartOfCodeInLine=l,exports.$isCodeHighlightNode=A,exports.$isCodeNode=T,exports.$outdentLeadingSpaces=g,exports.CodeExtension=y,exports.CodeHighlightNode=b,exports.CodeIndentExtension=k,exports.CodeNode=_,exports.DEFAULT_CODE_LANGUAGE=u,exports.getDefaultCodeLanguage=()=>u,exports.registerCodeIndentation=B;
|
package/LexicalCodeCore.prod.mjs
DELETED
|
@@ -1,9 +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{getTextDirection as t,$isElementNode as e,$isTabNode as n,$isLineBreakNode as r,$getSiblingCaret as i,$create as o,ElementNode as s,addClassNamesToElement as l,setDOMStyleFromCSS as u,$getEditor as g,$isTextNode as c,$createTabNode as a,$createLineBreakNode as h,$createParagraphNode as f,isHTMLElement as p,$applyNodeReplacement as d,TextNode as m,removeClassNamesFromElement as x,defineExtension as _,KEY_ENTER_COMMAND as S,$getSelection as y,$isRangeSelection as b,COMMAND_PRIORITY_LOW as T,safeCast as v,mergeRegister as N,KEY_TAB_COMMAND as C,INSERT_TAB_COMMAND as O,$insertNodes as P,INDENT_CONTENT_COMMAND as A,OUTDENT_CONTENT_COMMAND as H,KEY_ARROW_UP_COMMAND as w,KEY_ARROW_DOWN_COMMAND as D,MOVE_TO_START as L,MOVE_TO_END as k,$createPoint as B,$setSelectionFromCaretRange as F,$getCaretRangeInDirection as M,$getCaretRange as J,$getTextPointCaret as z,$normalizeCaret as E}from"lexical";import{getPeerDependencyFromEditor as K,effect as I,namedSignals as R}from"@lexical/extension";function W(t,...e){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",t);for(const t of e)r.append("v",t);throw n.search=r.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function j(t,e){let r=t;for(let o=i(t,e);o&&(xt(o.origin)||n(o.origin));o=o.getAdjacentCaret())r=o.origin;return r}function U(t){return j(t,"previous")}function $(t){return j(t,"next")}function q(n){const r=U(n),i=$(n);let o=r;for(;null!==o;){if(xt(o)){const e=t(o.getTextContent());if(null!==e)return e}if(o===i)break;o=o.getNextSibling()}const s=r.getParent();if(e(s)){const t=s.getDirection();if("ltr"===t||"rtl"===t)return t}return null}function G(t,e){let i=null,o=null,s=t,l=e,u=t.getTextContent();for(;;){if(0===l){if(s=s.getPreviousSibling(),null===s)break;if(xt(s)||n(s)||r(s)||W(167),r(s)){i={node:s,offset:1};break}l=Math.max(0,s.getTextContentSize()-1),u=s.getTextContent()}else l--;const t=u[l];xt(s)&&" "!==t&&(o={node:s,offset:l})}if(null!==o)return o;let g=null;if(e<t.getTextContentSize())xt(t)&&(g=t.getTextContent()[e]);else{const e=t.getNextSibling();xt(e)&&(g=e.getTextContent()[0])}if(null!==g&&" "!==g)return i;{const n=function(t,e){let n=t,i=e,o=t.getTextContent(),s=t.getTextContentSize();for(;;){if(!xt(n)||i===s){if(n=n.getNextSibling(),null===n||r(n))return null;xt(n)&&(i=0,o=n.getTextContent(),s=n.getTextContentSize())}if(xt(n)){if(" "!==o[i])return{node:n,offset:i};i++}}}(t,e);return null!==n?n:i}}function Q(t){const e=$(t);return r(e)&&W(168),e}function V(t,e,n){if(!Number.isInteger(e)||e<=0)return!1;const r=t.getTextContent(),i=/^ +/.exec(r);if(!i)return!1;const o=Math.min(e,i[0].length),s=t.getKey(),l=n.anchor.key===s&&"text"===n.anchor.type?n.anchor.offset:null,u=n.focus.key===s&&"text"===n.focus.type?n.focus.offset:null;return t.spliceText(0,o,""),null!==l&&n.anchor.set(s,Math.max(0,l-o),"text"),null!==u&&n.focus.set(s,Math.max(0,u-o),"text"),!0}const X="javascript",Y=()=>X;function Z(t,e){for(const n of t.childNodes){if(p(n)&&n.tagName===e)return!0;if(Z(n,e))return!0}return!1}const tt="data-language",et="data-highlight-language",nt="data-theme",rt=()=>{};class it extends s{__language;__theme;__isSyntaxHighlightSupported;static getType(){return"code"}static clone(t){return new it(t.__language,t.__key)}constructor(t,e){super(e),this.__language=t||void 0,this.__isSyntaxHighlightSupported=!1,this.__theme=void 0}afterCloneFrom(t){super.afterCloneFrom(t),this.__language=t.__language,this.__theme=t.__theme,this.__isSyntaxHighlightSupported=t.__isSyntaxHighlightSupported}createDOM(t){const e=document.createElement("code");l(e,t.theme.code),e.setAttribute("spellcheck","false");const n=this.getLanguage();n&&(e.setAttribute(tt,n),this.getIsSyntaxHighlightSupported()&&e.setAttribute(et,n));const r=this.getTheme();r&&e.setAttribute(nt,r);const i=this.getStyle();return i&&u(e.style,i),e}updateDOM(t,e,n){const r=this.__language,i=t.__language;r?r!==i&&e.setAttribute(tt,r):i&&e.removeAttribute(tt);const o=this.__isSyntaxHighlightSupported;t.__isSyntaxHighlightSupported&&i?o&&r?r!==i&&e.setAttribute(et,r):e.removeAttribute(et):o&&r&&e.setAttribute(et,r);const s=this.__theme,l=t.__theme;s?s!==l&&e.setAttribute(nt,s):l&&e.removeAttribute(nt);const g=this.__style,c=t.__style;return g!==c&&u(e.style,g,c),!1}exportDOM(t){const e=document.createElement("pre");l(e,t._config.theme.code),e.setAttribute("spellcheck","false");const n=this.getLanguage();n&&(e.setAttribute(tt,n),this.getIsSyntaxHighlightSupported()&&e.setAttribute(et,n));const r=this.getTheme();r&&e.setAttribute(nt,r);const i=this.getStyle();return i&&u(e.style,i),{element:e}}static importDOM(){return{code:t=>null!=t.textContent&&(/\r?\n/.test(t.textContent)||Z(t,"BR"))?{conversion:lt,priority:1}:null,div:()=>({conversion:ut,priority:1}),pre:()=>({conversion:lt,priority:0}),table:t=>ht(t)?{conversion:gt,priority:3}:null,td:t=>{const e=t,n=e.closest("table");return e.classList.contains("js-file-line")||n&&ht(n)?{conversion:ct,priority:3}:null},tr:t=>{const e=t.closest("table");return e&&ht(e)?{conversion:ct,priority:3}:null}}}static importJSON(t){return ot().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setLanguage(t.language).setTheme(t.theme)}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),theme:this.getTheme()}}insertNewAfter(t,e=!0){if(!K(g(),"@lexical/code")){rt();const e=ft(t);if(e)return e}const{anchor:r,focus:i}=t,o=(r.isBefore(i)?r:i).getNode();if(c(o)){let t=U(o);const e=[];for(;;)if(n(t))e.push(a()),t=t.getNextSibling();else{if(!xt(t))break;{let n=0;const r=t.getTextContent(),i=t.getTextContentSize();for(;n<i&&" "===r[n];)n++;if(0!==n&&e.push(mt(" ".repeat(n))),n!==i)break;t=t.getNextSibling()}}const i=o.splitText(r.offset)[0],s=0===r.offset?0:1,l=i.getIndexWithinParent()+s,u=o.getParentOrThrow(),g=[h(),...e];u.splice(l,0,g);const c=e[e.length-1];c?c.select():0===r.offset?i.selectPrevious():i.getNextSibling().selectNext(0,0)}if(st(o)){const{offset:e}=t.anchor;o.splice(e,0,[h()]),o.select(e+1,e+1)}return null}canIndent(){return!1}collapseAtStart(){const t=f();return this.getChildren().forEach(e=>t.append(e)),this.replace(t),!0}setLanguage(t){const e=this.getWritable();return e.__language=t||void 0,e}getLanguage(){return this.getLatest().__language}setIsSyntaxHighlightSupported(t){const e=this.getWritable();return e.__isSyntaxHighlightSupported=t,e}getIsSyntaxHighlightSupported(){return this.getLatest().__isSyntaxHighlightSupported}setTheme(t){const e=this.getWritable();return e.__theme=t||void 0,e}getTheme(){return this.getLatest().__theme}}function ot(t,e){return o(it).setLanguage(t).setTheme(e)}function st(t){return t instanceof it}function lt(t){return{node:ot(t.getAttribute(tt))}}function ut(t){const e=t,n=at(e);return n||function(t){let e=t.parentElement;for(;null!==e;){if(at(e))return!0;e=e.parentElement}return!1}(e)?{node:n?ot():null}:{node:null}}function gt(){return{node:ot()}}function ct(){return{node:null}}function at(t){return null!==t.style.fontFamily.match("monospace")}function ht(t){return t.classList.contains("js-file-line-container")}function ft(t){const{anchor:e}=t;if(t.isCollapsed()&&"element"===e.type){const t=e.getNode();if(st(t)){const n=t.getChildrenSize();if(n>=2&&e.offset===n){const e=t.getLastChild();if(r(e)&&r(e.getPreviousSibling())){const e=f();return t.splice(n-2,2,[]).insertAfter(e,!1),e.select(),e}}}}return null}class pt extends m{__highlightType;constructor(t="",e,n){super(t,n),this.__highlightType=e}static getType(){return"code-highlight"}static clone(t){return new pt(t.__text,t.__highlightType||void 0,t.__key)}afterCloneFrom(t){super.afterCloneFrom(t),this.__highlightType=t.__highlightType}getHighlightType(){return this.getLatest().__highlightType}setHighlightType(t){const e=this.getWritable();return e.__highlightType=t||void 0,e}canHaveFormat(){return!1}createDOM(t){const e=super.createDOM(t),n=dt(t.theme,this.__highlightType);return l(e,n),e}updateDOM(t,e,n){const r=super.updateDOM(t,e,n),i=dt(n.theme,t.__highlightType),o=dt(n.theme,this.__highlightType);return i!==o&&(i&&x(e,i),o&&l(e,o)),r}static importJSON(t){return mt().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setHighlightType(t.highlightType)}exportJSON(){return{...super.exportJSON(),highlightType:this.getHighlightType()}}setFormat(t){return this}isParentRequired(){return!0}createParentElementNode(){return ot()}}function dt(t,e){return e&&t&&t.codeHighlight&&t.codeHighlight[e]}function mt(t="",e){return d(new pt(t,e))}function xt(t){return t instanceof pt}const _t=_({name:"@lexical/code",nodes:()=>[it,pt],register:t=>t.registerCommand(S,t=>{const e=y();return!(!b(e)||!ft(e))&&(t.preventDefault(),!0)},T)});function St(t){if(!b(t))return!1;const e=t.anchor.getNode(),n=st(e)?e:e.getParent(),r=t.focus.getNode(),i=st(r)?r:r.getParent();return st(n)&&n.is(i)}function yt(t){const e=t.getNodes(),i=[];if(1===e.length&&st(e[0]))return i;let o=[];for(let t=0;t<e.length;t++){const s=e[t];xt(s)||n(s)||r(s)||W(169),r(s)?o.length>0&&(i.push(o),o=[]):o.push(s)}if(o.length>0){const e=t.isBackward()?t.anchor:t.focus,n=B(o[0].getKey(),0,"text");e.is(n)||i.push(o)}return i}function bt(t,e){const r=y();if(!b(r)||!St(r))return!1;const o=yt(r),s=o.length;if(0===s&&r.isCollapsed())return t===A&&r.insertNodes([a()]),!0;if(0===s&&t===A&&"\n"===r.getTextContent()){const t=a(),e=h(),n=r.isBackward()?"previous":"next";return r.insertNodes([t,e]),F(M(J(z(t,"next",0),E(i(e,"next"))),n)),!0}for(let i=0;i<s;i++){const s=o[i];if(s.length>0){let o=s[0];if(0===i&&(o=U(o)),t===A){const t=a();if(o.insertBefore(t),0===i){const e=r.isBackward()?"focus":"anchor",n=B(o.getKey(),0,"text");r[e].is(n)&&r[e].set(t.getKey(),0,"text")}}else n(o)?o.remove():void 0!==e&&xt(o)&&V(o,e,r)}}return!0}function Tt(t,e){const i=y();if(!b(i))return!1;const{anchor:o,focus:s}=i,l=o.offset,u=s.offset,g=o.getNode(),c=s.getNode(),a=t===w;if(!St(i)||!xt(g)&&!n(g)||!xt(c)&&!n(c))return!1;if(!e.altKey){if(i.isCollapsed()){const t=g.getParentOrThrow();if(a&&0===l&&null===g.getPreviousSibling()){if(null===t.getPreviousSibling())return t.selectPrevious(),e.preventDefault(),!0}else if(!a&&l===g.getTextContentSize()&&null===g.getNextSibling()){if(null===t.getNextSibling())return t.selectNext(),e.preventDefault(),!0}}return!1}let h,f;if(g.isBefore(c)?(h=U(g),f=$(c)):(h=U(c),f=$(g)),null==h||null==f)return!1;const p=h.getNodesBetween(f);for(let t=0;t<p.length;t++){const e=p[t];if(!xt(e)&&!n(e)&&!r(e))return!1}e.preventDefault(),e.stopPropagation();const d=a?h.getPreviousSibling():f.getNextSibling();if(!r(d))return!0;const m=a?d.getPreviousSibling():d.getNextSibling();if(null==m)return!0;const x=xt(m)||n(m)||r(m)?a?U(m):$(m):null;let _=null!=x?x:m;return d.remove(),p.forEach(t=>t.remove()),t===w?(p.forEach(t=>_.insertBefore(t)),_.insertBefore(d)):(_.insertAfter(d),_=d,p.forEach(t=>{_.insertAfter(t),_=t})),i.setTextNodeRange(g,l,c,u),!0}function vt(t,e){const i=y();if(!b(i))return!1;const{anchor:o,focus:s}=i,l=o.getNode(),u=s.getNode(),g=t===L;if(!St(i)||!xt(l)&&!n(l)||!xt(u)&&!n(u))return!1;const c=u;if("rtl"===q(c)?!g:g){const t=G(c,s.offset);if(null!==t){const{node:e,offset:n}=t;r(e)?e.selectNext(0,0):i.setTextNodeRange(e,n,e,n)}else c.getParentOrThrow().selectStart()}else{Q(c).select()}return e.preventDefault(),e.stopPropagation(),!0}function Nt(t,e){return N(t.registerCommand(C,e=>{const n=function(t){const e=y();if(!b(e)||!St(e))return null;const n=t?H:A,r=t?H:O,i=e.anchor,o=e.focus;if(i.is(o))return r;const s=yt(e);if(1!==s.length)return n;const l=s[0];let u,g;0===l.length&&W(285),e.isBackward()?(u=o,g=i):(u=i,g=o);const c=U(l[0]),a=$(l[0]),h=B(c.getKey(),0,"text"),f=B(a.getKey(),a.getTextContentSize(),"text");return u.isBefore(h)||f.isBefore(g)?n:h.isBefore(u)||g.isBefore(f)?r:n}(e.shiftKey);return null!==n&&(e.preventDefault(),t.dispatchCommand(n,void 0),!0)},T),t.registerCommand(O,()=>!!St(y())&&(P([a()]),!0),T),t.registerCommand(A,()=>bt(A),T),t.registerCommand(H,()=>bt(H,e),T),t.registerCommand(w,t=>{const e=y();if(!b(e))return!1;const{anchor:n}=e,r=n.getNode();return!!St(e)&&(e.isCollapsed()&&0===n.offset&&null===r.getPreviousSibling()&&st(r.getParentOrThrow())?(t.preventDefault(),!0):Tt(w,t))},T),t.registerCommand(D,t=>{const e=y();if(!b(e))return!1;const{anchor:n}=e,r=n.getNode();return!!St(e)&&(e.isCollapsed()&&n.offset===r.getTextContentSize()&&null===r.getNextSibling()&&st(r.getParentOrThrow())?(t.preventDefault(),!0):Tt(D,t))},T),t.registerCommand(L,t=>vt(L,t),T),t.registerCommand(k,t=>vt(k,t),T))}const Ct=_({build:(t,e)=>R(e),config:v({disabled:!1,tabSize:void 0}),dependencies:[_t],name:"@lexical/code-indent",register:(t,e,n)=>{const r=n.getOutput();return I(()=>{if(!r.disabled.value)return Nt(t,r.tabSize.value)})}});export{mt as $createCodeHighlightNode,ot as $createCodeNode,q as $getCodeLineDirection,Q as $getEndOfCodeInLine,U as $getFirstCodeNodeOfLine,$ as $getLastCodeNodeOfLine,G as $getStartOfCodeInLine,xt as $isCodeHighlightNode,st as $isCodeNode,V as $outdentLeadingSpaces,_t as CodeExtension,pt as CodeHighlightNode,Ct as CodeIndentExtension,it as CodeNode,X as DEFAULT_CODE_LANGUAGE,Y as getDefaultCodeLanguage,Nt as registerCodeIndentation};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|