dkodaj/rte/9.0.1

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. Otherwise the RTE won't do anything.

Note: When the RTE is active (the textarea object is part of your view), it keeps taking away the focus from every other element. Use freeze to stop this.

Init and update

type Rte

The rich text editor. Under the hood, it is a record type that tracks cursor position, selection, formatting etc.

init : String -> Rte
update : Msg -> Rte -> ( Rte, Cmd Msg )

View

textarea : Rte -> List (Attribute Msg) -> Html Msg

Show the RTE.

showContent : Content -> List (Attribute Msg) -> Html Msg

Display, in an uneditable div, the content generated by the RTE.

{ fontSizeUnit : Maybe String
, indentUnit : Maybe ( Float, String )
, highlighter : Maybe (Content -> Content)
}
-> Content
-> List (Attribute Msg)
-> Html Msg

Same with some parameters.

showEncodedContent : List (Attribute Msg) -> String -> Html Msg

Same but using a json generated with encodeContentString.

{ fontSizeUnit : Maybe String
, indentUnit : Maybe ( Float, String )
, highlighter : Maybe (Content -> Content)
}
-> List (Attribute Msg)
-> String
-> Html Msg

Same with some parameters.

View Styled

Same as the previous but with Html.Styled.

textareaStyled : Rte -> List (Attribute Msg) -> Html Msg
showContentStyled : Content -> List (Attribute Msg) -> Html Msg
{ fontSizeUnit : Maybe String
, indentUnit : Maybe ( Float, String )
, highlighter : Maybe (Content -> Content)
}
-> Content
-> List (Attribute Msg)
-> Html Msg
showEncodedContentStyled : List (Attribute Msg) -> String -> Html Msg
{ fontSizeUnit : Maybe String
, indentUnit : Maybe ( Float, String )
, highlighter : Maybe (Content -> Content)
}
-> List (Attribute Msg)
-> String
-> Html Msg

Configure

Control whether pasting something that begins with with http(s):// becomes a clickable link. Default is False.

Prevent typing more than a certain number of characters. There is no default limit, but the RTE becomes increasingly slow for longer texts (>10-12K characters).

setFontSizeUnit : String -> Rte -> Rte

Defaults to px.

setHighlighter : Maybe (Content -> Content) -> Rte -> Rte

See MiniRte.Types on highlighters.

setIndentUnit : ( Float, String ) -> Rte -> Rte

Defaults to (50,"px"). Matters when you use increaseIndent or decreaseIndent.

setSelectionStyle : List ( String, String ) -> Rte -> Rte

Controls how selected text looks. Defaults to [("background","hsl(217,71%,53%)"), ("color","hsl(0,0%,100%)")].

When you paste an image link x, it becomes an img element with src=x. Something qualifies as an image link iff it starts with data:image/ or it ends with .jpeg, .jpg, .gif, .png, .apng, .svg, .bmp, or .ico.

Control

You can build a rich text editor toolbar from these (see the example in the repo).

addContent : Content -> Rte -> ( Rte, Cmd Msg )

Add content at current cursor position.

addCustomHtml : EmbeddedHtmlRecord -> Rte -> ( Rte, Cmd Msg )

Embed a custom html node within text.

addImageWithSrc : String -> Rte -> ( Rte, Cmd Msg )

An img node with the specified src.

addText : String -> Rte -> ( Rte, Cmd Msg )

Insert text at current cursor position.

copy : Rte -> ( Rte, Cmd Msg )

Copy current selection. Results in a ToBrowserClipboard Msg which must be captured in update. See the example in the repo.

cut : Rte -> ( Rte, Cmd Msg )
decreaseIndent : Rte -> ( Rte, Cmd Msg )

Hides cursor and stops listening to mouse and keyboard events.

increaseIndent : Rte -> ( Rte, Cmd Msg )
paste : String -> Rte -> ( Rte, Cmd Msg )
replaceContent : Content -> Rte -> Rte

Clears the editor and loads new content.

Same with plain text.

setFontSize : Float -> Rte -> ( Rte, Cmd Msg )

Controls the font size within the selected text / at the current cursor position.

setSelection : ( Int, Int ) -> Rte -> ( Rte, Cmd Msg )
toggleBold : Rte -> ( Rte, Cmd Msg )
toggleClass : String -> Rte -> ( Rte, Cmd Msg )
toggleFontFamily : List String -> Rte -> ( Rte, Cmd Msg )
toggleHeading : Rte -> ( Rte, Cmd Msg )

Turns the current paragraph into an h1 element.

toggleItalic : Rte -> ( Rte, Cmd Msg )
toggleNodeType : String -> Rte -> ( Rte, Cmd Msg )
toggleUnderline : Rte -> ( Rte, Cmd Msg )
undo : Rte -> ( Rte, Cmd Msg )

Show the cursor and activate mouse/keyboard listeners.

Info

content : Rte -> Content

The edited content of the RTE.

fontFamily : Rte -> List String

The font-family (set with toggleFontFamily) at the cursor position.

fontSize : Rte -> Maybe Float

The font-size (set with setFontSize) at the cursor position.

freezed : Rte -> Bool
textContent : Rte -> String

The plain text content of the RTE.

Conversions

textToContent : String -> Content
contentToText : Content -> String
decodeContentString : String -> Result String Content

Convert content json string into edited content.

decodeContentGZip : Bytes -> Result String Content

Convert gzipped content json into edited content.

Encode the edited content in a json string.

Encode the edited content in a gzipped json string.

Hardwired key combos

  • Ctrl-0: en dash (–)
  • Ctrl-1: em dash (—)
  • Ctrl-Z: undo