docxkeun 0.1.3 → 0.3.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 (199) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/README.md +166 -16
  3. package/dist/api/editor.d.ts +231 -7
  4. package/dist/api/editor.js +1125 -26
  5. package/dist/api/headers.d.ts +51 -0
  6. package/dist/api/headers.js +133 -0
  7. package/dist/api/index.d.ts +6 -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 +401 -0
  15. package/dist/api/selection.d.ts +57 -0
  16. package/dist/api/selection.js +91 -0
  17. package/dist/api/tables.d.ts +60 -0
  18. package/dist/api/tables.js +138 -0
  19. package/dist/charts/color.d.ts +45 -0
  20. package/dist/charts/color.js +322 -0
  21. package/dist/charts/format.d.ts +12 -0
  22. package/dist/charts/format.js +459 -0
  23. package/dist/charts/index.d.ts +11 -0
  24. package/dist/charts/index.js +8 -0
  25. package/dist/charts/model.d.ts +225 -0
  26. package/dist/charts/model.js +758 -0
  27. package/dist/charts/outline.d.ts +17 -0
  28. package/dist/charts/outline.js +214 -0
  29. package/dist/charts/render.d.ts +20 -0
  30. package/dist/charts/render.js +1772 -0
  31. package/dist/drawing/arrows.d.ts +37 -0
  32. package/dist/drawing/arrows.js +99 -0
  33. package/dist/drawing/formula.d.ts +40 -0
  34. package/dist/drawing/formula.js +159 -0
  35. package/dist/drawing/geometry.d.ts +61 -0
  36. package/dist/drawing/geometry.js +286 -0
  37. package/dist/drawing/index.d.ts +12 -0
  38. package/dist/drawing/index.js +8 -0
  39. package/dist/drawing/presets.d.ts +72 -0
  40. package/dist/drawing/presets.js +2874 -0
  41. package/dist/fonts/shaping/apply.d.ts +94 -0
  42. package/dist/fonts/shaping/apply.js +349 -0
  43. package/dist/fonts/shaping/arabic.d.ts +28 -0
  44. package/dist/fonts/shaping/arabic.js +235 -0
  45. package/dist/fonts/shaping/buffer.d.ts +53 -0
  46. package/dist/fonts/shaping/buffer.js +95 -0
  47. package/dist/fonts/shaping/gdef.d.ts +30 -0
  48. package/dist/fonts/shaping/gdef.js +66 -0
  49. package/dist/fonts/shaping/gpos.d.ts +86 -0
  50. package/dist/fonts/shaping/gpos.js +428 -0
  51. package/dist/fonts/shaping/gsub.d.ts +65 -0
  52. package/dist/fonts/shaping/gsub.js +313 -0
  53. package/dist/fonts/shaping/index.d.ts +32 -0
  54. package/dist/fonts/shaping/index.js +402 -0
  55. package/dist/fonts/shaping/indic.d.ts +6 -0
  56. package/dist/fonts/shaping/indic.js +805 -0
  57. package/dist/fonts/shaping/layout-common.d.ts +124 -0
  58. package/dist/fonts/shaping/layout-common.js +349 -0
  59. package/dist/fonts/shaping/plan.d.ts +87 -0
  60. package/dist/fonts/shaping/plan.js +152 -0
  61. package/dist/fonts/shaping/scripts.d.ts +26 -0
  62. package/dist/fonts/shaping/scripts.js +160 -0
  63. package/dist/fonts/shaping/shaper.d.ts +23 -0
  64. package/dist/fonts/shaping/shaper.js +5 -0
  65. package/dist/fonts/shaping/unicode.d.ts +34 -0
  66. package/dist/fonts/shaping/unicode.js +138 -0
  67. package/dist/images/bmp.d.ts +9 -0
  68. package/dist/images/bmp.js +234 -0
  69. package/dist/images/gif.d.ts +9 -0
  70. package/dist/images/gif.js +215 -0
  71. package/dist/images/index.d.ts +18 -0
  72. package/dist/images/index.js +63 -0
  73. package/dist/images/png-encode.d.ts +4 -0
  74. package/dist/images/png-encode.js +73 -0
  75. package/dist/images/size.d.ts +10 -0
  76. package/dist/images/size.js +83 -0
  77. package/dist/images/tiff.d.ts +14 -0
  78. package/dist/images/tiff.js +407 -0
  79. package/dist/index.d.ts +7 -2
  80. package/dist/index.js +5 -0
  81. package/dist/layout/bidi.d.ts +18 -0
  82. package/dist/layout/bidi.js +71 -0
  83. package/dist/layout/blocks.d.ts +20 -3
  84. package/dist/layout/blocks.js +88 -58
  85. package/dist/layout/cache.d.ts +31 -0
  86. package/dist/layout/cache.js +38 -0
  87. package/dist/layout/drawing.d.ts +30 -0
  88. package/dist/layout/drawing.js +120 -0
  89. package/dist/layout/engine.d.ts +3 -0
  90. package/dist/layout/engine.js +218 -11
  91. package/dist/layout/index.d.ts +1 -0
  92. package/dist/layout/index.js +1 -0
  93. package/dist/layout/lines.d.ts +63 -5
  94. package/dist/layout/lines.js +245 -20
  95. package/dist/layout/paint.d.ts +25 -3
  96. package/dist/layout/paint.js +26 -10
  97. package/dist/layout/shapes.d.ts +7 -0
  98. package/dist/layout/shapes.js +338 -0
  99. package/dist/layout/style.d.ts +4 -0
  100. package/dist/layout/style.js +2 -0
  101. package/dist/layout/types.d.ts +89 -2
  102. package/dist/math/boxes.d.ts +88 -0
  103. package/dist/math/boxes.js +166 -0
  104. package/dist/math/index.d.ts +29 -0
  105. package/dist/math/index.js +44 -0
  106. package/dist/math/layout.d.ts +13 -0
  107. package/dist/math/layout.js +771 -0
  108. package/dist/math/linear.d.ts +3 -0
  109. package/dist/math/linear.js +152 -0
  110. package/dist/math/omml.d.ts +15 -0
  111. package/dist/math/omml.js +57 -0
  112. package/dist/math/paths.d.ts +61 -0
  113. package/dist/math/paths.js +450 -0
  114. package/dist/math/symbols.d.ts +49 -0
  115. package/dist/math/symbols.js +269 -0
  116. package/dist/math/text.d.ts +53 -0
  117. package/dist/math/text.js +316 -0
  118. package/dist/metafile/canvas.d.ts +80 -0
  119. package/dist/metafile/canvas.js +515 -0
  120. package/dist/metafile/dib.d.ts +27 -0
  121. package/dist/metafile/dib.js +300 -0
  122. package/dist/metafile/emf.d.ts +31 -0
  123. package/dist/metafile/emf.js +591 -0
  124. package/dist/metafile/geometry.d.ts +49 -0
  125. package/dist/metafile/geometry.js +85 -0
  126. package/dist/metafile/index.d.ts +27 -0
  127. package/dist/metafile/index.js +34 -0
  128. package/dist/metafile/reader.d.ts +28 -0
  129. package/dist/metafile/reader.js +59 -0
  130. package/dist/metafile/state.d.ts +118 -0
  131. package/dist/metafile/state.js +172 -0
  132. package/dist/metafile/text.d.ts +17 -0
  133. package/dist/metafile/text.js +130 -0
  134. package/dist/metafile/wmf.d.ts +33 -0
  135. package/dist/metafile/wmf.js +395 -0
  136. package/dist/model/content.d.ts +92 -7
  137. package/dist/model/content.js +251 -17
  138. package/dist/model/document.d.ts +34 -1
  139. package/dist/model/document.js +80 -1
  140. package/dist/model/fields.d.ts +4 -0
  141. package/dist/model/fields.js +10 -0
  142. package/dist/model/numbering.d.ts +2 -0
  143. package/dist/model/numbering.js +22 -0
  144. package/dist/model/props.d.ts +2 -0
  145. package/dist/model/props.js +3 -0
  146. package/dist/opc/index.d.ts +1 -1
  147. package/dist/opc/package.d.ts +34 -0
  148. package/dist/opc/package.js +58 -4
  149. package/dist/pdf/export.js +97 -6
  150. package/dist/pdf/images.d.ts +7 -0
  151. package/dist/pdf/images.js +39 -2
  152. package/dist/render/activity.d.ts +179 -0
  153. package/dist/render/activity.js +1003 -0
  154. package/dist/render/clipboard.d.ts +29 -0
  155. package/dist/render/clipboard.js +401 -0
  156. package/dist/render/comments.d.ts +166 -0
  157. package/dist/render/comments.js +779 -0
  158. package/dist/render/context-menu.d.ts +124 -0
  159. package/dist/render/context-menu.js +525 -0
  160. package/dist/render/editing.d.ts +146 -5
  161. package/dist/render/editing.js +678 -53
  162. package/dist/render/index.d.ts +12 -0
  163. package/dist/render/index.js +11 -0
  164. package/dist/render/navigation-panel.d.ts +76 -0
  165. package/dist/render/navigation-panel.js +431 -0
  166. package/dist/render/selection.js +5 -0
  167. package/dist/render/svg.d.ts +4 -6
  168. package/dist/render/svg.js +69 -7
  169. package/dist/render/toolbar.d.ts +83 -0
  170. package/dist/render/toolbar.js +505 -0
  171. package/dist/render/ui/comment-layout.d.ts +39 -0
  172. package/dist/render/ui/comment-layout.js +79 -0
  173. package/dist/render/ui/icons.d.ts +6 -0
  174. package/dist/render/ui/icons.js +38 -0
  175. package/dist/render/ui/menu.d.ts +38 -0
  176. package/dist/render/ui/menu.js +274 -0
  177. package/dist/render/ui/nav-model.d.ts +11 -0
  178. package/dist/render/ui/nav-model.js +34 -0
  179. package/dist/render/ui/overflow.d.ts +5 -0
  180. package/dist/render/ui/overflow.js +18 -0
  181. package/dist/render/ui/popover.d.ts +18 -0
  182. package/dist/render/ui/popover.js +109 -0
  183. package/dist/render/ui/theme.d.ts +2 -0
  184. package/dist/render/ui/theme.js +29 -0
  185. package/dist/render/undo.d.ts +76 -0
  186. package/dist/render/undo.js +144 -0
  187. package/dist/render/viewer.d.ts +100 -7
  188. package/dist/render/viewer.js +304 -92
  189. package/dist/security/policy.d.ts +3 -0
  190. package/dist/security/policy.js +6 -2
  191. package/dist/security/protection.d.ts +1 -0
  192. package/dist/security/protection.js +1 -1
  193. package/dist/text/bidi-data.d.ts +53 -0
  194. package/dist/text/bidi-data.js +318 -0
  195. package/dist/text/bidi.d.ts +37 -0
  196. package/dist/text/bidi.js +508 -0
  197. package/dist/xml/ns.d.ts +5 -0
  198. package/dist/xml/ns.js +7 -0
  199. package/package.json +9 -2
