Expand description
§canvist_core
The platform-agnostic editing engine behind canvist. It sits between the
document model (canvist_model) and the platform hosts, and owns
everything about editing that does not depend on pixels:
EditorState: the document, the selection, stored marks, and IME composition, changed only throughEditorState::changeandEditorState::apply_steps.commands: plain functions such as typing, deleting by grapheme or word, splitting and joining blocks, lists, marks, and paste.motion: caret movement by grapheme, word, line, and document. Lines come from aLineNavigator, which the layout engine implements and hosts pass per event throughEnv::new;Env::headlessfalls back toParagraphNavigator.Caretcarries theAffinitythat tells the two sides of a soft wrap apart.Editor: the state plusHistory, aKeymapwith platform-awareMod,InputRules, andPasteRules, all contributed byExtensions.BaseKeymapbinds the platform’s standard keys andUndoInputRulelets Backspace take back an input rule.- A headless, typed API:
EditorBuilder::addreturns each extension’s typed handle;CommandSpecandQuerySpecgive dynamic hosts argument-checked named commands and queries;Editor::metadatadescribes all of it for binding generators; andEditor::canandEditor::chaindry-run or combine commands without side effects, on top of the model’s exact savepoints. - Building blocks for extensions:
queryreads the selection’s surroundings by type name, andSimpleMarkdescribes a mark that only switches on and off.
Grapheme and word boundaries follow Unicode (UAX #29), and inline embeds behave as single atoms everywhere.
§Example
use std::sync::Arc;
use canvist_core::EditorState;
use canvist_core::commands;
use canvist_model::BlockSpec;
use canvist_model::Document;
use canvist_model::Schema;
let schema = Arc::new(Schema::builder().block(BlockSpec::text("paragraph")).build()?);
let mut state = EditorState::new(Document::new(schema, 1))?;
commands::insert_text(&mut state, "Hello")?;
commands::split_block(&mut state)?;
commands::insert_text(&mut state, "world")?;
assert_eq!(state.doc().plain_text(), "Hello\nworld");An Editor adds keymaps, rules, and history on top:
use std::time::Duration;
use canvist_core::BaseKeymap;
use canvist_core::Editor;
use canvist_core::Env;
use canvist_core::Extension;
use canvist_core::KeyPress;
use canvist_model::BlockSpec;
use canvist_model::SchemaBuilder;
struct Paragraphs;
impl Extension for Paragraphs {
fn name(&self) -> &'static str {
"paragraphs"
}
fn schema(&self, schema: SchemaBuilder) -> SchemaBuilder {
schema.block(BlockSpec::text("paragraph"))
}
}
let mut editor = Editor::builder().extension(Paragraphs).extension(BaseKeymap).build()?;
let mut env = Env::headless(Duration::ZERO);
editor.insert_text("Hi", &mut env)?;
editor.handle_key(KeyPress::plain("Enter"), &mut env)?;
editor.undo(&mut env)?;
assert_eq!(editor.state().doc().plain_text(), "Hi");Re-exports§
pub use commands::CommandResult;pub use commands::Slice;pub use motion::Direction;pub use motion::Unit;
Modules§
- commands
- Commands: plain functions that read an
EditorState, decide whether they apply, and change it through one transaction. - motion
- Selection motion: by grapheme, word, line, and document, plus vertical
motion. Line and vertical motion ask a
LineNavigator, because only layout knows where visual lines are. - position
- Positions in document order: comparing points, ordering selections, and finding the blocks a selection covers.
- query
- Reading the selection’s surroundings for an extension’s queries.
- text
- Grapheme and word boundaries inside one block’s text.
Structs§
- Adornment
- Text shown at a point without being part of the document.
- Args
- Arguments checked against a command’s or query’s declared fields.
- Base
Keymap - Arrow keys, Home and End, Backspace and Delete with their word and line variants, Enter, Select All, and Escape, bound the way each platform binds them. It has the lowest priority, so extensions override any key.
- Caret
- A caret: a point and the line it belongs to at a soft wrap.
- Chain
- Commands that run together as one update and one undo step, or not at all: if any command does not apply or fails, everything rolls back. Undo and redo cannot be part of a chain.
- Command
Meta - A command: its name, what it does, and its arguments. Commands return an update or nothing.
- Command
Spec - A named command an extension offers, with typed arguments.
- Composition
- The text an input method editor is still composing (its preedit).
- Context
- Everything a bound command may use: the state, the history (for undo and redo), and the line navigator (for vertical and line motion).
- Dictation
- The dictation state of an editor: segments waiting to be inserted, and text inserted a moment ago.
- Editor
- A complete editing engine: state, history, keymaps, rules, and the extensions that provided them.
- Editor
Builder - Configures and builds an
Editor. - Editor
State - A document, its selection, and the transient state commands consult.
- Editor
Transaction - A model
Transactionthat also sets the selection, stored marks, and composition, created byEditorState::change. - Env
- What the host knows about the moment an input event happened.
- Extension
Meta - Everything one extension adds.
- Field
- A named, typed value: a command argument or a node attribute.
- History
Config - How
UndoHistorygroups changes. - Input
Rule - Rewrites text as it is typed, e.g.
##into a heading. - KeyPress
- A key press as the host reports it.
- Keymap
- Commands bound to key presses. Several commands may share a key; they run in binding order until one applies.
- Mark
Handle - Typed access to a
SimpleMarkextension, such as bold. - Modifiers
- The modifier keys held during a key press: the four flags that key events on every platform carry.
- Navigation
Error - Why a navigator could not answer, e.g. a block it had to lay out could not be measured.
- Node
Meta - A block, mark, or embed an extension registers, with its attributes.
- Paragraph
Navigator - A
LineNavigatorthat treats each text block as a single line and measures horizontal position in grapheme columns, for headless use. - Paste
Rule - Formats matching text in pasted content, e.g. URLs into links or
**bold**into bold text. - Query
Meta - A query: its name, what it answers, its arguments, and its result type.
- Query
Spec - A named question an extension can answer about the state, such as whether bold is active or the heading level at the caret.
- Registration
- Proof that an extension was registered with an
EditorBuilder. OnlyEditorBuilder::addcreates one, so a handle holding it cannot exist for an extension the app never added: - Rule
Input - What input rules match against: up to
RULE_CONTEXTbytes of the caret’s block before the caret, followed by the typed text. - Rule
Match - Where an input rule matched.
- Settling
- Text dictation inserted a moment ago, for hosts to fade into ink.
- Simple
Mark - Everything that distinguishes one on-or-off mark from another.
- Undo
Grouping - The rule
UndoHistorygroups changes by, on its own: whether each update joins the previous undo step. A shared history that keeps its steps elsewhere asks it how to track each local change, so collaborative undo groups typing exactly as single-user undo does. - Undo
History - Single-user undo history with grouping by time and by kind of edit.
- Undo
Input Rule - Backspace right after an input rule restores the text that triggered
it, so
##then Backspace leaves a literal##. It runs before every other Backspace binding, such as joining list items, because the rule’s effect is what the user is taking back. - Update
- Everything one change did: the model commit plus the editor state around it. Hosts use it to relayout touched blocks and repaint the selection; history uses it to record undo steps.
- Vertical
Motion - The result of moving up or down a line.
Enums§
- Affinity
- Which visual line a caret at a soft line break belongs to: the offset where one line wraps into the next is both the end of the first line and the start of the second.
- Edit
Kind - What kind of edit an update made.
- Editor
Error - Why an editor operation failed.
- Line
Edge - The logical start or end of a visual line.
- Lookback
- How much text before the caret an unanchored rule searches. Regular expressions cost time per byte, so a rule that runs on a keystroke must look at a bounded amount of text whatever the paragraph’s length.
- Node
Kind - What a schema item is.
- Origin
- Where an update came from.
- Platform
- The platform family, which decides what
Modmeans. - Transcript
- What a speech model said about one segment.
- Value
Type - The type of an argument, attribute, or query result.
- Vertical
Direction - Up or down a visual line.
Constants§
- DEFAULT_
LOOKBACK - The default
Lookback: enough for Markdown emphasis around a phrase. - RULE_
CONTEXT - The most text before the caret an input rule sees, as in TipTap.
Traits§
- Extension
- A feature: schema items plus the behaviour that goes with them.
- History
- Records updates and undoes or redoes them.
- Line
Navigator - Answers questions about visual lines, which only layout knows.
- Typed
Extension - An extension with a typed handle.
Functions§
- block_
type - Look up a block type an extension depends on.
- command
- Wrap a plain state command, such as one from
commands, as a bindableCommand. - embed_
type - Look up an embed type an extension depends on.
- mark_
type - Look up a mark type an extension depends on.
- parse_
binding - Parse a binding such as
Mod-Shift-zinto its key and modifiers. - redo
- Redo the last undone group.
- undo
- Undo the last group of changes.
- undo_
input_ rule - Revert an input rule that fired on the previous keystroke and type the
text that triggered it instead, so
##followed by Backspace leaves the literal##. Does nothing once anything else has happened. The result is a new edit in history, so undoing it reapplies the rule. - value_
to_ json - An attribute value as JSON.
Type Aliases§
- Binding
- A key binding contributed by an extension.
- Command
- A command that keymaps, named-command tables, and hosts can run.
- Command
Builder - Builds a command from checked arguments.
- Mark
Attrs - Computes a mark’s attributes from the matched text, e.g. a link’s href.
- QueryFn
- Answers a query from the state and checked arguments.
- Rule
Handler - Applies an input rule that matched.
- Update
Filter - A check every local update, fix-ups included, must pass to stand; see
Extension::update_filters.stateis the state after the update.