Skip to content

Commit de0d178

Browse files
authored
Merge branch 'main' into jv_table_cell_bg
2 parents b33d4f0 + a79ed52 commit de0d178

32 files changed

+7541
-4026
lines changed

package-lock.json

Lines changed: 6043 additions & 3362 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lexical-code/flow/LexicalCode.js.flow

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,22 @@ import {ElementNode, TextNode} from 'lexical';
2626
/**
2727
* CodeHighlighter
2828
*/
29+
declare export function $getEndOfCodeInLine(
30+
anchor: CodeHighlightNode | TabNode,
31+
): CodeHighlightNode | TabNode;
32+
/** @deprecated renamed to {@link $getEndOfCodeInLine} by @lexical/eslint-plugin rules-of-lexical */
2933
declare export function getEndOfCodeInLine(
3034
anchor: CodeHighlightNode | TabNode,
3135
): CodeHighlightNode | TabNode;
3236

37+
declare export function $getStartOfCodeInLine(
38+
anchor: CodeHighlightNode | TabNode,
39+
offset: number,
40+
): null | {
41+
node: CodeHighlightNode | TabNode | LineBreakNode,
42+
offset: number,
43+
};
44+
/** @deprecated renamed to {@link $getStartOfCodeInLine} by @lexical/eslint-plugin rules-of-lexical */
3345
declare export function getStartOfCodeInLine(
3446
anchor: CodeHighlightNode | TabNode,
3547
offset: number,
@@ -85,12 +97,20 @@ declare export var DEFAULT_CODE_LANGUAGE: string;
8597
declare export var getCodeLanguages: () => Array<string>;
8698
declare export var getDefaultCodeLanguage: () => string;
8799

100+
declare export function $getFirstCodeNodeOfLine(
101+
anchor: CodeHighlightNode | TabNode | LineBreakNode,
102+
): CodeHighlightNode | TabNode | LineBreakNode;
103+
/** @deprecated renamed to {@link $getFirstCodeNodeOfLine} by @lexical/eslint-plugin rules-of-lexical */
88104
declare export function getFirstCodeNodeOfLine(
89105
anchor: CodeHighlightNode | TabNode | LineBreakNode,
90106
): null | CodeHighlightNode | TabNode | LineBreakNode;
91107

92108
declare export function getLanguageFriendlyName(lang: string): string;
93109

110+
declare export function $getLastCodeNodeOfLine(
111+
anchor: CodeHighlightNode | TabNode | LineBreakNode,
112+
): CodeHighlightNode | TabNode | LineBreakNode;
113+
/** @deprecated renamed to {@link $getLastCodeNodeOfLine} by @lexical/eslint-plugin rules-of-lexical */
94114
declare export function getLastCodeNodeOfLine(
95115
anchor: CodeHighlightNode | TabNode | LineBreakNode,
96116
): CodeHighlightNode | TabNode | LineBreakNode;

packages/lexical-code/src/CodeHighlightNode.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77
*/
88

99
import type {
10+
CaretDirection,
1011
EditorConfig,
1112
EditorThemeClasses,
1213
LexicalNode,
1314
LexicalUpdateJSON,
1415
LineBreakNode,
1516
NodeKey,
1617
SerializedTextNode,
18+
SiblingCaret,
1719
Spread,
1820
TabNode,
1921
} from 'lexical';
2022

2123
import {
24+
$getAdjacentCaret,
2225
addClassNamesToElement,
2326
removeClassNamesFromElement,
2427
} from '@lexical/utils';
2528
import {
2629
$applyNodeReplacement,
30+
$getSiblingCaret,
2731
$isTabNode,
2832
ElementNode,
2933
TextNode,
@@ -224,26 +228,32 @@ export function $isCodeHighlightNode(
224228
return node instanceof CodeHighlightNode;
225229
}
226230

227-
export function getFirstCodeNodeOfLine(
231+
function $getLastMatchingCodeNode<D extends CaretDirection>(
228232
anchor: CodeHighlightNode | TabNode | LineBreakNode,
229-
): null | CodeHighlightNode | TabNode | LineBreakNode {
230-
let previousNode = anchor;
231-
let node: null | LexicalNode = anchor;
232-
while ($isCodeHighlightNode(node) || $isTabNode(node)) {
233-
previousNode = node;
234-
node = node.getPreviousSibling();
233+
direction: D,
234+
): CodeHighlightNode | TabNode | LineBreakNode {
235+
let matchingNode: CodeHighlightNode | TabNode | LineBreakNode = anchor;
236+
for (
237+
let caret: null | SiblingCaret<LexicalNode, D> = $getSiblingCaret(
238+
anchor,
239+
direction,
240+
);
241+
caret && ($isCodeHighlightNode(caret.origin) || $isTabNode(caret.origin));
242+
caret = $getAdjacentCaret(caret)
243+
) {
244+
matchingNode = caret.origin;
235245
}
236-
return previousNode;
246+
return matchingNode;
237247
}
238248

239-
export function getLastCodeNodeOfLine(
249+
export function $getFirstCodeNodeOfLine(
240250
anchor: CodeHighlightNode | TabNode | LineBreakNode,
241251
): CodeHighlightNode | TabNode | LineBreakNode {
242-
let nextNode = anchor;
243-
let node: null | LexicalNode = anchor;
244-
while ($isCodeHighlightNode(node) || $isTabNode(node)) {
245-
nextNode = node;
246-
node = node.getNextSibling();
247-
}
248-
return nextNode;
252+
return $getLastMatchingCodeNode(anchor, 'previous');
253+
}
254+
255+
export function $getLastCodeNodeOfLine(
256+
anchor: CodeHighlightNode | TabNode | LineBreakNode,
257+
): CodeHighlightNode | TabNode | LineBreakNode {
258+
return $getLastMatchingCodeNode(anchor, 'next');
249259
}

packages/lexical-code/src/CodeHighlighter.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ import invariant from 'shared/invariant';
4545
import {Prism} from './CodeHighlighterPrism';
4646
import {
4747
$createCodeHighlightNode,
48+
$getFirstCodeNodeOfLine,
49+
$getLastCodeNodeOfLine,
4850
$isCodeHighlightNode,
4951
CodeHighlightNode,
5052
DEFAULT_CODE_LANGUAGE,
51-
getFirstCodeNodeOfLine,
52-
getLastCodeNodeOfLine,
5353
} from './CodeHighlightNode';
5454
import {$isCodeNode, CodeNode} from './CodeNode';
5555

@@ -75,7 +75,7 @@ export const PrismTokenizer: Tokenizer = {
7575
},
7676
};
7777

78-
export function getStartOfCodeInLine(
78+
export function $getStartOfCodeInLine(
7979
anchor: CodeHighlightNode | TabNode,
8080
offset: number,
8181
): null | {
@@ -188,10 +188,10 @@ function findNextNonBlankInLine(
188188
}
189189
}
190190

191-
export function getEndOfCodeInLine(
191+
export function $getEndOfCodeInLine(
192192
anchor: CodeHighlightNode | TabNode,
193193
): CodeHighlightNode | TabNode {
194-
const lastNode = getLastCodeNodeOfLine(anchor);
194+
const lastNode = $getLastCodeNodeOfLine(anchor);
195195
invariant(
196196
!$isLineBreakNode(lastNode),
197197
'Unexpected lineBreakNode in getEndOfCodeInLine',
@@ -535,8 +535,8 @@ function $handleTab(shiftKey: boolean): null | LexicalCommand<void> {
535535
if ($isCodeNode(firstNode)) {
536536
return indentOrOutdent;
537537
}
538-
const firstOfLine = getFirstCodeNodeOfLine(firstNode);
539-
const lastOfLine = getLastCodeNodeOfLine(firstNode);
538+
const firstOfLine = $getFirstCodeNodeOfLine(firstNode);
539+
const lastOfLine = $getLastCodeNodeOfLine(firstNode);
540540
const anchor = selection.anchor;
541541
const focus = selection.focus;
542542
let selectionFirst;
@@ -578,7 +578,7 @@ function $handleMultilineIndent(type: LexicalCommand<void>): boolean {
578578
line[0];
579579
// First and last lines might not be complete
580580
if (i === 0) {
581-
firstOfLine = getFirstCodeNodeOfLine(firstOfLine);
581+
firstOfLine = $getFirstCodeNodeOfLine(firstOfLine);
582582
}
583583
if (firstOfLine !== null) {
584584
if (type === INDENT_CONTENT_COMMAND) {
@@ -608,11 +608,7 @@ function $handleMultilineIndent(type: LexicalCommand<void>): boolean {
608608
}
609609
return true;
610610
}
611-
const firstOfLine = getFirstCodeNodeOfLine(firstNode);
612-
invariant(
613-
firstOfLine !== null,
614-
'Expected getFirstCodeNodeOfLine to return a valid Code Node',
615-
);
611+
const firstOfLine = $getFirstCodeNodeOfLine(firstNode);
616612
if (type === INDENT_CONTENT_COMMAND) {
617613
if ($isLineBreakNode(firstOfLine)) {
618614
firstOfLine.insertAfter($createTabNode());
@@ -687,11 +683,11 @@ function $handleShiftLines(
687683
let start;
688684
let end;
689685
if (anchorNode.isBefore(focusNode)) {
690-
start = getFirstCodeNodeOfLine(anchorNode);
691-
end = getLastCodeNodeOfLine(focusNode);
686+
start = $getFirstCodeNodeOfLine(anchorNode);
687+
end = $getLastCodeNodeOfLine(focusNode);
692688
} else {
693-
start = getFirstCodeNodeOfLine(focusNode);
694-
end = getLastCodeNodeOfLine(anchorNode);
689+
start = $getFirstCodeNodeOfLine(focusNode);
690+
end = $getLastCodeNodeOfLine(anchorNode);
695691
}
696692
if (start == null || end == null) {
697693
return false;
@@ -733,8 +729,8 @@ function $handleShiftLines(
733729
$isTabNode(sibling) ||
734730
$isLineBreakNode(sibling)
735731
? arrowIsUp
736-
? getFirstCodeNodeOfLine(sibling)
737-
: getLastCodeNodeOfLine(sibling)
732+
? $getFirstCodeNodeOfLine(sibling)
733+
: $getLastCodeNodeOfLine(sibling)
738734
: null;
739735
let insertionPoint =
740736
maybeInsertionPoint != null ? maybeInsertionPoint : sibling;
@@ -781,7 +777,7 @@ function $handleMoveTo(
781777
}
782778

783779
if (isMoveToStart) {
784-
const start = getStartOfCodeInLine(focusNode, focus.offset);
780+
const start = $getStartOfCodeInLine(focusNode, focus.offset);
785781
if (start !== null) {
786782
const {node, offset} = start;
787783
if ($isLineBreakNode(node)) {
@@ -793,7 +789,7 @@ function $handleMoveTo(
793789
focusNode.getParentOrThrow().selectStart();
794790
}
795791
} else {
796-
const node = getEndOfCodeInLine(focusNode);
792+
const node = $getEndOfCodeInLine(focusNode);
797793
node.select();
798794
}
799795

packages/lexical-code/src/CodeNode.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import {
3737
import {Prism} from './CodeHighlighterPrism';
3838
import {
3939
$createCodeHighlightNode,
40+
$getFirstCodeNodeOfLine,
4041
$isCodeHighlightNode,
41-
getFirstCodeNodeOfLine,
4242
} from './CodeHighlightNode';
4343

4444
export type SerializedCodeNode = Spread<
@@ -259,7 +259,8 @@ export class CodeNode extends ElementNode {
259259
const firstPoint = anchor.isBefore(focus) ? anchor : focus;
260260
const firstSelectionNode = firstPoint.getNode();
261261
if ($isTextNode(firstSelectionNode)) {
262-
let node = getFirstCodeNodeOfLine(firstSelectionNode);
262+
let node: null | LexicalNode =
263+
$getFirstCodeNodeOfLine(firstSelectionNode);
263264
const insertNodes = [];
264265
// eslint-disable-next-line no-constant-condition
265266
while (true) {

packages/lexical-code/src/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,40 @@
66
*
77
*/
88

9+
import {$getEndOfCodeInLine, $getStartOfCodeInLine} from './CodeHighlighter';
10+
import {
11+
$getFirstCodeNodeOfLine,
12+
$getLastCodeNodeOfLine,
13+
} from './CodeHighlightNode';
14+
915
export {
10-
getEndOfCodeInLine,
11-
getStartOfCodeInLine,
16+
$getEndOfCodeInLine,
17+
$getStartOfCodeInLine,
1218
PrismTokenizer,
1319
registerCodeHighlighting,
1420
} from './CodeHighlighter';
1521
export {
1622
$createCodeHighlightNode,
23+
$getFirstCodeNodeOfLine,
24+
$getLastCodeNodeOfLine,
1725
$isCodeHighlightNode,
1826
CODE_LANGUAGE_FRIENDLY_NAME_MAP,
1927
CODE_LANGUAGE_MAP,
2028
CodeHighlightNode,
2129
DEFAULT_CODE_LANGUAGE,
2230
getCodeLanguages,
2331
getDefaultCodeLanguage,
24-
getFirstCodeNodeOfLine,
2532
getLanguageFriendlyName,
26-
getLastCodeNodeOfLine,
2733
normalizeCodeLang,
2834
} from './CodeHighlightNode';
2935
export type {SerializedCodeNode} from './CodeNode';
3036
export {$createCodeNode, $isCodeNode, CodeNode} from './CodeNode';
37+
38+
/** @deprecated renamed to {@link $getFirstCodeNodeOfLine} by @lexical/eslint-plugin rules-of-lexical */
39+
export const getFirstCodeNodeOfLine = $getFirstCodeNodeOfLine;
40+
/** @deprecated renamed to {@link $getLastCodeNodeOfLine} by @lexical/eslint-plugin rules-of-lexical */
41+
export const getLastCodeNodeOfLine = $getLastCodeNodeOfLine;
42+
/** @deprecated renamed to {@link $getEndOfCodeInLine} by @lexical/eslint-plugin rules-of-lexical */
43+
export const getEndOfCodeInLine = $getEndOfCodeInLine;
44+
/** @deprecated renamed to {@link $getStartOfCodeInLine} by @lexical/eslint-plugin rules-of-lexical */
45+
export const getStartOfCodeInLine = $getStartOfCodeInLine;

packages/lexical-list/src/LexicalListItemNode.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export class ListItemNode extends ElementNode {
8080
}
8181
element.value = this.__value;
8282
$setListItemThemeClassNames(element, config.theme, this);
83+
const nextStyle = this.__style || this.__textStyle;
84+
if (nextStyle) {
85+
element.style.cssText = nextStyle;
86+
}
8387
return element;
8488
}
8589

@@ -95,7 +99,14 @@ export class ListItemNode extends ElementNode {
9599
// @ts-expect-error - this is always HTMLListItemElement
96100
dom.value = this.__value;
97101
$setListItemThemeClassNames(dom, config.theme, this);
98-
102+
const prevStyle = prevNode.__style || prevNode.__textStyle;
103+
const nextStyle = this.__style || this.__textStyle;
104+
if (prevStyle !== nextStyle) {
105+
dom.style.cssText = nextStyle;
106+
if (nextStyle === '') {
107+
dom.removeAttribute('style');
108+
}
109+
}
99110
return false;
100111
}
101112

0 commit comments

Comments
 (0)