@barocss/editor-core
Browse live examples: JavaScript / DOM
Editor sessions, commands, extensions, selection, keybindings, and history.
Purpose
Use this package for an editor without a prescribed UI. For Note, Word, Slides, or Site, start with that product's editor factory instead.
Install
npm install @barocss/editor-core @barocss/schema @barocss/extensions
The published package provides ES modules and TypeScript declarations. Use a bundler that supports package exports.
Public entry points
| Import | Role |
|---|---|
@barocss/editor-core | Public JavaScript and TypeScript API |
Import only these public paths. Source paths such as @barocss/editor-core/src/... are not part of the published API.
Usage
import { Editor } from '@barocss/editor-core';
import { createSchema, getMinimalSchemaDefinition } from '@barocss/schema';
import { createCoreExtensions } from '@barocss/extensions';
const editor = new Editor({
schema: createSchema('example', getMinimalSchemaDefinition()),
extensions: createCoreExtensions(),
});
editor.loadDocument({ stype: 'document', content: [
{ stype: 'paragraph', content: [{ stype: 'inline-text', text: 'Hello' }] },
]}, 'example-session');
console.log(editor.getRootId());
// Attach your view here. When the session ends:
editor.destroy();
Integration notes
Create one editor per independent editing session. Load a document after construction, attach a view separately, and call destroy when the session ends.
Extend an editor
An extension is an object or a class instance with a unique name. Install it with the constructor's extensions array or editor.use(extension). Use commands for declarative command registration. onCreate and onDestroy can attach and remove host subscriptions. Create a fresh extension instance for each editor when it holds mutable state.
This example adds a command that appends a paragraph. It uses the model transaction API so the edit participates in history. Install @barocss/model in addition to the packages above.
import { Editor, type Extension } from '@barocss/editor-core';
import { createSchema, getMinimalSchemaDefinition } from '@barocss/schema';
import { createCoreExtensions } from '@barocss/extensions';
import { addChild, transaction } from '@barocss/model';
export async function demonstrateCommand() {
const appendParagraph: Extension = {
name: 'example.append-paragraph',
commands: [{
name: 'example.appendParagraph',
canExecute: (editor) => !!editor.getRootId(),
execute: async (editor) => {
const root = editor.getRootId();
if (!root) return false;
const result = await transaction(editor, [addChild(root, {
stype: 'paragraph',
content: [{ stype: 'inline-text', text: 'Added through a command.' }],
})]).commit();
return result.success;
},
}],
};
const editor = new Editor({
schema: createSchema('command-example', getMinimalSchemaDefinition()),
extensions: [...createCoreExtensions(), appendParagraph],
});
try {
editor.loadDocument({ stype: 'document', content: [
{ stype: 'paragraph', content: [{ stype: 'inline-text', text: 'Original.' }] },
]}, 'command-example');
const inserted = await editor.executeCommand('example.appendParagraph');
const undone = await editor.executeCommand('undo');
return { inserted, undone };
} finally {
editor.destroy();
}
}
This command targets the minimal document schema. It does not mount an editing view or add a toolbar button. Adapt the insertion target and schema checks before using it in a product kit.
Extension boundaries
registerCommandreplaces an existing command with the same name. Use namespaced names for host commands.useignores an extension whose name is already installed.unuseremoves commands declared in itscommandsarray; it does not restore a previously overwritten command. Do not treat it as a general hot-swap mechanism.editor.onreturns no unsubscribe function. Pair it witheditor.off(event, callback)using the same callback.- Transaction before-hooks run through the model transaction builder. Do not assume a hook intercepts direct store writes or every editor API.
- A schema, command, renderer, and UI control are separate registrations. Installing one does not provide all four.
See extension boundaries and product customization for the supported composition paths.
Documentation
- Package guide
- Choose a package
- Source and tests
- Detailed architecture reference (older deep reference; use the package guide for current entry points).
License
MIT. The published archive includes the license in dist/LICENSE.