@beyondwork/docx-react-component 1.0.17 → 1.0.19
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/README.md +8 -2
- package/package.json +32 -34
- package/src/api/README.md +5 -1
- package/src/api/public-types.ts +374 -4
- package/src/api/session-state.ts +58 -0
- package/src/core/commands/formatting-commands.ts +1 -0
- package/src/core/commands/image-commands.ts +147 -0
- package/src/core/commands/index.ts +5 -1
- package/src/core/commands/list-commands.ts +231 -36
- package/src/core/commands/paragraph-layout-commands.ts +339 -0
- package/src/core/commands/section-layout-commands.ts +680 -0
- package/src/core/commands/style-commands.ts +262 -0
- package/src/core/search/search-text.ts +329 -0
- package/src/core/selection/mapping.ts +41 -0
- package/src/core/state/editor-state.ts +1 -1
- package/src/index.ts +30 -0
- package/src/io/docx-session.ts +260 -39
- package/src/io/export/serialize-main-document.ts +202 -5
- package/src/io/export/serialize-numbering.ts +28 -7
- package/src/io/normalize/normalize-text.ts +63 -25
- package/src/io/ooxml/numbering-sentinels.ts +44 -0
- package/src/io/ooxml/parse-footnotes.ts +212 -20
- package/src/io/ooxml/parse-headers-footers.ts +229 -25
- package/src/io/ooxml/parse-inline-media.ts +16 -0
- package/src/io/ooxml/parse-main-document.ts +411 -6
- package/src/io/ooxml/parse-numbering.ts +7 -0
- package/src/io/ooxml/parse-settings.ts +184 -0
- package/src/io/ooxml/parse-shapes.ts +25 -0
- package/src/io/ooxml/parse-styles.ts +463 -0
- package/src/io/ooxml/parse-theme.ts +32 -0
- package/src/model/canonical-document.ts +133 -3
- package/src/model/cds-1.0.0.ts +13 -0
- package/src/model/snapshot.ts +2 -1
- package/src/runtime/document-layout.ts +332 -0
- package/src/runtime/document-navigation.ts +564 -0
- package/src/runtime/document-runtime.ts +265 -35
- package/src/runtime/document-search.ts +145 -0
- package/src/runtime/numbering-prefix.ts +47 -26
- package/src/runtime/page-layout-estimation.ts +212 -0
- package/src/runtime/read-only-diagnostics-runtime.ts +1 -0
- package/src/runtime/session-capabilities.ts +2 -0
- package/src/runtime/story-context.ts +164 -0
- package/src/runtime/story-targeting.ts +162 -0
- package/src/runtime/surface-projection.ts +239 -12
- package/src/runtime/table-schema.ts +87 -5
- package/src/runtime/view-state.ts +459 -0
- package/src/ui/WordReviewEditor.tsx +1902 -312
- package/src/ui/browser-export.ts +52 -0
- package/src/ui/headless/preserve-editor-selection.ts +5 -0
- package/src/ui/headless/selection-helpers.ts +20 -0
- package/src/ui/headless/selection-toolbar-model.ts +22 -0
- package/src/ui/headless/use-editor-keyboard.ts +6 -1
- package/src/ui-tailwind/chrome/tw-page-ruler.tsx +386 -0
- package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +125 -14
- package/src/ui-tailwind/editor-surface/perf-probe.ts +107 -0
- package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +45 -6
- package/src/ui-tailwind/editor-surface/pm-contextual-ui.ts +31 -0
- package/src/ui-tailwind/editor-surface/pm-position-map.ts +2 -2
- package/src/ui-tailwind/editor-surface/pm-schema.ts +47 -5
- package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +95 -22
- package/src/ui-tailwind/editor-surface/search-plugin.ts +19 -68
- package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +11 -0
- package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +394 -77
- package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +0 -1
- package/src/ui-tailwind/index.ts +2 -1
- package/src/ui-tailwind/review/tw-comment-sidebar.tsx +277 -147
- package/src/ui-tailwind/review/tw-review-rail.tsx +6 -6
- package/src/ui-tailwind/theme/editor-theme.css +123 -0
- package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +4 -0
- package/src/ui-tailwind/toolbar/tw-toolbar.tsx +291 -12
- package/src/ui-tailwind/tw-review-workspace.tsx +926 -27
- package/src/validation/compatibility-engine.ts +92 -20
- package/src/validation/diagnostics.ts +1 -0
- package/src/validation/docx-comment-proof.ts +487 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime-owned view state — non-canonical interaction model.
|
|
3
|
+
*
|
|
4
|
+
* This module owns the editor's interaction and measurement state that is
|
|
5
|
+
* separate from the canonical document model. View state is never serialized
|
|
6
|
+
* into persisted snapshots or exported DOCX; it exists only while the editor
|
|
7
|
+
* session is live.
|
|
8
|
+
*
|
|
9
|
+
* Design boundaries:
|
|
10
|
+
* - Canonical document state lives in EditorState / CanonicalDocumentEnvelope.
|
|
11
|
+
* - View state is runtime-owned, non-canonical, non-exported.
|
|
12
|
+
* - Builds on Wave 35's activeStory and PageLayoutSnapshot model.
|
|
13
|
+
* - Runtime viewMode models editor posture ("editing" | "review" | "view").
|
|
14
|
+
* - Workspace mode and zoom are runtime-owned so hosts and the mounted shell
|
|
15
|
+
* share one view-state contract.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type {
|
|
19
|
+
ActiveListContext,
|
|
20
|
+
ActiveNoteContext,
|
|
21
|
+
CaretAffinity,
|
|
22
|
+
EditorStoryTarget,
|
|
23
|
+
EditorSurfaceSnapshot,
|
|
24
|
+
EditorViewStateSnapshot,
|
|
25
|
+
LayoutMeasurement,
|
|
26
|
+
PageLayoutSnapshot,
|
|
27
|
+
PageRegionHitTest,
|
|
28
|
+
SelectionSnapshot,
|
|
29
|
+
SurfaceBlockSnapshot,
|
|
30
|
+
SurfaceInlineSegment,
|
|
31
|
+
ViewMode,
|
|
32
|
+
WorkspaceMode,
|
|
33
|
+
ZoomLevel,
|
|
34
|
+
} from "../api/public-types";
|
|
35
|
+
import type { NumberingCatalog } from "../model/canonical-document.ts";
|
|
36
|
+
|
|
37
|
+
export interface ViewState {
|
|
38
|
+
viewMode: ViewMode;
|
|
39
|
+
workspaceMode: WorkspaceMode;
|
|
40
|
+
zoomLevel: ZoomLevel;
|
|
41
|
+
isFocused: boolean;
|
|
42
|
+
caretAffinity: CaretAffinity;
|
|
43
|
+
activePageRegion: PageRegionHitTest | null;
|
|
44
|
+
activeObjectFrame: LayoutMeasurement["objectFrame"] | null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const MIN_ZOOM_PERCENT = 50;
|
|
48
|
+
const MAX_ZOOM_PERCENT = 200;
|
|
49
|
+
|
|
50
|
+
const DEFAULT_VIEW_STATE: ViewState = {
|
|
51
|
+
viewMode: "editing",
|
|
52
|
+
workspaceMode: "canvas",
|
|
53
|
+
zoomLevel: 100,
|
|
54
|
+
isFocused: false,
|
|
55
|
+
caretAffinity: "none",
|
|
56
|
+
activePageRegion: null,
|
|
57
|
+
activeObjectFrame: null,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function createViewState(initial?: Partial<ViewState>): ViewState {
|
|
61
|
+
return { ...DEFAULT_VIEW_STATE, ...initial };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function setViewMode(state: ViewState, mode: ViewMode): ViewState {
|
|
65
|
+
if (state.viewMode === mode) return state;
|
|
66
|
+
return { ...state, viewMode: mode };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function setWorkspaceMode(
|
|
70
|
+
state: ViewState,
|
|
71
|
+
workspaceMode: WorkspaceMode,
|
|
72
|
+
): ViewState {
|
|
73
|
+
if (state.workspaceMode === workspaceMode) {
|
|
74
|
+
return state;
|
|
75
|
+
}
|
|
76
|
+
return { ...state, workspaceMode };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function setZoomLevel(state: ViewState, zoomLevel: ZoomLevel): ViewState {
|
|
80
|
+
const normalizedZoom = normalizeZoomLevel(zoomLevel, state.zoomLevel);
|
|
81
|
+
if (state.zoomLevel === normalizedZoom) {
|
|
82
|
+
return state;
|
|
83
|
+
}
|
|
84
|
+
return { ...state, zoomLevel: normalizedZoom };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function setFocused(state: ViewState, focused: boolean): ViewState {
|
|
88
|
+
if (state.isFocused === focused) return state;
|
|
89
|
+
return { ...state, isFocused: focused };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function setCaretAffinity(state: ViewState, affinity: CaretAffinity): ViewState {
|
|
93
|
+
if (state.caretAffinity === affinity) return state;
|
|
94
|
+
return { ...state, caretAffinity: affinity };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function setActivePageRegion(state: ViewState, region: PageRegionHitTest | null): ViewState {
|
|
98
|
+
return { ...state, activePageRegion: region };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function setActiveObjectFrame(
|
|
102
|
+
state: ViewState,
|
|
103
|
+
frame: LayoutMeasurement["objectFrame"] | null,
|
|
104
|
+
): ViewState {
|
|
105
|
+
return { ...state, activeObjectFrame: frame };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Derive list context from the surface block at the current selection head.
|
|
110
|
+
*/
|
|
111
|
+
export function deriveActiveListContext(
|
|
112
|
+
surface: EditorSurfaceSnapshot | undefined,
|
|
113
|
+
selectionHead: number,
|
|
114
|
+
numberingCatalog?: NumberingCatalog,
|
|
115
|
+
): ActiveListContext | null {
|
|
116
|
+
if (!surface) return null;
|
|
117
|
+
const block = findBlockAtPosition(surface.blocks, selectionHead);
|
|
118
|
+
if (!block || block.kind !== "paragraph" || !block.numbering) return null;
|
|
119
|
+
|
|
120
|
+
const isOrdered = resolveListOrdering(block.numbering, numberingCatalog);
|
|
121
|
+
return {
|
|
122
|
+
numberingInstanceId: block.numbering.numberingInstanceId,
|
|
123
|
+
level: block.numbering.level,
|
|
124
|
+
isOrdered,
|
|
125
|
+
...(block.numberingPrefix ? { markerText: block.numberingPrefix } : {}),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Derive note context when the active story targets a footnote or endnote.
|
|
131
|
+
*/
|
|
132
|
+
export function deriveActiveNoteContext(
|
|
133
|
+
activeStory: EditorStoryTarget,
|
|
134
|
+
mainSurface: EditorSurfaceSnapshot | undefined,
|
|
135
|
+
): ActiveNoteContext | null {
|
|
136
|
+
if (activeStory.kind !== "footnote" && activeStory.kind !== "endnote") {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const referencePosition = findNoteReferencePosition(mainSurface, activeStory);
|
|
141
|
+
return {
|
|
142
|
+
noteKind: activeStory.kind,
|
|
143
|
+
noteId: activeStory.noteId,
|
|
144
|
+
referencePosition,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Derive layout measurement from the surface at the current selection.
|
|
150
|
+
*/
|
|
151
|
+
export function deriveLayoutMeasurement(
|
|
152
|
+
surface: EditorSurfaceSnapshot | undefined,
|
|
153
|
+
selectionOrPosition: SelectionSnapshot | number,
|
|
154
|
+
viewState: ViewState,
|
|
155
|
+
): LayoutMeasurement {
|
|
156
|
+
const selectionHead =
|
|
157
|
+
typeof selectionOrPosition === "number"
|
|
158
|
+
? selectionOrPosition
|
|
159
|
+
: selectionOrPosition.activeRange.kind === "node"
|
|
160
|
+
? selectionOrPosition.activeRange.at
|
|
161
|
+
: selectionOrPosition.head;
|
|
162
|
+
const block = surface ? findBlockAtPosition(surface.blocks, selectionHead) : null;
|
|
163
|
+
const tabStops = block?.kind === "paragraph" && block.tabStops ? block.tabStops : [];
|
|
164
|
+
const listMarkerLane = deriveListMarkerLane(block);
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
pageRegions: viewState.activePageRegion ? [viewState.activePageRegion] : [],
|
|
168
|
+
caretAffinity: viewState.caretAffinity,
|
|
169
|
+
tabStops,
|
|
170
|
+
listMarkerLane: listMarkerLane ?? undefined,
|
|
171
|
+
objectFrame: viewState.activeObjectFrame ?? undefined,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Build the full EditorViewStateSnapshot from runtime + surface state.
|
|
177
|
+
*/
|
|
178
|
+
export function createEditorViewStateSnapshot(
|
|
179
|
+
viewState: ViewState,
|
|
180
|
+
activeStory: EditorStoryTarget,
|
|
181
|
+
selection: SelectionSnapshot,
|
|
182
|
+
surface: EditorSurfaceSnapshot | undefined,
|
|
183
|
+
mainSurface: EditorSurfaceSnapshot | undefined,
|
|
184
|
+
pageLayout: PageLayoutSnapshot | null | undefined,
|
|
185
|
+
numberingCatalog?: NumberingCatalog,
|
|
186
|
+
): EditorViewStateSnapshot {
|
|
187
|
+
const selectionPosition =
|
|
188
|
+
selection.activeRange.kind === "node" ? selection.activeRange.at : selection.head;
|
|
189
|
+
const derivedViewState = deriveInteractionViewState(
|
|
190
|
+
viewState,
|
|
191
|
+
activeStory,
|
|
192
|
+
selection,
|
|
193
|
+
surface,
|
|
194
|
+
pageLayout,
|
|
195
|
+
);
|
|
196
|
+
const activeListContext = deriveActiveListContext(surface, selectionPosition, numberingCatalog);
|
|
197
|
+
const activeNoteContext = deriveActiveNoteContext(activeStory, mainSurface);
|
|
198
|
+
const measurement = deriveLayoutMeasurement(surface, selection, derivedViewState);
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
viewMode: derivedViewState.viewMode,
|
|
202
|
+
workspaceMode: derivedViewState.workspaceMode,
|
|
203
|
+
zoomLevel: derivedViewState.zoomLevel,
|
|
204
|
+
activeStory,
|
|
205
|
+
selection,
|
|
206
|
+
caretAffinity: derivedViewState.caretAffinity,
|
|
207
|
+
activeListContext,
|
|
208
|
+
activeNoteContext,
|
|
209
|
+
activePageRegion: derivedViewState.activePageRegion,
|
|
210
|
+
activeObjectFrame: derivedViewState.activeObjectFrame,
|
|
211
|
+
measurement,
|
|
212
|
+
isFocused: derivedViewState.isFocused,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---------------------------------------------------------------------------
|
|
217
|
+
// Internal helpers
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
|
|
220
|
+
function findBlockAtPosition(
|
|
221
|
+
blocks: readonly SurfaceBlockSnapshot[],
|
|
222
|
+
position: number,
|
|
223
|
+
): SurfaceBlockSnapshot | null {
|
|
224
|
+
for (const block of blocks) {
|
|
225
|
+
if (position >= block.from && position <= block.to) {
|
|
226
|
+
if (block.kind === "sdt_block") {
|
|
227
|
+
const inner = findBlockAtPosition(block.children, position);
|
|
228
|
+
if (inner) return inner;
|
|
229
|
+
}
|
|
230
|
+
return block;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function resolveListOrdering(
|
|
237
|
+
numbering: NonNullable<Extract<SurfaceBlockSnapshot, { kind: "paragraph" }>["numbering"]>,
|
|
238
|
+
numberingCatalog?: NumberingCatalog,
|
|
239
|
+
): boolean {
|
|
240
|
+
if (!numberingCatalog) {
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const instance = numberingCatalog.instances[numbering.numberingInstanceId];
|
|
245
|
+
const definition = instance
|
|
246
|
+
? numberingCatalog.abstractDefinitions[instance.abstractNumberingId]
|
|
247
|
+
: undefined;
|
|
248
|
+
const levelDefinition = definition?.levels.find((level) => level.level === numbering.level);
|
|
249
|
+
|
|
250
|
+
if (!levelDefinition) {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return levelDefinition.format !== "bullet";
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function deriveInteractionViewState(
|
|
258
|
+
viewState: ViewState,
|
|
259
|
+
activeStory: EditorStoryTarget,
|
|
260
|
+
selection: SelectionSnapshot,
|
|
261
|
+
surface: EditorSurfaceSnapshot | undefined,
|
|
262
|
+
pageLayout: PageLayoutSnapshot | null | undefined,
|
|
263
|
+
): ViewState {
|
|
264
|
+
const activePageRegion = deriveActivePageRegion(activeStory, pageLayout) ?? viewState.activePageRegion;
|
|
265
|
+
const activeObjectFrame = deriveActiveObjectFrame(surface, selection) ?? viewState.activeObjectFrame;
|
|
266
|
+
const caretAffinity = deriveCaretAffinity(surface, selection);
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
...viewState,
|
|
270
|
+
caretAffinity,
|
|
271
|
+
activePageRegion,
|
|
272
|
+
activeObjectFrame,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function deriveActivePageRegion(
|
|
277
|
+
activeStory: EditorStoryTarget,
|
|
278
|
+
pageLayout: PageLayoutSnapshot | null | undefined,
|
|
279
|
+
): PageRegionHitTest | null {
|
|
280
|
+
if (!pageLayout) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
const sectionIndex = pageLayout?.sectionIndex ?? 0;
|
|
284
|
+
switch (activeStory.kind) {
|
|
285
|
+
case "header":
|
|
286
|
+
return { region: "header", sectionIndex, columnIndex: 0 };
|
|
287
|
+
case "footer":
|
|
288
|
+
return { region: "footer", sectionIndex, columnIndex: 0 };
|
|
289
|
+
default:
|
|
290
|
+
return { region: "body", sectionIndex, columnIndex: 0 };
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function deriveActiveObjectFrame(
|
|
295
|
+
surface: EditorSurfaceSnapshot | undefined,
|
|
296
|
+
selection: SelectionSnapshot,
|
|
297
|
+
): LayoutMeasurement["objectFrame"] | null {
|
|
298
|
+
if (!surface) {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const position =
|
|
303
|
+
selection.activeRange.kind === "node" ? selection.activeRange.at : selection.head;
|
|
304
|
+
const block = findBlockAtPosition(surface.blocks, position);
|
|
305
|
+
if (!block || block.kind !== "paragraph") {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const segment = findObjectSegmentAtPosition(block.segments, position);
|
|
310
|
+
if (!segment) {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (segment.kind === "image") {
|
|
315
|
+
return {
|
|
316
|
+
kind: "image",
|
|
317
|
+
anchorPos: segment.from,
|
|
318
|
+
display: segment.display === "floating" ? "floating" : "inline",
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const objectKind = inferOpaqueObjectKind(segment);
|
|
323
|
+
if (!objectKind) {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return {
|
|
328
|
+
kind: objectKind,
|
|
329
|
+
anchorPos: segment.from,
|
|
330
|
+
display: "inline",
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function deriveCaretAffinity(
|
|
335
|
+
surface: EditorSurfaceSnapshot | undefined,
|
|
336
|
+
selection: SelectionSnapshot,
|
|
337
|
+
): CaretAffinity {
|
|
338
|
+
if (selection.activeRange.kind === "node") {
|
|
339
|
+
return selection.activeRange.assoc < 0 ? "backward" : "forward";
|
|
340
|
+
}
|
|
341
|
+
if (!selection.isCollapsed || !surface) {
|
|
342
|
+
return "none";
|
|
343
|
+
}
|
|
344
|
+
const block = findBlockAtPosition(surface.blocks, selection.head);
|
|
345
|
+
if (!block || block.kind !== "paragraph") {
|
|
346
|
+
return "none";
|
|
347
|
+
}
|
|
348
|
+
for (const segment of block.segments) {
|
|
349
|
+
if (!isObjectLikeSegment(segment)) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (selection.head === segment.from) {
|
|
353
|
+
return "forward";
|
|
354
|
+
}
|
|
355
|
+
if (selection.head === segment.to) {
|
|
356
|
+
return "backward";
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return "none";
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export function findNoteReferencePosition(
|
|
363
|
+
mainSurface: EditorSurfaceSnapshot | undefined,
|
|
364
|
+
target: EditorStoryTarget & { kind: "footnote" | "endnote" },
|
|
365
|
+
): number {
|
|
366
|
+
if (!mainSurface) return 0;
|
|
367
|
+
for (const block of mainSurface.blocks) {
|
|
368
|
+
if (block.kind === "paragraph") {
|
|
369
|
+
for (const segment of block.segments) {
|
|
370
|
+
if (
|
|
371
|
+
segment.kind === "note_ref" &&
|
|
372
|
+
segment.noteKind === target.kind &&
|
|
373
|
+
segment.noteId === target.noteId
|
|
374
|
+
) {
|
|
375
|
+
return segment.from;
|
|
376
|
+
}
|
|
377
|
+
// Fallback: opaque_inline references from older surface projections
|
|
378
|
+
if (
|
|
379
|
+
segment.kind === "opaque_inline" &&
|
|
380
|
+
segment.label.toLowerCase().includes(target.kind) &&
|
|
381
|
+
segment.detail.includes(target.noteId)
|
|
382
|
+
) {
|
|
383
|
+
return segment.from;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return 0;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function normalizeZoomLevel(
|
|
392
|
+
zoomLevel: ZoomLevel,
|
|
393
|
+
currentZoom: ZoomLevel,
|
|
394
|
+
): ZoomLevel {
|
|
395
|
+
if (zoomLevel === "pageWidth" || zoomLevel === "onePage") {
|
|
396
|
+
return zoomLevel;
|
|
397
|
+
}
|
|
398
|
+
if (!Number.isFinite(zoomLevel) || zoomLevel <= 0) {
|
|
399
|
+
return currentZoom;
|
|
400
|
+
}
|
|
401
|
+
return Math.max(
|
|
402
|
+
MIN_ZOOM_PERCENT,
|
|
403
|
+
Math.min(MAX_ZOOM_PERCENT, Math.round(zoomLevel)),
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function findObjectSegmentAtPosition(
|
|
408
|
+
segments: readonly SurfaceInlineSegment[],
|
|
409
|
+
position: number,
|
|
410
|
+
): Extract<SurfaceInlineSegment, { kind: "image" | "opaque_inline" }> | null {
|
|
411
|
+
for (const segment of segments) {
|
|
412
|
+
if (!isObjectLikeSegment(segment)) {
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (position >= segment.from && position <= segment.to) {
|
|
416
|
+
return segment;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function isObjectLikeSegment(
|
|
423
|
+
segment: SurfaceInlineSegment,
|
|
424
|
+
): segment is Extract<SurfaceInlineSegment, { kind: "image" | "opaque_inline" }> {
|
|
425
|
+
if (segment.kind === "image") {
|
|
426
|
+
return true;
|
|
427
|
+
}
|
|
428
|
+
if (segment.kind !== "opaque_inline") {
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
return inferOpaqueObjectKind(segment) !== null;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function inferOpaqueObjectKind(
|
|
435
|
+
segment: Extract<SurfaceInlineSegment, { kind: "opaque_inline" }>,
|
|
436
|
+
): "textbox" | "shape" | null {
|
|
437
|
+
if (segment.label === "Shape") {
|
|
438
|
+
return segment.detail.startsWith("Text box")
|
|
439
|
+
? "textbox"
|
|
440
|
+
: "shape";
|
|
441
|
+
}
|
|
442
|
+
if (segment.label === "VML shape") {
|
|
443
|
+
return segment.detail.includes("#_x0000_t202")
|
|
444
|
+
? "textbox"
|
|
445
|
+
: "shape";
|
|
446
|
+
}
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function deriveListMarkerLane(
|
|
451
|
+
block: SurfaceBlockSnapshot | null,
|
|
452
|
+
): { indent: number; markerWidth: number } | null {
|
|
453
|
+
if (!block || block.kind !== "paragraph" || !block.numbering) return null;
|
|
454
|
+
const indent = block.indentation?.hanging ?? block.indentation?.left ?? 360;
|
|
455
|
+
return {
|
|
456
|
+
indent,
|
|
457
|
+
markerWidth: Math.min(indent, 360),
|
|
458
|
+
};
|
|
459
|
+
}
|