noteloom

Custom blocks & extensions

Every built-in type is authored the same way your own would be — there is no special-cased internal path.defineBlock() / defineInline() validate a config object and return a plain registry entry; pass them (nesting allowed) to useEditor({ extensions: [...] }).

jsx
import { useEditor, NoteloomEditor, defineBlock, starterKit } from 'noteloom';
import { MyBlock } from './MyBlock.jsx';

const myBlock = defineBlock({
  name: 'myBlock',                 // required — the block `type`
  component: MyBlock,              // required — React component, receives { id }
  contentModel: 'void',           // 'blocks' | 'runs' | 'void' (sets isLeaf)
  defaultProps: {},
  toHTML: (block, ctx) => '<div data-type="myBlock"></div>',
  fromHTML: (node, ctx) => null,  // return null if the node isn't yours
  toPlainText: (block, ctx) => '',
  slashCommand: { label: 'My Block', keywords: ['my'], run: (store, ctx) => {} },
});

function Editor() {
  const editor = useEditor({ extensions: [...starterKit(), myBlock] });
  return <NoteloomEditor editor={editor} />;
}

starterKit() is every built-in type as an array — useEditor() with no extensions registers exactly that set. starterKit({ exclude: ['canvas'] }) drops some; spread it in and add your own alongside.

Block config fields

FieldRequiredPurpose
nameYesUnique — used as the block type.
componentYesReact component. Receives only { id } — read data via useBlock(id).
contentModelNo'blocks' (holds child blocks) · 'runs' (holds text) · 'void' (neither). Default 'blocks'. Sets isLeaf.
defaultPropsNoProps for a freshly-inserted block.
toHTML / toPlainText / toMarkdown / fromHTMLNoSerialization. fromHTML returns null when a pasted node isn't yours.
slashCommand / slashCommandsNo{ label, icon, keywords, run(store, ctx) } — surfaced in the "/" menu.

The component's contract

Your component receives only { id }. Read the block's data with useBlock(id) (subscribes only to that block). For a 'runs' block render <EditableBlockContent /> so typing, marks, and inline widgets work for free; for a 'blocks' block render <BlockChildren parentId={id} />.

Custom inline types

defineInline() is the same idea for atomic mid-sentence widgets. Most inline needs are covered by createSelectFieldType instead of building one from scratch.

jsx
import { defineInline } from 'noteloom';

const myChip = defineInline({
  name: 'myChip',
  component: MyChipNode,           // receives { id }; read data with useRun(id)
  toHTML: (run, ctx) => `<span data-inline-type="myChip">${run.data?.label ?? ''}</span>`,
  fromHTML: (node, ctx) => null,
  toPlainText: (run, ctx) => run.data?.label ?? '',
  slashCommand: { label: 'My Chip', keywords: ['chip'], run: (store, ctx) => {} },
});

useEditor({ extensions: [...starterKit(), myChip] });

Behavior extensions

defineExtension() bundles types and/or behavior — keymap, onBeforeInput, onPaste, setup(ctx) — run by <NoteloomEditor> against a stable ctx facade. Returning truthy marks the event handled. smartQuotes() and autoPairBrackets() are ready-made ones.

jsx
import { defineExtension } from 'noteloom';

const behavior = defineExtension({
  name: 'my-behavior',
  keymap: {
    'Mod-Shift-x': (ctx, event) => {
      // ctx: { store, registry, inlineRegistry, container, getBlock, getRun,
      //        getRootId, applyOperation, applyOperations, getSelection,
      //        getCaret, setCaret, subscribe }
      return true; // truthy = handled: preventDefault + stop built-ins seeing it
    },
  },
  onBeforeInput: (ctx, event) => false,
  setup: (ctx) => {
    const stop = ctx.subscribe(() => {/* react to every change */});
    return stop; // cleanup on unmount
  },
});

useEditor({ extensions: [...starterKit(), behavior] });

Scaffold one

npx noteloom new block <name> (or new inline <name>) writes a <name>/ folder with a defineBlock() / defineInline() starting point, a component, and a test.

The callback form (legacy)

The registerBlocks / registerBuiltInBlocks / registerInlineTypes callbacks still work — a defineBlock() result is a plain registry entry, so registry.register('myBlock', myBlock) is equivalent. They're deprecated for removal in a future major; prefer extensions.

jsx
import { useEditor, NoteloomEditor, registerBuiltInBlocks } from 'noteloom';

const editor = useEditor({
  registerBlocks: (registry) => {
    registerBuiltInBlocks(registry);
    registry.register('myBlock', myBlockEntry); // a plain object, same fields as defineBlock's config
  },
});