Editor

A keyboard-operable block editor with an insertion menu.

Preview

Block editor

How it works

One native contenteditable. Type / for blocks, @ to mention, Markdown at the start of a line or around words, and select text for the toolbar. Tab nests, Escape then Tab leaves. Agents run the same toolbar actions on blocks or text they name (AgentRanSelectionAction); Share quote is consequential, so an agent names the revision and a person confirms.

Installation

npx shadcn@latest add checkfu/uicontext/editor

Integration

Keep the blocks in your app and pass them back immediately on each Changed event. The editor owns native input, selection, and composition.

For saves that can finish out of order, set sync.revision on the first mount and echo each change token as sync.acknowledged. Apply a saved result only while its revision, session, and sequence still match. Advance the revision and clear the acknowledgement when restoring a saved version.

bun run test:consumer --case contracts --keep

Run this from the UiContext checkout. From the temporary app it prints, run bunx vite to open it.

View the executable example
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "./ui/uicontext/components/Button.ts"
import { Block, paragraph, plainText } from "./ui/uicontext/components/kit/Blocks.ts"
import { Editor, EditorChangeToken } from "./ui/uicontext/components/kit/Editor.ts"

const Save = Schema.Struct({ blocks: Schema.Array(Block), change: EditorChangeToken })
const Message = UiSchema.TaggedEnum({
	Edited: { blocks: Schema.Array(Block), change: Schema.optional(EditorChangeToken) },
	Restored: {},
	DeliveredSave: {},
})

const sameEdit = (a: typeof EditorChangeToken.Type | null, b: typeof EditorChangeToken.Type) =>
	a?.revision === b.revision && a.session === b.session && a.sequence === b.sequence

export const Notes = Html.component({
	name: "ControlledNotes",
	model: Schema.Struct({
		blocks: Schema.Array(Block),
		revision: Schema.Int,
		acknowledged: Schema.NullOr(EditorChangeToken),
		pending: Schema.NullOr(Save),
		status: Schema.String,
	}),
	message: Message,
}).make({
	init: () => ({
		blocks: [paragraph("note", "Starting note")],
		revision: 0,
		acknowledged: null,
		pending: null,
		status: "",
	}),
	update: {
		Edited: ($, { blocks, change }) => {
			if (change === undefined) {
				return $.evolve({
					blocks,
					revision: (revision) => revision + 1,
					acknowledged: null,
					pending: null,
				})
			}
			if (change.revision !== $.model.revision) return $.model
			return $.evolve({ blocks, acknowledged: change, pending: { blocks, change }, status: "Draft ready to save." })
		},
		Restored: ($) => $.evolve({
			blocks: [paragraph("note", "Restored note")],
			revision: (revision) => revision + 1,
			acknowledged: null,
			status: "Revision restored.",
		}),
		DeliveredSave: ($) => {
			const { pending, acknowledged } = $.model
			if (pending === null) return $.model
			return sameEdit(acknowledged, pending.change)
				? $.evolve({ blocks: pending.blocks, pending: null, status: "Draft saved." })
				: $.evolve({ pending: null, status: "Earlier save ignored." })
		},
	},
	view: (h, { blocks, revision, acknowledged, pending, status }) =>
		h.section.attrs({ "aria-labelledby": "notes-heading", class: "contract-notes" }).children(
			h.h2.attr("id", "notes-heading").children("Editable notes"),
			Editor(h, {
				key: "notes",
				props: {
					id: "notes",
					label: "Notes",
					blocks,
					sync: { revision, acknowledged: acknowledged ?? undefined },
				},
				onEmit: {
					Changed: ({ blocks, change }) => Message.Edited({ blocks, change }),
					ChoseCommand: () => undefined,
					QueriedMention: () => undefined,
					RequestedAction: () => undefined,
					OpenedLink: () => undefined,
					OpenedMention: () => undefined,
					Blurred: () => undefined,
				},
			}),
			h.div.attr("class", "contract-actions").children(
				Button(h, { emphasis: "outline" }).on("click", Message.Restored()).children("Restore revision"),
				Button(h, { emphasis: "outline" }).disabled(pending === null)
					.on("click", Message.DeliveredSave()).children("Deliver save response"),
			),
			h.output.attr("aria-label", "App-owned note").children(blocks.map((block) => plainText(block.content)).join("\n")),
			h.p.attr("role", "status").children(status),
		),
})
notes.ts on GitHub

Source