@domternal/extension-table

中文文档
1.8.1发布于 2026/11/08MITTypeScriptESM + CJS零依赖近 30 天下载 2128

Table extension for Domternal editor

原始使用文档
文档目录8 节
  1. Install
  2. Quick start
  3. Configure resizing and UI
  4. Work with tables
  5. Optional table layout settings
  6. Content and paste
  7. More documentation
  8. License

@domternal/extension-table

Version MIT License

Editable tables for Domternal, built on prosemirror-tables. Includes cell merge/split, column resizing, header rows and columns, cell styling, selection and keyboard navigation. The built-in table view supplies row/column handles and a cell toolbar.

Documentation · Examples

Install

The package declares Node.js 22 or later for tooling.

pnpm add @domternal/core @domternal/pm @domternal/extension-table @domternal/theme

Core and pm are peers with the range >=1.8.0 <2.0.0. Use version 1.8.1 for all installed Domternal Free packages. The example uses the optional theme for table layout and UI; you can supply your own styles instead.

Quick start

<div id="editor" class="dm-editor"></div>
import { Editor, StarterKit } from '@domternal/core';
import { Table } from '@domternal/extension-table';
import '@domternal/theme';

const editor = new Editor({
  element: document.getElementById('editor')!,
  extensions: [StarterKit, Table],
});

editor.commands.insertTable({ rows: 3, cols: 3, withHeaderRow: true });

Table registers TableRow, TableCell, TableHeader and Gapcursor automatically. You do not need to add those separately. Call editor.destroy() when removing the editor.

Configure resizing and UI

Replace Table in the extension array with a configured instance:

Table.configure({
  resizeBehavior: 'neighbor',
  constrainToContainer: true,
  cellMinWidth: 25,
  defaultCellMinWidth: 100,
});
OptionDefaultPurpose
resizeBehavior'neighbor'Resize against the neighboring column; 'independent' grows the table and 'redistribute' redistributes widths.
constrainToContainertrueLimit growth to available space, with horizontal scrolling when minimum widths cannot fit.
cellMinWidth25Minimum column width while dragging, in pixels.
defaultCellMinWidth100Width floor for columns without explicit widths, in pixels.
allowTableNodeSelectionfalseAllow selecting the whole table as a node.
ViewTableViewReplace the built-in node view, or use null to omit its handles and cell toolbar.

Disabling the built-in view does not disable the column-resizing plugins. Adding a column preserves existing custom widths where possible, using free space before borrowing from nearby columns. See resize behavior and container constraints.

Work with tables

Commands use the current cell or cell selection:

editor.commands.addRowAfter();
editor.commands.addColumnAfter();
editor.commands.setCellAttribute('background', '#fff3cd');
editor.commands.setCellAttribute('textAlign', 'center');
TaskCommands
Create or remove a tableinsertTable, deleteTable
Change rows or columnsaddRowBefore, addRowAfter, deleteRow, addColumnBefore, addColumnAfter, deleteColumn
Merge or split selected cellsmergeCells, splitCell
Toggle headerstoggleHeaderRow, toggleHeaderColumn, toggleHeaderCell
Select or navigate cellssetCellSelection, goToNextCell, goToPreviousCell
Style cellssetCellAttribute

Merge requires a compatible cell selection. Commands return false when the current selection or table does not support the operation. Use editor.can() when enabling your own controls. See the command reference for arguments, cell positions and repair helpers.

Tab moves to the next cell and can add a final row; Shift+Tab moves back. Inside list and task items, list indentation takes precedence. Selecting all cells and pressing Backspace or Delete removes the table.

Optional table layout settings

Free 1.6.0 adds opt-in TableLayout. Add it beside Table to store explicit row heights, cell padding/borders and page-break preferences. Without it, the existing table schema is unchanged. Enable it on every collaborative writer before storing these attributes.

import { Table, TableLayout } from '@domternal/extension-table';

const extensions = [StarterKit, Table, TableLayout];

editor.commands.setTableLayout({ breakBefore: 'page' });
editor.commands.setTableRowLayout({ height: 48, heightRule: 'atLeast', allowSplit: false });
editor.commands.setTableCellLayout({
  padding: { top: 8, right: 12, bottom: 8, left: 12 },
  borders: { bottom: { width: 1, style: 'solid', color: '#445566' } },
});

