gbpptx 0.2.0 → 0.4.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 (46) hide show
  1. package/README.md +237 -214
  2. package/package.json +5 -3
  3. package/src/kernel/model.js +1 -2
  4. package/src/kernel/parts.js +11 -15
  5. package/src/kernel/print.js +14 -16
  6. package/web/app/main.js +8 -8
  7. package/web/app/slides/arrange.js +101 -0
  8. package/web/app/slides/clipboard.js +93 -0
  9. package/web/app/slides/file.js +213 -0
  10. package/web/app/slides/find.js +201 -0
  11. package/web/app/slides/history.js +100 -0
  12. package/web/app/slides/keys.js +179 -0
  13. package/web/app/slides/menus.js +105 -0
  14. package/web/app/slides/objects.js +254 -0
  15. package/web/app/slides/render.js +183 -0
  16. package/web/app/slides/shared.js +85 -0
  17. package/web/app/slides/slides.js +103 -0
  18. package/web/app/slides/table.js +102 -0
  19. package/web/app/slides/text.js +418 -0
  20. package/web/app/slides/view.js +156 -0
  21. package/web/app/slides-app.js +323 -2446
  22. package/web/app/ui/canvas.js +769 -755
  23. package/web/app/ui/color-panel.js +58 -76
  24. package/web/app/ui/dialogs.js +120 -159
  25. package/web/app/ui/file-io.js +116 -132
  26. package/web/app/ui/format-pane.js +427 -427
  27. package/web/app/ui/icons.js +129 -129
  28. package/web/app/ui/menu.js +75 -89
  29. package/web/app/ui/notify.js +45 -45
  30. package/web/app/ui/page-view.js +221 -221
  31. package/web/app/ui/popup.js +80 -80
  32. package/web/app/ui/present.js +292 -292
  33. package/web/app/ui/ribbon.js +868 -878
  34. package/web/app/ui/slide-pane.js +197 -189
  35. package/web/app/ui/statusbar.js +93 -93
  36. package/web/app/ui/text-styles.js +84 -84
  37. package/web/app/ui/titlebar.js +184 -67
  38. package/web/app/ui/transition.js +30 -30
  39. package/web/index.html +23 -21
  40. package/web/serve.mjs +22 -67
  41. package/web/styles/office.css +466 -644
  42. package/web/styles/slides.css +97 -219
  43. package/web/styles/tokens.css +154 -155
  44. package/src/kernel/util/escape.js +0 -21
  45. package/src/kernel/util/id.js +0 -13
  46. package/web/app/ui/backstage.js +0 -255
