# Operation

An operation is the lowest-level description of a specific change to a part of Slate's value. They are designed to be collaborative-editing friendly.

All of the [`Commands`](https://docs.slatejs.org/v0.47/slate-core/commands) methods result in operations being created and applied to a [`Value`](https://docs.slatejs.org/v0.47/slate-core/value) They're accessible via the `editor.operations` property.

There are a handful of Slate operation types. The goal is to have the fewest possible types, while still maintaining the necessary semantics for collaborative editing to work.

## Properties

See each operation separately below to see what they consist of.

Note that all operations have a `data` property which can be used to attach arbitrary data to the operation, just like the [`Node`](https://docs.slatejs.org/v0.47/slate-core/node) models ([`Block`](https://docs.slatejs.org/v0.47/slate-core/block), [`Inline`](https://docs.slatejs.org/v0.47/slate-core/inline), etc).

## Text Operations

### `insert_text`

```javascript
{
  type: 'insert_text',
  path: List,
  offset: Number,
  text: String,
  marks: List,
  data: Map,
}
```

Inserts a `text` string at `offset` into a text node at `path`, with optional `marks` to be applied to the inserted characters.

### `remove_text`

```javascript
{
  type: 'remove_text',
  path: List,
  offset: Number,
  text: String,
  data: Map,
}
```

Removes a string of `text` at `offset` into a text node at `path`.

## Mark Operations

### `add_mark`

```javascript
{
  type: 'add_mark',
  path: List,
  offset: Number,
  length: Number,
  mark: Mark,
  data: Map,
}
```

Adds a `mark` to the text node at `path` starting at an `offset` and spanning `length` characters.

### `remove_mark`

```javascript
{
  type: 'remove_mark',
  path: List,
  offset: Number,
  length: Number,
  mark: Mark,
  data: Map,
}
```

Removes a `mark` from a text node at `path` starting at an `offset` and spanning `length` characters.

### `set_mark`

```javascript
{
  type: 'set_mark',
  path: List,
  offset: Number,
  length: Number,
  properties: Object,
  newProperties: Object,
  data: Map,
}
```

Set new `newProperties` on any marks that match an existing `properties` mark in a text node at `path`, starting at an `offset` and spanning `length` characters.

## Node Operations

### `insert_node`

```javascript
{
  type: 'insert_node',
  path: List,
  node: Node,
  data: Map,
}
```

Insert a new `node` at `path`.

### `merge_node`

```javascript
{
  type: 'merge_node',
  path: List,
  position: Number,
  properties: Object,
  data: Map,
}
```

Merge the node at `path` with its previous sibling. The `position` refers to either the index in the child nodes of the previous sibling in the case of [`Block`](https://docs.slatejs.org/v0.47/slate-core/block) or [`Inline`](https://docs.slatejs.org/v0.47/slate-core/inline) nodes, and the index in the characters of the previous sibling in the case of [`Text`](https://docs.slatejs.org/v0.47/slate-core/text) nodes. The `properties` object contains properties of the merged node in the event that the change is undone.

### `move_node`

```javascript
{
  type: 'move_node',
  path: List,
  newPath: Array,
  data: Map,
}
```

Move the node at `path` to a `newPath`.

### `remove_node`

```javascript
{
  type: 'remove_node',
  path: List,
  node: Node,
  data: Map,
}
```

Remove the node at `path`.

### `set_node`

```javascript
{
  type: 'set_node',
  path: List,
  properties: Object,
  newProperties: Object,
  data: Map,
}
```

Set new `properties` on the node at `path`.

### `split_node`

```javascript
{
  type: 'split_node',
  path: List,
  position: Number,
  target: Number,
  properties: Object,
  data: Map,
}
```

Split the node at `path` at `position`. The `position` refers to either the index in the child nodes in the case of [`Block`](https://docs.slatejs.org/v0.47/slate-core/block) or [`Inline`](https://docs.slatejs.org/v0.47/slate-core/inline) nodes, and the index in the characters in the case of [`Text`](https://docs.slatejs.org/v0.47/slate-core/text) nodes. In the case of nested splits, `target` refers to the target path of the child split operation. The `properties` object contains properties that should be assigned to the new node created after the split operation is complete.

## Value Operations

### `set_selection`

```javascript
{
  type: 'set_selection',
  properties: Object,
  newProperties: Object,
  data: Map,
}
```

Set new `properties` on the selection.

### `set_value`

```javascript
{
  type: 'set_value',
  properties: Object,
  newProperties: Object,
  data: Map,
}
```

Set new `properties` on a value. Properties can contain `data` and `decorations`.

## Helpers

### `apply`

`apply(value: Value, operation: Object) => Value`

Applies an `operation` to a `value` object.

### `invert`

`invert(operation: Object) => Object`

Create an inverse operation that will undo the changes made by the original.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.slatejs.org/v0.47/slate-core/operation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
