@forevka/wordcanvas 0.7.1 → 0.7.3

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.
Files changed (64) hide show
  1. package/dist-lib/assets/{worker-D8rUOKfg.js → worker-CqU4Popa.js} +10903 -10736
  2. package/dist-lib/assets/worker-Cw5cYW5z.js +2418 -0
  3. package/dist-lib/blockFactory-DDCRWDnA.js +91 -0
  4. package/dist-lib/browser-BUrsjziD.js +612 -0
  5. package/dist-lib/builder.js +13 -13
  6. package/dist-lib/editorApp-CB_ZArIF.js +9566 -0
  7. package/dist-lib/export.js +4507 -4456
  8. package/dist-lib/generate-toc.js +7 -7
  9. package/dist-lib/import.js +2 -2
  10. package/dist-lib/lists-DpmgbDyt.js +110 -0
  11. package/dist-lib/measure.js +2 -2
  12. package/dist-lib/{measureHost-CMiJKdlk.js → measureHost-BlukHGwx.js} +1 -1
  13. package/dist-lib/metrics-bvGO8KDK.js +96 -0
  14. package/dist-lib/paintStyle-vT6jtWbH.js +74 -0
  15. package/dist-lib/pipeline-UryP-u7Z.js +1850 -0
  16. package/dist-lib/recalc-docx.js +2 -2
  17. package/dist-lib/recalcToc-BfXlXZik.js +3985 -0
  18. package/dist-lib/styleProps-DhqDEfvc.js +149 -0
  19. package/dist-lib/stylesheet-LTBEGkvG.js +146 -0
  20. package/dist-lib/toc-D09f4PrI.js +939 -0
  21. package/dist-lib/{webmcp-6WxRNKCc.js → webmcp-K6UZgQJ0.js} +60 -68
  22. package/dist-lib/wordcanvas-DBx2ana_.js +734 -0
  23. package/dist-lib/wordcanvas.js +4 -2
  24. package/dist-node/{chunk-CDN6WU2P.js → chunk-373K2XRY.js} +40 -22
  25. package/dist-node/chunk-373K2XRY.js.map +7 -0
  26. package/dist-node/{chunk-22JHAERT.js → chunk-3PUV3BG6.js} +235 -39
  27. package/dist-node/chunk-3PUV3BG6.js.map +7 -0
  28. package/dist-node/{chunk-4ICCHZX4.js → chunk-5RX3IHWE.js} +39 -3
  29. package/dist-node/chunk-5RX3IHWE.js.map +7 -0
  30. package/dist-node/{chunk-4PDVRJF6.js → chunk-EMY2P524.js} +32 -2
  31. package/dist-node/chunk-EMY2P524.js.map +7 -0
  32. package/dist-node/{chunk-BRAPV465.js → chunk-HWQKGDS2.js} +2 -2
  33. package/dist-node/{chunk-6AJJMHKY.js → chunk-KJV7GJ3H.js} +1 -1
  34. package/dist-node/chunk-KJV7GJ3H.js.map +7 -0
  35. package/dist-node/{chunk-RNB336F3.js → chunk-ZLLSXOO2.js} +92 -14
  36. package/dist-node/{chunk-RNB336F3.js.map → chunk-ZLLSXOO2.js.map} +2 -2
  37. package/dist-node/{engine-5FGDJD2H.js → engine-LLA6PCS5.js} +4 -4
  38. package/dist-node/export.js +89 -24
  39. package/dist-node/export.js.map +2 -2
  40. package/dist-node/generate-toc.js +6 -6
  41. package/dist-node/import.js +3 -3
  42. package/dist-node/measure.js +2 -2
  43. package/dist-node/recalc-docx.js +4 -4
  44. package/package.json +1 -1
  45. package/types/model.d.ts +46 -0
  46. package/types/wordcanvas.d.ts +493 -1
  47. package/dist-lib/assets/worker-wXuuyshQ.js +0 -2258
  48. package/dist-lib/blockFactory-CUI4Rqiq.js +0 -186
  49. package/dist-lib/browser-DkKxreJM.js +0 -1305
  50. package/dist-lib/editorApp-DLXMmtu5.js +0 -8023
  51. package/dist-lib/lists-Sc5I40JO.js +0 -110
  52. package/dist-lib/metrics-CkAo4zh8.js +0 -143
  53. package/dist-lib/pipeline-CbxmCE84.js +0 -1705
  54. package/dist-lib/recalcToc-g1hFKSNs.js +0 -3935
  55. package/dist-lib/styleProps-Db5pEkG8.js +0 -127
  56. package/dist-lib/toc-B6VpkJge.js +0 -213
  57. package/dist-lib/wordcanvas-CFqpDK0k.js +0 -125
  58. package/dist-node/chunk-22JHAERT.js.map +0 -7
  59. package/dist-node/chunk-4ICCHZX4.js.map +0 -7
  60. package/dist-node/chunk-4PDVRJF6.js.map +0 -7
  61. package/dist-node/chunk-6AJJMHKY.js.map +0 -7
  62. package/dist-node/chunk-CDN6WU2P.js.map +0 -7
  63. /package/dist-node/{chunk-BRAPV465.js.map → chunk-HWQKGDS2.js.map} +0 -0
  64. /package/dist-node/{engine-5FGDJD2H.js.map → engine-LLA6PCS5.js.map} +0 -0
