# Columns

> Arrange blocks in two or more editable columns, drag blocks between them, and resize adjacent columns.

`columns()` lays out blocks side by side. Each column can hold several blocks. Use a command to start with a fixed number of columns, or drag one block beside another to create a two-column layout.

## Try it

[Create, resize, and rearrange columns](https://gpdoc.dev/docs/plugins/structure/columns/#columns-example)

## Install

```sh
cargo add gpdoc-plugin-columns
```

The default `gpui` feature provides the side-by-side view and resize dividers. For a headless editor, pass `--no-default-features` to the install command. The document schema and editing commands work in either configuration.

## Example

Use a `columns` node with two `column` children to open the editor with blocks already side by side. The paragraph below the layout gives you a block to drag into either column.

```rust
use gpdoc::{Editor, EditorConfig, Node, PluginViewExt, ViewPlugin};
use gpdoc_plugin_columns::{column_layout, columns};
use gpdoc_plugin_drag_handle::drag_handle;
use gpdoc_plugin_drop_cursor::drop_cursor;
use gpdoc_plugin_paragraph::paragraph;
use gpui::{div, prelude::*, rgb};


fn column_view() -> ViewPlugin {
    column_layout(|track, _, _| {
        track.divider(div().border_r_1().border_color(rgb(0xd4d4d4)))
    })
}


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(columns().with_view(column_view))
    .plugin(drag_handle())
    .plugin(drop_cursor())
    .build()?;


let layout = Node::container("columns", vec![
    Node::container("column", vec![Node::textblock("paragraph", "Draft the outline")]),
    Node::container("column", vec![Node::textblock("paragraph", "Collect feedback")]),
]);
let blocks = vec![layout, Node::textblock("paragraph", "Publish the notes")];
let mut editor = Editor::from_document(&config, blocks)?;
```

Columns defines where a block can land. Drag handle supplies the grip and moves the block. Drop cursor paints the insertion line. See [Drag handle](/docs/plugins/behavior/drag-handle/) and [Drop cursor](/docs/plugins/behavior/drop-cursor/).

## Compose a column layout

`column_layout` calls your renderer once per column. The plugin measures the column, applies its share of the available width, and handles dragging and resizing.

### Anatomy

```text
column_layout(...)
└── ColumnTrack
    ├── content(...)  Container for the editable blocks
    └── divider(...)  Resize handle between this column and the next
```

### Style the content and divider

Replace `column_view` in the example above to tint each column and highlight the divider on hover. The plugin inserts the blocks into the container and positions the divider on the right edge of each column except the last.

```rust
fn column_view() -> ViewPlugin {
    column_layout(|track, _, _| {
        track
            .content(div().rounded_sm().bg(rgb(0xf4f6f8)))
            .divider(div().border_r_1().border_color(rgb(0xc4c9cf))
                .hover(|handle| handle.border_color(rgb(0x346bb0))))
    })
}
```

## Create three columns

Start with a paragraph, wrap it in three columns, and type into the second column.

```rust
use gpdoc_plugin_columns::Set;


let outline = Node::textblock("paragraph", "Draft the outline");
let mut editor = Editor::from_document(&config, vec![outline.clone()])?;
editor.run(Set::Three)?;
editor.insert_text("Collect feedback")?;


let group = &editor.document().blocks()[0];
assert_eq!(group.child_count(), 3);
assert_eq!(group.child(0).child(0), &outline);
assert_eq!(group.child(1).child(0), &Node::textblock("paragraph", "Collect feedback"));
assert_eq!(group.child(2).child(0), &Node::textblock("paragraph", ""));
```

## Drag and drop

Hover beside a block inside a column to reveal its grip. The group and the individual column wrappers have no grip. To resize a column, drag the divider between columns.

| Where you release the grip                                                  | Insertion line                | Result                                                    |
| --------------------------------------------------------------------------- | ----------------------------- | --------------------------------------------------------- |
| Left or right edge of a standalone block                                    | Vertical                      | Create two columns with the dragged content on that side. |
| Side edge of an existing column, including the boundary between two columns | Vertical                      | Insert a sibling column containing the dragged blocks.    |
| Above or below a block inside a column                                      | Horizontal, within the column | Insert the dragged blocks at that position.               |
| Blank space below the last block in a shorter column                        | Horizontal, within the column | Append the dragged blocks to that column.                 |
| Top or bottom edge of the whole group                                       | Horizontal, across the group  | Move the dragged blocks above or below the layout.        |

Moving the last block out of a column removes that column. If only one column remains, its blocks return to the parent in order. Undo restores the move and the previous layout together.

## Document structure and widths

A `columns` node holds at least two `column` children. Each `column` holds one or more blocks. For example, the first column below holds two paragraphs.

```text
columns
├── column
│   ├── paragraph "Draft the outline"
│   └── paragraph "Review the draft"
└── column
    └── paragraph "Collect feedback"
```

Each `column` stores an integer `weight` attribute, which defaults to `10000`. The view divides the available width in proportion to these weights. Two columns with equal weights each take half the width. Dragging their divider updates the two neighboring weights in one undoable edit. The widths persist when you save and reload the document.

## Commands

| Command                                                           | When to use it                                                                                                                            | Selection after the command                                                                                                |
| ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `Set::Two`, `Set::Three`, `Set::Four`, `Set::Five` (`setColumns`) | Wrap the current block or adjacent selected blocks in the first column. Each additional column starts with an empty text block.           | When wrapping one empty text block, typing starts in the first column. With existing content, typing starts in the second. |
| `AddAfter` (`addLayoutColumnAfter`)                               | With the selection inside one column, insert a column immediately to its right.                                                           | The caret moves into the new column.                                                                                       |
| `Unset` (`unsetColumns`)                                          | With the selection inside a group, unwrap it. Blocks return to the parent from left to right, in their original order within each column. | The selection moves to the first lifted block.                                                                             |

Use `can` to enable an “Add column” action only where it applies.

```rust
use gpdoc_plugin_columns::AddAfter;


if editor.can(AddAfter)? {
    editor.run(AddAfter)?;
}
```

## Source code

* [Column schema and commands](/source/plugins/columns/src/lib.rs.txt)
* [GPUI layout and drop destinations](/source/plugins/columns/src/view.rs.txt)
* [Divider interaction and width updates](/source/plugins/columns/src/view/resize.rs.txt)