From 9ae5f281c27c49353b51dbde8a5e5a56bbd77a2a Mon Sep 17 00:00:00 2001 From: LIlGG <1103069291@qq.com> Date: Thu, 11 Jan 2024 14:39:50 +0800 Subject: [PATCH] pref: add additional attributes and colgroup for tables --- .../editor/src/extensions/table/index.ts | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/console/packages/editor/src/extensions/table/index.ts b/console/packages/editor/src/extensions/table/index.ts index e1bb717c84..9a597a0099 100644 --- a/console/packages/editor/src/extensions/table/index.ts +++ b/console/packages/editor/src/extensions/table/index.ts @@ -1,9 +1,18 @@ -import TiptapTable, { type TableOptions } from "@tiptap/extension-table"; -import { isActive, type Editor, type Range } from "@/tiptap/vue-3"; -import type { - Node as ProseMirrorNode, - NodeView, - EditorState, +import TiptapTable, { + type TableOptions, + createColGroup, +} from "@tiptap/extension-table"; +import { + isActive, + type Editor, + type Range, + mergeAttributes, +} from "@/tiptap/vue-3"; +import { + type Node as ProseMirrorNode, + type NodeView, + type EditorState, + type DOMOutputSpec, } from "@/tiptap/pm"; import TableCell from "./table-cell"; import TableRow from "./table-row"; @@ -375,6 +384,26 @@ const Table = TiptapTable.extend({ }, }; }, + + renderHTML({ node, HTMLAttributes }) { + const { colgroup, tableWidth, tableMinWidth } = createColGroup( + node, + this.options.cellMinWidth + ); + + const table: DOMOutputSpec = [ + "table", + mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, { + style: tableWidth + ? `width: ${tableWidth}` + : `minWidth: ${tableMinWidth}`, + }), + colgroup, + ["tbody", 0], + ]; + + return table; + }, }).configure({ resizable: true }); export default Table;