docxkeun 0.1.3 → 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 +48 -0
  2. package/README.md +137 -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
@@ -0,0 +1,505 @@
1
+ import { icon } from './ui/icons.js';
2
+ import { fitGroups } from './ui/overflow.js';
3
+ import { positionMenu } from './ui/menu.js';
4
+ import { ensureUiStyle } from './ui/theme.js';
5
+ const STYLE_ID = 'docxkeun-toolbar-style';
6
+ const CSS = `
7
+ .docxkeun-toolbar{position:relative;background:var(--dk-ui-bg);color:var(--dk-ui-fg);border-bottom:1px solid var(--dk-ui-line);font:var(--dk-ui-font);box-sizing:border-box}
8
+ .docxkeun-toolbar[hidden]{display:none}
9
+ .docxkeun-toolbar .dk-tb-row{display:flex;flex-wrap:nowrap;align-items:center;gap:2px;padding:4px 6px;min-height:36px;box-sizing:border-box;overflow:hidden}
10
+ .docxkeun-toolbar.dk-collapsed .dk-tb-row,.docxkeun-toolbar.dk-collapsed .dk-tb-pop{display:none}
11
+ .docxkeun-toolbar .dk-tb-group{display:inline-flex;align-items:center;gap:2px;flex:none}
12
+ .docxkeun-toolbar .dk-tb-sep{width:1px;height:20px;background:var(--dk-ui-line);margin:0 4px}
13
+ .docxkeun-toolbar .dk-tb-spacer{flex:1 1 auto;min-width:0}
14
+ .docxkeun-toolbar button,.docxkeun-toolbar select{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}
15
+ .docxkeun-toolbar select{border-color:var(--dk-ui-line);background:var(--dk-ui-bg);display:inline-block}
16
+ .docxkeun-toolbar button:hover:not(:disabled){background:var(--dk-ui-hover)}
17
+ .docxkeun-toolbar button:focus-visible,.docxkeun-toolbar select:focus-visible,.docxkeun-toolbar input:focus-visible+button{outline:2px solid var(--dk-ui-accent);outline-offset:1px}
18
+ .docxkeun-toolbar button[aria-pressed=true]{background:var(--dk-ui-accent-soft)}
19
+ .docxkeun-toolbar button:disabled,.docxkeun-toolbar select:disabled{opacity:.4;cursor:default}
20
+ .docxkeun-toolbar .color{position:relative;display:inline-flex;align-items:center}
21
+ .docxkeun-toolbar .color input{position:absolute;inset:0;opacity:0;cursor:pointer;width:100%;height:100%}
22
+ .docxkeun-toolbar .swatch{display:block;height:3px;margin-top:1px}
23
+ .docxkeun-toolbar .story{font-size:12px;color:var(--dk-ui-muted);padding:0 6px;white-space:nowrap}
24
+ .docxkeun-toolbar .dk-tb-more[hidden]{display:none}
25
+ .docxkeun-toolbar .dk-tb-pop{position:fixed;left:0;top:0;z-index:20;display:flex;flex-direction:column;gap:4px;padding:6px;background:var(--dk-ui-bg);border:1px solid var(--dk-ui-line);border-radius:var(--dk-ui-radius);box-shadow:var(--dk-ui-shadow)}
26
+ .docxkeun-toolbar .dk-tb-pop[hidden]{display:none}
27
+ .docxkeun-toolbar .dk-tb-pop .dk-tb-sep{display:none}
28
+ .docxkeun-toolbar .dk-tb-strip{display:none;width:100%;height:18px;min-height:0;border-radius:0;font-size:11px;color:var(--dk-ui-muted);gap:4px}
29
+ .docxkeun-toolbar.dk-collapsed .dk-tb-strip{display:flex}
30
+ .docxkeun-toolbar.dk-collapsed .dk-tb-strip[hidden]{display:none}
31
+ .docxkeun-toolbar .dk-tb-strip svg{width:12px;height:12px}
32
+ `;
33
+ function ensureStyle() {
34
+ ensureUiStyle();
35
+ if (typeof document === 'undefined' || document.getElementById(STYLE_ID))
36
+ return;
37
+ const style = document.createElement('style');
38
+ style.id = STYLE_ID;
39
+ style.textContent = CSS;
40
+ document.head.appendChild(style);
41
+ }
42
+ function opt(text, value) {
43
+ const o = document.createElement('option');
44
+ o.value = value;
45
+ o.textContent = text;
46
+ return o;
47
+ }
48
+ const SIZES = [8, 9, 10, 10.5, 11, 12, 14, 16, 18, 20, 24, 28, 36, 48, 72];
49
+ const HIGHLIGHTS = [
50
+ ['none', 'No highlight'],
51
+ ['yellow', 'Yellow'],
52
+ ['green', 'Bright green'],
53
+ ['cyan', 'Turquoise'],
54
+ ['magenta', 'Pink'],
55
+ ['lightGray', 'Gray'],
56
+ ];
57
+ const STORY_NAMES = { header: 'Editing header', footer: 'Editing footer', footnote: 'Editing footnote', endnote: 'Editing endnote', textbox: 'Editing text box' };
58
+ /** Controls the toolbar of a viewer (`viewer.toolbar`); the DOM is built on the first `show()`. */
59
+ export class ToolbarController {
60
+ viewer;
61
+ options;
62
+ el;
63
+ shown = false;
64
+ fold = false;
65
+ restore = false;
66
+ format;
67
+ controls = new Map();
68
+ cleanup = [];
69
+ groups = [];
70
+ row;
71
+ pop;
72
+ moreWrap;
73
+ moreButton;
74
+ foldButton;
75
+ strip;
76
+ storyLabel;
77
+ resizeObserver;
78
+ constructor(viewer, options = {}) {
79
+ this.viewer = viewer;
80
+ this.options = options;
81
+ this.fold = !!options.collapsed;
82
+ }
83
+ get element() {
84
+ return this.el;
85
+ }
86
+ get visible() {
87
+ return this.shown;
88
+ }
89
+ get collapsed() {
90
+ return this.fold;
91
+ }
92
+ set collapsed(value) {
93
+ if (value === this.fold)
94
+ return;
95
+ this.fold = value;
96
+ if (!this.el)
97
+ return;
98
+ this.applyFold();
99
+ this.announce();
100
+ }
101
+ show() {
102
+ if (!this.el)
103
+ this.build();
104
+ this.restore = false;
105
+ if (this.shown)
106
+ return;
107
+ this.shown = true;
108
+ this.el.hidden = false;
109
+ this.announce();
110
+ this.relayout();
111
+ }
112
+ hide() {
113
+ this.restore = false;
114
+ if (!this.shown)
115
+ return;
116
+ this.shown = false;
117
+ this.closeMenu();
118
+ if (this.el)
119
+ this.el.hidden = true;
120
+ this.announce();
121
+ }
122
+ /** Show the toolbar as soon as editing turns on (used when it was asked for while read-only). */
123
+ deferShow() {
124
+ this.restore = true;
125
+ }
126
+ /** The viewer's `setEditable`: hide with editing, and bring the previous state back with it. */
127
+ setEditable(on) {
128
+ if (on) {
129
+ if (this.restore)
130
+ this.show();
131
+ }
132
+ else {
133
+ const was = this.shown || this.restore;
134
+ this.hide();
135
+ this.restore = was;
136
+ }
137
+ }
138
+ announce() {
139
+ this.viewer.emit('chromechange', { part: 'toolbar', visible: this.shown, collapsed: this.fold });
140
+ }
141
+ get editing() {
142
+ return this.viewer.editing;
143
+ }
144
+ build() {
145
+ ensureStyle();
146
+ const el = (this.el = document.createElement('div'));
147
+ el.className = 'docxkeun-toolbar docxkeun-ui-root';
148
+ el.setAttribute('role', 'toolbar');
149
+ el.setAttribute('aria-label', 'Formatting');
150
+ // Clicking a button must not take focus (and the selection) away from the page.
151
+ el.addEventListener('mousedown', (e) => {
152
+ if (e.target.closest('button'))
153
+ e.preventDefault();
154
+ });
155
+ el.addEventListener('keydown', (e) => this.onKey(e));
156
+ const row = (this.row = document.createElement('div'));
157
+ row.className = 'dk-tb-row';
158
+ const edit = () => this.editing;
159
+ this.group('history', row, () => {
160
+ this.button('undo', 'Undo', icon('undo'), () => edit()?.undo());
161
+ this.button('redo', 'Redo', icon('redo'), () => edit()?.redo());
162
+ });
163
+ this.group('text', row, () => {
164
+ this.select('style', 'Paragraph style', [], (v) => void edit()?.setStyle(v));
165
+ this.select('size', 'Font size', SIZES.map((n) => [String(n), String(n)]), (v) => edit()?.format({ size: Number(v) }));
166
+ });
167
+ this.group('font', row, () => {
168
+ this.button('bold', 'Bold', '<b>B</b>', () => edit()?.toggle('bold'));
169
+ this.button('italic', 'Italic', '<i style="font-family:serif">I</i>', () => edit()?.toggle('italic'));
170
+ this.button('underline', 'Underline', '<u>U</u>', () => edit()?.toggle('underline'));
171
+ this.button('strike', 'Strikethrough', '<s>S</s>', () => edit()?.toggle('strike'));
172
+ this.colorInput();
173
+ this.select('highlight', 'Highlight', HIGHLIGHTS, (v) => edit()?.format({ highlight: v }));
174
+ });
175
+ this.group('align', row, () => {
176
+ const names = [['left', 'alignLeft'], ['center', 'alignCenter'], ['right', 'alignRight'], ['justify', 'justify']];
177
+ for (const [align, name] of names)
178
+ this.button(`align-${align}`, `Align ${align}`, icon(name), () => void edit()?.formatParagraphs({ align }));
179
+ });
180
+ this.group('list', row, () => {
181
+ this.button('bullets', 'Bulleted list', icon('bullets'), () => void edit()?.setList(this.format?.list === 'bullet' ? 'none' : 'bullet'));
182
+ this.button('numbers', 'Numbered list', icon('numbers'), () => void edit()?.setList(this.format?.list === 'number' ? 'none' : 'decimal'));
183
+ this.button('outdent', 'Decrease indent', icon('outdent'), () => void edit()?.indent(-1));
184
+ this.button('indent', 'Increase indent', icon('indent'), () => void edit()?.indent(1));
185
+ });
186
+ this.group('direction', row, () => {
187
+ this.button('ltr', 'Left-to-right text', icon('ltr'), () => void edit()?.formatParagraphs({ direction: 'ltr' }));
188
+ this.button('rtl', 'Right-to-left text', icon('rtl'), () => void edit()?.formatParagraphs({ direction: 'rtl' }));
189
+ });
190
+ this.group('track', row, () => {
191
+ this.button('track', 'Track changes', `${icon('track')}<span style="margin-left:4px">Track</span>`, () => {
192
+ const ed = edit();
193
+ if (ed)
194
+ ed.trackChanges = !ed.trackChanges;
195
+ });
196
+ });
197
+ this.moreWrap = document.createElement('span');
198
+ this.moreWrap.className = 'dk-tb-more';
199
+ this.moreWrap.hidden = true;
200
+ this.moreButton = this.rawButton('Show more formatting', icon('more'), () => this.toggleMenu());
201
+ this.moreButton.setAttribute('aria-haspopup', 'true');
202
+ this.moreButton.setAttribute('aria-expanded', 'false');
203
+ this.moreWrap.appendChild(this.moreButton);
204
+ const spacer = document.createElement('span');
205
+ spacer.className = 'dk-tb-spacer';
206
+ this.storyLabel = document.createElement('span');
207
+ this.storyLabel.className = 'story';
208
+ this.storyLabel.setAttribute('aria-live', 'polite');
209
+ this.foldButton = this.rawButton('Hide toolbar', icon('fold'), () => (this.collapsed = true));
210
+ row.append(this.moreWrap, spacer, this.storyLabel, this.foldButton);
211
+ this.pop = document.createElement('div');
212
+ this.pop.className = 'dk-tb-pop';
213
+ this.pop.setAttribute('role', 'group');
214
+ this.pop.setAttribute('aria-label', 'More formatting');
215
+ this.pop.hidden = true;
216
+ this.strip = document.createElement('button');
217
+ this.strip.type = 'button';
218
+ this.strip.className = 'dk-tb-strip';
219
+ this.strip.setAttribute('aria-label', 'Show toolbar');
220
+ this.strip.innerHTML = `${icon('unfold')}<span>Show toolbar</span>`;
221
+ this.strip.addEventListener('click', () => (this.collapsed = false));
222
+ el.append(row, this.pop, this.strip);
223
+ el.hidden = true;
224
+ this.applyFold();
225
+ const container = this.options.container;
226
+ if (container)
227
+ container.appendChild(el);
228
+ else
229
+ this.viewer.frame().toolbarSlot.appendChild(el);
230
+ this.cleanup.push(this.viewer.on('caretchange', ({ format }) => this.update(format)));
231
+ this.cleanup.push(this.viewer.on('layout', () => this.fillStyles()));
232
+ const onDoc = (e) => {
233
+ if (!this.pop.hidden && !el.contains(e.target))
234
+ this.closeMenu();
235
+ };
236
+ document.addEventListener('mousedown', onDoc);
237
+ this.cleanup.push(() => {
238
+ window.removeEventListener('resize', this.onWinResize);
239
+ window.removeEventListener('scroll', this.onWinScroll, true);
240
+ });
241
+ this.cleanup.push(() => document.removeEventListener('mousedown', onDoc));
242
+ if (typeof ResizeObserver !== 'undefined') {
243
+ this.resizeObserver = new ResizeObserver(() => this.relayout());
244
+ this.resizeObserver.observe(el);
245
+ }
246
+ this.fillStyles();
247
+ this.update(this.editing?.caretFormat());
248
+ this.roving();
249
+ }
250
+ applyFold() {
251
+ const el = this.el;
252
+ el.classList.toggle('dk-collapsed', this.fold);
253
+ this.strip.hidden = !this.fold;
254
+ this.foldButton.setAttribute('aria-expanded', String(!this.fold));
255
+ this.closeMenu();
256
+ if (!this.fold)
257
+ this.relayout();
258
+ }
259
+ group(name, parent, fill) {
260
+ const g = document.createElement('span');
261
+ g.className = 'dk-tb-group';
262
+ g.dataset.group = name;
263
+ if (this.groups.length) {
264
+ const sep = document.createElement('span');
265
+ sep.className = 'dk-tb-sep';
266
+ sep.setAttribute('role', 'separator');
267
+ g.appendChild(sep);
268
+ }
269
+ this.groups.push(g);
270
+ this.target = g;
271
+ fill();
272
+ this.target = undefined;
273
+ parent.appendChild(g);
274
+ }
275
+ target;
276
+ rawButton(label, html, run) {
277
+ const b = document.createElement('button');
278
+ b.type = 'button';
279
+ b.innerHTML = html;
280
+ b.title = label;
281
+ b.dataset.nav = '';
282
+ b.setAttribute('aria-label', label);
283
+ b.addEventListener('click', run);
284
+ return b;
285
+ }
286
+ button(key, label, html, run) {
287
+ const b = this.rawButton(label, html, () => {
288
+ run();
289
+ this.closeMenu();
290
+ this.editing?.focus();
291
+ });
292
+ this.controls.set(key, b);
293
+ this.target?.appendChild(b);
294
+ }
295
+ select(key, label, options, run) {
296
+ const s = document.createElement('select');
297
+ s.title = label;
298
+ s.dataset.nav = '';
299
+ s.setAttribute('aria-label', label);
300
+ for (const [value, text] of options)
301
+ s.add(opt(text, value));
302
+ s.addEventListener('change', () => {
303
+ run(s.value);
304
+ this.editing?.focus();
305
+ });
306
+ this.controls.set(key, s);
307
+ this.target?.appendChild(s);
308
+ }
309
+ colorInput() {
310
+ const wrap = document.createElement('label');
311
+ wrap.className = 'color';
312
+ wrap.title = 'Text colour';
313
+ const face = document.createElement('button');
314
+ face.type = 'button';
315
+ face.tabIndex = -1;
316
+ face.innerHTML = '<span style="font-weight:600">A</span><span class="swatch" style="background:#c00000"></span>';
317
+ face.setAttribute('aria-hidden', 'true');
318
+ const input = document.createElement('input');
319
+ input.type = 'color';
320
+ input.value = '#c00000';
321
+ input.dataset.nav = '';
322
+ input.setAttribute('aria-label', 'Text colour');
323
+ input.addEventListener('change', () => {
324
+ face.querySelector('.swatch').style.background = input.value;
325
+ this.editing?.format({ color: input.value.slice(1).toUpperCase() });
326
+ this.editing?.focus();
327
+ });
328
+ wrap.append(face, input);
329
+ this.controls.set('color', input);
330
+ this.target?.appendChild(wrap);
331
+ }
332
+ /** Offer the document's own Normal, Title and Heading styles (or the ones given). */
333
+ fillStyles() {
334
+ const select = this.controls.get('style');
335
+ const doc = this.viewer.document;
336
+ let styles = this.options.styles;
337
+ if (!styles) {
338
+ const wanted = ['Normal', 'Title', 'Subtitle', 'Heading1', 'Heading2', 'Heading3', 'Heading4', 'Quote', 'ListParagraph'];
339
+ styles = wanted.filter((id) => !doc || id === 'Normal' || doc.styles.get(id)).map((id) => ({ id, name: doc?.styles.get(id)?.name?.replace(/^heading/, 'Heading') ?? id }));
340
+ }
341
+ const key = styles.map((s) => s.id).join();
342
+ if (select.dataset.key === key)
343
+ return;
344
+ select.dataset.key = key;
345
+ select.replaceChildren(...styles.map((s) => opt(s.name, s.id)));
346
+ }
347
+ update(format) {
348
+ this.format = format;
349
+ const on = !!format;
350
+ for (const c of this.controls.values())
351
+ c.disabled = !on;
352
+ const pressed = (key, value) => this.controls.get(key)?.setAttribute('aria-pressed', String(value));
353
+ if (!format) {
354
+ this.storyLabel.textContent = '';
355
+ this.roving();
356
+ return;
357
+ }
358
+ pressed('bold', format.bold);
359
+ pressed('italic', format.italic);
360
+ pressed('underline', format.underline);
361
+ pressed('strike', format.strike);
362
+ for (const a of ['left', 'center', 'right', 'justify'])
363
+ pressed(`align-${a}`, format.align === a);
364
+ pressed('bullets', format.list === 'bullet');
365
+ pressed('numbers', format.list === 'number');
366
+ pressed('ltr', format.direction === 'ltr');
367
+ pressed('rtl', format.direction === 'rtl');
368
+ pressed('track', format.trackChanges);
369
+ this.controls.get('undo').disabled = !(this.editing?.canUndo ?? format.canUndo);
370
+ this.controls.get('redo').disabled = !(this.editing?.canRedo ?? format.canRedo);
371
+ const size = this.controls.get('size');
372
+ const sizeText = format.size !== undefined ? String(format.size) : '';
373
+ if (sizeText && ![...size.options].some((o) => o.value === sizeText))
374
+ size.add(opt(sizeText, sizeText));
375
+ size.value = sizeText;
376
+ this.controls.get('style').value = format.styleId ?? 'Normal';
377
+ this.controls.get('highlight').value = format.highlight ?? 'none';
378
+ this.storyLabel.textContent = STORY_NAMES[format.story] ?? '';
379
+ this.roving();
380
+ }
381
+ // Overflow ----------------------------------------------------------------------------------
382
+ /**
383
+ * Fit the groups into the row: those that do not fit move into the "more" menu, rightmost
384
+ * first. Widths and room are measured unless given (tests, hosts without layout).
385
+ */
386
+ relayout(widths, available) {
387
+ if (!this.el || !this.shown || this.fold)
388
+ return;
389
+ for (const g of this.groups)
390
+ this.row.insertBefore(g, this.moreWrap);
391
+ this.moreWrap.hidden = true;
392
+ let w = widths;
393
+ let room = available;
394
+ if (!w || room === undefined) {
395
+ const rowWidth = this.row.clientWidth;
396
+ if (!rowWidth) {
397
+ this.closeMenu();
398
+ return;
399
+ }
400
+ w = this.groups.map((g) => g.offsetWidth + 2);
401
+ room = rowWidth - 12 - (this.foldButton.offsetWidth + 2) - this.storyLabel.offsetWidth;
402
+ }
403
+ const keep = fitGroups(w, room, (this.moreButton.offsetWidth || 28) + 2);
404
+ if (keep >= this.groups.length) {
405
+ this.closeMenu();
406
+ this.roving();
407
+ return;
408
+ }
409
+ this.moreWrap.hidden = false;
410
+ for (const g of this.groups.slice(keep))
411
+ this.pop.appendChild(g);
412
+ if (!this.pop.hidden)
413
+ this.placePop();
414
+ this.roving();
415
+ }
416
+ onWinResize = () => {
417
+ if (!this.pop.hidden)
418
+ this.placePop();
419
+ };
420
+ onWinScroll = (e) => {
421
+ if (this.pop.hidden || this.pop.contains(e.target))
422
+ return;
423
+ this.closeMenu();
424
+ };
425
+ toggleMenu() {
426
+ if (this.pop.hidden) {
427
+ this.pop.hidden = false;
428
+ this.placePop();
429
+ window.addEventListener('resize', this.onWinResize);
430
+ window.addEventListener('scroll', this.onWinScroll, true);
431
+ this.moreButton.setAttribute('aria-expanded', 'true');
432
+ this.roving();
433
+ }
434
+ else
435
+ this.closeMenu();
436
+ }
437
+ /** Flip/clamp the overflow popover inside the viewport, anchored under the "more" button. */
438
+ placePop() {
439
+ const b = this.moreButton.getBoundingClientRect();
440
+ const r = this.pop.getBoundingClientRect();
441
+ const vp = { w: window.innerWidth, h: window.innerHeight };
442
+ // Right-align under the button; positionMenu flips up/clamps when it would overflow.
443
+ const p = positionMenu({ x: b.right - r.width, y: b.bottom }, { w: r.width, h: r.height }, vp);
444
+ if (p.y < b.bottom && p.y + r.height > b.top)
445
+ p.y = Math.max(4, b.top - r.height);
446
+ this.pop.style.left = `${p.x}px`;
447
+ this.pop.style.top = `${p.y}px`;
448
+ }
449
+ closeMenu() {
450
+ window.removeEventListener('resize', this.onWinResize);
451
+ window.removeEventListener('scroll', this.onWinScroll, true);
452
+ if (!this.el)
453
+ return;
454
+ this.pop.hidden = true;
455
+ this.moreButton.setAttribute('aria-expanded', 'false');
456
+ this.roving();
457
+ }
458
+ // Keyboard ----------------------------------------------------------------------------------
459
+ navItems() {
460
+ const row = [...this.row.querySelectorAll('[data-nav]')];
461
+ const more = this.pop.hidden ? [] : [...this.pop.querySelectorAll('[data-nav]')];
462
+ // The "more" button comes after the row's controls and before the fold button.
463
+ const fold = row.indexOf(this.foldButton);
464
+ const items = [...row.slice(0, fold), ...more, this.foldButton];
465
+ return items.filter((n) => !n.disabled && !(this.moreWrap.hidden && n === this.moreButton));
466
+ }
467
+ /** The one tab stop: the current one if still usable, else the first enabled visible control. */
468
+ roving(active) {
469
+ if (!this.el || !this.row)
470
+ return;
471
+ const usable = this.navItems();
472
+ const all = [...this.el.querySelectorAll('[data-nav]')];
473
+ const current = [active, all.find((n) => n.tabIndex === 0)].find((n) => n && usable.includes(n)) ?? usable[0];
474
+ for (const n of all)
475
+ n.tabIndex = n === current ? 0 : -1;
476
+ }
477
+ onKey(e) {
478
+ if (e.key === 'Escape' && !this.pop.hidden) {
479
+ this.closeMenu();
480
+ this.moreButton.focus();
481
+ return;
482
+ }
483
+ const keys = ['ArrowRight', 'ArrowLeft', 'Home', 'End'];
484
+ const target = e.target;
485
+ if (!keys.includes(e.key) || !target.hasAttribute('data-nav'))
486
+ return;
487
+ const items = this.navItems();
488
+ const at = items.indexOf(target);
489
+ if (at < 0)
490
+ return;
491
+ const next = e.key === 'Home' ? 0 : e.key === 'End' ? items.length - 1 : (at + (e.key === 'ArrowRight' ? 1 : -1) + items.length) % items.length;
492
+ const to = items[next];
493
+ e.preventDefault();
494
+ this.roving(to);
495
+ to.focus();
496
+ }
497
+ destroy() {
498
+ for (const fn of this.cleanup)
499
+ fn();
500
+ this.resizeObserver?.disconnect();
501
+ this.el?.remove();
502
+ }
503
+ }
504
+ /** The earlier name of the toolbar. */
505
+ export { ToolbarController as FormatToolbar };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Pure layout for the viewer's comment column. Nothing here touches the DOM; all values are in
3
+ * the same unit as the anchors (Task 7 passes CSS px).
4
+ */
5
+ export interface CardBox {
6
+ id: string;
7
+ anchorY: number;
8
+ height: number;
9
+ }
10
+ /**
11
+ * Tops for comment cards, keyed by card id. Cards are ordered by `anchorY` (stable for ties).
12
+ *
13
+ * Without `openId`, each card sits at its anchor unless that would overlap the card before it.
14
+ * With `openId`, the open card stays at its anchor; cards above are stacked upward from it and
15
+ * cards below are stacked downward from it. If the topmost card would start above 0, the open
16
+ * card and every card above it move down by that overflow; cards below follow the open card.
17
+ */
18
+ export declare function layoutCards(cards: CardBox[], openId?: string, gap?: number): Map<string, number>;
19
+ /**
20
+ * Whether comments show as cards or as bubbles. Cards need the viewer to be at least
21
+ * `0.75 × widestPageAt100 + 300` wide. The `previous` mode adds a 40-unit band so the mode does
22
+ * not flicker when the width hovers near the threshold.
23
+ */
24
+ export declare function commentMode(viewerWidth: number, widestPageAt100: number, previous: 'cards' | 'bubbles' | undefined): 'cards' | 'bubbles';
25
+ /** Height (and width) of a comment bubble, in CSS px. */
26
+ export declare const BUBBLE_SIZE = 28;
27
+ /**
28
+ * Groups anchors into bubbles. Anchors are sorted by `y` (stable for ties). An anchor joins the
29
+ * current group only when it is closer than `minGap` to the group's `y`; otherwise it starts a new
30
+ * group. A group's `y` is its first anchor's `y`, which is where the bubble is drawn. The default
31
+ * gap (a bubble's height plus 2) keeps neighbouring bubbles from overlapping.
32
+ */
33
+ export declare function bubbleGroups(anchors: Array<{
34
+ id: string;
35
+ y: number;
36
+ }>, minGap?: number): Array<{
37
+ y: number;
38
+ ids: string[];
39
+ }>;
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Pure layout for the viewer's comment column. Nothing here touches the DOM; all values are in
3
+ * the same unit as the anchors (Task 7 passes CSS px).
4
+ */
5
+ /**
6
+ * Tops for comment cards, keyed by card id. Cards are ordered by `anchorY` (stable for ties).
7
+ *
8
+ * Without `openId`, each card sits at its anchor unless that would overlap the card before it.
9
+ * With `openId`, the open card stays at its anchor; cards above are stacked upward from it and
10
+ * cards below are stacked downward from it. If the topmost card would start above 0, the open
11
+ * card and every card above it move down by that overflow; cards below follow the open card.
12
+ */
13
+ export function layoutCards(cards, openId, gap = 8) {
14
+ const sorted = [...cards].sort((a, b) => a.anchorY - b.anchorY);
15
+ const tops = new Map();
16
+ const openIndex = openId === undefined ? -1 : sorted.findIndex((c) => c.id === openId);
17
+ if (openIndex === -1) {
18
+ let previousBottom = -Infinity;
19
+ for (const card of sorted) {
20
+ const top = Math.max(card.anchorY, previousBottom + gap);
21
+ tops.set(card.id, top);
22
+ previousBottom = top + card.height;
23
+ }
24
+ return tops;
25
+ }
26
+ const open = sorted[openIndex];
27
+ // Tops for cards 0..openIndex before any shift; the open card stays at its anchor.
28
+ const upTops = [open.anchorY];
29
+ for (let i = openIndex - 1; i >= 0; i--) {
30
+ const card = sorted[i];
31
+ const nextTop = upTops[0];
32
+ upTops.unshift(Math.min(card.anchorY, nextTop - card.height - gap));
33
+ }
34
+ const overflow = Math.max(0, -upTops[0]);
35
+ upTops.forEach((top, i) => tops.set(sorted[i].id, top + overflow));
36
+ let previousBottom = (open.anchorY + overflow) + open.height;
37
+ for (let i = openIndex + 1; i < sorted.length; i++) {
38
+ const card = sorted[i];
39
+ const top = Math.max(card.anchorY, previousBottom + gap);
40
+ tops.set(card.id, top);
41
+ previousBottom = top + card.height;
42
+ }
43
+ return tops;
44
+ }
45
+ /**
46
+ * Whether comments show as cards or as bubbles. Cards need the viewer to be at least
47
+ * `0.75 × widestPageAt100 + 300` wide. The `previous` mode adds a 40-unit band so the mode does
48
+ * not flicker when the width hovers near the threshold.
49
+ */
50
+ export function commentMode(viewerWidth, widestPageAt100, previous) {
51
+ const threshold = 0.75 * widestPageAt100 + 300;
52
+ if (previous === 'bubbles')
53
+ return viewerWidth >= threshold + 40 ? 'cards' : 'bubbles';
54
+ if (previous === 'cards')
55
+ return viewerWidth < threshold - 40 ? 'bubbles' : 'cards';
56
+ return viewerWidth >= threshold ? 'cards' : 'bubbles';
57
+ }
58
+ /** Height (and width) of a comment bubble, in CSS px. */
59
+ export const BUBBLE_SIZE = 28;
60
+ /**
61
+ * Groups anchors into bubbles. Anchors are sorted by `y` (stable for ties). An anchor joins the
62
+ * current group only when it is closer than `minGap` to the group's `y`; otherwise it starts a new
63
+ * group. A group's `y` is its first anchor's `y`, which is where the bubble is drawn. The default
64
+ * gap (a bubble's height plus 2) keeps neighbouring bubbles from overlapping.
65
+ */
66
+ export function bubbleGroups(anchors, minGap = BUBBLE_SIZE + 2) {
67
+ const sorted = [...anchors].sort((a, b) => a.y - b.y);
68
+ const groups = [];
69
+ for (const anchor of sorted) {
70
+ const current = groups[groups.length - 1];
71
+ if (current && anchor.y - current.y < minGap) {
72
+ current.ids.push(anchor.id);
73
+ }
74
+ else {
75
+ groups.push({ y: anchor.y, ids: [anchor.id] });
76
+ }
77
+ }
78
+ return groups;
79
+ }
@@ -0,0 +1,6 @@
1
+ /** Inline 16x16 stroke icons shared by the toolbar, navigation panel and context menu. */
2
+ export type IconName = 'undo' | 'redo' | 'alignLeft' | 'alignCenter' | 'alignRight' | 'justify' | 'bullets' | 'numbers' | 'outdent' | 'indent' | 'ltr' | 'rtl' | 'track' | 'fold' | 'unfold' | 'outline' | 'bookmark' | 'link' | 'comment' | 'search' | 'back' | 'forward' | 'cut' | 'copy' | 'paste' | 'more';
3
+ /** Inline SVG markup for an icon. With a `label` it is announced; without one it is decorative. */
4
+ export declare function icon(name: IconName, label?: string): string;
5
+ /** True when `name` is one of the built-in icons (own keys only). */
6
+ export declare function isIconName(name: string): name is IconName;
@@ -0,0 +1,38 @@
1
+ const PATHS = {
2
+ undo: '<path d="M5 4L2 7l3 3"/><path d="M2 7h8a4 4 0 010 8H7"/>',
3
+ redo: '<path d="M11 4l3 3-3 3"/><path d="M14 7H6a4 4 0 000 8h3"/>',
4
+ alignLeft: '<path d="M2 3h12M2 6.5h8M2 10h12M2 13.5h8"/>',
5
+ alignCenter: '<path d="M2 3h12M4 6.5h8M2 10h12M4 13.5h8"/>',
6
+ alignRight: '<path d="M2 3h12M6 6.5h8M2 10h12M6 13.5h8"/>',
7
+ justify: '<path d="M2 3h12M2 6.5h12M2 10h12M2 13.5h12"/>',
8
+ bullets: '<circle cx="3" cy="4" r=".8" fill="currentColor"/><circle cx="3" cy="8" r=".8" fill="currentColor"/><circle cx="3" cy="12" r=".8" fill="currentColor"/><path d="M6 4h8M6 8h8M6 12h8"/>',
9
+ numbers: '<path d="M6 4h8M6 8h8M6 12h8"/><text x="1" y="5.5" font-size="5" fill="currentColor" stroke="none">1</text><text x="1" y="9.5" font-size="5" fill="currentColor" stroke="none">2</text><text x="1" y="13.5" font-size="5" fill="currentColor" stroke="none">3</text>',
10
+ outdent: '<path d="M7 4h7M7 8h7M7 12h7M4 6L2 8l2 2"/>',
11
+ indent: '<path d="M7 4h7M7 8h7M7 12h7M2 6l2 2-2 2"/>',
12
+ rtl: '<path d="M6 2v9M9 2v9M6 2h5M5 2a2.5 2.5 0 000 5h1"/><path d="M14 14H3M5 12l-2 2 2 2"/>',
13
+ ltr: '<path d="M6 2v9M9 2v9M6 2h5M5 2a2.5 2.5 0 000 5h1"/><path d="M2 14h11M11 12l2 2-2 2"/>',
14
+ track: '<path d="M3 13l1-3 7-7 2 2-7 7z"/><path d="M2 15h12" stroke-dasharray="2 1.5"/>',
15
+ fold: '<path d="M4 10l4-4 4 4"/>',
16
+ unfold: '<path d="M4 6l4 4 4-4"/>',
17
+ outline: '<path d="M2 3h12M5 6.5h9M5 10h9M8 13.5h6"/>',
18
+ bookmark: '<path d="M4 2h8v12l-4-3-4 3z"/>',
19
+ link: '<path d="M7 9a3 3 0 004 0l2.5-2.5a3 3 0 00-4-4L8.5 3.5"/><path d="M9 7a3 3 0 00-4 0L2.5 9.5a3 3 0 004 4l1-1"/>',
20
+ comment: '<path d="M2 3h12v8H7l-3 3v-3H2z"/>',
21
+ search: '<circle cx="7" cy="7" r="4.5"/><path d="M10.5 10.5L14 14"/>',
22
+ back: '<path d="M10 3L5 8l5 5"/>',
23
+ forward: '<path d="M6 3l5 5-5 5"/>',
24
+ cut: '<circle cx="4.5" cy="12" r="2"/><circle cx="11.5" cy="12" r="2"/><path d="M5.5 10.3L11 2M10.5 10.3L5 2"/>',
25
+ copy: '<rect x="5.5" y="5.5" width="8" height="8" rx="1"/><path d="M10.5 5.5v-3h-8v8h3"/>',
26
+ paste: '<rect x="3" y="3" width="10" height="11" rx="1"/><path d="M6 3V2h4v1M6 7h4M6 10h4"/>',
27
+ more: '<circle cx="3.5" cy="8" r=".9" fill="currentColor"/><circle cx="8" cy="8" r=".9" fill="currentColor"/><circle cx="12.5" cy="8" r=".9" fill="currentColor"/>',
28
+ };
29
+ const escapeAttr = (text) => text.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;');
30
+ /** Inline SVG markup for an icon. With a `label` it is announced; without one it is decorative. */
31
+ export function icon(name, label) {
32
+ const a11y = label ? `role="img" aria-label="${escapeAttr(label)}"` : 'aria-hidden="true"';
33
+ return `<svg width="16" height="16" viewBox="0 0 16 16" ${a11y} fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">${PATHS[name]}</svg>`;
34
+ }
35
+ /** True when `name` is one of the built-in icons (own keys only). */
36
+ export function isIconName(name) {
37
+ return Object.prototype.hasOwnProperty.call(PATHS, name);
38
+ }