Otzel.Content protocol (otzel v0.3.2)
View SourceProtocol for content types that can be stored in delta operations.
The Content protocol defines how different content types behave within the OT system. By default, strings are supported, but you can implement this protocol for custom embedded content types like images, videos, or nested documents.
Built-in Implementations
BitString- Standard Elixir stringsOtzel.Content.Iomemo- Efficient IO-list based strings (default)Otzel.Content.Ot- Nested OT documents
Implementing Custom Content
For simple atomic embeds (size 1, cannot be split), use the atomic: true option:
defmodule MyApp.ImageEmbed do
use Otzel.Content, atomic: true
defstruct [:url, :width, :height]
def invert(_, _), do: raise "not implemented"
def compose(_, _), do: raise "not implemented"
def transform(_, _, _), do: raise "not implemented"
endThe atomic: true option automatically implements:
size/1- Returns 1take/2- Returns the whole contentmerge_into/2- Returns nil (cannot merge)as_binary/1- Returns nilembed?/1- Returns true (is embedded content)diff/2- Returns empty list for equal content, or delete+insert for different content
Checking Content Type
Use embed?/1 to check if content is an embedded type (returns true) or
string-like (returns false):
Otzel.Content.embed?(string_content) # => false
Otzel.Content.embed?(embed_content) # => true
Summary
Functions
Converts content to a binary string, or nil if not applicable
Composes two content values
Concatenates a list of content values into a single content value.
Computes the diff between two content values
Returns true if the content is an embedded type (not string-like)
Extracts the content from an operation.
Inverts content changes for undo operations
Attempts to merge adjacent content values
Remaps Insert operations to use a specific string module.
Returns the size (character count) of the content
Splits content at the given position
Transforms content for concurrent edits
Types
@type t() :: term()
All the types that implement this protocol.
Functions
Converts content to a binary string, or nil if not applicable
Composes two content values
Concatenates a list of content values into a single content value.
Delegates to the appropriate content module's concatenate/1 function
based on the type of the first element.
Computes the diff between two content values
Returns true if the content is an embedded type (not string-like)
Extracts the content from an operation.
For Insert operations, returns the content being inserted. For Retain operations, returns the target (count or embedded content).
Inverts content changes for undo operations
Attempts to merge adjacent content values
Remaps Insert operations to use a specific string module.
Converts the content of each Insert operation to the given module's
representation (e.g., String or Otzel.Content.Iomemo).
@spec size(t()) :: non_neg_integer()
Returns the size (character count) of the content
@spec take(t(), non_neg_integer()) :: {t(), t() | nil}
Splits content at the given position
Transforms content for concurrent edits