Quillon.Commands (Quillon v0.1.0)

Copy Markdown View Source

High-level API for common formatting operations.

Convenience wrappers around Quillon.Transform functions.

Example

para = Quillon.paragraph("Hello world")

# Toggle bold on "Hello"
para = Quillon.Commands.toggle_bold(para, 0, 5)

# Add a link
para = Quillon.Commands.set_link(para, 6, 11, "https://example.com")

# Clear all formatting
para = Quillon.Commands.clear_formatting(para, 0, 11)

Summary

Types

A block node (paragraph or heading)

Functions

Remove all formatting from the given range.

Check if the selection has a specific mark type.

Set font color on the given range.

Set a highlight on the given range.

Set a link on the given range.

Set a mention on the given range.

Toggle bold formatting on the given range.

Toggle code formatting on the given range.

Toggle italic formatting on the given range.

Toggle strikethrough formatting on the given range.

Toggle subscript formatting on the given range.

Toggle superscript formatting on the given range.

Toggle underline formatting on the given range.

Remove font color from the given range.

Remove highlight from the given range.

Remove link from the given range.

Remove mention from the given range.

Types

block()

@type block() :: {:paragraph | :heading, map(), list()}

A block node (paragraph or heading)

Functions

clear_formatting(block, start_offset, end_offset)

@spec clear_formatting(block(), non_neg_integer(), non_neg_integer()) :: block()

Remove all formatting from the given range.

Removes all simple marks (bold, italic, underline, strike, code) and attributed marks (link) from the text in the range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: [:bold, :italic]}, []}]}
iex> Quillon.Commands.clear_formatting(para, 0, 5)
{:paragraph, %{}, [{:text, %{text: "Hello", marks: []}, []}]}

selection_has_mark?(block, start_offset, end_offset, mark_type)

@spec selection_has_mark?(block(), non_neg_integer(), non_neg_integer(), atom()) ::
  boolean()

Check if the selection has a specific mark type.

Returns true only if ALL text in the range has the mark.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: [:bold]}, []}]}
iex> Quillon.Commands.selection_has_mark?(para, 0, 5, :bold)
true

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: []}, []}]}
iex> Quillon.Commands.selection_has_mark?(para, 0, 5, :bold)
false

set_font_color(block, start_offset, end_offset, color)

@spec set_font_color(block(), non_neg_integer(), non_neg_integer(), String.t()) ::
  block()

Set font color on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "red text", marks: []}, []}]}
iex> Quillon.Commands.set_font_color(para, 0, 8, "#ff0000")
{:paragraph, %{}, [{:text, %{text: "red text", marks: [{:font_color, %{color: "#ff0000"}}]}, []}]}

set_highlight(block, start_offset, end_offset, color)

@spec set_highlight(block(), non_neg_integer(), non_neg_integer(), String.t()) ::
  block()

Set a highlight on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "important", marks: []}, []}]}
iex> Quillon.Commands.set_highlight(para, 0, 9, "yellow")
{:paragraph, %{}, [{:text, %{text: "important", marks: [{:highlight, %{color: "yellow"}}]}, []}]}

set_link(block, start_offset, end_offset, href)

@spec set_link(block(), non_neg_integer(), non_neg_integer(), String.t() | map()) ::
  block()

Set a link on the given range.

Accepts either a URL string or a map of link attributes.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "click here", marks: []}, []}]}
iex> Quillon.Commands.set_link(para, 0, 10, "https://example.com")
{:paragraph, %{}, [{:text, %{text: "click here", marks: [{:link, %{href: "https://example.com"}}]}, []}]}

iex> para = {:paragraph, %{}, [{:text, %{text: "click here", marks: []}, []}]}
iex> Quillon.Commands.set_link(para, 0, 10, %{href: "/page", title: "Link"})
{:paragraph, %{}, [{:text, %{text: "click here", marks: [{:link, %{href: "/page", title: "Link"}}]}, []}]}

set_mention(block, start_offset, end_offset, attrs)

@spec set_mention(block(), non_neg_integer(), non_neg_integer(), map()) :: block()

Set a mention on the given range.

Accepts a map with :id, :type, and :label attributes.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "@alice", marks: []}, []}]}
iex> Quillon.Commands.set_mention(para, 0, 6, %{id: "user_123", type: "user", label: "@alice"})
{:paragraph, %{}, [{:text, %{text: "@alice", marks: [{:mention, %{id: "user_123", type: "user", label: "@alice"}}]}, []}]}

