Custom fields form

The generated fields cover simple settings. For anything else a plugin gives its own form: any Svelte component, shown in the block's fields bar below the fields (or alone). It receives the block's props bound: assigning a new object is one edit of the block, with its own undo step. What it shows and where it takes its data from is the app's code — its own stores, its own contexts, its own scripts.


A photo block with its own picker

This demo stores an id from the app's image library. Its form offers the images of the album the app names in a context of its own (switch it above the editor) and uploads a file with its own script; alt text and height are ordinary fields. Paste or drop an image file into the editor: the plugin's fromFiles adds it to the library and makes a photo of it.

import { ImageIcon } from '@lucide/svelte';
import { mdText, type BlockPlugin } from '@atom-forge/brickee';
import { addImage, findImage } from './images.svelte';

// Images are this app's business, not Brickee's: the block stores an id from the app's library (it could just as well
// store a URL), picks it in its own fields form (PhotoForm, from the album the app's context names) and resolves it
// with the app's own lookup.
import PhotoForm from './PhotoForm.svelte';
import PhotoView, { type PhotoProps } from './PhotoView.svelte';
import PhotoPreview from './PhotoPreview.svelte';

export const photo: BlockPlugin<PhotoProps> = {
	type: 'photo',
	label: 'Photo',
	hint: 'One picked image',
	keys: 'photo image picture',
	icon: ImageIcon,
	props: () => ({ image: '', alt: '' }),
	fields: [
		{ key: 'alt', label: 'Alt text', kind: 'text', placeholder: 'What the image shows' },
		{ key: 'ratio', label: 'Height', kind: 'slider', min: 20, max: 150, unit: '%', none: 'Original' }
	],
	form: PhotoForm,
	tag: () => 'photo',
	content: 'none',
	markdown: (p) => {
		const url = findImage(p.image)?.url;
		return url ? `![${mdText(p.alt)}](${url})` : '';
	},
	// A pasted or dropped image file joins the app's library and becomes a photo (another plugin gets other files).
	fromFiles: (files) => {
		const file = files.find((f) => f.type.startsWith('image/'));
		return file ? { image: addImage(file), alt: file.name.replace(/\.[^.]+$/, '') } : null;
	},
	view: PhotoView,
	preview: PhotoPreview
};
Album (the app’s context):
A blue gradient

Open the photo’s fields: the picker below them is the plugin’s own form.