docxkeun 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/README.md +139 -16
- package/dist/api/editor.d.ts +172 -5
- package/dist/api/editor.js +814 -24
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +5 -1
- package/dist/api/index.js +4 -1
- package/dist/api/lists.d.ts +37 -0
- package/dist/api/lists.js +132 -0
- package/dist/api/parts.js +1 -0
- package/dist/api/positions.js +1 -0
- package/dist/api/revisions.d.ts +61 -0
- package/dist/api/revisions.js +390 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -0
- package/dist/charts/color.d.ts +45 -0
- package/dist/charts/color.js +322 -0
- package/dist/charts/format.d.ts +12 -0
- package/dist/charts/format.js +459 -0
- package/dist/charts/index.d.ts +11 -0
- package/dist/charts/index.js +8 -0
- package/dist/charts/model.d.ts +225 -0
- package/dist/charts/model.js +758 -0
- package/dist/charts/outline.d.ts +17 -0
- package/dist/charts/outline.js +214 -0
- package/dist/charts/render.d.ts +20 -0
- package/dist/charts/render.js +1772 -0
- package/dist/drawing/arrows.d.ts +37 -0
- package/dist/drawing/arrows.js +99 -0
- package/dist/drawing/formula.d.ts +40 -0
- package/dist/drawing/formula.js +159 -0
- package/dist/drawing/geometry.d.ts +61 -0
- package/dist/drawing/geometry.js +286 -0
- package/dist/drawing/index.d.ts +12 -0
- package/dist/drawing/index.js +8 -0
- package/dist/drawing/presets.d.ts +72 -0
- package/dist/drawing/presets.js +2874 -0
- package/dist/fonts/shaping/apply.d.ts +94 -0
- package/dist/fonts/shaping/apply.js +349 -0
- package/dist/fonts/shaping/arabic.d.ts +28 -0
- package/dist/fonts/shaping/arabic.js +235 -0
- package/dist/fonts/shaping/buffer.d.ts +53 -0
- package/dist/fonts/shaping/buffer.js +95 -0
- package/dist/fonts/shaping/gdef.d.ts +30 -0
- package/dist/fonts/shaping/gdef.js +66 -0
- package/dist/fonts/shaping/gpos.d.ts +86 -0
- package/dist/fonts/shaping/gpos.js +428 -0
- package/dist/fonts/shaping/gsub.d.ts +65 -0
- package/dist/fonts/shaping/gsub.js +313 -0
- package/dist/fonts/shaping/index.d.ts +32 -0
- package/dist/fonts/shaping/index.js +402 -0
- package/dist/fonts/shaping/indic.d.ts +6 -0
- package/dist/fonts/shaping/indic.js +805 -0
- package/dist/fonts/shaping/layout-common.d.ts +124 -0
- package/dist/fonts/shaping/layout-common.js +349 -0
- package/dist/fonts/shaping/plan.d.ts +87 -0
- package/dist/fonts/shaping/plan.js +152 -0
- package/dist/fonts/shaping/scripts.d.ts +26 -0
- package/dist/fonts/shaping/scripts.js +160 -0
- package/dist/fonts/shaping/shaper.d.ts +23 -0
- package/dist/fonts/shaping/shaper.js +5 -0
- package/dist/fonts/shaping/unicode.d.ts +34 -0
- package/dist/fonts/shaping/unicode.js +138 -0
- package/dist/images/bmp.d.ts +9 -0
- package/dist/images/bmp.js +234 -0
- package/dist/images/gif.d.ts +9 -0
- package/dist/images/gif.js +215 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.js +63 -0
- package/dist/images/png-encode.d.ts +4 -0
- package/dist/images/png-encode.js +73 -0
- package/dist/images/size.d.ts +10 -0
- package/dist/images/size.js +83 -0
- package/dist/images/tiff.d.ts +14 -0
- package/dist/images/tiff.js +407 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +5 -0
- package/dist/layout/bidi.d.ts +18 -0
- package/dist/layout/bidi.js +71 -0
- package/dist/layout/blocks.d.ts +20 -3
- package/dist/layout/blocks.js +88 -58
- package/dist/layout/cache.d.ts +31 -0
- package/dist/layout/cache.js +38 -0
- package/dist/layout/drawing.d.ts +30 -0
- package/dist/layout/drawing.js +120 -0
- package/dist/layout/engine.d.ts +3 -0
- package/dist/layout/engine.js +218 -11
- package/dist/layout/index.d.ts +1 -0
- package/dist/layout/index.js +1 -0
- package/dist/layout/lines.d.ts +63 -5
- package/dist/layout/lines.js +245 -20
- package/dist/layout/paint.d.ts +25 -3
- package/dist/layout/paint.js +26 -10
- package/dist/layout/shapes.d.ts +7 -0
- package/dist/layout/shapes.js +338 -0
- package/dist/layout/style.d.ts +4 -0
- package/dist/layout/style.js +2 -0
- package/dist/layout/types.d.ts +89 -2
- package/dist/math/boxes.d.ts +88 -0
- package/dist/math/boxes.js +166 -0
- package/dist/math/index.d.ts +29 -0
- package/dist/math/index.js +44 -0
- package/dist/math/layout.d.ts +13 -0
- package/dist/math/layout.js +771 -0
- package/dist/math/linear.d.ts +3 -0
- package/dist/math/linear.js +152 -0
- package/dist/math/omml.d.ts +15 -0
- package/dist/math/omml.js +57 -0
- package/dist/math/paths.d.ts +61 -0
- package/dist/math/paths.js +450 -0
- package/dist/math/symbols.d.ts +49 -0
- package/dist/math/symbols.js +269 -0
- package/dist/math/text.d.ts +53 -0
- package/dist/math/text.js +316 -0
- package/dist/metafile/canvas.d.ts +80 -0
- package/dist/metafile/canvas.js +515 -0
- package/dist/metafile/dib.d.ts +27 -0
- package/dist/metafile/dib.js +300 -0
- package/dist/metafile/emf.d.ts +31 -0
- package/dist/metafile/emf.js +591 -0
- package/dist/metafile/geometry.d.ts +49 -0
- package/dist/metafile/geometry.js +85 -0
- package/dist/metafile/index.d.ts +27 -0
- package/dist/metafile/index.js +34 -0
- package/dist/metafile/reader.d.ts +28 -0
- package/dist/metafile/reader.js +59 -0
- package/dist/metafile/state.d.ts +118 -0
- package/dist/metafile/state.js +172 -0
- package/dist/metafile/text.d.ts +17 -0
- package/dist/metafile/text.js +130 -0
- package/dist/metafile/wmf.d.ts +33 -0
- package/dist/metafile/wmf.js +395 -0
- package/dist/model/content.d.ts +92 -7
- package/dist/model/content.js +251 -17
- package/dist/model/document.d.ts +34 -1
- package/dist/model/document.js +80 -1
- package/dist/model/fields.d.ts +4 -0
- package/dist/model/fields.js +10 -0
- package/dist/model/numbering.d.ts +2 -0
- package/dist/model/numbering.js +22 -0
- package/dist/model/props.d.ts +2 -0
- package/dist/model/props.js +3 -0
- package/dist/opc/index.d.ts +1 -1
- package/dist/opc/package.d.ts +34 -0
- package/dist/opc/package.js +58 -4
- package/dist/pdf/export.js +97 -6
- package/dist/pdf/images.d.ts +7 -0
- package/dist/pdf/images.js +39 -2
- package/dist/render/activity.d.ts +138 -0
- package/dist/render/activity.js +794 -0
- package/dist/render/clipboard.d.ts +29 -0
- package/dist/render/clipboard.js +401 -0
- package/dist/render/comments.d.ts +166 -0
- package/dist/render/comments.js +779 -0
- package/dist/render/context-menu.d.ts +124 -0
- package/dist/render/context-menu.js +525 -0
- package/dist/render/editing.d.ts +146 -5
- package/dist/render/editing.js +678 -53
- package/dist/render/index.d.ts +12 -0
- package/dist/render/index.js +11 -0
- package/dist/render/navigation-panel.d.ts +76 -0
- package/dist/render/navigation-panel.js +431 -0
- package/dist/render/selection.js +5 -0
- package/dist/render/svg.d.ts +4 -6
- package/dist/render/svg.js +69 -7
- package/dist/render/toolbar.d.ts +83 -0
- package/dist/render/toolbar.js +505 -0
- package/dist/render/ui/comment-layout.d.ts +39 -0
- package/dist/render/ui/comment-layout.js +79 -0
- package/dist/render/ui/icons.d.ts +6 -0
- package/dist/render/ui/icons.js +38 -0
- package/dist/render/ui/menu.d.ts +38 -0
- package/dist/render/ui/menu.js +274 -0
- package/dist/render/ui/nav-model.d.ts +11 -0
- package/dist/render/ui/nav-model.js +34 -0
- package/dist/render/ui/overflow.d.ts +5 -0
- package/dist/render/ui/overflow.js +18 -0
- package/dist/render/ui/popover.d.ts +18 -0
- package/dist/render/ui/popover.js +109 -0
- package/dist/render/ui/theme.d.ts +2 -0
- package/dist/render/ui/theme.js +29 -0
- package/dist/render/undo.d.ts +76 -0
- package/dist/render/undo.js +144 -0
- package/dist/render/viewer.d.ts +100 -7
- package/dist/render/viewer.js +302 -92
- package/dist/security/policy.d.ts +3 -0
- package/dist/security/policy.js +6 -2
- package/dist/security/protection.d.ts +1 -0
- package/dist/security/protection.js +1 -1
- package/dist/text/bidi-data.d.ts +53 -0
- package/dist/text/bidi-data.js +318 -0
- package/dist/text/bidi.d.ts +37 -0
- package/dist/text/bidi.js +508 -0
- package/dist/xml/ns.d.ts +5 -0
- package/dist/xml/ns.js +7 -0
- package/package.json +9 -2
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracked changes (revisions), the way Word stores them:
|
|
3
|
+
* - inserted runs inside `w:ins`, deleted runs inside `w:del` (their `w:t` become `w:delText`);
|
|
4
|
+
* - a paragraph mark inserted or deleted as `w:pPr/w:rPr/w:ins|w:del`;
|
|
5
|
+
* - formatting changes as `w:rPrChange` / `w:pPrChange` holding the old properties;
|
|
6
|
+
* - moves as `w:moveFrom` / `w:moveTo` (treated as a deletion and an insertion).
|
|
7
|
+
* This module records changes for the editor, lists them, and accepts or rejects them.
|
|
8
|
+
*/
|
|
9
|
+
import { attr, cloneElement, el, elements, findAll, firstChild, insertOrdered, removeChildren, textContent } from '../xml/index.js';
|
|
10
|
+
const RUN_REVISIONS = new Set(['w:ins', 'w:del', 'w:moveFrom', 'w:moveTo']);
|
|
11
|
+
const PROPERTY_CHANGES = new Set(['w:rPrChange', 'w:pPrChange', 'w:sectPrChange', 'w:tblPrChange', 'w:tblGridChange', 'w:trPrChange', 'w:tcPrChange', 'w:tblPrExChange', 'w:numberingChange']);
|
|
12
|
+
const RANGE_MARKERS = new Set(['w:moveFromRangeStart', 'w:moveFromRangeEnd', 'w:moveToRangeStart', 'w:moveToRangeEnd']);
|
|
13
|
+
export function revisionStamp(author, now = new Date()) {
|
|
14
|
+
return { author, date: now.toISOString().replace(/\.\d{3}Z$/, 'Z') };
|
|
15
|
+
}
|
|
16
|
+
/** The next free revision id (ids are shared by all revision elements in a part). */
|
|
17
|
+
export function nextRevisionId(root) {
|
|
18
|
+
let max = 0;
|
|
19
|
+
for (const name of [...RUN_REVISIONS, ...PROPERTY_CHANGES, 'w:cellIns', 'w:cellDel', 'w:cellMerge']) {
|
|
20
|
+
for (const node of findAll(root, name))
|
|
21
|
+
max = Math.max(max, Number.parseInt(attr(node, 'w:id') ?? '', 10) || 0);
|
|
22
|
+
}
|
|
23
|
+
return () => String(++max);
|
|
24
|
+
}
|
|
25
|
+
function stampAttrs(id, who) {
|
|
26
|
+
return { 'w:id': id, 'w:author': who.author, 'w:date': who.date };
|
|
27
|
+
}
|
|
28
|
+
/** Run properties of a new run must not carry another run's revision record. */
|
|
29
|
+
export function cleanRunProps(run) {
|
|
30
|
+
const rPr = firstChild(run, 'w:rPr');
|
|
31
|
+
if (rPr)
|
|
32
|
+
removeChildren(rPr, 'w:rPrChange');
|
|
33
|
+
}
|
|
34
|
+
/** Wrap runs (already in the tree, adjacent siblings) in one w:ins. */
|
|
35
|
+
export function wrapInserted(parent, runs, id, who) {
|
|
36
|
+
const first = parent.children.indexOf(runs[0]);
|
|
37
|
+
const ins = el('w:ins', stampAttrs(id, who), runs);
|
|
38
|
+
parent.children.splice(first, runs.length, ins);
|
|
39
|
+
return ins;
|
|
40
|
+
}
|
|
41
|
+
/** A new w:ins element holding runs (not yet in the tree). */
|
|
42
|
+
export function insertedRuns(runs, id, who) {
|
|
43
|
+
for (const r of runs)
|
|
44
|
+
cleanRunProps(r);
|
|
45
|
+
return el('w:ins', stampAttrs(id, who), runs);
|
|
46
|
+
}
|
|
47
|
+
function toDeletedText(run) {
|
|
48
|
+
const walk = (node) => {
|
|
49
|
+
for (const c of node.children) {
|
|
50
|
+
if (c.type !== 'element')
|
|
51
|
+
continue;
|
|
52
|
+
if (c.name === 'w:t')
|
|
53
|
+
c.name = 'w:delText';
|
|
54
|
+
else if (c.name === 'w:instrText')
|
|
55
|
+
c.name = 'w:delInstrText';
|
|
56
|
+
walk(c);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
walk(run);
|
|
60
|
+
}
|
|
61
|
+
function toLiveText(node) {
|
|
62
|
+
for (const c of node.children) {
|
|
63
|
+
if (c.type !== 'element')
|
|
64
|
+
continue;
|
|
65
|
+
if (c.name === 'w:delText')
|
|
66
|
+
c.name = 'w:t';
|
|
67
|
+
else if (c.name === 'w:delInstrText')
|
|
68
|
+
c.name = 'w:instrText';
|
|
69
|
+
toLiveText(c);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** The w:ins / w:del / w:moveFrom / w:moveTo a run sits directly in, if any. */
|
|
73
|
+
export function runRevision(run, parentOf) {
|
|
74
|
+
const parent = parentOf(run);
|
|
75
|
+
return parent && RUN_REVISIONS.has(parent.name) ? parent : undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Mark a run as deleted by `who`. A run the same author inserted is removed outright (as Word does
|
|
79
|
+
* when you delete your own insertion); a run that is already deleted is left alone.
|
|
80
|
+
*/
|
|
81
|
+
export function deleteRun(run, parentOf, nextId, who) {
|
|
82
|
+
const parent = parentOf(run);
|
|
83
|
+
if (!parent)
|
|
84
|
+
return;
|
|
85
|
+
if (parent.name === 'w:del' || parent.name === 'w:moveFrom')
|
|
86
|
+
return;
|
|
87
|
+
if ((parent.name === 'w:ins' || parent.name === 'w:moveTo') && attr(parent, 'w:author') === who.author) {
|
|
88
|
+
parent.children.splice(parent.children.indexOf(run), 1);
|
|
89
|
+
if (!elements(parent).length) {
|
|
90
|
+
const grand = parentOf(parent);
|
|
91
|
+
if (grand)
|
|
92
|
+
grand.children.splice(grand.children.indexOf(parent), 1);
|
|
93
|
+
}
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
toDeletedText(run);
|
|
97
|
+
const del = el('w:del', stampAttrs(nextId(), who), [run]);
|
|
98
|
+
parent.children.splice(parent.children.indexOf(run), 1, del);
|
|
99
|
+
}
|
|
100
|
+
function markRPr(p) {
|
|
101
|
+
let pPr = firstChild(p, 'w:pPr');
|
|
102
|
+
if (!pPr) {
|
|
103
|
+
pPr = el('w:pPr');
|
|
104
|
+
p.children.unshift(pPr);
|
|
105
|
+
}
|
|
106
|
+
let rPr = firstChild(pPr, 'w:rPr');
|
|
107
|
+
if (!rPr) {
|
|
108
|
+
rPr = el('w:rPr');
|
|
109
|
+
// In w:pPr, rPr comes after the paragraph properties and before sectPr / pPrChange.
|
|
110
|
+
const after = elements(pPr).find((c) => c.name === 'w:sectPr' || c.name === 'w:pPrChange');
|
|
111
|
+
if (after)
|
|
112
|
+
pPr.children.splice(pPr.children.indexOf(after), 0, rPr);
|
|
113
|
+
else
|
|
114
|
+
pPr.children.push(rPr);
|
|
115
|
+
}
|
|
116
|
+
return rPr;
|
|
117
|
+
}
|
|
118
|
+
/** How a paragraph's mark (its end) is tracked: inserted, deleted, or not at all. */
|
|
119
|
+
export function paragraphMarkRevision(p) {
|
|
120
|
+
const rPr = firstChild(firstChild(p, 'w:pPr'), 'w:rPr');
|
|
121
|
+
const ins = firstChild(rPr, 'w:ins') ?? firstChild(rPr, 'w:moveTo');
|
|
122
|
+
if (ins)
|
|
123
|
+
return { kind: 'ins', el: ins };
|
|
124
|
+
const del = firstChild(rPr, 'w:del') ?? firstChild(rPr, 'w:moveFrom');
|
|
125
|
+
if (del)
|
|
126
|
+
return { kind: 'del', el: del };
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
export function markParagraph(p, kind, id, who) {
|
|
130
|
+
const rPr = markRPr(p);
|
|
131
|
+
removeChildren(rPr, 'w:ins');
|
|
132
|
+
removeChildren(rPr, 'w:del');
|
|
133
|
+
// w:ins / w:del come first in a paragraph mark's rPr.
|
|
134
|
+
rPr.children.unshift(el(kind === 'ins' ? 'w:ins' : 'w:del', stampAttrs(id, who)));
|
|
135
|
+
}
|
|
136
|
+
/** Record the run's current formatting before it changes (once: the oldest state is what counts). */
|
|
137
|
+
export function recordRunFormat(run, id, who) {
|
|
138
|
+
let rPr = firstChild(run, 'w:rPr');
|
|
139
|
+
if (!rPr) {
|
|
140
|
+
rPr = el('w:rPr');
|
|
141
|
+
run.children.unshift(rPr);
|
|
142
|
+
}
|
|
143
|
+
if (firstChild(rPr, 'w:rPrChange'))
|
|
144
|
+
return;
|
|
145
|
+
const old = el('w:rPr', {}, elements(rPr).map(cloneElement));
|
|
146
|
+
rPr.children.push(el('w:rPrChange', stampAttrs(id, who), [old]));
|
|
147
|
+
}
|
|
148
|
+
/** Record a paragraph's current properties before they change. */
|
|
149
|
+
export function recordParagraphFormat(p, id, who, order) {
|
|
150
|
+
let pPr = firstChild(p, 'w:pPr');
|
|
151
|
+
if (!pPr) {
|
|
152
|
+
pPr = el('w:pPr');
|
|
153
|
+
p.children.unshift(pPr);
|
|
154
|
+
}
|
|
155
|
+
if (firstChild(pPr, 'w:pPrChange'))
|
|
156
|
+
return;
|
|
157
|
+
const old = el('w:pPr', {}, elements(pPr)
|
|
158
|
+
.filter((c) => c.name !== 'w:rPr' && c.name !== 'w:sectPr' && c.name !== 'w:pPrChange')
|
|
159
|
+
.map(cloneElement));
|
|
160
|
+
insertOrdered(pPr, el('w:pPrChange', stampAttrs(id, who), [old]), order);
|
|
161
|
+
}
|
|
162
|
+
// ---- listing ---------------------------------------------------------------------------------------
|
|
163
|
+
function textOfRuns(node) {
|
|
164
|
+
let s = '';
|
|
165
|
+
const walk = (n) => {
|
|
166
|
+
for (const c of n.children) {
|
|
167
|
+
if (c.type !== 'element')
|
|
168
|
+
continue;
|
|
169
|
+
if (c.name === 'w:t' || c.name === 'w:delText')
|
|
170
|
+
s += textContent(c);
|
|
171
|
+
else if (c.name === 'w:tab')
|
|
172
|
+
s += '\t';
|
|
173
|
+
else if (c.name === 'w:br' || c.name === 'w:cr')
|
|
174
|
+
s += '\n';
|
|
175
|
+
else
|
|
176
|
+
walk(c);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
walk(node);
|
|
180
|
+
return s;
|
|
181
|
+
}
|
|
182
|
+
function parentsOf(root) {
|
|
183
|
+
const parent = new Map();
|
|
184
|
+
const walk = (n) => {
|
|
185
|
+
for (const c of n.children) {
|
|
186
|
+
if (c.type !== 'element')
|
|
187
|
+
continue;
|
|
188
|
+
parent.set(c, n);
|
|
189
|
+
walk(c);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
walk(root);
|
|
193
|
+
return { parent };
|
|
194
|
+
}
|
|
195
|
+
function ancestor(p, node, name) {
|
|
196
|
+
for (let cur = p.parent.get(node); cur; cur = p.parent.get(cur))
|
|
197
|
+
if (cur.name === name)
|
|
198
|
+
return cur;
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
/** Every revision in a part, in document order. */
|
|
202
|
+
export function listRevisions(root) {
|
|
203
|
+
const p = parentsOf(root);
|
|
204
|
+
const out = [];
|
|
205
|
+
const walk = (node) => {
|
|
206
|
+
for (const c of node.children) {
|
|
207
|
+
if (c.type !== 'element')
|
|
208
|
+
continue;
|
|
209
|
+
const id = attr(c, 'w:id') ?? '';
|
|
210
|
+
const author = attr(c, 'w:author') ?? '';
|
|
211
|
+
const date = attr(c, 'w:date');
|
|
212
|
+
const base = { id, author, ...(date ? { date } : {}), el: c };
|
|
213
|
+
const parentName = p.parent.get(c)?.name;
|
|
214
|
+
if (RUN_REVISIONS.has(c.name)) {
|
|
215
|
+
if (parentName === 'w:rPr') {
|
|
216
|
+
// A paragraph mark (w:pPr/w:rPr) or a table row (w:trPr) revision.
|
|
217
|
+
const para = ancestor(p, c, 'w:p');
|
|
218
|
+
const kind = c.name === 'w:ins' || c.name === 'w:moveTo' ? 'paragraph-insert' : 'paragraph-delete';
|
|
219
|
+
if (para && p.parent.get(p.parent.get(c))?.name === 'w:pPr')
|
|
220
|
+
out.push({ ...base, kind, text: '', paragraph: para });
|
|
221
|
+
}
|
|
222
|
+
else if (parentName === 'w:trPr') {
|
|
223
|
+
const row = ancestor(p, c, 'w:tr');
|
|
224
|
+
out.push({ ...base, kind: c.name === 'w:ins' ? 'row-insert' : 'row-delete', text: textOfRuns(row), paragraph: findAll(row, 'w:p')[0] });
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
const kind = c.name === 'w:ins' ? 'insert' : c.name === 'w:del' ? 'delete' : c.name === 'w:moveFrom' ? 'move-from' : 'move-to';
|
|
228
|
+
out.push({ ...base, kind, text: textOfRuns(c), ...(ancestor(p, c, 'w:p') ? { paragraph: ancestor(p, c, 'w:p') } : {}) });
|
|
229
|
+
walk(c);
|
|
230
|
+
}
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (c.name === 'w:p') {
|
|
234
|
+
// The paragraph mark is the end of the paragraph: list its revision after the text's.
|
|
235
|
+
const start = out.length;
|
|
236
|
+
walk(c);
|
|
237
|
+
const marks = out.slice(start).filter((r) => (r.kind === 'paragraph-insert' || r.kind === 'paragraph-delete') && r.paragraph === c);
|
|
238
|
+
if (marks.length) {
|
|
239
|
+
const rest = out.slice(start).filter((r) => !marks.includes(r));
|
|
240
|
+
out.splice(start, out.length - start, ...rest, ...marks);
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (PROPERTY_CHANGES.has(c.name)) {
|
|
245
|
+
const kind = c.name === 'w:rPrChange' ? 'format' : c.name === 'w:pPrChange' ? 'paragraph-format' : 'property';
|
|
246
|
+
const run = ancestor(p, c, 'w:r');
|
|
247
|
+
out.push({ ...base, kind, text: run ? textOfRuns(run) : '', ...(ancestor(p, c, 'w:p') ? { paragraph: ancestor(p, c, 'w:p') } : {}) });
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
walk(c);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
walk(root);
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
// ---- accepting and rejecting -----------------------------------------------------------------------
|
|
257
|
+
function matches(r, filter) {
|
|
258
|
+
if (!filter)
|
|
259
|
+
return true;
|
|
260
|
+
if (filter.ids && !filter.ids.includes(r.id))
|
|
261
|
+
return false;
|
|
262
|
+
if (filter.author !== undefined && r.author !== filter.author)
|
|
263
|
+
return false;
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
function unwrap(p, node) {
|
|
267
|
+
const parent = p.parent.get(node);
|
|
268
|
+
if (!parent)
|
|
269
|
+
return;
|
|
270
|
+
parent.children.splice(parent.children.indexOf(node), 1, ...node.children);
|
|
271
|
+
for (const c of node.children)
|
|
272
|
+
if (c.type === 'element')
|
|
273
|
+
p.parent.set(c, parent);
|
|
274
|
+
}
|
|
275
|
+
function remove(p, node) {
|
|
276
|
+
const parent = p.parent.get(node);
|
|
277
|
+
if (parent)
|
|
278
|
+
parent.children.splice(parent.children.indexOf(node), 1);
|
|
279
|
+
}
|
|
280
|
+
/** Join a paragraph into the one after it (its mark goes away); the next paragraph keeps its properties. */
|
|
281
|
+
function joinWithNext(p, para) {
|
|
282
|
+
const parent = p.parent.get(para);
|
|
283
|
+
if (!parent)
|
|
284
|
+
return;
|
|
285
|
+
const i = parent.children.indexOf(para);
|
|
286
|
+
let next;
|
|
287
|
+
for (let j = i + 1; j < parent.children.length; j++) {
|
|
288
|
+
const c = parent.children[j];
|
|
289
|
+
if (c.type === 'element' && c.name === 'w:p') {
|
|
290
|
+
next = c;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
if (c.type === 'element' && (c.name === 'w:tbl' || c.name === 'w:sdt'))
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
const content = para.children.filter((c) => !(c.type === 'element' && c.name === 'w:pPr'));
|
|
297
|
+
if (!next) {
|
|
298
|
+
// Nothing to join with (last paragraph of its container): just drop the mark's revision.
|
|
299
|
+
const rPr = firstChild(firstChild(para, 'w:pPr'), 'w:rPr');
|
|
300
|
+
if (rPr)
|
|
301
|
+
for (const n of ['w:ins', 'w:del', 'w:moveTo', 'w:moveFrom'])
|
|
302
|
+
removeChildren(rPr, n);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const nextPPr = firstChild(next, 'w:pPr');
|
|
306
|
+
const at = nextPPr ? next.children.indexOf(nextPPr) + 1 : 0;
|
|
307
|
+
next.children.splice(at, 0, ...content);
|
|
308
|
+
for (const c of content)
|
|
309
|
+
if (c.type === 'element')
|
|
310
|
+
p.parent.set(c, next);
|
|
311
|
+
parent.children.splice(i, 1);
|
|
312
|
+
}
|
|
313
|
+
function restoreProperties(p, change) {
|
|
314
|
+
const owner = p.parent.get(change);
|
|
315
|
+
if (!owner)
|
|
316
|
+
return;
|
|
317
|
+
const old = elements(change)[0];
|
|
318
|
+
const keep = owner.name === 'w:pPr' ? elements(owner).filter((c) => c.name === 'w:rPr' || c.name === 'w:sectPr') : [];
|
|
319
|
+
owner.children = [...(old ? old.children : []), ...keep];
|
|
320
|
+
}
|
|
321
|
+
function apply(root, filter, accept) {
|
|
322
|
+
const p = parentsOf(root);
|
|
323
|
+
const revisions = listRevisions(root).filter((r) => matches(r, filter));
|
|
324
|
+
// Innermost and latest first, so nested revisions are resolved before their containers.
|
|
325
|
+
for (const r of [...revisions].reverse()) {
|
|
326
|
+
const node = r.el;
|
|
327
|
+
switch (r.kind) {
|
|
328
|
+
case 'insert':
|
|
329
|
+
case 'move-to':
|
|
330
|
+
if (accept)
|
|
331
|
+
unwrap(p, node);
|
|
332
|
+
else
|
|
333
|
+
remove(p, node);
|
|
334
|
+
break;
|
|
335
|
+
case 'delete':
|
|
336
|
+
case 'move-from':
|
|
337
|
+
if (accept)
|
|
338
|
+
remove(p, node);
|
|
339
|
+
else {
|
|
340
|
+
toLiveText(node);
|
|
341
|
+
unwrap(p, node);
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
case 'format':
|
|
345
|
+
case 'paragraph-format':
|
|
346
|
+
case 'property':
|
|
347
|
+
if (!accept)
|
|
348
|
+
restoreProperties(p, node);
|
|
349
|
+
remove(p, node);
|
|
350
|
+
break;
|
|
351
|
+
case 'paragraph-insert':
|
|
352
|
+
remove(p, node);
|
|
353
|
+
if (!accept && r.paragraph)
|
|
354
|
+
joinWithNext(p, r.paragraph);
|
|
355
|
+
break;
|
|
356
|
+
case 'paragraph-delete':
|
|
357
|
+
remove(p, node);
|
|
358
|
+
if (accept && r.paragraph)
|
|
359
|
+
joinWithNext(p, r.paragraph);
|
|
360
|
+
break;
|
|
361
|
+
case 'row-insert': {
|
|
362
|
+
const row = p.parent.get(p.parent.get(node));
|
|
363
|
+
remove(p, node);
|
|
364
|
+
if (!accept && row)
|
|
365
|
+
remove(p, row);
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
case 'row-delete': {
|
|
369
|
+
const row = p.parent.get(p.parent.get(node));
|
|
370
|
+
remove(p, node);
|
|
371
|
+
if (accept && row)
|
|
372
|
+
remove(p, row);
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
// Move range markers mean nothing once their moves are resolved.
|
|
378
|
+
if (revisions.some((r) => r.kind === 'move-from' || r.kind === 'move-to')) {
|
|
379
|
+
for (const name of RANGE_MARKERS)
|
|
380
|
+
for (const m of findAll(root, name))
|
|
381
|
+
remove(parentsOf(root), m);
|
|
382
|
+
}
|
|
383
|
+
return revisions.length;
|
|
384
|
+
}
|
|
385
|
+
export function acceptRevisions(root, filter) {
|
|
386
|
+
return apply(root, filter, true);
|
|
387
|
+
}
|
|
388
|
+
export function rejectRevisions(root, filter) {
|
|
389
|
+
return apply(root, filter, false);
|
|
390
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { LayoutResult, StoryKind } from '../layout/types.js';
|
|
2
|
+
import type { DocParagraph } from '../model/content.js';
|
|
3
|
+
import type { DocxDocument } from '../model/document.js';
|
|
4
|
+
/** A place in the text: a paragraph number (from 1) and a character offset in it. */
|
|
5
|
+
export interface TextSpot {
|
|
6
|
+
paragraph: number;
|
|
7
|
+
offset: number;
|
|
8
|
+
}
|
|
9
|
+
export interface SelectedParagraph {
|
|
10
|
+
paragraph: number;
|
|
11
|
+
/** The paragraph's whole text. */
|
|
12
|
+
text: string;
|
|
13
|
+
/** The selected part of `text`: [from, to). Equal for a caret. */
|
|
14
|
+
from: number;
|
|
15
|
+
to: number;
|
|
16
|
+
/** Paragraph style name, when not the normal style. */
|
|
17
|
+
style?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface SelectionInfo {
|
|
20
|
+
/** Which text the selection is in (paragraph numbers count within that story). */
|
|
21
|
+
story: 'body' | StoryKind;
|
|
22
|
+
/** The header, footer or notes part, for selections outside the body. */
|
|
23
|
+
part?: string;
|
|
24
|
+
/** Nothing selected, just a caret (or a click) at `start`. */
|
|
25
|
+
collapsed: boolean;
|
|
26
|
+
/** The selected text, paragraphs separated by newlines ('' for a caret). */
|
|
27
|
+
text: string;
|
|
28
|
+
start: TextSpot;
|
|
29
|
+
end: TextSpot;
|
|
30
|
+
paragraphs: SelectedParagraph[];
|
|
31
|
+
/** Pages (from 1) the selection appears on. */
|
|
32
|
+
pages: number[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A selection from paragraph positions (0-based indices into `paragraphs`, which are the story's
|
|
36
|
+
* paragraphs in document order, table cells included).
|
|
37
|
+
*/
|
|
38
|
+
export declare function selectionInfoFor(doc: DocxDocument, paragraphs: DocParagraph[], anchor: {
|
|
39
|
+
para: number;
|
|
40
|
+
char: number;
|
|
41
|
+
}, focus: {
|
|
42
|
+
para: number;
|
|
43
|
+
char: number;
|
|
44
|
+
}, options?: {
|
|
45
|
+
story?: SelectionInfo['story'];
|
|
46
|
+
part?: string;
|
|
47
|
+
layout?: LayoutResult;
|
|
48
|
+
}): SelectionInfo | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The selection as a few lines of text for an AI prompt: where it is, and each paragraph with the
|
|
51
|
+
* selected part in «guillemets» (or ‸ for a caret), shortened around the selection.
|
|
52
|
+
*/
|
|
53
|
+
export declare function describeSelection(info: SelectionInfo, options?: {
|
|
54
|
+
document?: string;
|
|
55
|
+
maxChars?: number;
|
|
56
|
+
context?: number;
|
|
57
|
+
}): string;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the user has selected, in terms an AI assistant can act on: paragraph numbers (the same
|
|
3
|
+
* numbering as docxkeun-mcp's read_document), character offsets, pages, and the text with a
|
|
4
|
+
* little context, like the "selected lines" an editor integration shares with a coding agent.
|
|
5
|
+
*/
|
|
6
|
+
import { paragraphText as plainText } from './editor.js';
|
|
7
|
+
/** Paragraph text with inline pictures as U+FFFC, the offsets the viewer's caret uses. */
|
|
8
|
+
const paragraphText = (p) => plainText(p, { objects: true });
|
|
9
|
+
/**
|
|
10
|
+
* A selection from paragraph positions (0-based indices into `paragraphs`, which are the story's
|
|
11
|
+
* paragraphs in document order, table cells included).
|
|
12
|
+
*/
|
|
13
|
+
export function selectionInfoFor(doc, paragraphs, anchor, focus, options = {}) {
|
|
14
|
+
if (!paragraphs.length)
|
|
15
|
+
return undefined;
|
|
16
|
+
const clamp = (c) => {
|
|
17
|
+
const para = Math.max(0, Math.min(c.para, paragraphs.length - 1));
|
|
18
|
+
return { para, char: Math.max(0, Math.min(c.char, paragraphText(paragraphs[para]).text.length)) };
|
|
19
|
+
};
|
|
20
|
+
let a = clamp(anchor);
|
|
21
|
+
let b = clamp(focus);
|
|
22
|
+
if (b.para < a.para || (b.para === a.para && b.char < a.char))
|
|
23
|
+
[a, b] = [b, a];
|
|
24
|
+
const out = [];
|
|
25
|
+
const texts = [];
|
|
26
|
+
for (let p = a.para; p <= b.para; p++) {
|
|
27
|
+
const para = paragraphs[p];
|
|
28
|
+
const text = paragraphText(para).text;
|
|
29
|
+
const from = p === a.para ? a.char : 0;
|
|
30
|
+
const to = p === b.para ? b.char : text.length;
|
|
31
|
+
const style = para.styleId && para.styleId !== doc.styles.defaults.paragraph ? (doc.styles.get(para.styleId)?.name ?? para.styleId) : undefined;
|
|
32
|
+
out.push({ paragraph: p + 1, text, from, to, ...(style ? { style } : {}) });
|
|
33
|
+
texts.push(text.slice(from, to));
|
|
34
|
+
}
|
|
35
|
+
const story = options.story ?? 'body';
|
|
36
|
+
return {
|
|
37
|
+
story,
|
|
38
|
+
...(options.part ? { part: options.part } : {}),
|
|
39
|
+
collapsed: a.para === b.para && a.char === b.char,
|
|
40
|
+
text: texts.join('\n').replace(//g, ''),
|
|
41
|
+
start: { paragraph: a.para + 1, offset: a.char },
|
|
42
|
+
end: { paragraph: b.para + 1, offset: b.char },
|
|
43
|
+
paragraphs: out,
|
|
44
|
+
pages: options.layout ? pagesOf(options.layout, paragraphs.slice(a.para, b.para + 1), story) : [],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function pagesOf(layout, paras, story) {
|
|
48
|
+
const els = new Set(paras.map((p) => p.el));
|
|
49
|
+
const pages = [];
|
|
50
|
+
for (const page of layout.pages) {
|
|
51
|
+
const lines = story === 'body' ? page.lines : page.storyLines;
|
|
52
|
+
if (lines.some((l) => els.has(l.paragraph)))
|
|
53
|
+
pages.push(page.index + 1);
|
|
54
|
+
}
|
|
55
|
+
return pages;
|
|
56
|
+
}
|
|
57
|
+
const range = (ns) => (ns.length === 0 ? '' : ns.length === 1 || ns[0] === ns[ns.length - 1] ? String(ns[0]) : `${ns[0]}–${ns[ns.length - 1]}`);
|
|
58
|
+
/**
|
|
59
|
+
* The selection as a few lines of text for an AI prompt: where it is, and each paragraph with the
|
|
60
|
+
* selected part in «guillemets» (or ‸ for a caret), shortened around the selection.
|
|
61
|
+
*/
|
|
62
|
+
export function describeSelection(info, options = {}) {
|
|
63
|
+
const max = options.maxChars ?? 2000;
|
|
64
|
+
const around = options.context ?? 60;
|
|
65
|
+
const where = [];
|
|
66
|
+
const n = info.paragraphs.length;
|
|
67
|
+
where.push(n > 1 ? `paragraphs ${info.start.paragraph}–${info.end.paragraph}` : `paragraph ${info.start.paragraph}`);
|
|
68
|
+
if (info.story !== 'body')
|
|
69
|
+
where.unshift(info.part ? `${info.story} (${info.part})` : info.story);
|
|
70
|
+
if (info.pages.length)
|
|
71
|
+
where.push(`page${info.pages.length > 1 ? 's' : ''} ${range(info.pages)}`);
|
|
72
|
+
const doc = options.document ? ` in ${options.document}` : '';
|
|
73
|
+
const head = info.collapsed
|
|
74
|
+
? `Cursor${doc}: ${where.join(', ')}, nothing selected.`
|
|
75
|
+
: `Selected${doc}: ${where.join(', ')}, ${info.text.length} character${info.text.length === 1 ? '' : 's'}.`;
|
|
76
|
+
let budget = max;
|
|
77
|
+
const lines = info.paragraphs.map((p) => {
|
|
78
|
+
const before = p.text.slice(Math.max(0, p.from - around), p.from);
|
|
79
|
+
const after = p.text.slice(p.to, p.to + around);
|
|
80
|
+
let chosen = p.text.slice(p.from, p.to);
|
|
81
|
+
if (chosen.length > budget)
|
|
82
|
+
chosen = `${chosen.slice(0, Math.max(0, budget))}… (${chosen.length - budget} more characters)`;
|
|
83
|
+
budget = Math.max(0, budget - chosen.length);
|
|
84
|
+
const lead = p.from - around > 0 ? '…' : '';
|
|
85
|
+
const tail = p.to + around < p.text.length ? '…' : '';
|
|
86
|
+
const mark = info.collapsed ? '‸' : `«${chosen}»`;
|
|
87
|
+
const style = p.style ? ` (${p.style})` : '';
|
|
88
|
+
return `[${p.paragraph}]${style} ${lead}${before}${mark}${after}${tail}`.replace(//g, '').replace(/\s*\n\s*/g, ' ');
|
|
89
|
+
});
|
|
90
|
+
return [head, ...lines].join('\n');
|
|
91
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DrawingML colours as charts use them: a:srgbClr, a:schemeClr, a:sysClr, a:prstClr, a:scrgbClr and
|
|
3
|
+
* a:hslClr, with the transforms Office applies (lumMod/lumOff/sat/hue in HSL, tint/shade in linear
|
|
4
|
+
* RGB, alpha), plus the fill and line properties of a c:spPr.
|
|
5
|
+
*/
|
|
6
|
+
import { type XmlElement } from '../xml/dom.js';
|
|
7
|
+
/** Theme colour lookup by scheme name (accent1, tx1, bg1, dk1, lt1, …) → RRGGBB. */
|
|
8
|
+
export type SchemeColorFn = (name: string) => string | undefined;
|
|
9
|
+
/** A colour with its opacity (0..1). */
|
|
10
|
+
export interface Paint {
|
|
11
|
+
color: string;
|
|
12
|
+
opacity: number;
|
|
13
|
+
}
|
|
14
|
+
/** Look a scheme name up in the caller's theme, then through the tx/bg aliases, then Office's default theme. */
|
|
15
|
+
export declare function lookupScheme(name: string, scheme: SchemeColorFn | undefined): string;
|
|
16
|
+
/** Resolve a colour element itself (a:srgbClr, a:schemeClr, …). `phClr` stands for `placeholder` when given. */
|
|
17
|
+
export declare function resolveColorElement(node: XmlElement, scheme: SchemeColorFn | undefined, placeholder?: string): Paint | undefined;
|
|
18
|
+
/** The colour inside a container such as a:solidFill, a:fgClr or a:gs. */
|
|
19
|
+
export declare function colorIn(container: XmlElement | undefined, scheme: SchemeColorFn | undefined, placeholder?: string): Paint | undefined;
|
|
20
|
+
/** A scheme colour with lumMod/lumOff (the series palette variations), computed without XML. */
|
|
21
|
+
export declare function schemeVariant(name: string, scheme: SchemeColorFn | undefined, lumMod?: number, lumOff?: number): string;
|
|
22
|
+
/** Shift a colour's HSL lightness (used for derived shades such as monochrome chart styles). */
|
|
23
|
+
export declare function adjustLightness(hex: string, lumMod: number, lumOff?: number): string;
|
|
24
|
+
/** Relative brightness 0..1, to pick a readable text colour over a fill. */
|
|
25
|
+
export declare function brightness(hex: string): number;
|
|
26
|
+
/**
|
|
27
|
+
* The fill an element group (c:spPr, a:ln) declares: `null` for a:noFill, undefined when nothing is
|
|
28
|
+
* said (the chart's automatic formatting applies). Gradients use their first stop and patterns their
|
|
29
|
+
* foreground, since the display list only has flat fills.
|
|
30
|
+
*/
|
|
31
|
+
export declare function fillOf(spPr: XmlElement | undefined, scheme: SchemeColorFn | undefined, placeholder?: string): Paint | null | undefined;
|
|
32
|
+
/** Line properties of a c:spPr's a:ln (or of an a:ln directly). Unset parts stay undefined. */
|
|
33
|
+
export interface LineProps {
|
|
34
|
+
/** `null` for a:noFill (no line). */
|
|
35
|
+
paint?: Paint | null;
|
|
36
|
+
/** Width in points. */
|
|
37
|
+
width?: number;
|
|
38
|
+
/** a:prstDash value (solid, dash, sysDot, …). */
|
|
39
|
+
dash?: string;
|
|
40
|
+
cap?: 'butt' | 'round' | 'square';
|
|
41
|
+
join?: 'miter' | 'round' | 'bevel';
|
|
42
|
+
}
|
|
43
|
+
export declare function lineOf(spPr: XmlElement | undefined, scheme: SchemeColorFn | undefined, placeholder?: string): LineProps;
|
|
44
|
+
/** Dash pattern in points for an a:prstDash preset, scaled by the line width as Office does. */
|
|
45
|
+
export declare function dashPattern(dash: string | undefined, width: number): number[] | undefined;
|