gbpptx 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +220 -158
  2. package/package.json +15 -4
  3. package/src/index.js +16 -8
  4. package/src/kernel/model.js +397 -91
  5. package/src/kernel/parts.js +3 -3
  6. package/src/kernel/pptx.js +17 -12
  7. package/src/kernel/read.js +747 -157
  8. package/src/kernel/roundtrip.js +118 -18
  9. package/src/kernel/text.js +133 -28
  10. package/src/kernel/write.js +160 -49
  11. package/web/app/main.js +8 -8
  12. package/web/app/slides/arrange.js +101 -0
  13. package/web/app/slides/clipboard.js +93 -0
  14. package/web/app/slides/file.js +243 -0
  15. package/web/app/slides/find.js +199 -0
  16. package/web/app/slides/history.js +100 -0
  17. package/web/app/slides/keys.js +174 -0
  18. package/web/app/slides/menus.js +105 -0
  19. package/web/app/slides/objects.js +254 -0
  20. package/web/app/slides/render.js +183 -0
  21. package/web/app/slides/shared.js +85 -0
  22. package/web/app/slides/slides.js +103 -0
  23. package/web/app/slides/table.js +102 -0
  24. package/web/app/slides/text.js +418 -0
  25. package/web/app/slides/view.js +130 -0
  26. package/web/app/slides-app.js +285 -844
  27. package/web/app/ui/backstage.js +255 -0
  28. package/web/app/ui/canvas.js +762 -277
  29. package/web/app/ui/color-panel.js +58 -58
  30. package/web/app/ui/dialogs.js +120 -120
  31. package/web/app/ui/file-io.js +116 -89
  32. package/web/app/ui/format-pane.js +427 -0
  33. package/web/app/ui/icons.js +129 -0
  34. package/web/app/ui/menu.js +75 -105
  35. package/web/app/ui/notify.js +45 -45
  36. package/web/app/ui/page-view.js +221 -150
  37. package/web/app/ui/popup.js +80 -80
  38. package/web/app/ui/present.js +292 -100
  39. package/web/app/ui/ribbon.js +878 -0
  40. package/web/app/ui/slide-pane.js +189 -0
  41. package/web/app/ui/statusbar.js +93 -0
  42. package/web/app/ui/text-styles.js +84 -84
  43. package/web/app/ui/titlebar.js +208 -0
  44. package/web/app/ui/transition.js +30 -0
  45. package/web/index.html +23 -21
  46. package/web/serve.mjs +20 -67
  47. package/web/styles/office.css +642 -0
  48. package/web/styles/slides.css +210 -212
  49. package/web/styles/tokens.css +154 -155
  50. package/src/kernel/menus.js +0 -123
  51. package/src/kernel/util/escape.js +0 -21
  52. package/src/kernel/util/id.js +0 -13
  53. package/web/app/ui/inspector.js +0 -209
  54. package/web/app/ui/slides-bar.js +0 -63
  55. package/web/app/ui/toolbar.js +0 -200
