onionskin-cli 0.0.0-stage → 0.0.1
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 +136 -0
- package/INSTALL.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE +46 -0
- package/README.md +88 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +54 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +247 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1910 -0
- package/src/settings.js +161 -0
- package/src/setup.js +160 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-CFISATkn.js +197 -0
- package/web/dist/assets/index-spXE4oTz.css +1 -0
- package/web/dist/index.html +14 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { attr, elements, escAttr, escText, parse, serialize } from "../xml-tree.js";
|
|
2
|
+
|
|
3
|
+
// Row and column edits of a DOCX table, done on word/document.xml with the lossless element tree (src/xml-tree.js): every byte outside the
|
|
4
|
+
// rows and cells an op adds or removes stays as the writer left it. The DOCX driver (src/ops/docx.js) checks the ops at bind and runs them
|
|
5
|
+
// on the engine's saved bytes, so the engine's own paragraph edits come first and these structural edits last.
|
|
6
|
+
//
|
|
7
|
+
// One `TableEdit` per table holds the rows and columns of the table as the file had them, so every op of one ops file addresses the table
|
|
8
|
+
// as delivered: a row inserted after row 2 by an earlier op does not move row 3. Rows inserted at the same place keep the order of the
|
|
9
|
+
// ops. A table is located by its order among top-level tables (`tableNo`, 1-based, as src/docx.js counts it), which paragraph edits
|
|
10
|
+
// cannot change, and rows and cells must be direct children (rows or cells wrapped in a content control are refused).
|
|
11
|
+
//
|
|
12
|
+
// Merged cells: a vertical merge (vMerge) is refused where a new row would split it or a deleted row is its first row; column ops are
|
|
13
|
+
// refused for any table with horizontal merges or offsets (gridSpan > 1, hMerge, gridBefore, gridAfter), where a column is not one
|
|
14
|
+
// cell per row.
|
|
15
|
+
|
|
16
|
+
export class TableError extends Error {
|
|
17
|
+
constructor(code, message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const no = (code, message) => {
|
|
23
|
+
throw new TableError(code, message);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const kidsNamed = (el, name) => elements(el).filter((k) => k.name === name);
|
|
27
|
+
const kidNamed = (el, name) => elements(el).find((k) => k.name === name);
|
|
28
|
+
const clone = (node) => parse(serialize(node)).kids[0];
|
|
29
|
+
const valOf = (el, name) => {
|
|
30
|
+
const k = el && kidNamed(el, name);
|
|
31
|
+
return k ? (attr(k, "w:val") ?? "") : null;
|
|
32
|
+
};
|
|
33
|
+
/** removes the named direct children (whitespace stays; it is insignificant between these elements) */
|
|
34
|
+
const dropKids = (el, names) => el && (el.kids = el.kids.filter((k) => !(k.t === "el" && names.includes(k.name))));
|
|
35
|
+
/** the open tag without the named attributes */
|
|
36
|
+
const withoutAttrs = (open, names) => names.reduce((o, n) => o.replace(new RegExp(`\\s${n.replace(/[.:]/g, "\\$&")}\\s*=\\s*("[^"]*"|'[^']*')`, "g"), ""), open);
|
|
37
|
+
function setAttr(el, name, value) {
|
|
38
|
+
const has = attr(el, name) !== undefined;
|
|
39
|
+
if (has) el.open = el.open.replace(new RegExp(`(\\s${name.replace(/[.:]/g, "\\$&")}\\s*=\\s*)("[^"]*"|'[^']*')`), `$1"${escAttr(value)}"`);
|
|
40
|
+
else el.open = el.open.replace(/(\s*\/?>)$/, ` ${name}="${escAttr(value)}"$1`);
|
|
41
|
+
}
|
|
42
|
+
function insertAt(parent, index, node) {
|
|
43
|
+
node.parent = parent;
|
|
44
|
+
parent.kids.splice(index, 0, node);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Top-level tables of the body in document order: what src/docx.js numbers 1, 2, 3 (a table with no table above it, anywhere in w:body). */
|
|
48
|
+
export function topTables(root) {
|
|
49
|
+
const out = [];
|
|
50
|
+
const walk = (el, inBody) => {
|
|
51
|
+
for (const k of elements(el)) {
|
|
52
|
+
if (k.name === "w:tbl" && inBody) out.push(k);
|
|
53
|
+
else walk(k, inBody || k.name === "w:body");
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
walk(root, false);
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A row's cells with their grid columns: [{tc, start, span, vMerge: null|"restart"|"continue", hMerge}], plus gridBefore/gridAfter. */
|
|
61
|
+
export function rowGrid(tr) {
|
|
62
|
+
const trPr = kidNamed(tr, "w:trPr");
|
|
63
|
+
const before = Number(valOf(trPr, "w:gridBefore") || 0);
|
|
64
|
+
const after = Number(valOf(trPr, "w:gridAfter") || 0);
|
|
65
|
+
let g = before;
|
|
66
|
+
const cells = kidsNamed(tr, "w:tc").map((tc) => {
|
|
67
|
+
const pr = kidNamed(tc, "w:tcPr");
|
|
68
|
+
const span = Math.max(1, Number(valOf(pr, "w:gridSpan") || 1));
|
|
69
|
+
const vm = pr && kidNamed(pr, "w:vMerge");
|
|
70
|
+
const cell = { tc, start: g, span, vMerge: vm ? (attr(vm, "w:val") === "restart" ? "restart" : "continue") : null, hMerge: !!(pr && kidNamed(pr, "w:hMerge")) };
|
|
71
|
+
g += span;
|
|
72
|
+
return cell;
|
|
73
|
+
});
|
|
74
|
+
return { cells, before, after };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const XML_BAD = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/; // not allowed in XML 1.0
|
|
78
|
+
/** One run's content: TAB and LF become w:tab and w:br, like the editor writes them. */
|
|
79
|
+
function runContent(text) {
|
|
80
|
+
return text
|
|
81
|
+
.split(/(\t|\n)/)
|
|
82
|
+
.filter((s) => s !== "")
|
|
83
|
+
.map((s) => (s === "\t" ? "<w:tab/>" : s === "\n" ? "<w:br/>" : `<w:t xml:space="preserve">${escText(s)}</w:t>`))
|
|
84
|
+
.join("");
|
|
85
|
+
}
|
|
86
|
+
const RANGE_MARK = /^w:(commentRange|bookmark|moveFromRange|moveToRange|perm|customXml\w*Range)(Start|End)$/;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A fresh cell built from `tc` (the neighbor): its cell properties without the vertical merge and tracked-change records, one paragraph
|
|
90
|
+
* with the neighbor's first paragraph properties and its first text run's properties, holding `text` (or empty). Nothing of the
|
|
91
|
+
* neighbor's content is copied, so no paragraph id, bookmark, comment or field is duplicated.
|
|
92
|
+
*/
|
|
93
|
+
function freshCell(tc, text) {
|
|
94
|
+
const pr = kidNamed(tc, "w:tcPr");
|
|
95
|
+
let tcPr = "";
|
|
96
|
+
if (pr) {
|
|
97
|
+
const c = clone(pr);
|
|
98
|
+
dropKids(c, ["w:vMerge", "w:cellIns", "w:cellDel", "w:cellMerge", "w:tcPrChange", "w:cnfStyle"]);
|
|
99
|
+
tcPr = serialize(c);
|
|
100
|
+
}
|
|
101
|
+
const p = kidNamed(tc, "w:p");
|
|
102
|
+
let pPr = "";
|
|
103
|
+
let rPr = "";
|
|
104
|
+
if (p) {
|
|
105
|
+
const pp = kidNamed(p, "w:pPr");
|
|
106
|
+
if (pp) {
|
|
107
|
+
const c = clone(pp);
|
|
108
|
+
dropKids(c, ["w:pPrChange", "w:sectPr", "w:cnfStyle"]);
|
|
109
|
+
dropKids(kidNamed(c, "w:rPr"), ["w:ins", "w:del", "w:moveFrom", "w:moveTo", "w:rPrChange"]);
|
|
110
|
+
pPr = serialize(c);
|
|
111
|
+
}
|
|
112
|
+
const run = findRun(p);
|
|
113
|
+
const rp = run && kidNamed(run, "w:rPr");
|
|
114
|
+
if (rp) {
|
|
115
|
+
const c = clone(rp);
|
|
116
|
+
dropKids(c, ["w:rPrChange"]);
|
|
117
|
+
rPr = serialize(c);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const body = text ? `<w:r>${rPr}${runContent(text)}</w:r>` : "";
|
|
121
|
+
return parse(`<w:tc>${tcPr}<w:p>${pPr}${body}</w:p></w:tc>`).kids[0];
|
|
122
|
+
}
|
|
123
|
+
/** the first run of a paragraph that holds text (outside deleted text), else its first run */
|
|
124
|
+
function findRun(p) {
|
|
125
|
+
let firstRun = null;
|
|
126
|
+
let withText = null;
|
|
127
|
+
const walk = (el) => {
|
|
128
|
+
for (const k of elements(el)) {
|
|
129
|
+
if (withText) return;
|
|
130
|
+
if (k.name === "w:del" || k.name === "w:pPr") continue;
|
|
131
|
+
if (k.name === "w:r") {
|
|
132
|
+
firstRun ??= k;
|
|
133
|
+
if (kidNamed(k, "w:t")) withText = k;
|
|
134
|
+
} else walk(k);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
walk(p);
|
|
138
|
+
return withText || firstRun;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const TWIPS = (s) => (/^\d+$/.test(s ?? "") ? Number(s) : null);
|
|
142
|
+
|
|
143
|
+
/** The edits of one table in one ops file. Throws TableError for what it refuses; never leaves the table half changed. */
|
|
144
|
+
export class TableEdit {
|
|
145
|
+
constructor(tbl) {
|
|
146
|
+
if (elements(tbl).some((k) => k.name === "w:sdt" || k.name === "w:customXml")) no("table_wrapped_rows", "the table has rows inside a content control; rows cannot be added or removed here");
|
|
147
|
+
if (tbl.parent && ancestorNamed(tbl, "w:p")) no("table_in_text_box", "the table is inside a text box");
|
|
148
|
+
this.tbl = tbl;
|
|
149
|
+
this.orig = kidsNamed(tbl, "w:tr");
|
|
150
|
+
for (const tr of this.orig) if (elements(tr).some((k) => k.name === "w:sdt" || k.name === "w:customXml")) no("table_wrapped_rows", "a row has cells inside a content control; it cannot be edited here");
|
|
151
|
+
const grid = kidNamed(tbl, "w:tblGrid");
|
|
152
|
+
this.cols = grid ? kidsNamed(grid, "w:gridCol").length : 0;
|
|
153
|
+
this.colMap = Array.from({ length: this.cols }, (_, i) => i); // original column -> current grid column, -1 once deleted
|
|
154
|
+
this.afterCols = new Map();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
rows() {
|
|
158
|
+
return kidsNamed(this.tbl, "w:tr");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#row(r) {
|
|
162
|
+
const tr = this.orig[r];
|
|
163
|
+
if (!tr) no("no_such_row", `the table has no row ${r} (rows are 0-based; it has ${this.orig.length})`);
|
|
164
|
+
if (tr.gone) no("row_deleted", `row ${r} was deleted by an earlier op of this file`);
|
|
165
|
+
return tr;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Inserts a row before or after original row `r`; returns the new row's 0-based index in the table. */
|
|
169
|
+
insertRow(r, where, cells = []) {
|
|
170
|
+
const ref = this.#row(r);
|
|
171
|
+
if (cells.some((t) => XML_BAD.test(t))) no("bad_text", "a cell text holds a control character");
|
|
172
|
+
const rows = this.rows();
|
|
173
|
+
const at = rows.indexOf(ref);
|
|
174
|
+
let j = at;
|
|
175
|
+
if (where === "after") for (j = at + 1; rows[j] && rows[j].addedAfter === r; j++);
|
|
176
|
+
const below = rows[j] ?? null; // the row that will be under the new one
|
|
177
|
+
if (below) for (const c of rowGrid(below).cells) if (c.vMerge === "continue") no("merged_cells", "a vertically merged cell spans the place of the new row; split the merge in Word first");
|
|
178
|
+
const refCells = kidsNamed(ref, "w:tc");
|
|
179
|
+
if (cells.length > refCells.length) no("too_many_cells", `the row has ${refCells.length} cell(s); ${cells.length} cell texts were given`);
|
|
180
|
+
const tr = clone(ref);
|
|
181
|
+
tr.open = withoutAttrs(tr.open, ["w14:paraId", "w14:textId"]);
|
|
182
|
+
const trPr = kidNamed(tr, "w:trPr");
|
|
183
|
+
if (trPr) {
|
|
184
|
+
dropKids(trPr, ["w:ins", "w:del", "w:trPrChange", "w:cnfStyle"]);
|
|
185
|
+
if (!(where === "before" && kidNamed(trPr, "w:tblHeader"))) dropKids(trPr, ["w:tblHeader"]); // a new row repeats as a header only above a header row
|
|
186
|
+
}
|
|
187
|
+
tr.kids = trPr ? [trPr] : [];
|
|
188
|
+
for (const [i, tc] of refCells.entries()) {
|
|
189
|
+
const fresh = freshCell(tc, cells[i] ?? "");
|
|
190
|
+
fresh.parent = tr;
|
|
191
|
+
tr.kids.push(fresh);
|
|
192
|
+
}
|
|
193
|
+
if (where === "after") tr.addedAfter = r;
|
|
194
|
+
const kids = this.tbl.kids;
|
|
195
|
+
insertAt(this.tbl, below ? kids.indexOf(below) : kids.indexOf(rows[rows.length - 1]) + 1, tr);
|
|
196
|
+
return this.rows().indexOf(tr);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
deleteRow(r) {
|
|
200
|
+
const ref = this.#row(r);
|
|
201
|
+
const rows = this.rows();
|
|
202
|
+
if (rows.length === 1) no("last_row", "the table has only this row; a table needs at least one");
|
|
203
|
+
if (looseMarks([ref])) no("open_range", "a comment or bookmark starts or ends in this row and goes on outside it");
|
|
204
|
+
const next = rows[rows.indexOf(ref) + 1];
|
|
205
|
+
if (next) {
|
|
206
|
+
const starts = new Set(rowGrid(ref).cells.filter((c) => c.vMerge === "restart").map((c) => c.start));
|
|
207
|
+
if (rowGrid(next).cells.some((c) => c.vMerge === "continue" && starts.has(c.start))) no("merged_cells", "this row is the first row of a vertically merged cell that goes on below; unmerge it in Word first");
|
|
208
|
+
}
|
|
209
|
+
this.tbl.kids.splice(this.tbl.kids.indexOf(ref), 1);
|
|
210
|
+
ref.gone = true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** every current row is one plain cell per grid column: what a column op needs */
|
|
214
|
+
#uniform() {
|
|
215
|
+
if (!this.cols) no("table_without_grid", "the table has no column grid (w:tblGrid)");
|
|
216
|
+
for (const tr of this.rows()) {
|
|
217
|
+
const g = rowGrid(tr);
|
|
218
|
+
if (g.before || g.after || g.cells.some((c) => c.span > 1 || c.hMerge) || g.cells.length !== this.cols) no("merged_cells", "the table has horizontally merged or offset cells, so a column is not one cell per row; columns cannot be added or removed here");
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
#col(c) {
|
|
223
|
+
if (c >= this.colMap.length) no("no_such_column", `the table has no column ${c} (columns are 0-based; it has ${this.colMap.length})`);
|
|
224
|
+
if (this.colMap[c] < 0) no("column_deleted", `column ${c} was deleted by an earlier op of this file`);
|
|
225
|
+
return this.colMap[c];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Inserts a column before or after original column `c`, scaling the widths so the table keeps its width; returns its 0-based index. */
|
|
229
|
+
insertColumn(c, where, cells = []) {
|
|
230
|
+
const cur = this.#col(c);
|
|
231
|
+
this.#uniform();
|
|
232
|
+
if (cells.some((t) => XML_BAD.test(t))) no("bad_text", "a cell text holds a control character");
|
|
233
|
+
const rows = this.rows();
|
|
234
|
+
if (cells.length > rows.length) no("too_many_cells", `the table has ${rows.length} row(s); ${cells.length} cell texts were given`);
|
|
235
|
+
const pos = where === "after" ? cur + 1 + (this.afterCols.get(c) || 0) : cur;
|
|
236
|
+
const grid = kidNamed(this.tbl, "w:tblGrid");
|
|
237
|
+
const gridCols = kidsNamed(grid, "w:gridCol");
|
|
238
|
+
const widths = gridCols.map((g) => TWIPS(attr(g, "w:w")));
|
|
239
|
+
const gc = clone(gridCols[cur]);
|
|
240
|
+
const kidsAt = (parent, list, i) => (i < list.length ? parent.kids.indexOf(list[i]) : parent.kids.indexOf(list[list.length - 1]) + 1);
|
|
241
|
+
insertAt(grid, kidsAt(grid, gridCols, pos), gc);
|
|
242
|
+
// widths: the new column is as wide as its neighbor, then every column is scaled so the sum stays what it was (the page still fits)
|
|
243
|
+
let next = null;
|
|
244
|
+
if (widths.every((w) => w !== null)) {
|
|
245
|
+
const total = widths.reduce((a, b) => a + b, 0);
|
|
246
|
+
const all = [...widths.slice(0, pos), widths[cur], ...widths.slice(pos)];
|
|
247
|
+
const sum = total + widths[cur];
|
|
248
|
+
next = all.map((w) => Math.max(1, Math.round((w * total) / sum)));
|
|
249
|
+
kidsNamed(grid, "w:gridCol").forEach((g, i) => setAttr(g, "w:w", String(next[i])));
|
|
250
|
+
}
|
|
251
|
+
for (const [i, tr] of rows.entries()) {
|
|
252
|
+
const tcs = kidsNamed(tr, "w:tc");
|
|
253
|
+
const fresh = freshCell(tcs[cur], cells[i] ?? "");
|
|
254
|
+
insertAt(tr, kidsAt(tr, tcs, pos), fresh);
|
|
255
|
+
if (next) kidsNamed(tr, "w:tc").forEach((tc, k) => {
|
|
256
|
+
const w = kidNamed(kidNamed(tc, "w:tcPr") ?? { kids: [] }, "w:tcW");
|
|
257
|
+
if (w && (attr(w, "w:type") ?? "dxa") === "dxa" && TWIPS(attr(w, "w:w")) !== null) setAttr(w, "w:w", String(next[k]));
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
if (where === "after") this.afterCols.set(c, (this.afterCols.get(c) || 0) + 1);
|
|
261
|
+
for (let k = 0; k < this.colMap.length; k++) if (this.colMap[k] >= pos) this.colMap[k]++;
|
|
262
|
+
this.cols++;
|
|
263
|
+
return pos;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
deleteColumn(c) {
|
|
267
|
+
const cur = this.#col(c);
|
|
268
|
+
this.#uniform();
|
|
269
|
+
if (this.cols === 1) no("last_column", "the table has only this column; a table needs at least one");
|
|
270
|
+
if (looseMarks(this.rows().map((tr) => kidsNamed(tr, "w:tc")[cur]))) no("open_range", "a comment or bookmark starts or ends in this column and goes on outside it");
|
|
271
|
+
const grid = kidNamed(this.tbl, "w:tblGrid");
|
|
272
|
+
const gc = kidsNamed(grid, "w:gridCol")[cur];
|
|
273
|
+
grid.kids.splice(grid.kids.indexOf(gc), 1);
|
|
274
|
+
for (const tr of this.rows()) {
|
|
275
|
+
const tc = kidsNamed(tr, "w:tc")[cur];
|
|
276
|
+
tr.kids.splice(tr.kids.indexOf(tc), 1);
|
|
277
|
+
}
|
|
278
|
+
this.colMap = this.colMap.map((k, i) => (i === c ? -1 : k > cur ? k - 1 : k));
|
|
279
|
+
this.cols--;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** true when `nodes` (the rows or cells an op removes) hold one end of a comment, bookmark or other range without the other end */
|
|
284
|
+
function looseMarks(nodes) {
|
|
285
|
+
const open = new Map();
|
|
286
|
+
const walk = (el) => {
|
|
287
|
+
for (const k of elements(el)) {
|
|
288
|
+
const m = RANGE_MARK.exec(k.name);
|
|
289
|
+
const id = m && attr(k, "w:id");
|
|
290
|
+
if (id !== undefined && m) {
|
|
291
|
+
const key = `${m[1]} ${id}`;
|
|
292
|
+
open.set(key, (open.get(key) || 0) + (m[2] === "Start" ? 1 : -1));
|
|
293
|
+
}
|
|
294
|
+
walk(k);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
for (const n of nodes) walk({ kids: [n] });
|
|
298
|
+
return [...open.values()].some((v) => v !== 0);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function ancestorNamed(el, name) {
|
|
302
|
+
for (let p = el.parent; p; p = p.parent) if (p.name === name) return p;
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The table edits of one ops file on one copy of document.xml. `run({kind, tableNo, args})` does one edit (kind: insertRow, deleteRow,
|
|
308
|
+
* insertColumn, deleteColumn) and returns its result; a TableError leaves the copy as it was. `xml()` is the copy now.
|
|
309
|
+
*/
|
|
310
|
+
export function tableEditor(xml) {
|
|
311
|
+
const root = parse(xml);
|
|
312
|
+
const tables = topTables(root);
|
|
313
|
+
const editors = new Map();
|
|
314
|
+
return {
|
|
315
|
+
run({ kind, tableNo, args }) {
|
|
316
|
+
const tbl = tables[tableNo - 1];
|
|
317
|
+
if (!tbl) no("no_such_table", `table ${tableNo} is not in the document`);
|
|
318
|
+
if (!editors.has(tbl)) editors.set(tbl, new TableEdit(tbl));
|
|
319
|
+
return editors.get(tbl)[kind](...args);
|
|
320
|
+
},
|
|
321
|
+
xml: () => serialize(root),
|
|
322
|
+
};
|
|
323
|
+
}
|