toggle_bold(block, start_offset, end_offset)

@spec toggle_bold(block(), non_neg_integer(), non_neg_integer()) :: block()

Toggle bold formatting on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: []}, []}]}
iex> Quillon.Commands.toggle_bold(para, 0, 5)
{:paragraph, %{}, [{:text, %{text: "Hello", marks: [:bold]}, []}]}

toggle_code(block, start_offset, end_offset)

@spec toggle_code(block(), non_neg_integer(), non_neg_integer()) :: block()

Toggle code formatting on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: []}, []}]}
iex> Quillon.Commands.toggle_code(para, 0, 5)
{:paragraph, %{}, [{:text, %{text: "Hello", marks: [:code]}, []}]}

toggle_italic(block, start_offset, end_offset)

@spec toggle_italic(block(), non_neg_integer(), non_neg_integer()) :: block()

Toggle italic formatting on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: []}, []}]}
iex> Quillon.Commands.toggle_italic(para, 0, 5)
{:paragraph, %{}, [{:text, %{text: "Hello", marks: [:italic]}, []}]}

toggle_strike(block, start_offset, end_offset)

@spec toggle_strike(block(), non_neg_integer(), non_neg_integer()) :: block()

Toggle strikethrough formatting on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: []}, []}]}
iex> Quillon.Commands.toggle_strike(para, 0, 5)
{:paragraph, %{}, [{:text, %{text: "Hello", marks: [:strike]}, []}]}

toggle_subscript(block, start_offset, end_offset)

@spec toggle_subscript(block(), non_neg_integer(), non_neg_integer()) :: block()

Toggle subscript formatting on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "H2O", marks: []}, []}]}
iex> Quillon.Commands.toggle_subscript(para, 1, 2)
{:paragraph, %{}, [{:text, %{text: "H", marks: []}, []}, {:text, %{text: "2", marks: [:subscript]}, []}, {:text, %{text: "O", marks: []}, []}]}

toggle_superscript(block, start_offset, end_offset)

@spec toggle_superscript(block(), non_neg_integer(), non_neg_integer()) :: block()

Toggle superscript formatting on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "x2", marks: []}, []}]}
iex> Quillon.Commands.toggle_superscript(para, 1, 2)
{:paragraph, %{}, [{:text, %{text: "x", marks: []}, []}, {:text, %{text: "2", marks: [:superscript]}, []}]}

toggle_underline(block, start_offset, end_offset)

@spec toggle_underline(block(), non_neg_integer(), non_neg_integer()) :: block()

Toggle underline formatting on the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "Hello", marks: []}, []}]}
iex> Quillon.Commands.toggle_underline(para, 0, 5)
{:paragraph, %{}, [{:text, %{text: "Hello", marks: [:underline]}, []}]}

unset_font_color(block, start_offset, end_offset)

@spec unset_font_color(block(), non_neg_integer(), non_neg_integer()) :: block()

Remove font color from the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "text", marks: [{:font_color, %{color: "#ff0000"}}]}, []}]}
iex> Quillon.Commands.unset_font_color(para, 0, 4)
{:paragraph, %{}, [{:text, %{text: "text", marks: []}, []}]}

unset_highlight(block, start_offset, end_offset)

@spec unset_highlight(block(), non_neg_integer(), non_neg_integer()) :: block()

Remove highlight from the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "text", marks: [{:highlight, %{color: "yellow"}}]}, []}]}
iex> Quillon.Commands.unset_highlight(para, 0, 4)
{:paragraph, %{}, [{:text, %{text: "text", marks: []}, []}]}

unset_link(block, start_offset, end_offset)

@spec unset_link(block(), non_neg_integer(), non_neg_integer()) :: block()

Remove link from the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "click", marks: [{:link, %{href: "/"}}]}, []}]}
iex> Quillon.Commands.unset_link(para, 0, 5)
{:paragraph, %{}, [{:text, %{text: "click", marks: []}, []}]}

unset_mention(block, start_offset, end_offset)

@spec unset_mention(block(), non_neg_integer(), non_neg_integer()) :: block()

Remove mention from the given range.

Examples

iex> para = {:paragraph, %{}, [{:text, %{text: "@alice", marks: [{:mention, %{id: "1", type: "user", label: "@alice"}}]}, []}]}
iex> Quillon.Commands.unset_mention(para, 0, 6)
{:paragraph, %{}, [{:text, %{text: "@alice", marks: []}, []}]}