gbpptx 0.1.0 → 0.1.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 +158 -0
- package/package.json +2 -2
- package/src/kernel/roundtrip.js +7 -3
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# gbpptx
|
|
2
|
+
|
|
3
|
+
前端演示引擎:**画布对象模型 + 真正的 `.pptx`(PresentationML)读写 + 自包含的 HTML 幻灯片编辑器**。
|
|
4
|
+
|
|
5
|
+
它维护一个纯对象的演示文稿模型(幻灯片 → 若干对象 → 各自的 `props`),能把它写成 PowerPoint 能直接打开的 `.pptx`,也能把外部 `.pptx` 读回这个模型。底层 ZIP/XML 交给 [`gbooxml`](https://www.npmjs.com/package/gbooxml);渲染走 DOM + SVG,不使用 `<canvas>` 元素。
|
|
6
|
+
|
|
7
|
+
## 三层结构
|
|
8
|
+
|
|
9
|
+
| 层 | 入口 | 职责 |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| 模型 | `gbpptx/model` | 快照结构、五种对象的工厂、主题、版式、几何/文本工具 |
|
|
12
|
+
| 文件 | `gbpptx`(根入口) | `parsePptx` 读、`buildPptx` 写、`buildPptxVerified` 自检写 |
|
|
13
|
+
| 界面 | `web/` | `SlidesApp` 完整编辑器(画布、缩略图条、检查器、菜单、放映、打印) |
|
|
14
|
+
|
|
15
|
+
根入口把模型层与文件层的全部名字一起导出,`import { createDraft, buildPptx } from 'gbpptx'` 即可。
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install gbpptx
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 导入路径
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import { createDraft, parsePptx, buildPptx } from 'gbpptx'; // 根入口,39 个名字
|
|
27
|
+
import { createSlide } from 'gbpptx/model'; // 只要模型层
|
|
28
|
+
import { fontSizeToSz, isPptx } from 'gbpptx/pptx'; // PresentationML 常量与单位换算
|
|
29
|
+
import { htmlToParas } from 'gbpptx/kernel/text'; // 通配深层入口
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
通配规则是 `./*` → `./src/*.js`,所以深层路径 **不带 `.js` 后缀**。注意 `./pptx` 指向的是常量与换算模块(`src/kernel/pptx.js`),不是读文件入口 —— 读入口是根入口的 `parsePptx`。
|
|
33
|
+
|
|
34
|
+
## 快速开始
|
|
35
|
+
|
|
36
|
+
### 造一份演示文稿并写成 .pptx
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { createDraft, createTextObject, createShapeObject, createTableObject, buildPptxVerified } from 'gbpptx';
|
|
40
|
+
|
|
41
|
+
const pres = createDraft(); // { version: 1, title: '', theme: 'clean', slides: [一张空页] }
|
|
42
|
+
pres.title = '季度回顾';
|
|
43
|
+
pres.theme = 'midnight';
|
|
44
|
+
|
|
45
|
+
const slide = pres.slides[0];
|
|
46
|
+
const text = createTextObject(120, 100, 1); // x, y, z
|
|
47
|
+
text.props.html = '<div>标题文字</div>';
|
|
48
|
+
slide.objects.push(text);
|
|
49
|
+
|
|
50
|
+
const shape = createShapeObject('round', 120, 220, 2);
|
|
51
|
+
shape.props.text = '要点';
|
|
52
|
+
slide.objects.push(shape);
|
|
53
|
+
|
|
54
|
+
slide.objects.push(createTableObject(500, 120, 3));
|
|
55
|
+
|
|
56
|
+
const bytes = await buildPptxVerified(pres); // Promise<Uint8Array>,可直接落盘
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`buildPptxVerified` 写完会读回来比对内容摘要,任何不一致就抛 `保存自检未通过…` 并拒绝这次输出;只想强行生成请用 `buildPptx`。
|
|
60
|
+
|
|
61
|
+
### 读一个外部 .pptx
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { parsePptx, normalizeSnapshot, isPptx } from 'gbpptx';
|
|
65
|
+
|
|
66
|
+
if (isPptx(bytes)) { // 同步:查 ZIP 魔数,排除 OLE 老格式
|
|
67
|
+
const pres = normalizeSnapshot(await parsePptx(bytes)) || createDraft();
|
|
68
|
+
console.log(pres.slides.length, pres.slides[0].objects.map((o) => o.type));
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`parsePptx` 已经产出规范形状,`normalizeSnapshot` 是防御性再走一遍(把未知字段、越界类型清洗掉),来自外部数据时建议加上。读回来的 id 形如 `s-import1` / `o-import7`,不是原文件里的 id。
|
|
73
|
+
|
|
74
|
+
### 只用模型层做数据
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
import { createSlideWithLayout, SLIDE_LAYOUTS, THEMES, themeOf } from 'gbpptx';
|
|
78
|
+
|
|
79
|
+
SLIDE_LAYOUTS.map((l) => l.key); // title / titleOnly / titleContent / twoContent / caption
|
|
80
|
+
createSlideWithLayout('titleContent'); // 带占位对象的整页;未知 key 退成空白页
|
|
81
|
+
Object.keys(THEMES); // clean midnight ocean sunset forest paper
|
|
82
|
+
themeOf('ocean'); // { label, bg, text, accent }
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## 对象模型
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
{
|
|
89
|
+
version: 1, title: '', theme: 'clean',
|
|
90
|
+
slides: [{
|
|
91
|
+
id: 's-1', bg: '', // '' 表示跟随主题底色,否则 #rrggbb
|
|
92
|
+
notes: '',
|
|
93
|
+
objects: [{
|
|
94
|
+
id: 'o-1', type: 'text', // text | image | shape | line | table
|
|
95
|
+
x: 120, y: 100, w: 360, h: 110, rot: 0, z: 1,
|
|
96
|
+
props: { /* 按 type 不同,见下表 */ },
|
|
97
|
+
}],
|
|
98
|
+
}],
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`z` 是升序叠放层级(`topZ(objects)` 取当前最大值),`rot` 是角度制。`line` 的 `w`/`h` 是**从 `(x, y)` 出发的向量**,可以为负,用来表达方向。
|
|
103
|
+
|
|
104
|
+
| `type` | `props` 字段 |
|
|
105
|
+
| --- | --- |
|
|
106
|
+
| `text` | `html`、`font`、`fontSize`、`color`、`align`(`left`/`center`/`right`/`justify`)、`bold`、`italic`、`underline` |
|
|
107
|
+
| `image` | `src`(导出必须是 data URL) |
|
|
108
|
+
| `shape` | `kind`、`fill`、`stroke`、`strokeW`、`text`、`textColor`、`font`、`fontSize`、`bold` |
|
|
109
|
+
| `line` | `stroke`、`strokeW`、`dash`、`arrowEnd` |
|
|
110
|
+
| `table` | `rows`(二维字符串数组)、`headerRow`、`font`、`fontSize`、`color`、`colW`、`rowH`(百分比数组) |
|
|
111
|
+
|
|
112
|
+
八种形状 `kind`:`rect`、`round`、`ellipse`、`triangle`、`diamond`、`arrow`、`star`、`chevron`,由 `SHAPE_KINDS` 给出中文标签,`KIND_TO_PRST` / `PRST_TO_KIND` 负责与 OOXML 预设形状名互转。
|
|
113
|
+
|
|
114
|
+
工厂函数默认尺寸:文本 360×110(字号 24)、形状 220×140(底色 `#e07b1f`)、线条 240×0、表格 440×200(3×3,首行为表头)。
|
|
115
|
+
|
|
116
|
+
## 坐标系与单位
|
|
117
|
+
|
|
118
|
+
逻辑画布固定 **960 × 540 px**(`SLIDE_W` / `SLIDE_H`,16:9)。写出为 `sldSz cx=9144000 cy=5143500` EMU,即 10″ × 5.625″。
|
|
119
|
+
|
|
120
|
+
- `EMU_PER_PX = 9525`(来自 gbooxml);`pxToEmu` / `emuToPx` 做互转。
|
|
121
|
+
- 读入外部 `.pptx` 时按源文件 `sldSz` 等比缩放到 960×540,所以 4:3 或自定义尺寸的稿子进来后坐标会自动归一。
|
|
122
|
+
- 旋转:模型存角度制,OOXML 存 1/60000 度(写 `rot * 60000`,读 `/ 60000`)。
|
|
123
|
+
- 字号:模型用 px,OOXML 用百分之磅,`fontSizeToSz(px)` / `szToFontSize(sz)` 按 `× 0.75 × 100` 换算。
|
|
124
|
+
|
|
125
|
+
界面缩放靠 CSS `transform: scale(zoom/100)`,模型坐标不受影响。
|
|
126
|
+
|
|
127
|
+
## 自带编辑器
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
node web/serve.mjs # http://localhost:5274/web/
|
|
131
|
+
node web/serve.mjs 8080 # 或 GBPPTX_PORT=8080
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
零依赖静态服务器。`web/index.html` 用 importmap 把 `gbooxml` 映射到 `/vendor/gbooxml/index.js`,服务器把该前缀指向同级目录的 gbooxml 源码 —— 也就是说这个 demo 只在仓库的 monorepo 布局下能跑;从 npm 单独安装后要把 importmap 指到 `node_modules/gbooxml/src/index.js`。
|
|
135
|
+
|
|
136
|
+
入口是 `web/app/main.js`:`new SlidesApp(document.getElementById('app'))` → `app.start()`,并在控制台暴露 `window.gbpptx`。没有测试套件,`await window.gbpptx.selfTest()` 是最接近端到端测试的东西:它构造一份含文本/形状/线(负向量 + 虚线 + 箭头)/表格的快照,走完 `buildPptxVerified` → `parsePptx` 再核对。
|
|
137
|
+
|
|
138
|
+
## 环境说明
|
|
139
|
+
|
|
140
|
+
- **读(`parsePptx`)纯 Node 可用**:段落 → HTML 是纯字符串拼接。
|
|
141
|
+
- **写(`buildPptx`)在含有文本内容的对象时需要 DOM**:HTML → 段落序列走 `DOMParser`。实测形状/线/表格在裸 Node 能写出,`text` 对象只要 `props.html` 非空就抛 `DOMParser is not defined`;纯 Node 请挂 jsdom(做法同 `gbdocs`)。
|
|
142
|
+
- `web/` 与 `printSlides` 是浏览器专属(`document`、iframe `print`、`localStorage`);`buildMenus` 是纯数据、无 DOM。
|
|
143
|
+
- 渲染不使用 `<canvas>` 元素:画布是一个 DOM 舞台,形状走 SVG `path`(`shapePath(kind, w, h)`)。
|
|
144
|
+
|
|
145
|
+
## 已知坑
|
|
146
|
+
|
|
147
|
+
1. `props.html` 为空的文本对象写出去后**读不回来**(空文本按无内容处理),别用它存占位。
|
|
148
|
+
2. **轴向线**(`w` 或 `h` 恰为 0)落盘时宽高被钳到 1px —— `createLineObject` 的默认 `240×0` 就是这种线,存一次之后它会变成 `240×1`。
|
|
149
|
+
3. **没有文字的 shape 存不下字号**:写出不会产生 run,读回时字号落回默认 24,所以只填了 `fontSize` 不填 `text` 的形状,字号等于没写。
|
|
150
|
+
4. 自检摘要会**把几何取整到 0.1 px,并忽略文本颜色、`colW`、`rowH`**;上面第 2、3 条也是同口径白名单(写出端本就无法表达,不算内容丢失)。别把它当逐字节 diff 用。
|
|
151
|
+
5. 主题经过 PowerPoint 主题**名称**往返,带 `'Guabi '` 前缀;导出后再打开,自定义主题色以外的一致性依赖这个前缀约定。
|
|
152
|
+
6. 图片 `props.src` 导出必须是 data URL,远程 URL 不会被抓取(自检会因对象数变化直接拦住)。
|
|
153
|
+
7. 表格 `colW` / `rowH` 是百分比数组,不是像素。
|
|
154
|
+
8. 老二进制 `.ppt` 会被 `isPptx` 直接判假(它只认 ZIP 魔数并显式排除 OLE 容器)。
|
|
155
|
+
|
|
156
|
+
## 许可
|
|
157
|
+
|
|
158
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gbpptx",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Front-end presentation engine: canvas object model + real .pptx (PresentationML) read/write, plus a self-contained HTML slide editor UI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"exports": {
|
|
10
10
|
".": "./src/index.js",
|
|
11
11
|
"./model": "./src/kernel/model.js",
|
|
12
|
-
"./pptx": "./src/kernel/pptx
|
|
12
|
+
"./pptx": "./src/kernel/pptx.js",
|
|
13
13
|
"./package.json": "./package.json",
|
|
14
14
|
"./*": "./src/*.js"
|
|
15
15
|
},
|
package/src/kernel/roundtrip.js
CHANGED
|
@@ -8,14 +8,18 @@ const stripTags = (html) => String(html || '').replace(/<br\s*\/?>/gi, '\n').rep
|
|
|
8
8
|
|
|
9
9
|
const r1 = (v) => Math.round((Number(v) || 0) * 10) / 10;
|
|
10
10
|
|
|
11
|
+
// 写出端 xfrm 把 cx/cy 钳到最小 1px(轴向线 h/w=0 存成 1),摘要必须同口径,否则自检永远不过
|
|
12
|
+
const edge = (v) => r1(Math.max(1, Number(v) || 0));
|
|
13
|
+
|
|
11
14
|
/** 一个对象的比对摘要(类型白名单内取「能往返」的字段,不比对列宽/行高百分比这类派生值)
|
|
12
15
|
* [设计] 文字颜色不参与比对:模型里 color 为空表示「跟随主题」,写出端会落成主题具体色(PowerPoint 必须显式给色),
|
|
13
|
-
* 回读即得到该具体色——这是主题口径的正常物化,不是内容丢失,故不算自检失败。
|
|
16
|
+
* 回读即得到该具体色——这是主题口径的正常物化,不是内容丢失,故不算自检失败。
|
|
17
|
+
* 同理两处白名单:空文本形状不会生成 run,字号无处存放(回读落在默认 24);轴向线的零宽/高被 xfrm 钳成 1px。 */
|
|
14
18
|
function objectDigest(o) {
|
|
15
|
-
const base = { type: o.type, x: r1(o.x), y: r1(o.y), w:
|
|
19
|
+
const base = { type: o.type, x: r1(o.x), y: r1(o.y), w: edge(o.w), h: edge(o.h), rot: r1(o.rot) };
|
|
16
20
|
const p = o.props || {};
|
|
17
21
|
if (o.type === 'text') return { ...base, text: stripTags(p.html), size: r1(p.fontSize) };
|
|
18
|
-
if (o.type === 'shape') return { ...base, kind: p.kind, fill: (p.fill || '').toLowerCase(), text: String(p.text || '').trim(), size: r1(p.fontSize) };
|
|
22
|
+
if (o.type === 'shape') return { ...base, kind: p.kind, fill: (p.fill || '').toLowerCase(), text: String(p.text || '').trim(), size: String(p.text || '').trim() ? r1(p.fontSize) : 0 };
|
|
19
23
|
if (o.type === 'line') return { ...base, dash: !!p.dash, arrowEnd: !!p.arrowEnd, strokeW: r1(p.strokeW) };
|
|
20
24
|
if (o.type === 'image') return { ...base, src: String(p.src || '').slice(0, 40) };
|
|
21
25
|
if (o.type === 'table') return { ...base, rows: (p.rows || []).map((row) => row.map((c) => String(c || '').trim())) };
|