gbpptx 0.6.0 → 0.6.2
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/LICENSE +21 -21
- package/README.md +253 -253
- package/package.json +48 -47
- package/src/index.js +29 -29
- package/src/kernel/model.js +651 -651
- package/src/kernel/parts.js +173 -173
- package/src/kernel/pptx.js +72 -72
- package/src/kernel/print.js +76 -76
- package/src/kernel/read.js +1018 -1018
- package/src/kernel/roundtrip.js +174 -174
- package/src/kernel/text.js +278 -278
- package/src/kernel/write.js +385 -385
- package/web/app/main.js +8 -8
- package/web/app/slides/arrange.js +101 -101
- package/web/app/slides/clipboard.js +93 -93
- package/web/app/slides/file.js +240 -240
- package/web/app/slides/find.js +201 -201
- package/web/app/slides/history.js +100 -100
- package/web/app/slides/keys.js +187 -187
- package/web/app/slides/menus.js +105 -105
- package/web/app/slides/objects.js +254 -254
- package/web/app/slides/render.js +183 -183
- package/web/app/slides/shared.js +85 -85
- package/web/app/slides/slides.js +103 -103
- package/web/app/slides/table.js +102 -102
- package/web/app/slides/text.js +418 -418
- package/web/app/slides/view.js +182 -182
- package/web/app/slides-app.js +354 -354
- package/web/app/ui/canvas.js +769 -769
- package/web/app/ui/color-panel.js +58 -58
- package/web/app/ui/dialogs.js +120 -120
- package/web/app/ui/file-io.js +116 -116
- package/web/app/ui/format-pane.js +427 -427
- package/web/app/ui/icons.js +241 -133
- package/web/app/ui/menu.js +77 -75
- package/web/app/ui/notify.js +45 -45
- package/web/app/ui/page-view.js +221 -221
- package/web/app/ui/popup.js +80 -80
- package/web/app/ui/present.js +292 -292
- package/web/app/ui/ribbon.js +855 -855
- package/web/app/ui/slide-pane.js +197 -197
- package/web/app/ui/statusbar.js +93 -93
- package/web/app/ui/text-styles.js +84 -84
- package/web/app/ui/titlebar.js +253 -250
- package/web/app/ui/transition.js +30 -30
- package/web/index.html +30 -29
- package/web/serve.mjs +26 -23
- package/web/styles/office.css +463 -463
- package/web/styles/slides.css +97 -97
- package/web/styles/tokens.css +154 -154
|
@@ -1,254 +1,254 @@
|
|
|
1
|
-
// [文件] gbpptx·slides·objects —— 对象属性与插入 —— 属性面板写入、形状/图片/表格插入与裁剪
|
|
2
|
-
|
|
3
|
-
// [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
|
|
4
|
-
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
-
|
|
6
|
-
import { clamp } from 'guabiutils';
|
|
7
|
-
import { contrastText, createImageObject, createLineObject, createShapeObject, createTableObject, createTextObject, setObjectFontSize, themeOf, topZ } from '../../../src/kernel/model.js';
|
|
8
|
-
import { notify } from '../ui/notify.js';
|
|
9
|
-
import { pickImageFile } from '../ui/file-io.js';
|
|
10
|
-
import { field, numberInput, openModal, promptText } from '../ui/dialogs.js';
|
|
11
|
-
import { el } from './shared.js';
|
|
12
|
-
function updateObject(id, patch) {
|
|
13
|
-
const obj = this.selectedById(id);
|
|
14
|
-
if (!obj) return;
|
|
15
|
-
this.pushHistory();
|
|
16
|
-
Object.assign(obj, patch);
|
|
17
|
-
this.notifyChange();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function updateProps(id, patch) {
|
|
21
|
-
const obj = this.selectedById(id);
|
|
22
|
-
if (!obj) return;
|
|
23
|
-
this.pushHistory();
|
|
24
|
-
if (patch.fontSize !== undefined && obj.type === 'text') setObjectFontSize(obj, Number(patch.fontSize));
|
|
25
|
-
else Object.assign(obj.props, patch);
|
|
26
|
-
this.notifyChange();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function setSelectedProp(key, value) {
|
|
30
|
-
const objs = this.selectedObjects();
|
|
31
|
-
if (!objs.length) return;
|
|
32
|
-
this.pushHistory();
|
|
33
|
-
for (const o of objs) {
|
|
34
|
-
if (!o.props) continue;
|
|
35
|
-
if (key === 'stroke') {
|
|
36
|
-
o.props.stroke = value;
|
|
37
|
-
o.props.strokeW = value ? Math.max(1, Number(o.props.strokeW) || 1) : 0;
|
|
38
|
-
} else if (key === 'strokeW') {
|
|
39
|
-
o.props.strokeW = Math.max(0, Number(value) || 0);
|
|
40
|
-
if (o.props.strokeW > 0 && !o.props.stroke) o.props.stroke = themeOf(this.snapshot.theme).text;
|
|
41
|
-
} else if (key === 'kind') {
|
|
42
|
-
if (o.type === 'shape') o.props.kind = value;
|
|
43
|
-
} else if (key === 'shadow') {
|
|
44
|
-
if (o.type !== 'line') o.props.shadow = !!value;
|
|
45
|
-
} else {
|
|
46
|
-
o.props[key] = value;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
this.notifyChange();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function setProp(key, value) { this.setSelectedProp(key, value); }
|
|
53
|
-
|
|
54
|
-
function applyShapeQuickStyle(style) {
|
|
55
|
-
const objs = this.selectedObjects();
|
|
56
|
-
if (!objs.length) return;
|
|
57
|
-
this.pushHistory();
|
|
58
|
-
for (const o of objs) {
|
|
59
|
-
if (o.type === 'shape') {
|
|
60
|
-
o.props.fill = style.fill;
|
|
61
|
-
o.props.stroke = style.stroke;
|
|
62
|
-
o.props.strokeW = style.stroke ? 1.5 : 0;
|
|
63
|
-
o.props.textColor = style.text;
|
|
64
|
-
o.props.shadow = false;
|
|
65
|
-
} else if (o.type === 'line') {
|
|
66
|
-
o.props.stroke = style.stroke || style.fill || style.text;
|
|
67
|
-
o.props.strokeW = 2;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
this.notifyChange();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function rotateSelected(delta) {
|
|
74
|
-
const objs = this.selectedObjects();
|
|
75
|
-
if (!objs.length) return;
|
|
76
|
-
this.pushHistory();
|
|
77
|
-
for (const o of objs) o.rot = (((Number(o.rot) || 0) + delta) % 360 + 360) % 360;
|
|
78
|
-
this.notifyChange();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function resetSelected() {
|
|
82
|
-
const objs = this.selectedObjects();
|
|
83
|
-
if (!objs.length) return;
|
|
84
|
-
this.pushHistory();
|
|
85
|
-
for (const o of objs) {
|
|
86
|
-
o.rot = 0;
|
|
87
|
-
if (o.type === 'text') { o.w = 360; o.h = 110; }
|
|
88
|
-
else if (o.type === 'shape') { o.w = 220; o.h = 140; }
|
|
89
|
-
else if (o.type === 'table') { o.w = 440; o.h = 200; }
|
|
90
|
-
else if (o.type === 'line') { o.w = Math.sign(o.w || 1) * 240; o.h = 0; }
|
|
91
|
-
}
|
|
92
|
-
this.notifyChange();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function insert(kind, variant) {
|
|
96
|
-
const size = this.size;
|
|
97
|
-
const slide = this.currentSlide();
|
|
98
|
-
if (!slide) return;
|
|
99
|
-
let obj = null;
|
|
100
|
-
let w = 360;
|
|
101
|
-
let h = 110;
|
|
102
|
-
if (kind === 'text') { w = 360; h = 110; }
|
|
103
|
-
else if (kind === 'shape') { w = 220; h = 140; }
|
|
104
|
-
else if (kind === 'line' || kind === 'arrow') { w = 240; h = 0; }
|
|
105
|
-
else if (kind === 'table') { w = 440; h = 200; }
|
|
106
|
-
else if (kind === 'image') { this.pickImage(); return; }
|
|
107
|
-
const x = Math.round((size.w - w) / 2);
|
|
108
|
-
const y = Math.round((size.h - h) / 2);
|
|
109
|
-
const z = topZ(slide.objects) + 1;
|
|
110
|
-
if (kind === 'text') obj = createTextObject(x, y, z);
|
|
111
|
-
else if (kind === 'shape') {
|
|
112
|
-
obj = createShapeObject(variant || 'rect', x, y, z);
|
|
113
|
-
const accent = themeOf(this.snapshot.theme).accent;
|
|
114
|
-
obj.props.fill = accent;
|
|
115
|
-
obj.props.stroke = '';
|
|
116
|
-
obj.props.strokeW = 0;
|
|
117
|
-
obj.props.textColor = contrastText(accent);
|
|
118
|
-
} else if (kind === 'line' || kind === 'arrow') {
|
|
119
|
-
obj = createLineObject(x, y, z, kind === 'arrow');
|
|
120
|
-
obj.props.stroke = themeOf(this.snapshot.theme).text;
|
|
121
|
-
}
|
|
122
|
-
else if (kind === 'table') obj = createTableObject(x, y, z);
|
|
123
|
-
if (!obj) return;
|
|
124
|
-
this.pushHistory();
|
|
125
|
-
slide.objects.push(obj);
|
|
126
|
-
this.selectedIds = [obj.id];
|
|
127
|
-
this.notifyChange();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function insertTable(rows, cols) {
|
|
131
|
-
const size = this.size;
|
|
132
|
-
const slide = this.currentSlide();
|
|
133
|
-
if (!slide) return;
|
|
134
|
-
const w = 440;
|
|
135
|
-
const h = 200;
|
|
136
|
-
const obj = createTableObject(Math.round((size.w - w) / 2), Math.round((size.h - h) / 2), topZ(slide.objects) + 1, rows, cols);
|
|
137
|
-
this.pushHistory();
|
|
138
|
-
slide.objects.push(obj);
|
|
139
|
-
this.selectedIds = [obj.id];
|
|
140
|
-
this.notifyChange();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async function pickImage() {
|
|
144
|
-
const file = await pickImageFile();
|
|
145
|
-
if (!file) return;
|
|
146
|
-
await this.insertImageFile(file);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async function insertImageFile(file, point = null) {
|
|
150
|
-
if (!file || !/^image\/(png|jpeg|gif)$/i.test(file.type)) {
|
|
151
|
-
notify.warning('只支持 PNG / JPEG / GIF 图片');
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const src = await new Promise((resolve, reject) => {
|
|
155
|
-
const reader = new FileReader();
|
|
156
|
-
reader.onload = () => resolve(String(reader.result));
|
|
157
|
-
reader.onerror = () => reject(reader.error);
|
|
158
|
-
reader.readAsDataURL(file);
|
|
159
|
-
}).catch(() => '');
|
|
160
|
-
if (!src) return;
|
|
161
|
-
const img = await new Promise((resolve) => {
|
|
162
|
-
const image = new Image();
|
|
163
|
-
image.onload = () => resolve(image);
|
|
164
|
-
image.onerror = () => resolve(null);
|
|
165
|
-
image.src = src;
|
|
166
|
-
});
|
|
167
|
-
if (!img) return;
|
|
168
|
-
const size = this.size;
|
|
169
|
-
const maxW = size.w * 0.62;
|
|
170
|
-
const maxH = size.h * 0.62;
|
|
171
|
-
const scale = Math.min(1, maxW / img.width, maxH / img.height);
|
|
172
|
-
const w = Math.max(24, Math.round(img.width * scale));
|
|
173
|
-
const h = Math.max(24, Math.round(img.height * scale));
|
|
174
|
-
const cx = point ? point.x : size.w / 2;
|
|
175
|
-
const cy = point ? point.y : size.h / 2;
|
|
176
|
-
const x = clamp(Math.round(cx - w / 2), 0, Math.max(0, size.w - w));
|
|
177
|
-
const y = clamp(Math.round(cy - h / 2), 0, Math.max(0, size.h - h));
|
|
178
|
-
const slide = this.currentSlide();
|
|
179
|
-
if (!slide) return;
|
|
180
|
-
const obj = createImageObject(src, x, y, w, h, topZ(slide.objects) + 1);
|
|
181
|
-
this.pushHistory();
|
|
182
|
-
slide.objects.push(obj);
|
|
183
|
-
this.selectedIds = [obj.id];
|
|
184
|
-
this.notifyChange();
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async function replaceSelectedImage() {
|
|
188
|
-
const obj = this.firstSelected();
|
|
189
|
-
if (!obj || obj.type !== 'image') return;
|
|
190
|
-
const file = await pickImageFile();
|
|
191
|
-
if (!file) return;
|
|
192
|
-
const src = await new Promise((resolve) => {
|
|
193
|
-
const reader = new FileReader();
|
|
194
|
-
reader.onload = () => resolve(String(reader.result));
|
|
195
|
-
reader.onerror = () => resolve('');
|
|
196
|
-
reader.readAsDataURL(file);
|
|
197
|
-
});
|
|
198
|
-
if (!src) return;
|
|
199
|
-
this.pushHistory();
|
|
200
|
-
obj.props.src = src;
|
|
201
|
-
obj.props.crop = { l: 0, t: 0, r: 0, b: 0 };
|
|
202
|
-
this.notifyChange();
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function resetSelectedImage() {
|
|
206
|
-
const obj = this.firstSelected();
|
|
207
|
-
if (!obj || obj.type !== 'image') return;
|
|
208
|
-
this.pushHistory();
|
|
209
|
-
obj.props.crop = { l: 0, t: 0, r: 0, b: 0 };
|
|
210
|
-
obj.props.shadow = false;
|
|
211
|
-
this.notifyChange();
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function cropSelected() {
|
|
215
|
-
const obj = this.firstSelected();
|
|
216
|
-
if (!obj || obj.type !== 'image') return;
|
|
217
|
-
const crop = obj.props.crop || { l: 0, t: 0, r: 0, b: 0 };
|
|
218
|
-
let inputs = null;
|
|
219
|
-
openModal({
|
|
220
|
-
title: '裁剪图片',
|
|
221
|
-
width: 420,
|
|
222
|
-
confirmText: '应用',
|
|
223
|
-
onConfirm: () => {
|
|
224
|
-
if (!inputs) return;
|
|
225
|
-
const v = (key) => clamp(Number(inputs[key].value) || 0, 0, 49);
|
|
226
|
-
this.pushHistory();
|
|
227
|
-
obj.props.crop = { l: v('l'), t: v('t'), r: v('r'), b: v('b') };
|
|
228
|
-
this.notifyChange();
|
|
229
|
-
},
|
|
230
|
-
build: (body) => {
|
|
231
|
-
inputs = {};
|
|
232
|
-
for (const [key, label] of [['l', '左'], ['t', '上'], ['r', '右'], ['b', '下']]) {
|
|
233
|
-
const input = numberInput(crop[key] || 0, { min: 0, max: 49 });
|
|
234
|
-
inputs[key] = input;
|
|
235
|
-
body.appendChild(field(`${label}侧裁剪 %`, input));
|
|
236
|
-
}
|
|
237
|
-
const hint = el('p', 'pp-muted', '按百分比裁掉图片四边;四边合计不超过 98%。');
|
|
238
|
-
body.appendChild(hint);
|
|
239
|
-
},
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function editAltText() {
|
|
244
|
-
const obj = this.firstSelected();
|
|
245
|
-
if (!obj || obj.type !== 'image') return;
|
|
246
|
-
promptText({ title: '替代文字', label: '描述图片内容', value: obj.props.alt || '' }).then((value) => {
|
|
247
|
-
if (value === null) return;
|
|
248
|
-
this.pushHistory();
|
|
249
|
-
obj.props.alt = value;
|
|
250
|
-
this.notifyChange();
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
export const objectsMethods = { updateObject, updateProps, setSelectedProp, setProp, applyShapeQuickStyle, rotateSelected, resetSelected, insert, insertTable, pickImage, insertImageFile, replaceSelectedImage, resetSelectedImage, cropSelected, editAltText };
|
|
1
|
+
// [文件] gbpptx·slides·objects —— 对象属性与插入 —— 属性面板写入、形状/图片/表格插入与裁剪
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
|
|
4
|
+
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
+
|
|
6
|
+
import { clamp } from 'guabiutils';
|
|
7
|
+
import { contrastText, createImageObject, createLineObject, createShapeObject, createTableObject, createTextObject, setObjectFontSize, themeOf, topZ } from '../../../src/kernel/model.js';
|
|
8
|
+
import { notify } from '../ui/notify.js';
|
|
9
|
+
import { pickImageFile } from '../ui/file-io.js';
|
|
10
|
+
import { field, numberInput, openModal, promptText } from '../ui/dialogs.js';
|
|
11
|
+
import { el } from './shared.js';
|
|
12
|
+
function updateObject(id, patch) {
|
|
13
|
+
const obj = this.selectedById(id);
|
|
14
|
+
if (!obj) return;
|
|
15
|
+
this.pushHistory();
|
|
16
|
+
Object.assign(obj, patch);
|
|
17
|
+
this.notifyChange();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function updateProps(id, patch) {
|
|
21
|
+
const obj = this.selectedById(id);
|
|
22
|
+
if (!obj) return;
|
|
23
|
+
this.pushHistory();
|
|
24
|
+
if (patch.fontSize !== undefined && obj.type === 'text') setObjectFontSize(obj, Number(patch.fontSize));
|
|
25
|
+
else Object.assign(obj.props, patch);
|
|
26
|
+
this.notifyChange();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function setSelectedProp(key, value) {
|
|
30
|
+
const objs = this.selectedObjects();
|
|
31
|
+
if (!objs.length) return;
|
|
32
|
+
this.pushHistory();
|
|
33
|
+
for (const o of objs) {
|
|
34
|
+
if (!o.props) continue;
|
|
35
|
+
if (key === 'stroke') {
|
|
36
|
+
o.props.stroke = value;
|
|
37
|
+
o.props.strokeW = value ? Math.max(1, Number(o.props.strokeW) || 1) : 0;
|
|
38
|
+
} else if (key === 'strokeW') {
|
|
39
|
+
o.props.strokeW = Math.max(0, Number(value) || 0);
|
|
40
|
+
if (o.props.strokeW > 0 && !o.props.stroke) o.props.stroke = themeOf(this.snapshot.theme).text;
|
|
41
|
+
} else if (key === 'kind') {
|
|
42
|
+
if (o.type === 'shape') o.props.kind = value;
|
|
43
|
+
} else if (key === 'shadow') {
|
|
44
|
+
if (o.type !== 'line') o.props.shadow = !!value;
|
|
45
|
+
} else {
|
|
46
|
+
o.props[key] = value;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
this.notifyChange();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function setProp(key, value) { this.setSelectedProp(key, value); }
|
|
53
|
+
|
|
54
|
+
function applyShapeQuickStyle(style) {
|
|
55
|
+
const objs = this.selectedObjects();
|
|
56
|
+
if (!objs.length) return;
|
|
57
|
+
this.pushHistory();
|
|
58
|
+
for (const o of objs) {
|
|
59
|
+
if (o.type === 'shape') {
|
|
60
|
+
o.props.fill = style.fill;
|
|
61
|
+
o.props.stroke = style.stroke;
|
|
62
|
+
o.props.strokeW = style.stroke ? 1.5 : 0;
|
|
63
|
+
o.props.textColor = style.text;
|
|
64
|
+
o.props.shadow = false;
|
|
65
|
+
} else if (o.type === 'line') {
|
|
66
|
+
o.props.stroke = style.stroke || style.fill || style.text;
|
|
67
|
+
o.props.strokeW = 2;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this.notifyChange();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function rotateSelected(delta) {
|
|
74
|
+
const objs = this.selectedObjects();
|
|
75
|
+
if (!objs.length) return;
|
|
76
|
+
this.pushHistory();
|
|
77
|
+
for (const o of objs) o.rot = (((Number(o.rot) || 0) + delta) % 360 + 360) % 360;
|
|
78
|
+
this.notifyChange();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function resetSelected() {
|
|
82
|
+
const objs = this.selectedObjects();
|
|
83
|
+
if (!objs.length) return;
|
|
84
|
+
this.pushHistory();
|
|
85
|
+
for (const o of objs) {
|
|
86
|
+
o.rot = 0;
|
|
87
|
+
if (o.type === 'text') { o.w = 360; o.h = 110; }
|
|
88
|
+
else if (o.type === 'shape') { o.w = 220; o.h = 140; }
|
|
89
|
+
else if (o.type === 'table') { o.w = 440; o.h = 200; }
|
|
90
|
+
else if (o.type === 'line') { o.w = Math.sign(o.w || 1) * 240; o.h = 0; }
|
|
91
|
+
}
|
|
92
|
+
this.notifyChange();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function insert(kind, variant) {
|
|
96
|
+
const size = this.size;
|
|
97
|
+
const slide = this.currentSlide();
|
|
98
|
+
if (!slide) return;
|
|
99
|
+
let obj = null;
|
|
100
|
+
let w = 360;
|
|
101
|
+
let h = 110;
|
|
102
|
+
if (kind === 'text') { w = 360; h = 110; }
|
|
103
|
+
else if (kind === 'shape') { w = 220; h = 140; }
|
|
104
|
+
else if (kind === 'line' || kind === 'arrow') { w = 240; h = 0; }
|
|
105
|
+
else if (kind === 'table') { w = 440; h = 200; }
|
|
106
|
+
else if (kind === 'image') { this.pickImage(); return; }
|
|
107
|
+
const x = Math.round((size.w - w) / 2);
|
|
108
|
+
const y = Math.round((size.h - h) / 2);
|
|
109
|
+
const z = topZ(slide.objects) + 1;
|
|
110
|
+
if (kind === 'text') obj = createTextObject(x, y, z);
|
|
111
|
+
else if (kind === 'shape') {
|
|
112
|
+
obj = createShapeObject(variant || 'rect', x, y, z);
|
|
113
|
+
const accent = themeOf(this.snapshot.theme).accent;
|
|
114
|
+
obj.props.fill = accent;
|
|
115
|
+
obj.props.stroke = '';
|
|
116
|
+
obj.props.strokeW = 0;
|
|
117
|
+
obj.props.textColor = contrastText(accent);
|
|
118
|
+
} else if (kind === 'line' || kind === 'arrow') {
|
|
119
|
+
obj = createLineObject(x, y, z, kind === 'arrow');
|
|
120
|
+
obj.props.stroke = themeOf(this.snapshot.theme).text;
|
|
121
|
+
}
|
|
122
|
+
else if (kind === 'table') obj = createTableObject(x, y, z);
|
|
123
|
+
if (!obj) return;
|
|
124
|
+
this.pushHistory();
|
|
125
|
+
slide.objects.push(obj);
|
|
126
|
+
this.selectedIds = [obj.id];
|
|
127
|
+
this.notifyChange();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function insertTable(rows, cols) {
|
|
131
|
+
const size = this.size;
|
|
132
|
+
const slide = this.currentSlide();
|
|
133
|
+
if (!slide) return;
|
|
134
|
+
const w = 440;
|
|
135
|
+
const h = 200;
|
|
136
|
+
const obj = createTableObject(Math.round((size.w - w) / 2), Math.round((size.h - h) / 2), topZ(slide.objects) + 1, rows, cols);
|
|
137
|
+
this.pushHistory();
|
|
138
|
+
slide.objects.push(obj);
|
|
139
|
+
this.selectedIds = [obj.id];
|
|
140
|
+
this.notifyChange();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function pickImage() {
|
|
144
|
+
const file = await pickImageFile();
|
|
145
|
+
if (!file) return;
|
|
146
|
+
await this.insertImageFile(file);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function insertImageFile(file, point = null) {
|
|
150
|
+
if (!file || !/^image\/(png|jpeg|gif)$/i.test(file.type)) {
|
|
151
|
+
notify.warning('只支持 PNG / JPEG / GIF 图片');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const src = await new Promise((resolve, reject) => {
|
|
155
|
+
const reader = new FileReader();
|
|
156
|
+
reader.onload = () => resolve(String(reader.result));
|
|
157
|
+
reader.onerror = () => reject(reader.error);
|
|
158
|
+
reader.readAsDataURL(file);
|
|
159
|
+
}).catch(() => '');
|
|
160
|
+
if (!src) return;
|
|
161
|
+
const img = await new Promise((resolve) => {
|
|
162
|
+
const image = new Image();
|
|
163
|
+
image.onload = () => resolve(image);
|
|
164
|
+
image.onerror = () => resolve(null);
|
|
165
|
+
image.src = src;
|
|
166
|
+
});
|
|
167
|
+
if (!img) return;
|
|
168
|
+
const size = this.size;
|
|
169
|
+
const maxW = size.w * 0.62;
|
|
170
|
+
const maxH = size.h * 0.62;
|
|
171
|
+
const scale = Math.min(1, maxW / img.width, maxH / img.height);
|
|
172
|
+
const w = Math.max(24, Math.round(img.width * scale));
|
|
173
|
+
const h = Math.max(24, Math.round(img.height * scale));
|
|
174
|
+
const cx = point ? point.x : size.w / 2;
|
|
175
|
+
const cy = point ? point.y : size.h / 2;
|
|
176
|
+
const x = clamp(Math.round(cx - w / 2), 0, Math.max(0, size.w - w));
|
|
177
|
+
const y = clamp(Math.round(cy - h / 2), 0, Math.max(0, size.h - h));
|
|
178
|
+
const slide = this.currentSlide();
|
|
179
|
+
if (!slide) return;
|
|
180
|
+
const obj = createImageObject(src, x, y, w, h, topZ(slide.objects) + 1);
|
|
181
|
+
this.pushHistory();
|
|
182
|
+
slide.objects.push(obj);
|
|
183
|
+
this.selectedIds = [obj.id];
|
|
184
|
+
this.notifyChange();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function replaceSelectedImage() {
|
|
188
|
+
const obj = this.firstSelected();
|
|
189
|
+
if (!obj || obj.type !== 'image') return;
|
|
190
|
+
const file = await pickImageFile();
|
|
191
|
+
if (!file) return;
|
|
192
|
+
const src = await new Promise((resolve) => {
|
|
193
|
+
const reader = new FileReader();
|
|
194
|
+
reader.onload = () => resolve(String(reader.result));
|
|
195
|
+
reader.onerror = () => resolve('');
|
|
196
|
+
reader.readAsDataURL(file);
|
|
197
|
+
});
|
|
198
|
+
if (!src) return;
|
|
199
|
+
this.pushHistory();
|
|
200
|
+
obj.props.src = src;
|
|
201
|
+
obj.props.crop = { l: 0, t: 0, r: 0, b: 0 };
|
|
202
|
+
this.notifyChange();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function resetSelectedImage() {
|
|
206
|
+
const obj = this.firstSelected();
|
|
207
|
+
if (!obj || obj.type !== 'image') return;
|
|
208
|
+
this.pushHistory();
|
|
209
|
+
obj.props.crop = { l: 0, t: 0, r: 0, b: 0 };
|
|
210
|
+
obj.props.shadow = false;
|
|
211
|
+
this.notifyChange();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function cropSelected() {
|
|
215
|
+
const obj = this.firstSelected();
|
|
216
|
+
if (!obj || obj.type !== 'image') return;
|
|
217
|
+
const crop = obj.props.crop || { l: 0, t: 0, r: 0, b: 0 };
|
|
218
|
+
let inputs = null;
|
|
219
|
+
openModal({
|
|
220
|
+
title: '裁剪图片',
|
|
221
|
+
width: 420,
|
|
222
|
+
confirmText: '应用',
|
|
223
|
+
onConfirm: () => {
|
|
224
|
+
if (!inputs) return;
|
|
225
|
+
const v = (key) => clamp(Number(inputs[key].value) || 0, 0, 49);
|
|
226
|
+
this.pushHistory();
|
|
227
|
+
obj.props.crop = { l: v('l'), t: v('t'), r: v('r'), b: v('b') };
|
|
228
|
+
this.notifyChange();
|
|
229
|
+
},
|
|
230
|
+
build: (body) => {
|
|
231
|
+
inputs = {};
|
|
232
|
+
for (const [key, label] of [['l', '左'], ['t', '上'], ['r', '右'], ['b', '下']]) {
|
|
233
|
+
const input = numberInput(crop[key] || 0, { min: 0, max: 49 });
|
|
234
|
+
inputs[key] = input;
|
|
235
|
+
body.appendChild(field(`${label}侧裁剪 %`, input));
|
|
236
|
+
}
|
|
237
|
+
const hint = el('p', 'pp-muted', '按百分比裁掉图片四边;四边合计不超过 98%。');
|
|
238
|
+
body.appendChild(hint);
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function editAltText() {
|
|
244
|
+
const obj = this.firstSelected();
|
|
245
|
+
if (!obj || obj.type !== 'image') return;
|
|
246
|
+
promptText({ title: '替代文字', label: '描述图片内容', value: obj.props.alt || '' }).then((value) => {
|
|
247
|
+
if (value === null) return;
|
|
248
|
+
this.pushHistory();
|
|
249
|
+
obj.props.alt = value;
|
|
250
|
+
this.notifyChange();
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export const objectsMethods = { updateObject, updateProps, setSelectedProp, setProp, applyShapeQuickStyle, rotateSelected, resetSelected, insert, insertTable, pickImage, insertImageFile, replaceSelectedImage, resetSelectedImage, cropSelected, editAltText };
|