@@ -0,0 +1,100 @@
1
+ // [文件] gbpptx·slides·history —— 脏状态与撤销历史 —— 自动保存调度、快照栈、变更通知
2
+
3
+ // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { clamp } from 'guabiutils';
7
+ import { savePrefs, HISTORY_LIMIT, AUTOSAVE_MS } from './shared.js';
8
+ function setDirty(on) {
9
+ this.dirty = !!on;
10
+ this.titlebar.refresh();
11
+ }
12
+
13
+ function touch() {
14
+ this.revision += 1;
15
+ this.setDirty(true);
16
+ this.scheduleAutosave();
17
+ }
18
+
19
+ function markDirty() {
20
+ this.touch();
21
+ this.titlebar.refresh();
22
+ }
23
+
24
+ function persistPrefs() {
25
+ savePrefs(this.prefs);
26
+ }
27
+
28
+ function scheduleAutosave() {
29
+ clearTimeout(this.autosaveTimer);
30
+ if (!this.autosave || !this.doc.handle || !this.dirty) return;
31
+ this.autosaveTimer = setTimeout(() => {
32
+ if (this.dirty && this.doc.handle) this.save(true);
33
+ }, AUTOSAVE_MS);
34
+ }
35
+
36
+ function setAutosave(on) {
37
+ this.autosave = !!on;
38
+ this.prefs.autosave = this.autosave;
39
+ savePrefs(this.prefs);
40
+ this.scheduleAutosave();
41
+ this.titlebar.refresh();
42
+ }
43
+
44
+ function pushHistory() {
45
+ this.pushHistorySnapshot(JSON.stringify(this.snapshot));
46
+ }
47
+
48
+ function pushHistorySnapshot(snapshot) {
49
+ this.history.push(snapshot);
50
+ if (this.history.length > HISTORY_LIMIT) this.history.shift();
51
+ this.future = [];
52
+ }
53
+
54
+ function beginGesture() {
55
+ this.pushHistory();
56
+ }
57
+
58
+ function canUndo() { return this.history.length > 0; }
59
+
60
+ function canRedo() { return this.future.length > 0; }
61
+
62
+ function undo() {
63
+ this.commitEditing({ render: false });
64
+ if (!this.history.length) return;
65
+ this.future.push(JSON.stringify(this.snapshot));
66
+ this.snapshot = JSON.parse(this.history.pop());
67
+ this.index = clamp(this.index, 0, this.snapshot.slides.length - 1);
68
+ this.selectedIds = [];
69
+ this.touch();
70
+ this.renderAll();
71
+ }
72
+
73
+ function redo() {
74
+ this.commitEditing({ render: false });
75
+ if (!this.future.length) return;
76
+ this.history.push(JSON.stringify(this.snapshot));
77
+ this.snapshot = JSON.parse(this.future.pop());
78
+ this.index = clamp(this.index, 0, this.snapshot.slides.length - 1);
79
+ this.selectedIds = [];
80
+ this.touch();
81
+ this.renderAll();
82
+ }
83
+
84
+ function notifyChange(opts = {}) {
85
+ if (this.editing) this.commitEditing({ render: false });
86
+ this.touch();
87
+ this.statusbar.setStatus('');
88
+ if (opts.canvasOnly && this.view === 'normal') {
89
+ this.canvas.render();
90
+ this.slidePane.render();
91
+ this.formatPane.render();
92
+ this.ribbon.refresh();
93
+ this.titlebar.refresh();
94
+ this.statusbar.refresh();
95
+ return;
96
+ }
97
+ this.renderAll();
98
+ }
99
+
100
+ export const historyMethods = { setDirty, touch, markDirty, persistPrefs, scheduleAutosave, setAutosave, pushHistory, pushHistorySnapshot, beginGesture, canUndo, canRedo, undo, redo, notifyChange };
@@ -0,0 +1,179 @@
1
+ // [文件] gbpptx·slides·keys —— 全局事件 —— 快捷键分发、粘贴与拖放
2
+
3
+ // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { isReadOnlyViewKey } from 'guabiutils';
7
+ import { isPptxFile } from '../ui/file-io.js';
8
+ function bindGlobal() {
9
+ this.onKeyBound = (e) => this.onKeyDown(e);
10
+ this.onSelectionChangeBound = () => this.onSelectionChange();
11
+ this.onBeforeUnloadBound = (e) => {
12
+ if (!this.dirty) return;
13
+ e.preventDefault();
14
+ e.returnValue = '';
15
+ };
16
+ this.onDragOverBound = (e) => {
17
+ if ([...e.dataTransfer.types].includes('Files')) e.preventDefault();
18
+ };
19
+ this.onDropBound = (e) => this.onDrop(e);
20
+ this.onPasteBound = (e) => this.onPaste(e);
21
+ this.onResizeBound = () => {
22
+ if (this.view === 'reading') this.renderReading();
23
+ };
24
+
25
+ document.addEventListener('keydown', this.onKeyBound);
26
+ document.addEventListener('selectionchange', this.onSelectionChangeBound);
27
+ document.addEventListener('paste', this.onPasteBound);
28
+ window.addEventListener('beforeunload', this.onBeforeUnloadBound);
29
+ window.addEventListener('dragover', this.onDragOverBound);
30
+ window.addEventListener('drop', this.onDropBound);
31
+ window.addEventListener('resize', this.onResizeBound);
32
+ }
33
+
34
+ function isTypingTarget(target) {
35
+ return target instanceof Element && (target.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName));
36
+ }
37
+
38
+ function onKeyDown(e) {
39
+ if (this.presenter?.el) return;
40
+ // [设计] 事件落在应用外时 DOM 守卫接不到,全局快捷键入口按同一份白名单再挡一次
41
+ if (this.readOnly && !isReadOnlyViewKey(e)) return;
42
+ const target = e.target;
43
+ const typing = this.isTypingTarget(target);
44
+ const mod = e.ctrlKey || e.metaKey;
45
+ const k = e.key.toLowerCase();
46
+
47
+ if (mod && !e.altKey) {
48
+ if (k === 's') { e.preventDefault(); e.shiftKey ? this.saveAs(this.doc.name) : this.save(); return; }
49
+ if (k === 'o') { e.preventDefault(); this.openFile(); return; }
50
+ if (k === 'n') { e.preventDefault(); this.newDoc(); return; }
51
+ if (k === 'f') { e.preventDefault(); this.commitEditing({ render: false }); this.openFind(false); return; }
52
+ if (k === 'h') { e.preventDefault(); this.commitEditing({ render: false }); this.openFind(true); return; }
53
+ if (this.editing) {
54
+ const styleKey = { b: 'bold', i: 'italic', u: 'underline' }[k];
55
+ if (styleKey) { e.preventDefault(); this.toggleTextStyle(styleKey); return; }
56
+ const alignKey = { l: 'left', e: 'center', r: 'right', j: 'justify' }[k];
57
+ if (alignKey) { e.preventDefault(); this.applyAlign(alignKey); return; }
58
+ }
59
+ if (typing) return;
60
+ if (k === 'z' && e.shiftKey) { e.preventDefault(); this.redo(); return; }
61
+ if (k === 'z') { e.preventDefault(); this.undo(); return; }
62
+ if (k === 'y') { e.preventDefault(); this.redo(); return; }
63
+ if (k === 'm') { e.preventDefault(); this.addSlide(); return; }
64
+ if (k === 'd') { e.preventDefault(); this.selectedIds.length ? this.duplicateSelected() : this.dupSlide(); return; }
65
+ if (k === 'a') { e.preventDefault(); this.selectAll(); return; }
66
+ if (k === 'c') { e.preventDefault(); this.copySelected(); return; }
67
+ if (k === 'x') { e.preventDefault(); this.cutSelected(); return; }
68
+ if (k === 'v') {
69
+ // 有内部对象剪贴板才拦截;否则交给 paste 事件处理系统文字/图片
70
+ if (this.canPaste()) { e.preventDefault(); this.paste(); }
71
+ return;
72
+ }
73
+ const alignKey = { l: 'left', e: 'center', r: 'right', j: 'justify' }[k];
74
+ if (alignKey) { e.preventDefault(); this.applyAlign(alignKey); return; }
75
+ if (k === '=' || k === '+') { e.preventDefault(); this.setZoom(this.zoom + 10); return; }
76
+ if (k === '-') { e.preventDefault(); this.setZoom(this.zoom - 10); return; }
77
+ if (k === '0') { e.preventDefault(); this.setZoom(100); return; }
78
+ if (k === 'b' && !typing) { e.preventDefault(); this.toggleTextStyle('bold'); return; }
79
+ if (k === 'i' && !typing) { e.preventDefault(); this.toggleTextStyle('italic'); return; }
80
+ if (k === 'u' && !typing) { e.preventDefault(); this.toggleTextStyle('underline'); return; }
81
+ }
82
+
83
+ if (typing) {
84
+ if (e.key === 'Escape' && this.findEl && !this.findEl.hidden) this.closeFind();
85
+ return;
86
+ }
87
+
88
+ if (e.key === 'F5') { e.preventDefault(); e.shiftKey ? this.presentFromCurrent() : this.presentFromStart(); return; }
89
+ if (e.key === 'Escape') {
90
+ if (this.preview) { this.setPreview(false); return; }
91
+ if (this.findEl && !this.findEl.hidden) { this.closeFind(); return; }
92
+ if (this.view !== 'normal') { this.setView('normal'); return; }
93
+ this.clearSelection();
94
+ return;
95
+ }
96
+ if (this.view === 'reading') {
97
+ if (e.key === 'ArrowRight' || e.key === 'PageDown' || e.key === ' ') { e.preventDefault(); this.selectSlide(this.index + 1); }
98
+ if (e.key === 'ArrowLeft' || e.key === 'PageUp') { e.preventDefault(); this.selectSlide(this.index - 1); }
99
+ return;
100
+ }
101
+ if (this.view === 'sorter' && ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
102
+ e.preventDefault();
103
+ const delta = e.key === 'ArrowRight' || e.key === 'ArrowDown' ? 1 : -1;
104
+ this.selectSlide(this.index + delta);
105
+ return;
106
+ }
107
+
108
+ if (e.key === 'Delete' || e.key === 'Backspace') {
109
+ if (this.selectedIds.length) { e.preventDefault(); this.removeSelected(); }
110
+ return;
111
+ }
112
+ if (e.key === 'F2' || e.key === 'Enter') {
113
+ const o = this.firstSelected();
114
+ if (o && (o.type === 'text' || o.type === 'shape')) {
115
+ e.preventDefault();
116
+ this.startEditingById(o.id);
117
+ }
118
+ return;
119
+ }
120
+ if (e.key === 'Tab') {
121
+ const objs = this.currentSlide()?.objects || [];
122
+ if (!objs.length) return;
123
+ e.preventDefault();
124
+ const current = this.selectedIds[0];
125
+ const at = objs.findIndex((o) => o.id === current);
126
+ const next = e.shiftKey ? (at - 1 + objs.length) % objs.length : (at + 1) % objs.length;
127
+ this.selectObjects([objs[next].id]);
128
+ return;
129
+ }
130
+ if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(e.key) && this.selectedIds.length) {
131
+ e.preventDefault();
132
+ const step = e.shiftKey ? 10 : 1;
133
+ const dx = e.key === 'ArrowLeft' ? -step : e.key === 'ArrowRight' ? step : 0;
134
+ const dy = e.key === 'ArrowUp' ? -step : e.key === 'ArrowDown' ? step : 0;
135
+ this.nudge(dx, dy);
136
+ }
137
+ }
138
+
139
+ function onPaste(e) {
140
+ if (this.readOnly) return;
141
+ const files = [...(e.clipboardData?.files || [])];
142
+ const items = [...(e.clipboardData?.items || [])];
143
+ const imageItem = items.find((i) => i.type.startsWith('image/'));
144
+ const image = files.find((f) => /^image\/(png|jpeg|gif)$/i.test(f.type)) || imageItem?.getAsFile();
145
+ if (image) {
146
+ // 图片一律作为独立对象插入(正文里不支持内联图片)
147
+ e.preventDefault();
148
+ this.insertImageFile(image);
149
+ return;
150
+ }
151
+ if (this.editing || this.isTypingTarget(e.target)) return;
152
+ const text = e.clipboardData?.getData('text/plain');
153
+ if (text && text.trim()) {
154
+ e.preventDefault();
155
+ this.pasteText(text);
156
+ }
157
+ }
158
+
159
+ async function onDrop(e) {
160
+ if (this.readOnly) return;
161
+ const files = [...(e.dataTransfer?.files || [])];
162
+ if (!files.length) return;
163
+ e.preventDefault();
164
+ const pptx = files.find((f) => isPptxFile(f));
165
+ if (pptx) {
166
+ await this.importFile(pptx);
167
+ return;
168
+ }
169
+ const images = files.filter((f) => /^image\/(png|jpeg|gif)$/i.test(f.type));
170
+ if (!images.length) return;
171
+ let point = null;
172
+ if (this.canvas.el.contains(e.target) || e.target === this.canvas.el) {
173
+ const rect = this.canvas.inner.getBoundingClientRect();
174
+ point = { x: (e.clientX - rect.left) / (this.zoom / 100), y: (e.clientY - rect.top) / (this.zoom / 100) };
175
+ }
176
+ for (const image of images) await this.insertImageFile(image, point);
177
+ }
178
+
179
+ 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-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 };