gbpptx 0.1.1 → 0.2.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 +140 -84
- package/package.json +13 -3
- package/src/index.js +16 -8
- package/src/kernel/model.js +396 -89
- package/src/kernel/parts.js +15 -11
- package/src/kernel/pptx.js +17 -12
- package/src/kernel/print.js +16 -14
- package/src/kernel/read.js +747 -157
- package/src/kernel/roundtrip.js +118 -18
- package/src/kernel/text.js +133 -28
- package/src/kernel/write.js +160 -49
- package/web/app/slides-app.js +2079 -477
- package/web/app/ui/backstage.js +255 -0
- package/web/app/ui/canvas.js +619 -141
- package/web/app/ui/color-panel.js +27 -9
- package/web/app/ui/dialogs.js +42 -3
- package/web/app/ui/file-io.js +66 -23
- package/web/app/ui/format-pane.js +427 -0
- package/web/app/ui/icons.js +129 -0
- package/web/app/ui/menu.js +26 -42
- package/web/app/ui/page-view.js +135 -64
- package/web/app/ui/present.js +257 -65
- package/web/app/ui/ribbon.js +878 -0
- package/web/app/ui/slide-pane.js +189 -0
- package/web/app/ui/statusbar.js +93 -0
- package/web/app/ui/titlebar.js +101 -0
- package/web/app/ui/transition.js +30 -0
- package/web/index.html +1 -0
- package/web/serve.mjs +1 -1
- package/web/styles/office.css +644 -0
- package/web/styles/slides.css +184 -177
- package/src/kernel/menus.js +0 -123
- package/web/app/ui/inspector.js +0 -209
- package/web/app/ui/slides-bar.js +0 -63
- package/web/app/ui/toolbar.js +0 -200
package/web/app/ui/inspector.js
DELETED
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
// [文件] gbpptx·检查器 —— 选中对象的属性面板(几何 + 按类型属性;多选时给对齐/层级)
|
|
2
|
-
import { SHAPE_KINDS, TYPE_NAMES } from '../../../src/kernel/model.js';
|
|
3
|
-
import { TEXT_COLORS } from './text-styles.js';
|
|
4
|
-
import { openColorPanel } from './color-panel.js';
|
|
5
|
-
import { field, numberInput, selectInput, textInput } from './dialogs.js';
|
|
6
|
-
|
|
7
|
-
const ALIGN_ITEMS = [
|
|
8
|
-
{ key: 'left', label: '左对齐' }, { key: 'centerH', label: '水平居中' }, { key: 'right', label: '右对齐' },
|
|
9
|
-
{ key: 'top', label: '上对齐' }, { key: 'middle', label: '垂直居中' }, { key: 'bottom', label: '下对齐' },
|
|
10
|
-
];
|
|
11
|
-
const ORDER_ITEMS = [
|
|
12
|
-
{ key: 'front', label: '置于顶层' }, { key: 'forward', label: '上移一层' },
|
|
13
|
-
{ key: 'backward', label: '下移一层' }, { key: 'back', label: '置于底层' },
|
|
14
|
-
];
|
|
15
|
-
|
|
16
|
-
export class Inspector {
|
|
17
|
-
constructor(app) {
|
|
18
|
-
this.app = app;
|
|
19
|
-
this.el = document.createElement('aside');
|
|
20
|
-
this.el.className = 'sl-inspector';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
render() {
|
|
24
|
-
const app = this.app;
|
|
25
|
-
this.el.replaceChildren();
|
|
26
|
-
const ids = app.selectedIds;
|
|
27
|
-
if (ids.length === 0) {
|
|
28
|
-
const empty = document.createElement('div');
|
|
29
|
-
empty.className = 'sl-inspector__empty';
|
|
30
|
-
empty.textContent = '未选中对象\n在画布上单击对象以编辑属性';
|
|
31
|
-
empty.style.whiteSpace = 'pre-line';
|
|
32
|
-
this.el.appendChild(empty);
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const objs = app.currentSlide().objects.filter((o) => ids.includes(o.id));
|
|
36
|
-
if (objs.length > 1) {
|
|
37
|
-
this.#renderMulti(objs);
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
this.#renderSingle(objs[0]);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
#title(text) {
|
|
44
|
-
const t = document.createElement('div');
|
|
45
|
-
t.className = 'sl-field-title';
|
|
46
|
-
t.textContent = text;
|
|
47
|
-
this.el.appendChild(t);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
#renderMulti() {
|
|
51
|
-
this.#title(`${this.app.selectedIds.length} 个对象`);
|
|
52
|
-
const row = document.createElement('div');
|
|
53
|
-
row.style.cssText = 'padding:0 10px;display:flex;flex-wrap:wrap;gap:4px';
|
|
54
|
-
for (const a of ALIGN_ITEMS) {
|
|
55
|
-
const b = document.createElement('button');
|
|
56
|
-
b.type = 'button';
|
|
57
|
-
b.className = 'gb-btn';
|
|
58
|
-
b.textContent = a.label;
|
|
59
|
-
b.addEventListener('click', () => this.app.alignToSlide(a.key));
|
|
60
|
-
row.appendChild(b);
|
|
61
|
-
}
|
|
62
|
-
this.el.appendChild(row);
|
|
63
|
-
const row2 = document.createElement('div');
|
|
64
|
-
row2.style.cssText = 'padding:8px 10px;display:flex;flex-wrap:wrap;gap:4px';
|
|
65
|
-
for (const o of ORDER_ITEMS) {
|
|
66
|
-
const b = document.createElement('button');
|
|
67
|
-
b.type = 'button';
|
|
68
|
-
b.className = 'gb-btn';
|
|
69
|
-
b.textContent = o.label;
|
|
70
|
-
b.addEventListener('click', () => this.app.arrange(o.key));
|
|
71
|
-
row2.appendChild(b);
|
|
72
|
-
}
|
|
73
|
-
this.el.appendChild(row2);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
#renderSingle(o) {
|
|
77
|
-
const app = this.app;
|
|
78
|
-
const p = o.props;
|
|
79
|
-
this.#title(TYPE_NAMES[o.type] || o.type);
|
|
80
|
-
|
|
81
|
-
const geo = document.createElement('div');
|
|
82
|
-
const numField = (label, key, opts = {}) => {
|
|
83
|
-
const input = numberInput(Math.round(o[key]), opts);
|
|
84
|
-
input.addEventListener('change', () => {
|
|
85
|
-
const v = Number(input.value);
|
|
86
|
-
if (Number.isFinite(v)) app.updateObject(o.id, { [key]: v });
|
|
87
|
-
});
|
|
88
|
-
return field(label, input);
|
|
89
|
-
};
|
|
90
|
-
geo.append(
|
|
91
|
-
numField('X', 'x'), numField('Y', 'y'),
|
|
92
|
-
numField('宽', 'w'), numField('高', 'h'),
|
|
93
|
-
numField('旋转°', 'rot', { step: 1 }),
|
|
94
|
-
);
|
|
95
|
-
this.el.appendChild(geo);
|
|
96
|
-
|
|
97
|
-
if (o.type === 'text') {
|
|
98
|
-
this.#title('文字');
|
|
99
|
-
const size = numberInput(p.fontSize, { min: 6, max: 200 });
|
|
100
|
-
size.addEventListener('change', () => app.setFontSize(Number(size.value)));
|
|
101
|
-
this.el.appendChild(field('字号', size));
|
|
102
|
-
const align = selectInput([
|
|
103
|
-
{ value: 'left', label: '左对齐' }, { value: 'center', label: '居中' },
|
|
104
|
-
{ value: 'right', label: '右对齐' }, { value: 'justify', label: '两端对齐' },
|
|
105
|
-
], p.align);
|
|
106
|
-
align.addEventListener('change', () => app.updateProps(o.id, { align: align.value }));
|
|
107
|
-
this.el.appendChild(field('对齐', align));
|
|
108
|
-
this.el.appendChild(this.#colorField('颜色', p.color || '', (c) => app.updateProps(o.id, { color: c })));
|
|
109
|
-
const row = document.createElement('div');
|
|
110
|
-
row.style.cssText = 'display:flex;gap:6px;padding:0 10px 8px';
|
|
111
|
-
for (const [key, label] of [['bold', '粗'], ['italic', '斜'], ['underline', '下划线']]) {
|
|
112
|
-
const b = document.createElement('button');
|
|
113
|
-
b.type = 'button';
|
|
114
|
-
b.className = `gb-btn${p[key] ? ' gb-btn--primary' : ''}`;
|
|
115
|
-
b.textContent = label;
|
|
116
|
-
b.addEventListener('click', () => app.updateProps(o.id, { [key]: !p[key] }));
|
|
117
|
-
row.appendChild(b);
|
|
118
|
-
}
|
|
119
|
-
this.el.appendChild(row);
|
|
120
|
-
} else if (o.type === 'shape') {
|
|
121
|
-
this.#title('图形');
|
|
122
|
-
const kind = selectInput(SHAPE_KINDS.map((k) => ({ value: k.kind, label: k.label })), p.kind);
|
|
123
|
-
kind.addEventListener('change', () => app.updateProps(o.id, { kind: kind.value }));
|
|
124
|
-
this.el.appendChild(field('形状', kind));
|
|
125
|
-
this.el.appendChild(this.#colorField('填充', p.fill || '', (c) => app.updateProps(o.id, { fill: c || '#e07b1f' })));
|
|
126
|
-
this.el.appendChild(this.#colorField('描边', p.stroke || '', (c) => app.updateProps(o.id, { stroke: c })));
|
|
127
|
-
const sw = numberInput(p.strokeW, { min: 0, max: 40 });
|
|
128
|
-
sw.addEventListener('change', () => app.updateProps(o.id, { strokeW: Number(sw.value) }));
|
|
129
|
-
this.el.appendChild(field('描边宽', sw));
|
|
130
|
-
const txt = textInput(p.text || '', '图形内文字');
|
|
131
|
-
txt.addEventListener('input', () => app.updateProps(o.id, { text: txt.value }));
|
|
132
|
-
this.el.appendChild(field('文字', txt));
|
|
133
|
-
const size = numberInput(p.fontSize, { min: 6, max: 200 });
|
|
134
|
-
size.addEventListener('change', () => app.updateProps(o.id, { fontSize: Number(size.value) }));
|
|
135
|
-
this.el.appendChild(field('字号', size));
|
|
136
|
-
this.el.appendChild(this.#colorField('文字色', p.textColor || '', (c) => app.updateProps(o.id, { textColor: c || '#ffffff' })));
|
|
137
|
-
const bold = document.createElement('div');
|
|
138
|
-
bold.style.cssText = 'display:flex;gap:6px;padding:0 10px 8px';
|
|
139
|
-
const bb = document.createElement('button');
|
|
140
|
-
bb.type = 'button';
|
|
141
|
-
bb.className = `gb-btn${p.bold ? ' gb-btn--primary' : ''}`;
|
|
142
|
-
bb.textContent = '粗体';
|
|
143
|
-
bb.addEventListener('click', () => app.updateProps(o.id, { bold: !p.bold }));
|
|
144
|
-
bold.appendChild(bb);
|
|
145
|
-
this.el.appendChild(bold);
|
|
146
|
-
} else if (o.type === 'line') {
|
|
147
|
-
this.#title('线条');
|
|
148
|
-
this.el.appendChild(this.#colorField('颜色', p.stroke || '', (c) => app.updateProps(o.id, { stroke: c || '#202124' })));
|
|
149
|
-
const sw = numberInput(p.strokeW, { min: 1, max: 40 });
|
|
150
|
-
sw.addEventListener('change', () => app.updateProps(o.id, { strokeW: Number(sw.value) }));
|
|
151
|
-
this.el.appendChild(field('线宽', sw));
|
|
152
|
-
const row = document.createElement('div');
|
|
153
|
-
row.style.cssText = 'display:flex;gap:6px;padding:0 10px 8px';
|
|
154
|
-
for (const [key, label] of [['dash', '虚线'], ['arrowEnd', '箭头']]) {
|
|
155
|
-
const b = document.createElement('button');
|
|
156
|
-
b.type = 'button';
|
|
157
|
-
b.className = `gb-btn${p[key] ? ' gb-btn--primary' : ''}`;
|
|
158
|
-
b.textContent = label;
|
|
159
|
-
b.addEventListener('click', () => app.updateProps(o.id, { [key]: !p[key] }));
|
|
160
|
-
row.appendChild(b);
|
|
161
|
-
}
|
|
162
|
-
this.el.appendChild(row);
|
|
163
|
-
} else if (o.type === 'table') {
|
|
164
|
-
this.#title('表格');
|
|
165
|
-
const rows = numberInput(p.rows.length, { min: 1, max: 30 });
|
|
166
|
-
rows.addEventListener('change', () => app.resizeTable(o.id, Number(rows.value), p.rows[0].length));
|
|
167
|
-
this.el.appendChild(field('行数', rows));
|
|
168
|
-
const cols = numberInput(p.rows[0].length, { min: 1, max: 20 });
|
|
169
|
-
cols.addEventListener('change', () => app.resizeTable(o.id, p.rows.length, Number(cols.value)));
|
|
170
|
-
this.el.appendChild(field('列数', cols));
|
|
171
|
-
const size = numberInput(p.fontSize, { min: 6, max: 100 });
|
|
172
|
-
size.addEventListener('change', () => app.updateProps(o.id, { fontSize: Number(size.value) }));
|
|
173
|
-
this.el.appendChild(field('字号', size));
|
|
174
|
-
const head = document.createElement('div');
|
|
175
|
-
head.style.cssText = 'display:flex;gap:6px;padding:0 10px 8px';
|
|
176
|
-
const hb = document.createElement('button');
|
|
177
|
-
hb.type = 'button';
|
|
178
|
-
hb.className = `gb-btn${p.headerRow ? ' gb-btn--primary' : ''}`;
|
|
179
|
-
hb.textContent = '首行加粗';
|
|
180
|
-
hb.addEventListener('click', () => app.updateProps(o.id, { headerRow: !p.headerRow }));
|
|
181
|
-
head.appendChild(hb);
|
|
182
|
-
this.el.appendChild(head);
|
|
183
|
-
const hint = document.createElement('div');
|
|
184
|
-
hint.style.cssText = 'padding:0 10px 10px;color:var(--dt-text-tertiary);font-size:12px';
|
|
185
|
-
hint.textContent = '双击单元格可直接改文字';
|
|
186
|
-
this.el.appendChild(hint);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
#colorField(label, value, onPick) {
|
|
191
|
-
const btn = document.createElement('button');
|
|
192
|
-
btn.type = 'button';
|
|
193
|
-
btn.className = 'gb-input';
|
|
194
|
-
btn.style.cssText = `display:flex;align-items:center;gap:6px;cursor:pointer;text-align:left`;
|
|
195
|
-
const dot = document.createElement('span');
|
|
196
|
-
dot.style.cssText = `width:14px;height:14px;border-radius:3px;border:1px solid var(--dt-border-strong);background:${value || 'transparent'}`;
|
|
197
|
-
btn.append(dot, document.createTextNode(value || '默认'));
|
|
198
|
-
btn.addEventListener('click', () => {
|
|
199
|
-
openColorPanel({
|
|
200
|
-
anchor: btn,
|
|
201
|
-
colors: TEXT_COLORS,
|
|
202
|
-
value,
|
|
203
|
-
columns: 10,
|
|
204
|
-
onPick: (c) => onPick(c),
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
return field(label, btn);
|
|
208
|
-
}
|
|
209
|
-
}
|
package/web/app/ui/slides-bar.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// [文件] gbpptx·幻灯片侧栏 —— 缩略图列表(选中/新建/复制/删除/上下移)+ 演讲者备注
|
|
2
|
-
import { SLIDE_W } from '../../../src/kernel/model.js';
|
|
3
|
-
import { renderPage } from './page-view.js';
|
|
4
|
-
|
|
5
|
-
const THUMB_W = 168;
|
|
6
|
-
|
|
7
|
-
export class SlidesBar {
|
|
8
|
-
constructor(app) {
|
|
9
|
-
this.app = app;
|
|
10
|
-
this.el = document.createElement('aside');
|
|
11
|
-
this.el.className = 'sl-side';
|
|
12
|
-
this.el.innerHTML = `
|
|
13
|
-
<div class="sl-side__title">幻灯片</div>
|
|
14
|
-
<div class="sl-thumbs"></div>
|
|
15
|
-
<div class="sl-notes">
|
|
16
|
-
<div class="sl-notes__label">演讲者备注</div>
|
|
17
|
-
<textarea placeholder="输入本页备注…"></textarea>
|
|
18
|
-
</div>`;
|
|
19
|
-
this.thumbsEl = this.el.querySelector('.sl-thumbs');
|
|
20
|
-
this.notesEl = this.el.querySelector('textarea');
|
|
21
|
-
this.notesEl.addEventListener('input', () => {
|
|
22
|
-
this.app.currentSlide().notes = this.notesEl.value;
|
|
23
|
-
this.app.notifyChange({ thumbs: false });
|
|
24
|
-
});
|
|
25
|
-
this.thumbsEl.addEventListener('click', (e) => {
|
|
26
|
-
const thumb = e.target.closest('.sl-thumb');
|
|
27
|
-
if (!thumb) return;
|
|
28
|
-
this.app.selectSlide(Number(thumb.dataset.index));
|
|
29
|
-
});
|
|
30
|
-
this.thumbsEl.addEventListener('dblclick', (e) => {
|
|
31
|
-
const thumb = e.target.closest('.sl-thumb');
|
|
32
|
-
if (thumb) this.app.presentAt(Number(thumb.dataset.index));
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
render() {
|
|
37
|
-
const { slides } = this.app.snapshot;
|
|
38
|
-
const theme = this.app.snapshot.theme;
|
|
39
|
-
this.thumbsEl.replaceChildren();
|
|
40
|
-
slides.forEach((slide, i) => {
|
|
41
|
-
const thumb = document.createElement('div');
|
|
42
|
-
thumb.className = `sl-thumb${i === this.app.index ? ' is-active' : ''}`;
|
|
43
|
-
thumb.dataset.index = String(i);
|
|
44
|
-
const no = document.createElement('div');
|
|
45
|
-
no.className = 'sl-thumb__no';
|
|
46
|
-
no.textContent = String(i + 1);
|
|
47
|
-
const box = document.createElement('div');
|
|
48
|
-
box.className = 'sl-thumb__box';
|
|
49
|
-
const page = renderPage(slide, theme);
|
|
50
|
-
const scale = THUMB_W / SLIDE_W;
|
|
51
|
-
page.style.transform = `scale(${scale})`;
|
|
52
|
-
box.appendChild(page);
|
|
53
|
-
thumb.append(no, box);
|
|
54
|
-
this.thumbsEl.appendChild(thumb);
|
|
55
|
-
});
|
|
56
|
-
this.notesEl.value = this.app.currentSlide()?.notes || '';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** 滚动到当前页(切换页后保证可见) */
|
|
60
|
-
scrollToActive() {
|
|
61
|
-
this.thumbsEl.querySelector('.sl-thumb.is-active')?.scrollIntoView({ block: 'nearest' });
|
|
62
|
-
}
|
|
63
|
-
}
|
package/web/app/ui/toolbar.js
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
// [文件] gbpptx·工具栏 —— 历史/插入/文字与图形格式/排列/主题/缩放
|
|
2
|
-
import { SHAPE_KINDS, THEMES, THEME_KEYS } from '../../../src/kernel/model.js';
|
|
3
|
-
import { FONT_FAMILIES, FONT_SIZES, TEXT_COLORS } from './text-styles.js';
|
|
4
|
-
import { openMenu } from './menu.js';
|
|
5
|
-
import { openColorPanel } from './color-panel.js';
|
|
6
|
-
|
|
7
|
-
const svg = (body) => `<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round">${body}</svg>`;
|
|
8
|
-
const ICONS = {
|
|
9
|
-
undo: svg('<path d="M9 7L4 12l5 5M4 12h11a5 5 0 0 1 0 10"/>'),
|
|
10
|
-
redo: svg('<path d="M15 7l5 5-5 5M20 12H9a5 5 0 0 0 0 10"/>'),
|
|
11
|
-
text: svg('<path d="M6 5h12M12 5v14M9 19h6"/>'),
|
|
12
|
-
image: svg('<rect x="3" y="4" width="18" height="16" rx="1.5"/><path d="M4 16l4-4 4 4 3-3 5 5"/><circle cx="9" cy="9" r="1.4"/>'),
|
|
13
|
-
shape: svg('<rect x="3" y="3" width="18" height="18" rx="2"/>'),
|
|
14
|
-
line: svg('<path d="M4 20L20 4"/>'),
|
|
15
|
-
arrow: svg('<path d="M4 12h14M14 7l5 5-5 5"/>'),
|
|
16
|
-
table: svg('<rect x="3" y="4" width="18" height="16" rx="1.5"/><path d="M3 10h18M3 15h18M9 4v16M15 4v16"/>'),
|
|
17
|
-
bold: svg('<path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z"/>'),
|
|
18
|
-
italic: svg('<path d="M15 5h-5M14 19H9M14 5l-4 14"/>'),
|
|
19
|
-
underline: svg('<path d="M7 5v7a5 5 0 0 0 10 0V5M5 20h14"/>'),
|
|
20
|
-
color: svg('<path d="M6 18h12M8 14l4-9 4 9M7 14h10"/>'),
|
|
21
|
-
fill: svg('<rect x="4" y="4" width="16" height="16" rx="2"/>'),
|
|
22
|
-
stroke: svg('<rect x="4" y="4" width="16" height="16" rx="2" stroke-dasharray="3 3"/>'),
|
|
23
|
-
align: svg('<path d="M4 6h16M4 12h10M4 18h14"/>'),
|
|
24
|
-
order: svg('<path d="M4 8h10M4 16h16M14 5v6M18 14v6"/>'),
|
|
25
|
-
copy: svg('<rect x="8" y="8" width="12" height="12" rx="2"/><path d="M4 16V6a2 2 0 0 1 2-2h8"/>'),
|
|
26
|
-
del: svg('<path d="M5 7h14M9 7V5h6v2M7 7l1 12h8l1-12"/>'),
|
|
27
|
-
present: svg('<path d="M4 5h16v11H4zM12 16v4M9 20h6"/>'),
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const toolButton = (name, title, onClick) => {
|
|
31
|
-
const b = document.createElement('button');
|
|
32
|
-
b.type = 'button';
|
|
33
|
-
b.className = 'sl-toolbtn';
|
|
34
|
-
b.title = title;
|
|
35
|
-
b.innerHTML = ICONS[name] || '';
|
|
36
|
-
b.addEventListener('click', onClick);
|
|
37
|
-
return b;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const sep = () => {
|
|
41
|
-
const s = document.createElement('span');
|
|
42
|
-
s.className = 'sl-toolbar__sep';
|
|
43
|
-
return s;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/** 下拉选择(触发钮 + 浮层条目) */
|
|
47
|
-
function makeSelect({ title, options, width, get, onChange }) {
|
|
48
|
-
const btn = document.createElement('button');
|
|
49
|
-
btn.type = 'button';
|
|
50
|
-
btn.className = 'sl-select';
|
|
51
|
-
btn.title = title;
|
|
52
|
-
btn.style.minWidth = `${width}px`;
|
|
53
|
-
btn.innerHTML = '<span class="sl-select__label"></span><span class="sl-select__caret">▾</span>';
|
|
54
|
-
const labelEl = btn.querySelector('.sl-select__label');
|
|
55
|
-
const sync = () => {
|
|
56
|
-
const v = get();
|
|
57
|
-
const opt = options.find((o) => String(o.value) === String(v));
|
|
58
|
-
labelEl.textContent = opt ? opt.label : (title || '');
|
|
59
|
-
if (opt?.style?.fontFamily) labelEl.style.fontFamily = opt.style.fontFamily;
|
|
60
|
-
};
|
|
61
|
-
btn.addEventListener('click', () => {
|
|
62
|
-
openMenu({
|
|
63
|
-
anchor: btn,
|
|
64
|
-
placement: 'bottom-start',
|
|
65
|
-
items: options.map((o) => ({ label: o.label, checked: String(o.value) === String(get()), onClick: () => { onChange(o.value); setTimeout(sync, 0); } })),
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
sync();
|
|
69
|
-
return { el: btn, refresh: sync };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 构建工具栏
|
|
74
|
-
* @param {object} app 编辑器控制器
|
|
75
|
-
* @returns {{ el: HTMLElement, refresh: Function }}
|
|
76
|
-
*/
|
|
77
|
-
export function buildToolbar(app) {
|
|
78
|
-
const bar = document.createElement('div');
|
|
79
|
-
bar.className = 'sl-toolbar';
|
|
80
|
-
const refs = { buttons: {}, selects: {} };
|
|
81
|
-
|
|
82
|
-
const sel = () => app.firstSelected();
|
|
83
|
-
|
|
84
|
-
const group1 = document.createElement('div');
|
|
85
|
-
group1.className = 'sl-toolbar__group';
|
|
86
|
-
refs.buttons.undo = toolButton('undo', '撤销 (Ctrl+Z)', () => app.undo());
|
|
87
|
-
refs.buttons.redo = toolButton('redo', '重做 (Ctrl+Y)', () => app.redo());
|
|
88
|
-
group1.append(refs.buttons.undo, refs.buttons.redo);
|
|
89
|
-
bar.append(group1, sep());
|
|
90
|
-
|
|
91
|
-
const group2 = document.createElement('div');
|
|
92
|
-
group2.className = 'sl-toolbar__group';
|
|
93
|
-
group2.append(
|
|
94
|
-
toolButton('text', '文本框', () => app.insert('text')),
|
|
95
|
-
toolButton('image', '图片…', () => app.insert('image')),
|
|
96
|
-
toolButton('shape', '形状', (e) => openMenu({
|
|
97
|
-
anchor: e.currentTarget,
|
|
98
|
-
placement: 'bottom-start',
|
|
99
|
-
items: SHAPE_KINDS.map((k) => ({ label: k.label, onClick: () => app.insert('shape', k.kind) })),
|
|
100
|
-
})),
|
|
101
|
-
toolButton('line', '线条', () => app.insert('line')),
|
|
102
|
-
toolButton('arrow', '箭头', () => app.insert('arrow')),
|
|
103
|
-
toolButton('table', '表格', () => app.insert('table')),
|
|
104
|
-
toolButton('present', '放映 (F5)', () => app.present()),
|
|
105
|
-
);
|
|
106
|
-
bar.append(group2, sep());
|
|
107
|
-
|
|
108
|
-
const group3 = document.createElement('div');
|
|
109
|
-
group3.className = 'sl-toolbar__group';
|
|
110
|
-
refs.selects.size = makeSelect({
|
|
111
|
-
title: '字号',
|
|
112
|
-
width: 56,
|
|
113
|
-
options: [{ value: '', label: '默认' }, ...FONT_SIZES.map((s) => ({ value: s, label: String(s) }))],
|
|
114
|
-
get: () => (sel() ? Math.round(sel().props.fontSize) : ''),
|
|
115
|
-
onChange: (v) => app.setFontSize(Number(v) || 0),
|
|
116
|
-
});
|
|
117
|
-
refs.selects.font = makeSelect({
|
|
118
|
-
title: '字体',
|
|
119
|
-
width: 110,
|
|
120
|
-
options: [{ value: '', label: '默认' }, ...FONT_FAMILIES.map((f) => ({ value: f.value, label: f.label, style: { fontFamily: f.value } }))],
|
|
121
|
-
get: () => (sel() ? sel().props.font || '' : ''),
|
|
122
|
-
onChange: (v) => app.setFontFamily(v),
|
|
123
|
-
});
|
|
124
|
-
refs.buttons.bold = toolButton('bold', '加粗', () => app.toggleProp('bold'));
|
|
125
|
-
refs.buttons.italic = toolButton('italic', '斜体', () => app.toggleProp('italic'));
|
|
126
|
-
refs.buttons.underline = toolButton('underline', '下划线', () => app.toggleProp('underline'));
|
|
127
|
-
const colorBtn = toolButton('color', '文字颜色', (e) => openColorPanel({
|
|
128
|
-
anchor: e.currentTarget, colors: TEXT_COLORS, columns: 10,
|
|
129
|
-
value: sel()?.props.color || '', onPick: (c) => app.setProp('color', c),
|
|
130
|
-
}));
|
|
131
|
-
group3.append(refs.selects.size.el, refs.selects.font.el, refs.buttons.bold, refs.buttons.italic, refs.buttons.underline, colorBtn);
|
|
132
|
-
bar.append(group3, sep());
|
|
133
|
-
|
|
134
|
-
const group4 = document.createElement('div');
|
|
135
|
-
group4.className = 'sl-toolbar__group';
|
|
136
|
-
const fillBtn = toolButton('fill', '填充色', (e) => openColorPanel({
|
|
137
|
-
anchor: e.currentTarget, colors: TEXT_COLORS, columns: 10,
|
|
138
|
-
value: sel()?.props.fill || '', onPick: (c) => app.setProp('fill', c || '#e07b1f'),
|
|
139
|
-
}));
|
|
140
|
-
const strokeBtn = toolButton('stroke', '描边色', (e) => openColorPanel({
|
|
141
|
-
anchor: e.currentTarget, colors: TEXT_COLORS, columns: 10,
|
|
142
|
-
value: sel()?.props.stroke || '', onPick: (c) => app.setProp('stroke', c),
|
|
143
|
-
}));
|
|
144
|
-
refs.buttons.align = toolButton('align', '对齐页面', (e) => openMenu({
|
|
145
|
-
anchor: e.currentTarget, placement: 'bottom-start',
|
|
146
|
-
items: [
|
|
147
|
-
{ label: '左对齐', onClick: () => app.alignToSlide('left') },
|
|
148
|
-
{ label: '水平居中', onClick: () => app.alignToSlide('centerH') },
|
|
149
|
-
{ label: '右对齐', onClick: () => app.alignToSlide('right') },
|
|
150
|
-
{ divided: true },
|
|
151
|
-
{ label: '上对齐', onClick: () => app.alignToSlide('top') },
|
|
152
|
-
{ label: '垂直居中', onClick: () => app.alignToSlide('middle') },
|
|
153
|
-
{ label: '下对齐', onClick: () => app.alignToSlide('bottom') },
|
|
154
|
-
],
|
|
155
|
-
}));
|
|
156
|
-
refs.buttons.order = toolButton('order', '层级', (e) => openMenu({
|
|
157
|
-
anchor: e.currentTarget, placement: 'bottom-start',
|
|
158
|
-
items: [
|
|
159
|
-
{ label: '置于顶层', onClick: () => app.arrange('front') },
|
|
160
|
-
{ label: '上移一层', onClick: () => app.arrange('forward') },
|
|
161
|
-
{ label: '下移一层', onClick: () => app.arrange('backward') },
|
|
162
|
-
{ label: '置于底层', onClick: () => app.arrange('back') },
|
|
163
|
-
],
|
|
164
|
-
}));
|
|
165
|
-
refs.buttons.copy = toolButton('copy', '复制对象 (Ctrl+D)', () => app.duplicateSelected());
|
|
166
|
-
refs.buttons.del = toolButton('del', '删除对象 (Del)', () => app.removeSelected());
|
|
167
|
-
group4.append(fillBtn, strokeBtn, refs.buttons.align, refs.buttons.order, refs.buttons.copy, refs.buttons.del);
|
|
168
|
-
bar.append(group4, sep());
|
|
169
|
-
|
|
170
|
-
const group5 = document.createElement('div');
|
|
171
|
-
group5.className = 'sl-toolbar__group';
|
|
172
|
-
refs.selects.theme = makeSelect({
|
|
173
|
-
title: '主题',
|
|
174
|
-
width: 92,
|
|
175
|
-
options: THEME_KEYS.map((k) => ({ value: k, label: THEMES[k].label })),
|
|
176
|
-
get: () => app.snapshot.theme,
|
|
177
|
-
onChange: (v) => app.setTheme(v),
|
|
178
|
-
});
|
|
179
|
-
group5.appendChild(refs.selects.theme.el);
|
|
180
|
-
bar.append(group5);
|
|
181
|
-
|
|
182
|
-
const refresh = () => {
|
|
183
|
-
const has = app.selectedIds.length > 0;
|
|
184
|
-
const o = sel();
|
|
185
|
-
for (const [key, b] of Object.entries(refs.buttons)) {
|
|
186
|
-
if (['undo', 'redo'].includes(key)) continue;
|
|
187
|
-
if (['align', 'order', 'copy', 'del'].includes(key)) b.disabled = !has;
|
|
188
|
-
}
|
|
189
|
-
refs.buttons.undo.disabled = !app.canUndo();
|
|
190
|
-
refs.buttons.redo.disabled = !app.canRedo();
|
|
191
|
-
refs.buttons.bold.classList.toggle('is-active', Boolean(o?.props.bold));
|
|
192
|
-
refs.buttons.italic.classList.toggle('is-active', Boolean(o?.props.italic));
|
|
193
|
-
refs.buttons.underline.classList.toggle('is-active', Boolean(o?.props.underline));
|
|
194
|
-
refs.selects.size.refresh();
|
|
195
|
-
refs.selects.font.refresh();
|
|
196
|
-
refs.selects.theme.refresh();
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
return { el: bar, refresh };
|
|
200
|
-
}
|