rupertlssmith / rte-toolkit-patch / RichText.Config.Command

This module contains types relating to defining transforms, commands and command maps

Command

transform : Transform -> Command

Creates a command from a transform.

transform removeSelection
--> A command from the removeSelection transform

internal : InternalAction -> Command

Creates a command from an internal action.

internal Undo
--> A command that will execute undo


type InternalAction
    = Undo
    | Redo

InternalAction is a fixed set of actions that require internal editor state to execute. For now this only includes undo and redo.


type alias NamedCommand =
( String
, Command 
)

Type alias for a command with a name. Giving a command a name is useful for debugging purposes when debugging a chain of commands. The command name is also stored in the history, which can help define more advanced behavior.

( "removeSelection", removeSelection )


type alias NamedCommandList =
List NamedCommand

Type alias for a list of named commands.

[ ( "backspaceWord", backspaceWord ), ( "backspace", backspace ) ]


type Command
    = TransformCommand Transform
    | InternalCommand InternalAction

Command is either a transform or internal action that can be applied to an editor via RichText.Editor.applyCommand

Command map


type CommandMap

A command map provides bindings between keydown/beforeinput events and a list of named commands.

emptyCommandMap
    |> set
        [ inputEvent "insertLineBreak", key [ shift, enter ], key [ shift, return ] ]
        [ ( "insertLineBreak", transform insertLineBreak ) ]

Note that on a successful command, preventDefault is called on the associated event. This means that if a key command succeeds, the input command should not be triggered.


type CommandBinding

A command binding can either be a key press or an input event. Note that each browser has varying degrees of Input Level 2 support, so relying just on input events is usually not enough to support all browsers. On the flip side, virtual keyboards, specifically Android virtual keyboards, can fire synthetic keyboard events that don't contain the key value, so for some actions on those platforms you may need rely on input events.

key : List String -> CommandBinding

Creates a key binding given a list of keys.

inputEvent : String -> CommandBinding

Creates an input event key given the input event type.

emptyCommandMap : CommandMap

An empty command map

set : List CommandBinding -> NamedCommandList -> CommandMap -> CommandMap

Returns a CommandMap with each command binding set to the given named command list.

emptyCommandMap
    |> set
        [ inputEvent "insertLineBreak", key [ shift, enter ], key [ shift, return ] ]
        [ ( "insertLineBreak", transform insertLineBreak ) ]

withDefaultInputEventCommand : (InputEvent -> NamedCommandList) -> CommandMap -> CommandMap

Returns a commandMap with the defaultKeyCommand set to the provided value. The defaultInputEventCommand is used if there are no key bindings set for a keyboard event.

emptyCommandMap
    |> withDefaultInputEventCommand func

withDefaultKeyCommand : (KeyboardEvent -> NamedCommandList) -> CommandMap -> CommandMap

Returns a commandMap with the defaultKeyCommand set to the provided value.

emptyCommandMap
    |> withDefaultKeyCommand func

defaultKeyCommand : CommandMap -> KeyboardEvent -> NamedCommandList

defaultKeyCommand is used if there are no key bindings set for a keyboard event.

defaultInputEventCommand : CommandMap -> InputEvent -> NamedCommandList

defaultInputEventCommand is used if there are no input event bindings set for an input event type.

combine : CommandMap -> CommandMap -> CommandMap

Combines two command maps, with the second CommandMap commands being appended to the first CommandMap.

combine listCommandBindings defaultCommandBindings
--> Creates a command map that prioritizes list command bindings, then default command bindings

Transform


type alias Transform =
RichText.Model.State.State -> Result String RichText.Model.State.State

A Transform is a function that receives a state and returns a result either with a new modified state, or a String explaining why the transform couldn't be done. With the exception of internal commands, all commands are defined by one or more transforms.

removeSelection : Transform
removeSelection state =
    Ok (state |> withSelection Nothing)

Event


type alias InputEvent =
{ data : Maybe String
, isComposing : Basics.Bool
, inputType : String 
}

The attributes parsed from an input event.


type alias KeyboardEvent =
{ keyCode : Basics.Int
, key : String
, altKey : Basics.Bool
, metaKey : Basics.Bool
, ctrlKey : Basics.Bool
, shiftKey : Basics.Bool
, isComposing : Basics.Bool 
}

The attributes parsed from a keyboard event.

namedCommandListFromInputEvent : InputEvent -> CommandMap -> NamedCommandList

Derives a named command list from an input event.

namedCommandListFromKeyboardEvent : String -> KeyboardEvent -> CommandMap -> NamedCommandList

Derives a named command list from a keyboard event. The first argument is the value of shortKey, a platform dependent shortcut for either Meta or Control.