@r-zubkov/ngx-blocknote 0.0.0-stage → 0.1.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/usage.md ADDED
@@ -0,0 +1,323 @@
1
+ # Using ngx-blocknote
2
+
3
+ Start with the [quick start](../README.md#quick-start) for a runnable editor
4
+ and setup instructions.
5
+
6
+ ## Editor API
7
+
8
+ Your component creates and owns the editor. The host mounts it and connects its
9
+ events to Angular. When the editor changes or the host is removed, Angular
10
+ removes the host’s listeners and unmounts the editor. You can reuse the same
11
+ instance later.
12
+
13
+ | Binding | What it does |
14
+ | ------------------- | ----------------------------------------------------------------------------------------------------- |
15
+ | `[editor]` | Supplies the required `BlockNoteEditor` instance. Set `initialContent` when you create that instance. |
16
+ | `[editable]` | Toggles editing. Omitting it or passing `undefined` leaves the editor’s current setting in effect. |
17
+ | `(contentChange)` | Emits `NgxBlockNoteChangeEvent` with the editor and `getChanges()` for the transaction. |
18
+ | `(selectionChange)` | Emits `NgxBlockNoteSelectionChangeEvent` with the editor. |
19
+ | `(focusChange)` | Emits `NgxBlockNoteFocusChangeEvent` with the editor, `focused`, and the DOM event. |
20
+
21
+ Read the document through `editor.document` and update it with BlockNote’s
22
+ editor methods. All three event types preserve your block, inline content, and
23
+ style schema types. Use `.ngx-blocknote` to style the Angular host and
24
+ `.ngx-blocknote-mount` to target the editor’s mount element.
25
+ [BlockNote editor reference](https://www.blocknotejs.org/docs/reference/editor/overview)
26
+
27
+ ### Editor context and signals
28
+
29
+ - Call `injectBlockNoteEditor()` from an Angular injection context within the
30
+ editor host. Its read-only signal follows the nearest host’s `[editor]` input.
31
+ Read it from a template or from `ngOnInit` onward. For a custom schema, supply
32
+ the block, inline content, and style schema types.
33
+ - Use `toBlockNoteSignal(store, { selector, equal, injector })` to read an
34
+ extension store as a signal. It reads the initial value synchronously and
35
+ uses `Object.is` by default. Pass an `injector` to set the cleanup scope
36
+ explicitly or to call the helper outside an injection context. If you swap
37
+ editors, scope each store subscription to a component instance for that editor.
38
+
39
+ ### Example with events and a store signal
40
+
41
+ This version of `main.ts` displays the last content change, cursor block type,
42
+ focus state, and the formatting toolbar extension’s visibility.
43
+
44
+ ```ts
45
+ import {
46
+ ChangeDetectionStrategy,
47
+ Component,
48
+ inject,
49
+ Injector,
50
+ signal,
51
+ type OnInit,
52
+ type Signal,
53
+ } from "@angular/core";
54
+ import { bootstrapApplication } from "@angular/platform-browser";
55
+ import { BlockNoteEditor } from "@blocknote/core";
56
+ import { FormattingToolbarExtension } from "@blocknote/core/extensions";
57
+ import {
58
+ injectBlockNoteEditor,
59
+ NgxBlockNoteComponent,
60
+ toBlockNoteSignal,
61
+ } from "@r-zubkov/ngx-blocknote";
62
+
63
+ @Component({
64
+ selector: "editor-selection-status",
65
+ changeDetection: ChangeDetectionStrategy.OnPush,
66
+ template: `<p>Formatting toolbar requested: {{ toolbarVisible?.() ?? false }}</p>`,
67
+ })
68
+ class EditorSelectionStatus implements OnInit {
69
+ private readonly editor = injectBlockNoteEditor();
70
+ private readonly injector = inject(Injector);
71
+ protected toolbarVisible?: Signal<boolean>;
72
+
73
+ ngOnInit(): void {
74
+ const toolbar = this.editor().getExtension(FormattingToolbarExtension);
75
+ if (toolbar) {
76
+ this.toolbarVisible = toBlockNoteSignal(toolbar.store, {
77
+ injector: this.injector,
78
+ selector: (visible) => visible,
79
+ });
80
+ }
81
+ }
82
+ }
83
+
84
+ @Component({
85
+ selector: "app-root",
86
+ imports: [EditorSelectionStatus, NgxBlockNoteComponent],
87
+ changeDetection: ChangeDetectionStrategy.OnPush,
88
+ template: `
89
+ <button type="button" (click)="editable.set(!editable())">
90
+ {{ editable() ? "Switch to read-only" : "Enable editing" }}
91
+ </button>
92
+ <ngx-blocknote
93
+ [editor]="editor"
94
+ [editable]="editable()"
95
+ (contentChange)="lastChange.set($event.getChanges()[0]?.type ?? 'none')"
96
+ (selectionChange)="cursorBlock.set($event.editor.getTextCursorPosition().block.type)"
97
+ (focusChange)="focused.set($event.focused)"
98
+ >
99
+ <editor-selection-status />
100
+ </ngx-blocknote>
101
+ <output>Last change: {{ lastChange() }}</output>
102
+ <p>Cursor block: {{ cursorBlock() }}</p>
103
+ <p>Editor focused: {{ focused() }}</p>
104
+ `,
105
+ })
106
+ class AppComponent {
107
+ protected readonly editor = BlockNoteEditor.create({
108
+ initialContent: [{ type: "paragraph", content: "Hello BlockNote" }],
109
+ });
110
+ protected readonly editable = signal(true);
111
+ protected readonly lastChange = signal("none");
112
+ protected readonly cursorBlock = signal<string | undefined>(undefined);
113
+ protected readonly focused = signal(false);
114
+ }
115
+
116
+ bootstrapApplication(AppComponent).catch((error: unknown) => {
117
+ console.error(error);
118
+ });
119
+ ```
120
+
121
+ ## Custom renderers
122
+
123
+ Register Angular-backed specs with `BlockNoteSchema.create()`. Each factory
124
+ takes a standalone component and the `EnvironmentInjector` that should provide
125
+ its dependencies. Declare a required `context` input with the corresponding
126
+ context type.
127
+
128
+ | Render | Factory | Context | Register in |
129
+ | ----------------- | -------------------------------- | ----------------------------- | -------------------- |
130
+ | Blocks and frames | `createAngularBlockSpec` | `AngularBlockContext` | `blockSpecs` |
131
+ | Inline content | `createAngularInlineContentSpec` | `AngularInlineContentContext` | `inlineContentSpecs` |
132
+ | Text styles | `createAngularStyleSpec` | `AngularStyleContext` | `styleSpecs` |
133
+
134
+ - **Content slots.** For blocks with `inline` or `plain` content, inline specs
135
+ with `plain` or `styled` content, and text styles, provide a stable, empty
136
+ `data-content-dom` descendant or a `findContentDOM` callback. BlockNote manages
137
+ that element’s contents. Use `content: "none"` for atomic inline components
138
+ that render from props. A style’s value is `true` for boolean styles or a
139
+ string for string styles.
140
+ - **Frames.** Set `content: "none"` and `layoutOnly: true` for a layout block.
141
+ Give child blocks a stable, empty `data-frame-slot` or resolve it with
142
+ `findFrameSlot`. For a regular block, use `frame: { component, findFrameSlot }`
143
+ to wrap its content and children in another Angular component. Set
144
+ `contenteditable="false"` on frame controls outside the slot.
145
+ - **Updates.** Narrow `context().renderMode` to `live` before calling
146
+ `updateBlock` or `updateInlineContent`.
147
+ - **HTML.** Supply `parse` and `toExternalHTML` to recognize imported markup
148
+ and control export. The default export uses an Angular snapshot and releases
149
+ the view immediately. A custom block export can supply `childrenDOM` to keep
150
+ nested blocks inside the markup. Use `editor.blocksToFullHTML()` when
151
+ round-tripping the complete block structure.
152
+
153
+ ### Example with a custom schema
154
+
155
+ Save this as `custom-schema.ts`. Use `CustomSchemaEditor` as a standalone
156
+ route component or import it into another component and render
157
+ `<custom-schema-editor />`. The example groups an alert in a layout and
158
+ places a styled word and an atomic mention inside the alert’s editable text.
159
+
160
+ ```ts
161
+ import {
162
+ ChangeDetectionStrategy,
163
+ Component,
164
+ EnvironmentInjector,
165
+ inject,
166
+ input,
167
+ } from "@angular/core";
168
+ import {
169
+ BlockNoteEditor,
170
+ BlockNoteSchema,
171
+ defaultBlockSpecs,
172
+ defaultInlineContentSpecs,
173
+ defaultStyleSpecs,
174
+ } from "@blocknote/core";
175
+ import {
176
+ createAngularBlockSpec,
177
+ createAngularInlineContentSpec,
178
+ createAngularStyleSpec,
179
+ NgxBlockNoteComponent,
180
+ type AngularBlockContext,
181
+ type AngularInlineContentContext,
182
+ type AngularStyleContext,
183
+ } from "@r-zubkov/ngx-blocknote";
184
+
185
+ const alertConfig = {
186
+ type: "alert",
187
+ content: "inline",
188
+ propSchema: { tone: { default: "info", values: ["info", "warning"] } },
189
+ } as const;
190
+
191
+ const layoutConfig = {
192
+ type: "layout",
193
+ layoutOnly: true,
194
+ content: "none",
195
+ propSchema: {},
196
+ } as const;
197
+
198
+ const mentionConfig = {
199
+ type: "mention",
200
+ content: "none",
201
+ propSchema: { label: { default: "" } },
202
+ } as const;
203
+
204
+ const emphasisConfig = { type: "emphasis", propSchema: "string" } as const;
205
+
206
+ @Component({
207
+ changeDetection: ChangeDetectionStrategy.OnPush,
208
+ template: `
209
+ <aside [attr.data-alert]="context().block.props.tone">
210
+ <strong contenteditable="false">{{ context().block.props.tone }}</strong>
211
+ <div data-content-dom></div>
212
+ </aside>
213
+ `,
214
+ })
215
+ class AlertBlock {
216
+ readonly context = input.required<AngularBlockContext<typeof alertConfig>>();
217
+ }
218
+
219
+ @Component({
220
+ changeDetection: ChangeDetectionStrategy.OnPush,
221
+ template: `<section aria-label="Grouped blocks" data-frame-slot></section>`,
222
+ })
223
+ class LayoutBlock {
224
+ readonly context = input.required<AngularBlockContext<typeof layoutConfig>>();
225
+ }
226
+
227
+ @Component({
228
+ changeDetection: ChangeDetectionStrategy.OnPush,
229
+ template: `<span contenteditable="false">@{{ context().inlineContent.props.label }}</span>`,
230
+ })
231
+ class Mention {
232
+ readonly context =
233
+ input.required<AngularInlineContentContext<typeof mentionConfig>>();
234
+ }
235
+
236
+ @Component({
237
+ changeDetection: ChangeDetectionStrategy.OnPush,
238
+ template: `<mark [attr.data-emphasis]="context().value" data-content-dom></mark>`,
239
+ })
240
+ class Emphasis {
241
+ readonly context = input.required<AngularStyleContext<typeof emphasisConfig>>();
242
+ }
243
+
244
+ export function createCustomEditor(environmentInjector: EnvironmentInjector) {
245
+ const schema = BlockNoteSchema.create({
246
+ blockSpecs: {
247
+ paragraph: defaultBlockSpecs.paragraph,
248
+ alert: createAngularBlockSpec(alertConfig, {
249
+ component: AlertBlock,
250
+ environmentInjector,
251
+ }),
252
+ layout: createAngularBlockSpec(layoutConfig, {
253
+ component: LayoutBlock,
254
+ environmentInjector,
255
+ }),
256
+ },
257
+ inlineContentSpecs: {
258
+ ...defaultInlineContentSpecs,
259
+ mention: createAngularInlineContentSpec(mentionConfig, {
260
+ component: Mention,
261
+ environmentInjector,
262
+ }),
263
+ },
264
+ styleSpecs: {
265
+ ...defaultStyleSpecs,
266
+ emphasis: createAngularStyleSpec(emphasisConfig, {
267
+ component: Emphasis,
268
+ environmentInjector,
269
+ }),
270
+ },
271
+ });
272
+
273
+ return BlockNoteEditor.create({
274
+ schema,
275
+ initialContent: [
276
+ {
277
+ type: "layout",
278
+ children: [
279
+ {
280
+ type: "alert",
281
+ props: { tone: "warning" },
282
+ content: [
283
+ { type: "text", text: "Review", styles: { emphasis: "urgent" } },
284
+ " with ",
285
+ { type: "mention", props: { label: "Ada" } },
286
+ ],
287
+ },
288
+ ],
289
+ },
290
+ ],
291
+ });
292
+ }
293
+
294
+ @Component({
295
+ selector: "custom-schema-editor",
296
+ imports: [NgxBlockNoteComponent],
297
+ changeDetection: ChangeDetectionStrategy.OnPush,
298
+ template: `<ngx-blocknote [editor]="editor" />`,
299
+ })
300
+ export class CustomSchemaEditor {
301
+ protected readonly editor = createCustomEditor(inject(EnvironmentInjector));
302
+ }
303
+ ```
304
+
305
+ ## Server rendering and hydration
306
+
307
+ In an Angular SSR app, enable hydration in the shared `app.config.ts` used by
308
+ the browser and server bootstraps. Add the provider to your existing config.
309
+
310
+ ```ts
311
+ import type { ApplicationConfig } from "@angular/core";
312
+ import { provideClientHydration } from "@angular/platform-browser";
313
+
314
+ export const appConfig: ApplicationConfig = {
315
+ providers: [provideClientHydration()],
316
+ };
317
+ ```
318
+
319
+ The server renders an empty editor mount. Angular reuses that element on the
320
+ client, where the adapter mounts your editor. Keep the host and projected
321
+ content consistent across both renders, and run DOM-dependent work such as
322
+ custom HTML export in the browser. The custom schema example can run on a
323
+ hydrated route.