@bloklabs/react 1.6.2 → 1.8.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/src/useBlok.ts CHANGED
@@ -10,9 +10,15 @@ import {
10
10
  type BlockPortalRegistry,
11
11
  } from './block-portal-registry';
12
12
  import { setRegistry, removeRegistry } from './registry-map';
13
+ import { setContentBaseline, removeContentBaseline } from './content-baseline-map';
13
14
  import { bindLiveToolConfigFunctions, buildLiveBlockConfig } from './live-tool-config';
14
- import { createEmittedEchoWindow, normalizeReadOnlyConfig, toRenderableData } from '@bloklabs/core/adapters';
15
- import type { Blok } from '@/types';
15
+ import {
16
+ createEmittedEchoWindow,
17
+ equalsOutputData,
18
+ normalizeReadOnlyConfig,
19
+ toRenderableData,
20
+ } from '@bloklabs/core/adapters';
21
+ import type { Blok, LiveHandlers } from '@/types';
16
22
  import type { UseBlokConfig } from './types';
17
23
 
18
24
  interface EditorInstanceState {
@@ -135,12 +141,15 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
135
141
  const depsToken = useMemo(() => ({}), deps ?? []);
136
142
 
137
143
  // Tracks the data the editor currently reflects — set when content is seeded
138
- // or rendered (the reactive `data` effect below) AND when the editor emits its
139
- // own serialized content via `onSave`. The latter is what makes a controlled
140
- // `onSave -> setData -> data` round-trip a no-op: the echoed payload deep-equals
141
- // this baseline, so the data effect skips the redundant render() (which would
142
- // otherwise reset the caret). Declared here so the `onSave` wrapper below can
143
- // update it before the consumer's setState re-runs the data effect.
144
+ // or rendered (the reactive `data` effect below), when the editor emits its
145
+ // own serialized content via `onSave`, and when an IMPERATIVE call changed the
146
+ // content out of band (the content-baseline channel registered below). The
147
+ // `onSave` writer is what makes a controlled `onSave -> setData -> data`
148
+ // round-trip a no-op: the echoed payload content-equals this baseline, so the
149
+ // data effect skips the redundant render() (which would otherwise reset the
150
+ // caret). Declared here so the `onSave` wrapper below can update it before the
151
+ // consumer's setState re-runs the data effect. `undefined` means "nothing
152
+ // recorded yet" — never an empty document.
144
153
  const lastRenderedDataRef = useRef(config.data);
145
154
  // Window of recently emitted `onSave` payloads. The baseline above only
146
155
  // remembers the LAST one, but a host that persists on save and refetches can
@@ -172,6 +181,19 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
172
181
  // Last-applied tool-level `toolbox` setting per tool name (dedupe baseline for
173
182
  // the reactive toolbox effect below). Seeded from the construction-time tools.
174
183
  const appliedToolboxRef = useRef<Map<string, unknown>>(new Map());
184
+ // Live callback channel (see the handler-presence effect below): the current
185
+ // editor's stable per-handler wrappers (each forwards to the LATEST render's
186
+ // prop through `configRef`, so their identity never changes) and the presence
187
+ // actually installed on that editor — the dedupe baseline.
188
+ const handlerWrappersRef = useRef<LiveHandlers | null>(null);
189
+ const appliedHandlerPresenceRef = useRef<Record<keyof LiveHandlers, boolean>>({
190
+ onChange: false,
191
+ onSave: false,
192
+ onEnter: false,
193
+ onSubmit: false,
194
+ onBeforeRender: false,
195
+ onAfterRender: false,
196
+ });
175
197
 
176
198
  // Main lifecycle effect
177
199
  useEffect(() => {
@@ -212,6 +234,7 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
212
234
  if (state.editor !== null && !state.isDestroyed) {
213
235
  removeHolder(state.editor);
214
236
  removeRegistry(state.editor);
237
+ removeContentBaseline(state.editor);
215
238
  try {
216
239
  state.editor.destroy();
217
240
  } catch {
@@ -259,60 +282,69 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
259
282
  ])
260
283
  );
261
284
 
262
- const blokConfig = {
263
- ...currentConfig,
264
- ...(liveTools === undefined ? {} : { tools: liveTools as UseBlokConfig['tools'] }),
265
- holder,
266
- onReady: (...args: Parameters<NonNullable<UseBlokConfig['onReady']>>): void => {
267
- configRef.current.onReady?.(...args);
268
- },
285
+ // Stable per-editor wrappers for the live callback config. Each forwards to
286
+ // the LATEST render's prop through `configRef`, so a consumer never has to
287
+ // freeze callback identities — and, because identity is fixed for the
288
+ // editor's life, only PRESENCE has to be synced afterwards (below).
289
+ const handlerWrappers: LiveHandlers = {
269
290
  onChange: (...args: Parameters<NonNullable<UseBlokConfig['onChange']>>): void => {
270
291
  configRef.current.onChange?.(...args);
271
292
  },
272
- // Forward the return value: it is the "handled" signal the core acts on.
273
- onEnter: (...args: Parameters<NonNullable<UseBlokConfig['onEnter']>>): boolean | void =>
274
- configRef.current.onEnter?.(...args),
275
- };
276
-
277
- // Only attach onSubmit when the consumer opted in: its mere presence makes
278
- // Enter serialize-and-submit instead of splitting a block, so an absent prop
279
- // must stay absent. The wrapper reads through the ref so the latest callback
280
- // is always used without recreating the editor.
281
- if (currentConfig.onSubmit) {
282
- blokConfig.onSubmit = (...args: Parameters<NonNullable<UseBlokConfig['onSubmit']>>): void => {
283
- configRef.current.onSubmit?.(...args);
284
- };
285
- }
286
-
287
- // Only attach onSave when the consumer opted in: its mere presence makes the
288
- // core serialize on every change batch, so an absent prop must stay absent.
289
- // The wrapper reads through the ref so the latest callback is always used.
290
- if (currentConfig.onSave) {
291
- blokConfig.onSave = (...args: Parameters<NonNullable<UseBlokConfig['onSave']>>): void => {
293
+ onSave: (...args: Parameters<NonNullable<UseBlokConfig['onSave']>>): void => {
292
294
  // Record the editor's own serialized output as the rendered baseline so a
293
295
  // controlled consumer echoing it straight back into `data` is a no-op —
294
296
  // no redundant render(), no caret reset, no round-trip recursion.
295
297
  lastRenderedDataRef.current = args[0];
296
298
  echoWindowRef.current.record(args[0]);
297
299
  configRef.current.onSave?.(...args);
298
- };
299
- }
300
-
301
- // onBeforeRender / onAfterRender are opt-in (absent prop must stay absent so
302
- // the core skips them). When present, route through the ref so the latest
303
- // callback is always used without recreating the editor.
304
- if (currentConfig.onBeforeRender) {
305
- blokConfig.onBeforeRender = (
300
+ },
301
+ // Forward the return value: it is the "handled" signal the core acts on.
302
+ onEnter: (...args: Parameters<NonNullable<UseBlokConfig['onEnter']>>): boolean | void =>
303
+ configRef.current.onEnter?.(...args),
304
+ onSubmit: (...args: Parameters<NonNullable<UseBlokConfig['onSubmit']>>): void => {
305
+ configRef.current.onSubmit?.(...args);
306
+ },
307
+ onBeforeRender: (
306
308
  ...args: Parameters<NonNullable<UseBlokConfig['onBeforeRender']>>
307
309
  ): ReturnType<NonNullable<UseBlokConfig['onBeforeRender']>> =>
308
- configRef.current.onBeforeRender?.(...args) ?? args[0];
309
- }
310
-
311
- if (currentConfig.onAfterRender) {
312
- blokConfig.onAfterRender = (...args: Parameters<NonNullable<UseBlokConfig['onAfterRender']>>): void => {
310
+ configRef.current.onBeforeRender?.(...args) ?? args[0],
311
+ onAfterRender: (...args: Parameters<NonNullable<UseBlokConfig['onAfterRender']>>): void => {
313
312
  configRef.current.onAfterRender?.(...args);
314
- };
315
- }
313
+ },
314
+ };
315
+
316
+ handlerWrappersRef.current = handlerWrappers;
317
+
318
+ // Handler PRESENCE is load-bearing in core — an `onSubmit` turns Enter into
319
+ // serialize-and-submit, an `onSave` arms the change pipeline — so an absent
320
+ // prop must leave the key absent. Attach only the wrappers whose prop the
321
+ // consumer actually passed, and record what was installed so the effect
322
+ // below can push later presence flips instead of recreating the editor.
323
+ const handlerPresence: Record<keyof LiveHandlers, boolean> = {
324
+ onChange: currentConfig.onChange !== undefined,
325
+ onSave: currentConfig.onSave !== undefined,
326
+ onEnter: currentConfig.onEnter !== undefined,
327
+ onSubmit: currentConfig.onSubmit !== undefined,
328
+ onBeforeRender: currentConfig.onBeforeRender !== undefined,
329
+ onAfterRender: currentConfig.onAfterRender !== undefined,
330
+ };
331
+
332
+ appliedHandlerPresenceRef.current = handlerPresence;
333
+
334
+ const blokConfig = {
335
+ ...currentConfig,
336
+ ...(liveTools === undefined ? {} : { tools: liveTools as UseBlokConfig['tools'] }),
337
+ holder,
338
+ onReady: (...args: Parameters<NonNullable<UseBlokConfig['onReady']>>): void => {
339
+ configRef.current.onReady?.(...args);
340
+ },
341
+ ...(handlerPresence.onChange ? { onChange: handlerWrappers.onChange } : {}),
342
+ ...(handlerPresence.onSave ? { onSave: handlerWrappers.onSave } : {}),
343
+ ...(handlerPresence.onEnter ? { onEnter: handlerWrappers.onEnter } : {}),
344
+ ...(handlerPresence.onSubmit ? { onSubmit: handlerWrappers.onSubmit } : {}),
345
+ ...(handlerPresence.onBeforeRender ? { onBeforeRender: handlerWrappers.onBeforeRender } : {}),
346
+ ...(handlerPresence.onAfterRender ? { onAfterRender: handlerWrappers.onAfterRender } : {}),
347
+ };
316
348
 
317
349
  const blok = new BlokRuntime(blokConfig);
318
350
  state.editor = blok;
@@ -321,6 +353,19 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
321
353
  constructedDataRef.current = currentConfig.data;
322
354
  setHolder(blok, holder);
323
355
  setRegistry(blok, registry);
356
+ // Imperative-content channel: `useBlokHandle().clear()` / `.render()` change
357
+ // the content OUT OF BAND of the reactive `data` effect below. They report
358
+ // the new content here so the dedupe baseline and the emitted-echo window
359
+ // (both read by that effect) describe the editor as it actually is —
360
+ // otherwise restoring a document the editor itself emitted is dismissed as
361
+ // an echo and never rendered.
362
+ setContentBaseline(blok, {
363
+ markRendered: (content): void => {
364
+ echoWindowRef.current.clear();
365
+ lastRenderedDataRef.current = content;
366
+ pendingDataRef.current = undefined;
367
+ },
368
+ });
324
369
 
325
370
  void blok.isReady
326
371
  .then(() => {
@@ -333,6 +378,7 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
333
378
  if (state.editor === blok && !state.isDestroyed) {
334
379
  removeHolder(blok);
335
380
  removeRegistry(blok);
381
+ removeContentBaseline(blok);
336
382
  try {
337
383
  blok.destroy();
338
384
  } catch {
@@ -445,6 +491,74 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
445
491
  }
446
492
  });
447
493
 
494
+ // Reactive: onChange
495
+ // Reactive: onSave
496
+ // Reactive: onEnter
497
+ // Reactive: onSubmit
498
+ // Reactive: onBeforeRender
499
+ // Reactive: onAfterRender
500
+ //
501
+ // Callback PRESENCE was frozen at construction, and in core the presence of a
502
+ // handler IS the semantics: an `onSubmit` makes Enter serialize-and-submit
503
+ // instead of splitting the block, an `onSave` arms the whole change-observation
504
+ // pipeline. A host whose "Enter sends" toggle (or `onSave` subscription) lives
505
+ // in React state therefore had to bump `deps` and destroy/rebuild the editor,
506
+ // losing caret and undo history. Instead, diff each handler's presence against
507
+ // what is installed and push genuine flips through the runtime `handlers.set`
508
+ // API — passing `undefined` for a prop that disappeared, which is what makes
509
+ // the change reversible rather than a one-way latch. Identity never changes
510
+ // (the wrappers forward through `configRef`), so only presence is diffed.
511
+ // Applies once the editor is ready — module APIs attach at isReady — and the
512
+ // ready transition re-renders, so a flip made earlier is not lost.
513
+ useEffect(() => {
514
+ const state = stateRef.current;
515
+ const wrappers = handlerWrappersRef.current;
516
+
517
+ if (state.editor === null || state.isDestroyed || !state.isEditorReady || wrappers === null) {
518
+ return;
519
+ }
520
+
521
+ const latest = configRef.current;
522
+ const applied = appliedHandlerPresenceRef.current;
523
+ const diff: LiveHandlers = {};
524
+ const changed: { value: boolean } = { value: false };
525
+
526
+ const sync = <K extends keyof LiveHandlers>(key: K, write: (value: LiveHandlers[K]) => void): void => {
527
+ const present = latest[key] !== undefined;
528
+
529
+ if (present === applied[key]) {
530
+ return;
531
+ }
532
+
533
+ applied[key] = present;
534
+ changed.value = true;
535
+ write(present ? wrappers[key] : undefined);
536
+ };
537
+
538
+ sync('onChange', (value) => {
539
+ diff.onChange = value;
540
+ });
541
+ sync('onSave', (value) => {
542
+ diff.onSave = value;
543
+ });
544
+ sync('onEnter', (value) => {
545
+ diff.onEnter = value;
546
+ });
547
+ sync('onSubmit', (value) => {
548
+ diff.onSubmit = value;
549
+ });
550
+ sync('onBeforeRender', (value) => {
551
+ diff.onBeforeRender = value;
552
+ });
553
+ sync('onAfterRender', (value) => {
554
+ diff.onAfterRender = value;
555
+ });
556
+
557
+ if (changed.value) {
558
+ state.editor.handlers.set(diff);
559
+ }
560
+ });
561
+
448
562
  // Reactive: readOnly (object form normalized so the effect deps are stable booleans).
449
563
  // The object form additionally syncs `hideControls` through the options
450
564
  // parameter of `readOnly.set` — a plain boolean prop keeps the historical
@@ -597,11 +711,12 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
597
711
  //
598
712
  // `data` seeds the editor at construction. Afterwards, changing the prop to
599
713
  // new *content* re-renders the editor via the public render() API — no
600
- // recreation. Updates are deep-equal–deduped against the editor's current
601
- // content baseline (`lastRenderedDataRef`, declared above and also updated by
602
- // the `onSave` wrapper) so a new reference with the same content — including
603
- // the editor's own serialized output echoed back — is a no-op and won't
604
- // clobber the caret. Renders are serialized so rapid changes can't overlap.
714
+ // recreation. Updates are content-deduped against the editor's current
715
+ // baseline (`lastRenderedDataRef`, declared above and also updated by the
716
+ // `onSave` wrapper and the imperative content-baseline channel) so a new
717
+ // reference with the same content — including the editor's own serialized
718
+ // output echoed back, however the host reshaped it in transit — is a no-op and
719
+ // won't clobber the caret. Renders are serialized so rapid changes can't overlap.
605
720
  const { data } = config;
606
721
  useEffect(() => {
607
722
  if (editor === null || data === undefined) {
@@ -644,7 +759,20 @@ export function useBlok(configInput: UseBlokConfig, deps?: DependencyList): Blok
644
759
  // Skip when the editor already reflects this content (last SUCCESSFUL render)
645
760
  // OR a render of the same content is already queued/in-flight — re-rendering
646
761
  // identical content would needlessly reset the caret.
647
- if (deepEqual(data, lastRenderedDataRef.current) || deepEqual(data, pendingDataRef.current)) {
762
+ //
763
+ // Compared with the SAME structural lens the echo window uses
764
+ // (`equalsOutputData`, not a raw deep-equal): a host that persists the
765
+ // editor's own document and hands back a stripped copy — fresh `time`,
766
+ // dropped ids, no `lastEditedAt` stamp — is still echoing content the editor
767
+ // already shows. `undefined` means "nothing recorded yet", which is NOT an
768
+ // empty document, so it must never match.
769
+ const baseline = lastRenderedDataRef.current;
770
+ const inFlight = pendingDataRef.current;
771
+
772
+ if (
773
+ (baseline !== undefined && equalsOutputData(data, baseline)) ||
774
+ (inFlight !== undefined && equalsOutputData(data, inFlight))
775
+ ) {
648
776
  return;
649
777
  }
650
778
 
@@ -701,6 +829,7 @@ function deferDestroy(
701
829
  if (state.editor !== null) {
702
830
  removeHolder(state.editor);
703
831
  removeRegistry(state.editor);
832
+ removeContentBaseline(state.editor);
704
833
  try {
705
834
  state.editor.destroy();
706
835
  } catch {
@@ -1,4 +1,6 @@
1
1
  import { useRef, useMemo, type RefCallback } from 'react';
2
+ import { EMPTY_OUTPUT_DATA } from '@bloklabs/core/adapters';
3
+ import { getContentBaseline } from './content-baseline-map';
2
4
  import type { Blok, OutputData, LooseOutputData } from '@/types';
3
5
 
4
6
  /**
@@ -32,12 +34,22 @@ export interface BlokEditorHandle {
32
34
  * @param atEnd - place the caret at the end of the focused block
33
35
  */
34
36
  focus(atEnd?: boolean): boolean;
35
- /** Clear all content. Resolves immediately (no-op) before ready. */
37
+ /**
38
+ * Clear all content. Resolves immediately (no-op) before ready.
39
+ *
40
+ * Safe to mix with a controlled `data` prop: the adapter's content baseline is
41
+ * updated once the clear lands, so setting `data` back to the document the
42
+ * editor itself emitted re-renders it instead of being dismissed as an echo.
43
+ */
36
44
  clear(): Promise<void>;
37
45
  /** Serialize the current content, or resolve to `null` before ready. */
38
46
  save(): Promise<OutputData | null>;
39
47
  /**
40
48
  * Render new content in place. Resolves immediately (no-op) before ready.
49
+ *
50
+ * Safe to mix with a controlled `data` prop: the rendered document becomes the
51
+ * adapter's content baseline, so a later `data` change back to the previous
52
+ * document still re-renders.
41
53
  * @param data - the document to render
42
54
  */
43
55
  render(data: OutputData | LooseOutputData): Promise<void>;
@@ -81,11 +93,35 @@ export function useBlokHandle(): BlokEditorHandle {
81
93
  return instanceRef.current !== null;
82
94
  },
83
95
  focus: (atEnd?: boolean): boolean => instanceRef.current?.focus(atEnd) ?? false,
84
- clear: (): Promise<void> => instanceRef.current?.clear() ?? Promise.resolve(),
96
+ // clear/render change the content OUT OF BAND of the controlled `data`
97
+ // effect, so they report the result to the adapter's content baseline (see
98
+ // content-baseline-map). Reported only once the call RESOLVES: a failed one
99
+ // leaves the editor on its previous content, and a baseline pointing at
100
+ // content the editor never showed would dedupe away the correcting update.
101
+ clear: (): Promise<void> => {
102
+ const instance = instanceRef.current;
103
+
104
+ if (instance === null) {
105
+ return Promise.resolve();
106
+ }
107
+
108
+ return instance.clear().then(() => {
109
+ getContentBaseline(instance)?.markRendered(EMPTY_OUTPUT_DATA);
110
+ });
111
+ },
85
112
  save: (): Promise<OutputData | null> =>
86
113
  instanceRef.current !== null ? instanceRef.current.save() : Promise.resolve(null),
87
- render: (data: OutputData | LooseOutputData): Promise<void> =>
88
- instanceRef.current?.render(data) ?? Promise.resolve(),
114
+ render: (data: OutputData | LooseOutputData): Promise<void> => {
115
+ const instance = instanceRef.current;
116
+
117
+ if (instance === null) {
118
+ return Promise.resolve();
119
+ }
120
+
121
+ return instance.render(data).then(() => {
122
+ getContentBaseline(instance)?.markRendered(data);
123
+ });
124
+ },
89
125
  setReadOnly: (state?: boolean): Promise<boolean> => {
90
126
  const instance = instanceRef.current;
91
127
 
package/types/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { BlokConfig, BlockToolData, OutputBlockData, OutputData, LooseOutputData } from '@bloklabs/core';
2
2
  import type { BlocksToHtmlOptions } from '@bloklabs/core/view';
3
- import type { BlockAPI, BlockToolConstructable, ToolboxConfigEntry } from '@bloklabs/core';
3
+ import type { BlockAPI, BlockOrigin, BlockToolConstructable, ToolboxConfigEntry } from '@bloklabs/core';
4
4
  import type { InlineToolConstructable, SanitizerConfig } from '@bloklabs/core';
5
5
  import type { API, MarkSnapshot, MarkSpec } from '@bloklabs/core';
6
6
  import type { Blok, EditorWidth, BlockRenderedPayload, BlocksRenderedPayload } from '@bloklabs/core';
@@ -35,6 +35,13 @@ export { getDirection, normalizeLocale } from '@bloklabs/core/locales';
35
35
  * forwarded — it only affects the initial locale resolution.
36
36
  * - `data` — re-renders via `editor.render(value)` when the content changes
37
37
  * (deep-equal–deduped and serialized; seeds the initial content at creation)
38
+ * - `onChange`, `onSave`, `onEnter`, `onSubmit`, `onBeforeRender`,
39
+ * `onAfterRender` — calls `editor.handlers.set({ ... })` when a callback
40
+ * APPEARS or DISAPPEARS. Callback presence is load-bearing in core (an
41
+ * `onSubmit` turns Enter into serialize-and-submit; an `onSave` arms the
42
+ * change pipeline), so dropping the prop UNSETS the handler rather than
43
+ * leaving a stale one behind. Callback IDENTITY is always live — the adapter
44
+ * forwards to the latest render, so you never need to memoize them.
38
45
  *
39
46
  * All other config is consumed once at editor creation.
40
47
  */
@@ -162,11 +169,21 @@ export interface BlokEditorHandle {
162
169
  readonly isReady: boolean;
163
170
  /** Focus the editor. Returns whether focus landed; `false` (no-op) before ready. */
164
171
  focus(atEnd?: boolean): boolean;
165
- /** Clear all content. Resolves immediately (no-op) before ready. */
172
+ /**
173
+ * Clear all content. Resolves immediately (no-op) before ready. Safe to mix
174
+ * with a controlled `data` prop: the adapter's content baseline is updated
175
+ * once the clear lands, so setting `data` back to a document the editor itself
176
+ * emitted re-renders it instead of being dismissed as an echo.
177
+ */
166
178
  clear(): Promise<void>;
167
179
  /** Serialize the current content, or resolve to `null` before ready. */
168
180
  save(): Promise<OutputData | null>;
169
- /** Render new content in place. Resolves immediately (no-op) before ready. */
181
+ /**
182
+ * Render new content in place. Resolves immediately (no-op) before ready. Safe
183
+ * to mix with a controlled `data` prop: the rendered document becomes the
184
+ * adapter's content baseline, so a later `data` change back to the previous
185
+ * document still re-renders.
186
+ */
170
187
  render(data: OutputData | LooseOutputData): Promise<void>;
171
188
  /** Set or toggle read-only mode. Resolves to the resulting state; `false` (no-op) before ready. */
172
189
  setReadOnly(state?: boolean): Promise<boolean>;
@@ -205,6 +222,17 @@ export declare function BlokProvider(props: {
205
222
  /** Reads the app-wide Blok defaults from the nearest `BlokProvider` (or `{}`). */
206
223
  export declare function useBlokDefaults(): Partial<UseBlokConfig>;
207
224
 
225
+ /**
226
+ * The live Blok instance a component is rendered inside, or null before it
227
+ * exists. Inside a `createReactBlock` component this is the block's OWN editor —
228
+ * `createPortal` preserves the component tree's context — so
229
+ * `useBlocks(useBlokInstance())` works with no host plumbing, and makes a
230
+ * container block reactive to its own child tree (`useBlocks` re-renders on the
231
+ * editor's `block changed` event). Outside a `BlokContent`/`BlokEditor` subtree
232
+ * it returns null.
233
+ */
234
+ export declare function useBlokInstance(): Blok | null;
235
+
208
236
  /**
209
237
  * A plain, serializable view of one block in the tree.
210
238
  *
@@ -671,6 +699,14 @@ export interface ReactBlockRenderProps<Data, Config = Record<string, unknown>> {
671
699
  commit: (patch: Partial<Data>) => void;
672
700
  /** This block's per-block API. */
673
701
  block: BlockAPI;
702
+ /**
703
+ * The EDITOR-level API this block belongs to (`api.blocks`, `api.caret`,
704
+ * `api.toolbar`…) — the same object a vanilla tool receives in its
705
+ * constructor. For the reactive, id/parentId-relative view of the tree (and
706
+ * to re-render when your own children change), pair `useBlocks` with
707
+ * `useBlokInstance()` instead; the api handle itself is not reactive.
708
+ */
709
+ api: API;
674
710
  /**
675
711
  * Read-only flag. Toggled IN PLACE by core's read-only switch (no remount,
676
712
  * ephemeral state survives). Honor it — a block that ignores it stays
@@ -686,9 +722,41 @@ export interface ReactBlockRenderProps<Data, Config = Record<string, unknown>> {
686
722
  */
687
723
  config: Readonly<Partial<Config>>;
688
724
  /** Engine-owned child slot — render `<BlockChildren />` for a container block. */
689
- BlockChildren: React.ComponentType;
725
+ BlockChildren: React.ComponentType<BlockChildrenProps>;
726
+ }
727
+
728
+ /**
729
+ * One child's decoration: attribute name -> value. `null`/`undefined` removes
730
+ * the attribute; a boolean or number is stringified (so `false` writes
731
+ * `data-active="false"`, which CSS can select, rather than dropping the hook).
732
+ */
733
+ export type ChildAttributes = Record<string, string | number | boolean | null | undefined>;
734
+
735
+ /** Props of the engine-owned {@link ReactBlockRenderProps.BlockChildren} slot. */
736
+ export interface BlockChildrenProps {
737
+ /**
738
+ * Per-child decoration, applied to each child's HOLDER after the holders are
739
+ * mounted. Named hooks (`data-step-index`, `data-active`…) replace positional
740
+ * `:nth-child()` CSS over Blok's holders, which breaks the moment a child is
741
+ * inserted, removed or reordered. The holders stay DIRECT children of the slot
742
+ * (core requires that for hierarchy reparenting and caret navigation), so
743
+ * decoration is attributes, never wrapper elements. Attributes the callback
744
+ * stops producing are removed on the next pass.
745
+ *
746
+ * Writing on a child's holder is inert by design; the guarantee stops at the
747
+ * holder and its `[data-blok-element-content]` wrapper — writing AT or BELOW a
748
+ * child's tool root DOES score as that child's edit.
749
+ */
750
+ childAttributes?: (child: BlockAPI, index: number) => ChildAttributes;
690
751
  }
691
752
 
753
+ /**
754
+ * Every STATIC member of core's block-tool contract a React block may declare
755
+ * for itself. `toolbox` and `isReadOnlySupported` are excluded — the factory
756
+ * owns them.
757
+ */
758
+ export type BlockToolStatics = Omit<BlockToolConstructable, 'toolbox' | 'isReadOnlySupported'>;
759
+
692
760
  /**
693
761
  * Props handed to a React block's `viewComponent` (the read-only renderer):
694
762
  * the entry props minus `commit` (no write path) and `readOnly` (always true).
@@ -739,11 +807,92 @@ export interface CreateReactBlockSpec<
739
807
  * omit it to keep the single-component in-place toggle.
740
808
  */
741
809
  viewComponent?: React.ComponentType<ReactBlockViewProps<Data, Config>>;
810
+ /**
811
+ * Static members of core's tool contract, forwarded verbatim onto the
812
+ * generated tool class (`ownsChildren`, `keepsChildrenOnEnter`,
813
+ * `conversionConfig`, `pasteConfig`, `sanitize`, `shortcut`, `upgradeData`…)
814
+ * — the single channel for everything core reads off the CLASS rather than
815
+ * the instance.
816
+ *
817
+ * `keepsChildrenOnEnter` is the per-tool Enter policy: declare it and Enter on
818
+ * this container's empty LAST child creates the new line INSIDE it instead of
819
+ * escaping to the container's parent (Blok's default, which is Notion's
820
+ * callout behaviour). It is policy the DOM cannot express — a callout renders
821
+ * the same nested-blocks slot as a column yet wants the escape — so a layout
822
+ * container (a card, a `steps` block) had to hijack the editor-global
823
+ * `config.onEnter` before it existed.
824
+ */
825
+ statics?: BlockToolStatics;
826
+ /**
827
+ * The element the +/drag toolbar should vertically center on — core's
828
+ * `getToolbarAnchorElement` hook, resolved against this block's host element
829
+ * on every call. A container block whose own chrome is not editable needs it:
830
+ * with no anchor, core centers the toolbar on the first `[contenteditable]`
831
+ * under the host, which for a container is its FIRST CHILD BLOCK. Return
832
+ * `null`/`undefined` (or omit) to keep core's default.
833
+ */
834
+ getToolbarAnchorElement?: (host: HTMLElement, block: BlockAPI) => HTMLElement | null | undefined;
742
835
  onRendered?: (block: BlockAPI) => void;
836
+ /**
837
+ * Fired ONCE per block instance, after the portal's FIRST commit — the first
838
+ * moment this block's rendered DOM exists (and, for a container, the moment
839
+ * `<BlockChildren />` has adopted the child holders). `onRendered` cannot mean
840
+ * that: core calls `rendered()` in the same tick as `render()`, before React
841
+ * has drawn anything.
842
+ *
843
+ * It is also the create-vs-restore signal: `context.origin` says whether the
844
+ * author just made this block (`user`/`api`/`convert`) or the document is
845
+ * being re-materialised (`load`/`replay`/`paste`), so a container can seed its
846
+ * default children here exactly once.
847
+ *
848
+ * To observe OTHER blocks settling — e.g. before placing the caret in a child
849
+ * you just inserted — listen for the `block:childrenMounted` editor event
850
+ * instead: `api.events.on('block:childrenMounted', ({ blockId, childIds }) => …)`.
851
+ */
852
+ onMounted?: (block: BlockAPI, context: ReactBlockMountedContext) => void;
853
+ /**
854
+ * The SEEDING hook: `onMounted`, narrowed to a genuine creation. Fired ONCE
855
+ * per block instance, after the portal's first commit, and only when the
856
+ * author just made this block (`origin` of `user`, `api` or `convert`) —
857
+ * never for a `load`/`replay`/`paste` restore, and never for the off-tree
858
+ * `probe` instance core builds to read a tool's default data.
859
+ *
860
+ * Prefer it over hand-testing `context.origin` in `onMounted`: the intuitive
861
+ * `origin === 'user'` check is wrong — it drops `api.blocks.insert(...)` and
862
+ * turn-into, so the container comes up empty for every path except a
863
+ * keystroke.
864
+ * @example
865
+ * ```tsx
866
+ * onCreated: (block, { api }) => {
867
+ * if (block.getChildren().length === 0) {
868
+ * api.blocks.insertInsideParent(block.id);
869
+ * }
870
+ * }
871
+ * ```
872
+ */
873
+ onCreated?: (block: BlockAPI, context: ReactBlockMountedContext) => void;
743
874
  onMoved?: (block: BlockAPI) => void;
744
875
  onRemoved?: (block: BlockAPI) => void;
745
876
  }
746
877
 
878
+ /**
879
+ * Second argument of {@link CreateReactBlockSpec.onMounted} and
880
+ * {@link CreateReactBlockSpec.onCreated}.
881
+ */
882
+ export interface ReactBlockMountedContext {
883
+ /**
884
+ * Why this block instance was constructed. CREATION origins (`user`, `api`,
885
+ * `convert`) mean the author just made it, so seeding default children is
886
+ * correct; RESTORE origins (`load`, `replay`, `paste`) mean the document
887
+ * already says what the children are. `probe` is an off-tree instance built
888
+ * only to read a tool's default data and must not touch the block tree.
889
+ * Defaults to `'api'`.
890
+ */
891
+ origin: BlockOrigin;
892
+ /** The editor-level API (`api.blocks`, `api.caret`, `api.events`…). */
893
+ api: API;
894
+ }
895
+
747
896
  /**
748
897
  * Author a first-party React block: returns a `BlockToolConstructable`
749
898
  * registered exactly like a vanilla tool (`tools: { type: createReactBlock(...) }`).
@@ -940,7 +1089,13 @@ export interface BlockPortalEntry {
940
1089
  */
941
1090
  export interface BlockPortalRegistry {
942
1091
  register(id: string, entry: BlockPortalEntry): void;
943
- unregister(id: string): void;
1092
+ /**
1093
+ * Remove the entry for `id`. Pass the caller's own `hostEl` to make the
1094
+ * removal ownership-checked — the entry is deleted only when it is still the
1095
+ * one that host registered, so a superseded owner's late teardown can never
1096
+ * clobber a same-id re-register.
1097
+ */
1098
+ unregister(id: string, hostEl?: HTMLElement): void;
944
1099
  setProps(id: string, props: Record<string, unknown>): void;
945
1100
  /**
946
1101
  * Replace the `config` prop of every entry registered under `toolName`, and