Wiki source code of Headless BlockNote Editor

Last modified by superadmin on 2026/11/25 21:38

Show last authors
1 The ##BlocknoteEditor## Vue component of the ##@xwiki/platform-editors-blocknote-headless## package renders a BlockNote editor for XWiki content. It wraps the React editor of the ##@xwiki/platform-editors-blocknote-react## package. To use it in your own component, see [[Integrate BlockNote Headless in a Vue Component>>doc:documentation.xs.dev.front-end.editor.blocknote.headless-integration.WebHome]].
2
3 {{version since="18.10.0RC1"}}The editor instance is configured with a single ##options## object: the options of each editor feature are grouped under ##features##, and the services the editor needs are taken from the service container instead of being passed as callbacks.{{/version}} ({{version before="18.10.0RC1"}}The editor was configured with the ##editorProps##, ##macros##, ##editorContent##, ##collaboration## and ##depsContainer## properties.{{/version}})
4
5 == BlocknoteEditor ==
6
7 {{id name="HCBlockNoteView"/}}
8
9 === Props ===
10
11 {{code language="typescript"}}
12 type Props = {
13 /** The configuration of the editor instance */
14 options: HeadlessEditorOptions;
15
16 /** Content to initialize the editor with */
17 content: BlockType[];
18 };
19 {{/code}}
20
21 === Editor Options ===
22
23 ##HeadlessEditorOptions## are the options of the editor (##EditorOptions##, exported by ##@xwiki/platform-editors-blocknote-react##), plus the options of the link modal shown by the Vue component.
24
25 {{code language="typescript"}}
26 type HeadlessEditorOptions = Omit<EditorOptions, "features"> & {
27 features?: EditorFeatures & {
28 links?: HeadlessLinkFeatureOptions | false;
29 };
30 };
31
32 type HeadlessLinkFeatureOptions = {
33 /**
34 * The link target types that the link modal doesn't offer for new links, while still using them to edit the links
35 * they handle. Unknown types are ignored.
36 */
37 excludedTargetTypes?: string[];
38 };
39
40 type EditorOptions = {
41 /** The Inversify container the editor takes the services and components it needs from. */
42 serviceContainer: Container;
43
44 /**
45 * The name of the editor instance, usually the name of the form field it is attached to. When editing in realtime,
46 * the editor instances with the same name, in the same collaboration session, edit the same content. Realtime
47 * collaboration is disabled without a name.
48 */
49 name?: string;
50
51 /**
52 * The reference of the document holding the edited content: the files are uploaded to it, and the references of the
53 * linked pages, attachments and images are relative to it. The current document (as provided by the
54 * `DocumentService`) when not specified.
55 */
56 sourceDocumentReference?: DocumentReference;
57
58 /**
59 * The id of the syntax of the edited content (e.g. "xwiki/2.1"). Its configuration, looked up in the service
60 * container, determines the editor features that are available. The minimal syntax, which disables most editor
61 * features, is used when the syntax has no configuration.
62 */
63 syntax: string;
64
65 /** The language of the editor UI (menus, toolbars, placeholders). */
66 lang: EditorLanguage;
67
68 /** The accessible name of the editing area (its `aria-label`). */
69 label: string;
70
71 /** Whether to focus the editor, with the caret at the start of the content, as soon as it is created. */
72 autofocus?: boolean;
73
74 appearance?: {
75 /**
76 * The display theme of the editor UI. It follows the color scheme preferred by the user when not specified.
77 */
78 theme?: "light" | "dark";
79
80 /** Who styles the edited content: the editor (the default), or the page hosting the editor. */
81 contentStyles?: "editor" | "host";
82 };
83
84 /** The realtime collaboration session to join. Ignored when the editor instance has no name. */
85 collaboration?: Collaboration;
86
87 /**
88 * Whether the user can upload files to the source document (by pasting or dropping them, or with the upload
89 * actions). Enabled by default, as long as there's something to store the files with.
90 */
91 upload?: boolean;
92
93 /**
94 * Where to keep the data about the blocks of the edited content that isn't part of the content (e.g. the output of
95 * the macros). A store is created for the editor instance when not specified.
96 */
97 metadata?: BlockMetadataStore;
98
99 /** The options of each editor feature. */
100 features?: EditorFeatures;
101 };
102 {{/code}}
103
104 === Editor Features ===
105
106 The options of each editor feature are keyed by the feature id. A feature is enabled when its options are missing, and setting them to ##false## disables it.
107
108 {{code language="typescript"}}
109 interface EditorFeatures {
110 /** The CSS classes (space separated) to add to the code blocks. */
111 codeBlocks?: { className?: string } | false;
112
113 /** The image suggestions, the image toolbar and the image panel. */
114 images?: false;
115
116 /** The actions to insert and edit links. The options are those of the link editor (see HeadlessLinkFeatureOptions). */
117 links?: object | false;
118
119 /** The macros, with the list of client-rendered macros, if any. */
120 macros?: { list?: MacroWithUnknownParamsType[] } | false;
121
122 /** The subscript and superscript text styles (when the syntax supports them). */
123 textStyles?: false;
124
125 /**
126 * The block types and text styles produced by the XWiki rendering (definition list items, groups, raw content,
127 * verbatim text, XWiki parameters), when the xwikiRenderingFeature is bound.
128 */
129 xwikiRendering?: false;
130 }
131 {{/code}}
132
133 The features are bound in the service container. The editor has no feature of its own: call the ##ComponentInit## of ##@xwiki/platform-editors-blocknote-react## to bind the generic ones (file transfer, code blocks, text styles, images, links and macros). It binds them in the order they contribute in (e.g. to the formatting toolbar). To bind only some of them, or in another order, bind the exported features yourself (##fileTransferFeature##, ##codeBlocksFeature##, ##textStylesFeature##, ##imagesFeature##, ##linksFeature##, ##macrosFeature##). The ##xwikiRenderingFeature## isn't bound by ##ComponentInit##: bind it when the content comes from the XWiki rendering.
134
135 {{code language="typescript"}}
136 import {
137 ComponentInit as BlockNoteReactComponentInit,
138 editorFeatureName,
139 xwikiRenderingFeature,
140 } from "@xwiki/platform-editors-blocknote-react";
141 import { Container } from "inversify";
142 import type { EditorFeature } from "@xwiki/platform-editors-blocknote-react";
143
144 const container = new Container();
145 new BlockNoteReactComponentInit(container);
146 container
147 .bind<EditorFeature>(editorFeatureName)
148 .toConstantValue(xwikiRenderingFeature);
149 {{/code}}
150
151 === Link Target Types ===
152
153 {{version since="18.10.0RC1"}}The link modal offers one link type per **link target type** bound in the service container, except those listed in ##features.links.excludedTargetTypes##, sorted by their ##order##. A link target type converts the links of one kind between their URL (used by the editor), their resource reference (used when the content is saved) and the configuration edited in the link modal. Its form, the fields edited in the link modal, is bound separately, so that the link target type doesn't depend on Vue. A link target type without a form is edited with a single text field, and its configuration is then a ##PlainTextLinkTargetConfig## (##{ location: string }##).{{/version}}
154
155 |=Package|=Binds
156 |##@xwiki/platform-link-type-api##|The ##LinkTargetTypeManager## the link modal and the editor use to find the link target types. Always required.
157 |##@xwiki/platform-link-type-default##|The built-in link target types: ##page## (order 0), ##attachment## (100), ##email## (200), ##path## (300), ##unc## (400) and ##url##, last since it accepts any URL. To offer fewer of them while still editing the existing links of those types, list them in ##excludedTargetTypes##. To drop them entirely, skip its ##ComponentInit## and bind the types you want yourself.
158 |##@xwiki/platform-link-type-ui##|The forms of the ##page##, ##attachment##, ##email## and ##url## types.
159
160 A link target type implements ##LinkTargetType## (##@xwiki/platform-link-type-api##), bound to ##linkTargetTypeRole## without a name; the link target types are told apart by their ##type##:
161
162 {{code language="typescript"}}
163 interface LinkTargetType<TConfig = unknown> {
164 /** Identifies the link target type. */
165 readonly type: string;
166
167 /** Sorts the link type selector, and the link target types asked to parse a URL (lower first). Defaults to 1000. */
168 readonly order?: number;
169
170 /** The label of the link type selector entry, translated by the link target type itself. */
171 getLabel(locale: string): Promise<string>;
172
173 /** The configuration used when the user selects this link type. */
174 createDefaultConfig(): TConfig;
175
176 /** The configuration of the link with the given URL, or null when the URL isn't of this type. */
177 fromURL(url: string): TConfig | null;
178 toURL(config: TConfig, context?: LinkContext): string;
179
180 /** The text of a link left without display text. The URL of the link when not implemented. */
181 getDefaultLabel?(config: TConfig, context?: LinkContext): string;
182
183 /** The configuration of the link with the given reference, or null when the reference isn't of this type. */
184 fromReference?(reference: ResourceReference, context?: LinkContext): TConfig | null;
185
186 /** The reference of the link, or undefined when the configuration doesn't designate a target yet. */
187 toReference?(config: TConfig, context?: LinkContext): ResourceReference | undefined;
188 }
189 {{/code}}
190
191 The ##context## gives the document holding the link (##sourceDocument##), against which relative references are resolved. When no link target type accepts the reference of an edited link, the link modal edits it as is, under its resource type.
192
193 The form implements ##LinkTargetTypeForm## (##@xwiki/platform-link-modal-ui##), bound to ##linkTargetTypeFormRole## without a name, with the ##type## of its link target type. ##targetField()## returns the Vue component of the field designating the target, ##optionFields()## (optional) the one of the fields shown in the "Options" group. Both receive the configuration with ##v-model## and the service container as the ##depsContainer## prop; the target field also receives the ##autofocus## and ##linkContext## props. Each component must declare these props even when it doesn't use them.
194
195 For example, a link type for the issues of an issue tracker, edited with a single text field (no form) holding the issue key, and saved as a URL:
196
197 {{code language="typescript"}}
198 import { linkTargetTypeRole } from "@xwiki/platform-link-type-api";
199 import { injectable } from "inversify";
200 import type { LinkTargetType, PlainTextLinkTargetConfig } from "@xwiki/platform-link-type-api";
201 import type { ResourceReference } from "@xwiki/platform-rendering-api";
202 import type { Container } from "inversify";
203
204 const ISSUE_URL = "https://issues.example.org/browse/";
205
206 @injectable()
207 class IssueLinkTargetType implements LinkTargetType<PlainTextLinkTargetConfig> {
208 readonly type = "issue";
209 // Before the URL link target type, which accepts any URL.
210 readonly order = 250;
211
212 async getLabel(): Promise<string> {
213 return "Issue";
214 }
215
216 createDefaultConfig(): PlainTextLinkTargetConfig {
217 return { location: "" };
218 }
219
220 fromURL(url: string): PlainTextLinkTargetConfig | null {
221 return url.startsWith(ISSUE_URL) ? { location: url.substring(ISSUE_URL.length) } : null;
222 }
223
224 toURL({ location }: PlainTextLinkTargetConfig): string {
225 return ISSUE_URL + location.trim();
226 }
227
228 getDefaultLabel({ location }: PlainTextLinkTargetConfig): string {
229 return location.trim();
230 }
231
232 fromReference(reference: ResourceReference): PlainTextLinkTargetConfig | null {
233 return reference.type === "url" ? this.fromURL(reference.reference) : null;
234 }
235
236 toReference(config: PlainTextLinkTargetConfig): ResourceReference | undefined {
237 return config.location.trim() === ""
238 ? undefined
239 : { type: "url", typed: false, reference: this.toURL(config), parameters: {} };
240 }
241 }
242
243 export function bindIssueLinkTargetType(container: Container): void {
244 container.bind<LinkTargetType>(linkTargetTypeRole).to(IssueLinkTargetType);
245 }
246 {{/code}}
247
248 The link target types are bound in the service container of the editor: an extension installed in XWiki can't add one to the BlockNote editor of XWiki, whose container is internal to its WebJar.
249
250 === Vue Events ===
251
252 {{code language="typescript"}}
253 {
254 // Emitted as soon as the editor's content changes, whoever caused the change: the local user, the initial content
255 // being loaded, or another participant of the realtime session.
256 "instant-change": [];
257
258 // Emitted in the same context as "instant-change", but debounced
259 "debounced-change": [content: BlockType[]];
260
261 // The subset of "instant-change" caused by the local user: emitted neither when the editor is filled with its
262 // initial content, nor when the content changes because of another participant of the realtime session.
263 "local-instant-change": [];
264 }
265 {{/code}}
266
267 === Vue Expose ===
268
269 {{code language="typescript"}}
270 {
271 // Get the editor's content
272 getContent(): BlockType[];
273
274 // Get the editor instance, or nothing before the editor is mounted
275 getEditor(): EditorType | undefined;
276 }
277 {{/code}}

Get Connected