# Task list

> Create checklists with a saved checked state for each item.

`task_list()` registers the checklist container. `task_item()` registers the entries and stores each entry’s completion state in a boolean `checked` attribute.

## Try it

[Try the task-list editor example](https://gpdoc.dev/docs/plugins/structure/task-list/#task-list-example)

## Install

```sh
cargo add gpdoc-plugin-task-list
```

## Example

Register the checklist container and its clickable task rows. Use this configuration in the [Quickstart editor](/docs/quickstart/#configure-the-editor).

```rust
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_paragraph::paragraph;
use gpdoc_plugin_task_list::{TaskRow, task_item, task_list, task_rows};


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(task_item().with_view(task_view))
    .plugin(task_list())
    .build()
    .expect("configure editor");


fn task_view() -> ViewPlugin {
    task_rows(TaskRow::render)
}
```

Type `[x]` followed by a space to start a completed task. Click its checkbox to clear the checked state.

## Compose a GPUI checklist

Each task row receives the checked state, a checkbox that updates the document, and the editable task body.

### Anatomy

```text
task_rows(...)
└── div()              Task row
    ├── task.checkbox  Toggles the checked state and focuses the editor
    └── task.content   Editable task body
```

### Assemble the checkbox and task body

Replace `task_view` in the example above.

```rust
use gpui::{div, prelude::*};


fn task_view() -> ViewPlugin {
    task_rows(|task, _, _| {
        let mark = if task.checked { "☑" } else { "☐" };
        div().flex().child(task.checkbox.child(mark)).child(task.content)
    })
}
```

Put the glyph inside `task.checkbox` so clicking it toggles the task.

## Create a checked task

```rust
use gpdoc::{Editor, Node, Value};


let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "")])?;
editor.insert_text("[x] ")?;
editor.insert_text("Write the release notes")?;


let (_, item) = editor.document().iter_nodes().find(|(_, node)| {
  node.kind == "taskItem"
}).unwrap();
assert_eq!(item.attrs["checked"], Value::Bool(true));
```

## Settings

Register `gpdoc_plugin_task_list::task_list()` for the container. Register `task_item()` for the entries.

### `nested`

Defaults to `false`. To allow subtasks, register `task_item_with_nesting(nested: bool)` in place of `task_item()`. This enables the `sinkTaskItem` command.

```rust
use gpdoc::EditorConfig;
use gpdoc_plugin_task_list::task_item_with_nesting;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(task_item_with_nesting(true))
    .plugin(task_list())
    .build()?;
```

### `checked`

A boolean on each `taskItem`, default `false`. Completion survives a document JSON round trip.

## Commands

These commands take no arguments.

### `toggleTaskList`

Wrap selected paragraphs in a checklist, lift selected items from a checklist, or convert a compatible list to a checklist.

```rust
editor.run(gpdoc_plugin_task_list::Toggle)?;
```

### `splitTaskItem`

Split at a text caret or replace the selected text within a task paragraph with an item boundary. The new item starts unchecked. An item containing only an empty paragraph lifts one level.

```rust
editor.run(gpdoc_plugin_task_list::Split)?;
```

### `liftTaskItem`

With the selection inside one task, move it up one level.

```rust
editor.run(gpdoc_plugin_task_list::Lift)?;
```

### `sinkTaskItem`

With nesting enabled, move the selected task under its previous sibling.

```rust
editor.run(gpdoc_plugin_task_list::Sink)?;
```

## Keyboard shortcuts

| Shortcut                  | Command          |
| ------------------------- | ---------------- |
| **Mod + Shift + 9**       | `toggleTaskList` |
| **Enter**                 | `splitTaskItem`  |
| **Shift + Tab**           | `liftTaskItem`   |
| **Tab** (nesting enabled) | `sinkTaskItem`   |

At the start of a paragraph, type `[ ]` followed by a space for an unchecked task or `[x]` followed by a space for a checked one. The input rule consumes the marker.

## Source code

[task-list/src/lib.rs](/source/plugins/task-list/src/lib.rs.txt)