return {
...super.exportDOM(editor),
after: (tableElement) => {
- if (tableElement) {
- const newElement = tableElement.cloneNode() as ParentNode;
- const colGroup = document.createElement('colgroup');
- const tBody = document.createElement('tbody');
- if (isHTMLElement(tableElement)) {
- tBody.append(...tableElement.children);
- }
- const firstRow = this.getFirstChildOrThrow<TableRowNode>();
-
- if (!$isTableRowNode(firstRow)) {
- throw new Error('Expected to find row node.');
- }
+ if (!tableElement) {
+ return;
+ }
- const colCount = firstRow.getChildrenSize();
+ const newElement = tableElement.cloneNode() as ParentNode;
+ const tBody = document.createElement('tbody');
- for (let i = 0; i < colCount; i++) {
- const col = document.createElement('col');
- colGroup.append(col);
+ if (isHTMLElement(tableElement)) {
+ for (const child of Array.from(tableElement.children)) {
+ if (child.nodeName === 'TR') {
+ tBody.append(child);
+ } else {
+ newElement.append(child);
+ }
}
+ }
- newElement.replaceChildren(colGroup, tBody);
+ newElement.append(tBody);
- return newElement as HTMLElement;
- }
+ return newElement as HTMLElement;
},
};
}
return;
}
+ const targetColWidth = Math.min(Math.round(840 / columns), 240);
+ const colWidths = Array(columns).fill(targetColWidth + 'px');
+
this.getContext().editor.update(() => {
const table = $createTableNodeWithDimensions(rows, columns, false) as CustomTableNode;
+ table.setColWidths(colWidths);
$insertNewBlockNodeAtSelection(table);
});
}
import {
$getBlockElementNodesInSelection,
$getNodeFromSelection,
- $insertNewBlockNodeAtSelection, $selectionContainsNodeType,
+ $insertNewBlockNodeAtSelection, $selectionContainsNodeType, $selectSingleNode,
$toggleSelectionBlockNodeType,
getLastSelection
} from "./selection";
editor.update(() => {
const codeBlock = $createCodeBlockNode();
codeBlock.setCode(selection?.getTextContent() || '');
- $insertNewBlockNodeAtSelection(codeBlock, true);
+
+ const selectionNodes = $getBlockElementNodesInSelection(selection);
+ const firstSelectionNode = selectionNodes[0];
+ const extraNodes = selectionNodes.slice(1);
+ if (firstSelectionNode) {
+ firstSelectionNode.replace(codeBlock);
+ extraNodes.forEach(n => n.remove());
+ } else {
+ $insertNewBlockNodeAtSelection(codeBlock, true);
+ }
+
$openCodeEditorForNode(editor, codeBlock);
- codeBlock.selectStart();
+ $selectSingleNode(codeBlock);
});
} else {
$openCodeEditorForNode(editor, codeBlock);
import {
$getRoot,
$isDecoratorNode,
- $isElementNode,
+ $isElementNode, $isRootNode,
$isTextNode,
ElementNode,
LexicalEditor,
export function $getNearestNodeBlockParent(node: LexicalNode): LexicalNode|null {
const isBlockNode = (node: LexicalNode): boolean => {
- return ($isElementNode(node) || $isDecoratorNode(node)) && !node.isInline();
+ return ($isElementNode(node) || $isDecoratorNode(node)) && !node.isInline() && !$isRootNode(node);
};
if (isBlockNode(node)) {
}
export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
- const selection = $getSelection();
- const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
+ const selectionNodes = $getSelection()?.getNodes() || [];
+ const blockElement = selectionNodes.length > 0 ? $getNearestNodeBlockParent(selectionNodes[0]) : null;
if (blockElement) {
if (insertAfter) {