mweiss/elm-rte-toolkit/1.0.0

NOTE: the latest version is 1.0.4

RichText.State

This module contains functions to validate and reduce editor state. These methods are used every time a command is applied.

State functions

Reduces the state with the following rules:

  • Neighboring text nodes with the same marks are merged into one text node
  • Empty text nodes (regardless of marks) that are not part of the current collapsed selection are removed if there is another neighboring text node
before : State
before =
    state
        (block
            (Element.element doc [])
            (blockChildren <|
                Array.fromList
                    [ block
                        (Element.element paragraph [])
                        (inlineChildren <| Array.fromList [ plainText "te", plainText "xt" ])
                    ]
            )
        )
        (Just <| caret [ 0, 1 ] 2)


after : State
after =
    state
        (block
            (Element.element doc [])
            (blockChildren <|
                Array.fromList
                    [ block
                        (Element.element paragraph [])
                        (inlineChildren <| Array.fromList [ plainText "text" ])
                    ]
            )
        )
        (Just <| caret [ 0, 0 ] 4)

reduce before == after
--> True
validate : Spec -> State -> Result String State

Validates the state against the spec and returns the valid state if everything is okay, otherwise returns a comma separated string of error messages.

example : State
example =
    state
        (block
            (Element.element doc [])
            (blockChildren <|
                Array.fromList
                    [ block
                        (Element.element paragraph [])
                        (inlineChildren <| Array.fromList [ plainText "text" ])
                    ]
            )
        )
        (Just <| caret [ 0, 0 ] 2)


(Ok example) == (validate markdown example)
--> True

Helpers

Just the selection translation function that gets called in reduce. Note that this is really only useful if you're creating transforms that merge or remove inline nodes and you can't find a way to easily figure out the new selection state.