![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
slate-table
Advanced tools
A set of utilities for simple and flexible table editing in your slate editor.
Demo · Features · Documentation
Rendering tables in slate is easy, but incorporating the functionality to work with more complex table structures can be tricky and time-consuming. slate-table
offers essential utilities and incorporates sensible default behavior to help you craft the table experience your domain editor needs.
🚧 This package is currently in beta, and there may be breaking changes to both behavior and API. If you come across any difficulties or would like to share your feedback, feel free to raise an issue.
Some fundamental features that are currently available:
Feature | Implemented |
---|---|
Creating and deleting tables | ✅ |
Inserting and deleting rows | ✅ |
Inserting and deleting columns | ✅ |
Merge and split cells | ✅ |
Table navigation | ✅ |
Nested tables | 🙅 |
Support for nested tables is expected in future updates.
slate-table
is view-layer agnostic, so it doesn't ship with any predefined components. While this means that you'll need to create your own components, it also gives you the freedom to shape the table experience your domain editor needs.
It is also important to note that slate-table
makes some assumptions about your schema:
type
property: Make sure to specify a type
property for every table block in your schema.rowSpan
and colSpan
properties. Make sure to expect them in your components.💡 You can refer to the source code of the example implementation for further insights.
This module is distributed via npm and should be installed as one of your project's dependencies:
npm install slate-table
💡 This package also depends on
slate
. Please make sure you have it installed.
withTable
The withTable
function is the Slate plugin that enhances the editor behavior for tables, including selection, deletion, and normalization. The options
parameter allows you to specify the type
property for the corresponding table blocks and disable any default behavior.
💡 The options passed to the
withTable
function will be used in all subsequent table operations.
import { withTable } from "slate-table";
const editor = withTable(editor, {
blocks: {
table: "table",
thead: "table-head",
tbody: "table-body",
tfoot: "table-footer",
tr: "table-row",
th: "header-cell",
td: "table-cell",
content: "paragraph",
},
withDelete: true,
withFragments: true,
withInsertText: true,
withNormalization: true,
withSelection: true,
withSelectionAdjustment: true,
});
TableEditor
The TableEditor
simplifies table editing by providing a set of static methods for inserting and removing tables, rows, and columns, as well as merging and splitting cells.
insertTable(editor: Editor, options?: Partial<InsertTableOptions>): void
Inserts a table at the specified location with the specified number of rows. If no location is specified it will be inserted at the current selection.
import { TableEditor } from "slate-table";
TableEditor.insertTable(editor, { rows: 3, cols: 3, at: [] });
removeTable(editor: Editor, options?: { at?: Location }): void
Removes a table at the specified location. If no location is specified it will remove the table at the current selection.
import { TableEditor } from "slate-table";
TableEditor.removeTable(editor);
insertRow(editor: Editor, options?: { at?: Location, before?: boolean }): void
Inserts a new row at the specified location. If no location is specified it will insert the row at the current selection.
import { TableEditor } from "slate-table";
TableEditor.insertRow(editor, { before: true });
removeRow(editor: Editor, options?: { at?: Location }): void
Removes the row at the specified location. If no location is specified it will remove the row at the current selection.
import { TableEditor } from "slate-table";
TableEditor.removeRow(editor);
insertColumn(editor: Editor, options?: { at?: Location, before?: boolean }): void
Inserts a column at the specified location. If no location is specified it will insert the column at the current selection.
import { TableEditor } from "slate-table";
TableEditor.insertColumn(editor, { before: true });
removeColumn(editor: Editor, options?: { at?: Location }): void
Removes the column at the specified location. If no location is specified it will remove the column at the current selection.
import { TableEditor } from "slate-table";
TableEditor.removeColumn(editor);
canMerge(editor: Editor): boolean
Checks if the current selection can be merged. Merging is not possible when any of the following conditions are met:
import { TableEditor } from "slate-table";
TableEditor.canMerge(editor);
merge(editor: Editor): void
Merges the selected cells in the table.
import { TableEditor } from "slate-table";
TableEditor.merge(editor);
split(editor: Editor, options?: { at?: Location; all?: boolean }): void
Splits either the cell at the current selection or a specified location. If a range selection is present, all cells within the range will be split.
import { TableEditor } from "slate-table";
TableEditor.split(editor, { all: true });
TableCursor
The TableCursor
offers a set of static methods for manipulating the cursor within the table and retrieving the table selection.
isInTable(editor: Editor, options?: { at?: Location }): boolean
Returns true
if the selection is inside a table, otherwise false
.
import { TableCursor } from "slate-table";
TableCursor.isInTable(editor);
upward(editor: Editor, options?: { mode?: SelectionMode }): boolean
Moves the cursor to the cell above the current selection and returns true
if the action was successful.
import { TableCursor } from "slate-table";
TableCursor.upward(editor);
downward(editor: Editor, options?: { mode?: SelectionMode }): boolean
Moves the cursor to the cell below the current selection and returns true
if the action was successful.
import { TableCursor } from "slate-table";
TableCursor.downward(editor);
forward(editor: Editor, options?: { mode?: SelectionMode }): boolean
Moves the cursor to the cell next to the current selection and returns true
if the action was successful.
import { TableCursor } from "slate-table";
TableCursor.forward(editor);
backward(editor: Editor, options?: { mode?: SelectionMode }): boolean
Moves the cursor to the cell before to the current selection and returns true
if the action was successful.
import { TableCursor } from "slate-table";
TableCursor.backward(editor);
isOnEdge(editor: Editor, edge: "start" | "end" | "top" | "bottom"): boolean
Checks if the selection is positioned on an edge within a "td" or "th" block.
start
: checks if the cursor is positioned at the start of the cell's contentend
: checks if the cursor is positioned at the end of the cell's contenttop
: checks if the cursor is positioned at the first block of the cell's contentbottom
: checks if the cursor is positioned at the last block of the cell's contentimport { TableCursor } from "slate-table";
TableCursor.isOnEdge(editor, "end");
isInFirstCell(editor: Editor, options?: { reverse?: boolean }): boolean
Checks if the cursor is in the first cell of the table.
import { TableCursor } from "slate-table";
TableCursor.isInFirstCell(editor);
isInLastCell(editor: Editor): boolean
Checks if the cursor is in the last cell of the table. This is the reverse of TableCursor.isInFirstCell
and is provided for legibility.
import { TableCursor } from "slate-table";
TableCursor.isInLastCell(editor);
isInFirstRow(editor: Editor, options?: { reverse?: boolean }): boolean
Checks if the cursor is in the first tr of the table.
import { TableCursor } from "slate-table";
TableCursor.isInFirstRow(editor);
isInLastRow(editor: Editor): boolean
Checks if the cursor is in the last row of the table. This is the reverse of TableCursor.isInFirstRow
and is provided for legibility.
import { TableCursor } from "slate-table";
TableCursor.isInLastRow(editor);
selection(editor: Editor): Generator<NodeEntry[]>
A generator that retrieves a matrix of NodeEntry[][]
containing the selected cells.
import { TableCursor } from "slate-table";
const selected = [...TableCursor.selection(editor)];
unselect(editor: Editor): void
Clears the table selection from the editor.
import { TableCursor } from "slate-table";
TableCursor.unselect(editor);
isSelected(editor: Editor, element: Element): boolean
Checks whether a given cell is part of the current table selection.
import { TableCursor } from "slate-table";
const isSelected = TableCursor.isSelected(editor, element);
FAQs
table plugin for slate.js
We found that slate-table demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.