Skip to main content

Crate canvist

Crate canvist 

Source
Expand description

§canvist

The canvist rich-text editor engine in one crate. Add it once and reach every part of the engine through a module, turning on the parts you need with Cargo features:

cargo add canvist
cargo add canvist --features collab,ssr
ModuleCrateFeatureWhat it is
modelcanvist_modelalwaysThe document model: schema, blocks, marks, steps, transactions
corecanvist_corealwaysThe editing engine: state, commands, keymaps, history
extensionscanvist_extensionsalwaysThe extensions; starter-kit (default) turns on TipTap’s kit
headlesscanvist_headlessheadlessLoad, edit, and export documents with no UI (default)
collabcanvist_collabcollabYjs collaboration: bindings, y-sync, awareness, shared undo
layoutcanvist_layoutlayoutText and block layout, hit testing, display lists, SVG/HTML
scenecanvist_scenecanvasThe design canvas’s scene model
canvascanvist_canvascanvasThe canvas’s camera, hit testing, selection, handles, tools
paintcanvist_paintpaintDraw scenes as SVG, PNG (png), or with vello (gpu)

The default features, headless and starter-kit, are what a server, a script, or a test needs. The other features add to them:

  • extras and full: every extension beyond the StarterKit, with the emoji table loaded at runtime (extras) or compiled in (full).
  • collab: Yjs collaboration. With headless, a document is a Yjs peer and a y-sync endpoint.
  • journal: Doc::record_commits, for hosts that keep a layout of a document other code changes too.
  • ssr: headless::Renderer, which renders the HTML and SVG the canvas editor would paint.
  • layout, and parley to measure text with real fonts.
  • canvas, paint, png, and gpu: the design canvas and its renderers.
  • headless-scene: headless::SceneEngine, which loads, edits, and exports canvas scenes as SVG and PNG on a server.

Each module is the crate it names, so its documentation, and every path in it, apply unchanged: canvist_headless::Engine is canvist::headless::Engine.

§Example

Parse Markdown, select a word, make it bold, and export the result:

use canvist::headless::Engine;
use canvist::headless::Format;
use canvist::headless::Position;

// An engine holds the extensions. Build one and share it.
let engine = Engine::starter_kit();
let kit = engine.handles();
let mut doc = engine.parse(Format::Markdown, "# Plan\n\nShip the docs")?;

// Select "docs": block 1, characters 9 to 13.
doc.select_range(&Position::new([1], 9), &Position::new([1], 13))?;
doc.run(&kit.bold.toggle())?;

assert_eq!(doc.export(Format::Markdown), "# Plan\n\nShip the **docs**");
assert_eq!(
	doc.export(Format::Html),
	"<h1>Plan</h1><p>Ship the <strong>docs</strong></p>"
);

Modules§

canvas
canvist_canvas
collab
canvist_collab
core
canvist_core
extensions
canvist_extensions
headless
canvist_headless
layout
canvist_layout
model
canvist_model
paint
canvist_paint
scene
canvist_scene