docxkeun 0.1.2 → 0.2.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.
Files changed (197) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +139 -16
  3. package/dist/api/editor.d.ts +172 -5
  4. package/dist/api/editor.js +814 -24
  5. package/dist/api/headers.d.ts +51 -0
  6. package/dist/api/headers.js +133 -0
  7. package/dist/api/index.d.ts +5 -1
  8. package/dist/api/index.js +4 -1
  9. package/dist/api/lists.d.ts +37 -0
  10. package/dist/api/lists.js +132 -0
  11. package/dist/api/parts.js +1 -0
  12. package/dist/api/positions.js +1 -0
  13. package/dist/api/revisions.d.ts +61 -0
  14. package/dist/api/revisions.js +390 -0
  15. package/dist/api/selection.d.ts +57 -0
  16. package/dist/api/selection.js +91 -0
  17. package/dist/charts/color.d.ts +45 -0
  18. package/dist/charts/color.js +322 -0
  19. package/dist/charts/format.d.ts +12 -0
  20. package/dist/charts/format.js +459 -0
  21. package/dist/charts/index.d.ts +11 -0
  22. package/dist/charts/index.js +8 -0
  23. package/dist/charts/model.d.ts +225 -0
  24. package/dist/charts/model.js +758 -0
  25. package/dist/charts/outline.d.ts +17 -0
  26. package/dist/charts/outline.js +214 -0
  27. package/dist/charts/render.d.ts +20 -0
  28. package/dist/charts/render.js +1772 -0
  29. package/dist/drawing/arrows.d.ts +37 -0
  30. package/dist/drawing/arrows.js +99 -0
  31. package/dist/drawing/formula.d.ts +40 -0
  32. package/dist/drawing/formula.js +159 -0
  33. package/dist/drawing/geometry.d.ts +61 -0
  34. package/dist/drawing/geometry.js +286 -0
  35. package/dist/drawing/index.d.ts +12 -0
  36. package/dist/drawing/index.js +8 -0
  37. package/dist/drawing/presets.d.ts +72 -0
  38. package/dist/drawing/presets.js +2874 -0
  39. package/dist/fonts/shaping/apply.d.ts +94 -0
  40. package/dist/fonts/shaping/apply.js +349 -0
  41. package/dist/fonts/shaping/arabic.d.ts +28 -0
  42. package/dist/fonts/shaping/arabic.js +235 -0
  43. package/dist/fonts/shaping/buffer.d.ts +53 -0
  44. package/dist/fonts/shaping/buffer.js +95 -0
  45. package/dist/fonts/shaping/gdef.d.ts +30 -0
  46. package/dist/fonts/shaping/gdef.js +66 -0
  47. package/dist/fonts/shaping/gpos.d.ts +86 -0
  48. package/dist/fonts/shaping/gpos.js +428 -0
  49. package/dist/fonts/shaping/gsub.d.ts +65 -0
  50. package/dist/fonts/shaping/gsub.js +313 -0
  51. package/dist/fonts/shaping/index.d.ts +32 -0
  52. package/dist/fonts/shaping/index.js +402 -0
  53. package/dist/fonts/shaping/indic.d.ts +6 -0
  54. package/dist/fonts/shaping/indic.js +805 -0
  55. package/dist/fonts/shaping/layout-common.d.ts +124 -0
  56. package/dist/fonts/shaping/layout-common.js +349 -0
  57. package/dist/fonts/shaping/plan.d.ts +87 -0
  58. package/dist/fonts/shaping/plan.js +152 -0
  59. package/dist/fonts/shaping/scripts.d.ts +26 -0
  60. package/dist/fonts/shaping/scripts.js +160 -0
  61. package/dist/fonts/shaping/shaper.d.ts +23 -0
  62. package/dist/fonts/shaping/shaper.js +5 -0
  63. package/dist/fonts/shaping/unicode.d.ts +34 -0
  64. package/dist/fonts/shaping/unicode.js +138 -0
  65. package/dist/images/bmp.d.ts +9 -0
  66. package/dist/images/bmp.js +234 -0
  67. package/dist/images/gif.d.ts +9 -0
  68. package/dist/images/gif.js +215 -0
  69. package/dist/images/index.d.ts +18 -0
  70. package/dist/images/index.js +63 -0
  71. package/dist/images/png-encode.d.ts +4 -0
  72. package/dist/images/png-encode.js +73 -0
  73. package/dist/images/size.d.ts +10 -0
  74. package/dist/images/size.js +83 -0
  75. package/dist/images/tiff.d.ts +14 -0
  76. package/dist/images/tiff.js +407 -0
  77. package/dist/index.d.ts +7 -2
  78. package/dist/index.js +5 -0
  79. package/dist/layout/bidi.d.ts +18 -0
  80. package/dist/layout/bidi.js +71 -0
  81. package/dist/layout/blocks.d.ts +20 -3
  82. package/dist/layout/blocks.js +88 -58
  83. package/dist/layout/cache.d.ts +31 -0
  84. package/dist/layout/cache.js +38 -0
  85. package/dist/layout/drawing.d.ts +30 -0
  86. package/dist/layout/drawing.js +120 -0
  87. package/dist/layout/engine.d.ts +3 -0
  88. package/dist/layout/engine.js +218 -11
  89. package/dist/layout/index.d.ts +1 -0
  90. package/dist/layout/index.js +1 -0
  91. package/dist/layout/lines.d.ts +63 -5
  92. package/dist/layout/lines.js +245 -20
  93. package/dist/layout/paint.d.ts +25 -3
  94. package/dist/layout/paint.js +26 -10
  95. package/dist/layout/shapes.d.ts +7 -0
  96. package/dist/layout/shapes.js +338 -0
  97. package/dist/layout/style.d.ts +4 -0
  98. package/dist/layout/style.js +2 -0
  99. package/dist/layout/types.d.ts +89 -2
  100. package/dist/math/boxes.d.ts +88 -0
  101. package/dist/math/boxes.js +166 -0
  102. package/dist/math/index.d.ts +29 -0
  103. package/dist/math/index.js +44 -0
  104. package/dist/math/layout.d.ts +13 -0
  105. package/dist/math/layout.js +771 -0
  106. package/dist/math/linear.d.ts +3 -0
  107. package/dist/math/linear.js +152 -0
  108. package/dist/math/omml.d.ts +15 -0
  109. package/dist/math/omml.js +57 -0
  110. package/dist/math/paths.d.ts +61 -0
  111. package/dist/math/paths.js +450 -0
  112. package/dist/math/symbols.d.ts +49 -0
  113. package/dist/math/symbols.js +269 -0
  114. package/dist/math/text.d.ts +53 -0
  115. package/dist/math/text.js +316 -0
  116. package/dist/metafile/canvas.d.ts +80 -0
  117. package/dist/metafile/canvas.js +515 -0
  118. package/dist/metafile/dib.d.ts +27 -0
  119. package/dist/metafile/dib.js +300 -0
  120. package/dist/metafile/emf.d.ts +31 -0
  121. package/dist/metafile/emf.js +591 -0
  122. package/dist/metafile/geometry.d.ts +49 -0
  123. package/dist/metafile/geometry.js +85 -0
  124. package/dist/metafile/index.d.ts +27 -0
  125. package/dist/metafile/index.js +34 -0
  126. package/dist/metafile/reader.d.ts +28 -0
  127. package/dist/metafile/reader.js +59 -0
  128. package/dist/metafile/state.d.ts +118 -0
  129. package/dist/metafile/state.js +172 -0
  130. package/dist/metafile/text.d.ts +17 -0
  131. package/dist/metafile/text.js +130 -0
  132. package/dist/metafile/wmf.d.ts +33 -0
  133. package/dist/metafile/wmf.js +395 -0
  134. package/dist/model/content.d.ts +92 -7
  135. package/dist/model/content.js +251 -17
  136. package/dist/model/document.d.ts +34 -1
  137. package/dist/model/document.js +80 -1
  138. package/dist/model/fields.d.ts +4 -0
  139. package/dist/model/fields.js +10 -0
  140. package/dist/model/numbering.d.ts +2 -0
  141. package/dist/model/numbering.js +22 -0
  142. package/dist/model/props.d.ts +2 -0
  143. package/dist/model/props.js +3 -0
  144. package/dist/opc/index.d.ts +1 -1
  145. package/dist/opc/package.d.ts +34 -0
  146. package/dist/opc/package.js +58 -4
  147. package/dist/pdf/export.js +97 -6
  148. package/dist/pdf/images.d.ts +7 -0
  149. package/dist/pdf/images.js +39 -2
  150. package/dist/render/activity.d.ts +138 -0
  151. package/dist/render/activity.js +794 -0
  152. package/dist/render/clipboard.d.ts +29 -0
  153. package/dist/render/clipboard.js +401 -0
  154. package/dist/render/comments.d.ts +166 -0
  155. package/dist/render/comments.js +779 -0
  156. package/dist/render/context-menu.d.ts +124 -0
  157. package/dist/render/context-menu.js +525 -0
  158. package/dist/render/editing.d.ts +146 -5
  159. package/dist/render/editing.js +678 -53
  160. package/dist/render/index.d.ts +12 -0
  161. package/dist/render/index.js +11 -0
  162. package/dist/render/navigation-panel.d.ts +76 -0
  163. package/dist/render/navigation-panel.js +431 -0
  164. package/dist/render/selection.js +5 -0
  165. package/dist/render/svg.d.ts +4 -6
  166. package/dist/render/svg.js +69 -7
  167. package/dist/render/toolbar.d.ts +83 -0
  168. package/dist/render/toolbar.js +505 -0
  169. package/dist/render/ui/comment-layout.d.ts +39 -0
  170. package/dist/render/ui/comment-layout.js +79 -0
  171. package/dist/render/ui/icons.d.ts +6 -0
  172. package/dist/render/ui/icons.js +38 -0
  173. package/dist/render/ui/menu.d.ts +38 -0
  174. package/dist/render/ui/menu.js +274 -0
  175. package/dist/render/ui/nav-model.d.ts +11 -0
  176. package/dist/render/ui/nav-model.js +34 -0
  177. package/dist/render/ui/overflow.d.ts +5 -0
  178. package/dist/render/ui/overflow.js +18 -0
  179. package/dist/render/ui/popover.d.ts +18 -0
  180. package/dist/render/ui/popover.js +109 -0
  181. package/dist/render/ui/theme.d.ts +2 -0
  182. package/dist/render/ui/theme.js +29 -0
  183. package/dist/render/undo.d.ts +76 -0
  184. package/dist/render/undo.js +144 -0
  185. package/dist/render/viewer.d.ts +100 -7
  186. package/dist/render/viewer.js +302 -92
  187. package/dist/security/policy.d.ts +3 -0
  188. package/dist/security/policy.js +6 -2
  189. package/dist/security/protection.d.ts +1 -0
  190. package/dist/security/protection.js +1 -1
  191. package/dist/text/bidi-data.d.ts +53 -0
  192. package/dist/text/bidi-data.js +318 -0
  193. package/dist/text/bidi.d.ts +37 -0
  194. package/dist/text/bidi.js +508 -0
  195. package/dist/xml/ns.d.ts +5 -0
  196. package/dist/xml/ns.js +7 -0
  197. package/package.json +9 -2
