dkodaj/rte/6.0.2

NOTE: the latest version is 10.0.1

MiniRte

Don't forget to hook subscriptions and update into your app's own subscriptions and update function. Without that, the editor won't do anything.

Note: While the editor is active, it keeps taking away the focus from every other element (source). This may interfere with Html.select and such. Use the FreezeEditor Msg to take away the focus (example) and Active True to give it back.

Init and update

init : Parameters msg -> ( Rte msg, Cmd msg )
type alias Rte msg =
{ emojiBox : Bool
, inputBox : Maybe InputBox
, styling :
{ active : List (Attribute msg)
, inactive : List (Attribute msg)
}
, tagger : Msg -> msg
, textarea : Editor
}

Normally, you don't have to do anything with these variables; use update and Msg to modify the editor's state.

If emojiBox is True, then the emojiBox is visible.

If inputBox is Nothing, then the inputBox is not visible.

styling.active styles the div containing the editor in active mode (when editing), styling.passive does the same in display mode or inactive mode. Use init to set it.

tagger connects RTE Msg's with your app's own Msg type. Use init to set it.

textarea is a complicated object that you shouldn't mess with.

type alias Parameters msg =
{ id : String
, content : Content
, fontSizeUnit : Maybe String
, highlighter : Maybe (Content -> Content)
, indentUnit : Maybe ( Float, String )
, pasteImageLinksAsImages : Bool
, pasteLinksAsLinks : Bool
, selectionStyle : List ( String, String )
, styling :
{ active : List (Attribute msg)
, inactive : List (Attribute msg)
}
, tagger : Msg -> msg
}

id must be unique.

content is the initial content of the textarea. Use Array.empty to initialize with empty content. To initialize with saved content, use decodeContentString or decodeContentGZip (example). To convert plain text into Content, use textToContent.

fontSizeUnit defaults to "px".

highlighter runs before each view update, re-stylings the text as you see fit. See the Content type and the example.

indentUnit defaults to (50,"px").

If pasteImageLinksAsImages is True, then pasting an image link or an image (data url) into the RTE will insert the image into the text.

if pasteLinksAsLinks is True, then pasting a link (something that starts with "http://" or "https://") will create a clickable link.

selectionStyle controls the appearance of selected text. It defaults to [("background", "hsl(217,71%,53%)"), ("color", "white")].

styling.active styles the textarea div in active mode (when editing). Use update (Active True/False) to switch between modes.

tagger turns the package's own Msg type into your app's msg.

subscriptions : Rte msg -> Sub msg

Handles keydown/keyup and mouse events and it keeps the RTE in focus (source).

update : Msg -> Rte msg -> ( Rte msg, Cmd msg )

Use this to handle the RTE's own Msg updates (example).

textToContent : String -> Content

Convert a string into Content.

contentToText : Content -> String

Ignores EmbeddedHtml elements (replaces them with "").

View

textarea : Rte msg -> Html msg

Display the edited text plus the cursor. Example

display : (Msg -> msg) -> DisplayParams msg -> Html msg

Display formatted content, without an editor (e.g. as a blog post).

type alias DisplayParams msg =
{ content : String
, fontSizeUnit : Maybe String
, highlighter : Maybe (Content -> Content)
, id : String
, indentUnit : Maybe ( Float, String )
, styling : List (Attribute msg)
}

content can be plain text (in which case it is turned into Content by textToContent), or it can be a json string created with encodeContentString.

fontSizeUnit defaults to "px".

Use highlighter to to re-style the text the way you want it (e.g. code highlighting). See the Content type and the example.

id must be unique.

indentUnit defaults to (50,"px").

The content of styling is attached to the div that contains the text.

Toolbar

emojiBox : Rte msg -> EmojiBoxParams msg -> Html msg

Make it appear/disappear with update ToggleEmojiBox. Each x in params.emojis turns into a clickable div that triggers update AddText events. Example

type alias EmojiBoxParams msg =
{ styling :
{ active : List (Attribute msg)
, inactive : List (Attribute msg)
}
, emojis : List String
}
fontSelector : Rte msg -> FontSelectorParams msg -> Html msg

A Html.select element that triggers update Font events. Example

type alias FontSelectorParams msg =
{ styling : List (Attribute msg)
, fonts : List (List String)
}
fontSizeSelector : Rte msg -> FontSizeSelectorParams msg -> Html msg

A Html.select element that triggers update FontSize events. Example

type alias FontSizeSelectorParams msg =
{ styling : List (Attribute msg)
, sizes : List Float
}
inputBox : Rte msg -> InputBoxParams msg -> Html msg

Input box for adding hyperlinks and image links. Make it appear/disappear with update ToggleImageBox or update ToggleLinkBox. It contains an OK button that triggers update ImageAdd or update LinkAdd. Example

type alias InputBoxParams msg =
{ styling :
{ active : List (Attribute msg)
, inactive : List (Attribute msg)
}
}
onOffSwitch : Rte msg -> SwitchParams -> Html msg

A switch that turns editing on/off. The params.width field controls its width in px. Example

type alias SwitchParams =
{ activeColor : String
, inactiveColor : String
, width : Float
}

Serialize/deserialize content

encodeContentString : Rte msg -> String

Serialize the edited text as string.

encodeContentGZip : Rte msg -> Bytes

Serialize the edited text as a gzip file (example).

decodeContentString : String -> Result String Content

Convert serialized content string back into content.

decodeContentGZip : Bytes -> Result String Content

Convert gzipped serialized content back into content (example).

Info

isActive : Rte msg -> Bool

Checks if the editor is active.

textContent : Rte msg -> String

The plain text content of the textarea.