Yex.Text (y_ex v0.8.0)
View SourceA shareable type that is optimized for shared editing on text. This module provides functionality for collaborative text editing with support for rich text formatting.
Features
- Insert and delete text at any position
- Apply formatting attributes (bold, italic, etc.)
- Support for Quill Delta format for change tracking
- Collaborative editing with conflict resolution
Summary
Functions
Transforms this type to a Quill Delta
Converts the text object to its preliminary representation. This is useful when you need to serialize or transfer the text content and formatting.
Deletes text content starting at the specified index. Supports negative indices for deletion from the end. Returns :ok on success, :error on failure.
Applies formatting attributes to a range of text. Returns :ok on success, :error on failure.
Inserts text content at the specified index. Returns :ok on success, :error on failure.
Inserts text content with formatting attributes at the specified index. Returns :ok on success, :error on failure.
Returns the length of the text content in characters.
Transforms this type to a Quill Delta
Returns the text content as a string.
Types
Functions
Transforms this type to a Quill Delta
Examples Syncs two clients by exchanging the complete document structure
iex> doc = Yex.Doc.new()
iex> text = Yex.Doc.get_text(doc, "text")
iex> delta = [%{ "retain" => 1}, %{ "delete" => 3}]
iex> Yex.Text.insert(text,0, "12345")
iex> Yex.Text.apply_delta(text,delta)
iex> Yex.Text.to_delta(text)
[%{insert: "15"}]
@spec as_prelim(t()) :: Yex.TextPrelim.t()
Converts the text object to its preliminary representation. This is useful when you need to serialize or transfer the text content and formatting.
Parameters
text
- The text object to convert
Deletes text content starting at the specified index. Supports negative indices for deletion from the end. Returns :ok on success, :error on failure.
Parameters
text
- The text object to modifyindex
- The starting position to delete from (0-based, negative indices count from end)length
- The number of characters to delete
Applies formatting attributes to a range of text. Returns :ok on success, :error on failure.
Parameters
text
- The text object to modifyindex
- The starting position to format from (0-based)length
- The number of characters to formatattr
- A map of formatting attributes to apply (e.g. %{"bold" => true})
@spec insert(t(), integer(), Yex.input_type()) :: :ok | :error
Inserts text content at the specified index. Returns :ok on success, :error on failure.
Parameters
text
- The text object to modifyindex
- The position to insert at (0-based)content
- The text content to insert
@spec insert(t(), integer(), Yex.input_type(), map()) :: :ok | :error
Inserts text content with formatting attributes at the specified index. Returns :ok on success, :error on failure.
Parameters
text
- The text object to modifyindex
- The position to insert at (0-based)content
- The text content to insertattr
- A map of formatting attributes to apply (e.g. %{"bold" => true})
Returns the length of the text content in characters.
Parameters
text
- The text object to get the length of
Transforms this type to a Quill Delta
Examples creates a few changes, then gets them back as a batch of change maps
iex> doc = Yex.Doc.new()
iex> text = Yex.Doc.get_text(doc, "text")
iex> Yex.Text.insert(text, 0, "12345")
iex> Yex.Text.insert(text, 0, "0", %{"bold" => true})
iex> Yex.Text.to_delta(text)
[%{insert: "0", attributes: %{"bold" => true}}, %{insert: "12345"}]
Returns the text content as a string.
Parameters
text
- The text object to convert to string