@@ -3,3 +3,15 @@ export { pageToSvg, type SvgOptions } from './svg.js';
3
3
  export { CanvasFontProvider } from './canvas-fonts.js';
4
4
  export { positionFromDom, selectionPositions, type InlinePosition } from './selection.js';
5
5
  export { collectNavTargets, nextTarget, compareLocations, textIn, type NavTarget, type NavTargets, type NavKind, type DocLocation } from './navigation.js';
6
+ export { NavigationController, type NavTab, type NavigationOptions } from './navigation-panel.js';
7
+ export { CommentsController, relativeTime, type CommentThread, type CommentsMode, type CommentsOptions } from './comments.js';
8
+ export { filterTargets, currentHeading } from './ui/nav-model.js';
9
+ export type { MenuItem, MenuHandle } from './ui/menu.js';
10
+ export { openMenu } from './ui/menu.js';
11
+ export { promptPopover } from './ui/popover.js';
12
+ export { ContextMenuController, hitTarget, defaultItems, parseLinkTarget, type ContextMenuOptions, type MenuContext, type MenuTarget, type MenuTargetKind, type MenuEnv, type MenuActions, type FormatChoice, type ParagraphChoice } from './context-menu.js';
13
+ export { ToolbarController, FormatToolbar, type ToolbarOptions } from './toolbar.js';
14
+ export { InlineEditing, type CaretFormat, type EditStory, type EditingOptions, type Caret } from './editing.js';
15
+ export { htmlToParagraphs, type PastedParagraph, type PastedRun } from './clipboard.js';
16
+ export { selectionInfoFor, describeSelection, type SelectionInfo, type SelectedParagraph, type TextSpot } from '../api/selection.js';
17
+ export { AiActivity, diffParagraphs, diffText, type ParagraphDiff, type ParagraphChange, type ParagraphSnapshot, type ActivityOptions } from './activity.js';
@@ -3,3 +3,14 @@ export { pageToSvg } from './svg.js';
3
3
  export { CanvasFontProvider } from './canvas-fonts.js';
