Skip to content

Commit c314a60

Browse files
committed
WYSIWYG: Code & table fixes
- Fixed new code block insertion to remove selection area instead of just adding after. - Added default table column widths to not be collapsed - Updated table dom export to not duplicate colgroups.
1 parent 9b2520a commit c314a60

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

resources/js/wysiwyg/lexical/table/LexicalTableNode.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,26 @@ export class TableNode extends ElementNode {
8383
return {
8484
...super.exportDOM(editor),
8585
after: (tableElement) => {
86-
if (tableElement) {
87-
const newElement = tableElement.cloneNode() as ParentNode;
88-
const colGroup = document.createElement('colgroup');
89-
const tBody = document.createElement('tbody');
90-
if (isHTMLElement(tableElement)) {
91-
tBody.append(...tableElement.children);
92-
}
93-
const firstRow = this.getFirstChildOrThrow<TableRowNode>();
94-
95-
if (!$isTableRowNode(firstRow)) {
96-
throw new Error('Expected to find row node.');
97-
}
86+
if (!tableElement) {
87+
return;
88+
}
9889

99-
const colCount = firstRow.getChildrenSize();
90+
const newElement = tableElement.cloneNode() as ParentNode;
91+
const tBody = document.createElement('tbody');
10092

101-
for (let i = 0; i < colCount; i++) {
102-
const col = document.createElement('col');
103-
colGroup.append(col);
93+
if (isHTMLElement(tableElement)) {
94+
for (const child of Array.from(tableElement.children)) {
95+
if (child.nodeName === 'TR') {
96+
tBody.append(child);
97+
} else {
98+
newElement.append(child);
99+
}
104100
}
101+
}
105102

106-
newElement.replaceChildren(colGroup, tBody);
103+
newElement.append(tBody);
107104

108-
return newElement as HTMLElement;
109-
}
105+
return newElement as HTMLElement;
110106
},
111107
};
112108
}

resources/js/wysiwyg/ui/framework/blocks/table-creator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ export class EditorTableCreator extends EditorUiElement {
7474
return;
7575
}
7676

77+
const targetColWidth = Math.min(Math.round(840 / columns), 240);
78+
const colWidths = Array(columns).fill(targetColWidth + 'px');
79+
7780
this.getContext().editor.update(() => {
7881
const table = $createTableNodeWithDimensions(rows, columns, false) as CustomTableNode;
82+
table.setColWidths(colWidths);
7983
$insertNewBlockNodeAtSelection(table);
8084
});
8185
}

resources/js/wysiwyg/utils/formats.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {$createTextNode, $getSelection, $insertNodes, LexicalEditor, LexicalNode
33
import {
44
$getBlockElementNodesInSelection,
55
$getNodeFromSelection,
6-
$insertNewBlockNodeAtSelection, $selectionContainsNodeType,
6+
$insertNewBlockNodeAtSelection, $selectionContainsNodeType, $selectSingleNode,
77
$toggleSelectionBlockNodeType,
88
getLastSelection
99
} from "./selection";
@@ -65,9 +65,19 @@ export function formatCodeBlock(editor: LexicalEditor) {
6565
editor.update(() => {
6666
const codeBlock = $createCodeBlockNode();
6767
codeBlock.setCode(selection?.getTextContent() || '');
68-
$insertNewBlockNodeAtSelection(codeBlock, true);
68+
69+
const selectionNodes = $getBlockElementNodesInSelection(selection);
70+
const firstSelectionNode = selectionNodes[0];
71+
const extraNodes = selectionNodes.slice(1);
72+
if (firstSelectionNode) {
73+
firstSelectionNode.replace(codeBlock);
74+
extraNodes.forEach(n => n.remove());
75+
} else {
76+
$insertNewBlockNodeAtSelection(codeBlock, true);
77+
}
78+
6979
$openCodeEditorForNode(editor, codeBlock);
70-
codeBlock.selectStart();
80+
$selectSingleNode(codeBlock);
7181
});
7282
} else {
7383
$openCodeEditorForNode(editor, codeBlock);

resources/js/wysiwyg/utils/nodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
$getRoot,
33
$isDecoratorNode,
4-
$isElementNode,
4+
$isElementNode, $isRootNode,
55
$isTextNode,
66
ElementNode,
77
LexicalEditor,
@@ -84,7 +84,7 @@ export function $getNearestBlockNodeForCoords(editor: LexicalEditor, x: number,
8484

8585
export function $getNearestNodeBlockParent(node: LexicalNode): LexicalNode|null {
8686
const isBlockNode = (node: LexicalNode): boolean => {
87-
return ($isElementNode(node) || $isDecoratorNode(node)) && !node.isInline();
87+
return ($isElementNode(node) || $isDecoratorNode(node)) && !node.isInline() && !$isRootNode(node);
8888
};
8989

9090
if (isBlockNode(node)) {

resources/js/wysiwyg/utils/selection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export function $insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: b
8282
}
8383

8484
export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
85-
const selection = $getSelection();
86-
const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
85+
const selectionNodes = $getSelection()?.getNodes() || [];
86+
const blockElement = selectionNodes.length > 0 ? $getNearestNodeBlockParent(selectionNodes[0]) : null;
8787

8888
if (blockElement) {
8989
if (insertAfter) {

0 commit comments

Comments
 (0)