Skip to content

Repository files navigation

Svelty Editor

A Svelte 5 wrapper component for Editor.js -- built with runes, TypeScript, and lazy-loaded tools.

Features

  • Svelte 5 -- uses $props, $state, and $effect runes
  • TypeScript -- fully typed props, events, and exports
  • Bring your own tools -- install only the Editor.js plugins you need
  • Snippet support -- render custom UI around the editor with Svelte 5 snippets
  • Component API -- save(), setReadOnly(), registerTool(), and getManager() via bind:this
  • EditorManager -- access the underlying manager class directly for advanced use cases

Installation

npm install svelty-editor @editorjs/editorjs

Then install whichever Editor.js tools you want to use:

npm install @editorjs/header @editorjs/list @editorjs/paragraph

Available tool packages

PackageBlock type
@editorjs/headerHeadings (H1-H6)
@editorjs/listOrdered / unordered lists
@editorjs/paragraphParagraphs
@editorjs/imageImage blocks
@editorjs/embedEmbedded content (YouTube, etc.)
@editorjs/delimiterVisual dividers
@editorjs/markerInline text highlighting
@editorjs/quoteBlock quotes
@editorjs/tableTables
@editorjs/warningWarning callouts

You can also use any Editor.js plugin -- just pass its class in the tools prop.

Quick Start

<scriptlang="ts">import { SveltyEditor } from'svelty-editor';importHeaderfrom'@editorjs/header';importListfrom'@editorjs/list';importParagraphfrom'@editorjs/paragraph';let editor:ReturnType<typeofSveltyEditor>;asyncfunction handleSave() {const data =awaiteditor.save();console.log(data); }</script>
<SveltyEditorbind:this={editor}
tools={{
header: { class: Header },
list: { class: List },
paragraph: { class: Paragraph }
}}
placeholder="Start writing..."
/>
<buttononclick={handleSave}>Save</button>

Props

SveltyEditor accepts all Editor.js configuration options (except holder, which is managed internally), plus:

PropTypeDescription
toolsRecord<string, EditorTool>Map of tool names to their config (including the class).
dataOutputDataInitial editor content.
placeholderstringPlaceholder text for an empty editor.
autofocusbooleanFocus the editor on mount.
readOnlybooleanStart in read-only mode.
onChange(api, event) => voidCalled when editor content changes.
onReady() => voidCalled when the editor is ready.
classstringCSS class applied to the outer wrapper.
childrenSnippetSvelte 5 snippet rendered above the editor.

Component Methods

Access these via bind:this:

<SveltyEditorbind:this={editor} ... />

save()

Returns the editor content as an OutputData object.

constdata=awaiteditor.save();

setReadOnly(readOnly: boolean)

Toggle read-only mode.

editor.setReadOnly(true);

registerTool(name, loader, config?)

Dynamically register a tool at runtime. The editor will reinitialize to pick it up.

awaiteditor.registerTool('checklist',()=>import('@editorjs/checklist'),{inlineToolbar: true});

getManager()

Returns the underlying EditorManager instance for advanced use cases.

constmanager=editor.getManager();consteditorjs=manager.getEditor();// raw Editor.js instance

Tool Configuration

Each tool entry accepts an EditorTool object:

tools={{header: {class: Header,inlineToolbar: true,config: {levels: [1,2,3],defaultLevel: 2},shortcut: 'CMD+SHIFT+H'}}}
FieldTypeDescription
classTool constructorThe Editor.js tool class.
inlineToolbarboolean | string[]Enable inline toolbar for this block.
configRecord<string, unknown>Tool-specific configuration.
shortcutstringKeyboard shortcut.
tunesstring[]Block tunes to apply.

Using Snippets

Render custom UI above the editor content using Svelte 5 snippets:

<SveltyEditorbind:this={editor} tools={...}>
{#snippetchildren()}
<divclass="my-toolbar">
<buttononclick={() =>editor.save()}>Save</button>
</div>
{/snippet}
</SveltyEditor>

Listening to Changes

<SveltyEditortools={...}
onChange={(api, event) => {
console.log('Content changed:', event);
}}
onReady={() => {
console.log('Editor is ready');
}}
/>

Loading Initial Data

<SveltyEditortools={...}
data={{
blocks: [
{ type: 'header', data: { text: 'Hello', level: 2 } },
{ type: 'paragraph', data: { text: 'Some content here.' } }
]
}}
/>

Advanced: EditorManager

For more control, import EditorManager directly:

import{EditorManager}from'svelty-editor';importtype{EditorOptions}from'svelty-editor';

The manager exposes:

  • initialize() -- create the editor instance
  • save() -- get editor output
  • destroy() -- clean up
  • setReadOnly(boolean) -- toggle read-only
  • registerTool(name, loader, config?) -- add a tool dynamically
  • updateOptions(options) -- update editor options
  • getEditor() -- access the raw Editor.js instance
  • getInitializedTools() -- get loaded tool configs

TypeScript

All types are exported:

importtype{EditorOptions,EditorTool,EditorChangeEvent,I18nConfig,ToolsLoadMap,SveltyEditorProps}from'svelty-editor';

Development

git clone https://github.com/code-gio/svelty-editor.git
cd svelty-editor
npm install
npm run dev

Visit http://localhost:5173 to see the demo page.

License

MIT

About

A Svelte 5 wrapper component for Editor.js -- built with runes, TypeScript, and lazy-loaded tools.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages