A Slate plugin to handle table edition.
Demo: https://whatever-company.github.io/slate-tables/
$ npm login --registry=https://npm.pkg.github.com --scope=@whatever-company
> Username: USERNAME
> Password: TOKEN (from your github settings)
> Email: PUBLIC-EMAIL-ADDRESS
$ npm install @whatever-company/slate-tables
- Pressing Up/Down moves the cursor to the row above/below
- Pressing Left/Right moves the cursor to the cell before/after
- Pressing Enter inserts a new row (if enabled)
- Pressing Tab moves the cursor to the next cell and creats a new row
- Pressing Shift+Tab moves the cursor to the previous cell
The plugin supports nested tables natively.
Colspan and Rowspan are supported. All operations create a matrix containing all cells' positions.
Nothing is done to support copy/paste in this plugin. You have to implement it in you codebase.
import TablePlugin from '@eliumhq/slate-tables'
const tablePlugin = TablePlugin({
saveColumns: false,
enterCreatesRow: true,
blocks: {
table: 'table',
row: 'table-row',
cell: 'table-cell',
content: 'paragraph'
}
})
const plugins = [tablePlugin]
Here is what your Slate document containing tables should look like:
<value>
<document>
<paragraph>Some text</paragraph>
<table>
<table-row>
<table-cell colspan="2">
<paragraph>Any block can goes into cells</paragraph>
</table-cell>
</table-row>
<table-row>
<table-cell>
<paragraph>Second row</paragraph>
</table-cell>
<table-cell>
<paragraph>Second row</paragraph>
</table-cell>
</table-row>
</table>
</document>
</value>
Option object you can pass to the plugin.
prop | type | default | desc |
---|---|---|---|
saveColumns | boolean | false | when enabled, it will save the columns in an array of object in the table node's data (useful to render a <colgroup/> tag) |
enterCreatesRow | boolean | false | Add the support of the creation of a new row when user press enter |
blocks -> table | string | table | Node name for the <table /> |
blocks -> row | string | table-row | Node name for the <tr /> |
blocks -> cell | string | table-cell | Node name for the <td /> |
blocks -> content | string | paragraph | Node name for the default child of a cell |
Constructs an instance of the table plugin, for the given options. You can then add this instance to the list of plugins passed to Slate.
Creates a table at current selection.
Creates a table at the given range.
Creates a row at the current start block.
Creates a row after the focused one inside the table at key
. If atIndex
is given, it will insert the new row at this index.
Creates a row at the end of the focused table (start of the selection must be in a table)
Creates a column after the focused one. This will alter all row nodes to add a cell.
Creates a column after the row found at key
. This will alter all row nodes to add a cell.
Delete the focused column. This will alter all row nodes to remove a cell.
Delete the column at key
. This will alter all row nodes to remove a cell.
Deletes the focused table.
Deletes the focused at key
.
Deletes focused row.
Deletes the row at key
.
Deletes content in cells found in range
. It's necessary to use this instead of editor.deleteAtRange
to not delete row & cell nodes.
Merges properties
in focused cell's data.
Merges properties
in cell's data.
It will merges properties
with the current column's data (stored in table.data.columns
)
Merges column's data with properties
at given index
.
Merges properties
with table's data at current selection.
Merges properties
with table's data at key
.
Increases the rowspan of the cell at key
.
Increases the colspan of the cell at key
.
Decreases the rowspan of the cel at key
.
Decreases the colspan of the cel at key
.
Returns true if selection.start
or selection.end
is in a table node.
Returns true if range.start
or range.end
is in a table node.
Returns a Table
object for the table at key
. This object contains helpers to access sibling cells, rows, columns.
Returns the cell at key
.
Returns the row at key
.
Returns true if the cell at key
can have a colspan increased by one.
Returns true if the cell at key
can have a rowspan increased by one.
Returns true if the cell at key
can have a colspan decreased by one.
Returns true if the cell at key
can have a rowspan decreased by one.
Returns properties of the table at key