Skip to content
gpdoc
Playground

Table

Insert tables and edit their rows, columns, headers, and cells.

table() registers the table, row, cell, and header cell nodes together. You can select a rectangle of cells to merge or edit them as a group.

Terminal window
cargo add gpdoc-plugin-table

Register table() to get an editable grid with cell selection and column resizing.

Register the supplied bordered cells and resize guide in the Quickstart editor configuration.

use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_paragraph::paragraph;
use gpdoc_plugin_table::{default_cell, default_resize_guide, table, table_grid};
let config = EditorConfig::builder()
.plugin(paragraph())
.plugin(table().with_view(grid_view))
.build()
.expect("configure editor");
fn grid_view() -> ViewPlugin {
table_grid(default_cell, default_resize_guide)
}

Insert a table with gpdoc_plugin_table::Insert::default(). Drag a column boundary to resize it.

The grid measures cells, selects them on pointer input, and resizes columns. Supply a renderer for each cell and a painter for the resize guide.

table_grid(cell, resize_guide)
├── cell Renderer for each measured cell
│ ├── cell.layer(...) Optional decoration behind the editable blocks
│ └── cell.content(...) Container for the editable blocks
└── resize_guide Painter for the dragged column boundary

Replace grid_view in the example above.

use gpui::{div, prelude::*};
fn grid_view() -> ViewPlugin {
table_grid(|cell, _, _| {
let content = if cell.is_header() { div().child("Header") } else { div() };
cell.content(content)
}, default_resize_guide)
}

The grid adds the editable blocks to your container. To decorate a full cell, use cell.layer(...) before cell.content(...). The grid paints layers in the order you add them.

The default table() view uses the surrounding text color for selection and resizing. default_cell and default_resize_guide provide the bordered cells and blue guide used in the first example.

use gpdoc::{Editor, Node};
use std::num::NonZeroUsize;
let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "")])?;
editor.run(gpdoc_plugin_table::Insert {
rows: NonZeroUsize::new(3).unwrap(),
columns: NonZeroUsize::new(2).unwrap(),
with_header_row: true,
})?;
let nodes = editor.document().iter_nodes();
assert_eq!(nodes.filter(|(_, node)| node.kind == "tableHeader").count(), 2);

Register gpdoc_plugin_table::table() with paragraph() to install all four table node types. The same registration supplies the table commands and cell selection behavior.

Set the dimensions and header option on gpdoc_plugin_table::Insert. Each new cell starts with an empty paragraph.

Argument Type Default
rows NonZeroUsize 3
columns NonZeroUsize 3
with_header_row bool true

Regular cells and header cells both have integer colspan and rowspan attributes (default 1), nullable string align (default null), and JSON colwidth (default null). Resizing a column supplies one width entry per column the cell spans.

Insert creates a table with up to 100,000 cells at a selection that allows a table. In an empty paragraph or a fully selected text block, it replaces that block. To create a default table, run Insert::default().

editor.run(gpdoc_plugin_table::Insert::default())?;

To remove the table containing the start of the selection, run Delete. Use DeleteSelected to remove a table only when a cell selection covers every cell.

SetSelection selects a rectangle between two cells in the same table. Pass the document position immediately before the first cell as anchor_cell. The optional head_cell defaults to the anchor. Both are nonnegative integers. In the empty 3-by-2 table from the example above, the first two header cells start at positions 2 and 6.

editor.run(gpdoc_plugin_table::SetSelection { anchor_cell: 2, head_cell: Some(6) })?;

Place the caret in a cell or select a rectangle. These commands take no arguments.

Command Result
AddRowBefore, AddRowAfter Insert a row above or below the selection.
AddColumnBefore, AddColumnAfter Insert a column to the left or right of the selection.
DeleteRow, DeleteColumn Remove the selected rows or columns. At least one row and one column must remain.
editor.run(gpdoc_plugin_table::AddRowAfter)?;

Run MergeCells on a rectangular selection of multiple cells. Every selected cell must fit entirely inside the rectangle. Run SplitCell inside a single spanning cell to divide it. Its content stays in the upper-left cell. Use MergeOrSplit to choose between these actions based on the selection.

editor.run(gpdoc_plugin_table::MergeCells)?;

Inside the table, run ToggleHeaderRow or ToggleHeaderColumn to toggle the first row or first column. Use ToggleHeaderCell to turn selected cells into headers. If any selected cell is already a header, the selection becomes regular cells.

SetCellAttribute applies an attribute to the current cell or all selected cells. Pass the attribute’s string name and a value of the matching type.

editor.run(gpdoc_plugin_table::SetCellAttribute { name: "align".into(), value: "center".into() })?;

ResizeColumn updates every cell covering the specified column. Pass a document position identifying the table in cell, a zero-based integer column, and a positive integer width. The position in this example identifies the first cell of the example table.

editor.run(gpdoc_plugin_table::ResizeColumn { cell: 2, column: 0, width: 180 })?;

Fix scans the document for malformed table grids and repairs them. You can run it with the selection outside a table.

editor.run(gpdoc_plugin_table::Fix)?;
Shortcut Command Result
Tab Tab Select the next cell’s text. At the last cell, append a row and advance into it.
Shift + Tab PreviousCell Select the previous cell’s text, if there is a previous cell.
Backspace, Delete, or either key with Mod DeleteSelected Remove the table when every cell is selected.

To move forward only when the next cell exists, use GoToNextCell.

editor.run(gpdoc_plugin_table::GoToNextCell)?;