@@ -0,0 +1,174 @@
1
+ // [文件] gbpptx·slides·keys —— 全局事件 —— 快捷键分发、粘贴与拖放
2
+
3
+ // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { isPptxFile } from '../ui/file-io.js';
7
+ function bindGlobal() {
8
+ this.onKeyBound = (e) => this.onKeyDown(e);
9
+ this.onSelectionChangeBound = () => this.onSelectionChange();
10
+ this.onBeforeUnloadBound = (e) => {
11
+ if (!this.dirty) return;
12
+ e.preventDefault();
13
+ e.returnValue = '';
14
+ };
15
+ this.onDragOverBound = (e) => {
16
+ if ([...e.dataTransfer.types].includes('Files')) e.preventDefault();
17
+ };
18
+ this.onDropBound = (e) => this.onDrop(e);
19
+ this.onPasteBound = (e) => this.onPaste(e);
20
+ this.onResizeBound = () => {
21
+ if (this.view === 'reading') this.renderReading();
22
+ };
23
+
24
+ document.addEventListener('keydown', this.onKeyBound);
25
+ document.addEventListener('selectionchange', this.onSelectionChangeBound);
26
+ document.addEventListener('paste', this.onPasteBound);
27
+ window.addEventListener('beforeunload', this.onBeforeUnloadBound);
28
+ window.addEventListener('dragover', this.onDragOverBound);
29
+ window.addEventListener('drop', this.onDropBound);
30
+ window.addEventListener('resize', this.onResizeBound);
31
+ }
32
+
33
+ function isTypingTarget(target) {
34
+ return target instanceof Element && (target.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName));
35
+ }
36
+
37
+ function onKeyDown(e) {
38
+ if (this.presenter?.el) return;
39
+ if (this.backstage?.el && !this.backstage.el.hidden) return;
40
+ const target = e.target;
41
+ const typing = this.isTypingTarget(target);
42
+ const mod = e.ctrlKey || e.metaKey;
43
+ const k = e.key.toLowerCase();
44
+
45
+ if (mod && !e.altKey) {
46
+ if (k === 's') { e.preventDefault(); e.shiftKey ? this.saveAs(this.doc.name) : this.save(); return; }
47
+ if (k === 'o') { e.preventDefault(); this.openFile(); return; }
48
+ if (k === 'n') { e.preventDefault(); this.newDoc(); return; }
49
+ if (k === 'f') { e.preventDefault(); this.commitEditing({ render: false }); this.openFind(false); return; }
50
+ if (k === 'h') { e.preventDefault(); this.commitEditing({ render: false }); this.openFind(true); return; }
51
+ if (this.editing) {
52
+ const styleKey = { b: 'bold', i: 'italic', u: 'underline' }[k];
53
+ if (styleKey) { e.preventDefault(); this.toggleTextStyle(styleKey); return; }
54
+ const alignKey = { l: 'left', e: 'center', r: 'right', j: 'justify' }[k];
55
+ if (alignKey) { e.preventDefault(); this.applyAlign(alignKey); return; }
56
+ }
57
+ if (typing) return;
58
+ if (k === 'z' && e.shiftKey) { e.preventDefault(); this.redo(); return; }
59
+ if (k === 'z') { e.preventDefault(); this.undo(); return; }
60
+ if (k === 'y') { e.preventDefault(); this.redo(); return; }
61
+ if (k === 'm') { e.preventDefault(); this.addSlide(); return; }
62
+ if (k === 'd') { e.preventDefault(); this.selectedIds.length ? this.duplicateSelected() : this.dupSlide(); return; }
63
+ if (k === 'a') { e.preventDefault(); this.selectAll(); return; }
64
+ if (k === 'c') { e.preventDefault(); this.copySelected(); return; }
65
+ if (k === 'x') { e.preventDefault(); this.cutSelected(); return; }
66
+ if (k === 'v') {
67
+ // 有内部对象剪贴板才拦截;否则交给 paste 事件处理系统文字/图片
68
+ if (this.canPaste()) { e.preventDefault(); this.paste(); }
69
+ return;
70
+ }
71
+ const alignKey = { l: 'left', e: 'center', r: 'right', j: 'justify' }[k];
72
+ if (alignKey) { e.preventDefault(); this.applyAlign(alignKey); return; }
73
+ if (k === '=' || k === '+') { e.preventDefault(); this.setZoom(this.zoom + 10); return; }
74
+ if (k === '-') { e.preventDefault(); this.setZoom(this.zoom - 10); return; }
75
+ if (k === '0') { e.preventDefault(); this.setZoom(100); return; }
76
+ if (k === 'b' && !typing) { e.preventDefault(); this.toggleTextStyle('bold'); return; }
77
+ if (k === 'i' && !typing) { e.preventDefault(); this.toggleTextStyle('italic'); return; }
78
+ if (k === 'u' && !typing) { e.preventDefault(); this.toggleTextStyle('underline'); return; }
79
+ }
80
+
81
+ if (typing) {
82
+ if (e.key === 'Escape' && this.findEl && !this.findEl.hidden) this.closeFind();
83
+ return;
84
+ }
85
+
86
+ if (e.key === 'F5') { e.preventDefault(); e.shiftKey ? this.presentFromCurrent() : this.presentFromStart(); return; }
87
+ if (e.key === 'Escape') {
88
+ if (this.findEl && !this.findEl.hidden) { this.closeFind(); return; }
89
+ if (this.view !== 'normal') { this.setView('normal'); return; }
90
+ this.clearSelection();
91
+ return;
92
+ }
93
+ if (this.view === 'reading') {
94
+ if (e.key === 'ArrowRight' || e.key === 'PageDown' || e.key === ' ') { e.preventDefault(); this.selectSlide(this.index + 1); }
95
+ if (e.key === 'ArrowLeft' || e.key === 'PageUp') { e.preventDefault(); this.selectSlide(this.index - 1); }
96
+ return;
97
+ }
98
+ if (this.view === 'sorter' && ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
99
+ e.preventDefault();
100
+ const delta = e.key === 'ArrowRight' || e.key === 'ArrowDown' ? 1 : -1;
101
+ this.selectSlide(this.index + delta);
102
+ return;
103
+ }
104
+
105
+ if (e.key === 'Delete' || e.key === 'Backspace') {
106
+ if (this.selectedIds.length) { e.preventDefault(); this.removeSelected(); }
107
+ return;
108
+ }
109
+ if (e.key === 'F2' || e.key === 'Enter') {
110
+ const o = this.firstSelected();
111
+ if (o && (o.type === 'text' || o.type === 'shape')) {
112
+ e.preventDefault();
113
+ this.startEditingById(o.id);
114
+ }
115
+ return;
116
+ }
117
+ if (e.key === 'Tab') {
118
+ const objs = this.currentSlide()?.objects || [];
119
+ if (!objs.length) return;
120
+ e.preventDefault();
121
+ const current = this.selectedIds[0];
122
+ const at = objs.findIndex((o) => o.id === current);
123
+ const next = e.shiftKey ? (at - 1 + objs.length) % objs.length : (at + 1) % objs.length;
124
+ this.selectObjects([objs[next].id]);
125
+ return;
126
+ }
127
+ if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(e.key) && this.selectedIds.length) {
128
+ e.preventDefault();
129
+ const step = e.shiftKey ? 10 : 1;
130
+ const dx = e.key === 'ArrowLeft' ? -step : e.key === 'ArrowRight' ? step : 0;
131
+ const dy = e.key === 'ArrowUp' ? -step : e.key === 'ArrowDown' ? step : 0;
132
+ this.nudge(dx, dy);
133
+ }
134
+ }
135
+
136
+ function onPaste(e) {
137
+ const files = [...(e.clipboardData?.files || [])];
138
+ const items = [...(e.clipboardData?.items || [])];
139
+ const imageItem = items.find((i) => i.type.startsWith('image/'));
140
+ const image = files.find((f) => /^image\/(png|jpeg|gif)$/i.test(f.type)) || imageItem?.getAsFile();
141
+ if (image) {
142
+ // 图片一律作为独立对象插入(正文里不支持内联图片)
143
+ e.preventDefault();
144
+ this.insertImageFile(image);
145
+ return;
146
+ }
147
+ if (this.editing || this.isTypingTarget(e.target)) return;
148
+ const text = e.clipboardData?.getData('text/plain');
149
+ if (text && text.trim()) {
150
+ e.preventDefault();
151
+ this.pasteText(text);
152
+ }
153
+ }
154
+
155
+ async function onDrop(e) {
156
+ const files = [...(e.dataTransfer?.files || [])];
157
+ if (!files.length) return;
158
+ e.preventDefault();
159
+ const pptx = files.find((f) => isPptxFile(f));
160
+ if (pptx) {
161
+ await this.importFile(pptx);
162
+ return;
163
+ }
164
+ const images = files.filter((f) => /^image\/(png|jpeg|gif)$/i.test(f.type));
165
+ if (!images.length) return;
166
+ let point = null;
167
+ if (this.canvas.el.contains(e.target) || e.target === this.canvas.el) {
168
+ const rect = this.canvas.inner.getBoundingClientRect();
169
+ point = { x: (e.clientX - rect.left) / (this.zoom / 100), y: (e.clientY - rect.top) / (this.zoom / 100) };
170
+ }
171
+ for (const image of images) await this.insertImageFile(image, point);
172
+ }
173
+
174
+ export const keysMethods = { bindGlobal, isTypingTarget, onKeyDown, onPaste, onDrop };
@@ -0,0 +1,105 @@
1
+ // [文件] gbpptx·slides·menus —— 右键菜单 —— 画布与缩略图两处上下文菜单
2
+
3
+ // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { SLIDE_LAYOUTS, TRANSITIONS } from '../../../src/kernel/model.js';
7
+ import { openContextMenu } from '../ui/menu.js';
8
+ import { icon } from '../ui/icons.js';
9
+ function openCanvasMenu(x, y) {
10
+ const has = this.selectedObjects().length > 0;
11
+ const o = this.firstSelected();
12
+ const items = [];
13
+ if (!has) {
14
+ items.push(
15
+ { label: '粘贴', icon: 'paste', shortcut: 'Ctrl+V', disabled: !this.canPaste(), onClick: () => this.paste() },
16
+ { divided: true },
17
+ { label: '新建文本框', icon: 'textBox', onClick: () => this.insert('text') },
18
+ { label: '新建幻灯片', icon: 'newSlide', shortcut: 'Ctrl+M', onClick: () => this.addSlide() },
19
+ { divided: true },
20
+ { label: '设置背景格式…', icon: 'background', onClick: () => this.openFormatPane() },
21
+ { label: '网格线', checked: this.gridVisible, onClick: () => this.toggleGrid() },
22
+ { label: '参考线', checked: this.guidesVisible, onClick: () => this.toggleGuides() },
23
+ );
24
+ } else {
25
+ items.push(
26
+ { label: '剪切', icon: 'cut', shortcut: 'Ctrl+X', onClick: () => this.cutSelected() },
27
+ { label: '复制', icon: 'copy', shortcut: 'Ctrl+C', onClick: () => this.copySelected() },
28
+ { label: '粘贴', icon: 'paste', shortcut: 'Ctrl+V', disabled: !this.canPaste(), onClick: () => this.paste() },
29
+ { label: '复制对象', icon: 'duplicate', shortcut: 'Ctrl+D', onClick: () => this.duplicateSelected() },
30
+ { divided: true },
31
+ { label: '置于顶层', icon: 'bringForward', onClick: () => this.arrange('front') },
32
+ { label: '上移一层', onClick: () => this.arrange('forward') },
33
+ { label: '下移一层', onClick: () => this.arrange('backward') },
34
+ { label: '置于底层', icon: 'sendBackward', onClick: () => this.arrange('back') },
35
+ { divided: true },
36
+ {
37
+ label: '对齐页面',
38
+ icon: 'alignLeft',
39
+ children: [
40
+ { label: '左对齐', onClick: () => this.alignToSlide('left') },
41
+ { label: '水平居中', onClick: () => this.alignToSlide('centerH') },
42
+ { label: '右对齐', onClick: () => this.alignToSlide('right') },
43
+ { divided: true },
44
+ { label: '顶端对齐', onClick: () => this.alignToSlide('top') },
45
+ { label: '垂直居中', onClick: () => this.alignToSlide('middle') },
46
+ { label: '底端对齐', onClick: () => this.alignToSlide('bottom') },
47
+ ],
48
+ },
49
+ { divided: true },
50
+ );
51
+ if (o?.type === 'text' || o?.type === 'shape') {
52
+ items.push({ label: '编辑文字', icon: 'text', onClick: () => this.startEditingById(o.id) });
53
+ }
54
+ if (o?.type === 'image') {
55
+ items.push(
56
+ { label: '替换图片…', icon: 'replaceImage', onClick: () => this.replaceSelectedImage() },
57
+ { label: '裁剪图片…', icon: 'crop', onClick: () => this.cropSelected() },
58
+ );
59
+ }
60
+ if (o?.type === 'table') {
61
+ items.push(
62
+ { label: '在上方插入行', icon: 'rowAbove', onClick: () => this.insertTableRow('above') },
63
+ { label: '在下方插入行', icon: 'rowBelow', onClick: () => this.insertTableRow('below') },
64
+ { label: '在左侧插入列', icon: 'colLeft', onClick: () => this.insertTableColumn('left') },
65
+ { label: '在右侧插入列', icon: 'colRight', onClick: () => this.insertTableColumn('right') },
66
+ );
67
+ }
68
+ items.push(
69
+ { label: '设置形状格式…', icon: 'formatPane', onClick: () => this.openFormatPane() },
70
+ { divided: true },
71
+ { label: '删除对象', icon: 'delete', shortcut: 'Del', danger: true, onClick: () => this.removeSelected() },
72
+ );
73
+ }
74
+ openContextMenu({ x, y, items });
75
+ }
76
+
77
+ function openSlideMenu(x, y, index) {
78
+ const slide = this.snapshot.slides[index];
79
+ if (!slide) return;
80
+ openContextMenu({
81
+ x,
82
+ y,
83
+ items: [
84
+ { label: '新建幻灯片', icon: 'newSlide', onClick: () => this.addSlide() },
85
+ { label: '复制幻灯片', icon: 'duplicate', onClick: () => this.dupSlide() },
86
+ { label: '删除幻灯片', icon: 'delete', danger: true, disabled: this.snapshot.slides.length <= 1, onClick: () => this.delSlide() },
87
+ { divided: true },
88
+ { label: '隐藏幻灯片', icon: 'hide', checked: !!slide.hidden, onClick: () => this.toggleSlideHidden() },
89
+ {
90
+ label: '新建页(版式)',
91
+ icon: 'layout',
92
+ children: SLIDE_LAYOUTS.map((l) => ({ label: l.label, onClick: () => this.addSlide(l.key) })),
93
+ },
94
+ {
95
+ label: '切换',
96
+ icon: 'transition',
97
+ children: TRANSITIONS.map((t) => ({ label: t.label, onClick: () => this.setTransition({ type: t.key, dir: t.dir || 'l' }) })),
98
+ },
99
+ { divided: true },
100
+ { label: '设置背景格式…', icon: 'background', onClick: () => this.openFormatPane() },
101
+ ],
102
+ });
103
+ }
104
+
105
+ export const menusMethods = { openCanvasMenu, openSlideMenu };
@@ -0,0 +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-backstage__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 };