4
4
  export { positionFromDom, selectionPositions } from './selection.js';
5
5
  export { collectNavTargets, nextTarget, compareLocations, textIn } from './navigation.js';
6
+ export { NavigationController } from './navigation-panel.js';
7
+ export { CommentsController, relativeTime } from './comments.js';
8
+ export { filterTargets, currentHeading } from './ui/nav-model.js';
9
+ export { openMenu } from './ui/menu.js';
10
+ export { promptPopover } from './ui/popover.js';
11
+ export { ContextMenuController, hitTarget, defaultItems, parseLinkTarget } from './context-menu.js';
12
+ export { ToolbarController, FormatToolbar } from './toolbar.js';
13
+ export { InlineEditing } from './editing.js';
14
+ export { htmlToParagraphs } from './clipboard.js';
15
+ export { selectionInfoFor, describeSelection } from '../api/selection.js';
16
+ export { AiActivity, diffParagraphs, diffText } from './activity.js';
@@ -0,0 +1,76 @@
1
+ /**
2
+ * The navigation panel beside the pages: an outline of the headings, the bookmarks and the links,
3
+ * with search, previous/next, back/forward, and a fold into a thin rail. `viewer.navigation` is its
4
+ * controller; the DOM is built on the first `show()`. The controller also keeps the lists
5
+ * (`headings`, `bookmarks`, `links`) that `viewer.navigation` has always offered.
6
+ */
7
+ import type { DocxViewer } from './viewer.js';
8
+ import type { NavTarget } from './navigation.js';
9
+ export type NavTab = 'outline' | 'bookmarks' | 'links';
10
+ export interface NavigationOptions {
11
+ /** The tab shown first. Default: outline. */
12
+ tab?: NavTab;
13
+ /** Start folded into the rail. Default: folded when the frame is narrower than 720px. */
14
+ collapsed?: boolean;
15
+ }
16
+ /** What the panel needs from the viewer. */
17
+ export type NavigationHost = Pick<DocxViewer, 'root' | 'on' | 'emit' | 'frame' | 'goTo' | 'back' | 'forward' | 'canGoBack' | 'canGoForward' | 'location' | 'next' | 'previous' | 'layout' | 'scrollToComment' | 'collectedTargets'>;
18
+ /** Controls the navigation panel of a viewer (`viewer.navigation`); the DOM is built on the first `show()`. */
19
+ export declare class NavigationController {
20
+ private readonly viewer;
21
+ private el;
22
+ private shown;
23
+ private fold;
24
+ private active;
25
+ private query;
26
+ private showHidden;
27
+ private current;
28
+ private readonly items;
29
+ private readonly cleanup;
30
+ private scrollTimer;
31
+ private list;
32
+ /** The row last marked current (revealed once, not on every scroll). */
33
+ private marked;
34
+ private count;
35
+ private back;
36
+ private forward;
37
+ private tabButtons;
38
+ private hiddenLabel;
39
+ private emptyEl;
40
+ constructor(viewer: NavigationHost, options?: NavigationOptions);
41
+ /** The headings of the document, in order. */
42
+ get headings(): NavTarget[];
43
+ /** The bookmarks (including hidden `_Toc…` ones), in order. */
44
+ get bookmarks(): NavTarget[];
45
+ /** The hyperlinks in the body, in order. */
46
+ get links(): NavTarget[];
47
+ get element(): HTMLElement | undefined;
48
+ get visible(): boolean;
49
+ get collapsed(): boolean;
50
+ set collapsed(value: boolean);
51
+ get tab(): NavTab;
52
+ set tab(value: NavTab);
53
+ /** Show the panel. With a tab, unfold onto that tab; without one, keep the fold as it is. */
54
+ show(tab?: NavTab): void;
55
+ hide(): void;
56
+ /** Rebuild the lists from the current layout (done automatically after each layout). */
57
+ refresh(): void;
58
+ destroy(): void;
59
+ private announce;
60
+ private mount;
61
+ private build;
62
+ private rail;
63
+ private panel;
64
+ private get spec();
65
+ private targets;
66
+ private renderList;
67
+ /** Highlight the heading the viewport is at (outline tab only). */
68
+ private markCurrent;
69
+ /** Scroll the list (never the page around it) so a row is in view. */
70
+ private reveal;
71
+ private syncHistory;
72
+ private step;
73
+ private unfoldTo;
74
+ /** The first comment below the top of the viewport, wrapping to the first one in the document. */
75
+ private goToNextComment;
76
+ }
@@ -0,0 +1,431 @@
1
+ import { icon } from './ui/icons.js';
2
+ import { currentHeading, filterTargets } from './ui/nav-model.js';
3
+ import { ensureUiStyle } from './ui/theme.js';
4
+ const STYLE_ID = 'docxkeun-navigation-style';
5
+ const NARROW = 720;
6
+ const CSS = `
7
+ .docxkeun-nav{flex:none;box-sizing:border-box;width:240px;overflow:hidden;display:flex;flex-direction:column;background:var(--dk-ui-bg);color:var(--dk-ui-fg);border-right:1px solid var(--dk-ui-line);font:var(--dk-ui-font)}
8
+ .docxkeun-nav[hidden]{display:none}
9
+ .docxkeun-nav.dk-rail{width:36px;align-items:center;padding:4px 0;gap:2px}
10
+ @media (prefers-reduced-motion:no-preference){.docxkeun-nav{transition:width .15s ease}}
11
+ .docxkeun-nav button{height:28px;min-width:28px;border:1px solid transparent;border-radius:4px;background:transparent;color:inherit;font:inherit;padding:0 6px;cursor:pointer;display:inline-flex;align-items:center;justify-content:center}
12
+ .docxkeun-nav button:hover:not(:disabled){background:var(--dk-ui-hover)}
13
+ .docxkeun-nav button:focus-visible,.docxkeun-nav input:focus-visible{outline:2px solid var(--dk-ui-accent);outline-offset:1px}
14
+ .docxkeun-nav button:disabled{opacity:.4;cursor:default}
15
+ .docxkeun-nav .dk-nav-bar{display:flex;align-items:center;gap:2px;padding:4px 6px}
16
+ .docxkeun-nav .dk-nav-bar .dk-nav-spacer{flex:1 1 auto}
17
+ .docxkeun-nav .dk-nav-tabs{display:flex;gap:2px;padding:0 6px;border-bottom:1px solid var(--dk-ui-line)}
18
+ .docxkeun-nav .dk-nav-tabs button{border-radius:4px 4px 0 0;height:30px;flex:1 1 0;min-width:0;padding:0 4px;font-size:12px;white-space:nowrap;color:var(--dk-ui-muted);border-bottom:2px solid transparent}
19
+ .docxkeun-nav .dk-nav-tabs button[aria-selected=true]{color:var(--dk-ui-fg);border-bottom-color:var(--dk-ui-accent)}
20
+ .docxkeun-nav input[type=search]{flex:1 1 auto;min-width:0;height:28px;box-sizing:border-box;border:1px solid var(--dk-ui-line);border-radius:4px;background:var(--dk-ui-bg);color:inherit;font:inherit;padding:0 8px}
21
+ .docxkeun-nav .dk-nav-hidden[hidden],.docxkeun-nav .dk-nav-empty[hidden]{display:none}
22
+ .docxkeun-nav .dk-nav-hidden{display:flex;align-items:center;gap:6px;padding:2px 10px;font-size:12px;color:var(--dk-ui-muted)}
23
+ .docxkeun-nav .dk-nav-count{font-size:12px;color:var(--dk-ui-muted);padding:0 4px;white-space:nowrap}
24
+ .docxkeun-nav .dk-nav-list{list-style:none;margin:0;padding:2px 4px;overflow:auto;flex:1 1 auto;min-height:0}
25
+ .docxkeun-nav .dk-nav-list button{width:100%;height:auto;min-height:26px;justify-content:flex-start;text-align:left;gap:8px;padding-top:3px;padding-bottom:3px}
26
+ .docxkeun-nav .dk-nav-list [aria-current=location]{background:var(--dk-ui-accent-soft)}
27
+ .docxkeun-nav .dk-nav-label{flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
28
+ .docxkeun-nav .dk-nav-meta{flex:none;font-size:11px;color:var(--dk-ui-muted);max-width:90px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
29
+ .docxkeun-nav .dk-nav-empty{padding:16px 12px;color:var(--dk-ui-muted)}
30
+ .docxkeun-nav .dk-nav-foot{display:flex;padding:4px 6px;border-top:1px solid var(--dk-ui-line)}
31
+ `;
32
+ function ensureStyle() {
33
+ ensureUiStyle();
34
+ if (typeof document === 'undefined' || document.getElementById(STYLE_ID))
35
+ return;
36
+ const style = document.createElement('style');
37
+ style.id = STYLE_ID;
38
+ style.textContent = CSS;
39
+ document.head.appendChild(style);
40
+ }
41
+ const TABS = [
42
+ { tab: 'outline', label: 'Outline', icon: 'outline', kind: 'heading', key: 'headings', empty: 'No headings' },
43
+ { tab: 'bookmarks', label: 'Bookmarks', icon: 'bookmark', kind: 'bookmark', key: 'bookmarks', empty: 'No bookmarks' },
44
+ { tab: 'links', label: 'Links', icon: 'link', kind: 'link', key: 'links', empty: 'No links' },
45
+ ];
46
+ function button(label, html, run, extra = '') {
47
+ const b = document.createElement('button');
48
+ b.type = 'button';
49
+ b.innerHTML = html;
50
+ b.title = label;
51
+ b.setAttribute('aria-label', label);
52
+ if (extra)
53
+ b.className = extra;
54
+ b.addEventListener('click', run);
55
+ return b;
56
+ }
57
+ /** Controls the navigation panel of a viewer (`viewer.navigation`); the DOM is built on the first `show()`. */
58
+ export class NavigationController {
59
+ viewer;
60
+ el;
61
+ shown = false;
62
+ fold;
63
+ active;
64
+ query = '';
65
+ showHidden = false;
66
+ current;
67
+ items = new Map();
68
+ cleanup = [];
69
+ scrollTimer;
70
+ list;
71
+ /** The row last marked current (revealed once, not on every scroll). */
72
+ marked;
73
+ count;
74
+ back;
75
+ forward;
76
+ tabButtons = [];
77
+ hiddenLabel;
78
+ emptyEl;
79
+ constructor(viewer, options = {}) {
80
+ this.viewer = viewer;
81
+ this.fold = options.collapsed;
82
+ this.active = options.tab ?? 'outline';
83
+ }
84
+ /** The headings of the document, in order. */
85
+ get headings() {
86
+ return this.viewer.collectedTargets().headings;
87
+ }
88
+ /** The bookmarks (including hidden `_Toc…` ones), in order. */
89
+ get bookmarks() {
90
+ return this.viewer.collectedTargets().bookmarks;
91
+ }
92
+ /** The hyperlinks in the body, in order. */
93
+ get links() {
94
+ return this.viewer.collectedTargets().links;
95
+ }
96
+ get element() {
97
+ return this.el;
98
+ }
99
+ get visible() {
100
+ return this.shown;
101
+ }
102
+ get collapsed() {
103
+ return !!this.fold;
104
+ }
105
+ set collapsed(value) {
106
+ if (value === !!this.fold && this.fold !== undefined)
107
+ return;
108
+ this.fold = value;
109
+ if (!this.el)
110
+ return;
111
+ this.build();
112
+ this.announce();
113
+ }
114
+ get tab() {
115
+ return this.active;
116
+ }
117
+ set tab(value) {
118
+ if (value === this.active)
119
+ return;
120
+ this.active = value;
121
+ this.query = '';
122
+ if (!this.el)
123
+ return;
124
+ if (this.fold)
125
+ return;
126
+ this.build();
127
+ }
128
+ /** Show the panel. With a tab, unfold onto that tab; without one, keep the fold as it is. */
129
+ show(tab) {
130
+ let changed = !this.shown;
131
+ if (tab) {
132
+ this.active = tab;
133
+ this.query = '';
134
+ if (this.fold)
135
+ changed = true;
136
+ this.fold = false;
137
+ }
138
+ if (!this.el)
139
+ this.mount();
140
+ else if (tab)
141
+ this.build();
142
+ this.shown = true;
143
+ this.el.hidden = false;
144
+ if (changed)
145
+ this.announce();
146
+ }
147
+ hide() {
148
+ if (!this.shown)
149
+ return;
150
+ this.shown = false;
151
+ if (this.el)
152
+ this.el.hidden = true;
153
+ this.announce();
154
+ }
155
+ /** Rebuild the lists from the current layout (done automatically after each layout). */
156
+ refresh() {
157
+ if (!this.el || this.fold) {
158
+ if (this.el)
159
+ this.build();
160
+ return;
161
+ }
162
+ this.renderList();
163
+ }
164
+ destroy() {
165
+ clearTimeout(this.scrollTimer);
166
+ for (const fn of this.cleanup.splice(0))
167
+ fn();
168
+ this.el?.remove();
169
+ this.el = undefined;
170
+ }
171
+ announce() {
172
+ this.viewer.emit('chromechange', { part: 'navigation', visible: this.shown, collapsed: !!this.fold });
173
+ }
174
+ mount() {
175
+ ensureStyle();
176
+ const { frame, body } = this.viewer.frame();
177
+ if (this.fold === undefined) {
178
+ const width = frame.getBoundingClientRect().width || frame.clientWidth;
179
+ // A width of 0 means the frame is not laid out yet; leave the panel open then.
180
+ this.fold = width > 0 && width < NARROW;
181
+ }
182
+ const el = (this.el = document.createElement('nav'));
183
+ el.className = 'docxkeun-nav docxkeun-ui-root';
184
+ el.setAttribute('aria-label', 'Navigation');
185
+ el.hidden = true;
186
+ body.insertBefore(el, this.viewer.root);
187
+ this.build();
188
+ this.cleanup.push(this.viewer.on('layout', () => this.refresh()));
189
+ this.cleanup.push(this.viewer.on('navigate', ({ target }) => {
190
+ this.current = target?.kind === 'heading' ? target : undefined;
191
+ this.syncHistory();
192
+ this.markCurrent();
193
+ }));
194
+ const onScroll = () => {
195
+ if (this.scrollTimer)
196
+ return;
197
+ this.scrollTimer = setTimeout(() => {
198
+ this.scrollTimer = undefined;
199
+ this.current = undefined;
200
+ this.markCurrent();
201
+ }, 100);
202
+ };
203
+ this.viewer.root.addEventListener('scroll', onScroll, { passive: true });
204
+ this.cleanup.push(() => this.viewer.root.removeEventListener('scroll', onScroll));
205
+ }
206
+ build() {
207
+ const el = this.el;
208
+ // Rebuilding drops the focused control; when focus was inside the panel, put it on the counterpart.
209
+ const hadFocus = el.contains(document.activeElement);
210
+ this.items.clear();
211
+ this.list = this.count = this.back = this.forward = undefined;
212
+ this.tabButtons = [];
213
+ el.classList.toggle('dk-rail', !!this.fold);
214
+ if (this.fold) {
215
+ el.replaceChildren(...this.rail());
216
+ if (hadFocus)
217
+ el.querySelector('[data-unfold]')?.focus();
218
+ return;
219
+ }
220
+ el.replaceChildren(...this.panel());
221
+ this.renderList();
222
+ if (hadFocus)
223
+ this.tabButtons.find((b) => b.dataset.tab === this.active)?.focus();
224
+ }
225
+ rail() {
226
+ const out = [];
227
+ for (const t of TABS)
228
+ out.push(button(t.label, icon(t.icon), () => this.unfoldTo(t.tab)));
229
+ if ((this.viewer.layout?.commentAnchors.size ?? 0) > 0) {
230
+ out.push(button('Comments', icon('comment'), () => this.goToNextComment()));
231
+ }
232
+ const spacer = document.createElement('span');
233
+ spacer.style.flex = '1 1 auto';
234
+ const unfold = button('Show navigation', icon('forward'), () => (this.collapsed = false));
235
+ unfold.dataset.unfold = '';
236
+ out.push(spacer, unfold);
237
+ return out;
238
+ }
239
+ panel() {
240
+ const bar = document.createElement('div');
241
+ bar.className = 'dk-nav-bar';
242
+ this.back = button('Back', icon('back'), () => this.viewer.back());
243
+ this.forward = button('Forward', icon('forward'), () => this.viewer.forward());
244
+ bar.append(this.back, this.forward);
245
+ const spacer = document.createElement('span');
246
+ spacer.className = 'dk-nav-spacer';
247
+ bar.appendChild(spacer);
248
+ const tabs = document.createElement('div');
249
+ tabs.className = 'dk-nav-tabs';
250
+ tabs.setAttribute('role', 'tablist');
251
+ tabs.setAttribute('aria-label', 'Navigation views');
252
+ for (const t of TABS) {
253
+ const b = button(t.label, t.label, () => (this.tab = t.tab));
254
+ b.setAttribute('role', 'tab');
255
+ b.dataset.tab = t.tab;
256
+ tabs.appendChild(b);
257
+ this.tabButtons.push(b);
258
+ }
259
+ tabs.addEventListener('keydown', (e) => {
260
+ const i = TABS.findIndex((t) => t.tab === this.active);
261
+ const next = e.key === 'ArrowRight' ? i + 1 : e.key === 'ArrowLeft' ? i - 1 : undefined;
262
+ if (next === undefined)
263
+ return;
264
+ e.preventDefault();
265
+ this.tab = TABS[(next + TABS.length) % TABS.length].tab;
266
+ this.tabButtons.find((b) => b.getAttribute('aria-selected') === 'true')?.focus();
267
+ });
268
+ const search = document.createElement('div');
269
+ search.className = 'dk-nav-bar';
270
+ const input = document.createElement('input');
271
+ input.type = 'search';
272
+ input.placeholder = 'Search';
273
+ input.setAttribute('aria-label', 'Search the list');
274
+ input.value = this.query;
275
+ input.addEventListener('input', () => {
276
+ this.query = input.value;
277
+ this.renderList();
278
+ });
279
+ const prev = button('Previous', icon('fold'), () => this.step(-1));
280
+ const next = button('Next', icon('unfold'), () => this.step(1));
281
+ prev.dataset.step = 'prev';
282
+ next.dataset.step = 'next';
283
+ this.count = document.createElement('span');
284
+ this.count.className = 'dk-nav-count';
285
+ search.append(input, this.count, prev, next);
286
+ const list = (this.list = document.createElement('ul'));
287
+ list.className = 'dk-nav-list';
288
+ const foot = document.createElement('div');
289
+ foot.className = 'dk-nav-foot';
290
+ foot.appendChild(button('Fold navigation', icon('back'), () => (this.collapsed = true)));
291
+ const label = (this.hiddenLabel = document.createElement('label'));
292
+ label.className = 'dk-nav-hidden';
293
+ const sw = document.createElement('input');
294
+ sw.type = 'checkbox';
295
+ sw.setAttribute('role', 'switch');
296
+ sw.checked = this.showHidden;
297
+ sw.addEventListener('change', () => {
298
+ this.showHidden = sw.checked;
299
+ this.renderList();
300
+ });
301
+ label.append(sw, 'Show hidden bookmarks');
302
+ const empty = (this.emptyEl = document.createElement('div'));
303
+ empty.className = 'dk-nav-empty';
304
+ empty.hidden = true;
305
+ return [bar, tabs, search, label, empty, list, foot];
306
+ }
307
+ get spec() {
308
+ return TABS.find((t) => t.tab === this.active);
309
+ }
310
+ targets() {
311
+ const spec = this.spec;
312
+ let list = this.viewer.collectedTargets()[spec.key];
313
+ if (spec.kind === 'bookmark' && !this.showHidden)
314
+ list = list.filter((b) => !b.hidden);
315
+ return filterTargets(list, this.query);
316
+ }
317
+ renderList() {
318
+ const list = this.list;
319
+ if (!list)
320
+ return;
321
+ const spec = this.spec;
322
+ this.items.clear();
323
+ for (const b of this.tabButtons) {
324
+ const on = b.dataset.tab === this.active;
325
+ b.setAttribute('aria-selected', String(on));
326
+ b.tabIndex = on ? 0 : -1;
327
+ }
328
+ const prev = this.el.querySelector('[data-step=prev]');
329
+ const next = this.el.querySelector('[data-step=next]');
330
+ prev?.setAttribute('aria-label', `Previous ${spec.kind}`);
331
+ next?.setAttribute('aria-label', `Next ${spec.kind}`);
332
+ if (prev)
333
+ prev.title = `Previous ${spec.kind}`;
334
+ if (next)
335
+ next.title = `Next ${spec.kind}`;
336
+ this.syncHistory();
337
+ // The "show hidden" switch belongs to the Bookmarks tab only.
338
+ this.hiddenLabel.hidden = spec.kind !== 'bookmark';
339
+ const all = this.viewer.collectedTargets()[spec.key].length;
340
+ const targets = this.targets();
341
+ this.count.textContent = String(targets.length);
342
+ const empty = this.emptyEl;
343
+ empty.hidden = targets.length > 0;
344
+ if (!targets.length) {
345
+ empty.textContent = all && this.query.trim() ? 'No matches' : spec.empty;
346
+ list.replaceChildren();
347
+ return;
348
+ }
349
+ const rows = targets.map((t) => {
350
+ const li = document.createElement('li');
351
+ const b = document.createElement('button');
352
+ b.type = 'button';
353
+ b.title = t.link?.url ?? t.label;
354
+ if (t.level)
355
+ b.style.paddingLeft = `${6 + t.level * 14}px`;
356
+ const text = document.createElement('span');
357
+ text.className = 'dk-nav-label';
358
+ text.textContent = t.label;
359
+ const meta = document.createElement('span');
360
+ meta.className = 'dk-nav-meta';
361
+ meta.textContent = t.kind === 'link' ? (t.link?.anchor ? `#${t.link.anchor}` : (t.link?.url ?? '')) : `p. ${t.page + 1}`;
362
+ b.append(text, meta);
363
+ b.addEventListener('click', () => this.viewer.goTo(t, { history: true }));
364
+ li.appendChild(b);
365
+ this.items.set(t, b);
366
+ return li;
367
+ });
368
+ list.replaceChildren(...rows);
369
+ this.markCurrent();
370
+ }
371
+ /** Highlight the heading the viewport is at (outline tab only). */
372
+ markCurrent() {
373
+ if (this.active !== 'outline' || !this.items.size)
374
+ return;
375
+ const at = this.viewer.location;
376
+ // A jump leaves 24pt above its target, so a heading that close below the top is the current one.
377
+ const now = this.current && this.items.has(this.current) ? this.current : currentHeading([...this.items.keys()], at, 30);
378
+ for (const [t, b] of this.items) {
379
+ if (t === now) {
380
+ b.setAttribute('aria-current', 'location');
381
+ if (b !== this.marked)
382
+ this.reveal(b);
383
+ this.marked = b;
384
+ }
385
+ else
386
+ b.removeAttribute('aria-current');
387
+ }
388
+ }
389
+ /** Scroll the list (never the page around it) so a row is in view. */
390
+ reveal(row) {
391
+ const list = this.list;
392
+ if (!list)
393
+ return;
394
+ const box = list.getBoundingClientRect();
395
+ if (!box.height)
396
+ return;
397
+ const r = row.getBoundingClientRect();
398
+ if (r.top < box.top)
399
+ list.scrollTop += r.top - box.top;
400
+ else if (r.bottom > box.bottom)
401
+ list.scrollTop += Math.min(r.bottom - box.bottom, r.top - box.top);
402
+ }
403
+ syncHistory() {
404
+ if (this.back)
405
+ this.back.disabled = !this.viewer.canGoBack;
406
+ if (this.forward)
407
+ this.forward.disabled = !this.viewer.canGoForward;
408
+ }
409
+ step(dir) {
410
+ const opts = { includeHidden: this.showHidden };
411
+ if (dir > 0)
412
+ this.viewer.next(this.spec.kind, opts);
413
+ else
414
+ this.viewer.previous(this.spec.kind, opts);
415
+ }
416
+ unfoldTo(tab) {
417
+ this.active = tab;
418
+ this.query = '';
419
+ this.collapsed = false;
420
+ }
421
+ /** The first comment below the top of the viewport, wrapping to the first one in the document. */
422
+ goToNextComment() {
423
+ const anchors = [...(this.viewer.layout?.commentAnchors ?? [])].map(([id, a]) => ({ id, page: a.page, y: a.rect.y }));
424
+ if (!anchors.length)
425
+ return;
426
+ anchors.sort((a, b) => a.page - b.page || a.y - b.y);
427
+ const at = this.viewer.location;
428
+ const next = anchors.find((a) => a.page > at.page || (a.page === at.page && a.y > at.y)) ?? anchors[0];
429
+ this.viewer.scrollToComment(next.id);
430
+ }
431
+ }
@@ -18,6 +18,11 @@ export function positionFromDom(node, offset, pages) {
18
18
  const item = pi === undefined ? undefined : pages[pi]?.items[Number(el.getAttribute('data-i'))];
19
19
  if (!item || item.type !== 'text' || !item.source)
20
20
  return undefined;
21
+ if (el.hasAttribute('data-complex')) {
22
+ // The element holds the run in logical order.
23
+ const start = Math.min(...item.source.offsets);
24
+ return { inline: item.source.inline, offset: start + offset };
25
+ }
21
26
  let cluster;
22
27
  const fixed = el.getAttribute('data-c');
23
28
  if (fixed !== null) {
@@ -1,9 +1,4 @@
1
- /**
2
- * Display list → SVG markup. Text stays real text (selectable, searchable, accessible), with an
3
- * explicit x for every character so the browser draws exactly what layout computed.
4
- * All document-derived strings are escaped: documents are untrusted input.
5
- */
6
- import type { LayoutFont, Page } from '../layout/types.js';
1
+ import type { DisplayItem, LayoutFont, Page, PathItem } from '../layout/types.js';
7
2
  export interface SvgOptions {
8
3
  /** CSS font-family for a layout font (registered FontFace name, or a system family list). */
9
4
  fontFamily(font: LayoutFont): string;
@@ -11,4 +6,7 @@ export interface SvgOptions {
11
6
  imageUrl(target: string | undefined, external: boolean): string | undefined;
12
7
  }
13
8
  export declare function esc(s: string): string;
9
+ export declare function pathData(commands: PathItem['commands']): string;
10
+ /** SVG markup for a list of display items (without the <svg> wrapper). */
11
+ export declare function itemsToSvg(items: DisplayItem[], o: SvgOptions, indexBase?: number): string;
14
12
  export declare function pageToSvg(page: Page, o: SvgOptions): string;