@@ -0,0 +1,38 @@
1
+ export type MenuItem = '-' | {
2
+ id?: string;
3
+ label: string;
4
+ icon?: import('./icons.js').IconName | string;
5
+ shortcut?: string;
6
+ /** `true` disables the item; a string also shows as the tooltip (the reason). */
7
+ disabled?: boolean | string;
8
+ run?: () => void | Promise<void>;
9
+ items?: MenuItem[];
10
+ };
11
+ export interface MenuHandle {
12
+ close(): void;
13
+ readonly element: HTMLElement;
14
+ }
15
+ export interface MenuOptions {
16
+ label?: string;
17
+ onClose?: () => void;
18
+ /** Where to append the menu (default `document.body`). */
19
+ container?: HTMLElement;
20
+ }
21
+ /** Place a `size` box at `at`, flipping left/up when it would overflow, then clamping to the margin. */
22
+ export declare function positionMenu(at: {
23
+ x: number;
24
+ y: number;
25
+ }, size: {
26
+ w: number;
27
+ h: number;
28
+ }, viewport: {
29
+ w: number;
30
+ h: number;
31
+ }, margin?: number): {
32
+ x: number;
33
+ y: number;
34
+ };
35
+ export declare function openMenu(items: MenuItem[], at: {
36
+ x: number;
37
+ y: number;
38
+ }, options?: MenuOptions): MenuHandle;
@@ -0,0 +1,274 @@
1
+ /**
2
+ * A keyboard-accessible popup menu (the context menu is built on it): grouped rows with icons and
3
+ * right-aligned shortcuts, submenus, disabled items with a reason. `position: fixed`, so `at` is
4
+ * a viewport point such as a contextmenu event's clientX/clientY.
5
+ */
6
+ import { icon, isIconName } from './icons.js';
7
+ import { ensureUiStyle } from './theme.js';
8
+ /** Place a `size` box at `at`, flipping left/up when it would overflow, then clamping to the margin. */
9
+ export function positionMenu(at, size, viewport, margin = 4) {
10
+ let x = at.x;
11
+ let y = at.y;
12
+ if (x + size.w + margin > viewport.w)
13
+ x = at.x - size.w;
14
+ if (y + size.h + margin > viewport.h)
15
+ y = at.y - size.h;
16
+ x = Math.max(margin, Math.min(x, viewport.w - size.w - margin));
17
+ y = Math.max(margin, Math.min(y, viewport.h - size.h - margin));
18
+ return { x, y };
19
+ }
20
+ const STYLE_ID = 'docxkeun-menu-style';
21
+ const CSS = `
22
+ .dk-menu{position:fixed;z-index:2147483000;min-width:220px;max-width:min(360px,calc(100vw - 8px));max-height:calc(100vh - 8px);overflow-y:auto;box-sizing:border-box;padding:4px;background:var(--dk-ui-bg);color:var(--dk-ui-fg);border:1px solid var(--dk-ui-line);border-radius:var(--dk-ui-radius);box-shadow:var(--dk-ui-shadow);font:var(--dk-ui-font);outline:none}
23
+ .dk-menu-item{display:flex;align-items:center;gap:8px;min-height:28px;padding:3px 8px;border-radius:4px;cursor:pointer;white-space:nowrap;outline:none}
24
+ .dk-menu-item:hover,.dk-menu-item:focus{background:var(--dk-ui-hover)}
25
+ .dk-menu-item:focus-visible{outline:2px solid var(--dk-ui-accent);outline-offset:-2px}
26
+ .dk-menu-item[aria-disabled=true]{opacity:.45;cursor:default}
27
+ .dk-menu-item[aria-disabled=true]:hover{background:transparent}
28
+ .dk-menu-icon{flex:none;width:16px;height:16px;display:inline-flex;align-items:center;justify-content:center;color:var(--dk-ui-muted);font-size:13px;line-height:1}
29
+ .dk-menu-label{flex:1 1 auto;overflow:hidden;text-overflow:ellipsis}
30
+ .dk-menu-shortcut{flex:none;margin-left:24px;color:var(--dk-ui-muted);font-size:12px}
31
+ .dk-menu-arrow{flex:none;margin-left:16px;color:var(--dk-ui-muted)}
32
+ .dk-menu-sep{height:1px;margin:4px 6px;background:var(--dk-ui-line)}
33
+ `;
34
+ function ensureMenuStyle() {
35
+ ensureUiStyle();
36
+ if (typeof document === 'undefined' || document.getElementById(STYLE_ID))
37
+ return;
38
+ const style = document.createElement('style');
39
+ style.id = STYLE_ID;
40
+ style.textContent = CSS;
41
+ document.head.appendChild(style);
42
+ }
43
+ const isDisabled = (item) => !!item.disabled;
44
+ let current;
45
+ export function openMenu(items, at, options = {}) {
46
+ current?.close();
47
+ ensureMenuStyle();
48
+ const container = options.container ?? document.body;
49
+ const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null;
50
+ const levels = [];
51
+ let closed = false;
52
+ const close = () => {
53
+ if (closed)
54
+ return;
55
+ closed = true;
56
+ if (current === handle)
57
+ current = undefined;
58
+ document.removeEventListener('mousedown', onOutside, true);
59
+ for (const l of levels)
60
+ l.el.remove();
61
+ levels.length = 0;
62
+ if (previous?.isConnected)
63
+ previous.focus();
64
+ options.onClose?.();
65
+ };
66
+ const onOutside = (e) => {
67
+ const t = e.target;
68
+ if (!levels.some((l) => l.el.contains(t)))
69
+ close();
70
+ };
71
+ const closeFrom = (depth) => {
72
+ while (levels.length > depth) {
73
+ const l = levels.pop();
74
+ l.parentRow?.setAttribute('aria-expanded', 'false');
75
+ l.el.remove();
76
+ }
77
+ };
78
+ const enabledRows = (el) => [...el.querySelectorAll('[role=menuitem]')].filter((r) => r.getAttribute('aria-disabled') !== 'true');
79
+ const activate = (item, row, depth, viaKeyboard) => {
80
+ if (isDisabled(item))
81
+ return;
82
+ if (item.items?.length) {
83
+ openSub(item.items, row, depth, viaKeyboard);
84
+ return;
85
+ }
86
+ if (!item.run)
87
+ return;
88
+ const run = item.run;
89
+ close();
90
+ try {
91
+ const result = run();
92
+ if (result && typeof result.catch === 'function') {
93
+ result.catch((err) => console.error(err));
94
+ }
95
+ }
96
+ catch (err) {
97
+ console.error(err);
98
+ }
99
+ };
100
+ const build = (list, depth, label) => {
101
+ const el = document.createElement('div');
102
+ el.className = 'dk-menu docxkeun-ui-root';
103
+ el.setAttribute('role', 'menu');
104
+ el.tabIndex = -1;
105
+ if (label)
106
+ el.setAttribute('aria-label', label);
107
+ for (const item of list) {
108
+ if (item === '-') {
109
+ const sep = document.createElement('div');
110
+ sep.className = 'dk-menu-sep';
111
+ sep.setAttribute('role', 'separator');
112
+ el.appendChild(sep);
113
+ continue;
114
+ }
115
+ const row = document.createElement('div');
116
+ row.className = 'dk-menu-item';
117
+ row.setAttribute('role', 'menuitem');
118
+ row.tabIndex = -1;
119
+ if (item.id)
120
+ row.dataset.id = item.id;
121
+ if (isDisabled(item)) {
122
+ row.setAttribute('aria-disabled', 'true');
123
+ if (typeof item.disabled === 'string')
124
+ row.title = item.disabled;
125
+ }
126
+ const ic = document.createElement('span');
127
+ ic.className = 'dk-menu-icon';
128
+ if (item.icon) {
129
+ if (isIconName(item.icon))
130
+ ic.innerHTML = icon(item.icon);
131
+ else
132
+ ic.textContent = item.icon;
133
+ }
134
+ ic.setAttribute('aria-hidden', 'true');
135
+ const text = document.createElement('span');
136
+ text.className = 'dk-menu-label';
137
+ text.textContent = item.label;
138
+ row.append(ic, text);
139
+ if (item.shortcut) {
140
+ const sc = document.createElement('span');
141
+ sc.className = 'dk-menu-shortcut';
142
+ sc.textContent = item.shortcut;
143
+ row.appendChild(sc);
144
+ }
145
+ if (item.items?.length) {
146
+ row.setAttribute('aria-haspopup', 'menu');
147
+ row.setAttribute('aria-expanded', 'false');
148
+ const arrow = document.createElement('span');
149
+ arrow.className = 'dk-menu-arrow';
150
+ arrow.setAttribute('aria-hidden', 'true');
151
+ arrow.innerHTML = icon('forward');
152
+ row.appendChild(arrow);
153
+ }
154
+ row.addEventListener('mouseenter', () => {
155
+ if (isDisabled(item))
156
+ return;
157
+ row.focus();
158
+ if (item.items?.length)
159
+ openSub(item.items, row, depth, false);
160
+ else
161
+ closeFrom(depth + 1);
162
+ });
163
+ row.addEventListener('click', (e) => {
164
+ e.stopPropagation();
165
+ activate(item, row, depth, false);
166
+ });
167
+ row._dkItem = item;
168
+ el.appendChild(row);
169
+ }
170
+ el.addEventListener('keydown', (e) => onKey(e, el, depth));
171
+ return el;
172
+ };
173
+ const place = (el, pos, parentRow) => {
174
+ const rect = el.getBoundingClientRect();
175
+ const size = { w: rect.width, h: rect.height };
176
+ const vp = { w: window.innerWidth, h: window.innerHeight };
177
+ let p;
178
+ if (parentRow) {
179
+ const pr = parentRow.getBoundingClientRect();
180
+ p = positionMenu({ x: pr.right, y: pr.top }, size, vp);
181
+ if (p.x < pr.right)
182
+ p = { x: Math.max(4, pr.left - size.w), y: p.y };
183
+ }
184
+ else
185
+ p = positionMenu(pos, size, vp);
186
+ el.style.left = `${p.x}px`;
187
+ el.style.top = `${p.y}px`;
188
+ };
189
+ const show = (list, pos, depth, parentRow, label) => {
190
+ const el = build(list, depth, label);
191
+ el.style.left = '0px';
192
+ el.style.top = '0px';
193
+ container.appendChild(el);
194
+ place(el, pos, parentRow);
195
+ levels.push({ el, parentRow });
196
+ return el;
197
+ };
198
+ function openSub(list, row, depth, focusFirst) {
199
+ if (levels[depth + 1]?.parentRow === row) {
200
+ if (focusFirst)
201
+ enabledRows(levels[depth + 1].el)[0]?.focus();
202
+ return;
203
+ }
204
+ closeFrom(depth + 1);
205
+ row.setAttribute('aria-expanded', 'true');
206
+ const el = show(list, { x: 0, y: 0 }, depth + 1, row);
207
+ if (focusFirst)
208
+ enabledRows(el)[0]?.focus();
209
+ }
210
+ function onKey(e, el, depth) {
211
+ const rows = enabledRows(el);
212
+ const active = rows.indexOf(document.activeElement);
213
+ const focusAt = (i) => rows[(i + rows.length) % rows.length]?.focus();
214
+ const stop = () => {
215
+ e.preventDefault();
216
+ e.stopPropagation();
217
+ };
218
+ switch (e.key) {
219
+ case 'ArrowDown':
220
+ stop();
221
+ focusAt(active < 0 ? 0 : active + 1);
222
+ break;
223
+ case 'ArrowUp':
224
+ stop();
225
+ focusAt(active < 0 ? rows.length - 1 : active - 1);
226
+ break;
227
+ case 'Home':
228
+ stop();
229
+ focusAt(0);
230
+ break;
231
+ case 'End':
232
+ stop();
233
+ focusAt(rows.length - 1);
234
+ break;
235
+ case 'ArrowRight': {
236
+ const row = rows[active];
237
+ const item = row && row._dkItem;
238
+ if (row && item?.items?.length) {
239
+ stop();
240
+ openSub(item.items, row, depth, true);
241
+ }
242
+ break;
243
+ }
244
+ case 'ArrowLeft':
245
+ if (depth > 0) {
246
+ stop();
247
+ const parentRow = levels[depth].parentRow;
248
+ closeFrom(depth);
249
+ parentRow?.focus();
250
+ }
251
+ break;
252
+ case 'Enter':
253
+ case ' ': {
254
+ const row = rows[active];
255
+ if (row) {
256
+ stop();
257
+ activate(row._dkItem, row, depth, true);
258
+ }
259
+ break;
260
+ }
261
+ case 'Escape':
262
+ case 'Tab':
263
+ stop();
264
+ close();
265
+ break;
266
+ }
267
+ }
268
+ const root = show(items, at, 0, undefined, options.label);
269
+ const handle = { close, element: root };
270
+ current = handle;
271
+ document.addEventListener('mousedown', onOutside, true);
272
+ (enabledRows(root)[0] ?? root).focus();
273
+ return handle;
274
+ }
@@ -0,0 +1,11 @@
1
+ /** Pure helpers behind the navigation panel: searching the lists and finding the current heading. */
2
+ import { type DocLocation, type NavTarget } from '../navigation.js';
3
+ /** Lower-case and strip accents, so "é" matches "e" and "Pay" matches "payment". */
4
+ export declare function fold(text: string): string;
5
+ /** Targets whose label (or, for links, URL or anchor) contains the query, ignoring case and accents. */
6
+ export declare function filterTargets(list: NavTarget[], query: string): NavTarget[];
7
+ /**
8
+ * The last heading at or above a location (`tolerance` points below it still count, so a heading
9
+ * that a jump left just under the viewport top is the current one). `headings` is in document order.
10
+ */
11
+ export declare function currentHeading(headings: NavTarget[], at: DocLocation, tolerance?: number): NavTarget | undefined;
@@ -0,0 +1,34 @@
1
+ /** Pure helpers behind the navigation panel: searching the lists and finding the current heading. */
2
+ import { compareLocations } from '../navigation.js';
3
+ /** Lower-case and strip accents, so "é" matches "e" and "Pay" matches "payment". */
4
+ export function fold(text) {
5
+ return text.normalize('NFD').replace(/\p{M}+/gu, '').toLowerCase();
6
+ }
7
+ /** Targets whose label (or, for links, URL or anchor) contains the query, ignoring case and accents. */
8
+ export function filterTargets(list, query) {
9
+ const q = fold(query.trim());
10
+ if (!q)
11
+ return list;
12
+ return list.filter((t) => {
13
+ if (fold(t.label).includes(q))
14
+ return true;
15
+ const url = t.link?.url;
16
+ const anchor = t.link?.anchor;
17
+ return (!!url && fold(url).includes(q)) || (!!anchor && fold(anchor).includes(q));
18
+ });
19
+ }
20
+ /**
21
+ * The last heading at or above a location (`tolerance` points below it still count, so a heading
22
+ * that a jump left just under the viewport top is the current one). `headings` is in document order.
23
+ */
24
+ export function currentHeading(headings, at, tolerance = 0) {
25
+ const limit = { page: at.page, x: at.x, y: at.y + tolerance };
26
+ let found;
27
+ for (const h of headings) {
28
+ if (compareLocations(h, limit) <= 0)
29
+ found = h;
30
+ else
31
+ break;
32
+ }
33
+ return found;
34
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * How many leading groups stay in a toolbar row of `available` pixels; the rest go to the "more"
3
+ * menu. When everything fits, no "more" button is needed and `moreButtonWidth` is not reserved.
4
+ */
5
+ export declare function fitGroups(groupWidths: number[], available: number, moreButtonWidth: number): number;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * How many leading groups stay in a toolbar row of `available` pixels; the rest go to the "more"
3
+ * menu. When everything fits, no "more" button is needed and `moreButtonWidth` is not reserved.
4
+ */
5
+ export function fitGroups(groupWidths, available, moreButtonWidth) {
6
+ const total = groupWidths.reduce((a, b) => a + b, 0);
7
+ if (total <= available)
8
+ return groupWidths.length;
9
+ let used = moreButtonWidth;
10
+ let count = 0;
11
+ for (const w of groupWidths) {
12
+ if (used + w > available)
13
+ break;
14
+ used += w;
15
+ count++;
16
+ }
17
+ return count;
18
+ }
@@ -0,0 +1,18 @@
1
+ export interface PromptOptions {
2
+ title: string;
3
+ value?: string;
4
+ placeholder?: string;
5
+ /** Viewport point (the popover is `position: fixed`). */
6
+ at: {
7
+ x: number;
8
+ y: number;
9
+ };
10
+ /** Return a message to show and keep the popover open, or undefined to accept. */
11
+ validate?: (v: string) => string | undefined;
12
+ /** Where to append the popover (default `document.body`). */
13
+ container?: HTMLElement;
14
+ /** Aborting closes the popover as if cancelled (resolves `undefined`). */
15
+ signal?: AbortSignal;
16
+ }
17
+ /** Resolves the entered text, or `undefined` on Esc, Cancel or an outside click. */
18
+ export declare function promptPopover(options: PromptOptions): Promise<string | undefined>;
@@ -0,0 +1,109 @@
1
+ /** A small titled popover with a text input (OK/Cancel), used for link and bookmark prompts. */
2
+ import { ensureUiStyle } from './theme.js';
3
+ import { positionMenu } from './menu.js';
4
+ const STYLE_ID = 'docxkeun-popover-style';
5
+ const CSS = `
6
+ .dk-popover{position:fixed;z-index:2147483000;width:260px;max-width:calc(100vw - 8px);box-sizing:border-box;padding:10px;display:flex;flex-direction:column;gap:8px;background:var(--dk-ui-bg);color:var(--dk-ui-fg);border:1px solid var(--dk-ui-line);border-radius:var(--dk-ui-radius);box-shadow:var(--dk-ui-shadow);font:var(--dk-ui-font)}
7
+ .dk-popover-title{font-weight:600}
8
+ .dk-popover input{height:28px;box-sizing:border-box;width:100%;padding:0 8px;border:1px solid var(--dk-ui-line);border-radius:4px;background:var(--dk-ui-bg);color:inherit;font:inherit}
9
+ .dk-popover input:focus-visible,.dk-popover button:focus-visible{outline:2px solid var(--dk-ui-accent);outline-offset:1px}
10
+ .dk-popover-error{color:#b3261e;font-size:12px}
11
+ .dk-popover-error:empty{display:none}
12
+ .dk-popover-actions{display:flex;justify-content:flex-end;gap:6px}
13
+ .dk-popover button{height:28px;padding:0 12px;border:1px solid var(--dk-ui-line);border-radius:4px;background:var(--dk-ui-bg);color:inherit;font:inherit;cursor:pointer}
14
+ .dk-popover button:hover{background:var(--dk-ui-hover)}
15
+ .dk-popover button[data-role=ok]{background:var(--dk-ui-accent);border-color:var(--dk-ui-accent);color:#fff}
16
+ `;
17
+ function ensureStyle() {
18
+ ensureUiStyle();
19
+ if (document.getElementById(STYLE_ID))
20
+ return;
21
+ const style = document.createElement('style');
22
+ style.id = STYLE_ID;
23
+ style.textContent = CSS;
24
+ document.head.appendChild(style);
25
+ }
26
+ /** Resolves the entered text, or `undefined` on Esc, Cancel or an outside click. */
27
+ export function promptPopover(options) {
28
+ if (options.signal?.aborted)
29
+ return Promise.resolve(undefined);
30
+ ensureStyle();
31
+ const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null;
32
+ const pop = document.createElement('div');
33
+ pop.className = 'dk-popover docxkeun-ui-root';
34
+ pop.setAttribute('role', 'dialog');
35
+ pop.setAttribute('aria-label', options.title);
36
+ const title = document.createElement('div');
37
+ title.className = 'dk-popover-title';
38
+ title.textContent = options.title;
39
+ const input = document.createElement('input');
40
+ input.type = 'text';
41
+ input.value = options.value ?? '';
42
+ if (options.placeholder)
43
+ input.placeholder = options.placeholder;
44
+ input.setAttribute('aria-label', options.title);
45
+ const error = document.createElement('div');
46
+ error.className = 'dk-popover-error';
47
+ error.setAttribute('role', 'alert');
48
+ const actions = document.createElement('div');
49
+ actions.className = 'dk-popover-actions';
50
+ const cancel = document.createElement('button');
51
+ cancel.type = 'button';
52
+ cancel.textContent = 'Cancel';
53
+ cancel.dataset.role = 'cancel';
54
+ const ok = document.createElement('button');
55
+ ok.type = 'button';
56
+ ok.textContent = 'OK';
57
+ ok.dataset.role = 'ok';
58
+ actions.append(cancel, ok);
59
+ pop.append(title, input, error, actions);
60
+ pop.style.left = '0px';
61
+ pop.style.top = '0px';
62
+ (options.container ?? document.body).appendChild(pop);
63
+ const rect = pop.getBoundingClientRect();
64
+ const p = positionMenu(options.at, { w: rect.width, h: rect.height }, { w: window.innerWidth, h: window.innerHeight });
65
+ pop.style.left = `${p.x}px`;
66
+ pop.style.top = `${p.y}px`;
67
+ return new Promise((resolve) => {
68
+ const finish = (value) => {
69
+ document.removeEventListener('mousedown', onOutside, true);
70
+ options.signal?.removeEventListener('abort', onAbort);
71
+ pop.remove();
72
+ if (previous?.isConnected)
73
+ previous.focus();
74
+ resolve(value);
75
+ };
76
+ const submit = () => {
77
+ const message = options.validate?.(input.value);
78
+ if (message) {
79
+ error.textContent = message;
80
+ input.setAttribute('aria-invalid', 'true');
81
+ input.focus();
82
+ return;
83
+ }
84
+ finish(input.value);
85
+ };
86
+ const onOutside = (e) => {
87
+ if (!pop.contains(e.target))
88
+ finish(undefined);
89
+ };
90
+ const onAbort = () => finish(undefined);
91
+ document.addEventListener('mousedown', onOutside, true);
92
+ options.signal?.addEventListener('abort', onAbort);
93
+ pop.addEventListener('keydown', (e) => {
94
+ if (e.key === 'Escape') {
95
+ e.preventDefault();
96
+ e.stopPropagation();
97
+ finish(undefined);
98
+ }
99
+ else if (e.key === 'Enter' && e.target === input) {
100
+ e.preventDefault();
101
+ submit();
102
+ }
103
+ });
104
+ cancel.addEventListener('click', () => finish(undefined));
105
+ ok.addEventListener('click', submit);
106
+ input.focus();
107
+ input.select();
108
+ });
109
+ }
@@ -0,0 +1,2 @@
1
+ /** Inject the shared variables and base classes once. */
2
+ export declare function ensureUiStyle(): void;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Shared look of the viewer's chrome (toolbar, navigation, comments, menu). Apps theme it by
3
+ * setting the public `--docxkeun-ui-*` variables (bg, fg, muted, line, hover, accent,
4
+ * accent-soft, radius, shadow, font) on any ancestor, e.g. the container or `:root`. The library
5
+ * never declares those; it reads them into internal `--dk-ui-*` variables that components use.
6
+ */
7
+ const STYLE_ID = 'docxkeun-ui-style';
8
+ const CSS = `
9
+ .docxkeun-frame,.docxkeun-viewer,.docxkeun-ui-root{--dk-ui-bg:var(--docxkeun-ui-bg,var(--docxkeun-toolbar-bg,#f6f6f4));--dk-ui-fg:var(--docxkeun-ui-fg,var(--docxkeun-toolbar-fg,#222));--dk-ui-muted:var(--docxkeun-ui-muted,#5f6368);--dk-ui-line:var(--docxkeun-ui-line,var(--docxkeun-toolbar-border,#dcdcd6));--dk-ui-hover:var(--docxkeun-ui-hover,var(--docxkeun-toolbar-hover,#e8e8e3));--dk-ui-accent:var(--docxkeun-ui-accent,#2e74b5);--dk-ui-accent-soft:var(--docxkeun-ui-accent-soft,var(--docxkeun-toolbar-active,#dde6f2));--dk-ui-radius:var(--docxkeun-ui-radius,6px);--dk-ui-shadow:var(--docxkeun-ui-shadow,0 1px 3px rgba(60,64,67,.3),0 4px 8px rgba(60,64,67,.15));--dk-ui-font:var(--docxkeun-ui-font,13px/1.4 system-ui,sans-serif)}
10
+ .docxkeun-frame{display:flex;flex-direction:column;width:100%;height:100%;min-height:0;box-sizing:border-box}
11
+ .docxkeun-toolbar-slot{flex:none}
12
+ .docxkeun-toolbar-slot:empty{display:none}
13
+ .docxkeun-body{display:flex;flex-direction:row;flex:1 1 auto;min-height:0;min-width:0}
14
+ .docxkeun-body>.docxkeun-viewer{flex:1 1 auto;min-width:0;height:auto}
15
+ .docxkeun-ui-btn{display:inline-flex;align-items:center;justify-content:center;height:28px;min-width:28px;border:1px solid transparent;border-radius:4px;background:transparent;color:var(--dk-ui-fg);font:var(--dk-ui-font);padding:0 6px;cursor:pointer}
16
+ .docxkeun-ui-btn:hover:not(:disabled){background:var(--dk-ui-hover)}
17
+ .docxkeun-ui-btn:focus-visible{outline:2px solid var(--dk-ui-accent);outline-offset:1px}
18
+ .docxkeun-ui-btn[aria-pressed=true]{background:var(--dk-ui-accent-soft)}
19
+ .docxkeun-ui-btn:disabled{opacity:.4;cursor:default}
20
+ `;
21
+ /** Inject the shared variables and base classes once. */
22
+ export function ensureUiStyle() {
23
+ if (typeof document === 'undefined' || document.getElementById(STYLE_ID))
24
+ return;
25
+ const style = document.createElement('style');
26
+ style.id = STYLE_ID;
27
+ style.textContent = CSS;
28
+ document.head.appendChild(style);
29
+ }
@@ -0,0 +1,76 @@
1
+ import type { DocumentCheckpoint, DocxDocument } from '../model/document.js';
2
+ import type { XmlElement } from '../xml/index.js';
3
+ import type { Caret, EditStory } from './editing.js';
4
+ export type UndoEntry = {
5
+ kind: 'story';
6
+ part: string;
7
+ story: EditStory;
8
+ root: XmlElement;
9
+ container?: number;
10
+ anchor: Caret;
11
+ focus: Caret;
12
+ } | {
13
+ kind: 'package';
14
+ checkpoint: DocumentCheckpoint;
15
+ story: EditStory;
16
+ container?: number;
17
+ anchor: Caret;
18
+ focus: Caret;
19
+ label: string;
20
+ };
21
+ export declare class UndoHistory {
22
+ private readonly limit;
23
+ /**
24
+ * How long (ms) a transaction waits for the one ahead of it before it gives up with an
25
+ * `EditError`. A change started after an `await` inside a transaction's own `run` waits for
26
+ * that run, which may be waiting for it: this turns that hang into an error.
27
+ */
28
+ static queueTimeout: number;
29
+ private undoStack;
30
+ private redoStack;
31
+ private tail;
32
+ private pending;
33
+ /** A transaction's `run` is executing synchronously (before its first `await` takes effect). */
34
+ private joinDepth;
35
+ constructor(limit?: number);
36
+ /** Record a new step; anything that could be redone is dropped. */
37
+ push(e: UndoEntry): void;
38
+ /**
39
+ * Step back. `current` builds the entry for the state being left (of the same kind as the entry
40
+ * being restored); it goes on the redo stack. Returns the entry to restore.
41
+ */
42
+ undo(current: (restoring: UndoEntry) => UndoEntry): UndoEntry | undefined;
43
+ redo(current: (restoring: UndoEntry) => UndoEntry): UndoEntry | undefined;
44
+ clear(): void;
45
+ /** A transaction is running or waiting; undo and redo do nothing meanwhile. */
46
+ get busy(): boolean;
47
+ /**
48
+ * A transaction's `run` is executing synchronously: a transaction started now is nested in it
49
+ * and joins it. Later in the run (after an `await`) this is false, as there is no telling a
50
+ * nested call from an unrelated one.
51
+ */
52
+ get joinable(): boolean;
53
+ get canUndo(): boolean;
54
+ get canRedo(): boolean;
55
+ /**
56
+ * Run a whole-document action as one undo step. A failure puts the document back as it was
57
+ * and records nothing.
58
+ *
59
+ * Transactions run one at a time: one requested while another is running waits for it, so it
60
+ * checkpoints a settled document and is an undo step of its own, and the other's failure does
61
+ * not touch it. If the wait passes `UndoHistory.queueTimeout`, it gives up with an `EditError`
62
+ * without running (nothing changed, nothing recorded).
63
+ *
64
+ * A transaction requested while another one's `run` is executing synchronously (a nested call
65
+ * before the run's first `await`) joins it: it runs at once as part of that step, with no
66
+ * checkpoint or entry of its own, and a failure goes to the outer `run` (which restores the
67
+ * document if it lets the error through). Later in the run, changes go through the run's own
68
+ * means (the editor it was given), since waiting for the outer step from inside it cannot end.
69
+ */
70
+ transaction<T>(doc: DocxDocument, label: string, run: () => T | Promise<T>, where: {
71
+ story: EditStory;
72
+ container?: number;
73
+ anchor: Caret;
74
+ focus: Caret;
75
+ }): Promise<T>;
76
+ }