Otzel.Op protocol (otzel v0.3.2)

View Source

Protocol for delta operations.

An operation represents a single action in a delta. There are three operation types:

Operations implement this protocol to provide common behaviors like merging adjacent operations, calculating size, and splitting operations.

Creating Operations

Use the helper functions in Otzel:

Otzel.insert("Hello")
Otzel.retain(5, %{"bold" => true})
Otzel.delete(3)

Or create structs directly:

%Otzel.Op.Insert{content: "Hello", attrs: nil}
%Otzel.Op.Retain{target: 5, attrs: %{"bold" => true}}
%Otzel.Op.Delete{count: 3}

Summary

Functions

Attempts to merge two adjacent operations into one.

Returns the size of an operation.

Splits an operation at the given position.

Types

Functions

compose(a, b)

@spec compose(t(), t()) :: {t() | nil, t(), t()}

from_json(json, opts \\ [])

json_encoders()

merge_into(op1, op2)

@spec merge_into(t(), t()) :: t() | nil

Attempts to merge two adjacent operations into one.

Returns the merged operation if the operations can be combined, or nil if they cannot be merged.

Operations can be merged when they are of the same type with compatible attributes.

size(operation)

@spec size(t()) :: non_neg_integer()

Returns the size of an operation.

For inserts and retains, this is the character count of the content. For deletes, this is the number of characters to delete.

take(operation, count)

@spec take(t(), non_neg_integer()) :: {t(), t() | nil}

Splits an operation at the given position.

Returns a tuple of {taken, rest} where taken is the first count units and rest is the remainder (or nil if nothing remains).

transform(a, b, priority)

@spec transform(a :: t(), b :: t(), Otzel.priority()) :: {t(), t(), t()}

transform_index(offset, index, op, priority)

@spec transform_index(non_neg_integer(), non_neg_integer(), t(), Otzel.priority()) ::
  non_neg_integer()