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.
Try it
Section titled “Try it”Install
Section titled “Install”cargo add gpdoc-plugin-tableRegister table() to get an editable grid with cell selection and column resizing.
Example
Section titled “Example”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.
Compose a table grid
Section titled “Compose a table grid”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.
Anatomy
Section titled “Anatomy”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 boundaryAssemble the cell and resize guide
Section titled “Assemble the cell and resize guide”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.
Insert a table with a header row
Section titled “Insert a table with a header row”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);Settings
Section titled “Settings”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.
Table dimensions
Section titled “Table dimensions”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 |
Cell attributes
Section titled “Cell attributes”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.
Commands
Section titled “Commands”Insert or remove a table
Section titled “Insert or remove a table”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.
Select cells
Section titled “Select cells”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) })?;Add or delete rows and columns
Section titled “Add or delete rows and columns”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)?;Merge or split cells
Section titled “Merge or split cells”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)?;Change headers and alignment
Section titled “Change headers and alignment”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() })?;Resize a column
Section titled “Resize a column”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 })?;Repair a table grid
Section titled “Repair a table grid”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)?;Keyboard shortcuts
Section titled “Keyboard shortcuts”| 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)?;