View Source Yex.StickyIndex (y_ex v0.7.1)

A sticky index is based on the Yjs model and is not affected by document changes. E.g. If you place a sticky index before a certain character, it will always point to this character. If you place a sticky index at the end of a type, it will always point to the end of the type. A numeric position is often unsuited for user selections, because it does not change when content is inserted before or after.

Insert(0, 'x')('a.bc') = 'xa.bc' Where . is the relative position.

Examples

iex> alias Yex.{StickyIndex, Doc, Text} iex> doc = Doc.new() iex> txt = Doc.get_text(doc, "text") iex> Doc.transaction(doc, fn -> ...> Text.insert(txt, 0, "abc") ...> # => 'abc' ...> # create position tracker (marked as . in the comments) ...> pos = StickyIndex.new(txt, 2, :after) ...> # => 'ab.c' ...> ...> # modify text ...> Text.insert(txt, 1, "def") ...> # => 'adefb.c' ...> Text.delete(txt, 4, 1) ...> # => 'adef.c' ...> ...> # get current offset index within the containing collection ...> {:ok, a} = StickyIndex.get_offset(pos) ...> # => 4 ...> assert a.index == 4 ...> end)

Summary

Types

shared_type()

@type shared_type() ::
  %Yex.Array{doc: term(), reference: term()}
  | %Yex.Text{doc: term(), reference: term()}
  | %Yex.XmlElement{doc: term(), reference: term()}
  | %Yex.XmlText{doc: term(), reference: term()}
  | %Yex.XmlFragment{doc: term(), reference: term()}

t()

@type t() :: %Yex.StickyIndex{
  assoc: :before | :after,
  doc: Yex.Doc.t(),
  reference: reference()
}

Functions

get_offset(sticky_index)

@spec get_offset(t()) :: {:ok, %{index: integer(), assoc: :before | :after}} | :error

new(shared_type, index, assoc)

@spec new(shared_type(), integer(), :before | :after) :: t()