Documentation
¶
Overview ¶
Package richedit is Giode's rich text editor: paragraphs, headings, lists, checklists, quotes, code, tables, images and rules, with inline formatting, mentions and other atoms, Markdown shortcuts, slash commands, undo, and Markdown, JSON and HTML in and out. Its document is a richdoc.Doc; its edits stream as richdoc operations for syncing.
Index ¶
- type RichEditor
- func (e *RichEditor) Apply(ops []richdoc.Op) error
- func (e *RichEditor) Blur()
- func (e *RichEditor) CanRedo() bool
- func (e *RichEditor) CanUndo() bool
- func (e *RichEditor) Clear() *RichEditor
- func (e *RichEditor) ClearFormatting()
- func (e *RichEditor) Doc() *richdoc.Doc
- func (e *RichEditor) Empty() bool
- func (e *RichEditor) Focus()
- func (e *RichEditor) Focused() bool
- func (e *RichEditor) HTML() string
- func (e *RichEditor) HasMark(k richdoc.MarkKind) bool
- func (e *RichEditor) Indent(out bool)
- func (e *RichEditor) InsertAtom(a richdoc.Atom)
- func (e *RichEditor) InsertHTML(html string)
- func (e *RichEditor) InsertImage(src, alt string)
- func (e *RichEditor) InsertMarkdown(md string)
- func (e *RichEditor) InsertRule()
- func (e *RichEditor) InsertTable(rows, cols int)
- func (e *RichEditor) InsertText(s string)
- func (e *RichEditor) Kind() (richdoc.Kind, int)
- func (e *RichEditor) Layout(gtx layout.Context) layout.Dimensions
- func (e *RichEditor) LoadImage(fn func(src string) image.Image) *RichEditor
- func (e *RichEditor) Markdown() string
- func (e *RichEditor) MarkdownShortcuts(on bool) *RichEditor
- func (e *RichEditor) MinHeight(dp int) *RichEditor
- func (e *RichEditor) OnChange(fn func()) *RichEditor
- func (e *RichEditor) OnLinkClick(fn func(url string)) *RichEditor
- func (e *RichEditor) OnLinkEdit(fn func(current string, set func(url string))) *RichEditor
- func (e *RichEditor) OnOps(fn func(tx *richdoc.Tx)) *RichEditor
- func (e *RichEditor) OnSubmit(fn func()) *RichEditor
- func (e *RichEditor) PasteMarkdown(on bool) *RichEditor
- func (e *RichEditor) Placeholder(s string) *RichEditor
- func (e *RichEditor) ReadOnly(on bool) *RichEditor
- func (e *RichEditor) Redo()
- func (e *RichEditor) Select(s richdoc.Selection)
- func (e *RichEditor) SelectAll()
- func (e *RichEditor) Selection() richdoc.Selection
- func (e *RichEditor) SetColor(color string, highlight bool)
- func (e *RichEditor) SetDoc(d *richdoc.Doc) *RichEditor
- func (e *RichEditor) SetKind(k richdoc.Kind, level int)
- func (e *RichEditor) SetLink(url string)
- func (e *RichEditor) SetMarkdown(md string) *RichEditor
- func (e *RichEditor) SetText(s string) *RichEditor
- func (e *RichEditor) SlashCommands(on bool) *RichEditor
- func (e *RichEditor) State() *richdoc.State
- func (e *RichEditor) SubmitOn(m SubmitMode) *RichEditor
- func (e *RichEditor) Suggest(trigger rune, fn Suggester) *RichEditor
- func (e *RichEditor) Text() string
- func (e *RichEditor) ToggleMark(k richdoc.MarkKind)
- func (e *RichEditor) Toolbar() elements.Element
- func (e *RichEditor) Undo()
- type SubmitMode
- type Suggester
- type Suggestion
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RichEditor ¶
type RichEditor struct {
elements.Contextual
// contains filtered or unexported fields
}
RichEditor is a stateful rich text editor. Create it once with New and lay it out every frame.
func New ¶
func New(placeholder string, st ...styles.Styles) *RichEditor
New returns an empty editor with a placeholder. The styles set its text color, font size and box.
func (*RichEditor) Apply ¶
func (e *RichEditor) Apply(ops []richdoc.Op) error
Apply applies a collaborator's operations, keeping the caret and selection in place around them. It clears the undo history, whose steps no longer fit.
func (*RichEditor) CanRedo ¶
func (e *RichEditor) CanRedo() bool
func (*RichEditor) CanUndo ¶
func (e *RichEditor) CanUndo() bool
CanUndo and CanRedo report whether there's a step to undo or redo.
func (*RichEditor) Clear ¶
func (e *RichEditor) Clear() *RichEditor
Clear empties the document, as sending a chat message does; it can be undone.
func (*RichEditor) ClearFormatting ¶
func (e *RichEditor) ClearFormatting()
ClearFormatting removes the selection's marks.
func (*RichEditor) Doc ¶
func (e *RichEditor) Doc() *richdoc.Doc
Doc is the document being edited. Don't change it directly.
func (*RichEditor) Empty ¶
func (e *RichEditor) Empty() bool
Empty reports whether the document has no text, tables or images.
func (*RichEditor) Focus ¶
func (e *RichEditor) Focus()
Focus gives the editor keyboard focus at its next layout.
func (*RichEditor) Focused ¶
func (e *RichEditor) Focused() bool
Focused reports whether it has keyboard focus.
func (*RichEditor) HasMark ¶
func (e *RichEditor) HasMark(k richdoc.MarkKind) bool
HasMark reports whether the selection (or what's typed next) has it.
func (*RichEditor) Indent ¶
func (e *RichEditor) Indent(out bool)
Indent nests the selected list items deeper, or shallower.
func (*RichEditor) InsertAtom ¶
func (e *RichEditor) InsertAtom(a richdoc.Atom)
InsertAtom inserts a mention, reference or other atom at the caret.
func (*RichEditor) InsertHTML ¶
func (e *RichEditor) InsertHTML(html string)
InsertHTML inserts HTML's content, sanitized, at the caret.
func (*RichEditor) InsertImage ¶
func (e *RichEditor) InsertImage(src, alt string)
InsertImage inserts an image block.
func (*RichEditor) InsertMarkdown ¶
func (e *RichEditor) InsertMarkdown(md string)
InsertMarkdown inserts Markdown's content at the caret.
func (*RichEditor) InsertRule ¶
func (e *RichEditor) InsertRule()
InsertRule inserts a horizontal rule.
func (*RichEditor) InsertTable ¶
func (e *RichEditor) InsertTable(rows, cols int)
InsertTable inserts a table of rows by cols empty cells, the first row its header.
func (*RichEditor) InsertText ¶
func (e *RichEditor) InsertText(s string)
InsertText inserts text at the caret, replacing the selection.
func (*RichEditor) Kind ¶
func (e *RichEditor) Kind() (richdoc.Kind, int)
Kind returns the kind and level of the block at the caret.
func (*RichEditor) Layout ¶
func (e *RichEditor) Layout(gtx layout.Context) layout.Dimensions
Layout lays the editor out in the width it's given, as tall as its content between MinHeight and the height it's given, scrolling past.
func (*RichEditor) LoadImage ¶
func (e *RichEditor) LoadImage(fn func(src string) image.Image) *RichEditor
LoadImage sets how image blocks get their pictures, by URL; without it they show as placeholders.
func (*RichEditor) Markdown ¶
func (e *RichEditor) Markdown() string
Markdown returns the document as Markdown.
func (*RichEditor) MarkdownShortcuts ¶
func (e *RichEditor) MarkdownShortcuts(on bool) *RichEditor
MarkdownShortcuts turns the Markdown typed at the start of paragraphs and around text into formatting ("- ", "# ", "**bold**"); on by default.
func (*RichEditor) MinHeight ¶
func (e *RichEditor) MinHeight(dp int) *RichEditor
MinHeight sets the editor's least height; it grows with its content up to the height it's given, then scrolls.
func (*RichEditor) OnChange ¶
func (e *RichEditor) OnChange(fn func()) *RichEditor
OnChange sets the handler called after every edit.
func (*RichEditor) OnLinkClick ¶
func (e *RichEditor) OnLinkClick(fn func(url string)) *RichEditor
OnLinkClick sets what opening a link does: Ctrl or Cmd and click, or any click when read-only. Unset, nothing opens.
func (*RichEditor) OnLinkEdit ¶
func (e *RichEditor) OnLinkEdit(fn func(current string, set func(url string))) *RichEditor
OnLinkEdit sets what Ctrl+K (Cmd+K) does: ask for a URL, starting from the selection's link, and call set with it ("" unlinks).
func (*RichEditor) OnOps ¶
func (e *RichEditor) OnOps(fn func(tx *richdoc.Tx)) *RichEditor
OnOps sets the handler called with every transaction applied - typed, pasted, undone - for syncing the document with collaborators. Transactions marshal to JSON.
func (*RichEditor) OnSubmit ¶
func (e *RichEditor) OnSubmit(fn func()) *RichEditor
OnSubmit sets the handler of Enter or Ctrl+Enter (SubmitOn).
func (*RichEditor) PasteMarkdown ¶
func (e *RichEditor) PasteMarkdown(on bool) *RichEditor
PasteMarkdown reads pasted text that looks like Markdown as Markdown; on by default.
func (*RichEditor) Placeholder ¶
func (e *RichEditor) Placeholder(s string) *RichEditor
Placeholder sets the text shown while the document is empty.
func (*RichEditor) ReadOnly ¶
func (e *RichEditor) ReadOnly(on bool) *RichEditor
ReadOnly shows the document without editing it; links open on a click.
func (*RichEditor) Redo ¶
func (e *RichEditor) Redo()
func (*RichEditor) Select ¶
func (e *RichEditor) Select(s richdoc.Selection)
Select sets the selection.
func (*RichEditor) SelectAll ¶
func (e *RichEditor) SelectAll()
SelectAll selects the whole document.
func (*RichEditor) Selection ¶
func (e *RichEditor) Selection() richdoc.Selection
Selection returns the selection.
func (*RichEditor) SetColor ¶
func (e *RichEditor) SetColor(color string, highlight bool)
SetColor colors the selection's text, or highlights it; "" clears.
func (*RichEditor) SetDoc ¶
func (e *RichEditor) SetDoc(d *richdoc.Doc) *RichEditor
SetDoc replaces the document and clears the undo history.
func (*RichEditor) SetKind ¶
func (e *RichEditor) SetKind(k richdoc.Kind, level int)
SetKind makes the selected blocks headings (with level), list items, quotes or code; or paragraphs when they already are.
func (*RichEditor) SetLink ¶
func (e *RichEditor) SetLink(url string)
SetLink links the selection; "" unlinks it.
func (*RichEditor) SetMarkdown ¶
func (e *RichEditor) SetMarkdown(md string) *RichEditor
SetMarkdown replaces the document with Markdown's.
func (*RichEditor) SetText ¶
func (e *RichEditor) SetText(s string) *RichEditor
SetText replaces the document with plain text, a paragraph a line.
func (*RichEditor) SlashCommands ¶
func (e *RichEditor) SlashCommands(on bool) *RichEditor
SlashCommands offers block types, a table, a divider and the like when "/" is typed at the start of a word; on by default.
func (*RichEditor) State ¶
func (e *RichEditor) State() *richdoc.State
State is the editing state: the document, selection and commands. Edits made through it directly aren't recorded for undo or streamed; use the editor's methods.
func (*RichEditor) SubmitOn ¶
func (e *RichEditor) SubmitOn(m SubmitMode) *RichEditor
SubmitOn sets what Enter does; OnSubmit what submitting does.
func (*RichEditor) Suggest ¶
func (e *RichEditor) Suggest(trigger rune, fn Suggester) *RichEditor
Suggest shows the suggestions fn returns while text follows trigger: '@' for mentions, '#' for tickets or deals, ':' for emoji.
func (*RichEditor) Text ¶
func (e *RichEditor) Text() string
Text returns the document's plain text.
func (*RichEditor) ToggleMark ¶
func (e *RichEditor) ToggleMark(k richdoc.MarkKind)
ToggleMark turns bold, italic, underline, strike or code on or off.
func (*RichEditor) Toolbar ¶
func (e *RichEditor) Toolbar() elements.Element
Toolbar returns a row of formatting buttons for the editor: marks, headings, lists, quote, code, link (with OnLinkEdit), undo and redo. Pressing them leaves the keyboard focus in the editor. Apps wanting another set build their own from the editor's commands.
type SubmitMode ¶
type SubmitMode int
SubmitMode is what Enter does.
const ( // SubmitNone: Enter starts a new paragraph. SubmitNone SubmitMode = iota // SubmitEnter: Enter submits and Shift+Enter starts a new paragraph, // as chat composers do. SubmitEnter // SubmitShortcut: Ctrl+Enter (Cmd+Enter on Apple) submits. SubmitShortcut )
type Suggester ¶
type Suggester func(query string) []Suggestion
Suggester returns the suggestions for what's been typed after its trigger. It runs on the UI goroutine every time the query changes: filter a local list, or return what a search started earlier found.
type Suggestion ¶
type Suggestion struct {
// Label is shown; Detail beside it, dimmed.
Label, Detail string
// Picking it inserts Atom, or Text, in place of the trigger and
// query; or runs Command after removing them.
Atom *richdoc.Atom
Text string
Command func(e *RichEditor)
}
Suggestion is an entry of a suggestion list, shown while text follows a trigger character ("@ada", "#45", "/head", ":smi").