Otzel.Attrs (otzel v0.3.2)

View Source

Utilities for working with operation attributes.

Attributes are maps of formatting properties applied to content, such as %{"bold" => true, "color" => "#ff0000"}. They are used with Insert and Retain operations to style text.

Attribute Values

  • Any truthy value applies the attribute
  • nil removes the attribute
  • Missing keys leave the attribute unchanged

Functions

  • compose/3 - Combines attributes from sequential operations
  • transform/3 - Adjusts attributes for concurrent operations
  • invert/2 - Creates attributes that undo a change
  • diff/2 - Computes the difference between two attribute sets

Examples

# Compose: second operation wins
Otzel.Attrs.compose(%{"bold" => true}, %{"italic" => true})
#=> %{"bold" => true, "italic" => true}

# Diff: compute changes needed
Otzel.Attrs.diff(%{"bold" => true}, %{"italic" => true})
#=> %{"bold" => nil, "italic" => true}

Summary

Types

t()

Attributes are a map of string keys to values, or nil.

Functions

Composes two attribute maps, with the second map taking precedence.

Computes the difference between two attribute maps.

Computes attributes that would undo a change.

Transforms attributes for concurrent operations.

Types

t()

@type t() :: %{optional(String.t()) => any()} | nil

Attributes are a map of string keys to values, or nil.

A nil value for a key indicates the attribute should be removed. A nil attrs map means no attributes are set.

Functions

cleanup(map, arg2)

compose(a, b, keep_nils \\ false)

@spec compose(t(), t(), keep_nils :: boolean()) :: t()

Composes two attribute maps, with the second map taking precedence.

Used when applying sequential operations - the later operation's attributes override earlier ones.

Options

  • keep_nils - When true, preserves nil values in the result (default: false)

Examples

iex> Otzel.Attrs.compose(%{"bold" => true}, %{"italic" => true})
%{"bold" => true, "italic" => true}

iex> Otzel.Attrs.compose(%{"bold" => true}, %{"bold" => nil})
nil

diff(same, same)

@spec diff(t(), t()) :: t()

Computes the difference between two attribute maps.

Returns attributes that, when applied to left, would produce right. Keys present in left but not right are set to nil (removal).

Examples

iex> Otzel.Attrs.diff(%{"bold" => true}, %{"bold" => true})
nil

iex> Otzel.Attrs.diff(%{"bold" => true}, %{"italic" => true})
%{"bold" => nil, "italic" => true}

iex> Otzel.Attrs.diff(nil, %{"bold" => true})
%{"bold" => true}

invert(attr, base)

@spec invert(t(), t()) :: t()

Computes attributes that would undo a change.

Given the attributes that were applied and the original base attributes, returns attributes that would restore the original state.

Examples

iex> Otzel.Attrs.invert(%{"bold" => true}, nil)
%{"bold" => nil}

iex> Otzel.Attrs.invert(%{"bold" => nil}, %{"bold" => true})
%{"bold" => true}

transform(from, into, priority)

@spec transform(t(), t(), Otzel.priority()) :: t()

Transforms attributes for concurrent operations.

When two operations modify the same content concurrently, this determines which attributes take effect based on priority.

Priority

  • :right - The into attributes win (default)
  • :left - Only into attributes not in from are kept

Examples

iex> Otzel.Attrs.transform(%{"bold" => true}, %{"italic" => true}, :right)
%{"italic" => true}

iex> Otzel.Attrs.transform(%{"bold" => true}, %{"bold" => false}, :left)
nil