@notectl/svelte
v0.0.4
Published
Svelte adapter for NotectlEditor
Maintainers
Readme
@notectl/svelte
Svelte adapter for NotectlEditor - a framework-agnostic rich text editor built on Web Components.
Installation
npm install @notectl/svelte @notectl/coreUsage
Component Usage
<script lang="ts">
import { NotectlEditor } from '@notectl/svelte';
import type { EditorAPI } from '@notectl/core';
let content = { type: 'doc', content: [] };
function handleContentChange(event: CustomEvent<{ content: unknown }>) {
content = event.detail.content;
console.log('Content changed:', content);
}
function handleReady(event: CustomEvent<{ editor: EditorAPI }>) {
const editor = event.detail.editor;
console.log('Editor ready:', editor);
}
</script>
<NotectlEditor
{content}
placeholder="Start writing..."
readOnly={false}
on:contentChange={handleContentChange}
on:ready={handleReady}
/>Action Usage
<script lang="ts">
import { notectlEditor } from '@notectl/svelte';
import type { EditorAPI } from '@notectl/core';
let content = { type: 'doc', content: [] };
function handleContentChange(newContent: unknown) {
content = newContent;
console.log('Content changed:', content);
}
function handleReady(editor: EditorAPI) {
console.log('Editor ready:', editor);
}
</script>
<div
use:notectlEditor={{
content,
placeholder: 'Start writing...',
onContentChange: handleContentChange,
onReady: handleReady,
}}
/>Store Usage
<script lang="ts">
import { NotectlEditor, createEditorStore } from '@notectl/svelte';
import type { EditorAPI } from '@notectl/core';
// Create reactive stores
const editorStore = createEditorStore({ type: 'doc', content: [] });
const { content, ready, error, isReady, hasError } = editorStore;
function handleReady(event: CustomEvent<{ editor: EditorAPI }>) {
const editor = event.detail.editor;
// Bind content store to editor
const unbind = $content.bindToEditor(editor);
// Set ready state
ready.set(true);
// Cleanup on component destroy
return () => {
unbind();
};
}
function handleContentChange(event: CustomEvent<{ content: unknown }>) {
content.set(event.detail.content);
}
function handleError(event: CustomEvent<{ error: Error }>) {
error.set(event.detail.error);
}
</script>
{#if $isReady}
<p>Editor is ready!</p>
{/if}
{#if $hasError}
<p>Error: {$error?.message}</p>
{/if}
<NotectlEditor
content={$content}
on:ready={handleReady}
on:contentChange={handleContentChange}
on:error={handleError}
/>
<pre>{JSON.stringify($content, null, 2)}</pre>API
Component Props
debug?: boolean- Enable debug modecontent?: string | object- Initial editor contentplaceholder?: string- Placeholder textreadOnly?: boolean- Read-only modeaccessibility?: object- Accessibility configurationi18n?: object- Internationalization settingstheme?: object- Theme configurationclassName?: string- Custom CSS class
Component Events
on:contentChange- Fired when content changeson:selectionChange- Fired when selection changeson:focus- Fired when editor gains focuson:blur- Fired when editor loses focuson:ready- Fired when editor is readyon:error- Fired when an error occurs
Component Methods
Access via bind:this:
<script>
let editor;
</script>
<NotectlEditor bind:this={editor} />
<button on:click={() => console.log(editor.getContent())}>
Get Content
</button>getContent(): unknown- Get current contentsetContent(content: unknown): void- Set contentgetState(): unknown- Get editor stateexecuteCommand(command: string, ...args: unknown[]): void- Execute commandregisterPlugin(plugin: unknown): void- Register a pluginunregisterPlugin(pluginId: string): void- Unregister a plugin
Actions
notectlEditor(node, options)
Initialize NotectlEditor on any element.
Options:
- All component props
onContentChange?: (content: unknown) => voidonSelectionChange?: (selection: unknown) => voidonFocus?: () => voidonBlur?: () => voidonReady?: (editor: EditorAPI) => voidonError?: (error: Error) => void
Stores
createEditorStore(initialContent?)
Create a composite store with all editor state.
Returns:
content: EditorContentStore- Content store with two-way bindingselection: EditorSelectionStore- Selection stateready: Writable<boolean>- Ready stateerror: EditorErrorStore- Error stateisReady: Readable<boolean>- Derived ready statehasError: Readable<boolean>- Derived error state
Individual Store Creators
createEditorContentStore(initial?)- Content storecreateEditorSelectionStore()- Selection storecreateEditorReadyStore()- Ready state storecreateEditorErrorStore()- Error store
License
MIT
