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,29 @@
1
+ /**
2
+ * Rich copy and paste: clipboard HTML (from Word, Google Docs, browsers) to paragraphs of
3
+ * formatted runs, and a selection back to HTML. A small tolerant tokenizer is used instead of
4
+ * the DOM so it also runs in workers and tests; scripts, styles and unknown markup are ignored.
5
+ */
6
+ import type { TextFormat } from '../api/editor.js';
7
+ import type { ListFormat } from '../api/lists.js';
8
+ import type { DocParagraph } from '../model/content.js';
9
+ export interface PastedRun {
10
+ text: string;
11
+ format: TextFormat;
12
+ link?: string;
13
+ }
14
+ export interface PastedParagraph {
15
+ /** Paragraph style id, e.g. "Heading1". */
16
+ style?: string;
17
+ list?: ListFormat;
18
+ runs: PastedRun[];
19
+ }
20
+ /** CSS colour → RRGGBB (hex, rgb(), a few names); undefined for black/auto/unknown. */
21
+ export declare function cssColor(value: string | undefined): string | undefined;
22
+ /** Paragraphs of formatted runs from clipboard HTML. */
23
+ export declare function htmlToParagraphs(html: string): PastedParagraph[];
24
+ /** HTML for parts of paragraphs (`from`/`to` are offsets in their text with objects as U+FFFC). */
25
+ export declare function paragraphsToHtml(parts: Array<{
26
+ para: DocParagraph;
27
+ from: number;
28
+ to: number;
29
+ }>, listKind: (p: DocParagraph) => 'bullet' | 'number' | undefined): string;
@@ -0,0 +1,401 @@
1
+ const NAMED = {
2
+ amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', mdash: '—', ndash: '–', hellip: '…',
3
+ lsquo: '‘', rsquo: '’', ldquo: '“', rdquo: '”', bull: '•', copy: '©', reg: '®', trade: '™',
4
+ euro: '€', deg: '°', times: '×', middot: '·', laquo: '«', raquo: '»', shy: '­',
5
+ };
6
+ function decodeEntities(s) {
7
+ return s.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);?/gi, (m, e) => {
8
+ if (e[0] === '#') {
9
+ const cp = e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
10
+ return Number.isFinite(cp) && cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : m;
11
+ }
12
+ return NAMED[e.toLowerCase()] ?? m;
13
+ });
14
+ }
15
+ function tokenize(html) {
16
+ const out = [];
17
+ let i = 0;
18
+ while (i < html.length) {
19
+ const lt = html.indexOf('<', i);
20
+ if (lt < 0) {
21
+ out.push({ type: 'text', text: html.slice(i) });
22
+ break;
23
+ }
24
+ if (lt > i)
25
+ out.push({ type: 'text', text: html.slice(i, lt) });
26
+ if (html.startsWith('<!--', lt)) {
27
+ const end = html.indexOf('-->', lt + 4);
28
+ i = end < 0 ? html.length : end + 3;
29
+ continue;
30
+ }
31
+ if (html[lt + 1] === '!' || html[lt + 1] === '?') {
32
+ const end = html.indexOf('>', lt);
33
+ i = end < 0 ? html.length : end + 1;
34
+ continue;
35
+ }
36
+ const m = /^<\/?\s*([a-zA-Z][\w:.-]*)/.exec(html.slice(lt, lt + 64));
37
+ if (!m) {
38
+ out.push({ type: 'text', text: '<' });
39
+ i = lt + 1;
40
+ continue;
41
+ }
42
+ // Find the tag end, skipping quoted attribute values.
43
+ let j = lt + m[0].length;
44
+ let quote = '';
45
+ for (; j < html.length; j++) {
46
+ const c = html[j];
47
+ if (quote) {
48
+ if (c === quote)
49
+ quote = '';
50
+ }
51
+ else if (c === '"' || c === "'")
52
+ quote = c;
53
+ else if (c === '>')
54
+ break;
55
+ }
56
+ const body = html.slice(lt + m[0].length, j);
57
+ const name = m[1].toLowerCase();
58
+ if (html[lt + 1] === '/')
59
+ out.push({ type: 'close', name });
60
+ else {
61
+ const attrs = {};
62
+ for (const a of body.matchAll(/([^\s=/>]+)(?:\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+)))?/g)) {
63
+ attrs[a[1].toLowerCase()] = decodeEntities(a[3] ?? a[4] ?? a[5] ?? '');
64
+ }
65
+ out.push({ type: 'open', name, attrs, selfClosing: /\/\s*$/.test(body) });
66
+ }
67
+ i = j + 1;
68
+ // Raw text elements: skip their content.
69
+ if (html[lt + 1] !== '/' && (name === 'script' || name === 'style' || name === 'title' || name === 'xml')) {
70
+ const close = html.toLowerCase().indexOf(`</${name}`, i);
71
+ i = close < 0 ? html.length : close;
72
+ }
73
+ }
74
+ return out;
75
+ }
76
+ function parseStyle(style) {
77
+ const out = {};
78
+ for (const decl of (style ?? '').split(';')) {
79
+ const k = decl.indexOf(':');
80
+ if (k > 0)
81
+ out[decl.slice(0, k).trim().toLowerCase()] = decl.slice(k + 1).trim();
82
+ }
83
+ return out;
84
+ }
85
+ /** CSS colour → RRGGBB (hex, rgb(), a few names); undefined for black/auto/unknown. */
86
+ export function cssColor(value) {
87
+ if (!value)
88
+ return undefined;
89
+ const v = value.trim().toLowerCase().replace(/\s*!important$/, '');
90
+ let hex;
91
+ const h = /^#([0-9a-f]{3}|[0-9a-f]{6})$/.exec(v);
92
+ if (h)
93
+ hex = h[1].length === 3 ? [...h[1]].map((c) => c + c).join('') : h[1];
94
+ const rgb = /^rgba?\(\s*(\d+)[\s,]+(\d+)[\s,]+(\d+)(?:[\s,/]+([\d.]+%?))?\s*\)$/.exec(v);
95
+ if (rgb) {
96
+ if (rgb[4] !== undefined && parseFloat(rgb[4]) === 0)
97
+ return undefined;
98
+ hex = [rgb[1], rgb[2], rgb[3]].map((n) => Math.min(255, Number(n)).toString(16).padStart(2, '0')).join('');
99
+ }
100
+ const names = { red: 'ff0000', blue: '0000ff', green: '008000', yellow: 'ffff00', orange: 'ffa500', purple: '800080', gray: '808080', grey: '808080', white: 'ffffff', black: '000000' };
101
+ hex ??= names[v];
102
+ return hex ? hex.toUpperCase() : undefined;
103
+ }
104
+ /** Word's highlight colours, nearest by RGB distance (only for fairly saturated backgrounds). */
105
+ function highlightFor(hex) {
106
+ if (!hex || hex === 'FFFFFF')
107
+ return undefined;
108
+ const table = { yellow: 'FFFF00', green: '00FF00', cyan: '00FFFF', magenta: 'FF00FF', blue: '0000FF', red: 'FF0000', lightGray: 'C0C0C0', darkGray: '808080' };
109
+ const rgb = (x) => [0, 2, 4].map((i) => parseInt(x.slice(i, i + 2), 16));
110
+ const [r, g, b] = rgb(hex);
111
+ let best;
112
+ let bestD = Infinity;
113
+ for (const [name, value] of Object.entries(table)) {
114
+ const [r2, g2, b2] = rgb(value);
115
+ const d = (r - r2) ** 2 + (g - g2) ** 2 + (b - b2) ** 2;
116
+ if (d < bestD)
117
+ [best, bestD] = [name, d];
118
+ }
119
+ return bestD < 120 ** 2 ? best : undefined;
120
+ }
121
+ function cssSize(value) {
122
+ const m = /^([\d.]+)\s*(pt|px)?$/.exec((value ?? '').trim().toLowerCase());
123
+ if (!m)
124
+ return undefined;
125
+ const n = parseFloat(m[1]) * (m[2] === 'px' ? 0.75 : 1);
126
+ return n >= 1 && n <= 400 ? Math.round(n * 2) / 2 : undefined;
127
+ }
128
+ const BLOCKS = new Set(['p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'blockquote', 'pre', 'tr', 'table', 'ul', 'ol', 'section', 'article', 'header', 'footer', 'dd', 'dt', 'figcaption', 'hr']);
129
+ const VOID = new Set(['br', 'img', 'hr', 'meta', 'link', 'input', 'col', 'wbr', 'area', 'base', 'source']);
130
+ /** Paragraphs of formatted runs from clipboard HTML. */
131
+ export function htmlToParagraphs(html) {
132
+ // Only the copied fragment when the clipboard marks it.
133
+ const start = html.indexOf('<!--StartFragment-->');
134
+ const end = html.indexOf('<!--EndFragment-->');
135
+ if (start >= 0 && end > start)
136
+ html = html.slice(start + 20, end);
137
+ const paragraphs = [];
138
+ let current;
139
+ const lists = [];
140
+ const stack = [{ name: '#root', format: {}, pre: false }];
141
+ const top = () => stack[stack.length - 1];
142
+ let cellCount = 0;
143
+ const startParagraph = (p = {}) => {
144
+ finishParagraph();
145
+ current = { runs: [], ...p };
146
+ };
147
+ const finishParagraph = () => {
148
+ if (!current)
149
+ return;
150
+ // Trim the paragraph's outer spaces (HTML whitespace is not content).
151
+ const runs = current.runs;
152
+ while (runs.length && !runs[0].text.replace(/^[ \t]+/, ''))
153
+ runs.shift();
154
+ if (runs[0])
155
+ runs[0].text = runs[0].text.replace(/^[ ]+/, '');
156
+ while (runs.length && !runs[runs.length - 1].text.replace(/[ \t]+$/, ''))
157
+ runs.pop();
158
+ if (runs.length)
159
+ runs[runs.length - 1].text = runs[runs.length - 1].text.replace(/[ ]+$/, '');
160
+ if (runs.length || current.list || current.style)
161
+ paragraphs.push(current);
162
+ current = undefined;
163
+ };
164
+ const addText = (raw, literal = false) => {
165
+ const f = top();
166
+ let text = literal ? raw : decodeEntities(raw);
167
+ if (!f.pre && !literal) {
168
+ text = text.replace(/[\s ]+/g, (ws) => (ws.includes(' ') ? ws.replace(/[^ ]/g, '').replace(/ /g, ' ') || ' ' : ' '));
169
+ if (!current && !text.trim())
170
+ return;
171
+ }
172
+ if (!current)
173
+ current = { runs: [] };
174
+ const last = current.runs[current.runs.length - 1];
175
+ // Collapse spaces across tags.
176
+ if (!f.pre && !literal && last && /[ \n\t]$/.test(last.text))
177
+ text = text.replace(/^ /, '');
178
+ if (!text)
179
+ return;
180
+ if (last && last.link === f.link && JSON.stringify(last.format) === JSON.stringify(f.format))
181
+ last.text += text;
182
+ else
183
+ current.runs.push({ text, format: { ...f.format }, link: f.link });
184
+ };
185
+ for (const t of tokenize(html)) {
186
+ if (t.type === 'text') {
187
+ addText(t.text);
188
+ continue;
189
+ }
190
+ if (t.type === 'close') {
191
+ const i = stack.map((f) => f.name).lastIndexOf(t.name);
192
+ if (i > 0)
193
+ stack.length = i;
194
+ if (t.name === 'ul' || t.name === 'ol')
195
+ lists.pop();
196
+ if (BLOCKS.has(t.name))
197
+ finishParagraph();
198
+ continue;
199
+ }
200
+ const name = t.name;
201
+ const attrs = t.attrs;
202
+ const css = parseStyle(attrs.style);
203
+ if (css.display === 'none' || css['mso-hide'] === 'all') {
204
+ if (!VOID.has(name) && !t.selfClosing)
205
+ stack.push({ ...top(), name, format: { ...top().format, hidden: true } });
206
+ continue;
207
+ }
208
+ if (name === 'br') {
209
+ const last = current?.runs[current.runs.length - 1];
210
+ if (last)
211
+ last.text = last.text.replace(/ $/, '');
212
+ addText('\n', true);
213
+ continue;
214
+ }
215
+ if (name === 'ul' || name === 'ol') {
216
+ finishParagraph();
217
+ lists.push(name === 'ul' ? 'bullet' : 'decimal');
218
+ }
219
+ const heading = /^h([1-6])$/.exec(name);
220
+ if (heading)
221
+ startParagraph({ style: `Heading${heading[1]}` });
222
+ else if (name === 'li')
223
+ startParagraph({ list: lists[lists.length - 1] ?? 'bullet' });
224
+ else if (name === 'tr') {
225
+ startParagraph();
226
+ cellCount = 0;
227
+ }
228
+ else if (name === 'td' || name === 'th') {
229
+ if (cellCount++ > 0)
230
+ addText('\t', true);
231
+ }
232
+ else if (BLOCKS.has(name))
233
+ startParagraph();
234
+ // Word marks its own list paragraphs with a class and an mso-list style instead of <li>.
235
+ if (name === 'p' && (/MsoListParagraph/i.test(attrs.class ?? '') || css['mso-list']) && current)
236
+ current.list = 'bullet';
237
+ if (VOID.has(name) || t.selfClosing)
238
+ continue;
239
+ const parent = top();
240
+ const format = { ...parent.format };
241
+ let link = parent.link;
242
+ switch (name) {
243
+ case 'b':
244
+ case 'strong':
245
+ format.bold = true;
246
+ break;
247
+ case 'i':
248
+ case 'em':
249
+ case 'cite':
250
+ format.italic = true;
251
+ break;
252
+ case 'u':
253
+ case 'ins':
254
+ format.underline = true;
255
+ break;
256
+ case 's':
257
+ case 'strike':
258
+ case 'del':
259
+ format.strike = true;
260
+ break;
261
+ case 'sup':
262
+ format.vertAlign = 'superscript';
263
+ break;
264
+ case 'sub':
265
+ format.vertAlign = 'subscript';
266
+ break;
267
+ case 'code':
268
+ case 'kbd':
269
+ case 'tt':
270
+ format.font = 'Courier New';
271
+ break;
272
+ case 'a':
273
+ if (attrs.href && /^(https?|mailto|tel):/i.test(attrs.href))
274
+ link = attrs.href;
275
+ break;
276
+ case 'font':
277
+ if (attrs.color)
278
+ format.color = cssColor(attrs.color);
279
+ if (attrs.face)
280
+ format.font = attrs.face.split(',')[0].replace(/['"]/g, '').trim();
281
+ break;
282
+ }
283
+ // Google Docs wraps everything in <b style="font-weight:normal">: CSS decides.
284
+ const weight = css['font-weight'];
285
+ if (weight)
286
+ format.bold = weight === 'bold' || weight === 'bolder' || Number(weight) >= 600;
287
+ if (css['font-style'])
288
+ format.italic = css['font-style'] === 'italic' || css['font-style'] === 'oblique';
289
+ const deco = css['text-decoration'] ?? css['text-decoration-line'];
290
+ if (deco) {
291
+ if (deco.includes('underline'))
292
+ format.underline = true;
293
+ if (deco.includes('line-through'))
294
+ format.strike = true;
295
+ if (deco === 'none' && name !== 'a')
296
+ format.underline = false;
297
+ }
298
+ const color = cssColor(css.color);
299
+ if (color && color !== '000000')
300
+ format.color = color;
301
+ const highlight = highlightFor(cssColor(css['background-color'] ?? css.background));
302
+ if (highlight)
303
+ format.highlight = highlight;
304
+ const size = cssSize(css['font-size']);
305
+ if (size)
306
+ format.size = size;
307
+ const family = css['font-family']?.split(',')[0]?.replace(/['"]/g, '').trim();
308
+ if (family && !/^(inherit|initial|sans-serif|serif|monospace|system-ui|-apple-system)$/i.test(family))
309
+ format.font = family;
310
+ if (css['vertical-align'] === 'super')
311
+ format.vertAlign = 'superscript';
312
+ if (css['vertical-align'] === 'sub')
313
+ format.vertAlign = 'subscript';
314
+ stack.push({ name, format, link, pre: parent.pre || name === 'pre' || /pre/.test(css['white-space'] ?? '') });
315
+ }
316
+ finishParagraph();
317
+ // Drop hidden runs and formatting that only repeats the default.
318
+ for (const p of paragraphs) {
319
+ p.runs = p.runs.filter((r) => !r.format.hidden);
320
+ for (const r of p.runs)
321
+ for (const k of Object.keys(r.format))
322
+ if (r.format[k] === false || r.format[k] === undefined)
323
+ delete r.format[k];
324
+ }
325
+ return paragraphs.filter((p) => p.runs.length || p.list || p.style);
326
+ }
327
+ const escapeHtml = (s) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
328
+ /** HTML for parts of paragraphs (`from`/`to` are offsets in their text with objects as U+FFFC). */
329
+ export function paragraphsToHtml(parts, listKind) {
330
+ let html = '';
331
+ let openList;
332
+ for (const { para, from, to } of parts) {
333
+ let inner = '';
334
+ let offset = 0;
335
+ for (const inline of para.inlines) {
336
+ let text;
337
+ if (inline.kind === 'text')
338
+ text = inline.text;
339
+ else if (inline.kind === 'tab')
340
+ text = '\t';
341
+ else if (inline.kind === 'break' && inline.type === 'line')
342
+ text = '\n';
343
+ else if (inline.kind === 'image' && !inline.float)
344
+ text = '';
345
+ if (text === undefined)
346
+ continue;
347
+ const a = Math.max(from, offset);
348
+ const b = Math.min(to, offset + text.length);
349
+ offset += text.length;
350
+ if (a >= b)
351
+ continue;
352
+ const piece = text.slice(a - (offset - text.length), b - (offset - text.length));
353
+ if (piece === '')
354
+ continue;
355
+ let h = escapeHtml(piece).replace(/\n/g, '<br>');
356
+ if (inline.kind === 'text') {
357
+ const pr = inline.props;
358
+ const css = [];
359
+ if (pr.color && pr.color !== 'auto')
360
+ css.push(`color:#${pr.color}`);
361
+ if (pr.size)
362
+ css.push(`font-size:${pr.size}pt`);
363
+ if (pr.fonts?.ascii)
364
+ css.push(`font-family:'${pr.fonts.ascii.replace(/'/g, '')}'`);
365
+ if (css.length)
366
+ h = `<span style="${css.join(';')}">${h}</span>`;
367
+ if (pr.bold)
368
+ h = `<b>${h}</b>`;
369
+ if (pr.italic)
370
+ h = `<i>${h}</i>`;
371
+ if (pr.underline && pr.underline !== 'none')
372
+ h = `<u>${h}</u>`;
373
+ if (pr.strike || pr.dstrike)
374
+ h = `<s>${h}</s>`;
375
+ if (pr.vertAlign === 'superscript')
376
+ h = `<sup>${h}</sup>`;
377
+ if (pr.vertAlign === 'subscript')
378
+ h = `<sub>${h}</sub>`;
379
+ }
380
+ inner += h;
381
+ }
382
+ const kind = listKind(para);
383
+ const tag = kind ? 'ul' : undefined;
384
+ const want = kind === 'bullet' ? 'ul' : kind === 'number' ? 'ol' : undefined;
385
+ if (openList && openList !== want) {
386
+ html += `</${openList}>`;
387
+ openList = undefined;
388
+ }
389
+ if (want && !openList) {
390
+ html += `<${want}>`;
391
+ openList = want;
392
+ }
393
+ const heading = /^Heading([1-6])$/.exec(para.styleId ?? '');
394
+ const block = tag ? 'li' : heading ? `h${heading[1]}` : 'p';
395
+ const dir = para.props.bidi ? ' dir="rtl"' : '';
396
+ html += `<${block}${dir}>${inner || '<br>'}</${block}>`;
397
+ }
398
+ if (openList)
399
+ html += `</${openList}>`;
400
+ return html;
401
+ }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * The comment column beside the pages: one layer for the whole document, with a card per thread
3
+ * placed at its anchor (cards mode) or a strip of bubbles (bubbles mode, when the viewer is too
4
+ * narrow). `viewer.comments` is its controller. The controller takes a small host (the viewer, or a
5
+ * stub in tests) that supplies the threads, where they sit and how edits are made.
6
+ */
7
+ import type { CommentInfo } from '../comments/read.js';
8
+ import type { DocRange } from '../api/editor.js';
9
+ import type { DocxEditor } from '../api/editor.js';
10
+ import type { Rect } from '../layout/types.js';
11
+ import type { DocxViewer } from './viewer.js';
12
+ export interface CommentThread {
13
+ /** The first comment of the thread. */
14
+ comment: CommentInfo;
15
+ replies: CommentInfo[];
16
+ /** Where the thread is anchored: the page, the top of its first line (points) and its highlighted areas. */
17
+ anchor: {
18
+ page: number;
19
+ y: number;
20
+ rects: Rect[];
21
+ };
22
+ }
23
+ export type CommentsMode = 'auto' | 'cards' | 'bubbles';
24
+ export interface CommentsOptions {
25
+ /** `auto` picks cards or bubbles from the viewer's width. Default: auto. */
26
+ mode?: CommentsMode;
27
+ /** Show resolved threads (faded). Default: false. */
28
+ showResolved?: boolean;
29
+ }
30
+ export interface CommentsControllerOptions extends CommentsOptions {
31
+ /** Start shown (default true). */
32
+ visible?: boolean;
33
+ }
34
+ type GuardedAction = 'comment' | 'reply' | 'resolveComment' | 'deleteComment';
35
+ /** What the column needs from the viewer. */
36
+ export interface CommentsHost extends Pick<DocxViewer, 'root' | 'on' | 'emit'> {
37
+ /** Every thread (resolved ones too), in document order. */
38
+ threads(): CommentThread[];
39
+ /**
40
+ * Where a thread's highlight is, in the scroll container's pixels: the top and height of its
41
+ * first line, `bottom` of its last line on that page (default `top + height`), and `pageRight`,
42
+ * the right edge of its page (where the connector starts).
43
+ */
44
+ anchorOf(thread: CommentThread): CommentSpot | undefined;
45
+ /** Left edge of the column in the scroll container's pixels. */
46
+ columnX(): number;
47
+ /** The widest page at 100% zoom, in CSS px. */
48
+ widestPage(): number;
49
+ editable(): boolean;
50
+ /** Whether the guard allows an action (true without a guard). */
51
+ can(action: GuardedAction, detail?: {
52
+ commentAuthor?: string;
53
+ }): boolean;
54
+ /** Who new comments and replies are written by. */
55
+ author(): string;
56
+ /** One undo step; a failure restores the document and rethrows. */
57
+ transaction<T>(label: string, fn: (editor: DocxEditor) => T | Promise<T>): Promise<T>;
58
+ /** The selection to comment on, with where it is. */
59
+ selection(): ({
60
+ range: DocRange;
61
+ } & CommentSpot) | undefined;
62
+ /** Mark the highlights of these comments as active. */
63
+ setActive(ids: string[]): void;
64
+ /** The column's width changed: rebuild the rows (which read `width`). */
65
+ rebuild(): void;
66
+ /** The open document (any value that changes when another one is loaded). */
67
+ currentDocument?(): unknown;
68
+ }
69
+ /** Where a thread's text (or the selection) is, in the scroll container's pixels. */
70
+ export interface CommentSpot {
71
+ top: number;
72
+ pageRight: number;
73
+ height: number;
74
+ bottom?: number;
75
+ }
76
+ /** "just now", "5m ago", "3h ago", "2d ago", else a short date. Empty without a date. */
77
+ export declare function relativeTime(date: string | undefined, now?: number): string;
78
+ /** Controls the comment column of a viewer (`viewer.comments`). */
79
+ export declare class CommentsController {
80
+ private readonly host;
81
+ private shown;
82
+ private pref;
83
+ private auto;
84
+ private eff;
85
+ private announced;
86
+ private showRes;
87
+ private threadList;
88
+ private openId;
89
+ private pendingOpen;
90
+ private draft;
91
+ private docSeen;
92
+ private readonly replying;
93
+ private colWidth;
94
+ private builtWidth;
95
+ private layer;
96
+ private readonly cards;
97
+ private readonly colors;
98
+ private readonly cleanup;
99
+ constructor(host: CommentsHost, options?: CommentsControllerOptions);
100
+ get visible(): boolean;
101
+ show(): void;
102
+ hide(): void;
103
+ private setVisible;
104
+ get mode(): CommentsMode;
105
+ set mode(value: CommentsMode);
106
+ get effectiveMode(): 'cards' | 'bubbles';
107
+ get showResolved(): boolean;
108
+ set showResolved(value: boolean);
109
+ /** The space the column takes beside the pages now, in CSS px (0 when hidden or without comments). */
110
+ get width(): number;
111
+ /** The column's element, when there is one. */
112
+ get element(): HTMLElement | undefined;
113
+ /** The id of the thread that is open. */
114
+ get openThread(): string | undefined;
115
+ /** @internal The viewer rebuilt its rows with the current width. */
116
+ rowsBuilt(): void;
117
+ /** Open a thread (a reply's id opens its thread): line it up with its anchor and draw the connector. */
118
+ open(id: string): void;
119
+ /** Open without redrawing. False when there is no such thread or it is already open. */
120
+ private openThreadById;
121
+ /** Open a thread and start its reply box (no-op where the viewer cannot reply). */
122
+ reply(id: string): void;
123
+ close(): void;
124
+ /** Start a comment on the current selection: a draft card with a text box beside it. */
125
+ compose(): void;
126
+ /** Recompute what is shown from the host and redraw. Called on layout, resize and state changes. */
127
+ refresh(): void;
128
+ /** Update the threads, the mode and the width, without touching the DOM. */
129
+ sync(): void;
130
+ destroy(): void;
131
+ private has;
132
+ private computeMode;
133
+ private find;
134
+ private resolvedCount;
135
+ /** Threads shown now: resolved ones only when switched on (or open), and only those with a place. */
136
+ private visibleThreads;
137
+ private actionsFor;
138
+ private onDocClick;
139
+ /** Esc closes the open thread from the page, the editing input or the column (capture: the editor handles Esc itself). */
140
+ private onKey;
141
+ private highlightAt;
142
+ private place;
143
+ private placeCards;
144
+ private fillSwitch;
145
+ private placeBubbles;
146
+ /**
147
+ * Put a bubble popover below the last line of its text (so it never covers it), its right edge
148
+ * on the bubble strip's, inside the visible part of the viewer; above the text when there is
149
+ * no room below.
150
+ */
151
+ private placePopover;
152
+ private step;
153
+ private wireCard;
154
+ private fillConnector;
155
+ private colorOf;
156
+ private head;
157
+ private fillCard;
158
+ private fillDraft;
159
+ /** A text box with Post and Cancel. `post` rejects to keep the box (the viewer reports the error). */
160
+ private composer;
161
+ private postReply;
162
+ private postDraft;
163
+ private resolve;
164
+ private remove;
165
+ }
166
+ export {};