@@ -2,7 +2,7 @@
2
2
  // Hand-written so the published types stay small, self-contained, and stable
3
3
  // regardless of internal refactors — no dependency on internal workspace types.
4
4
 
5
- import type { Document } from "./model";
5
+ import type { Block, CharStyle, DocPosition, Document, ParaStyle, Run } from "./model";
6
6
 
7
7
  export type { Document } from "./model";
8
8
 
@@ -19,6 +19,262 @@ export interface Participant {
19
19
  user?: UserInfo;
20
20
  }
21
21
 
22
+ /** Editor mode: "edit" (raw editing), "suggest" (edits become tracked changes),
23
+ * or "view" (read-only). The three are mutually exclusive. */
24
+ export type EditMode = "edit" | "suggest" | "view";
25
+
26
+ /** A selection/caret in the document (offsets are UTF-16 code units into a
27
+ * block's concatenated run text). `anchor === focus` is a collapsed caret. */
28
+ export interface DocSelection {
29
+ anchor: DocPosition;
30
+ focus: DocPosition;
31
+ /** Preserved X column for Up/Down caret movement; usually ignorable. */
32
+ goalX?: number;
33
+ }
34
+
35
+ // ===== Review layer (track changes + comments) =============================
36
+ // An attributed, anchored overlay on top of the core document. `getReview()`
37
+ // returns a snapshot; the `reviewChanged` event carries the full layer.
38
+
39
+ /** Rich-text comment body — the same style-homogeneous run list the document
40
+ * uses (so a comment composes/renders like any other content). */
41
+ export type Fragment = Run[];
42
+
43
+ /** A range into the live document; `start === end` is a point (caret) anchor. */
44
+ export interface ReviewAnchor {
45
+ start: DocPosition;
46
+ end: DocPosition;
47
+ }
48
+
49
+ export type SuggestionKind = "insert" | "delete" | "format" | "structural";
50
+
51
+ /** A tracked structural edit (split/merge/block/table op). `op`/`inverse` are the
52
+ * internal core ops captured at intercept time — opaque on the public surface
53
+ * (not part of the stable contract); `blockId` is the block the record hangs on. */
54
+ export interface StructuralChange {
55
+ op: unknown;
56
+ inverse: unknown;
57
+ blockId: string;
58
+ }
59
+
60
+ /** One tracked change. Text-bearing kinds (insert/delete/format) cover live text;
61
+ * `structural` records carry a degenerate point anchor + the applied op. */
62
+ export interface Suggestion {
63
+ /** Globally unique id. */
64
+ id: string;
65
+ kind: SuggestionKind;
66
+ /** Covers text present in the live document. */
67
+ anchor: ReviewAnchor;
68
+ /** Attribution (the embedder owns identity; the layer just presents it). */
69
+ author: UserInfo;
70
+ /** Wall-clock ms; display/sort only. */
71
+ createdAt: number;
72
+ /** format only: the applied char-style patch + its inverse (for reject). */
73
+ patch?: Partial<CharStyle>;
74
+ inverse?: Partial<CharStyle>;
75
+ /** structural only: the applied core op, its inverse, and the host block. */
76
+ structural?: StructuralChange;
77
+ /** Optional grouping so related records accept/reject as a unit. */
78
+ groupId?: string;
79
+ }
80
+
81
+ export interface Comment {
82
+ id: string;
83
+ author: UserInfo;
84
+ body: Fragment;
85
+ createdAt: number;
86
+ editedAt?: number;
87
+ /** Resolved identities of users @-mentioned in this comment. */
88
+ mentions?: UserInfo[];
89
+ }
90
+
91
+ export type ThreadStatus = "open" | "resolved";
92
+
93
+ export interface CommentThread {
94
+ id: string;
95
+ /** Highlighted span; `start === end` ⇒ point comment. */
96
+ anchor: ReviewAnchor;
97
+ status: ThreadStatus;
98
+ /** Ordered; `comments[0]` is the root, the rest are replies. */
99
+ comments: Comment[];
100
+ }
101
+
102
+ /** Snapshot of the review overlay (suggestions + comment threads). */
103
+ export interface ReviewLayer {
104
+ docId: string;
105
+ /** Core document version these anchors were last rebased to. */
106
+ baseVersion: number;
107
+ suggestions: Suggestion[];
108
+ threads: CommentThread[];
109
+ }
110
+
111
+ // ===== Theming & behavior (see WordCanvasOptions.theme / .behavior) =========
112
+
113
+ /** Ruler band styling (the strip the horizontal + vertical rulers paint). */
114
+ export interface RulerTheme {
115
+ /** Band fill behind the ticks. */
116
+ bg?: string;
117
+ /** The lighter "content area" portion of the band (within the margins). */
118
+ content?: string;
119
+ /** Tick + boundary line color. */
120
+ line?: string;
121
+ /** Number label color. */
122
+ label?: string;
123
+ /** CSS font for the number labels. */
124
+ font?: string;
125
+ }
126
+
127
+ /** Editor color theme — every field optional; omit to keep the built-in value.
128
+ * Affects the on-screen editor only (exported PDFs keep the built-in look). */
129
+ export interface EditorTheme {
130
+ /** Gray gutter behind the pages (the scroll area + ruler troughs). */
131
+ canvasBackground?: string;
132
+ /** Native table gridlines when a cell carries no explicit border. */
133
+ grid?: string;
134
+ /** Drawing-grid overlay mesh (the dotted snap grid). */
135
+ gridMesh?: string;
136
+ /** External hyperlink color. */
137
+ externalLink?: string;
138
+ /** UI accent (band-edit boundary and similar chrome affordances). */
139
+ accent?: string;
140
+ /** Footnote separator rule. */
141
+ footnoteRule?: string;
142
+ /** Non-printing formatting marks (space dots, tab arrows, pilcrows). */
143
+ formattingMark?: string;
144
+ /** TOC dot-leader color. */
145
+ tocLeader?: string;
146
+ /** Placeholder fill for an image whose bitmap hasn't loaded. */
147
+ imagePlaceholder?: string;
148
+ /** Rule between newspaper columns. */
149
+ columnSeparator?: string;
150
+ /** Blinking text caret color. */
151
+ caret?: string;
152
+ /** Find-highlight color. */
153
+ searchHighlight?: string;
154
+ /** Comment pin fill once its thread is resolved. */
155
+ reviewPinResolved?: string;
156
+ /** Comment pin stroke. */
157
+ reviewPinStroke?: string;
158
+ /** Gap (px) between stacked pages. */
159
+ pageGapPx?: number;
160
+ /** Ruler band styling. */
161
+ ruler?: RulerTheme;
162
+ }
163
+
164
+ /** Editor behavior tuning — every field optional. */
165
+ export interface EditorBehavior {
166
+ /** Multiplicative zoom step for +/- and Ctrl+wheel. Default 1.1. */
167
+ zoomStep?: number;
168
+ /** Absolute minimum zoom. Default 0.25. */
169
+ zoomMin?: number;
170
+ /** Absolute maximum zoom. Default 5. */
171
+ zoomMax?: number;
172
+ /** Indent / outdent step in px per toolbar press. Default 36. */
173
+ indentStepPx?: number;
174
+ /** Default drawing-grid spacing in px (view.gridSpacingPx still overrides). Default 24. */
175
+ gridSpacingPx?: number;
176
+ }
177
+
178
+ /** Overrides the LIBRARY's built-in default run/paragraph styles for NEW/blank
179
+ * documents and the fallback stylesheet — NOT a loaded .docx's own defaults. */
180
+ export interface DefaultStyleOverrides {
181
+ /** Body (Normal) font family, e.g. "Times New Roman, serif". */
182
+ fontFamily?: string;
183
+ /** Body font size in px (96 dpi). */
184
+ fontSizePx?: number;
185
+ /** Body text color (any CSS color string). */
186
+ color?: string;
187
+ /** Default paragraph line height (multiplier). */
188
+ lineHeight?: number;
189
+ /** Heading font family (Title/Heading1/Heading2). Omit to keep the built-in. */
190
+ headingFontFamily?: string;
191
+ }
192
+
193
+ /** A ready-made dark-canvas theme (darkens the chrome/gutter; pages stay white
194
+ * for print fidelity). Spread + tweak it for your own theme. */
195
+ export declare const darkCanvasTheme: EditorTheme;
196
+
197
+ // ===== Ribbon customization (see WordCanvasOptions.customizeRibbon) =========
198
+
199
+ /** Where to place a tab/group/button relative to an existing one (by id). When
200
+ * the anchor id is absent/omitted, the item is appended at the end. */
201
+ export interface RibbonAnchor {
202
+ before?: string;
203
+ after?: string;
204
+ }
205
+
206
+ /** The editor handle handed to a custom ribbon button's `onClick`, plus macro
207
+ * helpers — read/edit the document at the caret, emit events, open popups. */
208
+ export interface RibbonActionContext extends EditorHandle {
209
+ getSelection(): DocSelection | null;
210
+ insertText(text: string): void;
211
+ /** Emit a custom event to the embedder (`wc.on("custom", …)`). */
212
+ emit(name: string, payload?: unknown): void;
213
+ /** Tie a DOM node (removed) or callback (run) to the editor's teardown so an
214
+ * embedder's popup is cleaned up on destroy(). */
215
+ registerCleanup(target: HTMLElement | (() => void)): void;
216
+ }
217
+
218
+ /** A custom ribbon button. */
219
+ export interface RibbonButtonSpec {
220
+ /** Stable id (namespace it, e.g. "myco.macros.upper"). */
221
+ id: string;
222
+ /** Raw innerHTML for the button face — an SVG string, emoji, or text. */
223
+ icon?: string;
224
+ /** Text label (used when `icon` is omitted). */
225
+ label?: string;
226
+ tooltip?: string;
227
+ onClick: (ctx: RibbonActionContext) => void;
228
+ /** Optional pressed-state predicate, re-evaluated on every selection change. */
229
+ active?: (fmt: unknown) => boolean;
230
+ /** Optional enabled predicate; the button greys out when it returns false. */
231
+ enabled?: (fmt: unknown) => boolean;
232
+ }
233
+
234
+ export interface RibbonTabSpec extends RibbonAnchor {
235
+ id: string;
236
+ label: string;
237
+ }
238
+ export interface RibbonGroupSpec extends RibbonAnchor {
239
+ id: string;
240
+ label: string;
241
+ }
242
+
243
+ /** Ribbon mutation API. Ordering is by id; an unknown id is a no-op with a
244
+ * console warning. Discover built-in ids with `tabs()`/`groups()`/`items()`. */
245
+ export interface RibbonApi {
246
+ tabs(): string[];
247
+ groups(tabId: string): string[];
248
+ items(groupId: string): string[];
249
+ addTab(spec: RibbonTabSpec): void;
250
+ removeTab(id: string): void;
251
+ moveTab(id: string, pos: RibbonAnchor): void;
252
+ addGroup(tabId: string, spec: RibbonGroupSpec): void;
253
+ removeGroup(id: string): void;
254
+ moveGroup(id: string, pos: RibbonAnchor): void;
255
+ addButton(groupId: string, spec: RibbonButtonSpec & RibbonAnchor): void;
256
+ removeItem(id: string): void;
257
+ moveItem(id: string, pos: RibbonAnchor & { toGroup?: string }): void;
258
+ }
259
+
260
+ /** Hook for the `customizeRibbon` option — called once at mount. */
261
+ export type CustomizeRibbon = (api: RibbonApi) => void;
262
+
263
+ /** Options for `makeFloatingDialog` (a draggable, non-blocking panel). */
264
+ export interface FloatingDialogOptions {
265
+ backdrop: HTMLElement;
266
+ modal: HTMLElement;
267
+ handle: HTMLElement;
268
+ signal: AbortSignal;
269
+ noDrag?: string;
270
+ }
271
+
272
+ /** Turn a backdrop+modal pair into a draggable, non-blocking floating panel —
273
+ * a convenience for building config/informational popups from a custom button. */
274
+ export declare function makeFloatingDialog(o: FloatingDialogOptions): void;
275
+
276
+ // ===========================================================================
277
+
22
278
  export interface WordCanvasOptions {
23
279
  /** Element to mount the editor into. */
24
280
  container: HTMLElement;
@@ -34,6 +290,27 @@ export interface WordCanvasOptions {
34
290
  user?: UserInfo;
35
291
  /** Override how a share link is surfaced (default: a built-in dialog). */
36
292
  onShareLink?: (url: string, docId: string) => void;
293
+ /** Route exports to your own pipeline. When set, the toolbar's Export (PDF /
294
+ * DOCX) buttons hand the produced file to this callback — a Blob plus raw
295
+ * bytes, the format, and any export warnings (see SaveEvent) — instead of
296
+ * triggering a browser download. Return a promise to keep the UI responsive
297
+ * while you upload. Omit to keep the default download behaviour. (For a fully
298
+ * custom button, call `exportDocx()` / `exportPdf()` on the instance.) */
299
+ onSave?: SaveHandler;
300
+ /** Mount as a view-only viewer: the document renders and stays selectable and
301
+ * copyable, but the editing chrome is hidden and every mutation is a no-op.
302
+ * In an online session a read-only client still receives live remote edits.
303
+ * Equivalent to `mode: "view"`. */
304
+ readonly?: boolean;
305
+ /** Initial editor mode: "edit" (default), "suggest" (edits become tracked
306
+ * changes), or "view" (read-only). Defaults to "view" when `readonly`. */
307
+ mode?: EditMode;
308
+ /** Restrict which modes the user can switch to (constrains the mode picker and
309
+ * setMode). Omit ⇒ all three. e.g. ["suggest","view"] locks out raw editing. */
310
+ allowedModes?: EditMode[];
311
+ /** Users that can be @-mentioned in comments. The embedder owns this roster;
312
+ * update it later with `setKnownUsers`. */
313
+ knownUsers?: UserInfo[];
37
314
  /** Resolve a custom (developer-defined) field's content from your backend. When
38
315
  * set, right-clicking a custom field offers "Update Field (<name>)"; the
39
316
  * callback receives the field's name + verbatim instruction and returns the new
@@ -48,6 +325,31 @@ export interface WordCanvasOptions {
48
325
  * embedders that don't opt in. Connect an agent via the WebMCP browser
49
326
  * extension / Chrome DevTools MCP. */
50
327
  agentTools?: boolean | AgentToolsOptions;
328
+ /** Initial view-chrome state — what the reader sees on open (rulers, grid,
329
+ * panels, ribbon, status bar, the Export buttons, zoom). Omit any field to keep
330
+ * its default. See WordCanvasViewOptions. */
331
+ view?: WordCanvasViewOptions;
332
+ /** Color theme — pass a (partial) `EditorTheme` to recolor the editor chrome:
333
+ * the gray canvas/gutter, gridlines, hyperlink/accent colors, formatting marks,
334
+ * TOC leaders, rulers, caret, etc. Omit any field to keep its built-in value.
335
+ * A ready-made `darkCanvasTheme` is exported to spread + tweak. Affects the
336
+ * on-screen editor only (exported PDFs keep the built-in look). */
337
+ theme?: EditorTheme;
338
+ /** Override the LIBRARY's built-in default run/paragraph styles (body font,
339
+ * size, color, line height, heading font) for NEW/blank documents and the
340
+ * fallback stylesheet. IMPORTANT: a loaded .docx keeps its OWN defaults
341
+ * (w:docDefaults / Normal) — set defaults there, or here, not both. */
342
+ overrideDefaultStyles?: DefaultStyleOverrides;
343
+ /** Behavior tuning: zoom step (default 1.1), zoom clamp (0.25–5), indent step
344
+ * (36px), and default drawing-grid spacing (24px; `view.gridSpacingPx` wins). */
345
+ behavior?: EditorBehavior;
346
+ /** Customize the ribbon toolbar. Called once at mount with a `RibbonApi` to
347
+ * reorder/remove built-in tabs/groups/buttons (by id — discover them with
348
+ * `api.tabs()/groups()/items()`) and add your own tabs, groups, and buttons.
349
+ * A custom button's `onClick` gets a `RibbonActionContext` (the editor handle +
350
+ * `getSelection`/`insertText`/`emit`/`registerCleanup`) — for macros, config
351
+ * popups, and informational popups. */
352
+ customizeRibbon?: CustomizeRibbon;
51
353
  /** Track first-load progress so you can show a loader while the big chunks
52
354
  * stream. Fires for the editor JS chunk download (`phase: "bundle"`,
53
355
  * indeterminate) and the bundled font fetch (`phase: "fonts"`, the dominant
@@ -56,6 +358,49 @@ export interface WordCanvasOptions {
56
358
  onLoadProgress?: (progress: LoadProgress) => void;
57
359
  }
58
360
 
361
+ /** Initial view-chrome state (see WordCanvasOptions.view). Every field is
362
+ * optional; omit one to keep its built-in default. */
363
+ export interface WordCanvasViewOptions {
364
+ /** Show the horizontal ruler. Default true (always hidden in readonly). */
365
+ ruler?: boolean;
366
+ /** Show the vertical ruler. Default true (always hidden in readonly). */
367
+ verticalRuler?: boolean;
368
+ /** Show the drawing-grid overlay on every page. Default false. */
369
+ grid?: boolean;
370
+ /** Snap dragged anchored objects to the grid. Default false. */
371
+ snapToGrid?: boolean;
372
+ /** Grid step in document px (96dpi). Default 24 (1/4 inch). */
373
+ gridSpacingPx?: number;
374
+ /** Show non-printing formatting marks (spaces, tabs, paragraph ends, line
375
+ * breaks) on open. Default false. */
376
+ formattingMarks?: boolean;
377
+ /** Open the Outline / navigation pane (left drawer). Default true. */
378
+ outline?: boolean;
379
+ /** Open the Bookmarks panel. Default false. */
380
+ bookmarks?: boolean;
381
+ /** Open the Review pane (track changes + comments). Default false. */
382
+ reviewPane?: boolean;
383
+ /** Open the Activity panel (online only — who edited & when). Default false. */
384
+ activity?: boolean;
385
+ /** Show the ribbon toolbar. Default true (always hidden in readonly). When
386
+ * false the editor stays fully editable but chromeless. */
387
+ toolbar?: boolean;
388
+ /** Start with the ribbon body collapsed (tab strip stays). Default false. */
389
+ ribbonCollapsed?: boolean;
390
+ /** Show the status bar (page/word count + zoom). Default true. */
391
+ statusBar?: boolean;
392
+ /** Show the File ▸ Export **PDF** button. Default true. Set false when the
393
+ * embedder ships its own export/save pipeline (e.g. via `onSave` or the
394
+ * `exportPdf()` method) and doesn't want the built-in button. */
395
+ exportPdf?: boolean;
396
+ /** Show the File ▸ Export **DOCX** button. Default true. Set false when the
397
+ * embedder ships its own export/save pipeline (e.g. via `onSave` or the
398
+ * `exportDocx()` method) and doesn't want the built-in button. */
399
+ exportDocx?: boolean;
400
+ /** Initial presentational zoom (1 = 100%, clamped to [0.25, 5]). Default 1. */
401
+ zoom?: number;
402
+ }
403
+
59
404
  /** First-load progress (see WordCanvasOptions.onLoadProgress). */
60
405
  export interface LoadProgress {
61
406
  /** Coarse stage: "bundle" = downloading the editor JS chunk (indeterminate —
@@ -107,6 +452,79 @@ export type FieldResult = string | ArrayBuffer | Uint8Array | Blob;
107
452
  /** Host hook producing a field's result for a resolve request. */
108
453
  export type FieldResolver = (req: FieldResolveRequest) => Promise<FieldResult>;
109
454
 
455
+ /** The file format an export/save produces. */
456
+ export type SaveFormat = "docx" | "pdf";
457
+
458
+ /** A deduplicated lossy-mapping note from an export. `count` = times it fired. */
459
+ export interface ExportWarning {
460
+ code: string;
461
+ detail?: string;
462
+ count: number;
463
+ }
464
+
465
+ /** Payload handed to a host `onSave` handler when the user triggers an export
466
+ * while `onSave` is configured (see WordCanvasOptions.onSave). */
467
+ export interface SaveEvent {
468
+ /** The exported file as a Blob with the correct MIME type set. */
469
+ blob: Blob;
470
+ /** The same file content as raw bytes (handy for FormData / a Node Buffer). */
471
+ bytes: Uint8Array;
472
+ /** Which format the user exported. */
473
+ format: SaveFormat;
474
+ /** Deduplicated lossy-mapping notes from the export (usually empty). */
475
+ warnings: ExportWarning[];
476
+ }
477
+
478
+ /** Host hook invoked instead of the built-in download when the user exports. */
479
+ export type SaveHandler = (event: SaveEvent) => void | Promise<void>;
480
+
481
+ /** What to render into a child-document surface (see WordCanvas.createChild). */
482
+ export type ChildContent =
483
+ | { kind: "blocks"; blocks: Block[] }
484
+ | { kind: "fragment"; fragment: { blocks: { runs: Run[]; style: ParaStyle }[]; inline?: boolean } }
485
+ | { kind: "runs"; runs: Run[]; paraStyle?: ParaStyle }
486
+ /** Body OOXML (w:p / w:tbl, or a w:document). Media-free (no images by r:id). */
487
+ | { kind: "ooxml"; ooxml: string }
488
+ /** A real sample of a named style ("AaBbCc" in the style's resolved formatting). */
489
+ | { kind: "styleSample"; styleId: string; sampleText?: string };
490
+
491
+ export interface ChildRenderOptions {
492
+ /** Layout/wrap width in CSS px. Default: target.clientWidth. */
493
+ widthPx?: number;
494
+ /** Child section margins (all sides), CSS px. Default 0. */
495
+ padding?: number;
496
+ /** Cap the displayed height; content beyond it is clipped. */
497
+ maxHeightPx?: number;
498
+ /** Fixed presentational scale (1 = 100%). Ignored when `fit` is set. */
499
+ zoom?: number;
500
+ /** Auto-scale the content to fit `widthPx` × `maxHeightPx` (compact swatches). */
501
+ fit?: boolean;
502
+ }
503
+
504
+ /** A live interactive editor over a child slice (canvas-native editing). */
505
+ export interface ChildEditorHandle {
506
+ /** The edited content as model blocks (hand back to splice into the parent). */
507
+ getBlocks(): Block[];
508
+ getDocument(): Document;
509
+ /** Insert an image at the caret. The bytes are registered in the parent
510
+ * document's shared media store (content-addressed), so the image renders here,
511
+ * survives serialize/export, and resolves after commit-back. Dimensions default
512
+ * to the image's natural size. */
513
+ insertImage(bytes: Uint8Array, mime: string, opts?: { widthPx?: number; heightPx?: number }): Promise<void>;
514
+ destroy(): void;
515
+ }
516
+
517
+ /** A lightweight sibling document that shares the parent editor's live styles,
518
+ * fonts and theme and renders/edits a content slice on canvas. */
519
+ export interface ChildDocument {
520
+ /** Paint a static preview of `content` into `target`. Safe to call repeatedly. */
521
+ render(target: HTMLElement, content: ChildContent, opts?: ChildRenderOptions): void;
522
+ /** Mount an interactive editor over `content` inside `target`. */
523
+ mountEditor(target: HTMLElement, content: ChildContent, opts?: ChildRenderOptions): ChildEditorHandle;
524
+ /** Tear down every surface/editor this child created. */
525
+ destroy(): void;
526
+ }
527
+
110
528
  /** Event name → payload, for `on(...)`/`off(...)`. */
111
529
  export interface WordCanvasEventMap {
112
530
  ready: Record<string, never>;
@@ -114,22 +532,60 @@ export interface WordCanvasEventMap {
114
532
  userEntered: { siteId: string; user?: UserInfo };
115
533
  userLeave: { siteId: string; user?: UserInfo };
116
534
  presence: { participants: Participant[] };
535
+ /** The editor mode changed (picker or setMode). */
536
+ modeChanged: { mode: EditMode };
537
+ /** The review overlay changed (suggestion/comment added, resolved, rebased).
538
+ * Carries the full layer so panels can re-render. */
539
+ reviewChanged: { review: ReviewLayer };
540
+ /** A custom ribbon button emitted an event via `ctx.emit(name, payload)`. */
541
+ custom: { name: string; payload?: unknown };
117
542
  }
118
543
 
119
544
  /** Handle resolved once the editor is mounted (via `whenReady()`). */
120
545
  export interface EditorHandle {
121
546
  /** Snapshot of the current document (plain data). */
122
547
  getDocument(): Document;
548
+ /** Create a child document sharing this editor's live styles/fonts/theme. */
549
+ createChild(): ChildDocument;
123
550
  /** Replace the open document with a programmatically-built one (e.g. a
124
551
  * DocumentBuilder result). Cloned; drops undo history and any live collab
125
552
  * session; preserves zoom + scroll. */
126
553
  setDocument(doc: Document): void;
127
554
  /** Open a .docx (auto-publishes when online); resolves when loaded. */
128
555
  openDocx(file: File | ArrayBuffer): Promise<void>;
556
+ /** Export the current document to a .docx Blob (track changes baked to the
557
+ * original baseline, matching the toolbar's Export). For a custom Save button. */
558
+ exportDocx(): Promise<Blob>;
559
+ /** Export the current document to a PDF Blob (see `exportDocx`). */
560
+ exportPdf(): Promise<Blob>;
129
561
  /** Publish the current document and resolve its shareable link (online only). */
130
562
  share(): Promise<string>;
131
563
  getDocId(): string | null;
132
564
  getShareLink(): string | null;
565
+ /** The current selection/caret, or null when the editor isn't focused. */
566
+ getSelection(): DocSelection | null;
567
+ /** Insert plain text at the caret, replacing any selection. */
568
+ insertText(text: string): void;
569
+ // ---- review layer (track changes + comments) ----------------------------
570
+ /** The current editor mode. */
571
+ getMode(): EditMode;
572
+ /** Switch mode; returns false if the mode isn't allowed. */
573
+ setMode(mode: EditMode): boolean;
574
+ /** Snapshot of the review overlay (suggestions + comment threads). */
575
+ getReview(): ReviewLayer;
576
+ /** The effective @-mentionable roster (configured base + live editors). */
577
+ getKnownUsers(): UserInfo[];
578
+ /** Replace the configured base roster (live editors stay merged on top). */
579
+ setKnownUsers(users: UserInfo[]): void;
580
+ acceptSuggestion(id: string): void;
581
+ rejectSuggestion(id: string): void;
582
+ acceptAllSuggestions(): void;
583
+ rejectAllSuggestions(): void;
584
+ /** Add a comment thread anchored to the current selection. Returns the thread
585
+ * id, or null if there's no selection. */
586
+ addComment(body: Fragment, mentions?: UserInfo[]): string | null;
587
+ replyToComment(threadId: string, body: Fragment, mentions?: UserInfo[]): void;
588
+ resolveThread(threadId: string, resolved?: boolean): void;
133
589
  destroy(): void;
134
590
  }
135
591
 
@@ -141,8 +597,20 @@ export declare class WordCanvas {
141
597
  off<E extends keyof WordCanvasEventMap>(event: E, handler: (data: WordCanvasEventMap[E]) => void): void;
142
598
  /** Resolves once the editor is mounted and ready. */
143
599
  whenReady(): Promise<EditorHandle>;
600
+ /** Create a child document that shares this editor's live styles, fonts and
601
+ * theme — render a real, document-styled preview of a content slice onto a
602
+ * canvas (instead of an HTML approximation), or mount a canvas-native editor
603
+ * over a slice. Returned synchronously; render() before ready is buffered.
604
+ * (mountEditor() requires the editor to be ready — await whenReady() first.) */
605
+ createChild(): ChildDocument;
144
606
  /** Open a .docx. When online, auto-publishes it and surfaces a share link. */
145
607
  openDocx(file: File | ArrayBuffer): Promise<void>;
608
+ /** Export the current document to a .docx Blob — track changes baked to the
609
+ * original baseline, exactly like the toolbar's Export. Wire it to your own
610
+ * Save button and POST it anywhere. Resolves once the editor is ready. */
611
+ exportDocx(): Promise<Blob>;
612
+ /** Export the current document to a PDF Blob (see `exportDocx`). */
613
+ exportPdf(): Promise<Blob>;
146
614
  /** Replace the open document with a programmatically-built one (e.g. a
147
615
  * DocumentBuilder result). The input is cloned. Like openDocx, this starts a
148
616
  * NEW document: undo history and any live collab session are dropped (the
@@ -151,6 +619,30 @@ export declare class WordCanvas {
151
619
  setDocument(doc: Document): Promise<void>;
152
620
  /** Publish the current document and resolve its shareable link (online only). */
153
621
  share(): Promise<string>;
622
+ // ---- review layer (track changes + comments) ----------------------------
623
+ /** Current editor mode. Returns null before the editor is ready. */
624
+ getMode(): EditMode | null;
625
+ /** Switch mode. Resolves to false if the mode isn't allowed (or not ready). */
626
+ setMode(mode: EditMode): Promise<boolean>;
627
+ /** Snapshot of the review overlay (suggestions + comment threads). */
628
+ getReview(): Promise<ReviewLayer>;
629
+ /** The effective @-mentionable roster (configured base + live editors). */
630
+ getKnownUsers(): Promise<UserInfo[]>;
631
+ /** Replace the configured base roster (live editors stay merged on top). */
632
+ setKnownUsers(users: UserInfo[]): Promise<void>;
633
+ acceptSuggestion(id: string): Promise<void>;
634
+ rejectSuggestion(id: string): Promise<void>;
635
+ acceptAllSuggestions(): Promise<void>;
636
+ rejectAllSuggestions(): Promise<void>;
637
+ /** Add a comment thread anchored to the current selection. Resolves to the
638
+ * thread id, or null if there's no selection. */
639
+ addComment(body: Fragment, mentions?: UserInfo[]): Promise<string | null>;
640
+ replyToComment(threadId: string, body: Fragment, mentions?: UserInfo[]): Promise<void>;
641
+ resolveThread(threadId: string, resolved?: boolean): Promise<void>;
642
+ /** Current selection/caret. Returns null before ready or when unfocused. */
643
+ getSelection(): DocSelection | null;
644
+ /** Insert plain text at the caret (replacing any selection). */
645
+ insertText(text: string): Promise<void>;
154
646
  getDocId(): string | null;
155
647
  getShareLink(): string | null;
156
648
  destroy(): void;