Skip to content

Commit 2b74642

Browse files
committed
Lexical: Fixed code in lists, removed extra old alignment code
Code in lists could throw error on parse due to inner <code> tag being parsed but not actually used within a <pre>, so this updates the importDOM to disregard childdren for code blocks. This also improves the invariant implementation to not be so dev/debugger based, and to include vars in the output.
1 parent 7e03a97 commit 2b74642

File tree

10 files changed

+25
-68
lines changed

10 files changed

+25
-68
lines changed

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

-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import type {
1010
BaseSelection,
11-
ElementFormatType,
1211
LexicalCommand,
1312
LexicalNode,
1413
TextFormatType,
@@ -91,8 +90,6 @@ export const OUTDENT_CONTENT_COMMAND: LexicalCommand<void> = createCommand(
9190
);
9291
export const DROP_COMMAND: LexicalCommand<DragEvent> =
9392
createCommand('DROP_COMMAND');
94-
export const FORMAT_ELEMENT_COMMAND: LexicalCommand<ElementFormatType> =
95-
createCommand('FORMAT_ELEMENT_COMMAND');
9693
export const DRAGSTART_COMMAND: LexicalCommand<DragEvent> =
9794
createCommand('DRAGSTART_COMMAND');
9895
export const DRAGOVER_COMMAND: LexicalCommand<DragEvent> =

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

-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*
77
*/
88

9-
import type {ElementFormatType} from './nodes/LexicalElementNode';
109
import type {
1110
TextDetailType,
1211
TextFormatType,
@@ -111,27 +110,6 @@ export const DETAIL_TYPE_TO_DETAIL: Record<TextDetailType | string, number> = {
111110
unmergeable: IS_UNMERGEABLE,
112111
};
113112

114-
export const ELEMENT_TYPE_TO_FORMAT: Record<
115-
Exclude<ElementFormatType, ''>,
116-
number
117-
> = {
118-
center: IS_ALIGN_CENTER,
119-
end: IS_ALIGN_END,
120-
justify: IS_ALIGN_JUSTIFY,
121-
left: IS_ALIGN_LEFT,
122-
right: IS_ALIGN_RIGHT,
123-
start: IS_ALIGN_START,
124-
};
125-
126-
export const ELEMENT_FORMAT_TO_TYPE: Record<number, ElementFormatType> = {
127-
[IS_ALIGN_CENTER]: 'center',
128-
[IS_ALIGN_END]: 'end',
129-
[IS_ALIGN_JUSTIFY]: 'justify',
130-
[IS_ALIGN_LEFT]: 'left',
131-
[IS_ALIGN_RIGHT]: 'right',
132-
[IS_ALIGN_START]: 'start',
133-
};
134-
135113
export const TEXT_MODE_TO_TYPE: Record<TextModeType, 0 | 1 | 2> = {
136114
normal: IS_NORMAL,
137115
segmented: IS_SEGMENTED,

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

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ type NodeName = string;
146146
* Output for a DOM conversion.
147147
* Node can be set to 'ignore' to ignore the conversion and handling of the DOMNode
148148
* including all its children.
149+
*
150+
* You can specify a function to run for each converted child (forChild) or on all
151+
* the child nodes after the conversion is complete (after).
152+
* The key difference here is that forChild runs for every deeply nested child node
153+
* of the current node, whereas after will run only once after the
154+
* transformation of the node and all its children is complete.
149155
*/
150156
export type DOMConversionOutput = {
151157
after?: (childLexicalNodes: Array<LexicalNode>) => Array<LexicalNode>;

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

-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,12 @@ export type {
4949
} from './LexicalNode';
5050
export type {
5151
BaseSelection,
52-
ElementPointType as ElementPoint,
5352
NodeSelection,
5453
Point,
5554
PointType,
5655
RangeSelection,
57-
TextPointType as TextPoint,
5856
} from './LexicalSelection';
5957
export type {
60-
ElementFormatType,
6158
SerializedElementNode,
6259
} from './nodes/LexicalElementNode';
6360
export type {SerializedRootNode} from './nodes/LexicalRootNode';
@@ -87,7 +84,6 @@ export {
8784
DRAGSTART_COMMAND,
8885
DROP_COMMAND,
8986
FOCUS_COMMAND,
90-
FORMAT_ELEMENT_COMMAND,
9187
FORMAT_TEXT_COMMAND,
9288
INDENT_CONTENT_COMMAND,
9389
INSERT_LINE_BREAK_COMMAND,

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

-9
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ export type SerializedElementNode<
4646
SerializedLexicalNode
4747
>;
4848

49-
export type ElementFormatType =
50-
| 'left'
51-
| 'start'
52-
| 'center'
53-
| 'right'
54-
| 'end'
55-
| 'justify'
56-
| '';
57-
5849
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
5950
export interface ElementNode {
6051
getTopLevelElement(): ElementNode | null;

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

+5
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,11 @@ const nodeNameToTextFormat: Record<string, TextFormatType> = {
13141314

13151315
function convertTextFormatElement(domNode: HTMLElement): DOMConversionOutput {
13161316
const format = nodeNameToTextFormat[domNode.nodeName.toLowerCase()];
1317+
1318+
if (format === 'code' && domNode.closest('pre')) {
1319+
return {node: null};
1320+
}
1321+
13171322
if (format === undefined) {
13181323
return {node: null};
13191324
}

resources/js/wysiwyg/lexical/core/shared/invariant.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default function invariant(
1818
return;
1919
}
2020

21-
throw new Error(
22-
'Internal Lexical error: invariant() is meant to be replaced at compile ' +
23-
'time. There is no runtime version. Error: ' +
24-
message,
25-
);
21+
for (const arg of args) {
22+
message = (message || '').replace('%s', arg);
23+
}
24+
25+
throw new Error(message);
2626
}

resources/js/wysiwyg/lexical/html/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
DOMChildConversion,
1212
DOMConversion,
1313
DOMConversionFn,
14-
ElementFormatType,
1514
LexicalEditor,
1615
LexicalNode,
1716
} from 'lexical';
@@ -58,6 +57,7 @@ export function $generateNodesFromDOM(
5857
}
5958
}
6059
}
60+
6161
$unwrapArtificalNodes(allArtificialNodes);
6262

6363
return lexicalNodes;
@@ -324,8 +324,6 @@ function wrapContinuousInlines(
324324
nodes: Array<LexicalNode>,
325325
createWrapperFn: () => ElementNode,
326326
): Array<LexicalNode> {
327-
const textAlign = (domNode as HTMLElement).style
328-
.textAlign as ElementFormatType;
329327
const out: Array<LexicalNode> = [];
330328
let continuousInlines: Array<LexicalNode> = [];
331329
// wrap contiguous inline child nodes in para

resources/js/wysiwyg/lexical/rich-text/LexicalCodeBlockNode.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,14 @@ export class CodeBlockNode extends DecoratorNode<EditorDecoratorAdapter> {
145145
node.setId(element.id);
146146
}
147147

148-
return { node };
148+
return {
149+
node,
150+
after(childNodes): LexicalNode[] {
151+
// Remove any child nodes that may get parsed since we're manually
152+
// controlling the code contents.
153+
return [];
154+
},
155+
};
149156
},
150157
priority: 3,
151158
};

resources/js/wysiwyg/lexical/rich-text/index.ts

-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import type {
1010
CommandPayloadType,
11-
ElementFormatType,
1211
LexicalCommand,
1312
LexicalEditor,
1413
PasteCommandType,
@@ -44,7 +43,6 @@ import {
4443
DRAGSTART_COMMAND,
4544
DROP_COMMAND,
4645
ElementNode,
47-
FORMAT_ELEMENT_COMMAND,
4846
FORMAT_TEXT_COMMAND,
4947
INSERT_LINE_BREAK_COMMAND,
5048
INSERT_PARAGRAPH_COMMAND,
@@ -285,25 +283,6 @@ export function registerRichText(editor: LexicalEditor): () => void {
285283
},
286284
COMMAND_PRIORITY_EDITOR,
287285
),
288-
editor.registerCommand<ElementFormatType>(
289-
FORMAT_ELEMENT_COMMAND,
290-
(format) => {
291-
const selection = $getSelection();
292-
if (!$isRangeSelection(selection) && !$isNodeSelection(selection)) {
293-
return false;
294-
}
295-
const nodes = selection.getNodes();
296-
for (const node of nodes) {
297-
const element = $findMatchingParent(
298-
node,
299-
(parentNode): parentNode is ElementNode =>
300-
$isElementNode(parentNode) && !parentNode.isInline(),
301-
);
302-
}
303-
return true;
304-
},
305-
COMMAND_PRIORITY_EDITOR,
306-
),
307286
editor.registerCommand<boolean>(
308287
INSERT_LINE_BREAK_COMMAND,
309288
(selectStart) => {

0 commit comments

Comments
 (0)