Commands act on the current table, rows or cells. Passing null clears the corresponding setting. They participate in editor transactions and undo; the extension does not add a settings panel. editor.can() checks availability.

JSON stores tableLayout, rowLayout and cellLayout. Dimensions are CSS pixels: row height is 1–4,096, padding 0–256 per side and border width 0–16 per side. heightRule requires a height. Borders accept solid, dashed, dotted, double or none and an explicit six-digit hex color. HTML parsing also accepts absolute px, pt, mm, cm and in dimensions; percentages, calc() and var() are not resolved. Browsers treat row height as a minimum, even when the stored Word preference is exact. A browser can override a requested print break to fit the available page.

The read-only getNodeTableLayout, getNodeTableRowLayout and getNodeTableCellLayout helpers return validated settings only for nodes from the opt-in schema.

The separate Pro Export package supports a documented subset of these settings, as described in the Export guide. That implementation uses native Word settings and bounded PDF approximations. With allowSplit: false, its PDF output keeps a body row together only after it fits a fresh page with its repeated headers and final fonts. Oversized or unmeasurable rows remain splittable with a warning, preserving their content.

Content and paste

Cell pastes preserve table structure and grow the destination when needed. Add PasteCleanup explicitly for Office HTML normalization; it is not included in Table.

HTML and JSON loading bound rendered spans to 1 through 1,000 and report unsupported JSON spans through content diagnostics. Strict schema validation requires positive safe-integer spans. A shared document may still contain unsupported stored spans; some table operations remain unavailable until an explicit migration repairs them. Rendering does not silently rewrite that shared content. See span validation and the Core migration guide before migrating existing collaborative documents.

A cell selection made before a remote span becomes unsupported falls back to a text caret without rebuilding that table's map or deleting its cells. The explicit fixTables command also refuses a document that still contains unsupported spans. Normalizing those spans restores ordinary table commands and cell selections.

Column widths are null or an array of at most 1,000 finite, non-negative numbers no larger than Number.MAX_SAFE_INTEGER. Zero means an automatic width; fractional CSS widths remain supported. Invalid JSON widths become null with an unsupported-table-width diagnostic. HTML parsing discards invalid widths, and rendering a stored invalid width uses automatic sizing without rewriting shared content. normalizeContentAttributes({ codes: ['unsupported-table-width'] }) performs that explicit repair when the application chooses to migrate it.

PasteCleanup applies its own structural budgets and rejects a span above 1,000; that differs from Table's ordinary HTML parsing, which caps the span. Cell colors and alignment values also follow Core's style policy.

More documentation

  • Table guide: schema, attributes, cell paste, styling and full options.
  • Exports: TableView, createTable, CellSelection, TableMap and related helpers.
  • Localization: tableMessages and optional @domternal/extension-table/locales/de catalogs.

License

MIT. Adapted prosemirror-tables code is covered by the package's third-party notices.

版本与发布记录

@domternal/extension-table 共发布 47 个版本,首次发布于 2026/03/22;过去 12 个月发布了 47 个版本。

版本发布日期距今
1.8.1latest2026/11/085 天前
1.8.02026/10/2124 天前
1.7.02026/10/2025 天前
1.6.02026/10/1926 天前
1.5.12026/10/1628 天前
1.5.02026/10/1629 天前

关于 @domternal/extension-table 的常见问题

@domternal/extension-table 是什么?

Table extension for Domternal editor

如何安装 @domternal/extension-table?

在项目目录运行 npm install @domternal/extension-table,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@domternal/pm(>=1.8.0 <2.0.0)、@domternal/core(>=1.8.0 <2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@domternal/extension-table 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@domternal/extension-table 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@domternal/extension-table 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@domternal/extension-table 可以商用吗?

@domternal/extension-table 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@domternal/extension-table 的下载量有多少?

根据 npm 官方下载统计,@domternal/extension-table 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,128 次,日均约 71 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@domternal/extension-table 还在维护吗?

最新版本 1.8.1 发布于 2026/11/08(5 天前);至今共发布 47 个版本,其中过去 12 个月发布了 47 个;首次发布于 2026/03/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。