# Image Extension ## Example ```svelte { errorMessage = err.message; } }) ]} class="min-h-24" />
{#if errorMessage} {errorMessage} {/if}
``` ## Documentation ## Usage Add `ImageExtension` via `extraExtensions` with an `upload` function. Images can then be pasted, dropped, or inserted programmatically. ```svelte ``` That's it — paste or drag-drop an image and it will upload automatically. ## Programmatic insertion Use `pickAndInsertImage` to open a file picker, or `insertImage` to insert a file directly. Great for toolbar buttons or slash menu items. ```svelte ``` ## Slash menu integration Add an image item to `SlashMenu` for a `/image` command. ```svelte { editor.chain().focus().deleteRange(range).run(); pickAndInsertImage(editor); } } ]} /> ``` ## Editable alt text Use the built-in `EditableImage` component to let users edit alt text. It shows a pencil button on hover that opens a modal with a textarea. ```svelte ``` ## Custom image component Pass `imageComponent` to replace the default `` rendering with your own Svelte component. Your component receives tiptap's `NodeViewProps` — use `props.node.attrs.src` for the image URL and `props.updateAttributes()` to update node data. ```svelte
{props.node.attrs.alt} {#if props.node.attrs.alt}
{props.node.attrs.alt}
{/if}
``` ```svelte ``` ## How it works `ImageExtension` is a single tiptap Extension that bundles everything: - **`@tiptap/extension-image`** for rendering final `` elements - **Upload placeholder node** with a Svelte NodeView showing a preview and progress bar - **ProseMirror plugins** for `handlePaste` and `handleDrop` — no external DOM handlers needed - **Extension storage** with a runtime `Map` so `File` objects reach the upload function (node attributes stay serializable) When an image is inserted, a blob preview is shown immediately. The `upload` function runs in the background. Once it resolves, the placeholder is replaced with a final image using the **returned URL**. ## Works with any editor `ImageExtension` works with `CoreRichTextEditor`, `RichTextEditor`, or any tiptap editor that supports `extraExtensions`. ## Sources Based on "TipTap" (https://tiptap.dev/docs/editor/extensions/nodes/image) ## API Reference ### ImageExtension A tiptap Extension that bundles image display, upload placeholders, and paste/drop handling. Add via extraExtensions. | Prop | Type | Default | Description | |------|------|---------|-------------| | upload | `(file: File, onProgress: (p: number) => void, signal: AbortSignal) => Promise` | `-` | Upload function. Receives the file, a progress callback (0–1), and an AbortSignal. Must return the final image URL. (required) | | accept | `string` | `'image/*'` | Accepted file types for the file picker and validation. | | maxFileSize | `number` | `0` | Maximum file size in bytes. 0 means unlimited. | | onError | `(error: Error) => void` | `-` | Called when an upload fails or a file is rejected. | | HTMLAttributes | `Record` | `{}` | HTML attributes applied to final elements. | | imageComponent | `Component` | `-` | Custom Svelte component for rendering final images. Receives NodeViewProps from tiptap (node.attrs.src, node.attrs.alt, etc.). | ### EditableImage A pre-built image component with alt text editing. Shows a pencil button on hover that opens a modal with a textarea. Use as imageComponent option. | Prop | Type | Default | Description | |------|------|---------|-------------| ### pickAndInsertImage Helper function that opens a native file picker and inserts the selected image into the editor. | Prop | Type | Default | Description | |------|------|---------|-------------| | editor | `Editor` | `-` | The tiptap editor instance. (required) | ### insertImage Helper function that inserts a file directly as an uploading image into the editor. | Prop | Type | Default | Description | |------|------|---------|-------------| | editor | `Editor` | `-` | The tiptap editor instance. (required) | | file | `File` | `-` | The image file to upload. (required) | ### ImageUploadFn Type signature for the upload function. | Prop | Type | Default | Description | |------|------|---------|-------------| | file | `File` | `-` | The image file to upload. (required) | | onProgress | `(progress: number) => void` | `-` | Progress callback. Values range from 0 to 1. (required) | | abortSignal | `AbortSignal` | `-` | Signal to abort the upload when the placeholder is deleted. (required) |