NoRedInk/noredink-ui/18.5.1

NOTE: the latest version is 35.9.3

Nri.Ui.Highlightable.V1

A Highlightable represents a span of text, typically a word, and its state.

Highlightable is the unit by which text-wrapping happens. Depending on how the Highlighter is initialized, it's very possible for a Highlightable to consist of just a single whitespace.

A consecutive array of Highlightables sharing the same groupIndex form a fragment, which gets highlighted together as a group in response to user action such as clicking and dragging. Fragment as a concept only exists in how functions and variables are named, and is not expressed as a concrete type. We probably should refactor to make it an actual type.

Types

type alias Highlightable marker =
{ text : String
, uiState : UIState
, customAttributes : List Attribute
, marked : Maybe (MarkerModel marker)
, groupIndex : Int
, type_ : Type
}

A Highlightable comes in two flavors:

  • Highlightable | Interactive piece of text.

  • Static | Non-interactive piece of text. Gets highlighted grudgingly if the UI is going to get unintuitive otherwise. i.e. does not get highlighted when hovered etc.

    Data for a Highlightable:

  • text: String to display.

  • uiState: How the user is currently interacting with the Highlightable.

  • customAttributes: User-supplied CSS attributes that do not change once a Highlightable is initialized.

  • marked: Current highlight.

  • groupIndex: Index that identifies the fragment this Highlightable belongs to.

type Type
= Interactive
| Static
type UIState
= Hovered
| Hinted
| None

UIState connects user-behavior to the highlightable.

State transitions:

  • None → (mouse over) → Hovered
  • None → (click & drag over highlightables) → Hinted
  • Hovered → (mouse out) → None
  • Hovered → (mouse down) → Hinted
  • Hinted → (mouse out) → None
  • Hinted → (mouse up while on a different Highlightable) → None
  • Hinted → (mouse up while on me) → Hovered
= Class String
| Data String String

Custom attributes that do not change after initialization.

  • Class: add a class to the highlight
  • Data: add a data-X to the highlight (you don't need to prepend data- to this name.)

Initializers

-> Maybe (MarkerModel marker)
-> Int
-> ( List Attribute, String )
-> Highlightable marker
Maybe (MarkerModel marker)
-> Int
-> List ( List Attribute, String )
-> List (Highlightable marker)

Split a multi-word string into multiple single-word highlights. This is to deal with the highlighter not doing line breaks within a highlight group, which can look funny if a highlight contains longer text.

initFragments : Maybe (MarkerModel marker) -> String -> List (Highlightable marker)

Similar to initFragment but each word is treated as a fragment, instead of treating the whole string as a fragment.

Note that we're transforming all whitespace to spaces, so newlines are not preserved as me move to and from fragments

Transformations

splitHighlightableOnWords : Highlightable marker -> List (Highlightable marker)

Split a highlightable into one highlightable per word.

splitWords : String -> List String

Create list of words intersperse with spaces.

UIState related

blur : Highlightable marker -> Highlightable marker
clearHint : Highlightable marker -> Highlightable marker
hint : Highlightable marker -> Highlightable marker
hover : Highlightable marker -> Highlightable marker

Marker related

set :
Maybe (MarkerModel marker)
-> Highlightable marker
-> Highlightable marker
toggle : MarkerModel marker -> Highlightable marker -> Highlightable marker

Attribute related

A Sorter for Attributes