Code Drawing
'use client';
import * as React from 'react';
import { CodeDrawingPlugin } from '@platejs/code-drawing/react';
import { Plate, usePlateEditor } from 'platejs/react';
import { EditorKit } from '@/components/editor/editor-kit';
import { codeDrawingValue } from '@/registry/examples/values/code-drawing-value';
import { Editor, EditorContainer } from '@/components/ui/editor';
import { CodeDrawingElement } from '@/components/ui/code-drawing-node';
export default function CodeDrawingDemo() {
const editor = usePlateEditor({
plugins: [...EditorKit, CodeDrawingPlugin.withComponent(CodeDrawingElement)],
value: codeDrawingValue,
});
return (
<Plate editor={editor}>
<EditorContainer variant="demo">
<Editor />
</EditorContainer>
</Plate>
);
}
Features
- Support for multiple diagram types: PlantUml, Graphviz, Flowchart, and Mermaid
- Inline code editing with code-block-like styling
- Real-time preview with debounced rendering (500ms)
- Multiple view modes: Both (code and preview), Code only, Image only
- Responsive layout: horizontal on desktop (code left, preview right), vertical on mobile (preview top, code bottom)
- Border separator between code editor and preview in Both mode
- Toolbar controls: language selector and view mode selector (always visible on mobile, hover to show on desktop)
- Download rendered diagrams as images
- Floating toolbar with delete and download actions
Kit Usage
Installation
The fastest way to add code drawing functionality is with the CodeDrawingKit, which includes the pre-configured CodeDrawingPlugin with its Plate UI components.
'use client';
import { CodeDrawingPlugin } from '@platejs/code-drawing/react';
import { CodeDrawingElement } from '@/components/ui/code-drawing-node';
export const CodeDrawingKit = [
CodeDrawingPlugin.withComponent(CodeDrawingElement),
];CodeDrawingElement: Renders code drawing elements with inline editing and preview.
Add Kit
Add the kit to your plugins:
import { createPlateEditor } from 'platejs/react';
import { CodeDrawingKit } from '@/components/editor/plugins/code-drawing-kit';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
...CodeDrawingKit,
],
});Manual Usage
Installation
pnpm add @platejs/code-drawing
Add Plugin
Include CodeDrawingPlugin in your Plate plugins array when creating the editor.
import { CodeDrawingPlugin } from '@platejs/code-drawing/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
CodeDrawingPlugin,
],
});Configure Plugin
Configure the code drawing plugin with custom components.
import { CodeDrawingPlugin } from '@platejs/code-drawing/react';
import { createPlateEditor } from 'platejs/react';
import { CodeDrawingElement } from '@/components/ui/code-drawing-node';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
CodeDrawingPlugin.withComponent(CodeDrawingElement),
],
});withComponent: AssignsCodeDrawingElementto render code drawing elements.
Add Toolbar Button
You can add a toolbar button to insert code drawing elements. Add this item to the Insert Toolbar Button:
{
icon: <Code2Icon />,
label: 'Code Drawing',
value: KEYS.codeDrawing,
}Plugins
CodeDrawingPlugin
Plugin for rendering code drawings (PlantUml, Graphviz, Flowchart, Mermaid) with inline editing and preview capabilities.
API
editor.tf.insert.codeDrawing
Inserts a code drawing element at the current selection.
Transforms
tf.insert.codeDrawing
Inserts a code drawing element at the current selection.
Hooks
useCodeDrawingElement
A hook for a code drawing element that handles rendering and state management.
Types
TCodeDrawingElement
The code drawing element type.
CodeDrawingData
The data structure for code drawing elements.
CodeDrawingType
The type of diagram supported.
ViewMode
The view mode for code drawing elements.
Utilities
renderCodeDrawing
Renders a diagram from code based on the drawing type.
downloadImage
Downloads an image from a data URL.
On This Page
FeaturesKit UsageInstallationAdd KitManual UsageInstallationAdd PluginConfigure PluginAdd Toolbar ButtonPluginsCodeDrawingPluginAPIeditor.tf.insert.codeDrawingTransformstf.insert.codeDrawingHooksuseCodeDrawingElementTypesTCodeDrawingElementCodeDrawingDataCodeDrawingTypeViewModeUtilitiesrenderCodeDrawingdownloadImage