@rtif-sdk/engine
v1.13.0
Published
RTIF editor engine, history, and plugin host
Maintainers
Readme
@rtif-sdk/engine
Editor engine for RTIF (Rich Text Input Format). Manages document state, dispatches operations through a plugin lifecycle, and provides undo/redo history.
Install
npm install @rtif-sdk/engineUsage
Create an engine
import { createEngine } from '@rtif-sdk/engine';
import type { Document } from '@rtif-sdk/core';
const doc: Document = {
version: 1,
blocks: [{ id: 'b1', type: 'text', spans: [{ text: '' }] }],
};
const engine = createEngine(doc);Dispatch operations
engine.dispatch({ type: 'insert_text', offset: 0, text: 'Hello' });
console.log(engine.state.doc.blocks[0].spans[0].text); // 'Hello'Undo / Redo
engine.undo(); // reverts to empty document
engine.redo(); // re-applies 'Hello'Subscribe to changes
const unsubscribe = engine.onChange((state) => {
console.log('Document changed:', state.doc);
});Plugins
import type { Plugin } from '@rtif-sdk/engine';
const myPlugin: Plugin = {
id: 'my-plugin',
init(engine) {
engine.registerCommand('greet', (eng) => {
eng.dispatch({ type: 'insert_text', offset: 0, text: 'Hi! ' });
});
},
};
engine.use(myPlugin);
engine.exec('greet');Plugin Lifecycle
beforeApplyhooks — inspect, modify, or cancel operationsapply()— core applies operations to the documentafterApplyhooks — observe changes, trigger side effectsonChangelisteners — UI re-renders
Plugin errors are caught and isolated — a failing plugin never breaks the editor.
License
MIT
