@4399ywkf/editor 0.9.3 → 0.10.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/README.md +72 -13
- package/dist/{DocView-YBRD4V4E.js → DocView-HCBFGLWI.js} +3 -3
- package/dist/{DocView-YBRD4V4E.js.map → DocView-HCBFGLWI.js.map} +1 -1
- package/dist/PptxView-7244KC7T.js +3 -0
- package/dist/{PptxView-ZH4FZKNO.js.map → PptxView-7244KC7T.js.map} +1 -1
- package/dist/SheetView-6Z5EPBN4.js +3 -0
- package/dist/{SheetView-YFK7BUS5.js.map → SheetView-6Z5EPBN4.js.map} +1 -1
- package/dist/{chunk-4MQS76M2.js → chunk-ABXCS4IZ.js} +27 -38
- package/dist/chunk-ABXCS4IZ.js.map +1 -0
- package/dist/{chunk-JWHQF2CZ.js → chunk-FMTC3EDQ.js} +3 -3
- package/dist/{chunk-JWHQF2CZ.js.map → chunk-FMTC3EDQ.js.map} +1 -1
- package/dist/{chunk-3QTRZBIK.js → chunk-OBKIMG6G.js} +394 -46
- package/dist/chunk-OBKIMG6G.js.map +1 -0
- package/dist/{chunk-2DI4FJPC.js → chunk-OO2NIO3M.js} +8 -33
- package/dist/chunk-OO2NIO3M.js.map +1 -0
- package/dist/chunk-PKIYRSCM.js +2089 -0
- package/dist/chunk-PKIYRSCM.js.map +1 -0
- package/dist/{chunk-XB6FEC4I.js → chunk-PLKLRMV2.js} +3 -3
- package/dist/{chunk-XB6FEC4I.js.map → chunk-PLKLRMV2.js.map} +1 -1
- package/dist/chunk-QU25OJJ6.js +1189 -0
- package/dist/chunk-QU25OJJ6.js.map +1 -0
- package/dist/chunk-W2ES4Q4B.js +57 -0
- package/dist/chunk-W2ES4Q4B.js.map +1 -0
- package/dist/{chunk-5XG4YMAB.js → chunk-X374EYQC.js} +62 -36
- package/dist/chunk-X374EYQC.js.map +1 -0
- package/dist/chunk-XDL4R54M.js +44 -0
- package/dist/chunk-XDL4R54M.js.map +1 -0
- package/dist/csv-parse-fBA2l99I.d.ts +41 -0
- package/dist/csv.d.ts +3 -2
- package/dist/csv.js +4 -4
- package/dist/doc.js +1 -1
- package/dist/dom-CmWO26el.d.ts +15 -0
- package/dist/import-XL3AH3QH.js +7 -0
- package/dist/{import-O7RUERND.js.map → import-XL3AH3QH.js.map} +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/pdf.d.ts +1 -1
- package/dist/pptx.d.ts +27 -3
- package/dist/pptx.js +48 -1
- package/dist/pptx.js.map +1 -1
- package/dist/{registry-ByvNr_Fa.d.ts → registry-CwN0A_uZ.d.ts} +1 -1
- package/dist/sheet-runtime.d.ts +432 -1
- package/dist/sheet-runtime.js +815 -13
- package/dist/sheet-runtime.js.map +1 -1
- package/dist/sheet.d.ts +2 -2
- package/dist/sheet.js +1 -1
- package/dist/slide-runtime.d.ts +432 -0
- package/dist/slide-runtime.js +4 -0
- package/dist/slide-runtime.js.map +1 -0
- package/dist/style.css +3 -3
- package/dist/{csv-parse-DXlwzoZ6.d.ts → types-CQyBR7Xq.d.ts} +1 -39
- package/dist/xlsx.d.ts +11 -20
- package/dist/xlsx.js +42 -213
- package/dist/xlsx.js.map +1 -1
- package/package.json +6 -2
- package/dist/PptxView-ZH4FZKNO.js +0 -3
- package/dist/SheetView-YFK7BUS5.js +0 -3
- package/dist/chunk-2DI4FJPC.js.map +0 -1
- package/dist/chunk-3QTRZBIK.js.map +0 -1
- package/dist/chunk-4MQS76M2.js.map +0 -1
- package/dist/chunk-5HMKRNIH.js +0 -27
- package/dist/chunk-5HMKRNIH.js.map +0 -1
- package/dist/chunk-5XG4YMAB.js.map +0 -1
- package/dist/chunk-SDVGDHC4.js +0 -149
- package/dist/chunk-SDVGDHC4.js.map +0 -1
- package/dist/import-O7RUERND.js +0 -6
|
@@ -0,0 +1,2089 @@
|
|
|
1
|
+
import { childrenOf, childOf, boolAttr, intAttr, textOf } from './chunk-XDL4R54M.js';
|
|
2
|
+
import { unzipSync, strFromU8, strToU8, zipSync } from 'fflate';
|
|
3
|
+
|
|
4
|
+
// src/slide-runtime/xml.ts
|
|
5
|
+
var NS = {
|
|
6
|
+
p: "http://schemas.openxmlformats.org/presentationml/2006/main",
|
|
7
|
+
a: "http://schemas.openxmlformats.org/drawingml/2006/main",
|
|
8
|
+
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
|
9
|
+
p14: "http://schemas.microsoft.com/office/powerpoint/2010/main",
|
|
10
|
+
rel: "http://schemas.openxmlformats.org/package/2006/relationships",
|
|
11
|
+
ct: "http://schemas.openxmlformats.org/package/2006/content-types"
|
|
12
|
+
};
|
|
13
|
+
function defaultXmlEnv() {
|
|
14
|
+
const g = globalThis;
|
|
15
|
+
if (!g.DOMParser || !g.XMLSerializer) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
"当前环境没有 DOMParser / XMLSerializer:Node 里请注入 @xmldom/xmldom,如 new SlideRuntime({ ..., xml: { DOMParser, XMLSerializer } })"
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
DOMParser: g.DOMParser,
|
|
22
|
+
XMLSerializer: g.XMLSerializer
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
var DECL = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n';
|
|
26
|
+
function parseXml(env, text, path) {
|
|
27
|
+
let doc;
|
|
28
|
+
try {
|
|
29
|
+
doc = new env.DOMParser().parseFromString(text, "application/xml");
|
|
30
|
+
} catch (e) {
|
|
31
|
+
throw new Error(`${path} 不是合法的 XML:${e.message}`);
|
|
32
|
+
}
|
|
33
|
+
const root = doc?.documentElement;
|
|
34
|
+
if (!root || root.localName === "parsererror" || root.getElementsByTagName("parsererror").length) {
|
|
35
|
+
throw new Error(`${path} 不是合法的 XML`);
|
|
36
|
+
}
|
|
37
|
+
return doc;
|
|
38
|
+
}
|
|
39
|
+
function serializeXml(env, doc) {
|
|
40
|
+
const text = new env.XMLSerializer().serializeToString(doc);
|
|
41
|
+
return DECL + text.replace(/^\s*<\?xml[^?]*\?>\s*/, "");
|
|
42
|
+
}
|
|
43
|
+
function el(doc, qname, attrs2 = {}, children = []) {
|
|
44
|
+
const colon = qname.indexOf(":");
|
|
45
|
+
const ns = colon > 0 ? NS[qname.slice(0, colon)] : doc.documentElement.namespaceURI;
|
|
46
|
+
if (ns === void 0) throw new Error(`未知命名空间前缀:${qname}`);
|
|
47
|
+
const node = doc.createElementNS(ns, qname);
|
|
48
|
+
for (const [key, value] of Object.entries(attrs2)) {
|
|
49
|
+
if (value === void 0) continue;
|
|
50
|
+
if (key.startsWith("r:")) node.setAttributeNS(NS.r, key, String(value));
|
|
51
|
+
else node.setAttribute(key, String(value));
|
|
52
|
+
}
|
|
53
|
+
for (const child of children) node.appendChild(child);
|
|
54
|
+
return node;
|
|
55
|
+
}
|
|
56
|
+
function elements(parent) {
|
|
57
|
+
const out = [];
|
|
58
|
+
for (let n = parent.firstChild; n; n = n.nextSibling) if (n.nodeType === 1) out.push(n);
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
function descendants(root, tag) {
|
|
62
|
+
const out = [];
|
|
63
|
+
const walk = (node) => {
|
|
64
|
+
for (const child of elements(node)) {
|
|
65
|
+
if (child.localName === tag) out.push(child);
|
|
66
|
+
walk(child);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
walk(root);
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
function removeNode(node) {
|
|
73
|
+
node.parentNode?.removeChild(node);
|
|
74
|
+
}
|
|
75
|
+
function insertBeforeFirst(parent, child, tags) {
|
|
76
|
+
const ref = elements(parent).find((e) => tags.includes(e.localName));
|
|
77
|
+
if (ref) parent.insertBefore(child, ref);
|
|
78
|
+
else parent.appendChild(child);
|
|
79
|
+
}
|
|
80
|
+
function setTextContent(node, text) {
|
|
81
|
+
while (node.firstChild) node.removeChild(node.firstChild);
|
|
82
|
+
node.appendChild(node.ownerDocument.createTextNode(text));
|
|
83
|
+
}
|
|
84
|
+
function escapeXml(s) {
|
|
85
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
86
|
+
}
|
|
87
|
+
function rId(node) {
|
|
88
|
+
return node.getAttributeNS(NS.r, "id") || node.getAttribute("r:id") || "";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/slide-runtime/package.ts
|
|
92
|
+
var RT = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/";
|
|
93
|
+
var REL = {
|
|
94
|
+
slide: `${RT}slide`,
|
|
95
|
+
slideLayout: `${RT}slideLayout`,
|
|
96
|
+
slideMaster: `${RT}slideMaster`,
|
|
97
|
+
notesSlide: `${RT}notesSlide`,
|
|
98
|
+
notesMaster: `${RT}notesMaster`,
|
|
99
|
+
image: `${RT}image`,
|
|
100
|
+
hyperlink: `${RT}hyperlink`,
|
|
101
|
+
video: `${RT}video`,
|
|
102
|
+
audio: `${RT}audio`,
|
|
103
|
+
comments: `${RT}comments`,
|
|
104
|
+
media: "http://schemas.microsoft.com/office/2007/relationships/media",
|
|
105
|
+
modernComments: "http://schemas.microsoft.com/office/2018/10/relationships/comments"
|
|
106
|
+
};
|
|
107
|
+
var CTP = "application/vnd.openxmlformats-officedocument.presentationml.";
|
|
108
|
+
var CT = {
|
|
109
|
+
slide: `${CTP}slide+xml`,
|
|
110
|
+
notesSlide: `${CTP}notesSlide+xml`
|
|
111
|
+
};
|
|
112
|
+
var CONTENT_TYPES = "[Content_Types].xml";
|
|
113
|
+
var PRESENTATION = "ppt/presentation.xml";
|
|
114
|
+
function relsPathOf(part) {
|
|
115
|
+
const i = part.lastIndexOf("/");
|
|
116
|
+
return `${part.slice(0, i + 1)}_rels/${part.slice(i + 1)}.rels`;
|
|
117
|
+
}
|
|
118
|
+
function ownerOfRels(relsPath) {
|
|
119
|
+
const m = /^(.*?)_rels\/([^/]*)\.rels$/.exec(relsPath);
|
|
120
|
+
return m ? m[1] + m[2] : null;
|
|
121
|
+
}
|
|
122
|
+
function resolveTarget(fromPart, target) {
|
|
123
|
+
if (target.startsWith("/")) return target.slice(1);
|
|
124
|
+
const stack = fromPart.split("/").slice(0, -1);
|
|
125
|
+
for (const seg of target.split("/")) {
|
|
126
|
+
if (seg === "..") stack.pop();
|
|
127
|
+
else if (seg && seg !== ".") stack.push(seg);
|
|
128
|
+
}
|
|
129
|
+
return stack.join("/");
|
|
130
|
+
}
|
|
131
|
+
function relativeTarget(fromPart, toPart) {
|
|
132
|
+
const from = fromPart.split("/").slice(0, -1);
|
|
133
|
+
const to = toPart.split("/");
|
|
134
|
+
let i = 0;
|
|
135
|
+
while (i < from.length && i < to.length - 1 && from[i] === to[i]) i++;
|
|
136
|
+
return [...Array(from.length - i).fill(".."), ...to.slice(i)].join("/");
|
|
137
|
+
}
|
|
138
|
+
var PptxPackage = class _PptxPackage {
|
|
139
|
+
env;
|
|
140
|
+
/** 原始顺序的部件表;新增部件追加在末尾 */
|
|
141
|
+
files;
|
|
142
|
+
docs = /* @__PURE__ */ new Map();
|
|
143
|
+
dirty = /* @__PURE__ */ new Set();
|
|
144
|
+
changed = false;
|
|
145
|
+
constructor(files, env) {
|
|
146
|
+
this.files = files;
|
|
147
|
+
this.env = env;
|
|
148
|
+
}
|
|
149
|
+
static open(bytes, env = defaultXmlEnv()) {
|
|
150
|
+
let raw;
|
|
151
|
+
try {
|
|
152
|
+
raw = unzipSync(bytes);
|
|
153
|
+
} catch {
|
|
154
|
+
throw new Error("不是有效的 pptx:无法解压");
|
|
155
|
+
}
|
|
156
|
+
if (!raw[PRESENTATION]) throw new Error(`不是有效的 pptx:缺少 ${PRESENTATION}`);
|
|
157
|
+
return new _PptxPackage(new Map(Object.entries(raw)), env);
|
|
158
|
+
}
|
|
159
|
+
/** 有没有任何部件被改、增、删 */
|
|
160
|
+
get isDirty() {
|
|
161
|
+
return this.changed;
|
|
162
|
+
}
|
|
163
|
+
has(path) {
|
|
164
|
+
return this.files.has(path);
|
|
165
|
+
}
|
|
166
|
+
paths() {
|
|
167
|
+
return [...this.files.keys()];
|
|
168
|
+
}
|
|
169
|
+
/** 解析(并缓存)一个 XML 部件。只读不算脏 —— 改了要 touch */
|
|
170
|
+
xml(path) {
|
|
171
|
+
const cached = this.docs.get(path);
|
|
172
|
+
if (cached) return cached;
|
|
173
|
+
const data = this.files.get(path);
|
|
174
|
+
if (!data) throw new Error(`pptx 里缺少部件 ${path}`);
|
|
175
|
+
const doc = parseXml(this.env, strFromU8(data), path);
|
|
176
|
+
this.docs.set(path, doc);
|
|
177
|
+
return doc;
|
|
178
|
+
}
|
|
179
|
+
touch(path) {
|
|
180
|
+
if (!this.docs.has(path)) throw new Error(`内部错误:${path} 没有解析过就被标记为已修改`);
|
|
181
|
+
this.dirty.add(path);
|
|
182
|
+
this.changed = true;
|
|
183
|
+
}
|
|
184
|
+
createXml(path, text) {
|
|
185
|
+
if (this.files.has(path)) throw new Error(`内部错误:部件 ${path} 已存在`);
|
|
186
|
+
const doc = parseXml(this.env, text, path);
|
|
187
|
+
this.files.set(path, new Uint8Array(0));
|
|
188
|
+
this.docs.set(path, doc);
|
|
189
|
+
this.dirty.add(path);
|
|
190
|
+
this.changed = true;
|
|
191
|
+
return doc;
|
|
192
|
+
}
|
|
193
|
+
binary(path) {
|
|
194
|
+
return this.files.get(path);
|
|
195
|
+
}
|
|
196
|
+
setBinary(path, data) {
|
|
197
|
+
this.files.set(path, data);
|
|
198
|
+
this.docs.delete(path);
|
|
199
|
+
this.dirty.delete(path);
|
|
200
|
+
this.changed = true;
|
|
201
|
+
}
|
|
202
|
+
remove(path) {
|
|
203
|
+
if (!this.files.has(path)) return;
|
|
204
|
+
this.files.delete(path);
|
|
205
|
+
this.docs.delete(path);
|
|
206
|
+
this.dirty.delete(path);
|
|
207
|
+
this.changed = true;
|
|
208
|
+
}
|
|
209
|
+
/** 删部件 + 它的 rels + 它的 Override */
|
|
210
|
+
removePart(path) {
|
|
211
|
+
this.remove(path);
|
|
212
|
+
this.remove(relsPathOf(path));
|
|
213
|
+
this.removeOverride(path);
|
|
214
|
+
}
|
|
215
|
+
/** `dir/base{N}.ext`,N 取现有最大编号 + 1(删掉编号最大的部件后,这个编号会被再次使用) */
|
|
216
|
+
nextPartPath(dir, base, ext) {
|
|
217
|
+
const re = new RegExp(`^${dir}/${base}(\\d+)\\.`);
|
|
218
|
+
let n = 0;
|
|
219
|
+
for (const p of this.files.keys()) {
|
|
220
|
+
const m = re.exec(p);
|
|
221
|
+
if (m) n = Math.max(n, Number(m[1]));
|
|
222
|
+
}
|
|
223
|
+
let path;
|
|
224
|
+
do {
|
|
225
|
+
n += 1;
|
|
226
|
+
path = `${dir}/${base}${n}.${ext}`;
|
|
227
|
+
} while (this.files.has(path));
|
|
228
|
+
return path;
|
|
229
|
+
}
|
|
230
|
+
/* ------------------------------ rels ------------------------------ */
|
|
231
|
+
rels(part) {
|
|
232
|
+
const rp = relsPathOf(part);
|
|
233
|
+
if (!this.files.has(rp)) return [];
|
|
234
|
+
return childrenOf(this.xml(rp).documentElement, "Relationship").map((r) => {
|
|
235
|
+
const external = r.getAttribute("TargetMode") === "External";
|
|
236
|
+
const raw = r.getAttribute("Target") ?? "";
|
|
237
|
+
return {
|
|
238
|
+
id: r.getAttribute("Id") ?? "",
|
|
239
|
+
type: r.getAttribute("Type") ?? "",
|
|
240
|
+
target: external ? raw : resolveTarget(part, raw),
|
|
241
|
+
external
|
|
242
|
+
};
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
relsOfType(part, type) {
|
|
246
|
+
return this.rels(part).filter((r) => r.type === type);
|
|
247
|
+
}
|
|
248
|
+
/** 某类关系的第一个目标(版式、备注页这类一对一关系用) */
|
|
249
|
+
relTarget(part, type) {
|
|
250
|
+
return this.relsOfType(part, type)[0]?.target;
|
|
251
|
+
}
|
|
252
|
+
relsDoc(part) {
|
|
253
|
+
const rp = relsPathOf(part);
|
|
254
|
+
return this.files.has(rp) ? this.xml(rp) : this.createXml(rp, '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>');
|
|
255
|
+
}
|
|
256
|
+
static freeRelId(root) {
|
|
257
|
+
const used = new Set(childrenOf(root, "Relationship").map((r) => r.getAttribute("Id")));
|
|
258
|
+
let n = 1;
|
|
259
|
+
while (used.has(`rId${n}`)) n++;
|
|
260
|
+
return `rId${n}`;
|
|
261
|
+
}
|
|
262
|
+
/** 加一条关系,返回 rId。`id` 省略时取最小空闲的 rIdN */
|
|
263
|
+
addRel(part, type, targetPart, id) {
|
|
264
|
+
const doc = this.relsDoc(part);
|
|
265
|
+
const root = doc.documentElement;
|
|
266
|
+
if (id && childrenOf(root, "Relationship").some((r) => r.getAttribute("Id") === id)) {
|
|
267
|
+
throw new Error(`内部错误:${relsPathOf(part)} 里 ${id} 已存在`);
|
|
268
|
+
}
|
|
269
|
+
const rid = id ?? _PptxPackage.freeRelId(root);
|
|
270
|
+
root.appendChild(el(doc, "Relationship", { Id: rid, Type: type, Target: relativeTarget(part, targetPart) }));
|
|
271
|
+
this.touch(relsPathOf(part));
|
|
272
|
+
return rid;
|
|
273
|
+
}
|
|
274
|
+
/** 同类型、同目标的内部关系(复用用;外链不在此列) */
|
|
275
|
+
findRel(part, type, targetPart) {
|
|
276
|
+
return this.rels(part).find((r) => !r.external && r.type === type && r.target === targetPart)?.id;
|
|
277
|
+
}
|
|
278
|
+
/** 外部关系(如网址超链接):同类型同 URL 已存在则复用 */
|
|
279
|
+
addExternalRel(part, type, url) {
|
|
280
|
+
const hit = this.rels(part).find((r) => r.external && r.type === type && r.target === url);
|
|
281
|
+
if (hit) return hit.id;
|
|
282
|
+
const doc = this.relsDoc(part);
|
|
283
|
+
const root = doc.documentElement;
|
|
284
|
+
const rid = _PptxPackage.freeRelId(root);
|
|
285
|
+
root.appendChild(el(doc, "Relationship", { Id: rid, Type: type, Target: url, TargetMode: "External" }));
|
|
286
|
+
this.touch(relsPathOf(part));
|
|
287
|
+
return rid;
|
|
288
|
+
}
|
|
289
|
+
removeRel(part, id) {
|
|
290
|
+
const rp = relsPathOf(part);
|
|
291
|
+
if (!this.files.has(rp)) return;
|
|
292
|
+
const doc = this.xml(rp);
|
|
293
|
+
for (const r of childrenOf(doc.documentElement, "Relationship")) {
|
|
294
|
+
if (r.getAttribute("Id") === id) {
|
|
295
|
+
removeNode(r);
|
|
296
|
+
this.touch(rp);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* 部件 XML 里已没有任何 r:* 属性引用该 rId 时才删除关系;返回是否删除。
|
|
302
|
+
* 本来就没有这条关系时什么都不做、返回 false(不能把「没删」报成「删了」)。
|
|
303
|
+
*/
|
|
304
|
+
removeRelIfUnused(part, id) {
|
|
305
|
+
if (!this.rels(part).some((r) => r.id === id)) return false;
|
|
306
|
+
const stack = [this.xml(part).documentElement];
|
|
307
|
+
while (stack.length) {
|
|
308
|
+
const node = stack.pop();
|
|
309
|
+
for (let i = 0; i < node.attributes.length; i++) {
|
|
310
|
+
const a = node.attributes.item(i);
|
|
311
|
+
if (a && (a.namespaceURI === NS.r || a.name.startsWith("r:")) && a.value === id) return false;
|
|
312
|
+
}
|
|
313
|
+
stack.push(...elements(node));
|
|
314
|
+
}
|
|
315
|
+
this.removeRel(part, id);
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
/** 整份重写某部件的 rels(复制页用:保留原 rId,免得改页内的 r:embed) */
|
|
319
|
+
writeRels(part, rels) {
|
|
320
|
+
const rp = relsPathOf(part);
|
|
321
|
+
this.remove(rp);
|
|
322
|
+
const doc = this.createXml(rp, '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>');
|
|
323
|
+
for (const r of rels) {
|
|
324
|
+
doc.documentElement.appendChild(
|
|
325
|
+
el(doc, "Relationship", {
|
|
326
|
+
Id: r.id,
|
|
327
|
+
Type: r.type,
|
|
328
|
+
Target: r.external ? r.target : relativeTarget(part, r.target),
|
|
329
|
+
TargetMode: r.external ? "External" : void 0
|
|
330
|
+
})
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/** 哪些部件有关系指向 `target` */
|
|
335
|
+
referrers(target) {
|
|
336
|
+
const out = [];
|
|
337
|
+
for (const p of this.paths()) {
|
|
338
|
+
const owner = ownerOfRels(p);
|
|
339
|
+
if (owner === null) continue;
|
|
340
|
+
if (this.rels(owner).some((r) => !r.external && r.target === target)) out.push(owner);
|
|
341
|
+
}
|
|
342
|
+
return out;
|
|
343
|
+
}
|
|
344
|
+
/* --------------------------- Content_Types --------------------------- */
|
|
345
|
+
setOverride(part, contentType) {
|
|
346
|
+
const doc = this.xml(CONTENT_TYPES);
|
|
347
|
+
const name = `/${part}`;
|
|
348
|
+
const hit = childrenOf(doc.documentElement, "Override").find((o) => o.getAttribute("PartName") === name);
|
|
349
|
+
if (hit) hit.setAttribute("ContentType", contentType);
|
|
350
|
+
else doc.documentElement.appendChild(el(doc, "Override", { PartName: name, ContentType: contentType }));
|
|
351
|
+
this.touch(CONTENT_TYPES);
|
|
352
|
+
}
|
|
353
|
+
removeOverride(part) {
|
|
354
|
+
const doc = this.xml(CONTENT_TYPES);
|
|
355
|
+
const name = `/${part}`;
|
|
356
|
+
for (const o of childrenOf(doc.documentElement, "Override")) {
|
|
357
|
+
if (o.getAttribute("PartName") === name) {
|
|
358
|
+
removeNode(o);
|
|
359
|
+
this.touch(CONTENT_TYPES);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
ensureDefault(ext, contentType) {
|
|
364
|
+
const doc = this.xml(CONTENT_TYPES);
|
|
365
|
+
const root = doc.documentElement;
|
|
366
|
+
const lower = ext.toLowerCase();
|
|
367
|
+
if (childrenOf(root, "Default").some((d) => (d.getAttribute("Extension") ?? "").toLowerCase() === lower)) return;
|
|
368
|
+
const node = el(doc, "Default", { Extension: lower, ContentType: contentType });
|
|
369
|
+
const firstOverride = elements(root).find((e) => e.localName === "Override");
|
|
370
|
+
if (firstOverride) root.insertBefore(node, firstOverride);
|
|
371
|
+
else root.appendChild(node);
|
|
372
|
+
this.touch(CONTENT_TYPES);
|
|
373
|
+
}
|
|
374
|
+
/* ------------------------------ 打包 ------------------------------ */
|
|
375
|
+
save() {
|
|
376
|
+
const out = {};
|
|
377
|
+
for (const [path, data] of this.files) {
|
|
378
|
+
const doc = this.dirty.has(path) ? this.docs.get(path) : void 0;
|
|
379
|
+
out[path] = doc ? strToU8(serializeXml(this.env, doc)) : data;
|
|
380
|
+
}
|
|
381
|
+
return zipSync(out, { level: 6 });
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// src/slide-runtime/base64.ts
|
|
386
|
+
function base64ToBytes(b64) {
|
|
387
|
+
const bin = atob(b64.replace(/^data:[^,]*,/, ""));
|
|
388
|
+
const out = new Uint8Array(bin.length);
|
|
389
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
390
|
+
return out;
|
|
391
|
+
}
|
|
392
|
+
function bytesToBase64(bytes) {
|
|
393
|
+
let bin = "";
|
|
394
|
+
for (let i = 0; i < bytes.length; i += 32768) bin += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
395
|
+
return btoa(bin);
|
|
396
|
+
}
|
|
397
|
+
function sanitizePptxFilename(name) {
|
|
398
|
+
if (!name) return null;
|
|
399
|
+
const base = name.split(/[/\\]/).pop()?.trim();
|
|
400
|
+
if (!base || base === "." || base === "..") return null;
|
|
401
|
+
return /\.pptx$/i.test(base) ? base : `${base}.pptx`;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// src/slide-runtime/color.ts
|
|
405
|
+
function parseColor(s) {
|
|
406
|
+
if (typeof s !== "string") throw new Error(`颜色要写成 #RRGGBB,收到 ${s}`);
|
|
407
|
+
const m = /^#?([0-9a-fA-F]{6})$/.exec(s.trim());
|
|
408
|
+
if (!m) throw new Error(`颜色要写成 #RRGGBB,收到 ${s}`);
|
|
409
|
+
return m[1].toUpperCase();
|
|
410
|
+
}
|
|
411
|
+
var THEME_COLORS = [
|
|
412
|
+
"accent1",
|
|
413
|
+
"accent2",
|
|
414
|
+
"accent3",
|
|
415
|
+
"accent4",
|
|
416
|
+
"accent5",
|
|
417
|
+
"accent6",
|
|
418
|
+
"tx1",
|
|
419
|
+
"tx2",
|
|
420
|
+
"bg1",
|
|
421
|
+
"bg2",
|
|
422
|
+
"hlink"
|
|
423
|
+
];
|
|
424
|
+
function parseTextColor(s) {
|
|
425
|
+
if (typeof s === "string" && THEME_COLORS.includes(s)) return { scheme: s };
|
|
426
|
+
if (typeof s === "string" && /^#?[0-9a-fA-F]{6}$/.test(s.trim())) return { srgb: parseColor(s) };
|
|
427
|
+
throw new Error(`文字颜色要写成 #RRGGBB,或主题色 ${THEME_COLORS.join(" / ")},收到 ${s}`);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// src/slide-runtime/model.ts
|
|
431
|
+
var EMU_PER_PT = 12700;
|
|
432
|
+
var ptToEmu = (pt) => Math.round(pt * EMU_PER_PT);
|
|
433
|
+
var emuToPt = (emu) => Math.round(emu / EMU_PER_PT * 100) / 100;
|
|
434
|
+
function slideRefs(pkg) {
|
|
435
|
+
const root = pkg.xml(PRESENTATION).documentElement;
|
|
436
|
+
const rels = new Map(pkg.rels(PRESENTATION).map((r) => [r.id, r]));
|
|
437
|
+
return childrenOf(childOf(root, "sldIdLst"), "sldId").map((sldId) => {
|
|
438
|
+
const rid = rId(sldId);
|
|
439
|
+
const rel = rels.get(rid);
|
|
440
|
+
if (!rel) throw new Error(`presentation.xml 的 sldId ${sldId.getAttribute("id")} 指向不存在的关系 ${rid}`);
|
|
441
|
+
return { sldId, path: rel.target };
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
function slideCount(pkg) {
|
|
445
|
+
return slideRefs(pkg).length;
|
|
446
|
+
}
|
|
447
|
+
function slidePath(pkg, n) {
|
|
448
|
+
const refs = slideRefs(pkg);
|
|
449
|
+
if (!Number.isInteger(n) || n < 1 || n > refs.length) {
|
|
450
|
+
throw new Error(`页码 ${n} 越界:共 ${refs.length} 页(页码从 1 开始)`);
|
|
451
|
+
}
|
|
452
|
+
return refs[n - 1].path;
|
|
453
|
+
}
|
|
454
|
+
function slideSize(pkg) {
|
|
455
|
+
const sz = childOf(pkg.xml(PRESENTATION).documentElement, "sldSz");
|
|
456
|
+
return { cx: intAttr(sz, "cx") ?? 9144e3, cy: intAttr(sz, "cy") ?? 6858e3 };
|
|
457
|
+
}
|
|
458
|
+
function layoutOf(pkg, slide) {
|
|
459
|
+
return pkg.relTarget(slide, REL.slideLayout);
|
|
460
|
+
}
|
|
461
|
+
function layoutName(pkg, layout) {
|
|
462
|
+
return childOf(pkg.xml(layout).documentElement, "cSld")?.getAttribute("name") ?? "";
|
|
463
|
+
}
|
|
464
|
+
function notesPathOf(pkg, slide) {
|
|
465
|
+
return pkg.relTarget(slide, REL.notesSlide);
|
|
466
|
+
}
|
|
467
|
+
var SHAPE_TAGS = /* @__PURE__ */ new Set(["sp", "pic", "graphicFrame", "grpSp", "cxnSp", "contentPart"]);
|
|
468
|
+
function spTree(doc) {
|
|
469
|
+
const tree = childOf(childOf(doc.documentElement, "cSld"), "spTree");
|
|
470
|
+
if (!tree) throw new Error("页面缺少 p:cSld/p:spTree");
|
|
471
|
+
dedupeShapeIds(doc, tree);
|
|
472
|
+
return tree;
|
|
473
|
+
}
|
|
474
|
+
var deduped = /* @__PURE__ */ new WeakSet();
|
|
475
|
+
function dedupeShapeIds(doc, tree) {
|
|
476
|
+
if (deduped.has(doc)) return;
|
|
477
|
+
deduped.add(doc);
|
|
478
|
+
const all = descendants(tree, "cNvPr");
|
|
479
|
+
let max = 0;
|
|
480
|
+
for (const c of all) max = Math.max(max, intAttr(c, "id") ?? 0);
|
|
481
|
+
const seen = /* @__PURE__ */ new Set();
|
|
482
|
+
for (const c of all) {
|
|
483
|
+
const id = intAttr(c, "id");
|
|
484
|
+
if (id === void 0) continue;
|
|
485
|
+
if (seen.has(id)) {
|
|
486
|
+
max += 1;
|
|
487
|
+
c.setAttribute("id", String(max));
|
|
488
|
+
seen.add(max);
|
|
489
|
+
} else {
|
|
490
|
+
seen.add(id);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
function allShapes(tree) {
|
|
495
|
+
const out = [];
|
|
496
|
+
const walk = (parent, inGroup) => {
|
|
497
|
+
for (const child of elements(parent)) {
|
|
498
|
+
if (!SHAPE_TAGS.has(child.localName)) continue;
|
|
499
|
+
out.push({ el: child, inGroup });
|
|
500
|
+
if (child.localName === "grpSp") walk(child, true);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
walk(tree, false);
|
|
504
|
+
return out;
|
|
505
|
+
}
|
|
506
|
+
function nvPrOf(shape) {
|
|
507
|
+
return elements(shape).find((e) => e.localName.startsWith("nv") && e.localName.endsWith("Pr"));
|
|
508
|
+
}
|
|
509
|
+
function shapeIdOf(shape) {
|
|
510
|
+
return intAttr(childOf(nvPrOf(shape), "cNvPr"), "id");
|
|
511
|
+
}
|
|
512
|
+
function shapeNameOf(shape) {
|
|
513
|
+
return childOf(nvPrOf(shape), "cNvPr")?.getAttribute("name") ?? "";
|
|
514
|
+
}
|
|
515
|
+
function findShape(doc, id, slide) {
|
|
516
|
+
const hits = allShapes(spTree(doc)).filter((h) => shapeIdOf(h.el) === id);
|
|
517
|
+
if (!hits.length) throw new Error(`第 ${slide} 页没有 shape_id=${id} 的形状(先用 slide_read 看清 id)`);
|
|
518
|
+
if (hits.length > 1) throw new Error(`第 ${slide} 页有 ${hits.length} 个形状共用 shape_id=${id},无法确定改哪个`);
|
|
519
|
+
if (hits[0].inGroup) throw new Error(`shape_id=${id} 在分组里,本期不支持修改分组内的形状;只改文字的话可以用 slide_replace_text 的 {find, replace} 查找替换`);
|
|
520
|
+
return hits[0].el;
|
|
521
|
+
}
|
|
522
|
+
function nextShapeId(doc) {
|
|
523
|
+
let max = 0;
|
|
524
|
+
for (const c of descendants(spTree(doc), "cNvPr")) max = Math.max(max, intAttr(c, "id") ?? 0);
|
|
525
|
+
return max + 1;
|
|
526
|
+
}
|
|
527
|
+
function xfrmOf(shape) {
|
|
528
|
+
if (shape.localName === "graphicFrame") return childOf(shape, "xfrm");
|
|
529
|
+
if (shape.localName === "grpSp") return childOf(childOf(shape, "grpSpPr"), "xfrm");
|
|
530
|
+
return childOf(childOf(shape, "spPr"), "xfrm");
|
|
531
|
+
}
|
|
532
|
+
function boxOf(xfrm) {
|
|
533
|
+
const off = childOf(xfrm, "off");
|
|
534
|
+
const ext = childOf(xfrm, "ext");
|
|
535
|
+
if (!off || !ext) return void 0;
|
|
536
|
+
const box = {
|
|
537
|
+
x: intAttr(off, "x") ?? 0,
|
|
538
|
+
y: intAttr(off, "y") ?? 0,
|
|
539
|
+
w: intAttr(ext, "cx") ?? 0,
|
|
540
|
+
h: intAttr(ext, "cy") ?? 0
|
|
541
|
+
};
|
|
542
|
+
const rot = intAttr(xfrm, "rot");
|
|
543
|
+
if (rot) box.rot = rot;
|
|
544
|
+
return box;
|
|
545
|
+
}
|
|
546
|
+
function placeholderOf(shape) {
|
|
547
|
+
const ph = childOf(childOf(nvPrOf(shape), "nvPr"), "ph");
|
|
548
|
+
if (!ph) return void 0;
|
|
549
|
+
return { type: ph.getAttribute("type") || "obj", idx: intAttr(ph, "idx") };
|
|
550
|
+
}
|
|
551
|
+
function placeholderShapes(tree) {
|
|
552
|
+
return allShapes(tree).map((h) => h.el).filter((e) => placeholderOf(e));
|
|
553
|
+
}
|
|
554
|
+
function matchPlaceholder(tree, ph) {
|
|
555
|
+
const candidates = placeholderShapes(tree);
|
|
556
|
+
if (ph.idx !== void 0) {
|
|
557
|
+
const byIdx = candidates.find((e) => placeholderOf(e)?.idx === ph.idx);
|
|
558
|
+
if (byIdx) return byIdx;
|
|
559
|
+
}
|
|
560
|
+
const norm = (t) => t === "ctrTitle" ? "title" : t;
|
|
561
|
+
return candidates.find((e) => norm(placeholderOf(e).type) === norm(ph.type));
|
|
562
|
+
}
|
|
563
|
+
function masterTypeOf(type) {
|
|
564
|
+
if (type === "title" || type === "ctrTitle") return "title";
|
|
565
|
+
if (["dt", "ftr", "sldNum", "sldImg", "hdr"].includes(type)) return type;
|
|
566
|
+
return "body";
|
|
567
|
+
}
|
|
568
|
+
function matchMasterPlaceholder(tree, ph) {
|
|
569
|
+
const want = masterTypeOf(ph.type);
|
|
570
|
+
return placeholderShapes(tree).find((e) => masterTypeOf(placeholderOf(e).type) === want);
|
|
571
|
+
}
|
|
572
|
+
function inheritedBox(pkg, slide, shape) {
|
|
573
|
+
const ph = placeholderOf(shape);
|
|
574
|
+
if (!ph) return void 0;
|
|
575
|
+
const layout = layoutOf(pkg, slide);
|
|
576
|
+
if (!layout) return void 0;
|
|
577
|
+
const layoutShape = matchPlaceholder(spTree(pkg.xml(layout)), ph);
|
|
578
|
+
const layoutXfrm = layoutShape ? xfrmOf(layoutShape) : void 0;
|
|
579
|
+
const fromLayout = layoutXfrm ? boxOf(layoutXfrm) : void 0;
|
|
580
|
+
if (fromLayout) return fromLayout;
|
|
581
|
+
const master = pkg.relTarget(layout, REL.slideMaster);
|
|
582
|
+
if (!master) return void 0;
|
|
583
|
+
const masterShape = matchMasterPlaceholder(spTree(pkg.xml(master)), layoutShape ? placeholderOf(layoutShape) : ph);
|
|
584
|
+
const masterXfrm = masterShape ? xfrmOf(masterShape) : void 0;
|
|
585
|
+
return masterXfrm ? boxOf(masterXfrm) : void 0;
|
|
586
|
+
}
|
|
587
|
+
function notesBodyOf(doc) {
|
|
588
|
+
return allShapes(spTree(doc)).map((h) => h.el).find((e) => placeholderOf(e)?.type === "body");
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/slide-runtime/text.ts
|
|
592
|
+
function normalizeNewlines(s) {
|
|
593
|
+
return s.replace(/\r\n?/g, "\n");
|
|
594
|
+
}
|
|
595
|
+
function paragraphText(p) {
|
|
596
|
+
let out = "";
|
|
597
|
+
for (const child of elements(p)) {
|
|
598
|
+
if (child.localName === "r" || child.localName === "fld") out += textOf(childOf(child, "t"));
|
|
599
|
+
else if (child.localName === "br") out += "\n";
|
|
600
|
+
}
|
|
601
|
+
return out;
|
|
602
|
+
}
|
|
603
|
+
function bodyParagraphs(txBody) {
|
|
604
|
+
return childrenOf(txBody, "p").map(paragraphText);
|
|
605
|
+
}
|
|
606
|
+
function txBodyOf(shape) {
|
|
607
|
+
return childOf(shape, "txBody");
|
|
608
|
+
}
|
|
609
|
+
function ensureTxBody(shape) {
|
|
610
|
+
const existing = txBodyOf(shape);
|
|
611
|
+
if (existing) return existing;
|
|
612
|
+
const doc = shape.ownerDocument;
|
|
613
|
+
const body = el(doc, "p:txBody", {}, [el(doc, "a:bodyPr"), el(doc, "a:lstStyle"), el(doc, "a:p")]);
|
|
614
|
+
insertBeforeFirst(shape, body, ["extLst"]);
|
|
615
|
+
return body;
|
|
616
|
+
}
|
|
617
|
+
function cloneAs(src, qname) {
|
|
618
|
+
const node = el(src.ownerDocument, qname);
|
|
619
|
+
for (let i = 0; i < src.attributes.length; i++) {
|
|
620
|
+
const attr = src.attributes.item(i);
|
|
621
|
+
if (attr) node.setAttribute(attr.name, attr.value);
|
|
622
|
+
}
|
|
623
|
+
for (const child of elements(src)) node.appendChild(child.cloneNode(true));
|
|
624
|
+
return node;
|
|
625
|
+
}
|
|
626
|
+
function stripXmlIllegal(s) {
|
|
627
|
+
return s.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, "");
|
|
628
|
+
}
|
|
629
|
+
function setBodyText(txBody, text) {
|
|
630
|
+
const doc = txBody.ownerDocument;
|
|
631
|
+
const paras = childrenOf(txBody, "p");
|
|
632
|
+
const first = paras[0];
|
|
633
|
+
const pPr = first ? childOf(first, "pPr") : void 0;
|
|
634
|
+
const endRPr = first ? childOf(first, "endParaRPr") : void 0;
|
|
635
|
+
let rPr;
|
|
636
|
+
for (const p of paras) {
|
|
637
|
+
const r = childOf(p, "r");
|
|
638
|
+
if (r) {
|
|
639
|
+
rPr = childOf(r, "rPr");
|
|
640
|
+
break;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
for (const p of paras) txBody.removeChild(p);
|
|
644
|
+
for (const line of stripXmlIllegal(normalizeNewlines(text)).split("\n")) {
|
|
645
|
+
const p = el(doc, "a:p");
|
|
646
|
+
if (pPr) p.appendChild(pPr.cloneNode(true));
|
|
647
|
+
if (line) {
|
|
648
|
+
const props = rPr ? rPr.cloneNode(true) : endRPr ? cloneAs(endRPr, "a:rPr") : el(doc, "a:rPr", { lang: "zh-CN", altLang: "en-US", dirty: "0" });
|
|
649
|
+
const t = el(doc, "a:t");
|
|
650
|
+
t.appendChild(doc.createTextNode(line));
|
|
651
|
+
p.appendChild(el(doc, "a:r", {}, [props, t]));
|
|
652
|
+
} else if (endRPr) {
|
|
653
|
+
p.appendChild(endRPr.cloneNode(true));
|
|
654
|
+
}
|
|
655
|
+
insertBeforeFirst(txBody, p, ["extLst"]);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// src/slide-runtime/ops-shapes.ts
|
|
660
|
+
var FILL_TAGS = ["noFill", "solidFill", "gradFill", "blipFill", "pattFill", "grpFill"];
|
|
661
|
+
var AFTER_FILL = ["ln", "effectLst", "effectDag", "scene3d", "sp3d", "extLst"];
|
|
662
|
+
var AFTER_LN = ["effectLst", "effectDag", "scene3d", "sp3d", "extLst"];
|
|
663
|
+
var AUTOFIT = { none: "a:noAutofit", shrink: "a:normAutofit", resize: "a:spAutoFit" };
|
|
664
|
+
var AUTOFIT_TAGS = ["noAutofit", "normAutofit", "spAutoFit"];
|
|
665
|
+
var AFTER_AUTOFIT = ["scene3d", "sp3d", "flatTx", "extLst"];
|
|
666
|
+
var PRESET_GEOMETRIES = new Set(
|
|
667
|
+
`line lineInv triangle rtTriangle rect diamond parallelogram trapezoid nonIsoscelesTrapezoid pentagon hexagon heptagon octagon decagon dodecagon
|
|
668
|
+
star4 star5 star6 star7 star8 star10 star12 star16 star24 star32
|
|
669
|
+
roundRect round1Rect round2SameRect round2DiagRect snipRoundRect snip1Rect snip2SameRect snip2DiagRect
|
|
670
|
+
plaque ellipse teardrop homePlate chevron pieWedge pie blockArc donut noSmoking
|
|
671
|
+
rightArrow leftArrow upArrow downArrow stripedRightArrow notchedRightArrow bentUpArrow leftRightArrow upDownArrow leftUpArrow leftRightUpArrow quadArrow
|
|
672
|
+
leftArrowCallout rightArrowCallout upArrowCallout downArrowCallout leftRightArrowCallout upDownArrowCallout quadArrowCallout
|
|
673
|
+
bentArrow uturnArrow circularArrow leftCircularArrow leftRightCircularArrow curvedRightArrow curvedLeftArrow curvedUpArrow curvedDownArrow swooshArrow
|
|
674
|
+
cube can lightningBolt heart sun moon smileyFace irregularSeal1 irregularSeal2 foldedCorner bevel frame halfFrame corner diagStripe chord arc
|
|
675
|
+
leftBracket rightBracket leftBrace rightBrace bracketPair bracePair
|
|
676
|
+
straightConnector1 bentConnector2 bentConnector3 bentConnector4 bentConnector5 curvedConnector2 curvedConnector3 curvedConnector4 curvedConnector5
|
|
677
|
+
callout1 callout2 callout3 accentCallout1 accentCallout2 accentCallout3 borderCallout1 borderCallout2 borderCallout3
|
|
678
|
+
accentBorderCallout1 accentBorderCallout2 accentBorderCallout3 wedgeRectCallout wedgeRoundRectCallout wedgeEllipseCallout cloudCallout cloud
|
|
679
|
+
ribbon ribbon2 ellipseRibbon ellipseRibbon2 leftRightRibbon verticalScroll horizontalScroll wave doubleWave plus
|
|
680
|
+
flowChartProcess flowChartAlternateProcess flowChartDecision flowChartInputOutput flowChartPredefinedProcess flowChartInternalStorage flowChartDocument
|
|
681
|
+
flowChartMultidocument flowChartTerminator flowChartPreparation flowChartManualInput flowChartManualOperation flowChartConnector flowChartOffpageConnector
|
|
682
|
+
flowChartPunchedCard flowChartPunchedTape flowChartSummingJunction flowChartOr flowChartCollate flowChartSort flowChartExtract flowChartMerge
|
|
683
|
+
flowChartOfflineStorage flowChartOnlineStorage flowChartMagneticTape flowChartMagneticDisk flowChartMagneticDrum flowChartDisplay flowChartDelay
|
|
684
|
+
actionButtonBlank actionButtonHome actionButtonHelp actionButtonInformation actionButtonForwardNext actionButtonBackPrevious actionButtonEnd
|
|
685
|
+
actionButtonBeginning actionButtonReturn actionButtonDocument actionButtonSound actionButtonMovie
|
|
686
|
+
gear6 gear9 funnel mathPlus mathMinus mathMultiply mathDivide mathEqual mathNotEqual cornerTabs squareTabs plaqueTabs chartX chartStar chartPlus`.split(/\s+/)
|
|
687
|
+
);
|
|
688
|
+
function fillNode(doc, fill) {
|
|
689
|
+
if (fill === "none") return el(doc, "a:noFill");
|
|
690
|
+
return el(doc, "a:solidFill", {}, [el(doc, "a:srgbClr", { val: parseColor(fill) })]);
|
|
691
|
+
}
|
|
692
|
+
function xfrmNode(doc, a) {
|
|
693
|
+
return el(doc, "a:xfrm", {}, [
|
|
694
|
+
el(doc, "a:off", { x: ptToEmu(a.x), y: ptToEmu(a.y) }),
|
|
695
|
+
el(doc, "a:ext", { cx: ptToEmu(a.w), cy: ptToEmu(a.h) })
|
|
696
|
+
]);
|
|
697
|
+
}
|
|
698
|
+
function rectGeom(doc, prst = "rect") {
|
|
699
|
+
return el(doc, "a:prstGeom", { prst }, [el(doc, "a:avLst")]);
|
|
700
|
+
}
|
|
701
|
+
function sniffImage(bytes) {
|
|
702
|
+
const b = bytes;
|
|
703
|
+
if (b[0] === 137 && b[1] === 80 && b[2] === 78 && b[3] === 71) return { ext: "png", mime: "image/png" };
|
|
704
|
+
if (b[0] === 255 && b[1] === 216 && b[2] === 255) return { ext: "jpeg", mime: "image/jpeg" };
|
|
705
|
+
if (b[0] === 71 && b[1] === 73 && b[2] === 70 && b[3] === 56) return { ext: "gif", mime: "image/gif" };
|
|
706
|
+
return null;
|
|
707
|
+
}
|
|
708
|
+
function addElement(pkg, args) {
|
|
709
|
+
for (const k of ["x", "y", "w", "h"]) {
|
|
710
|
+
if (!Number.isFinite(args[k])) throw new Error(`${k} 必须是数字(单位 pt)`);
|
|
711
|
+
}
|
|
712
|
+
if (args.w <= 0 || args.h <= 0) throw new Error("w、h 必须大于 0");
|
|
713
|
+
if (!["text", "image", "shape"].includes(args.type)) throw new Error(`type 只能是 text / image / shape,收到 ${args.type}`);
|
|
714
|
+
const path = slidePath(pkg, args.slide);
|
|
715
|
+
const doc = pkg.xml(path);
|
|
716
|
+
const id = nextShapeId(doc);
|
|
717
|
+
let node;
|
|
718
|
+
if (args.type === "text") {
|
|
719
|
+
const body = el(doc, "p:txBody", {}, [
|
|
720
|
+
el(doc, "a:bodyPr", { wrap: "square", rtlCol: 0 }, [el(doc, "a:spAutoFit")]),
|
|
721
|
+
el(doc, "a:lstStyle"),
|
|
722
|
+
el(doc, "a:p")
|
|
723
|
+
]);
|
|
724
|
+
node = el(doc, "p:sp", {}, [
|
|
725
|
+
el(doc, "p:nvSpPr", {}, [
|
|
726
|
+
el(doc, "p:cNvPr", { id, name: `文本框 ${id - 1}` }),
|
|
727
|
+
el(doc, "p:cNvSpPr", { txBox: 1 }),
|
|
728
|
+
el(doc, "p:nvPr")
|
|
729
|
+
]),
|
|
730
|
+
el(doc, "p:spPr", {}, [xfrmNode(doc, args), rectGeom(doc), el(doc, "a:noFill")]),
|
|
731
|
+
body
|
|
732
|
+
]);
|
|
733
|
+
setBodyText(body, args.text ?? "");
|
|
734
|
+
} else if (args.type === "shape") {
|
|
735
|
+
const geometry = args.geometry ?? "rect";
|
|
736
|
+
if (!PRESET_GEOMETRIES.has(geometry)) {
|
|
737
|
+
throw new Error(`geometry 必须是 OOXML 预设几何名,如 rect、roundRect、ellipse、triangle、diamond、rightArrow、star5、chevron;收到 ${geometry}`);
|
|
738
|
+
}
|
|
739
|
+
const body = el(doc, "p:txBody", {}, [
|
|
740
|
+
el(doc, "a:bodyPr", { rtlCol: 0, anchor: "ctr" }),
|
|
741
|
+
el(doc, "a:lstStyle"),
|
|
742
|
+
el(doc, "a:p", {}, [el(doc, "a:pPr", { algn: "ctr" })])
|
|
743
|
+
]);
|
|
744
|
+
node = el(doc, "p:sp", {}, [
|
|
745
|
+
el(doc, "p:nvSpPr", {}, [el(doc, "p:cNvPr", { id, name: `${geometry} ${id - 1}` }), el(doc, "p:cNvSpPr"), el(doc, "p:nvPr")]),
|
|
746
|
+
el(doc, "p:spPr", {}, [
|
|
747
|
+
xfrmNode(doc, args),
|
|
748
|
+
rectGeom(doc, geometry),
|
|
749
|
+
fillNode(doc, args.fill ?? "#4472C4"),
|
|
750
|
+
el(doc, "a:ln", {}, [el(doc, "a:noFill")])
|
|
751
|
+
]),
|
|
752
|
+
body
|
|
753
|
+
]);
|
|
754
|
+
setBodyText(body, args.text ?? "");
|
|
755
|
+
} else {
|
|
756
|
+
if (!args.base64) throw new Error("type=image 需要 path(图片文件路径,由 hub 读取)");
|
|
757
|
+
const bytes = base64ToBytes(args.base64);
|
|
758
|
+
const format = sniffImage(bytes);
|
|
759
|
+
if (!format) throw new Error("只支持 png / jpeg / gif 图片");
|
|
760
|
+
const media = pkg.nextPartPath("ppt/media", "image", format.ext);
|
|
761
|
+
pkg.setBinary(media, bytes);
|
|
762
|
+
pkg.ensureDefault(format.ext, format.mime);
|
|
763
|
+
const rid = pkg.addRel(path, REL.image, media);
|
|
764
|
+
node = el(doc, "p:pic", {}, [
|
|
765
|
+
el(doc, "p:nvPicPr", {}, [
|
|
766
|
+
el(doc, "p:cNvPr", { id, name: `图片 ${id - 1}` }),
|
|
767
|
+
el(doc, "p:cNvPicPr", {}, [el(doc, "a:picLocks", { noChangeAspect: 1 })]),
|
|
768
|
+
el(doc, "p:nvPr")
|
|
769
|
+
]),
|
|
770
|
+
el(doc, "p:blipFill", {}, [el(doc, "a:blip", { "r:embed": rid }), el(doc, "a:stretch", {}, [el(doc, "a:fillRect")])]),
|
|
771
|
+
el(doc, "p:spPr", {}, [xfrmNode(doc, args), rectGeom(doc)])
|
|
772
|
+
]);
|
|
773
|
+
}
|
|
774
|
+
insertBeforeFirst(spTree(doc), node, ["extLst"]);
|
|
775
|
+
pkg.touch(path);
|
|
776
|
+
return { slide: args.slide, shape_id: id, type: args.type };
|
|
777
|
+
}
|
|
778
|
+
function spPrOf(shape) {
|
|
779
|
+
const spPr = childOf(shape, "spPr");
|
|
780
|
+
if (!spPr) throw new Error(`shape_id=${shapeIdOf(shape)}(${shape.localName})不支持填充 / 边框`);
|
|
781
|
+
return spPr;
|
|
782
|
+
}
|
|
783
|
+
function checkGeometry(pkg, slide, shape, p) {
|
|
784
|
+
const touched = [p.x, p.y, p.w, p.h, p.rotation].some((v) => v !== void 0);
|
|
785
|
+
if (!touched) return;
|
|
786
|
+
if (shape.localName === "graphicFrame" && (p.w !== void 0 || p.h !== void 0 || p.rotation !== void 0)) {
|
|
787
|
+
throw new Error(`shape_id=${p.shape_id} 是表格或图表,本期只支持移动(x、y)`);
|
|
788
|
+
}
|
|
789
|
+
if (xfrmOf(shape)) return;
|
|
790
|
+
const partial = [p.x, p.y, p.w, p.h].some((v) => v === void 0);
|
|
791
|
+
if (partial && !inheritedBox(pkg, slide, shape)) {
|
|
792
|
+
throw new Error(`shape_id=${p.shape_id} 没有自己的位置且无法从版式继承,请同时给出 x、y、w、h`);
|
|
793
|
+
}
|
|
794
|
+
if (!childOf(shape, shape.localName === "grpSp" ? "grpSpPr" : "spPr")) {
|
|
795
|
+
throw new Error(`shape_id=${p.shape_id} 缺少 spPr,无法设置位置`);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function setGeometry(pkg, slide, shape, p) {
|
|
799
|
+
const doc = shape.ownerDocument;
|
|
800
|
+
const base = inheritedBox(pkg, slide, shape);
|
|
801
|
+
let xfrm = xfrmOf(shape);
|
|
802
|
+
if (!xfrm) {
|
|
803
|
+
const parent = childOf(shape, shape.localName === "grpSp" ? "grpSpPr" : "spPr");
|
|
804
|
+
xfrm = el(doc, "a:xfrm");
|
|
805
|
+
parent.insertBefore(xfrm, parent.firstChild);
|
|
806
|
+
}
|
|
807
|
+
let off = childOf(xfrm, "off");
|
|
808
|
+
if (!off) {
|
|
809
|
+
off = el(doc, "a:off", { x: base?.x ?? 0, y: base?.y ?? 0 });
|
|
810
|
+
xfrm.insertBefore(off, xfrm.firstChild);
|
|
811
|
+
}
|
|
812
|
+
let ext = childOf(xfrm, "ext");
|
|
813
|
+
if (!ext) {
|
|
814
|
+
ext = el(doc, "a:ext", { cx: base?.w ?? 0, cy: base?.h ?? 0 });
|
|
815
|
+
xfrm.insertBefore(ext, off.nextSibling);
|
|
816
|
+
}
|
|
817
|
+
if (p.x !== void 0) off.setAttribute("x", String(ptToEmu(p.x)));
|
|
818
|
+
if (p.y !== void 0) off.setAttribute("y", String(ptToEmu(p.y)));
|
|
819
|
+
if (p.w !== void 0) ext.setAttribute("cx", String(ptToEmu(p.w)));
|
|
820
|
+
if (p.h !== void 0) ext.setAttribute("cy", String(ptToEmu(p.h)));
|
|
821
|
+
if (p.rotation !== void 0) {
|
|
822
|
+
const rot = Math.round((p.rotation % 360 + 360) % 360 * 6e4);
|
|
823
|
+
if (rot) xfrm.setAttribute("rot", String(rot));
|
|
824
|
+
else xfrm.removeAttribute("rot");
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
function setFill(shape, fill) {
|
|
828
|
+
const spPr = spPrOf(shape);
|
|
829
|
+
for (const child of elements(spPr)) if (FILL_TAGS.includes(child.localName)) removeNode(child);
|
|
830
|
+
insertBeforeFirst(spPr, fillNode(shape.ownerDocument, fill), AFTER_FILL);
|
|
831
|
+
}
|
|
832
|
+
function setLine(shape, line) {
|
|
833
|
+
const doc = shape.ownerDocument;
|
|
834
|
+
const spPr = spPrOf(shape);
|
|
835
|
+
let ln = childOf(spPr, "ln");
|
|
836
|
+
if (line === "none") {
|
|
837
|
+
if (ln) removeNode(ln);
|
|
838
|
+
insertBeforeFirst(spPr, el(doc, "a:ln", {}, [el(doc, "a:noFill")]), AFTER_LN);
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
if (!ln) {
|
|
842
|
+
ln = el(doc, "a:ln");
|
|
843
|
+
insertBeforeFirst(spPr, ln, AFTER_LN);
|
|
844
|
+
}
|
|
845
|
+
if (line.width !== void 0) ln.setAttribute("w", String(ptToEmu(line.width)));
|
|
846
|
+
if (line.color !== void 0) {
|
|
847
|
+
for (const child of elements(ln)) if (["noFill", "solidFill", "gradFill", "pattFill"].includes(child.localName)) removeNode(child);
|
|
848
|
+
ln.insertBefore(fillNode(doc, line.color), ln.firstChild);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
function setTextBody(shape, p) {
|
|
852
|
+
const doc = shape.ownerDocument;
|
|
853
|
+
const body = txBodyOf(shape);
|
|
854
|
+
let bodyPr = childOf(body, "bodyPr");
|
|
855
|
+
if (!bodyPr) {
|
|
856
|
+
bodyPr = el(doc, "a:bodyPr");
|
|
857
|
+
body.insertBefore(bodyPr, body.firstChild);
|
|
858
|
+
}
|
|
859
|
+
if (p.wrap !== void 0) bodyPr.setAttribute("wrap", p.wrap ? "square" : "none");
|
|
860
|
+
if (p.autofit !== void 0) {
|
|
861
|
+
const qname = AUTOFIT[p.autofit];
|
|
862
|
+
const tag = qname.slice(qname.indexOf(":") + 1);
|
|
863
|
+
const keep = elements(bodyPr).find((c) => c.localName === tag);
|
|
864
|
+
for (const c of elements(bodyPr)) if (c !== keep && AUTOFIT_TAGS.includes(c.localName)) removeNode(c);
|
|
865
|
+
if (!keep) insertBeforeFirst(bodyPr, el(doc, qname), AFTER_AUTOFIT);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
function modifyShapes(pkg, args) {
|
|
869
|
+
if (!Array.isArray(args.shapes) || !args.shapes.length) throw new Error("shapes 至少要有一项");
|
|
870
|
+
const path = slidePath(pkg, args.slide);
|
|
871
|
+
const doc = pkg.xml(path);
|
|
872
|
+
const targets = args.shapes.map((p) => ({ p, shape: findShape(doc, p.shape_id, args.slide) }));
|
|
873
|
+
const timing = Array.from(doc.getElementsByTagName("*")).find((e) => e.localName === "timing");
|
|
874
|
+
if (timing) {
|
|
875
|
+
const animated = new Set(
|
|
876
|
+
Array.from(timing.getElementsByTagName("*")).map((e) => e.getAttribute("spid")).filter((v) => Boolean(v))
|
|
877
|
+
);
|
|
878
|
+
for (const { p, shape } of targets) {
|
|
879
|
+
if (!p.delete) continue;
|
|
880
|
+
const ids = descendants(shape, "cNvPr").map((c) => c.getAttribute("id"));
|
|
881
|
+
if (ids.some((id) => id !== null && animated.has(id))) {
|
|
882
|
+
throw new Error(`shape_id=${p.shape_id} 带有动画(或其组内形状带动画),本期不支持删除带动画的形状;可以把它移出画面或改成空文本`);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
for (const { p, shape } of targets) {
|
|
887
|
+
if (p.delete) continue;
|
|
888
|
+
for (const k of ["x", "y", "rotation"]) {
|
|
889
|
+
if (p[k] !== void 0 && !Number.isFinite(p[k])) throw new Error(`shape_id=${p.shape_id} 的 ${k} 必须是数字`);
|
|
890
|
+
}
|
|
891
|
+
for (const k of ["w", "h"]) {
|
|
892
|
+
if (p[k] !== void 0 && !(Number.isFinite(p[k]) && p[k] > 0)) throw new Error("w、h 必须大于 0");
|
|
893
|
+
}
|
|
894
|
+
if (p.fill !== void 0 && p.fill !== "none") parseColor(p.fill);
|
|
895
|
+
if (p.line && p.line !== "none") {
|
|
896
|
+
if (p.line.color !== void 0) parseColor(p.line.color);
|
|
897
|
+
const w = p.line.width;
|
|
898
|
+
if (w !== void 0 && !(Number.isFinite(w) && w >= 0 && w <= 1584)) throw new Error("边框宽度取值 0~1584 pt");
|
|
899
|
+
}
|
|
900
|
+
if (p.fill !== void 0 || p.line !== void 0) spPrOf(shape);
|
|
901
|
+
if (p.wrap !== void 0 || p.autofit !== void 0) {
|
|
902
|
+
if (p.wrap !== void 0 && typeof p.wrap !== "boolean") throw new Error(`shape_id=${p.shape_id} 的 wrap 要写成 true 或 false`);
|
|
903
|
+
if (p.autofit !== void 0 && !Object.hasOwn(AUTOFIT, p.autofit)) {
|
|
904
|
+
throw new Error(`shape_id=${p.shape_id} 的 autofit 只能是 none / shrink / resize`);
|
|
905
|
+
}
|
|
906
|
+
if (shape.localName !== "sp") {
|
|
907
|
+
throw new Error(`shape_id=${p.shape_id} 不是文本形状(${shape.localName}),换行 / 自适应只对文本框有效`);
|
|
908
|
+
}
|
|
909
|
+
if (!txBodyOf(shape)) {
|
|
910
|
+
throw new Error(`shape_id=${p.shape_id} 还没有文字,先用 slide_replace_text 写入文字再设换行 / 自适应`);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
checkGeometry(pkg, path, shape, p);
|
|
914
|
+
}
|
|
915
|
+
let modified = 0;
|
|
916
|
+
let deleted = 0;
|
|
917
|
+
for (const { p, shape } of targets) {
|
|
918
|
+
if (p.delete) {
|
|
919
|
+
removeNode(shape);
|
|
920
|
+
deleted += 1;
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
if ([p.x, p.y, p.w, p.h, p.rotation].some((v) => v !== void 0)) setGeometry(pkg, path, shape, p);
|
|
924
|
+
if (p.fill !== void 0) setFill(shape, p.fill);
|
|
925
|
+
if (p.line !== void 0) setLine(shape, p.line);
|
|
926
|
+
if (p.wrap !== void 0 || p.autofit !== void 0) setTextBody(shape, p);
|
|
927
|
+
modified += 1;
|
|
928
|
+
}
|
|
929
|
+
pkg.touch(path);
|
|
930
|
+
return { slide: args.slide, modified, deleted };
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// src/slide-runtime/ops-slides.ts
|
|
934
|
+
function manageSlides(pkg, args) {
|
|
935
|
+
switch (args.action) {
|
|
936
|
+
case "add":
|
|
937
|
+
return addSlide(pkg, args.slide, args.layout);
|
|
938
|
+
case "delete":
|
|
939
|
+
return deleteSlide(pkg, args.slide);
|
|
940
|
+
case "duplicate":
|
|
941
|
+
return duplicateSlide(pkg, args.slide, args.to);
|
|
942
|
+
case "move":
|
|
943
|
+
return moveSlide(pkg, args.slide, args.to);
|
|
944
|
+
default:
|
|
945
|
+
throw new Error(`action 只能是 add / delete / duplicate / move,收到 ${String(args.action)}`);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
var EMPTY_SLIDE = '<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/><a:chOff x="0" y="0"/><a:chExt cx="0" cy="0"/></a:xfrm></p:grpSpPr></p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sld>';
|
|
949
|
+
var AFTER_SLD_ID_LST = [
|
|
950
|
+
"sldSz",
|
|
951
|
+
"notesSz",
|
|
952
|
+
"smartTags",
|
|
953
|
+
"embeddedFontLst",
|
|
954
|
+
"custShowLst",
|
|
955
|
+
"photoAlbum",
|
|
956
|
+
"custDataLst",
|
|
957
|
+
"kinsoku",
|
|
958
|
+
"defaultTextStyle",
|
|
959
|
+
"modifyVerifier",
|
|
960
|
+
"extLst"
|
|
961
|
+
];
|
|
962
|
+
function presRoot(pkg) {
|
|
963
|
+
return pkg.xml(PRESENTATION).documentElement;
|
|
964
|
+
}
|
|
965
|
+
function sldIdLst(pkg) {
|
|
966
|
+
const root = presRoot(pkg);
|
|
967
|
+
const existing = childOf(root, "sldIdLst");
|
|
968
|
+
if (existing) return existing;
|
|
969
|
+
const lst = el(root.ownerDocument, "p:sldIdLst");
|
|
970
|
+
insertBeforeFirst(root, lst, AFTER_SLD_ID_LST);
|
|
971
|
+
return lst;
|
|
972
|
+
}
|
|
973
|
+
function sectionIds(pkg) {
|
|
974
|
+
return descendants(presRoot(pkg), "sectionLst").flatMap((lst) => descendants(lst, "sldId"));
|
|
975
|
+
}
|
|
976
|
+
function removeFromSections(pkg, id) {
|
|
977
|
+
for (const s of sectionIds(pkg)) if (s.getAttribute("id") === id) removeNode(s);
|
|
978
|
+
}
|
|
979
|
+
function placeInSection(pkg, id, prevId) {
|
|
980
|
+
const doc = pkg.xml(PRESENTATION);
|
|
981
|
+
const lst = descendants(presRoot(pkg), "sectionLst")[0];
|
|
982
|
+
if (!lst) return;
|
|
983
|
+
const make = (like) => {
|
|
984
|
+
const node = doc.createElementNS(like.namespaceURI, like.nodeName);
|
|
985
|
+
node.setAttribute("id", id);
|
|
986
|
+
return node;
|
|
987
|
+
};
|
|
988
|
+
if (prevId) {
|
|
989
|
+
const prev = descendants(lst, "sldId").find((s) => s.getAttribute("id") === prevId);
|
|
990
|
+
if (prev?.parentNode) {
|
|
991
|
+
prev.parentNode.insertBefore(make(prev), prev.nextSibling);
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
const section = descendants(lst, "section")[0];
|
|
996
|
+
if (!section) return;
|
|
997
|
+
let list = childOf(section, "sldIdLst");
|
|
998
|
+
if (!list) {
|
|
999
|
+
list = doc.createElementNS(section.namespaceURI, `${section.prefix ? `${section.prefix}:` : ""}sldIdLst`);
|
|
1000
|
+
section.insertBefore(list, section.firstChild);
|
|
1001
|
+
}
|
|
1002
|
+
const tagged = doc.createElementNS(section.namespaceURI, `${section.prefix ? `${section.prefix}:` : ""}sldId`);
|
|
1003
|
+
tagged.setAttribute("id", id);
|
|
1004
|
+
list.insertBefore(tagged, list.firstChild);
|
|
1005
|
+
}
|
|
1006
|
+
function registerSlide(pkg, path, after) {
|
|
1007
|
+
const rid = pkg.addRel(PRESENTATION, REL.slide, path);
|
|
1008
|
+
const lst = sldIdLst(pkg);
|
|
1009
|
+
const items = childrenOf(lst, "sldId");
|
|
1010
|
+
const id = String(Math.max(255, ...items.map((s) => Number(s.getAttribute("id")) || 0)) + 1);
|
|
1011
|
+
const node = el(lst.ownerDocument, "p:sldId", { id, "r:id": rid });
|
|
1012
|
+
const ref = items[after];
|
|
1013
|
+
if (ref) lst.insertBefore(node, ref);
|
|
1014
|
+
else lst.appendChild(node);
|
|
1015
|
+
placeInSection(pkg, id, after > 0 ? items[after - 1].getAttribute("id") ?? null : null);
|
|
1016
|
+
pkg.touch(PRESENTATION);
|
|
1017
|
+
return id;
|
|
1018
|
+
}
|
|
1019
|
+
function allLayouts(pkg) {
|
|
1020
|
+
return pkg.paths().filter((p) => /^ppt\/slideLayouts\/slideLayout\d+\.xml$/.test(p)).sort((a, b) => Number(/(\d+)\.xml$/.exec(a)?.[1]) - Number(/(\d+)\.xml$/.exec(b)?.[1]));
|
|
1021
|
+
}
|
|
1022
|
+
function pickLayout(pkg, after, name) {
|
|
1023
|
+
const layouts = allLayouts(pkg);
|
|
1024
|
+
if (name !== void 0) {
|
|
1025
|
+
const hit = layouts.find((l) => layoutName(pkg, l) === name);
|
|
1026
|
+
if (!hit) throw new Error(`没有名为「${name}」的版式。可用:${layouts.map((l) => layoutName(pkg, l)).join("、")}`);
|
|
1027
|
+
return hit;
|
|
1028
|
+
}
|
|
1029
|
+
const refs = slideRefs(pkg);
|
|
1030
|
+
const basis = refs[Math.max(after, 1) - 1];
|
|
1031
|
+
const fromSlide = basis ? layoutOf(pkg, basis.path) : void 0;
|
|
1032
|
+
const layout = fromSlide ?? layouts[0];
|
|
1033
|
+
if (!layout) throw new Error("这份演示文稿没有任何版式,无法新增页");
|
|
1034
|
+
return layout;
|
|
1035
|
+
}
|
|
1036
|
+
var SKIP_PLACEHOLDERS = /* @__PURE__ */ new Set(["dt", "ftr", "sldNum"]);
|
|
1037
|
+
function addSlide(pkg, after, layoutNameArg) {
|
|
1038
|
+
const count2 = slideRefs(pkg).length;
|
|
1039
|
+
if (!Number.isInteger(after) || after < 0 || after > count2) {
|
|
1040
|
+
throw new Error(`slide=${after} 越界:新页插在第 slide 页之后,取值 0~${count2}(0 表示插到最前)`);
|
|
1041
|
+
}
|
|
1042
|
+
const layout = pickLayout(pkg, after, layoutNameArg);
|
|
1043
|
+
const path = pkg.nextPartPath("ppt/slides", "slide", "xml");
|
|
1044
|
+
const doc = pkg.createXml(path, EMPTY_SLIDE);
|
|
1045
|
+
const tree = spTree(doc);
|
|
1046
|
+
let id = 2;
|
|
1047
|
+
for (const { el: shape, inGroup } of allShapes(spTree(pkg.xml(layout)))) {
|
|
1048
|
+
const ph = placeholderOf(shape);
|
|
1049
|
+
if (inGroup || shape.localName !== "sp" || !ph || SKIP_PLACEHOLDERS.has(ph.type)) continue;
|
|
1050
|
+
const phNode = childOf(childOf(childOf(shape, "nvSpPr"), "nvPr"), "ph");
|
|
1051
|
+
tree.appendChild(
|
|
1052
|
+
el(doc, "p:sp", {}, [
|
|
1053
|
+
el(doc, "p:nvSpPr", {}, [
|
|
1054
|
+
el(doc, "p:cNvPr", { id, name: shapeNameOf(shape) }),
|
|
1055
|
+
el(doc, "p:cNvSpPr", {}, [el(doc, "a:spLocks", { noGrp: 1 })]),
|
|
1056
|
+
el(doc, "p:nvPr", {}, [doc.importNode(phNode, true)])
|
|
1057
|
+
]),
|
|
1058
|
+
el(doc, "p:spPr"),
|
|
1059
|
+
el(doc, "p:txBody", {}, [
|
|
1060
|
+
el(doc, "a:bodyPr"),
|
|
1061
|
+
el(doc, "a:lstStyle"),
|
|
1062
|
+
el(doc, "a:p", {}, [el(doc, "a:endParaRPr", { lang: "zh-CN", altLang: "en-US" })])
|
|
1063
|
+
])
|
|
1064
|
+
])
|
|
1065
|
+
);
|
|
1066
|
+
id += 1;
|
|
1067
|
+
}
|
|
1068
|
+
pkg.addRel(path, REL.slideLayout, layout);
|
|
1069
|
+
pkg.setOverride(path, CT.slide);
|
|
1070
|
+
registerSlide(pkg, path, after);
|
|
1071
|
+
return { action: "add", slide: after + 1, layout: layoutName(pkg, layout), placeholders: id - 2 };
|
|
1072
|
+
}
|
|
1073
|
+
function deleteSlide(pkg, n) {
|
|
1074
|
+
const path = slidePath(pkg, n);
|
|
1075
|
+
const refs = slideRefs(pkg);
|
|
1076
|
+
const { sldId } = refs[n - 1];
|
|
1077
|
+
const rid = rId(sldId);
|
|
1078
|
+
const id = sldId.getAttribute("id") ?? "";
|
|
1079
|
+
removeNode(sldId);
|
|
1080
|
+
removeFromSections(pkg, id);
|
|
1081
|
+
for (const sld of descendants(presRoot(pkg), "sld")) if (rId(sld) === rid) removeNode(sld);
|
|
1082
|
+
pkg.touch(PRESENTATION);
|
|
1083
|
+
pkg.removeRel(PRESENTATION, rid);
|
|
1084
|
+
const notes = notesPathOf(pkg, path);
|
|
1085
|
+
dropInboundLinks(pkg, path, notes);
|
|
1086
|
+
pkg.removePart(path);
|
|
1087
|
+
if (notes && pkg.referrers(notes).length === 0) pkg.removePart(notes);
|
|
1088
|
+
return { action: "delete", slide: n, slides: refs.length - 1 };
|
|
1089
|
+
}
|
|
1090
|
+
function dropInboundLinks(pkg, path, notes) {
|
|
1091
|
+
for (const owner of pkg.referrers(path)) {
|
|
1092
|
+
if (owner === path || owner === notes) continue;
|
|
1093
|
+
const ids = pkg.rels(owner).filter((r) => !r.external && r.target === path).map((r) => r.id);
|
|
1094
|
+
let touched = false;
|
|
1095
|
+
for (const id of ids) {
|
|
1096
|
+
pkg.removeRel(owner, id);
|
|
1097
|
+
if (owner.endsWith(".xml")) {
|
|
1098
|
+
const doc = pkg.xml(owner);
|
|
1099
|
+
for (const tag of ["hlinkClick", "hlinkHover", "hlinkMouseOver"]) {
|
|
1100
|
+
for (const node of descendants(doc.documentElement, tag)) {
|
|
1101
|
+
if (rId(node) === id) {
|
|
1102
|
+
removeNode(node);
|
|
1103
|
+
touched = true;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
if (touched) pkg.touch(owner);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
var SHAREABLE = /* @__PURE__ */ new Set([REL.slideLayout, REL.image, REL.hyperlink, REL.media, REL.video, REL.audio, REL.slide]);
|
|
1113
|
+
var DROPPED = /* @__PURE__ */ new Set([REL.comments, REL.modernComments]);
|
|
1114
|
+
function duplicateSlide(pkg, n, to) {
|
|
1115
|
+
const src = slidePath(pkg, n);
|
|
1116
|
+
const count2 = slideRefs(pkg).length;
|
|
1117
|
+
const position = to ?? n + 1;
|
|
1118
|
+
if (!Number.isInteger(position) || position < 1 || position > count2 + 1) {
|
|
1119
|
+
throw new Error(`to=${position} 越界:新页页码取值 1~${count2 + 1}`);
|
|
1120
|
+
}
|
|
1121
|
+
const rels = pkg.rels(src);
|
|
1122
|
+
const blocked = rels.filter((r) => !r.external && !SHAREABLE.has(r.type) && !DROPPED.has(r.type) && r.type !== REL.notesSlide);
|
|
1123
|
+
if (blocked.length) {
|
|
1124
|
+
const kinds = [...new Set(blocked.map((r) => r.type.split("/").pop()))].join("、");
|
|
1125
|
+
throw new Error(`第 ${n} 页包含本期不支持复制的对象(${kinds}),可以用 slide_manage_slides add 新建后重建内容`);
|
|
1126
|
+
}
|
|
1127
|
+
const dst = pkg.nextPartPath("ppt/slides", "slide", "xml");
|
|
1128
|
+
pkg.createXml(dst, serializeXml(pkg.env, pkg.xml(src)));
|
|
1129
|
+
const kept = rels.filter((r) => !DROPPED.has(r.type) && r.type !== REL.notesSlide);
|
|
1130
|
+
const notesRel = rels.find((r) => r.type === REL.notesSlide);
|
|
1131
|
+
if (notesRel) {
|
|
1132
|
+
const notesDst = pkg.nextPartPath("ppt/notesSlides", "notesSlide", "xml");
|
|
1133
|
+
pkg.createXml(notesDst, serializeXml(pkg.env, pkg.xml(notesRel.target)));
|
|
1134
|
+
pkg.writeRels(
|
|
1135
|
+
notesDst,
|
|
1136
|
+
pkg.rels(notesRel.target).map((r) => r.type === REL.slide && r.target === src ? { ...r, target: dst } : r)
|
|
1137
|
+
);
|
|
1138
|
+
pkg.setOverride(notesDst, CT.notesSlide);
|
|
1139
|
+
kept.push({ ...notesRel, target: notesDst });
|
|
1140
|
+
}
|
|
1141
|
+
pkg.writeRels(dst, kept);
|
|
1142
|
+
pkg.setOverride(dst, CT.slide);
|
|
1143
|
+
registerSlide(pkg, dst, position - 1);
|
|
1144
|
+
return { action: "duplicate", slide: position };
|
|
1145
|
+
}
|
|
1146
|
+
function moveSlide(pkg, n, to) {
|
|
1147
|
+
slidePath(pkg, n);
|
|
1148
|
+
const refs = slideRefs(pkg);
|
|
1149
|
+
if (to === void 0) throw new Error(`move 需要 to(移动后的页码,1~${refs.length})`);
|
|
1150
|
+
if (!Number.isInteger(to) || to < 1 || to > refs.length) {
|
|
1151
|
+
throw new Error(`to=${to} 越界:移动后的页码取值 1~${refs.length}`);
|
|
1152
|
+
}
|
|
1153
|
+
const lst = sldIdLst(pkg);
|
|
1154
|
+
const node = refs[n - 1].sldId;
|
|
1155
|
+
const id = node.getAttribute("id") ?? "";
|
|
1156
|
+
lst.removeChild(node);
|
|
1157
|
+
const items = childrenOf(lst, "sldId");
|
|
1158
|
+
const ref = items[to - 1];
|
|
1159
|
+
if (ref) lst.insertBefore(node, ref);
|
|
1160
|
+
else lst.appendChild(node);
|
|
1161
|
+
removeFromSections(pkg, id);
|
|
1162
|
+
placeInSection(pkg, id, to > 1 ? items[to - 2].getAttribute("id") ?? null : null);
|
|
1163
|
+
pkg.touch(PRESENTATION);
|
|
1164
|
+
return { action: "move", from: n, to };
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
// src/slide-runtime/ops-notes.ts
|
|
1168
|
+
var EMPTY_NOTES = '<p:notes xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr/><p:sp><p:nvSpPr><p:cNvPr id="2" name="幻灯片图像占位符 1"/><p:cNvSpPr><a:spLocks noGrp="1" noRot="1" noChangeAspect="1"/></p:cNvSpPr><p:nvPr><p:ph type="sldImg"/></p:nvPr></p:nvSpPr><p:spPr/></p:sp><p:sp><p:nvSpPr><p:cNvPr id="3" name="备注占位符 2"/><p:cNvSpPr><a:spLocks noGrp="1"/></p:cNvSpPr><p:nvPr><p:ph type="body" idx="1"/></p:nvPr></p:nvSpPr><p:spPr/><p:txBody><a:bodyPr/><a:lstStyle/><a:p/></p:txBody></p:sp></p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:notes>';
|
|
1169
|
+
function createNotes(pkg, slide) {
|
|
1170
|
+
const master = pkg.relTarget(PRESENTATION, REL.notesMaster);
|
|
1171
|
+
if (!master) throw new Error("这份演示文稿没有备注母版(notesMaster),暂不支持新建备注");
|
|
1172
|
+
const path = pkg.nextPartPath("ppt/notesSlides", "notesSlide", "xml");
|
|
1173
|
+
pkg.createXml(path, EMPTY_NOTES);
|
|
1174
|
+
pkg.addRel(path, REL.notesMaster, master);
|
|
1175
|
+
pkg.addRel(path, REL.slide, slide);
|
|
1176
|
+
pkg.addRel(slide, REL.notesSlide, path);
|
|
1177
|
+
pkg.setOverride(path, CT.notesSlide);
|
|
1178
|
+
return path;
|
|
1179
|
+
}
|
|
1180
|
+
function setNotes(pkg, args) {
|
|
1181
|
+
if (typeof args.text !== "string") throw new Error("slide_set_notes 需要 text");
|
|
1182
|
+
const slide = slidePath(pkg, args.slide);
|
|
1183
|
+
const existing = notesPathOf(pkg, slide);
|
|
1184
|
+
const path = existing ?? createNotes(pkg, slide);
|
|
1185
|
+
const body = notesBodyOf(pkg.xml(path));
|
|
1186
|
+
if (!body) throw new Error(`第 ${args.slide} 页的备注页没有正文占位符,无法写入`);
|
|
1187
|
+
setBodyText(ensureTxBody(body), args.text);
|
|
1188
|
+
pkg.touch(path);
|
|
1189
|
+
return { slide: args.slide, created: !existing };
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// src/slide-runtime/ops-table.ts
|
|
1193
|
+
var isContinuation = (tc) => boolAttr(tc, "hMerge") || boolAttr(tc, "vMerge");
|
|
1194
|
+
var isMerged = (tc) => (intAttr(tc, "gridSpan") ?? 1) > 1 || (intAttr(tc, "rowSpan") ?? 1) > 1 || isContinuation(tc);
|
|
1195
|
+
function cellBody(tc) {
|
|
1196
|
+
const existing = childOf(tc, "txBody");
|
|
1197
|
+
if (existing) return existing;
|
|
1198
|
+
const doc = tc.ownerDocument;
|
|
1199
|
+
const body = el(doc, "a:txBody", {}, [el(doc, "a:bodyPr"), el(doc, "a:lstStyle"), el(doc, "a:p")]);
|
|
1200
|
+
tc.insertBefore(body, tc.firstChild);
|
|
1201
|
+
return body;
|
|
1202
|
+
}
|
|
1203
|
+
function blank(node) {
|
|
1204
|
+
const copy = node.cloneNode(true);
|
|
1205
|
+
const strip = (e) => {
|
|
1206
|
+
for (const ext of childrenOf(e, "extLst")) removeNode(ext);
|
|
1207
|
+
};
|
|
1208
|
+
strip(copy);
|
|
1209
|
+
const cells = copy.localName === "tc" ? [copy] : childrenOf(copy, "tc");
|
|
1210
|
+
for (const tc of cells) {
|
|
1211
|
+
strip(tc);
|
|
1212
|
+
setBodyText(cellBody(tc), "");
|
|
1213
|
+
}
|
|
1214
|
+
return copy;
|
|
1215
|
+
}
|
|
1216
|
+
function count(args) {
|
|
1217
|
+
const n = args.count ?? 1;
|
|
1218
|
+
if (!Number.isInteger(n) || n < 1) throw new Error(`count 必须是正整数,收到 ${args.count}`);
|
|
1219
|
+
return n;
|
|
1220
|
+
}
|
|
1221
|
+
function tableEdit(pkg, args) {
|
|
1222
|
+
const path = slidePath(pkg, args.slide);
|
|
1223
|
+
const frame = findShape(pkg.xml(path), args.shape_id, args.slide);
|
|
1224
|
+
const tbl = childOf(childOf(childOf(frame, "graphic"), "graphicData"), "tbl");
|
|
1225
|
+
if (frame.localName !== "graphicFrame" || !tbl) throw new Error(`shape_id=${args.shape_id} 不是表格`);
|
|
1226
|
+
const grid = childOf(tbl, "tblGrid");
|
|
1227
|
+
const ext = childOf(childOf(frame, "xfrm"), "ext");
|
|
1228
|
+
const rows = () => childrenOf(tbl, "tr");
|
|
1229
|
+
const cols = () => childrenOf(grid, "gridCol");
|
|
1230
|
+
const mergeError = (where) => new Error(`${where}涉及合并单元格,本期不支持对合并区域做增删;可以只用 set_cells 改文字`);
|
|
1231
|
+
switch (args.action) {
|
|
1232
|
+
case "set_cells": {
|
|
1233
|
+
if (!args.cells?.length) throw new Error("set_cells 需要 cells: [{row, col, text}]");
|
|
1234
|
+
const targets = args.cells.map(({ row, col, text }) => {
|
|
1235
|
+
const tc = childrenOf(rows()[row - 1], "tc")[col - 1];
|
|
1236
|
+
if (!tc) throw new Error(`第 ${row} 行第 ${col} 列不存在(表格 ${rows().length} 行 ${cols().length} 列,从 1 开始)`);
|
|
1237
|
+
if (isContinuation(tc)) throw new Error(`第 ${row} 行第 ${col} 列是合并单元格的被合并部分,请写到合并区域的左上角`);
|
|
1238
|
+
return { tc, text };
|
|
1239
|
+
});
|
|
1240
|
+
for (const { tc, text } of targets) setBodyText(cellBody(tc), text);
|
|
1241
|
+
break;
|
|
1242
|
+
}
|
|
1243
|
+
case "insert_rows": {
|
|
1244
|
+
const n = count(args);
|
|
1245
|
+
const all = rows();
|
|
1246
|
+
const index = args.index ?? all.length + 1;
|
|
1247
|
+
if (!Number.isInteger(index) || index < 1 || index > all.length + 1) throw new Error(`index=${index} 越界:取值 1~${all.length + 1}`);
|
|
1248
|
+
const template = all[index - 2] ?? all[0];
|
|
1249
|
+
const next = all[index - 1];
|
|
1250
|
+
if (childrenOf(template, "tc").some(isMerged) || next && childrenOf(next, "tc").some(isContinuation)) {
|
|
1251
|
+
throw mergeError(`第 ${index} 行附近`);
|
|
1252
|
+
}
|
|
1253
|
+
for (let i = 0; i < n; i++) {
|
|
1254
|
+
const row = blank(template);
|
|
1255
|
+
if (next) tbl.insertBefore(row, next);
|
|
1256
|
+
else insertBeforeFirst(tbl, row, ["extLst"]);
|
|
1257
|
+
}
|
|
1258
|
+
ext.setAttribute("cy", String((intAttr(ext, "cy") ?? 0) + (intAttr(template, "h") ?? 0) * n));
|
|
1259
|
+
break;
|
|
1260
|
+
}
|
|
1261
|
+
case "delete_rows": {
|
|
1262
|
+
const n = count(args);
|
|
1263
|
+
const all = rows();
|
|
1264
|
+
const index = args.index ?? 0;
|
|
1265
|
+
if (!Number.isInteger(index) || index < 1 || index + n - 1 > all.length) throw new Error(`第 ${index}~${index + n - 1} 行越界:表格共 ${all.length} 行`);
|
|
1266
|
+
if (n >= all.length) throw new Error("至少保留一行;要删整张表请用 slide_modify_shapes 的 delete");
|
|
1267
|
+
const doomed = all.slice(index - 1, index - 1 + n);
|
|
1268
|
+
const after = all[index - 1 + n];
|
|
1269
|
+
if (doomed.some((tr) => childrenOf(tr, "tc").some(isMerged)) || after && childrenOf(after, "tc").some((tc) => boolAttr(tc, "vMerge"))) {
|
|
1270
|
+
throw mergeError(`第 ${index}~${index + n - 1} 行`);
|
|
1271
|
+
}
|
|
1272
|
+
const height = doomed.reduce((s, tr) => s + (intAttr(tr, "h") ?? 0), 0);
|
|
1273
|
+
for (const tr of doomed) removeNode(tr);
|
|
1274
|
+
ext.setAttribute("cy", String(Math.max(0, (intAttr(ext, "cy") ?? 0) - height)));
|
|
1275
|
+
break;
|
|
1276
|
+
}
|
|
1277
|
+
case "insert_cols": {
|
|
1278
|
+
const n = count(args);
|
|
1279
|
+
const gridCols = cols();
|
|
1280
|
+
const index = args.index ?? gridCols.length + 1;
|
|
1281
|
+
if (!Number.isInteger(index) || index < 1 || index > gridCols.length + 1) throw new Error(`index=${index} 越界:取值 1~${gridCols.length + 1}`);
|
|
1282
|
+
const t = index >= 2 ? index - 1 : 1;
|
|
1283
|
+
for (const tr of rows()) {
|
|
1284
|
+
const tcs = childrenOf(tr, "tc");
|
|
1285
|
+
if (isMerged(tcs[t - 1]) || tcs[index - 1] && boolAttr(tcs[index - 1], "hMerge")) throw mergeError(`第 ${index} 列附近`);
|
|
1286
|
+
}
|
|
1287
|
+
const colTemplate = gridCols[t - 1];
|
|
1288
|
+
for (let i = 0; i < n; i++) {
|
|
1289
|
+
const col = blank(colTemplate);
|
|
1290
|
+
if (gridCols[index - 1]) grid.insertBefore(col, gridCols[index - 1]);
|
|
1291
|
+
else insertBeforeFirst(grid, col, ["extLst"]);
|
|
1292
|
+
}
|
|
1293
|
+
for (const tr of rows()) {
|
|
1294
|
+
const tcs = childrenOf(tr, "tc");
|
|
1295
|
+
for (let i = 0; i < n; i++) {
|
|
1296
|
+
const cell = blank(tcs[t - 1]);
|
|
1297
|
+
if (tcs[index - 1]) tr.insertBefore(cell, tcs[index - 1]);
|
|
1298
|
+
else insertBeforeFirst(tr, cell, ["extLst"]);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
ext.setAttribute("cx", String((intAttr(ext, "cx") ?? 0) + (intAttr(colTemplate, "w") ?? 0) * n));
|
|
1302
|
+
break;
|
|
1303
|
+
}
|
|
1304
|
+
case "delete_cols": {
|
|
1305
|
+
const n = count(args);
|
|
1306
|
+
const gridCols = cols();
|
|
1307
|
+
const index = args.index ?? 0;
|
|
1308
|
+
if (!Number.isInteger(index) || index < 1 || index + n - 1 > gridCols.length) throw new Error(`第 ${index}~${index + n - 1} 列越界:表格共 ${gridCols.length} 列`);
|
|
1309
|
+
if (n >= gridCols.length) throw new Error("至少保留一列;要删整张表请用 slide_modify_shapes 的 delete");
|
|
1310
|
+
for (const tr of rows()) {
|
|
1311
|
+
const tcs = childrenOf(tr, "tc");
|
|
1312
|
+
const after = tcs[index - 1 + n];
|
|
1313
|
+
if (tcs.slice(index - 1, index - 1 + n).some(isMerged) || after && boolAttr(after, "hMerge")) throw mergeError(`第 ${index}~${index + n - 1} 列`);
|
|
1314
|
+
}
|
|
1315
|
+
const doomed = gridCols.slice(index - 1, index - 1 + n);
|
|
1316
|
+
const width = doomed.reduce((s, c) => s + (intAttr(c, "w") ?? 0), 0);
|
|
1317
|
+
for (const c of doomed) removeNode(c);
|
|
1318
|
+
for (const tr of rows()) for (const tc of childrenOf(tr, "tc").slice(index - 1, index - 1 + n)) removeNode(tc);
|
|
1319
|
+
ext.setAttribute("cx", String(Math.max(0, (intAttr(ext, "cx") ?? 0) - width)));
|
|
1320
|
+
break;
|
|
1321
|
+
}
|
|
1322
|
+
default:
|
|
1323
|
+
throw new Error(`action 只能是 set_cells / insert_rows / delete_rows / insert_cols / delete_cols,收到 ${String(args.action)}`);
|
|
1324
|
+
}
|
|
1325
|
+
pkg.touch(path);
|
|
1326
|
+
return { slide: args.slide, shape_id: args.shape_id, action: args.action, rows: rows().length, cols: cols().length };
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
// src/slide-runtime/rpr.ts
|
|
1330
|
+
var RPR_ORDER = [
|
|
1331
|
+
"ln",
|
|
1332
|
+
"noFill",
|
|
1333
|
+
"solidFill",
|
|
1334
|
+
"gradFill",
|
|
1335
|
+
"blipFill",
|
|
1336
|
+
"pattFill",
|
|
1337
|
+
"grpFill",
|
|
1338
|
+
"effectLst",
|
|
1339
|
+
"effectDag",
|
|
1340
|
+
"highlight",
|
|
1341
|
+
"uLnTx",
|
|
1342
|
+
"uLn",
|
|
1343
|
+
"uFillTx",
|
|
1344
|
+
"uFill",
|
|
1345
|
+
"latin",
|
|
1346
|
+
"ea",
|
|
1347
|
+
"cs",
|
|
1348
|
+
"sym",
|
|
1349
|
+
"hlinkClick",
|
|
1350
|
+
"hlinkMouseOver",
|
|
1351
|
+
"rtl",
|
|
1352
|
+
"extLst"
|
|
1353
|
+
];
|
|
1354
|
+
var FILL_TAGS2 = ["noFill", "solidFill", "gradFill", "blipFill", "pattFill", "grpFill"];
|
|
1355
|
+
function insertInOrder(rPr, node) {
|
|
1356
|
+
insertBeforeFirst(rPr, node, RPR_ORDER.slice(RPR_ORDER.indexOf(node.localName) + 1));
|
|
1357
|
+
}
|
|
1358
|
+
function removeChildren(rPr, tags) {
|
|
1359
|
+
for (const c of elements(rPr)) if (tags.includes(c.localName)) removeNode(c);
|
|
1360
|
+
}
|
|
1361
|
+
var CHAR_KEYS = ["bold", "italic", "underline", "strike", "size", "color", "font", "highlight"];
|
|
1362
|
+
function validateCharFormat(f, extraKeys = []) {
|
|
1363
|
+
if (!f || typeof f !== "object" || Array.isArray(f)) throw new Error("format 必须是对象");
|
|
1364
|
+
const keys = Object.keys(f);
|
|
1365
|
+
const unknown = keys.filter((k) => !CHAR_KEYS.includes(k) && !extraKeys.includes(k));
|
|
1366
|
+
if (unknown.length) throw new Error(`format 里有不认识的字段:${unknown.join("、")}`);
|
|
1367
|
+
if (!keys.length) throw new Error("format 至少要有一个字段");
|
|
1368
|
+
for (const k of ["bold", "italic", "underline", "strike"]) {
|
|
1369
|
+
if (f[k] !== void 0 && typeof f[k] !== "boolean") throw new Error(`${k} 要写成 true 或 false`);
|
|
1370
|
+
}
|
|
1371
|
+
if (f.size !== void 0 && f.size !== null) {
|
|
1372
|
+
const v = f.size;
|
|
1373
|
+
if (typeof v !== "number" || !Number.isFinite(v) || v < 1 || v > 400) throw new Error("size(字号,pt)取值 1~400");
|
|
1374
|
+
}
|
|
1375
|
+
if (f.color !== void 0 && f.color !== null) parseTextColor(f.color);
|
|
1376
|
+
if (f.highlight !== void 0 && f.highlight !== null) parseColor(f.highlight);
|
|
1377
|
+
if (f.font !== void 0 && f.font !== null) {
|
|
1378
|
+
const v = f.font;
|
|
1379
|
+
if (typeof v !== "string" || !v.trim() || stripXmlIllegal(v) !== v) throw new Error("font 要写成非空的字体名");
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
function ensureRPr(run) {
|
|
1383
|
+
const existing = childOf(run, "rPr");
|
|
1384
|
+
if (existing) return existing;
|
|
1385
|
+
const rPr = el(run.ownerDocument, "a:rPr");
|
|
1386
|
+
run.insertBefore(rPr, run.firstChild);
|
|
1387
|
+
return rPr;
|
|
1388
|
+
}
|
|
1389
|
+
function colorFill(doc, c) {
|
|
1390
|
+
const clr = "srgb" in c ? el(doc, "a:srgbClr", { val: c.srgb }) : el(doc, "a:schemeClr", { val: c.scheme });
|
|
1391
|
+
return el(doc, "a:solidFill", {}, [clr]);
|
|
1392
|
+
}
|
|
1393
|
+
function applyCharFormat(rPr, f) {
|
|
1394
|
+
const doc = rPr.ownerDocument;
|
|
1395
|
+
if (f.bold !== void 0) rPr.setAttribute("b", f.bold ? "1" : "0");
|
|
1396
|
+
if (f.italic !== void 0) rPr.setAttribute("i", f.italic ? "1" : "0");
|
|
1397
|
+
if (f.underline !== void 0) rPr.setAttribute("u", f.underline ? "sng" : "none");
|
|
1398
|
+
if (f.strike !== void 0) rPr.setAttribute("strike", f.strike ? "sngStrike" : "noStrike");
|
|
1399
|
+
if (f.size !== void 0) {
|
|
1400
|
+
if (f.size === null) rPr.removeAttribute("sz");
|
|
1401
|
+
else rPr.setAttribute("sz", String(Math.round(f.size * 100)));
|
|
1402
|
+
}
|
|
1403
|
+
if (f.color !== void 0) {
|
|
1404
|
+
removeChildren(rPr, FILL_TAGS2);
|
|
1405
|
+
if (f.color !== null) insertInOrder(rPr, colorFill(doc, parseTextColor(f.color)));
|
|
1406
|
+
}
|
|
1407
|
+
if (f.highlight !== void 0) {
|
|
1408
|
+
removeChildren(rPr, ["highlight"]);
|
|
1409
|
+
if (f.highlight !== null) {
|
|
1410
|
+
insertInOrder(rPr, el(doc, "a:highlight", {}, [el(doc, "a:srgbClr", { val: parseColor(f.highlight) })]));
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
if (f.font !== void 0) {
|
|
1414
|
+
removeChildren(rPr, ["latin", "ea", "cs"]);
|
|
1415
|
+
if (f.font !== null) {
|
|
1416
|
+
const typeface = f.font.trim();
|
|
1417
|
+
for (const tag of ["latin", "ea", "cs"]) insertInOrder(rPr, el(doc, `a:${tag}`, { typeface }));
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
var END_MARK_KEYS = ["bold", "italic", "underline", "strike", "size", "color", "font"];
|
|
1422
|
+
function endMarkFormat(f) {
|
|
1423
|
+
return Object.fromEntries(Object.entries(f).filter(([k]) => END_MARK_KEYS.includes(k)));
|
|
1424
|
+
}
|
|
1425
|
+
function setHyperlink(rPr, link) {
|
|
1426
|
+
const old = childOf(rPr, "hlinkClick");
|
|
1427
|
+
const prev = old ? rId(old) || void 0 : void 0;
|
|
1428
|
+
if (old) removeNode(old);
|
|
1429
|
+
if (link) insertInOrder(rPr, el(rPr.ownerDocument, "a:hlinkClick", { "r:id": link.rId, action: link.action }));
|
|
1430
|
+
return prev;
|
|
1431
|
+
}
|
|
1432
|
+
var on = (v) => v === "1" || v === "true" ? "1" : "0";
|
|
1433
|
+
function colorOf(holder) {
|
|
1434
|
+
if (holder.localName !== "solidFill" && holder.localName !== "highlight") return "complex";
|
|
1435
|
+
const c = elements(holder)[0];
|
|
1436
|
+
if (!c || elements(c).length) return "complex";
|
|
1437
|
+
if (c.localName === "srgbClr") return `#${(c.getAttribute("val") ?? "").toUpperCase()}`;
|
|
1438
|
+
if (c.localName === "schemeClr") return c.getAttribute("val") || "complex";
|
|
1439
|
+
return "complex";
|
|
1440
|
+
}
|
|
1441
|
+
function readCharFormat(rPr) {
|
|
1442
|
+
const out = {};
|
|
1443
|
+
if (!rPr) return out;
|
|
1444
|
+
if (rPr.hasAttribute("b")) out.b = on(rPr.getAttribute("b"));
|
|
1445
|
+
if (rPr.hasAttribute("i")) out.i = on(rPr.getAttribute("i"));
|
|
1446
|
+
if (rPr.hasAttribute("u")) out.u = rPr.getAttribute("u") === "none" ? "0" : "1";
|
|
1447
|
+
if (rPr.hasAttribute("strike")) out.s = rPr.getAttribute("strike") === "noStrike" ? "0" : "1";
|
|
1448
|
+
const sz = Number(rPr.getAttribute("sz"));
|
|
1449
|
+
if (rPr.hasAttribute("sz") && Number.isFinite(sz)) out.sz = sz / 100;
|
|
1450
|
+
const fill = elements(rPr).find((c) => FILL_TAGS2.includes(c.localName));
|
|
1451
|
+
if (fill) out.color = colorOf(fill);
|
|
1452
|
+
const hl = childOf(rPr, "highlight");
|
|
1453
|
+
if (hl) out.hl = colorOf(hl);
|
|
1454
|
+
const latin = childOf(rPr, "latin")?.getAttribute("typeface") || void 0;
|
|
1455
|
+
const ea = childOf(rPr, "ea")?.getAttribute("typeface") || void 0;
|
|
1456
|
+
if (latin) out.font = latin;
|
|
1457
|
+
if (ea && ea !== latin) out.fontEa = ea;
|
|
1458
|
+
const link = childOf(rPr, "hlinkClick");
|
|
1459
|
+
if (link) {
|
|
1460
|
+
const action = link.getAttribute("action");
|
|
1461
|
+
out.link = action ? { rId: rId(link), action } : { rId: rId(link) };
|
|
1462
|
+
}
|
|
1463
|
+
return out;
|
|
1464
|
+
}
|
|
1465
|
+
var ALGN = { left: "l", center: "ctr", right: "r", justify: "just" };
|
|
1466
|
+
var ALGN_BACK = { l: "left", ctr: "center", r: "right", just: "justify" };
|
|
1467
|
+
function isAlign(v) {
|
|
1468
|
+
return typeof v === "string" && Object.hasOwn(ALGN, v);
|
|
1469
|
+
}
|
|
1470
|
+
function setParagraphAlign(p, align) {
|
|
1471
|
+
let pPr = childOf(p, "pPr");
|
|
1472
|
+
if (!pPr) {
|
|
1473
|
+
pPr = el(p.ownerDocument, "a:pPr");
|
|
1474
|
+
p.insertBefore(pPr, p.firstChild);
|
|
1475
|
+
}
|
|
1476
|
+
pPr.setAttribute("algn", ALGN[align]);
|
|
1477
|
+
}
|
|
1478
|
+
function readParagraphAlign(p) {
|
|
1479
|
+
const v = childOf(p, "pPr")?.getAttribute("algn");
|
|
1480
|
+
if (!v) return void 0;
|
|
1481
|
+
return Object.hasOwn(ALGN_BACK, v) ? ALGN_BACK[v] : v;
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
// src/slide-runtime/read.ts
|
|
1485
|
+
function attrs(a) {
|
|
1486
|
+
return Object.entries(a).filter(([, v]) => v !== void 0 && v !== "").map(([k, v]) => ` ${k}="${escapeXml(String(v))}"`).join("");
|
|
1487
|
+
}
|
|
1488
|
+
function boxAttrs(box, inherited) {
|
|
1489
|
+
if (!box) return {};
|
|
1490
|
+
return {
|
|
1491
|
+
x: emuToPt(box.x),
|
|
1492
|
+
y: emuToPt(box.y),
|
|
1493
|
+
w: emuToPt(box.w),
|
|
1494
|
+
h: emuToPt(box.h),
|
|
1495
|
+
rot: box.rot ? Math.round(box.rot / 6e4 * 100) / 100 : void 0,
|
|
1496
|
+
inherited: inherited ? 1 : void 0
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
function tableCells(frame) {
|
|
1500
|
+
const tbl = childOf(childOf(childOf(frame, "graphic"), "graphicData"), "tbl");
|
|
1501
|
+
const out = [];
|
|
1502
|
+
childrenOf(tbl, "tr").forEach((tr, r) => {
|
|
1503
|
+
childrenOf(tr, "tc").forEach((tc, c) => out.push({ tc, row: r + 1, col: c + 1 }));
|
|
1504
|
+
});
|
|
1505
|
+
return out;
|
|
1506
|
+
}
|
|
1507
|
+
function cellText(tc) {
|
|
1508
|
+
const body = childOf(tc, "txBody");
|
|
1509
|
+
return body ? bodyParagraphs(body).join("\n") : "";
|
|
1510
|
+
}
|
|
1511
|
+
function linkLabel(pkg, slide, link) {
|
|
1512
|
+
const rel = pkg.rels(slide).find((r) => r.id === link.rId);
|
|
1513
|
+
if (!rel) return "?";
|
|
1514
|
+
if (rel.external) return rel.target;
|
|
1515
|
+
if (rel.type === REL.slide) {
|
|
1516
|
+
const n = slideRefs(pkg).findIndex((s) => s.path === rel.target);
|
|
1517
|
+
return n >= 0 ? `slide:${n + 1}` : "slide:?";
|
|
1518
|
+
}
|
|
1519
|
+
return rel.target;
|
|
1520
|
+
}
|
|
1521
|
+
function runAttrs(pkg, slide, f) {
|
|
1522
|
+
return {
|
|
1523
|
+
b: f.b,
|
|
1524
|
+
i: f.i,
|
|
1525
|
+
u: f.u,
|
|
1526
|
+
s: f.s,
|
|
1527
|
+
sz: f.sz,
|
|
1528
|
+
color: f.color,
|
|
1529
|
+
font: f.font,
|
|
1530
|
+
"font-ea": f.fontEa,
|
|
1531
|
+
hl: f.hl,
|
|
1532
|
+
link: f.link ? linkLabel(pkg, slide, f.link) : void 0
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
function renderFormattedParagraph(pkg, slide, p) {
|
|
1536
|
+
let inner = "";
|
|
1537
|
+
for (const child of elements(p)) {
|
|
1538
|
+
if (child.localName === "br") {
|
|
1539
|
+
inner += "\n";
|
|
1540
|
+
continue;
|
|
1541
|
+
}
|
|
1542
|
+
if (child.localName !== "r" && child.localName !== "fld") continue;
|
|
1543
|
+
const text = escapeXml(textOf(childOf(child, "t")));
|
|
1544
|
+
const a = attrs(runAttrs(pkg, slide, readCharFormat(childOf(child, "rPr"))));
|
|
1545
|
+
inner += a ? `<r${a}>${text}</r>` : text;
|
|
1546
|
+
}
|
|
1547
|
+
return `<p${attrs({ align: readParagraphAlign(p) })}>${inner}</p>`;
|
|
1548
|
+
}
|
|
1549
|
+
function renderFormattedBody(pkg, slide, body) {
|
|
1550
|
+
return childrenOf(body, "p").map((p) => renderFormattedParagraph(pkg, slide, p)).join("");
|
|
1551
|
+
}
|
|
1552
|
+
function renderTable(pkg, slide, tbl, head, fmt) {
|
|
1553
|
+
const rows = childrenOf(tbl, "tr");
|
|
1554
|
+
const cols = childrenOf(childOf(tbl, "tblGrid"), "gridCol").length;
|
|
1555
|
+
const body = rows.map((tr) => {
|
|
1556
|
+
const cells = childrenOf(tr, "tc").map((tc) => {
|
|
1557
|
+
const a = attrs({
|
|
1558
|
+
gridSpan: (intAttr(tc, "gridSpan") ?? 1) > 1 ? intAttr(tc, "gridSpan") : void 0,
|
|
1559
|
+
rowSpan: (intAttr(tc, "rowSpan") ?? 1) > 1 ? intAttr(tc, "rowSpan") : void 0,
|
|
1560
|
+
merged: boolAttr(tc, "hMerge") || boolAttr(tc, "vMerge") ? 1 : void 0
|
|
1561
|
+
});
|
|
1562
|
+
const text = cellText(tc);
|
|
1563
|
+
if (!text) return `<td${a}/>`;
|
|
1564
|
+
const inner = fmt ? renderFormattedBody(pkg, slide, childOf(tc, "txBody")) : escapeXml(text);
|
|
1565
|
+
return `<td${a}>${inner}</td>`;
|
|
1566
|
+
});
|
|
1567
|
+
return `<tr>${cells.join("")}</tr>`;
|
|
1568
|
+
}).join("");
|
|
1569
|
+
return `<table${attrs({ ...head, rows: rows.length, cols })}>${body}</table>`;
|
|
1570
|
+
}
|
|
1571
|
+
function renderShape(pkg, slide, shape, readonly, fmt) {
|
|
1572
|
+
const xfrm = xfrmOf(shape);
|
|
1573
|
+
let box = xfrm ? boxOf(xfrm) : void 0;
|
|
1574
|
+
let inherited = false;
|
|
1575
|
+
if (!box && shape.localName === "sp") {
|
|
1576
|
+
box = inheritedBox(pkg, slide, shape);
|
|
1577
|
+
inherited = Boolean(box);
|
|
1578
|
+
}
|
|
1579
|
+
const head = { id: shapeIdOf(shape), name: shapeNameOf(shape) };
|
|
1580
|
+
const geo = boxAttrs(box, inherited);
|
|
1581
|
+
const ro = readonly ? 1 : void 0;
|
|
1582
|
+
switch (shape.localName) {
|
|
1583
|
+
case "sp": {
|
|
1584
|
+
const a = attrs({ ...head, ph: placeholderOf(shape)?.type, ...geo, readonly: ro });
|
|
1585
|
+
const body = txBodyOf(shape);
|
|
1586
|
+
const paras = body ? bodyParagraphs(body) : [];
|
|
1587
|
+
if (!paras.some(Boolean)) return `<sp${a}/>`;
|
|
1588
|
+
const inner = fmt && body ? renderFormattedBody(pkg, slide, body) : paras.map((p) => `<p>${escapeXml(p)}</p>`).join("");
|
|
1589
|
+
return `<sp${a}>${inner}</sp>`;
|
|
1590
|
+
}
|
|
1591
|
+
case "pic":
|
|
1592
|
+
return `<pic${attrs({ ...head, ...geo, readonly: ro })}/>`;
|
|
1593
|
+
case "cxnSp":
|
|
1594
|
+
return `<line${attrs({ ...head, ...geo, readonly: ro })}/>`;
|
|
1595
|
+
case "grpSp": {
|
|
1596
|
+
const children = elements(shape).filter((c) => SHAPE_TAGS.has(c.localName)).map((c) => renderShape(pkg, slide, c, true, fmt)).join("");
|
|
1597
|
+
return `<group${attrs({ ...head, ...geo, readonly: 1 })}>${children}</group>`;
|
|
1598
|
+
}
|
|
1599
|
+
case "graphicFrame": {
|
|
1600
|
+
const data = childOf(childOf(shape, "graphic"), "graphicData");
|
|
1601
|
+
const tbl = childOf(data, "tbl");
|
|
1602
|
+
if (tbl) return renderTable(pkg, slide, tbl, { ...head, ...geo, readonly: ro }, fmt);
|
|
1603
|
+
const uri = data?.getAttribute("uri") ?? "";
|
|
1604
|
+
const kind = uri.includes("/chart") ? "chart" : uri.includes("/diagram") ? "diagram" : "object";
|
|
1605
|
+
return `<${kind}${attrs({ ...head, ...geo, readonly: 1 })}/>`;
|
|
1606
|
+
}
|
|
1607
|
+
default:
|
|
1608
|
+
return `<object${attrs({ ...head, ...geo, readonly: 1 })}/>`;
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
function readSlides(pkg, args = {}) {
|
|
1612
|
+
const total = slideCount(pkg);
|
|
1613
|
+
const wanted = args.slides?.length ? args.slides : Array.from({ length: total }, (_, i) => i + 1);
|
|
1614
|
+
const size = slideSize(pkg);
|
|
1615
|
+
const lines = [`<presentation${attrs({ slides: total, size: `${emuToPt(size.cx)}x${emuToPt(size.cy)}` })}>`];
|
|
1616
|
+
for (const n of wanted) {
|
|
1617
|
+
const path = slidePath(pkg, n);
|
|
1618
|
+
const doc = pkg.xml(path);
|
|
1619
|
+
const layout = layoutOf(pkg, path);
|
|
1620
|
+
const hidden = doc.documentElement.getAttribute("show") === "0" ? 1 : void 0;
|
|
1621
|
+
lines.push(` <slide${attrs({ index: n, layout: layout ? layoutName(pkg, layout) : void 0, hidden })}>`);
|
|
1622
|
+
for (const child of elements(spTree(doc))) {
|
|
1623
|
+
if (SHAPE_TAGS.has(child.localName)) lines.push(` ${renderShape(pkg, path, child, false, Boolean(args.include_format))}`);
|
|
1624
|
+
}
|
|
1625
|
+
if (args.include_notes) {
|
|
1626
|
+
const notes = notesPathOf(pkg, path);
|
|
1627
|
+
const body = notes ? notesBodyOf(pkg.xml(notes)) : void 0;
|
|
1628
|
+
const text = body && txBodyOf(body) ? bodyParagraphs(txBodyOf(body)).join("\n") : "";
|
|
1629
|
+
if (text) lines.push(` <notes>${escapeXml(text)}</notes>`);
|
|
1630
|
+
}
|
|
1631
|
+
lines.push(" </slide>");
|
|
1632
|
+
}
|
|
1633
|
+
lines.push("</presentation>");
|
|
1634
|
+
return { slides: total, xml: lines.join("\n") };
|
|
1635
|
+
}
|
|
1636
|
+
function findInSlides(pkg, args) {
|
|
1637
|
+
if (!args.query) throw new Error("slide_find 需要非空的 query");
|
|
1638
|
+
let test;
|
|
1639
|
+
if (args.regex) {
|
|
1640
|
+
let re;
|
|
1641
|
+
try {
|
|
1642
|
+
re = new RegExp(args.query, "i");
|
|
1643
|
+
} catch (e) {
|
|
1644
|
+
throw new Error(`正则不合法:${e.message}`);
|
|
1645
|
+
}
|
|
1646
|
+
test = (s) => re.test(s);
|
|
1647
|
+
} else {
|
|
1648
|
+
const q = args.query.toLowerCase();
|
|
1649
|
+
test = (s) => s.toLowerCase().includes(q);
|
|
1650
|
+
}
|
|
1651
|
+
const limit = args.limit ?? 50;
|
|
1652
|
+
const hits = [];
|
|
1653
|
+
let truncated = false;
|
|
1654
|
+
slideRefs(pkg).forEach(({ path }, i) => {
|
|
1655
|
+
for (const { el: el2 } of allShapes(spTree(pkg.xml(path)))) {
|
|
1656
|
+
const id = shapeIdOf(el2) ?? -1;
|
|
1657
|
+
if (el2.localName === "sp") {
|
|
1658
|
+
const body = txBodyOf(el2);
|
|
1659
|
+
const text = body ? bodyParagraphs(body).join("\n") : "";
|
|
1660
|
+
if (text && test(text)) hits.push({ slide: i + 1, shape_id: id, text });
|
|
1661
|
+
} else if (el2.localName === "graphicFrame") {
|
|
1662
|
+
for (const { tc, row, col } of tableCells(el2)) {
|
|
1663
|
+
const text = cellText(tc);
|
|
1664
|
+
if (text && test(text)) hits.push({ slide: i + 1, shape_id: id, cell: [row, col], text });
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
});
|
|
1669
|
+
if (hits.length > limit) {
|
|
1670
|
+
truncated = true;
|
|
1671
|
+
hits.length = limit;
|
|
1672
|
+
}
|
|
1673
|
+
return { hits, truncated };
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
// src/slide-runtime/ops-text.ts
|
|
1677
|
+
function replaceText(pkg, args) {
|
|
1678
|
+
if (args.find !== void 0) {
|
|
1679
|
+
if (args.slide !== void 0 || args.shape_id !== void 0 || args.text !== void 0) {
|
|
1680
|
+
throw new Error("slide_replace_text 的两种用法不能混用:整体替换给 {slide, shape_id, text},查找替换给 {find, replace, slides?}");
|
|
1681
|
+
}
|
|
1682
|
+
if (typeof args.replace !== "string") {
|
|
1683
|
+
throw new Error('查找替换需要 replace;要删除命中的文字请显式传 replace: ""');
|
|
1684
|
+
}
|
|
1685
|
+
return findReplace(pkg, args.find, stripXmlIllegal(args.replace), args.slides);
|
|
1686
|
+
}
|
|
1687
|
+
if (args.slide === void 0 || args.shape_id === void 0 || args.text === void 0) {
|
|
1688
|
+
throw new Error("slide_replace_text 需要 {slide, shape_id, text}(整体替换)或 {find, replace}(查找替换)");
|
|
1689
|
+
}
|
|
1690
|
+
const path = slidePath(pkg, args.slide);
|
|
1691
|
+
const shape = findShape(pkg.xml(path), args.shape_id, args.slide);
|
|
1692
|
+
if (shape.localName === "graphicFrame") {
|
|
1693
|
+
throw new Error(`shape_id=${args.shape_id} 是表格或图表,改单元格请用 slide_table_edit`);
|
|
1694
|
+
}
|
|
1695
|
+
if (shape.localName !== "sp") {
|
|
1696
|
+
throw new Error(`shape_id=${args.shape_id} 不是文本形状(${shape.localName})`);
|
|
1697
|
+
}
|
|
1698
|
+
const text = normalizeNewlines(args.text);
|
|
1699
|
+
setBodyText(ensureTxBody(shape), text);
|
|
1700
|
+
pkg.touch(path);
|
|
1701
|
+
return { mode: "set", slide: args.slide, shape_id: args.shape_id, paragraphs: text.split("\n").length };
|
|
1702
|
+
}
|
|
1703
|
+
function occurrences(s, sub) {
|
|
1704
|
+
return s.split(sub).length - 1;
|
|
1705
|
+
}
|
|
1706
|
+
function findReplace(pkg, find, replace, slides) {
|
|
1707
|
+
if (!find) throw new Error("find 不能为空");
|
|
1708
|
+
const targets = slides?.length ? slides : Array.from({ length: slideCount(pkg) }, (_, i) => i + 1);
|
|
1709
|
+
let replaced = 0;
|
|
1710
|
+
const unreplaced = [];
|
|
1711
|
+
for (const n of targets) {
|
|
1712
|
+
const path = slidePath(pkg, n);
|
|
1713
|
+
let touched = false;
|
|
1714
|
+
for (const { el: el2 } of allShapes(spTree(pkg.xml(path)))) {
|
|
1715
|
+
const bodies = el2.localName === "sp" ? [txBodyOf(el2)] : el2.localName === "graphicFrame" ? tableCells(el2).map(({ tc }) => childOf(tc, "txBody")) : [];
|
|
1716
|
+
for (const body of bodies) {
|
|
1717
|
+
if (!body) continue;
|
|
1718
|
+
for (const p of childrenOf(body, "p")) {
|
|
1719
|
+
const total = occurrences(paragraphText(p), find);
|
|
1720
|
+
if (!total) continue;
|
|
1721
|
+
let inRuns = 0;
|
|
1722
|
+
for (const r of childrenOf(p, "r")) {
|
|
1723
|
+
const t = childOf(r, "t");
|
|
1724
|
+
if (!t) continue;
|
|
1725
|
+
const s = textOf(t);
|
|
1726
|
+
const c = occurrences(s, find);
|
|
1727
|
+
if (!c) continue;
|
|
1728
|
+
setTextContent(t, s.split(find).join(replace));
|
|
1729
|
+
inRuns += c;
|
|
1730
|
+
touched = true;
|
|
1731
|
+
}
|
|
1732
|
+
replaced += inRuns;
|
|
1733
|
+
if (inRuns < total) unreplaced.push({ slide: n, shape_id: shapeIdOf(el2) ?? -1, text: paragraphText(p) });
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
if (touched) pkg.touch(path);
|
|
1738
|
+
}
|
|
1739
|
+
return { mode: "replace", replaced, unreplaced };
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
// src/slide-runtime/segments.ts
|
|
1743
|
+
function paragraphSegments(p) {
|
|
1744
|
+
let text = "";
|
|
1745
|
+
const segments = [];
|
|
1746
|
+
for (const child of elements(p)) {
|
|
1747
|
+
if (child.localName === "r" || child.localName === "fld") {
|
|
1748
|
+
const t = textOf(childOf(child, "t"));
|
|
1749
|
+
segments.push({ el: child, start: text.length, end: text.length + t.length, splittable: child.localName === "r" });
|
|
1750
|
+
text += t;
|
|
1751
|
+
} else if (child.localName === "br") {
|
|
1752
|
+
text += "\n";
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
return { text, segments };
|
|
1756
|
+
}
|
|
1757
|
+
function findMatches(text, match) {
|
|
1758
|
+
const out = [];
|
|
1759
|
+
if (!match) return out;
|
|
1760
|
+
let from = 0;
|
|
1761
|
+
for (; ; ) {
|
|
1762
|
+
const i = text.indexOf(match, from);
|
|
1763
|
+
if (i < 0) return out;
|
|
1764
|
+
out.push([i, i + match.length]);
|
|
1765
|
+
from = i + match.length;
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
function splitRun(run, at) {
|
|
1769
|
+
const t = childOf(run, "t");
|
|
1770
|
+
const s = textOf(t);
|
|
1771
|
+
const right = run.cloneNode(true);
|
|
1772
|
+
setTextContent(childOf(right, "t"), s.slice(at));
|
|
1773
|
+
setTextContent(t, s.slice(0, at));
|
|
1774
|
+
run.parentNode?.insertBefore(right, run.nextSibling);
|
|
1775
|
+
return right;
|
|
1776
|
+
}
|
|
1777
|
+
function isolate(p, start, end) {
|
|
1778
|
+
const out = [];
|
|
1779
|
+
for (const seg of paragraphSegments(p).segments) {
|
|
1780
|
+
if (seg.end <= start || seg.start >= end) continue;
|
|
1781
|
+
if (!seg.splittable) {
|
|
1782
|
+
out.push(seg.el);
|
|
1783
|
+
continue;
|
|
1784
|
+
}
|
|
1785
|
+
let target = seg.el;
|
|
1786
|
+
let from = seg.start;
|
|
1787
|
+
if (start > from) {
|
|
1788
|
+
target = splitRun(target, start - from);
|
|
1789
|
+
from = start;
|
|
1790
|
+
}
|
|
1791
|
+
if (end < seg.end) splitRun(target, end - from);
|
|
1792
|
+
out.push(target);
|
|
1793
|
+
}
|
|
1794
|
+
return out;
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
// src/slide-runtime/ops-format.ts
|
|
1798
|
+
var LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
1799
|
+
var URL_SCHEMES = /^(https?:\/\/|mailto:)/i;
|
|
1800
|
+
var SLIDE_JUMP = "ppaction://hlinksldjump";
|
|
1801
|
+
var isRun = (c) => c.localName === "r" || c.localName === "fld";
|
|
1802
|
+
var WRITE_TEXT_HINT = {
|
|
1803
|
+
shape: "先用 slide_replace_text 写入文字再设格式",
|
|
1804
|
+
cell: "先用 slide_table_edit 的 set_cells 写入文字再设格式"
|
|
1805
|
+
};
|
|
1806
|
+
function targetBodies(shape, args) {
|
|
1807
|
+
const id = args.shape_id;
|
|
1808
|
+
if (shape.localName === "sp") {
|
|
1809
|
+
if (args.cells !== void 0) throw new Error(`shape_id=${id} 不是表格,不能给 cells`);
|
|
1810
|
+
const body = txBodyOf(shape);
|
|
1811
|
+
if (!body) throw new Error(`shape_id=${id} 没有文字,无法设置格式;${WRITE_TEXT_HINT.shape}`);
|
|
1812
|
+
return [body];
|
|
1813
|
+
}
|
|
1814
|
+
const isTable = shape.localName === "graphicFrame" && Boolean(childOf(childOf(childOf(shape, "graphic"), "graphicData"), "tbl"));
|
|
1815
|
+
if (!isTable) throw new Error(`shape_id=${id} 不是文本形状或表格(${shape.localName})`);
|
|
1816
|
+
if (!Array.isArray(args.cells) || !args.cells.length) {
|
|
1817
|
+
throw new Error(`shape_id=${id} 是表格,需要 cells: [[行, 列], …](从 1 开始)`);
|
|
1818
|
+
}
|
|
1819
|
+
const cells = tableCells(shape);
|
|
1820
|
+
const bodies = args.cells.map((cell) => {
|
|
1821
|
+
const [row, col] = Array.isArray(cell) ? cell : [];
|
|
1822
|
+
const hit = cells.find((c) => c.row === row && c.col === col);
|
|
1823
|
+
if (!hit) throw new Error(`表格里没有第 ${row} 行第 ${col} 列(从 1 开始)`);
|
|
1824
|
+
if (boolAttr(hit.tc, "hMerge") || boolAttr(hit.tc, "vMerge")) {
|
|
1825
|
+
throw new Error(`第 ${row} 行第 ${col} 列是合并单元格的被合并部分,请改合并区域的左上角`);
|
|
1826
|
+
}
|
|
1827
|
+
const body = childOf(hit.tc, "txBody");
|
|
1828
|
+
if (!body) throw new Error(`第 ${row} 行第 ${col} 列没有文字,无法设置格式;${WRITE_TEXT_HINT.cell}`);
|
|
1829
|
+
return body;
|
|
1830
|
+
});
|
|
1831
|
+
return [...new Set(bodies)];
|
|
1832
|
+
}
|
|
1833
|
+
function selectedParagraphs(bodies, args) {
|
|
1834
|
+
return bodies.flatMap((body) => {
|
|
1835
|
+
const all = childrenOf(body, "p");
|
|
1836
|
+
if (args.paragraphs === void 0) return all;
|
|
1837
|
+
if (!Array.isArray(args.paragraphs) || !args.paragraphs.length) {
|
|
1838
|
+
throw new Error("paragraphs 要写成段落编号数组(从 1 开始)");
|
|
1839
|
+
}
|
|
1840
|
+
const picked = args.paragraphs.map((n) => {
|
|
1841
|
+
if (!Number.isInteger(n) || n < 1 || n > all.length) throw new Error(`第 ${n} 段不存在(共 ${all.length} 段,从 1 开始)`);
|
|
1842
|
+
return n;
|
|
1843
|
+
});
|
|
1844
|
+
return [...new Set(picked)].sort((a, b) => a - b).map((n) => all[n - 1]);
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
function parseLink(pkg, link) {
|
|
1848
|
+
if (link === null) return null;
|
|
1849
|
+
if (!link || typeof link !== "object" || Array.isArray(link)) throw new Error("link 要写成 { url } 或 { slide },清除写 null");
|
|
1850
|
+
const { url, slide } = link;
|
|
1851
|
+
if (url === void 0 === (slide === void 0)) throw new Error("link 只能给 url 或 slide 其中一个");
|
|
1852
|
+
if (url !== void 0) {
|
|
1853
|
+
if (typeof url !== "string" || !URL_SCHEMES.test(url) || /\s/.test(url) || stripXmlIllegal(url) !== url) {
|
|
1854
|
+
throw new Error("link.url 只接受 http:// https:// mailto: 开头、不含空白的地址");
|
|
1855
|
+
}
|
|
1856
|
+
return { url };
|
|
1857
|
+
}
|
|
1858
|
+
if (typeof slide !== "number") throw new Error("link.slide 要写成页码(数字,从 1 开始)");
|
|
1859
|
+
slidePath(pkg, slide);
|
|
1860
|
+
return { slide };
|
|
1861
|
+
}
|
|
1862
|
+
function assertSomethingToFormat(args, paragraphs, hits) {
|
|
1863
|
+
if (args.match === void 0) {
|
|
1864
|
+
if (!paragraphs.some((p) => elements(p).some(isRun))) {
|
|
1865
|
+
const hint = args.cells !== void 0 ? WRITE_TEXT_HINT.cell : WRITE_TEXT_HINT.shape;
|
|
1866
|
+
throw new Error(`目标里没有可设置格式的文字(#${args.shape_id});${hint}`);
|
|
1867
|
+
}
|
|
1868
|
+
return;
|
|
1869
|
+
}
|
|
1870
|
+
const covers = (h) => paragraphSegments(h.p).segments.some((s) => s.end > h.start && s.start < h.end);
|
|
1871
|
+
if (!hits.some(covers)) {
|
|
1872
|
+
throw new Error(`match 命中的只有换行,没有可设置格式的文字(#${args.shape_id});换一个包含文字的 match`);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
function formatText(pkg, args) {
|
|
1876
|
+
if (args.format === void 0 && args.align === void 0) throw new Error("format 和 align 至少给一个");
|
|
1877
|
+
const { link: rawLink, ...charFormat } = args.format ?? {};
|
|
1878
|
+
let link;
|
|
1879
|
+
if (args.format !== void 0) {
|
|
1880
|
+
validateCharFormat(args.format, ["link"]);
|
|
1881
|
+
if (rawLink !== void 0) link = parseLink(pkg, rawLink);
|
|
1882
|
+
}
|
|
1883
|
+
if (args.align !== void 0 && !isAlign(args.align)) throw new Error("align 只能是 left / center / right / justify");
|
|
1884
|
+
if (args.match !== void 0 && (typeof args.match !== "string" || !args.match || LONE_SURROGATE.test(args.match))) {
|
|
1885
|
+
throw new Error("match 要写成非空的文字");
|
|
1886
|
+
}
|
|
1887
|
+
if (args.occurrence !== void 0 && (args.match === void 0 || !Number.isInteger(args.occurrence) || args.occurrence < 1)) {
|
|
1888
|
+
throw new Error("occurrence 要和 match 一起给,取值从 1 开始");
|
|
1889
|
+
}
|
|
1890
|
+
const path = slidePath(pkg, args.slide);
|
|
1891
|
+
const shape = findShape(pkg.xml(path), args.shape_id, args.slide);
|
|
1892
|
+
const paragraphs = selectedParagraphs(targetBodies(shape, args), args);
|
|
1893
|
+
let hits = [];
|
|
1894
|
+
if (args.match !== void 0) {
|
|
1895
|
+
const match = args.match;
|
|
1896
|
+
for (const p of paragraphs) {
|
|
1897
|
+
for (const [start, end] of findMatches(paragraphSegments(p).text, match)) hits.push({ p, start, end });
|
|
1898
|
+
}
|
|
1899
|
+
if (!hits.length) throw new Error(`在 #${args.shape_id} 里没找到「${match}」(区分大小写),可先 slide_read 看原文`);
|
|
1900
|
+
if (args.occurrence !== void 0) {
|
|
1901
|
+
if (args.occurrence > hits.length) {
|
|
1902
|
+
throw new Error(`#${args.shape_id} 里只有 ${hits.length} 处「${match}」,occurrence=${args.occurrence} 越界`);
|
|
1903
|
+
}
|
|
1904
|
+
hits = [hits[args.occurrence - 1]];
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
if (args.format !== void 0) assertSomethingToFormat(args, paragraphs, hits);
|
|
1908
|
+
let linkRef;
|
|
1909
|
+
if (link === null) linkRef = null;
|
|
1910
|
+
else if (link && "url" in link) linkRef = { rId: pkg.addExternalRel(path, REL.hyperlink, link.url) };
|
|
1911
|
+
else if (link) {
|
|
1912
|
+
const target = slidePath(pkg, link.slide);
|
|
1913
|
+
linkRef = { rId: pkg.findRel(path, REL.slide, target) ?? pkg.addRel(path, REL.slide, target), action: SLIDE_JUMP };
|
|
1914
|
+
}
|
|
1915
|
+
const runs = args.format === void 0 ? [] : args.match !== void 0 ? [...new Set(hits.flatMap((h) => isolate(h.p, h.start, h.end)))] : paragraphs.flatMap((p) => elements(p).filter(isRun));
|
|
1916
|
+
const touched = new Set(args.match !== void 0 ? hits.map((h) => h.p) : paragraphs);
|
|
1917
|
+
const replaced = /* @__PURE__ */ new Set();
|
|
1918
|
+
if (args.format !== void 0) {
|
|
1919
|
+
for (const run of runs) {
|
|
1920
|
+
const rPr = ensureRPr(run);
|
|
1921
|
+
applyCharFormat(rPr, charFormat);
|
|
1922
|
+
if (linkRef !== void 0) {
|
|
1923
|
+
const prev = setHyperlink(rPr, linkRef);
|
|
1924
|
+
if (prev && prev !== linkRef?.rId) replaced.add(prev);
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
if (args.match === void 0) {
|
|
1928
|
+
const endFormat = endMarkFormat(charFormat);
|
|
1929
|
+
for (const p of paragraphs) {
|
|
1930
|
+
const endMark = childOf(p, "endParaRPr");
|
|
1931
|
+
if (endMark) applyCharFormat(endMark, endFormat);
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
if (args.align !== void 0) for (const p of touched) setParagraphAlign(p, args.align);
|
|
1936
|
+
for (const id of replaced) pkg.removeRelIfUnused(path, id);
|
|
1937
|
+
pkg.touch(path);
|
|
1938
|
+
return { slide: args.slide, shape_id: args.shape_id, runs: runs.length, paragraphs: touched.size };
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
// src/slide-runtime/runtime.ts
|
|
1942
|
+
var TYPE_LABEL = { text: "文本框", image: "图片", shape: "形状" };
|
|
1943
|
+
var SlideRuntime = class {
|
|
1944
|
+
opts;
|
|
1945
|
+
stack = [];
|
|
1946
|
+
constructor(opts) {
|
|
1947
|
+
this.opts = opts;
|
|
1948
|
+
}
|
|
1949
|
+
get history() {
|
|
1950
|
+
return this.stack.map((s) => s.edit);
|
|
1951
|
+
}
|
|
1952
|
+
get canUndo() {
|
|
1953
|
+
return this.stack.length > 0;
|
|
1954
|
+
}
|
|
1955
|
+
resetHistory() {
|
|
1956
|
+
this.stack.length = 0;
|
|
1957
|
+
}
|
|
1958
|
+
open() {
|
|
1959
|
+
const bytes = this.opts.getBytes();
|
|
1960
|
+
if (!bytes?.length) throw new Error("还没有打开 pptx:先在页面上打开一份,或调 slide_import_pptx");
|
|
1961
|
+
return { bytes, pkg: PptxPackage.open(bytes, this.opts.xml) };
|
|
1962
|
+
}
|
|
1963
|
+
/** 一次原子写:抛错或没有变化都不 commit */
|
|
1964
|
+
write(tool, fn, summary) {
|
|
1965
|
+
const { bytes, pkg } = this.open();
|
|
1966
|
+
const result = fn(pkg);
|
|
1967
|
+
if (!pkg.isDirty) return { ...result, changed: false };
|
|
1968
|
+
const next = pkg.save();
|
|
1969
|
+
const edit = { tool, summary: summary(result), time: Date.now() };
|
|
1970
|
+
this.stack.push({ bytes, edit });
|
|
1971
|
+
const max = this.opts.maxHistory ?? 20;
|
|
1972
|
+
while (this.stack.length > max) this.stack.shift();
|
|
1973
|
+
this.opts.commit(next, edit);
|
|
1974
|
+
return { ...result, changed: true };
|
|
1975
|
+
}
|
|
1976
|
+
/* ------------------------------ 读 ------------------------------ */
|
|
1977
|
+
read(args = {}) {
|
|
1978
|
+
return readSlides(this.open().pkg, args);
|
|
1979
|
+
}
|
|
1980
|
+
find(args) {
|
|
1981
|
+
return findInSlides(this.open().pkg, args);
|
|
1982
|
+
}
|
|
1983
|
+
/* ------------------------------ 写 ------------------------------ */
|
|
1984
|
+
replaceText(args) {
|
|
1985
|
+
return this.write(
|
|
1986
|
+
"slide_replace_text",
|
|
1987
|
+
(pkg) => replaceText(pkg, args),
|
|
1988
|
+
(r) => r.mode === "set" ? `改写第 ${r.slide} 页 #${r.shape_id} 的文本` : `替换「${args.find}」为「${args.replace ?? ""}」${r.replaced} 处`
|
|
1989
|
+
);
|
|
1990
|
+
}
|
|
1991
|
+
formatText(args) {
|
|
1992
|
+
return this.write(
|
|
1993
|
+
"slide_format_text",
|
|
1994
|
+
(pkg) => formatText(pkg, args),
|
|
1995
|
+
(r) => args.format !== void 0 ? `设置第 ${r.slide} 页 #${r.shape_id} 的文字格式(${r.runs} 处)` : `设置第 ${r.slide} 页 #${r.shape_id} 的段落对齐`
|
|
1996
|
+
);
|
|
1997
|
+
}
|
|
1998
|
+
manageSlides(args) {
|
|
1999
|
+
return this.write("slide_manage_slides", (pkg) => manageSlides(pkg, args), (r) => {
|
|
2000
|
+
if (r.action === "move") return `第 ${r.from} 页移到第 ${r.to} 页`;
|
|
2001
|
+
if (r.action === "add") return `新增第 ${r.slide} 页(${r.layout})`;
|
|
2002
|
+
if (r.action === "duplicate") return `复制第 ${args.slide} 页为第 ${r.slide} 页`;
|
|
2003
|
+
return `删除第 ${r.slide} 页`;
|
|
2004
|
+
});
|
|
2005
|
+
}
|
|
2006
|
+
addElement(args) {
|
|
2007
|
+
return this.write(
|
|
2008
|
+
"slide_add_element",
|
|
2009
|
+
(pkg) => addElement(pkg, args),
|
|
2010
|
+
(r) => `第 ${r.slide} 页新增${TYPE_LABEL[args.type] ?? args.type} #${r.shape_id}`
|
|
2011
|
+
);
|
|
2012
|
+
}
|
|
2013
|
+
modifyShapes(args) {
|
|
2014
|
+
return this.write(
|
|
2015
|
+
"slide_modify_shapes",
|
|
2016
|
+
(pkg) => modifyShapes(pkg, args),
|
|
2017
|
+
(r) => `修改第 ${r.slide} 页 ${r.modified} 个形状${r.deleted ? `、删除 ${r.deleted} 个` : ""}`
|
|
2018
|
+
);
|
|
2019
|
+
}
|
|
2020
|
+
tableEdit(args) {
|
|
2021
|
+
return this.write(
|
|
2022
|
+
"slide_table_edit",
|
|
2023
|
+
(pkg) => tableEdit(pkg, args),
|
|
2024
|
+
(r) => `编辑第 ${r.slide} 页表格 #${r.shape_id}(${r.action})`
|
|
2025
|
+
);
|
|
2026
|
+
}
|
|
2027
|
+
setNotes(args) {
|
|
2028
|
+
return this.write("slide_set_notes", (pkg) => setNotes(pkg, args), (r) => `写第 ${r.slide} 页备注`);
|
|
2029
|
+
}
|
|
2030
|
+
/** 撤销最近一次 AI 编辑。宿主以 tool=slide_undo 收到 commit */
|
|
2031
|
+
undo() {
|
|
2032
|
+
const top = this.stack.pop();
|
|
2033
|
+
if (!top) return null;
|
|
2034
|
+
this.opts.commit(top.bytes, { tool: "slide_undo", summary: `撤销:${top.edit.summary}`, time: Date.now() });
|
|
2035
|
+
return top.edit;
|
|
2036
|
+
}
|
|
2037
|
+
/* ---------------------------- 导入导出 ---------------------------- */
|
|
2038
|
+
importPptx(args) {
|
|
2039
|
+
if (!args.base64) throw new Error("slide_import_pptx 需要 path(由 hub 读成字节)");
|
|
2040
|
+
const bytes = base64ToBytes(args.base64);
|
|
2041
|
+
const slides = slideCount(PptxPackage.open(bytes, this.opts.xml));
|
|
2042
|
+
this.stack.length = 0;
|
|
2043
|
+
this.opts.commit(bytes, { tool: "slide_import_pptx", summary: `导入演示文稿(${slides} 页)`, time: Date.now() });
|
|
2044
|
+
return { slides };
|
|
2045
|
+
}
|
|
2046
|
+
/** base64 给传输层,模型只看路径 —— 和 doc_export_docx 同一约定 */
|
|
2047
|
+
exportPptx(args = {}) {
|
|
2048
|
+
const { bytes } = this.open();
|
|
2049
|
+
return { filename: sanitizePptxFilename(args.filename) ?? "演示文稿.pptx", base64: bytesToBase64(bytes), bytes: bytes.length };
|
|
2050
|
+
}
|
|
2051
|
+
/* ---------------------------- 统一入口 ---------------------------- */
|
|
2052
|
+
async call(tool, args = {}) {
|
|
2053
|
+
switch (tool) {
|
|
2054
|
+
case "slide_read":
|
|
2055
|
+
return this.read(args);
|
|
2056
|
+
case "slide_find":
|
|
2057
|
+
return this.find(args);
|
|
2058
|
+
case "slide_replace_text":
|
|
2059
|
+
return this.replaceText(args);
|
|
2060
|
+
case "slide_format_text":
|
|
2061
|
+
return this.formatText(args);
|
|
2062
|
+
case "slide_manage_slides":
|
|
2063
|
+
return this.manageSlides(args);
|
|
2064
|
+
case "slide_add_element":
|
|
2065
|
+
return this.addElement(args);
|
|
2066
|
+
case "slide_modify_shapes":
|
|
2067
|
+
return this.modifyShapes(args);
|
|
2068
|
+
case "slide_table_edit":
|
|
2069
|
+
return this.tableEdit(args);
|
|
2070
|
+
case "slide_set_notes":
|
|
2071
|
+
return this.setNotes(args);
|
|
2072
|
+
case "slide_undo": {
|
|
2073
|
+
const edit = this.undo();
|
|
2074
|
+
if (!edit) throw new Error("没有可撤销的 AI 编辑");
|
|
2075
|
+
return { undone: { tool: edit.tool, summary: edit.summary } };
|
|
2076
|
+
}
|
|
2077
|
+
case "slide_import_pptx":
|
|
2078
|
+
return this.importPptx(args);
|
|
2079
|
+
case "slide_export_pptx":
|
|
2080
|
+
return this.exportPptx(args);
|
|
2081
|
+
default:
|
|
2082
|
+
throw new Error(`未知工具 ${tool}`);
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
};
|
|
2086
|
+
|
|
2087
|
+
export { PptxPackage, SlideRuntime, addElement, findInSlides, formatText, manageSlides, modifyShapes, readSlides, replaceText, setNotes, tableEdit };
|
|
2088
|
+
//# sourceMappingURL=chunk-PKIYRSCM.js.map
|
|
2089
|
+
//# sourceMappingURL=chunk-PKIYRSCM.js.map
|