richeditor

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package richeditor gives TSX nsgo/base's RichEditor and its sessions, which link packages other apps should not: an app whose TSX uses them imports this package for its effect.

An app makes a session with createRichEditor from extensions (the built-in StarterKit, TableKit, Mention and CharacterCount, and its own from defineExtension) and plugins whose hooks are its functions, drives it with commands as data (command, chain, toggleMark, …), and shows it with <RichEditor editor={editor} />.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command is RichCommand: an edit as data, resolved against the editor it runs in, so a named command is that editor's and a type name its schema's.

type Editor

type Editor struct {
	// contains filtered or unexported fields
}

Editor is RichEditorSession: the session a RichEditor shows and the app drives, its document and selection read on demand.

func (*Editor) Blur

func (e *Editor) Blur()

func (*Editor) Can

func (e *Editor) Can(c *Command) (bool, error)

Can says whether a command would apply.

func (*Editor) Characters

func (e *Editor) Characters(nodes bool) int

Characters counts the document's characters, every position with nodes; Words its words.

func (*Editor) Close

func (e *Editor) Close() error

Close stops it hearing its session, which dispose() calls.

func (*Editor) CoordsAtPos

func (e *Editor) CoordsAtPos(pos int) (RichRect, bool, error)

CoordsAtPos is the caret's box at pos in the editor's content box, null with no view showing it.

func (*Editor) DocJSON

func (e *Editor) DocJSON() map[string]any

func (*Editor) DragBlock

func (e *Editor) DragBlock(pos int, x, y float64) (bool, error)

DragBlock starts a handle's drag at a point of the page, carrying the block at pos, DragTo moves its drop cursor and EndDrag drops it, as one undo step.

func (*Editor) DragTo

func (e *Editor) DragTo(x, y float64)

DragTo moves a handle's drag to a point of the page.

func (*Editor) Editable

func (e *Editor) Editable() bool

Editable says the user can change the document.

func (*Editor) EndDrag

func (e *Editor) EndDrag() bool

EndDrag drops what a handle carries and says whether it moved.

func (*Editor) Focus

func (e *Editor) Focus()

Focus and Blur are requests to the views showing it.

func (*Editor) GetAttributes

func (e *Editor) GetAttributes(name string) map[string]any

GetAttributes are the attributes of the mark or node of name at the selection, empty for none.

func (*Editor) HTML

func (e *Editor) HTML() string

func (*Editor) IsActive

func (e *Editor) IsActive(name string, attrs map[string]any) bool

IsActive says whether the node or mark of name, with attrs, is at the selection.

func (*Editor) IsFocused

func (e *Editor) IsFocused() bool

IsFocused says a view showing it has the keyboard.

func (*Editor) Markdown

func (e *Editor) Markdown() string

Markdown, HTML, Text and DocJSON are the document in those forms.

func (*Editor) On

func (e *Editor) On(event string, handler func(RichEditorEvent)) func()

On listens to an event (revision, update, selectionUpdate, transaction, focus, blur, destroy) and returns what stops it.

func (*Editor) PageCoordsAtPos

func (e *Editor) PageCoordsAtPos(pos int) (RichRect, bool, error)

PageCoordsAtPos and PosAtPageCoords answer in the page's points, for what the app places outside the editor.

func (*Editor) PosAtCoords

func (e *Editor) PosAtCoords(x, y float64) (int, bool)

PosAtCoords is the position nearest a point of the content box.

func (*Editor) PosAtPageCoords

func (e *Editor) PosAtPageCoords(x, y float64) (int, bool)

PosAtPageCoords is the position nearest a point of the page.

func (*Editor) RedoDepth

func (e *Editor) RedoDepth() int

func (*Editor) Revision

func (e *Editor) Revision() int

Revision counts the transactions applied.

func (*Editor) Run

func (e *Editor) Run(c *Command) (bool, error)

Run runs a command and says whether it applied; Can says whether it would.

func (*Editor) Selection

func (e *Editor) Selection() RichSelection

Selection is the selection now.

func (*Editor) SelectionRect

func (e *Editor) SelectionRect() (RichRect, bool)

SelectionRect is the box over the selection's lines.

func (*Editor) SetContent

func (e *Editor) SetContent(content any) error

SetContent replaces the document, kept out of the history: a string is Markdown, an object Tiptap's JSON.

