mweiss/elm-rte-toolkit/1.0.4

RichText.Model.Node

This module contains types related to the nodes in an editor.

An editor consists of two types of nodes, block and inline. Block nodes are used to represent hierarchical structures like blockquotes, tables and nested lists. Inline nodes are used to represent flat structures, like text, inline images, and hard breaks.

Block

type Block

A Block represents a block element in your document. A block can either have other block nodes as children, have all inline leaf nodes as children (e.g a text block), or be a leaf node.

block : Element -> Children -> Block

Creates a block node. The arguments are as follows

  • element is the element this block node represents
  • childNodes are the children related to this block node.
block
    (Element.element paragraph [])
    (inlineChildren <| Array.fromList [ plainText "some text" ])
element : Block -> Element

the element from a block node

the childNodes from a block node.

withElement : Element -> Block -> Block

a block node with the given element set

a block node with the given children set

Children

= BlockChildren BlockChildren
| InlineChildren InlineChildren
| Leaf

Children represents what children an editor block node can have. A block node may have other block nodes as children, inline leaf nodes as children, or it may be a leaf itself.

BlockChildren are child nodes that are all blocks.

Creates children from a block array

blockChildren (Array.fromList [ paragraphNode ])

Returns a block array from block children

InlineChildren are child nodes that are all inline. Internally, it's represented as both a flat structure (which can be accessed via inlineArray), and a hierarchical structure (which can be accessed via inlineTree).

Creates children derived from an inline array.

an array of inline nodes (flat structure)

a tree of mark nodes with inline leaf indices

a lookup array that maps the index of the inline array to its path in the inline tree

marksToMarkNodeList : List (List Mark) -> Array InlineTree

Transforms a list of list of marks to an array of inline tree nodes

Inline

type Inline
= InlineElement InlineElement
| Text Text

An inline leaf node represents an inline element in your document. It can either be an inline leaf node, like an image or line break, or a text node.

= MarkNode
{ mark : Mark
, children : Array InlineTree
}
| LeafNode Int

An inline tree is the nested structure of an inline array. Because marks when rendered can span multiple inline nodes, inline content is still technically hierarchical. When rendering or parsing, it can be useful to see this information as a tree instead of an array.

  • MarkNode node that represents a mark and its inline children
  • LeafNode the index of the inline in the inline array
inlineElement : Element -> List Mark -> Inline

Creates an Inline from an Element and Mark

marks : Inline -> List Mark

Derives the marks from an inline node

plainText : String -> Inline

A Inline that represents plain text

markedText : String -> List Mark -> Inline

Creates an inline that represents some text with marks

Path

type alias Path =
List Int

A node path is a list of indexes that represent the path from a node to a child. It's the main type used to identify where a node is in the editor.

parent : Path -> Path

Returns the parent path of the given path

parent [0, 1, 2]
--> [0, 1]
increment : Path -> Path

Increments the last index in a node path if one exists.

increment [0, 1]
--> [0, 2]
decrement : Path -> Path

Decrements the last index in a node path if one exists.

decrement [0, 0]
--> [0, 0]
toString : Path -> String

String representation of a path.

toString [0, 0]
--> "0:0"
commonAncestor : Path -> Path -> Path

Returns the common ancestor of the two paths.

commonAncestor [0, 1, 2] [0, 1, 4]
--> [0, 1]