-
-
Notifications
You must be signed in to change notification settings - Fork 725
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
Draft
nperez0111
wants to merge
4
commits into
main
Choose a base branch
from
feat/example-demos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5979e7f
feat(examples): polish editable title UX on alert block
nperez0111 0b130ea
feat(examples): add macro block example
nperez0111 2ac9ebd
chore: update lockfile for macro-block example
nperez0111 e2f8b66
fix(examples): drop .js extension on macroRegistry import
nperez0111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} />; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }; | ||
| }, | ||
| }, | ||
| ); | ||
48 changes: 48 additions & 0 deletions
48
examples/06-custom-schema/09-macro-block/src/macroRegistry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| `, | ||
| }, | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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