func (*Editor) SetEditable

func (e *Editor) SetEditable(on bool)

SetEditable sets whether the user can change the document.

func (*Editor) SetHTML

func (e *Editor) SetHTML(html string) error

SetHTML replaces the document with HTML's, kept out of the history.

func (*Editor) Size

func (e *Editor) Size() int

Size is the document's content size, the last position.

func (*Editor) Suggestion

func (e *Editor) Suggestion(key string) (RichSuggestion, bool)

Suggestion is the suggestion of key, null when it has none.

func (*Editor) Text

func (e *Editor) Text() string

func (*Editor) UndoDepth

func (e *Editor) UndoDepth() int

UndoDepth and RedoDepth count the steps undo and redo can take.

func (*Editor) Words

func (e *Editor) Words() int

type Extension

type Extension struct {
	// contains filtered or unexported fields
}

Extension is RichExtension: Tiptap's extension, a built-in one or the app's, made into the kernel's as createRichEditor builds the editor.

func CharacterCount

func CharacterCount(options map[string]any) *Extension

CharacterCount limits the characters to limit, counting every position with mode "nodes".

func Mention

func Mention(options map[string]any) *Extension

Mention is Tiptap's mention and its suggestion: char, key, allowSpaces, class and the captureKeys its list takes.

func StarterKit

func StarterKit(options map[string]any) *Extension

StarterKit is the kit of Tiptap's usual types: levels for headings, false for history, trailingNode, taskList, typography or gapcursor to leave one out, history's depth and newGroupDelay, and link's autolink, openOnClick and protocols.

func TableKit

func TableKit(options map[string]any) *Extension

TableKit is tables: cellMinWidth, and false for resizable or lastColumnResizable.

func (*Extension) Configure

func (x *Extension) Configure(options map[string]any) *Extension

Configure is a copy with options merged deeply over its own, as Tiptap's mergeDeep.

func (*Extension) Name

func (x *Extension) Name() string

Name is its name, unique in an editor.

func (*Extension) Options

func (x *Extension) Options() map[string]any

Options are its options, defaults under what was configured.

type RichClickEvent

type RichClickEvent struct {
	Pos, Clicks int
	Mod         bool
}

RichClickEvent is a press at a position, Clicks in a row, Mod held or not.

type RichDecoration

type RichDecoration struct {
	From, To int
	Class    string
	Node     bool
	Widget   func() core.Node
	Side     int
	Key      string
}

RichDecoration styles a range without changing the document: Class on its text, or on the block at From with Node; with Widget, a view in the text at From, before a cursor there when Side is below zero, kept while Key stays.

type RichEditorEvent

type RichEditorEvent struct {
	Revision   int
	DocChanged bool
	Origin     string
}

RichEditorEvent is what an editor's events carry.

type RichEditorOptions

type RichEditorOptions struct {
	Extensions []*Extension
	Markdown   string
	HTML       string
	JSON       map[string]any
	Plugins    []RichPluginSpec
	// Editable false keeps the user from changing the document.
	Editable *bool
	// OpenLink opens a link pressed with Mod, or in a read-only editor.
	OpenLink func(href string)
}

RichEditorOptions are createRichEditor's: the extensions, StarterKit and TableKit when none, the document as Markdown, HTML or Tiptap's JSON, and plugins after the extensions'.

type RichExtensionSpec

type RichExtensionSpec struct {
	Name     string
	Priority int
	Node     *RichNodeSpec
	Mark     *RichMarkSpec
	// AddOptions gives the defaults under what configure sets.
	AddOptions            func() map[string]any
	AddGlobalAttributes   func(options map[string]any) []RichGlobalAttributes
	AddKeyboardShortcuts  func(options map[string]any) map[string]*Command
	AddCommands           func(options map[string]any) map[string]func(args map[string]any) *Command
	AddInputRules         func(options map[string]any) []RichInputRule
	AddProseMirrorPlugins func(options map[string]any) []RichPluginSpec
}

RichExtensionSpec is defineExtension's, Tiptap's extension config: a node or a mark named after it, and functions of its options run once as an editor is made, returning data.

type RichGlobalAttributes

type RichGlobalAttributes struct {
	Types      []string
	Attributes map[string]map[string]any
}

RichGlobalAttributes add attributes to other types.

type RichInputRule

