Otzel.Attrs (otzel v0.3.2)
View SourceUtilities 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
nilremoves the attribute- Missing keys leave the attribute unchanged
Functions
compose/3- Combines attributes from sequential operationstransform/3- Adjusts attributes for concurrent operationsinvert/2- Creates attributes that undo a changediff/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
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
Functions
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
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}
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}
@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- Theintoattributes win (default):left- Onlyintoattributes not infromare kept
Examples
iex> Otzel.Attrs.transform(%{"bold" => true}, %{"italic" => true}, :right)
%{"italic" => true}
iex> Otzel.Attrs.transform(%{"bold" => true}, %{"bold" => false}, :left)
nil