@dojo-ng/rich-text-image
v0.1.0
Published
Image insert plugin for @dojo-ng/rich-text
Downloads
0
Readme
@dojo-ng/rich-text-image
Image insert plugin for @dojo-ng/rich-text
Part of Dojo NG, a framework-agnostic web component library. BSD-3-Clause.
An opt-in plugin for
@dojo-ng/rich-text(not a custom element). It contributes anImageNode(a decorator node rendering an<img>) plus anINSERT_IMAGE_COMMAND, and a toolbar button that opens adj-dialogfor inserting an image from a file (dj-file-input) or a URL — the most recent source wins. Alt text is REQUIRED: the insert button stays disabled until it is non-empty, because the accessible name is mandatory. ExportsimagePlugin,createImagePlugin({ upload }),ImageNode,$createImageNode,$isImageNode, andINSERT_IMAGE_COMMAND. Clicking an image selects it (a NodeSelection); Backspace/Delete then removes it (handled by Lexical). SettingpluginsREPLACES the default set, so spread...defaultPlugins. DATA-URL CAVEAT: the defaultuploadreads the file to a data URL, which bloats the HTML value — pass your ownupload(file) => Promise<string>in production to host the file and return a URL. There is no size limit by default; passcreateImagePlugin({ maxSize })(bytes) to cap the picker's file input. PASTE CAVEAT: the default paste sanitizer drops<img>tags, so images enter via the dialog or thevalueproperty, not paste. DEFERRED: resize/crop, captions, drag/paste insertion, alignment.
Install
npm install @dojo-ng/rich-text-imageUsage
Compose the image plugin with the default set. Pass createImagePlugin({ upload }) to host files instead of embedding data URLs.
<dj-rich-text id="editor" label="Article"></dj-rich-text>
<script type="module">
import "@dojo-ng/rich-text";
import { defaultPlugins } from "@dojo-ng/rich-text";
import { createImagePlugin } from "@dojo-ng/rich-text-image";
const imagePlugin = createImagePlugin({ upload: async (file) => (await myUploader(file)).url });
document.getElementById("editor").plugins = [...defaultPlugins, imagePlugin];
</script>