Skip to content
GitHub

Table

The table extension adds support for creating and editing tables in your document. It provides a set of commands and utilities for working with table structures.

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { defineExtension } from './extension'
import { TableHandle } from './table-handle'

export default function Editor() {
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({ extension, defaultContent })
  }, [])

  return (
    <ProseKit editor={editor}>
      <div className='box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow flex flex-col bg-white dark:bg-gray-950 color-black dark:color-white'>
        <div className='relative w-full flex-1 box-border overflow-y-scroll'>
          <div ref={editor.mount} className='ProseMirror box-border min-h-full px-[max(4rem,_calc(50%-20rem))] py-8 outline-none outline-0 [&_span[data-mention="user"]]:text-blue-500 [&_span[data-mention="tag"]]:text-violet-500'></div>
          <TableHandle />
        </div>
      </div>
    </ProseKit>
  )
}

const defaultContent = `
<table><tbody>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
  </tr>
</tbody></table>
`
import { defineTablefunction defineTable(): TableExtension
@public
} from 'prosekit/extensions/table'
const extensionconst extension: TableExtension = defineTablefunction defineTable(): TableExtension
@public
()

Insert a new table.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.insertTable
insertTable: CommandAction
(options: InsertTableOptions) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
({ rowInsertTableOptions.row: number
The number of rows in the table.
: 3, colInsertTableOptions.col: number
The number of columns in the table.
: 3, headerInsertTableOptions.header?: boolean | undefined
Whether the table has a header row.
@defaultfalse
: true })

Exit the table.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.exitTable
exitTable: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Select the table.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.selectTable
selectTable: CommandAction
(options?: SelectTableOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Select the table cell.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.selectTableCell
selectTableCell: CommandAction
(options?: SelectTableCellOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Select the table column.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.selectTableColumn
selectTableColumn: CommandAction
(options?: SelectTableColumnOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Select the table row.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.selectTableRow
selectTableRow: CommandAction
(options?: SelectTableRowOptions | undefined) => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Add a new column before the selected column.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.addTableColumnBefore
addTableColumnBefore: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Add a new column after the selected column.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.addTableColumnAfter
addTableColumnAfter: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Add a new row above the selected row or specific position.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.addTableRowAbove
addTableRowAbove: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Add a new row below the selected row or specific position.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.addTableRowBelow
addTableRowBelow: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Delete the selected table.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.deleteTable
deleteTable: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Delete the selected column or specific position.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.deleteTableColumn
deleteTableColumn: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Delete the selected row or specific position.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.deleteTableRow
deleteTableRow: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Delete the cell selection.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.deleteCellSelection
deleteCellSelection: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Merge the selected cells.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.mergeTableCells
mergeTableCells: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Split the selected cell.

editorconst editor: Editor<TableExtension>.commands
Editor<TableExtension>.commands: ToCommandAction<{
    insertTable: [options: InsertTableOptions];
    exitTable: [];
    selectTable: [options?: SelectTableOptions];
    ... 12 more ...;
    splitTableCell: [];
}>
All {@link CommandAction } s defined by the editor.
.splitTableCell
splitTableCell: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

These plugins are built-in plugins in prosemirror-tables.

import { defineTablePluginsfunction defineTablePlugins(): PlainExtension
@public
} from 'prosekit/extensions/table'
const extensionconst extension: PlainExtension = defineTablePluginsfunction defineTablePlugins(): PlainExtension
@public
()

You can use the TableHandle component to control the table.