Skip to content

Commit f3fa63a

Browse files
committed
Lexical: Merged custom paragraph node, removed old format/indent refs
Start of work to merge custom nodes into lexical, removing old unused format/indent core logic while extending common block elements where possible.
1 parent 5164375 commit f3fa63a

22 files changed

+95
-445
lines changed

resources/js/wysiwyg/lexical/core/LexicalEvents.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ function onSelectionChange(
355355
lastNode instanceof ParagraphNode &&
356356
lastNode.getChildrenSize() === 0
357357
) {
358-
selection.format = lastNode.getTextFormat();
359358
selection.style = lastNode.getTextStyle();
360359
} else {
361360
selection.format = 0;
@@ -578,7 +577,6 @@ function onBeforeInput(event: InputEvent, editor: LexicalEditor): void {
578577
if ($isRangeSelection(selection)) {
579578
const anchorNode = selection.anchor.getNode();
580579
anchorNode.markDirty();
581-
selection.format = anchorNode.getFormat();
582580
invariant(
583581
$isTextNode(anchorNode),
584582
'Anchor node must be a TextNode',
@@ -912,7 +910,6 @@ function onCompositionStart(
912910
// need to invoke the empty space heuristic below.
913911
anchor.type === 'element' ||
914912
!selection.isCollapsed() ||
915-
node.getFormat() !== selection.format ||
916913
($isTextNode(node) && node.getStyle() !== selection.style)
917914
) {
918915
// We insert a zero width character, ready for the composition

resources/js/wysiwyg/lexical/core/LexicalMutations.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@ function shouldUpdateTextNodeFromMutation(
9696
targetDOM: Node,
9797
targetNode: TextNode,
9898
): boolean {
99-
if ($isRangeSelection(selection)) {
100-
const anchorNode = selection.anchor.getNode();
101-
if (
102-
anchorNode.is(targetNode) &&
103-
selection.format !== anchorNode.getFormat()
104-
) {
105-
return false;
106-
}
107-
}
10899
return targetDOM.nodeType === DOM_TEXT_TYPE && targetNode.isAttached();
109100
}
110101

resources/js/wysiwyg/lexical/core/LexicalReconciler.ts

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import type {NodeKey, NodeMap} from './LexicalNode';
1717
import type {ElementNode} from './nodes/LexicalElementNode';
1818

1919
import invariant from 'lexical/shared/invariant';
20-
import normalizeClassNames from 'lexical/shared/normalizeClassNames';
2120

2221
import {
2322
$isDecoratorNode,
@@ -117,51 +116,6 @@ function setTextAlign(domStyle: CSSStyleDeclaration, value: string): void {
117116
domStyle.setProperty('text-align', value);
118117
}
119118

120-
const DEFAULT_INDENT_VALUE = '40px';
121-
122-
function setElementIndent(dom: HTMLElement, indent: number): void {
123-
const indentClassName = activeEditorConfig.theme.indent;
124-
125-
if (typeof indentClassName === 'string') {
126-
const elementHasClassName = dom.classList.contains(indentClassName);
127-
128-
if (indent > 0 && !elementHasClassName) {
129-
dom.classList.add(indentClassName);
130-
} else if (indent < 1 && elementHasClassName) {
131-
dom.classList.remove(indentClassName);
132-
}
133-
}
134-
135-
const indentationBaseValue =
136-
getComputedStyle(dom).getPropertyValue('--lexical-indent-base-value') ||
137-
DEFAULT_INDENT_VALUE;
138-
139-
dom.style.setProperty(
140-
'padding-inline-start',
141-
indent === 0 ? '' : `calc(${indent} * ${indentationBaseValue})`,
142-
);
143-
}
144-
145-
function setElementFormat(dom: HTMLElement, format: number): void {
146-
const domStyle = dom.style;
147-
148-
if (format === 0) {
149-
setTextAlign(domStyle, '');
150-
} else if (format === IS_ALIGN_LEFT) {
151-
setTextAlign(domStyle, 'left');
152-
} else if (format === IS_ALIGN_CENTER) {
153-
setTextAlign(domStyle, 'center');
154-
} else if (format === IS_ALIGN_RIGHT) {
155-
setTextAlign(domStyle, 'right');
156-
} else if (format === IS_ALIGN_JUSTIFY) {
157-
setTextAlign(domStyle, 'justify');
158-
} else if (format === IS_ALIGN_START) {
159-
setTextAlign(domStyle, 'start');
160-
} else if (format === IS_ALIGN_END) {
161-
setTextAlign(domStyle, 'end');
162-
}
163-
}
164-
165119
function $createNode(
166120
key: NodeKey,
167121
parentDOM: null | HTMLElement,
@@ -185,22 +139,14 @@ function $createNode(
185139
}
186140

187141
if ($isElementNode(node)) {
188-
const indent = node.__indent;
189142
const childrenSize = node.__size;
190143

191-
if (indent !== 0) {
192-
setElementIndent(dom, indent);
193-
}
194144
if (childrenSize !== 0) {
195145
const endIndex = childrenSize - 1;
196146
const children = createChildrenArray(node, activeNextNodeMap);
197147
$createChildren(children, node, 0, endIndex, dom, null);
198148
}
199-
const format = node.__format;
200149

201-
if (format !== 0) {
202-
setElementFormat(dom, format);
203-
}
204150
if (!node.isInline()) {
205151
reconcileElementTerminatingLineBreak(null, node, dom);
206152
}
@@ -349,10 +295,8 @@ function reconcileParagraphFormat(element: ElementNode): void {
349295
if (
350296
$isParagraphNode(element) &&
351297
subTreeTextFormat != null &&
352-
subTreeTextFormat !== element.__textFormat &&
353298
!activeEditorStateReadOnly
354299
) {
355-
element.setTextFormat(subTreeTextFormat);
356300
element.setTextStyle(subTreeTextStyle);
357301
}
358302
}
@@ -563,17 +507,6 @@ function $reconcileNode(
563507

564508
if ($isElementNode(prevNode) && $isElementNode(nextNode)) {
565509
// Reconcile element children
566-
const nextIndent = nextNode.__indent;
567-
568-
if (nextIndent !== prevNode.__indent) {
569-
setElementIndent(dom, nextIndent);
570-
}
571-
572-
const nextFormat = nextNode.__format;
573-
574-
if (nextFormat !== prevNode.__format) {
575-
setElementFormat(dom, nextFormat);
576-
}
577510
if (isDirty) {
578511
$reconcileChildrenWithDirection(prevNode, nextNode, dom);
579512
if (!$isRootNode(nextNode) && !nextNode.isInline()) {

resources/js/wysiwyg/lexical/core/__tests__/utils/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ export class TestElementNode extends ElementNode {
129129
serializedNode: SerializedTestElementNode,
130130
): TestInlineElementNode {
131131
const node = $createTestInlineElementNode();
132-
node.setFormat(serializedNode.format);
133-
node.setIndent(serializedNode.indent);
134132
node.setDirection(serializedNode.direction);
135133
return node;
136134
}
@@ -195,8 +193,6 @@ export class TestInlineElementNode extends ElementNode {
195193
serializedNode: SerializedTestInlineElementNode,
196194
): TestInlineElementNode {
197195
const node = $createTestInlineElementNode();
198-
node.setFormat(serializedNode.format);
199-
node.setIndent(serializedNode.indent);
200196
node.setDirection(serializedNode.direction);
201197
return node;
202198
}
@@ -241,8 +237,6 @@ export class TestShadowRootNode extends ElementNode {
241237
serializedNode: SerializedTestShadowRootNode,
242238
): TestShadowRootNode {
243239
const node = $createTestShadowRootNode();
244-
node.setFormat(serializedNode.format);
245-
node.setIndent(serializedNode.indent);
246240
node.setDirection(serializedNode.direction);
247241
return node;
248242
}
@@ -322,8 +316,6 @@ export class TestExcludeFromCopyElementNode extends ElementNode {
322316
serializedNode: SerializedTestExcludeFromCopyElementNode,
323317
): TestExcludeFromCopyElementNode {
324318
const node = $createTestExcludeFromCopyElementNode();
325-
node.setFormat(serializedNode.format);
326-
node.setIndent(serializedNode.indent);
327319
node.setDirection(serializedNode.direction);
328320
return node;
329321
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {ElementNode} from "./LexicalElementNode";
2+
import {CommonBlockAlignment, SerializedCommonBlockNode} from "../../../nodes/_common";
3+
4+
5+
export class CommonBlockNode extends ElementNode {
6+
__id: string = '';
7+
__alignment: CommonBlockAlignment = '';
8+
__inset: number = 0;
9+
10+
setId(id: string) {
11+
const self = this.getWritable();
12+
self.__id = id;
13+
}
14+
15+
getId(): string {
16+
const self = this.getLatest();
17+
return self.__id;
18+
}
19+
20+
setAlignment(alignment: CommonBlockAlignment) {
21+
const self = this.getWritable();
22+
self.__alignment = alignment;
23+
}
24+
25+
getAlignment(): CommonBlockAlignment {
26+
const self = this.getLatest();
27+
return self.__alignment;
28+
}
29+
30+
setInset(size: number) {
31+
const self = this.getWritable();
32+
self.__inset = size;
33+
}
34+
35+
getInset(): number {
36+
const self = this.getLatest();
37+
return self.__inset;
38+
}
39+
40+
exportJSON(): SerializedCommonBlockNode {
41+
return {
42+
...super.exportJSON(),
43+
id: this.__id,
44+
alignment: this.__alignment,
45+
inset: this.__inset,
46+
};
47+
}
48+
}
49+
50+
export function copyCommonBlockProperties(from: CommonBlockNode, to: CommonBlockNode): void {
51+
to.__id = from.__id;
52+
to.__alignment = from.__alignment;
53+
to.__inset = from.__inset;
54+
}

resources/js/wysiwyg/lexical/core/nodes/LexicalElementNode.ts

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ export type SerializedElementNode<
4242
{
4343
children: Array<T>;
4444
direction: 'ltr' | 'rtl' | null;
45-
format: ElementFormatType;
46-
indent: number;
4745
},
4846
SerializedLexicalNode
4947
>;
@@ -74,22 +72,16 @@ export class ElementNode extends LexicalNode {
7472
/** @internal */
7573
__size: number;
7674
/** @internal */
77-
__format: number;
78-
/** @internal */
7975
__style: string;
8076
/** @internal */
81-
__indent: number;
82-
/** @internal */
8377
__dir: 'ltr' | 'rtl' | null;
8478

8579
constructor(key?: NodeKey) {
8680
super(key);
8781
this.__first = null;
8882
this.__last = null;
8983
this.__size = 0;
90-
this.__format = 0;
9184
this.__style = '';
92-
this.__indent = 0;
9385
this.__dir = null;
9486
}
9587

@@ -98,28 +90,14 @@ export class ElementNode extends LexicalNode {
9890
this.__first = prevNode.__first;
9991
this.__last = prevNode.__last;
10092
this.__size = prevNode.__size;
101-
this.__indent = prevNode.__indent;
102-
this.__format = prevNode.__format;
10393
this.__style = prevNode.__style;
10494
this.__dir = prevNode.__dir;
10595
}
10696

107-
getFormat(): number {
108-
const self = this.getLatest();
109-
return self.__format;
110-
}
111-
getFormatType(): ElementFormatType {
112-
const format = this.getFormat();
113-
return ELEMENT_FORMAT_TO_TYPE[format] || '';
114-
}
11597
getStyle(): string {
11698
const self = this.getLatest();
11799
return self.__style;
118100
}
119-
getIndent(): number {
120-
const self = this.getLatest();
121-
return self.__indent;
122-
}
123101
getChildren<T extends LexicalNode>(): Array<T> {
124102
const children: Array<T> = [];
125103
let child: T | null = this.getFirstChild();
@@ -301,13 +279,6 @@ export class ElementNode extends LexicalNode {
301279
const self = this.getLatest();
302280
return self.__dir;
303281
}
304-
hasFormat(type: ElementFormatType): boolean {
305-
if (type !== '') {
306-
const formatFlag = ELEMENT_TYPE_TO_FORMAT[type];
307-
return (this.getFormat() & formatFlag) !== 0;
308-
}
309-
return false;
310-
}
311282

312283
// Mutators
313284

@@ -378,21 +349,11 @@ export class ElementNode extends LexicalNode {
378349
self.__dir = direction;
379350
return self;
380351
}
381-
setFormat(type: ElementFormatType): this {
382-
const self = this.getWritable();
383-
self.__format = type !== '' ? ELEMENT_TYPE_TO_FORMAT[type] : 0;
384-
return this;
385-
}
386352
setStyle(style: string): this {
387353
const self = this.getWritable();
388354
self.__style = style || '';
389355
return this;
390356
}
391-
setIndent(indentLevel: number): this {
392-
const self = this.getWritable();
393-
self.__indent = indentLevel;
394-
return this;
395-
}
396357
splice(
397358
start: number,
398359
deleteCount: number,
@@ -528,8 +489,6 @@ export class ElementNode extends LexicalNode {
528489
return {
529490
children: [],
530491
direction: this.getDirection(),
531-
format: this.getFormatType(),
532-
indent: this.getIndent(),
533492
type: 'element',
534493
version: 1,
535494
};

0 commit comments

Comments
 (0)