Skip to main content

Crate canvist_core

Crate canvist_core 

Source
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:

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.
BaseKeymap
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.
CommandMeta
A command: its name, what it does, and its arguments. Commands return an update or nothing.
CommandSpec
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.
EditorBuilder
Configures and builds an Editor.
EditorState
A document, its selection, and the transient state commands consult.
EditorTransaction
A model Transaction that also sets the selection, stored marks, and composition, created by EditorState::change.
Env
What the host knows about the moment an input event happened.
ExtensionMeta
Everything one extension adds.
Field
A named, typed value: a command argument or a node attribute.
HistoryConfig
How UndoHistory groups changes.
InputRule
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.
MarkHandle
Typed access to a SimpleMark extension, such as bold.
Modifiers
The modifier keys held during a key press: the four flags that key events on every platform carry.
NavigationError
Why a navigator could not answer, e.g. a block it had to lay out could not be measured.
NodeMeta
A block, mark, or embed an extension registers, with its attributes.
ParagraphNavigator
A LineNavigator that treats each text block as a single line and measures horizontal position in grapheme columns, for headless use.
PasteRule
Formats matching text in pasted content, e.g. URLs into links or **bold** into bold text.
QueryMeta
A query: its name, what it answers, its arguments, and its result type.
QuerySpec
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. Only EditorBuilder::add creates one, so a handle holding it cannot exist for an extension the app never added:
RuleInput
What input rules match against: up to RULE_CONTEXT bytes of the caret’s block before the caret, followed by the typed text.
RuleMatch
Where an input rule matched.
Settling
Text dictation inserted a moment ago, for hosts to fade into ink.
SimpleMark
Everything that distinguishes one on-or-off mark from another.
UndoGrouping
The rule UndoHistory groups 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.
UndoHistory
Single-user undo history with grouping by time and by kind of edit.
UndoInputRule
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.
VerticalMotion
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.
EditKind
What kind of edit an update made.
EditorError
Why an editor operation failed.
LineEdge
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.
NodeKind
What a schema item is.
Origin
Where an update came from.
Platform
The platform family, which decides what Mod means.
Transcript
What a speech model said about one segment.
ValueType
The type of an argument, attribute, or query result.
VerticalDirection
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.
LineNavigator
Answers questions about visual lines, which only layout knows.
TypedExtension
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 bindable Command.
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-z into 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.
CommandBuilder
Builds a command from checked arguments.
MarkAttrs
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.
RuleHandler
Applies an input rule that matched.
UpdateFilter
A check every local update, fix-ups included, must pass to stand; see Extension::update_filters. state is the state after the update.