A block document editor and its read-only viewers, with a toolbar, a bubble, a block menu and a slash menu, over the standard kit of headings, lists, tasks and marks.
Installation#
pnpm dlx shadcn@latest add @zui/document-editorUsage#
import { collectUiContributions } from '@zeroxsolutions/editor-core/document/ui/index';
import { standardKit } from '@/components/editor/document/features/standard';
import { Editor } from '@/components/editor/document/react';
import { BubbleMenu } from '@/components/editor/document/ui/bubble-menu';
import { EditorToolbar } from '@/components/editor/document/ui/editor-toolbar';
import { SlashMenu } from '@/components/editor/document/ui/slash-menu';const features = [standardKit()];
const { toolbar, slash, bubble } = collectUiContributions(features);
function NotesEditor({ content }: { content: DocJSON }) {
const [editor, setEditor] = useState<IEditor | null>(null);
return (
<>
{editor && <EditorToolbar editor={editor} items={toolbar} />}
<Editor features={features} content={content} onReady={setEditor} onChange={save} />
{editor && <SlashMenu editor={editor} items={slash} />}
{editor && <BubbleMenu editor={editor} items={bubble} />}
</>
);
}Editor builds the editor once, on mount, from features and content; to load another document, give
it a new key. The toolbar and the menus read the editor it hands to onReady, and draw the items the
features contribute. A feature item adds its own feature to features beside the standard kit.
To show a stored document without the editing engine, render Viewer, which a server can render:
import { Viewer } from '@/components/editor/document/react/viewer';
<Viewer doc={content} features={features} />;ViewerLive renders the same document through the live editor with editing off, for the reading
interactions a static render cannot give.
API reference#
Editor#
| Prop | Type | Default |
|---|---|---|
features | EditorFeature[] | - |
content | DocJSON | - |
editable | boolean | true |
className | string | - |
onReady | (editor: IEditor) => void | - |
onChange | (delta: Delta) => void | - |
It renders the document with data-editor="document" and the document-editor prose classes its styles
key on. Beside the standard kit's block drag handle it draws an "Insert block below" button, which opens the
slash menu on a new line.
Viewer#
| Prop | Type | Default |
|---|---|---|
doc | DocJSON | - |
features | EditorFeature[] | [] |
theme | IEditorTheme | - |
forcedMode | 'light' | 'dark' | - |
className | string | - |
Given theme or forcedMode, it themes its document apart from the page with
Editor Theme, in light mode unless forcedMode says otherwise.
ViewerLive#
Takes every prop Editor takes except editable and onChange.