gbpptx 0.2.0 → 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.
- package/README.md +220 -214
- package/package.json +4 -3
- package/src/kernel/model.js +1 -2
- package/src/kernel/parts.js +11 -15
- package/src/kernel/print.js +14 -16
- package/web/app/main.js +8 -8
- package/web/app/slides/arrange.js +101 -0
- package/web/app/slides/clipboard.js +93 -0
- package/web/app/slides/file.js +243 -0
- package/web/app/slides/find.js +199 -0
- package/web/app/slides/history.js +100 -0
- package/web/app/slides/keys.js +174 -0
- package/web/app/slides/menus.js +105 -0
- package/web/app/slides/objects.js +254 -0
- package/web/app/slides/render.js +183 -0
- package/web/app/slides/shared.js +85 -0
- package/web/app/slides/slides.js +103 -0
- package/web/app/slides/table.js +102 -0
- package/web/app/slides/text.js +418 -0
- package/web/app/slides/view.js +130 -0
- package/web/app/slides-app.js +285 -2446
- package/web/app/ui/backstage.js +255 -255
- package/web/app/ui/canvas.js +762 -755
- package/web/app/ui/color-panel.js +58 -76
- package/web/app/ui/dialogs.js +120 -159
- package/web/app/ui/file-io.js +116 -132
- package/web/app/ui/format-pane.js +427 -427
- package/web/app/ui/icons.js +129 -129
- package/web/app/ui/menu.js +75 -89
- 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 +878 -878
- package/web/app/ui/slide-pane.js +189 -189
- package/web/app/ui/statusbar.js +93 -93
- package/web/app/ui/text-styles.js +84 -84
- package/web/app/ui/titlebar.js +173 -66
- package/web/app/ui/transition.js +30 -30
- package/web/index.html +23 -22
- package/web/serve.mjs +20 -67
- package/web/styles/office.css +642 -644
- package/web/styles/slides.css +210 -219
- package/web/styles/tokens.css +154 -155
- package/src/kernel/util/escape.js +0 -21
- package/src/kernel/util/id.js +0 -13
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// [文件] gbpptx·slides·render —— 选择与渲染 —— 当前页/选区读取、整体重绘、缩略图与阅读视图
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
|
|
4
|
+
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
+
|
|
6
|
+
import { clamp } from 'guabiutils';
|
|
7
|
+
import { createSlide } from '../../../src/kernel/model.js';
|
|
8
|
+
import { renderPage } from '../ui/page-view.js';
|
|
9
|
+
import { icon } from '../ui/icons.js';
|
|
10
|
+
import { el } from './shared.js';
|
|
11
|
+
function currentSlide() {
|
|
12
|
+
return this.snapshot.slides[this.index] || this.snapshot.slides[0] || null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function selectedObjects() {
|
|
16
|
+
const slide = this.currentSlide();
|
|
17
|
+
if (!slide) return [];
|
|
18
|
+
return slide.objects.filter((o) => this.selectedIds.includes(o.id));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function firstSelected() {
|
|
22
|
+
return this.selectedObjects()[0] || null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function selectedById(id) {
|
|
26
|
+
return this.currentSlide()?.objects.find((o) => o.id === id) || null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function renderAll() {
|
|
30
|
+
if (!this.snapshot.slides.length) this.snapshot.slides.push(createSlide());
|
|
31
|
+
this.index = clamp(this.index, 0, this.snapshot.slides.length - 1);
|
|
32
|
+
this.updateLayout();
|
|
33
|
+
if (this.view === 'normal') {
|
|
34
|
+
this.canvas.render();
|
|
35
|
+
this.renderNotes();
|
|
36
|
+
} else if (this.view === 'sorter') {
|
|
37
|
+
this.renderSorter();
|
|
38
|
+
} else if (this.view === 'reading') {
|
|
39
|
+
this.renderReading();
|
|
40
|
+
}
|
|
41
|
+
this.slidePane.render();
|
|
42
|
+
this.formatPane.render();
|
|
43
|
+
this.ribbon.refresh();
|
|
44
|
+
this.titlebar.refresh();
|
|
45
|
+
this.statusbar.refresh();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function updateLayout() {
|
|
49
|
+
const normal = this.view === 'normal';
|
|
50
|
+
this.slidePane.el.hidden = !normal;
|
|
51
|
+
this.canvasHost.hidden = !normal;
|
|
52
|
+
this.canvas.el.hidden = !normal;
|
|
53
|
+
this.notesEl.hidden = !normal || !this.notesVisible;
|
|
54
|
+
this.sorterEl.hidden = this.view !== 'sorter';
|
|
55
|
+
this.readingEl.hidden = this.view !== 'reading';
|
|
56
|
+
this.root.dataset.view = this.view;
|
|
57
|
+
this.root.classList.toggle('has-pane', normal && this.formatPaneOpen);
|
|
58
|
+
this.root.classList.toggle('has-notes', normal && this.notesVisible);
|
|
59
|
+
if (!normal || !this.formatPaneOpen) this.formatPane.el.hidden = true;
|
|
60
|
+
else if (this.formatPaneOpen) this.formatPane.el.hidden = false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function renderNotes() {
|
|
64
|
+
const slide = this.currentSlide();
|
|
65
|
+
if (this.notesText && document.activeElement !== this.notesText) this.notesText.value = slide?.notes || '';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function renderSorter() {
|
|
69
|
+
const size = this.size;
|
|
70
|
+
const theme = this.snapshot.theme;
|
|
71
|
+
const width = 248;
|
|
72
|
+
this.sorterEl.replaceChildren();
|
|
73
|
+
this.snapshot.slides.forEach((slide, i) => {
|
|
74
|
+
const card = el('div', `pp-sorter-card${i === this.index ? ' gbp-is-active' : ''}${slide.hidden ? ' gbp-is-hidden' : ''}`);
|
|
75
|
+
card.dataset.index = String(i);
|
|
76
|
+
card.draggable = true;
|
|
77
|
+
const no = el('div', 'pp-sorter-card__no', String(i + 1));
|
|
78
|
+
const box = el('div', 'pp-sorter-card__box');
|
|
79
|
+
box.style.height = `${size.h * (width / size.w)}px`;
|
|
80
|
+
const page = renderPage(slide, theme, { size });
|
|
81
|
+
page.style.transform = `scale(${width / size.w})`;
|
|
82
|
+
page.style.transformOrigin = 'top left';
|
|
83
|
+
box.appendChild(page);
|
|
84
|
+
card.append(no, box);
|
|
85
|
+
if (slide.hidden) {
|
|
86
|
+
const flag = el('span', 'pp-sorter-card__flag');
|
|
87
|
+
flag.innerHTML = icon('hide', 14);
|
|
88
|
+
card.appendChild(flag);
|
|
89
|
+
}
|
|
90
|
+
this.sorterEl.appendChild(card);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function renderReading() {
|
|
95
|
+
const size = this.size;
|
|
96
|
+
const slide = this.currentSlide();
|
|
97
|
+
this.readingEl.replaceChildren();
|
|
98
|
+
if (!slide) return;
|
|
99
|
+
const availW = Math.max(320, (this.readingEl.clientWidth || window.innerWidth) - 120);
|
|
100
|
+
const availH = Math.max(240, (this.readingEl.clientHeight || window.innerHeight) - 130);
|
|
101
|
+
const scale = Math.min(availW / size.w, availH / size.h);
|
|
102
|
+
const stage = el('div', 'pp-reading__stage');
|
|
103
|
+
stage.style.width = `${size.w * scale}px`;
|
|
104
|
+
stage.style.height = `${size.h * scale}px`;
|
|
105
|
+
const page = renderPage(slide, this.snapshot.theme, { size });
|
|
106
|
+
page.style.transform = `scale(${scale})`;
|
|
107
|
+
page.style.transformOrigin = 'top left';
|
|
108
|
+
stage.appendChild(page);
|
|
109
|
+
const prev = el('button', 'pp-reading__nav pp-reading__nav--prev');
|
|
110
|
+
prev.type = 'button';
|
|
111
|
+
prev.title = '上一页';
|
|
112
|
+
prev.innerHTML = icon('chevronLeft', 24);
|
|
113
|
+
prev.disabled = this.index === 0;
|
|
114
|
+
prev.addEventListener('click', () => this.selectSlide(this.index - 1));
|
|
115
|
+
const next = el('button', 'pp-reading__nav pp-reading__nav--next');
|
|
116
|
+
next.type = 'button';
|
|
117
|
+
next.title = '下一页';
|
|
118
|
+
next.innerHTML = icon('chevronRight', 24);
|
|
119
|
+
next.disabled = this.index >= this.snapshot.slides.length - 1;
|
|
120
|
+
next.addEventListener('click', () => this.selectSlide(this.index + 1));
|
|
121
|
+
const bar = el('div', 'pp-reading__bar');
|
|
122
|
+
const counter = el('span', '', `${this.index + 1} / ${this.snapshot.slides.length}`);
|
|
123
|
+
const quit = el('button', 'pp-reading__quit', '关闭阅读视图 (Esc)');
|
|
124
|
+
quit.type = 'button';
|
|
125
|
+
quit.addEventListener('click', () => this.setView('normal'));
|
|
126
|
+
bar.append(counter, quit);
|
|
127
|
+
this.readingEl.append(prev, stage, next, bar);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function selectObjects(ids) {
|
|
131
|
+
const next = [...new Set(ids)].filter((id) => this.currentSlide()?.objects.some((o) => o.id === id));
|
|
132
|
+
if (this.editing && !next.includes(this.editing.id)) this.commitEditing({ render: false });
|
|
133
|
+
this.selectedIds = next;
|
|
134
|
+
this.canvas.syncSelection();
|
|
135
|
+
this.formatPane.render();
|
|
136
|
+
this.ribbon.refresh();
|
|
137
|
+
this.statusbar.refresh();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function toggleSelect(id) {
|
|
141
|
+
const set = new Set(this.selectedIds);
|
|
142
|
+
if (set.has(id)) set.delete(id);
|
|
143
|
+
else set.add(id);
|
|
144
|
+
this.selectObjects([...set]);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function clearSelection() {
|
|
148
|
+
if (this.editing) this.commitEditing({ render: false });
|
|
149
|
+
if (!this.selectedIds.length) return;
|
|
150
|
+
this.selectedIds = [];
|
|
151
|
+
this.canvas.syncSelection();
|
|
152
|
+
this.formatPane.render();
|
|
153
|
+
this.ribbon.refresh();
|
|
154
|
+
this.statusbar.refresh();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function selectAll() {
|
|
158
|
+
const slide = this.currentSlide();
|
|
159
|
+
if (!slide) return;
|
|
160
|
+
this.selectObjects(slide.objects.map((o) => o.id));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function selectSlide(i) {
|
|
164
|
+
this.commitEditing({ render: false });
|
|
165
|
+
const next = clamp(Number(i) || 0, 0, this.snapshot.slides.length - 1);
|
|
166
|
+
this.index = next;
|
|
167
|
+
this.selectedIds = [];
|
|
168
|
+
this.tableCursor = null;
|
|
169
|
+
this.notesSession = false;
|
|
170
|
+
this.renderAll();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function onSelectionChange() {
|
|
174
|
+
const e = this.editing;
|
|
175
|
+
if (!e?.node) return;
|
|
176
|
+
const sel = window.getSelection();
|
|
177
|
+
if (!sel?.rangeCount) return;
|
|
178
|
+
if (e.node.contains(sel.anchorNode) && e.node.contains(sel.focusNode)) {
|
|
179
|
+
e.range = sel.getRangeAt(0).cloneRange();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export const renderMethods = { currentSlide, selectedObjects, firstSelected, selectedById, renderAll, updateLayout, renderNotes, renderSorter, renderReading, selectObjects, toggleSelect, clearSelection, selectAll, selectSlide, onSelectionChange };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// [文件] gbpptx·slides·shared —— 编辑器常量与无状态工具(el / 偏好读写 / 行内样式读取 / 查找替换文本工具)
|
|
2
|
+
// [设计] 这些函数不碰 this,装配前后都是模块级函数;转义与颜色归一一律走 guabiutils,不重复实现。
|
|
3
|
+
|
|
4
|
+
import { escapeRegExp, normalizeColor } from 'guabiutils';
|
|
5
|
+
|
|
6
|
+
export const LS_KEY = 'gbpptx.office.prefs';
|
|
7
|
+
export const HISTORY_LIMIT = 100;
|
|
8
|
+
export const AUTOSAVE_MS = 2600;
|
|
9
|
+
export const ZOOM_MIN = 20;
|
|
10
|
+
export const ZOOM_MAX = 400;
|
|
11
|
+
|
|
12
|
+
export const el = (tag, cls, text) => {
|
|
13
|
+
const node = document.createElement(tag);
|
|
14
|
+
if (cls) node.className = cls;
|
|
15
|
+
if (text != null) node.textContent = text;
|
|
16
|
+
return node;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function loadPrefs() {
|
|
20
|
+
try { return JSON.parse(localStorage.getItem(LS_KEY) || '{}') || {}; } catch { return {}; }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function savePrefs(prefs) {
|
|
24
|
+
try { localStorage.setItem(LS_KEY, JSON.stringify(prefs)); } catch { /* 忽略存储失败 */ }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function styleFromNode(node, root) {
|
|
28
|
+
let bold = false;
|
|
29
|
+
let italic = false;
|
|
30
|
+
let underline = false;
|
|
31
|
+
let strike = false;
|
|
32
|
+
let font = '';
|
|
33
|
+
let size = 0;
|
|
34
|
+
let color = '';
|
|
35
|
+
let el = node;
|
|
36
|
+
while (el && el !== root.parentElement) {
|
|
37
|
+
const tag = el.tagName;
|
|
38
|
+
if (tag === 'B' || tag === 'STRONG') bold = true;
|
|
39
|
+
if (tag === 'I' || tag === 'EM') italic = true;
|
|
40
|
+
if (tag === 'U') underline = true;
|
|
41
|
+
if (tag === 'S' || tag === 'STRIKE' || tag === 'DEL') strike = true;
|
|
42
|
+
const cs = window.getComputedStyle(el);
|
|
43
|
+
if (cs.fontWeight === 'bold' || Number(cs.fontWeight) >= 600) bold = true;
|
|
44
|
+
if (cs.fontStyle === 'italic') italic = true;
|
|
45
|
+
if (cs.textDecorationLine?.includes('underline') || cs.textDecoration?.includes('underline')) underline = true;
|
|
46
|
+
if (cs.textDecorationLine?.includes('line-through') || cs.textDecoration?.includes('line-through')) strike = true;
|
|
47
|
+
if (!font) font = cs.fontFamily || '';
|
|
48
|
+
if (!size) size = parseFloat(cs.fontSize) || 0;
|
|
49
|
+
if (!color) color = normalizeColor(cs.color);
|
|
50
|
+
if (el === root) break;
|
|
51
|
+
el = el.parentElement;
|
|
52
|
+
}
|
|
53
|
+
return { bold, italic, underline, strike, font, fontSize: size, color };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function closestBlock(node, root) {
|
|
57
|
+
let el = node;
|
|
58
|
+
while (el && el !== root) {
|
|
59
|
+
if (['DIV', 'P', 'LI', 'H1', 'H2', 'H3', 'BLOCKQUOTE'].includes(el.tagName)) return el;
|
|
60
|
+
el = el.parentElement;
|
|
61
|
+
}
|
|
62
|
+
return root;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function closeAlign(block) {
|
|
66
|
+
const align = window.getComputedStyle(block).textAlign;
|
|
67
|
+
return align || 'left';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function replaceInHtml(html, query, replacement, all = false) {
|
|
71
|
+
if (!html || !query) return html;
|
|
72
|
+
const doc = new DOMParser().parseFromString(`<body>${html}</body>`, 'text/html');
|
|
73
|
+
const re = new RegExp(escapeRegExp(query), all ? 'gi' : 'i');
|
|
74
|
+
const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_TEXT);
|
|
75
|
+
const nodes = [];
|
|
76
|
+
let n = walker.nextNode();
|
|
77
|
+
while (n) { nodes.push(n); n = walker.nextNode(); }
|
|
78
|
+
for (const node of nodes) node.data = node.data.replace(re, () => replacement);
|
|
79
|
+
return doc.body.innerHTML;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function replaceInPlain(text, query, replacement, all = false) {
|
|
83
|
+
if (!text || !query) return text;
|
|
84
|
+
return String(text).replace(new RegExp(escapeRegExp(query), all ? 'gi' : 'i'), () => replacement);
|
|
85
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// [文件] gbpptx·slides·slides —— 页操作 —— 增删移动、切换动画、背景/主题/画幅
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
|
|
4
|
+
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
+
|
|
6
|
+
import { clamp } from 'guabiutils';
|
|
7
|
+
import { SLIDE_SIZES, THEMES, copySlide, createSlide, createSlideWithLayout, normalizeTransition, scaleSnapshotSize, transitionOf } from '../../../src/kernel/model.js';
|
|
8
|
+
import { notify } from '../ui/notify.js';
|
|
9
|
+
function addSlide(layoutKey) {
|
|
10
|
+
const slide = layoutKey ? createSlideWithLayout(layoutKey, this.size) : createSlide();
|
|
11
|
+
this.pushHistory();
|
|
12
|
+
this.snapshot.slides.splice(this.index + 1, 0, slide);
|
|
13
|
+
this.index += 1;
|
|
14
|
+
this.selectedIds = [];
|
|
15
|
+
this.notifyChange();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function dupSlide() {
|
|
19
|
+
this.pushHistory();
|
|
20
|
+
this.snapshot.slides.splice(this.index + 1, 0, copySlide(this.currentSlide()));
|
|
21
|
+
this.index += 1;
|
|
22
|
+
this.selectedIds = [];
|
|
23
|
+
this.notifyChange();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function delSlide() {
|
|
27
|
+
if (this.snapshot.slides.length <= 1) return notify.warning('至少要保留一页');
|
|
28
|
+
this.pushHistory();
|
|
29
|
+
this.snapshot.slides.splice(this.index, 1);
|
|
30
|
+
this.index = clamp(this.index, 0, this.snapshot.slides.length - 1);
|
|
31
|
+
this.selectedIds = [];
|
|
32
|
+
this.notifyChange();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function moveSlide(delta) {
|
|
36
|
+
this.moveSlideTo(this.index, this.index + delta);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function moveSlideTo(from, to) {
|
|
40
|
+
const length = this.snapshot.slides.length;
|
|
41
|
+
if (from < 0 || from >= length || to < 0 || to >= length || from === to) return;
|
|
42
|
+
this.pushHistory();
|
|
43
|
+
const [slide] = this.snapshot.slides.splice(from, 1);
|
|
44
|
+
this.snapshot.slides.splice(to, 0, slide);
|
|
45
|
+
this.index = to;
|
|
46
|
+
this.selectedIds = [];
|
|
47
|
+
this.notifyChange();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function toggleSlideHidden() {
|
|
51
|
+
const slide = this.currentSlide();
|
|
52
|
+
if (!slide) return;
|
|
53
|
+
this.pushHistory();
|
|
54
|
+
slide.hidden = !slide.hidden;
|
|
55
|
+
this.notifyChange();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function setTransition(patch) {
|
|
59
|
+
const slide = this.currentSlide();
|
|
60
|
+
if (!slide) return;
|
|
61
|
+
this.pushHistory();
|
|
62
|
+
slide.transition = normalizeTransition({ ...transitionOf(slide), ...patch });
|
|
63
|
+
this.notifyChange();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function applyTransitionAll() {
|
|
67
|
+
const current = transitionOf(this.currentSlide());
|
|
68
|
+
this.pushHistory();
|
|
69
|
+
for (const slide of this.snapshot.slides) slide.transition = { ...current };
|
|
70
|
+
this.notifyChange();
|
|
71
|
+
notify.success('已把当前切换应用到全部幻灯片');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function previewTransition() {
|
|
75
|
+
this.canvas.previewTransition(transitionOf(this.currentSlide()));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function setSlideBg(color) {
|
|
79
|
+
const slide = this.currentSlide();
|
|
80
|
+
if (!slide) return;
|
|
81
|
+
this.pushHistory();
|
|
82
|
+
slide.bg = color || '';
|
|
83
|
+
this.notifyChange();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function setTheme(key) {
|
|
87
|
+
if (!THEMES[key] || this.snapshot.theme === key) return;
|
|
88
|
+
this.pushHistory();
|
|
89
|
+
this.snapshot.theme = key;
|
|
90
|
+
this.notifyChange();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function setSlideSize(presetKey) {
|
|
94
|
+
const preset = SLIDE_SIZES.find((s) => s.key === presetKey);
|
|
95
|
+
if (!preset) return;
|
|
96
|
+
if (this.size.w === preset.w && this.size.h === preset.h) return;
|
|
97
|
+
this.pushHistory();
|
|
98
|
+
scaleSnapshotSize(this.snapshot, { w: preset.w, h: preset.h });
|
|
99
|
+
this.notifyChange();
|
|
100
|
+
this.fitToWindow();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const slidesMethods = { addSlide, dupSlide, delSlide, moveSlide, moveSlideTo, toggleSlideHidden, setTransition, applyTransitionAll, previewTransition, setSlideBg, setTheme, setSlideSize };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// [文件] gbpptx·slides·table —— 表格操作 —— 行列增删、表格样式、表头与隔行底色
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
|
|
4
|
+
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
+
|
|
6
|
+
import { clamp } from 'guabiutils';
|
|
7
|
+
import { notify } from '../ui/notify.js';
|
|
8
|
+
function resizeTable(id, rows, cols) {
|
|
9
|
+
const obj = this.selectedById(id);
|
|
10
|
+
if (!obj || obj.type !== 'table') return;
|
|
11
|
+
const r = clamp(Math.round(rows), 1, 50);
|
|
12
|
+
const c = clamp(Math.round(cols), 1, 30);
|
|
13
|
+
this.pushHistory();
|
|
14
|
+
const cur = obj.props.rows;
|
|
15
|
+
obj.props.rows = Array.from({ length: r }, (_, ri) => Array.from({ length: c }, (_, ci) => cur[ri]?.[ci] ?? ''));
|
|
16
|
+
obj.props.colW = [];
|
|
17
|
+
obj.props.rowH = [];
|
|
18
|
+
this.notifyChange();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function insertTableRow(where) {
|
|
22
|
+
const obj = this.firstSelected();
|
|
23
|
+
if (!obj || obj.type !== 'table') return;
|
|
24
|
+
const cur = this.tableCursor?.objId === obj.id ? this.tableCursor.ri : 0;
|
|
25
|
+
const at = where === 'above' ? cur : cur + 1;
|
|
26
|
+
this.pushHistory();
|
|
27
|
+
obj.props.rows.splice(at, 0, Array.from({ length: obj.props.rows[0].length }, () => ''));
|
|
28
|
+
obj.props.rowH = [];
|
|
29
|
+
this.notifyChange();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function insertTableColumn(where) {
|
|
33
|
+
const obj = this.firstSelected();
|
|
34
|
+
if (!obj || obj.type !== 'table') return;
|
|
35
|
+
const cur = this.tableCursor?.objId === obj.id ? this.tableCursor.ci : 0;
|
|
36
|
+
const at = where === 'left' ? cur : cur + 1;
|
|
37
|
+
this.pushHistory();
|
|
38
|
+
for (const row of obj.props.rows) row.splice(at, 0, '');
|
|
39
|
+
obj.props.colW = [];
|
|
40
|
+
this.notifyChange();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function deleteTableRow() {
|
|
44
|
+
const obj = this.firstSelected();
|
|
45
|
+
if (!obj || obj.type !== 'table') return;
|
|
46
|
+
if (obj.props.rows.length <= 1) return notify.warning('表格至少要保留一行');
|
|
47
|
+
const cur = this.tableCursor?.objId === obj.id ? this.tableCursor.ri : 0;
|
|
48
|
+
this.pushHistory();
|
|
49
|
+
obj.props.rows.splice(Math.min(cur, obj.props.rows.length - 1), 1);
|
|
50
|
+
obj.props.rowH = [];
|
|
51
|
+
this.notifyChange();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function deleteTableColumn() {
|
|
55
|
+
const obj = this.firstSelected();
|
|
56
|
+
if (!obj || obj.type !== 'table') return;
|
|
57
|
+
const cols = obj.props.rows[0]?.length || 0;
|
|
58
|
+
if (cols <= 1) return notify.warning('表格至少要保留一列');
|
|
59
|
+
const cur = this.tableCursor?.objId === obj.id ? this.tableCursor.ci : 0;
|
|
60
|
+
this.pushHistory();
|
|
61
|
+
for (const row of obj.props.rows) row.splice(Math.min(cur, cols - 1), 1);
|
|
62
|
+
obj.props.colW = [];
|
|
63
|
+
this.notifyChange();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function setTableStyle(style) {
|
|
67
|
+
const obj = this.firstSelected();
|
|
68
|
+
if (!obj || obj.type !== 'table') return;
|
|
69
|
+
this.pushHistory();
|
|
70
|
+
obj.props.tableStyle = style;
|
|
71
|
+
obj.props.headerRow = style !== 'plain';
|
|
72
|
+
obj.props.bandedRows = style === 'banded' || style === 'dark';
|
|
73
|
+
this.notifyChange();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function toggleHeaderRow() {
|
|
77
|
+
const obj = this.firstSelected();
|
|
78
|
+
if (!obj || obj.type !== 'table') return;
|
|
79
|
+
this.pushHistory();
|
|
80
|
+
obj.props.headerRow = !obj.props.headerRow;
|
|
81
|
+
if (!obj.props.headerRow) {
|
|
82
|
+
obj.props.tableStyle = obj.props.bandedRows ? (obj.props.tableStyle === 'dark' ? 'dark' : 'banded') : 'plain';
|
|
83
|
+
} else if (obj.props.tableStyle === 'plain') {
|
|
84
|
+
obj.props.tableStyle = 'header';
|
|
85
|
+
}
|
|
86
|
+
this.notifyChange();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function toggleBandedRows() {
|
|
90
|
+
const obj = this.firstSelected();
|
|
91
|
+
if (!obj || obj.type !== 'table') return;
|
|
92
|
+
this.pushHistory();
|
|
93
|
+
obj.props.bandedRows = !obj.props.bandedRows;
|
|
94
|
+
if (obj.props.bandedRows) {
|
|
95
|
+
if (obj.props.tableStyle === 'plain' || obj.props.tableStyle === 'header') obj.props.tableStyle = 'banded';
|
|
96
|
+
} else if (obj.props.tableStyle === 'banded' || obj.props.tableStyle === 'dark') {
|
|
97
|
+
obj.props.tableStyle = obj.props.headerRow ? 'header' : 'plain';
|
|
98
|
+
}
|
|
99
|
+
this.notifyChange();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const tableMethods = { resizeTable, insertTableRow, insertTableColumn, deleteTableRow, deleteTableColumn, setTableStyle, toggleHeaderRow, toggleBandedRows };
|