Text Editing
nano-ui provides two multi-line text editors depending on how much text you are editing:
textAreaedits a plainVar Text. Use it for short fields like notes, comments, or commit messages.textAreaDocumentedits aVar TextDocument, which shares unchanged lines across edits. Use it for files, code buffers, and large documents.
Short text with textArea
notes = do
draft <- useVar ""
textArea draft
Each keystroke produces a new flat Text, which is fast and convenient for a few kilobytes of text.
Large documents with textAreaDocument
editorView :: Var WidgetId -> Var TextDocument -> NanoUI ()
editorView editorId docVar = do
resp <- textAreaDocumentWith' (grow . minW 320 . minH 200) docVar
when (respChanged resp) markDirty
writeVar editorId (respId resp)
TextDocument copies only the lines touched by an edit and shares everything else. Call documentText to flatten the document back into a single Text when saving or exporting, rather than on every edit:
save :: FilePath -> TextDocument -> NanoUI ()
save path doc = liftIO (BS.writeFile path (TE.encodeUtf8 (documentText doc)))
For status bars, documentLineCount runs without flattening the text.
Unified undo history
Keyboard typing, mouse selection, right-click context menu actions, and programmatic commands all run through TextCommand, so everything shares a single undo/redo stack:
-- | Editing, navigation, selection, history, and clipboard operations on a
-- text field. Mutating commands record an undo step and replace any active
-- selection.
data TextCommand
= -- | Replace the selection with text (typing, a snippet).
InsertText !Text
| -- | Delete the selection, or from the cursor to where the motion lands:
-- @Delete CharLeft@ is Backspace, @Delete WordRight@ Ctrl+Delete.
Delete !TextMotion
| -- | Move the cursor, extending the selection when the flag is set.
-- Without it, 'CharLeft' and 'CharRight' over a selection go to its start
-- and its end.
Move !TextMotion !Bool
| SelectAll
| -- | Select from the first position (the anchor) to the second (the
-- cursor), clamped into the document.
Select !Cursor !Cursor
| -- | Replace the text between two positions, leaving the cursor after it.
Replace !Cursor !Cursor !Text
| -- | Replace the whole document as one undoable edit.
ReplaceAll !Text
| Undo
| Redo
| Cut
| Copy
| Paste
deriving (Eq, Show)
Dispatch a command to any text field or editor by its WidgetId:
editMenu :: WidgetId -> Text -> NanoUI ()
editMenu editor today = do
whenM (menuItem "Undo") (runTextCommand editor Undo)
whenM (menuItem "Insert date") (runTextCommand editor (InsertText today))
Query undo/redo availability to enable or disable Edit menu entries:
undoState :: Var WidgetId -> NanoUI (Bool, Bool)
undoState editorId = do
editor <- readVar editorId
canUndo <- textCanUndo editor
canRedo <- textCanRedo editor
pure (canUndo, canRedo)
See Tutorial 2: Build a Notepad for a full Edit menu wired up this way.
Tip
Overwriting a
Var TextorVar TextDocumentdirectly from outside the widget clears its undo stack. If you want a programmatic edit to be undoable, dispatch aTextCommand(InsertText,ReplaceAll, and so on) instead.
When using primed variants (textArea', textAreaDocument'), respChanged is True after actual text edits and False after cursor movement or selection changes, making it safe to use for dirty-state tracking. Keep respId to target the editor with runTextCommand or requestFocus.
Rich text and hyperlinks
For read-only paragraphs mixing bold, italic, monospaced code runs, highlights, tooltips, and clickable links, use richText (or selectableRichText when users should be able to drag-select and copy):
releaseNote :: NanoUI ()
releaseNote = do
clicked <-
selectableRichText
[ "Build "
, strong "v0.2"
, " with "
, inlineTooltip "Uses -j1 to keep memory modest" (inlineCode "cabal build -j1 all")
, " or read the "
, hyperlink "https://goolord.github.io/nano-ui/book/" "documentation"
, "."
]
for_ clicked openLink
Plain string literals produce Inline pieces via OverloadedStrings. Combine them with strong, emphasis, inlineCode, inlineWith, restyle, inlineBackground, inlineTooltip, and hyperlink target label. Both richText and selectableRichText wrap to their container width and return Maybe Text holding the target of any link clicked on that frame. For full CommonMark documents, use nano-ui-markdown.