type RichInputRule struct {
	Find    *regexp.Regexp
	Type    string
	Name    string
	Replace string
	Attrs   map[string]any
}

RichInputRule changes text as it is typed: Find, ending at the cursor ("$"), becomes a mark ("mark", its last group the text), a textblock ("textblock"), wraps the block ("wrapping") or is replaced by Replace ("text"). Name is the type, the extension's own when "".

type RichMarkSpec

type RichMarkSpec struct {
	Attrs map[string]map[string]any
	// Inclusive false keeps text typed at its end out of it.
	Inclusive       *bool
	Excludes, Group string
	Extra           map[string]any
}

RichMarkSpec is a mark type.

type RichNodeSpec

type RichNodeSpec struct {
	Content, Marks, Group string
	Inline, Atom, Code    bool
	Defining, Isolating   bool
	// Selectable false keeps node selections off it.
	Selectable *bool
	Whitespace string
	Attrs      map[string]map[string]any
	// LeafText is a leaf as text, RenderText what the view shows for an
	// inline leaf, of its attributes.
	LeafText, RenderText func(attrs map[string]any) string
	Extra                map[string]any
}

RichNodeSpec is a node type: ProseMirror's spec, attrs by name with their defaults ({ level: { default: 1 } }), and what it is as text.

type RichPasteEvent

type RichPasteEvent struct{ Text string }

RichPasteEvent is what is pasted, as text.

type RichPluginSpec

type RichPluginSpec struct {
	// Key names it among the editor's plugins.
	Key string
	// Keys bind keys by ProseMirror's names ("Mod-k") to commands.
	Keys            map[string]*Command
	HandleKeyDown   func(editor *Editor, key string) bool
	HandleTextInput func(editor *Editor, event RichTextInputEvent) bool
	HandleClick     func(editor *Editor, event RichClickEvent) bool
	HandlePaste     func(editor *Editor, event RichPasteEvent) bool
	// Decorations are what the view draws over the editor's state.
	Decorations func(editor *Editor) []RichDecoration
	// FilterTransaction refuses a transaction before it applies, the
	// editor's state still the one before it.
	FilterTransaction func(editor *Editor, tr RichTransactionInfo) bool
	// AppendTransaction may answer transactions that applied with a
	// command, run on the state they left.
	AppendTransaction func(editor *Editor, tr RichTransactionInfo) *Command
}

RichPluginSpec is a plugin whose behavior is the app's: a keymap and hooks the editor calls with itself, asked in plugin order, the first true taking the event.

type RichRect

type RichRect struct{ X, Y, Width, Height float64 }

RichRect is a box in points.

type RichSelection

type RichSelection struct {
	Type                   string
	Anchor, Head, From, To int
	Empty                  bool
}

RichSelection is a selection as JS reads it: its kind ("text", "node", "all", "cell", "gap") and positions.

type RichSuggestion

type RichSuggestion struct {
	Active   bool
	Query    string
	From, To int
	LastKey  string
	KeyCount int
}

RichSuggestion is an active suggestion's, as a mention list reads it.

type RichTextInputEvent

type RichTextInputEvent struct {
	From, To int
	Text     string
}

RichTextInputEvent is text typed over from to to.

type RichTransactionInfo

type RichTransactionInfo struct {
	DocChanged, SelectionSet bool
	Origin                   string
	Steps                    int
}

RichTransactionInfo is what JS reads of a transaction.

type View

type View struct {
	Editor              *Editor
	Markdown            string
	Placeholder         string
	BubbleMenu          func() core.Node
	BubbleMenuPlacement core.Placement
	BubbleMenuOffset    core.Points
	OnChange            func(revision int)
	OnSelectionChange   func(selection RichSelection)
	OnFocus, OnBlur     func()
	AutoFocus           bool
	Label               string
	Classes             richeditor.Classes
	Styles              richeditor.Styles
	Class               string
	Style               core.Style
	TestID              string
	OnLayout            func(core.Bounds)
	Tooltip             core.Node
}

View is RichEditor in TSX: base/richeditor's View over a RichEditorSession, else over a session of its own from Markdown.

func (View) Base

func (v View) Base() richeditor.View

Base is the Go view it shows, for a styled form to give its look.

func (View) Render

func (v View) Render(*core.Scope) core.Node

Render implements core.Node.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL