Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 761
Custom schema example demos: alert title input + macro block#2710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
5979e7f0b130ea2ac9ebde2f8b66File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "playground": true, | ||
| "docs": true, | ||
| "author": "matthewlipski", | ||
| "tags": [ | ||
| "Intermediate", | ||
| "Blocks", | ||
| "Custom Schemas" | ||
| ], | ||
| "dependencies": {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Macro Block | ||
| In this example, we create a custom `Macro` block using the vanilla `createBlockSpec` API from `@blocknote/core`. Each macro block stores an `id` prop, and the `id` is used to look up "before" and "after" HTML strings in a global map. Those strings are injected as html on either side of the editable inline content. | ||
| This pattern is useful when you want a block whose decoration is driven by a runtime registry — for example, server-driven labels, citations, or templated wrappers. | ||
| **Try it out:** Edit the inline text inside each macro block. Notice that the "before" and "after" decorations stay non-editable, and that swapping the `id` in `App.tsx` would change the surrounding HTML without touching the block's content. | ||
| **Relevant Docs:** | ||
| - [Custom Blocks](/docs/features/custom-schemas/custom-blocks) | ||
| - [Editor Setup](/docs/getting-started/editor-setup) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Macro Block</title> | ||
| <script> | ||
| <!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY --> | ||
| </script> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="./main.tsx"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY | ||
| import React from "react"; | ||
| import { createRoot } from "react-dom/client"; | ||
| import App from "./src/App.jsx"; | ||
| const root = createRoot(document.getElementById("root")!); | ||
| root.render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode> | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "name": "@blocknote/example-custom-schema-macro-block", | ||
| "description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY", | ||
| "type": "module", | ||
| "private": true, | ||
| "version": "0.12.4", | ||
| "scripts": { | ||
| "start": "vite", | ||
| "dev": "vite", | ||
| "build:prod": "tsc && vite build", | ||
| "preview": "vite preview" | ||
| }, | ||
| "dependencies": { | ||
| "@blocknote/ariakit": "latest", | ||
| "@blocknote/core": "latest", | ||
| "@blocknote/mantine": "latest", | ||
| "@blocknote/react": "latest", | ||
| "@blocknote/shadcn": "latest", | ||
| "@mantine/core": "^9.0.2", | ||
| "@mantine/hooks": "^9.0.2", | ||
| "react": "^19.2.3", | ||
| "react-dom": "^19.2.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/react": "^19.2.3", | ||
| "@types/react-dom": "^19.2.3", | ||
| "@vitejs/plugin-react": "^6.0.1", | ||
| "vite": "^8.0.8" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { BlockNoteSchema, defaultBlockSpecs } from "@blocknote/core"; | ||
| import "@blocknote/core/fonts/inter.css"; | ||
| import { BlockNoteView } from "@blocknote/mantine"; | ||
| import "@blocknote/mantine/style.css"; | ||
| import { useCreateBlockNote } from "@blocknote/react"; | ||
| import { macroBlock } from "./Macro"; | ||
| import "./styles.css"; | ||
| const schema = BlockNoteSchema.create({ | ||
| blockSpecs: { | ||
| ...defaultBlockSpecs, | ||
| macro: macroBlock(), | ||
| }, | ||
| }); | ||
| export default function App() { | ||
| const editor = useCreateBlockNote({ | ||
| schema, | ||
| initialContent: [ | ||
| { | ||
| type: "paragraph", | ||
| content: | ||
| "Below are two macro blocks. Each looks up its before/after content from a global registry by id — the first uses HTML strings, the second uses a live <input> element:", | ||
| }, | ||
| { | ||
| type: "macro", | ||
| props: { id: "warning" }, | ||
| content: "Stay hydrated while editing", | ||
| }, | ||
| { | ||
| type: "macro", | ||
| props: { id: "note" }, | ||
| content: "Type a note here, then add a label on the left", | ||
| }, | ||
| { | ||
| type: "paragraph", | ||
| content: "Edit the inline text inside each macro to see it stay live.", | ||
| }, | ||
| ], | ||
| }); | ||
| return <BlockNoteView editor={editor} />; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { createBlockSpec, defaultProps } from "@blocknote/core"; | ||
| import { macroRegistry } from "./macroRegistry"; | ||
| // The Macro block — built with the vanilla `createBlockSpec` API. | ||
| // | ||
| // It carries a single `id` prop that is used to look up the HTML that should | ||
| // be rendered in the "before" and "after" slots around the block's editable | ||
| // inline content. This can actually be in any structure, it is in your control | ||
| export const macroBlock = createBlockSpec( | ||
| { | ||
| type: "macro", | ||
| propSchema: { | ||
| textAlignment: defaultProps.textAlignment, | ||
| textColor: defaultProps.textColor, | ||
| id: { | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick, would change this to | ||
| default: "" as const, | ||
| }, | ||
| }, | ||
| content: "inline", | ||
| }, | ||
| { | ||
| render: (block) => { | ||
| const wrapper = document.createElement("div"); | ||
| wrapper.className = "macro-block"; | ||
| // We pull from the "registry" what this block should insert before and after | ||
| const definition = macroRegistry[block.props.id]; | ||
| const before = document.createElement("div"); | ||
| before.className = "macro-slot macro-slot-before"; | ||
| before.contentEditable = "false"; | ||
| const beforeContent = definition?.before ?? ""; | ||
| if (typeof beforeContent === "string") { | ||
| before.innerHTML = beforeContent; | ||
| } else { | ||
| before.appendChild(beforeContent); | ||
| } | ||
| const content = document.createElement("div"); | ||
| content.className = "macro-content"; | ||
| const after = document.createElement("div"); | ||
| after.className = "macro-slot macro-slot-after"; | ||
| after.contentEditable = "false"; | ||
| const afterContent = definition?.after ?? ""; | ||
| if (typeof afterContent === "string") { | ||
| after.innerHTML = afterContent; | ||
| } else { | ||
| after.appendChild(afterContent); | ||
| } | ||
| // You have full control over this HTML structure | ||
| // and can inject the rich-text content wherever you want | ||
| wrapper.append(before, content, after); | ||
| return { | ||
| dom: wrapper, | ||
| contentDOM: content, | ||
| }; | ||
| }, | ||
| }, | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // A global, runtime-mutable registry that maps a macro id to the content that | ||
| // should be injected before and after the block's editable inline content. | ||
| // | ||
| // Each slot can be either: | ||
| // - an HTML string → injected via innerHTML | ||
| // - an HTMLElement → injected via appendChild (lets you put live, stateful | ||
| // DOM into the slot — e.g. an <input>) | ||
| // | ||
| // In a real application this could be populated from a server response, a | ||
| // shared module, or any other side channel — the block render function only | ||
| // reads from it. | ||
| export type MacroDefinition = { | ||
| before: string | HTMLElement; | ||
| after: string | HTMLElement; | ||
| }; | ||
| // An interactive "before" slot: a real <input> that lives in the registry and | ||
| // gets appended into whichever macro block uses this id. This mirrors the | ||
| // pattern from the Alert block's title input — but here it's authored as | ||
| // vanilla DOM and managed by the registry rather than by React state. | ||
| const labelInput = document.createElement("input"); | ||
| labelInput.className = "macro-label-input"; | ||
| labelInput.type = "text"; | ||
| labelInput.placeholder = "Label…"; | ||
| labelInput.setAttribute("aria-label", "Macro label"); | ||
| export const macroRegistry: Record<string, MacroDefinition> = { | ||
| warning: { | ||
| before: ` | ||
| <span class="macro-badge macro-badge-warning"> | ||
| <span class="macro-emoji">⚠️</span> | ||
| Heads up | ||
| </span> | ||
| `, | ||
| after: ` | ||
| <a class="macro-link" href="https://www.blocknotejs.org/" target="_blank" rel="noreferrer"> | ||
| Read the docs → | ||
| </a> | ||
| `, | ||
| }, | ||
| note: { | ||
| // An HTMLElement instead of a string — the macro block will appendChild it. | ||
| before: labelInput, | ||
| after: ` | ||
| <span class="macro-meta">— the input on the left is a live DOM node</span> | ||
| `, | ||
| }, | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative solution could be to create a custom block for every macro at runtime, right? Pros / cons of that approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This supports either way, and given that XWiki throws away the BlockNote representation and only stores their format, the only difference would be code organization: having 1 very dynamic block, or N dynamic blocks.
Given that their macros are already dynamically created at runtime, I don't think this should make too much of a difference, so I'd leave it to them