# Text alignment

> Align paragraphs, headings, or other configured blocks.

Text alignment adds the `textAlign` attribute to chosen block types. The default alignment values are `left`, `center`, `right`, and `justify`.

## Try it

[Try the text-align editor example](https://gpdoc.dev/docs/plugins/formatting/text-align/#text-align-example)

## Install

```sh
cargo add gpdoc-plugin-text-align
```

## Example

Enable alignment on paragraphs in the [Quickstart editor configuration](/docs/quickstart/#configure-the-editor). `text_align(["paragraph"])` uses this formatter by default.

```rust
use gpdoc::{EditorConfig, PluginViewExt};
use gpdoc_plugin_text_align::{text_align, text_align_format};
use gpdoc_plugin_paragraph::paragraph;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(text_align(["paragraph"]).with_view(|| text_align_format(["paragraph"])))
    .build()
    .expect("configure editor");
```

Place the caret in a paragraph and press **Command + Shift + E** on macOS or **Ctrl + Shift + E** on Windows and Linux. The paragraph moves to the center.

## Settings

`text_align(types)` and `text_align_with_options(types, options: TextAlignOptions)` both create a text alignment plugin.

### `types`

Choose which blocks can store alignment. Supply an iterator of names (`impl IntoIterator<Item = impl Into<String>>`) for block types registered with your config. To align headings, register the [Heading plugin](/docs/plugins/structure/heading/) and include `"heading"` in the array.

```rust
let alignment = gpdoc_plugin_text_align::text_align(["paragraph", "heading"]);
```

### `alignments`

Restrict the menu to supported values with `TextAlignOptions::alignments` (`Vec<String>`). Defaults to `left`, `center`, `right`, and `justify`.

```rust
use gpdoc_plugin_text_align::{text_align_with_options, TextAlignOptions};


let alignment = text_align_with_options(["paragraph"], TextAlignOptions {
    alignments: vec!["left".into(), "center".into()],
    ..Default::default()
});
```

### `default_alignment`

Set the initial alignment and the value restored by reset. The default is `None` (`Option<String>`), stored as `null` on the block.

```rust
use gpdoc_plugin_text_align::{text_align_with_options, TextAlignOptions};


let alignment = text_align_with_options(["paragraph"], TextAlignOptions {
    default_alignment: Some("left".into()),
    ..Default::default()
});
```

## Commands

`editor.run` returns `Result<CommandOutcome, Error>`. Set and toggle return `NotApplicable` for a missing or unsupported `textAlign` string. All three return `NotApplicable` if the selection contains no configured block.

### `setTextAlign`

Apply the requested alignment to each eligible block in the selection.

```rust
editor.run(gpdoc_plugin_text_align::Set("center".into()))?;
```

### `toggleTextAlign`

Restore the default if the requested alignment covers the selection. Otherwise apply it.

```rust
editor.run(gpdoc_plugin_text_align::Toggle("center".into()))?;
```

### `unsetTextAlign`

Restore the configured default on eligible blocks.

```rust
editor.run(gpdoc_plugin_text_align::Unset)?;
```

## Keyboard shortcuts

Hold **Command + Shift** on macOS or **Ctrl + Shift** on Windows and Linux, then press **L** for left, **E** for center, **R** for right, or **J** for justified alignment.