gbpptx 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +253 -253
- package/package.json +48 -47
- package/src/index.js +29 -29
- package/src/kernel/model.js +651 -651
- package/src/kernel/parts.js +173 -173
- package/src/kernel/pptx.js +72 -72
- package/src/kernel/print.js +76 -76
- package/src/kernel/read.js +1018 -1018
- package/src/kernel/roundtrip.js +174 -174
- package/src/kernel/text.js +278 -278
- package/src/kernel/write.js +385 -385
- package/web/app/main.js +8 -8
- package/web/app/slides/arrange.js +101 -101
- package/web/app/slides/clipboard.js +93 -93
- package/web/app/slides/file.js +240 -240
- package/web/app/slides/find.js +201 -201
- package/web/app/slides/history.js +100 -100
- package/web/app/slides/keys.js +187 -187
- package/web/app/slides/menus.js +105 -105
- package/web/app/slides/objects.js +254 -254
- package/web/app/slides/render.js +183 -183
- package/web/app/slides/shared.js +85 -85
- package/web/app/slides/slides.js +103 -103
- package/web/app/slides/table.js +102 -102
- package/web/app/slides/text.js +418 -418
- package/web/app/slides/view.js +182 -182
- package/web/app/slides-app.js +354 -354
- package/web/app/ui/canvas.js +769 -769
- package/web/app/ui/color-panel.js +58 -58
- package/web/app/ui/dialogs.js +120 -120
- package/web/app/ui/file-io.js +116 -116
- package/web/app/ui/format-pane.js +427 -427
- package/web/app/ui/icons.js +241 -133
- package/web/app/ui/menu.js +77 -75
- package/web/app/ui/notify.js +45 -45
- package/web/app/ui/page-view.js +221 -221
- package/web/app/ui/popup.js +80 -80
- package/web/app/ui/present.js +292 -292
- package/web/app/ui/ribbon.js +855 -855
- package/web/app/ui/slide-pane.js +197 -197
- package/web/app/ui/statusbar.js +93 -93
- package/web/app/ui/text-styles.js +84 -84
- package/web/app/ui/titlebar.js +253 -250
- package/web/app/ui/transition.js +30 -30
- package/web/index.html +30 -29
- package/web/serve.mjs +26 -23
- package/web/styles/office.css +463 -463
- package/web/styles/slides.css +97 -97
- package/web/styles/tokens.css +154 -154
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
// [文件] gbpptx·色板 —— 预设色格 + 系统色轮 + Hex + 清除
|
|
2
|
-
import { openPopup } from './popup.js';
|
|
3
|
-
|
|
4
|
-
/** 打开色板;onPick(color) 回调(清除传空串) */
|
|
5
|
-
export function openColorPanel({ anchor, colors, value, title = '颜色', columns = 10, onPick }) {
|
|
6
|
-
const box = document.createElement('div');
|
|
7
|
-
box.className = 'gbp-color';
|
|
8
|
-
const grid = document.createElement('div');
|
|
9
|
-
grid.className = 'gbp-color__grid';
|
|
10
|
-
grid.style.gridTemplateColumns = `repeat(${columns}, 18px)`;
|
|
11
|
-
for (const color of colors) {
|
|
12
|
-
const sw = document.createElement('button');
|
|
13
|
-
sw.type = 'button';
|
|
14
|
-
sw.className = 'gbp-color__swatch';
|
|
15
|
-
sw.style.background = color;
|
|
16
|
-
if (value && String(value).toLowerCase() === color.toLowerCase()) sw.classList.add('gbp-is-active');
|
|
17
|
-
sw.title = color;
|
|
18
|
-
sw.addEventListener('click', () => { handle.close(); onPick(color); });
|
|
19
|
-
grid.appendChild(sw);
|
|
20
|
-
}
|
|
21
|
-
box.appendChild(grid);
|
|
22
|
-
|
|
23
|
-
const custom = document.createElement('div');
|
|
24
|
-
custom.className = 'gbp-color__custom';
|
|
25
|
-
const wheel = document.createElement('input');
|
|
26
|
-
wheel.type = 'color';
|
|
27
|
-
wheel.value = /^#[0-9a-f]{6}$/i.test(value || '') ? value : '#000000';
|
|
28
|
-
const hex = document.createElement('input');
|
|
29
|
-
hex.type = 'text';
|
|
30
|
-
hex.className = 'gbp-input';
|
|
31
|
-
hex.value = value || '';
|
|
32
|
-
hex.placeholder = '#rrggbb';
|
|
33
|
-
wheel.addEventListener('input', () => { hex.value = wheel.value; });
|
|
34
|
-
custom.append(wheel, hex);
|
|
35
|
-
box.appendChild(custom);
|
|
36
|
-
|
|
37
|
-
const actions = document.createElement('div');
|
|
38
|
-
actions.className = 'gbp-color__actions';
|
|
39
|
-
const apply = document.createElement('button');
|
|
40
|
-
apply.type = 'button';
|
|
41
|
-
apply.className = 'gbp-btn gbp-btn--primary';
|
|
42
|
-
apply.textContent = '应用';
|
|
43
|
-
const clear = document.createElement('button');
|
|
44
|
-
clear.type = 'button';
|
|
45
|
-
clear.className = 'gbp-btn';
|
|
46
|
-
clear.textContent = '清除';
|
|
47
|
-
apply.addEventListener('click', () => {
|
|
48
|
-
const v = String(hex.value || '').trim();
|
|
49
|
-
handle.close();
|
|
50
|
-
onPick(/^#?[0-9a-f]{6}$/i.test(v) ? (v.startsWith('#') ? v : `#${v}`) : '');
|
|
51
|
-
});
|
|
52
|
-
clear.addEventListener('click', () => { handle.close(); onPick(''); });
|
|
53
|
-
actions.append(apply, clear);
|
|
54
|
-
box.appendChild(actions);
|
|
55
|
-
|
|
56
|
-
const handle = openPopup({ anchor, content: box, placement: 'bottom-start', className: 'gbp-popup--color' });
|
|
57
|
-
return handle;
|
|
58
|
-
}
|
|
1
|
+
// [文件] gbpptx·色板 —— 预设色格 + 系统色轮 + Hex + 清除
|
|
2
|
+
import { openPopup } from './popup.js';
|
|
3
|
+
|
|
4
|
+
/** 打开色板;onPick(color) 回调(清除传空串) */
|
|
5
|
+
export function openColorPanel({ anchor, colors, value, title = '颜色', columns = 10, onPick }) {
|
|
6
|
+
const box = document.createElement('div');
|
|
7
|
+
box.className = 'gbp-color';
|
|
8
|
+
const grid = document.createElement('div');
|
|
9
|
+
grid.className = 'gbp-color__grid';
|
|
10
|
+
grid.style.gridTemplateColumns = `repeat(${columns}, 18px)`;
|
|
11
|
+
for (const color of colors) {
|
|
12
|
+
const sw = document.createElement('button');
|
|
13
|
+
sw.type = 'button';
|
|
14
|
+
sw.className = 'gbp-color__swatch';
|
|
15
|
+
sw.style.background = color;
|
|
16
|
+
if (value && String(value).toLowerCase() === color.toLowerCase()) sw.classList.add('gbp-is-active');
|
|
17
|
+
sw.title = color;
|
|
18
|
+
sw.addEventListener('click', () => { handle.close(); onPick(color); });
|
|
19
|
+
grid.appendChild(sw);
|
|
20
|
+
}
|
|
21
|
+
box.appendChild(grid);
|
|
22
|
+
|
|
23
|
+
const custom = document.createElement('div');
|
|
24
|
+
custom.className = 'gbp-color__custom';
|
|
25
|
+
const wheel = document.createElement('input');
|
|
26
|
+
wheel.type = 'color';
|
|
27
|
+
wheel.value = /^#[0-9a-f]{6}$/i.test(value || '') ? value : '#000000';
|
|
28
|
+
const hex = document.createElement('input');
|
|
29
|
+
hex.type = 'text';
|
|
30
|
+
hex.className = 'gbp-input';
|
|
31
|
+
hex.value = value || '';
|
|
32
|
+
hex.placeholder = '#rrggbb';
|
|
33
|
+
wheel.addEventListener('input', () => { hex.value = wheel.value; });
|
|
34
|
+
custom.append(wheel, hex);
|
|
35
|
+
box.appendChild(custom);
|
|
36
|
+
|
|
37
|
+
const actions = document.createElement('div');
|
|
38
|
+
actions.className = 'gbp-color__actions';
|
|
39
|
+
const apply = document.createElement('button');
|
|
40
|
+
apply.type = 'button';
|
|
41
|
+
apply.className = 'gbp-btn gbp-btn--primary';
|
|
42
|
+
apply.textContent = '应用';
|
|
43
|
+
const clear = document.createElement('button');
|
|
44
|
+
clear.type = 'button';
|
|
45
|
+
clear.className = 'gbp-btn';
|
|
46
|
+
clear.textContent = '清除';
|
|
47
|
+
apply.addEventListener('click', () => {
|
|
48
|
+
const v = String(hex.value || '').trim();
|
|
49
|
+
handle.close();
|
|
50
|
+
onPick(/^#?[0-9a-f]{6}$/i.test(v) ? (v.startsWith('#') ? v : `#${v}`) : '');
|
|
51
|
+
});
|
|
52
|
+
clear.addEventListener('click', () => { handle.close(); onPick(''); });
|
|
53
|
+
actions.append(apply, clear);
|
|
54
|
+
box.appendChild(actions);
|
|
55
|
+
|
|
56
|
+
const handle = openPopup({ anchor, content: box, placement: 'bottom-start', className: 'gbp-popup--color' });
|
|
57
|
+
return handle;
|
|
58
|
+
}
|
package/web/app/ui/dialogs.js
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
// [文件] gbpptx·对话框 —— 通用模态 + 单字段输入(供背景色/尺寸/页主题等表单复用)
|
|
2
|
-
|
|
3
|
-
/** 通用模态:build(bodyEl, close) 填充内容;onConfirm() 返回 false 可阻止关闭;onClose 在任意方式关闭后调用 */
|
|
4
|
-
export function openModal({ title, build, onConfirm, onClose, confirmText = '确定', width = 420 }) {
|
|
5
|
-
const overlay = document.createElement('div');
|
|
6
|
-
overlay.className = 'gbp-modal-overlay';
|
|
7
|
-
const box = document.createElement('div');
|
|
8
|
-
box.className = 'gbp-modal';
|
|
9
|
-
box.style.width = `${width}px`;
|
|
10
|
-
box.innerHTML = `
|
|
11
|
-
<div class="gbp-modal__head"><span class="gbp-modal__title"></span><button type="button" class="gbp-modal__close">✕</button></div>
|
|
12
|
-
<div class="gbp-modal__body"></div>
|
|
13
|
-
<div class="gbp-modal__foot">
|
|
14
|
-
<button type="button" class="gbp-btn" data-act="cancel">取消</button>
|
|
15
|
-
<button type="button" class="gbp-btn gbp-btn--primary" data-act="ok"></button>
|
|
16
|
-
</div>`;
|
|
17
|
-
box.querySelector('.gbp-modal__title').textContent = title;
|
|
18
|
-
box.querySelector('[data-act="ok"]').textContent = confirmText;
|
|
19
|
-
const bodyEl = box.querySelector('.gbp-modal__body');
|
|
20
|
-
|
|
21
|
-
let closed = false;
|
|
22
|
-
const close = () => {
|
|
23
|
-
if (closed) return;
|
|
24
|
-
closed = true;
|
|
25
|
-
document.removeEventListener('keydown', onKey, true);
|
|
26
|
-
overlay.remove();
|
|
27
|
-
onClose?.();
|
|
28
|
-
};
|
|
29
|
-
const onKey = (e) => {
|
|
30
|
-
if (e.key === 'Escape') {
|
|
31
|
-
e.stopPropagation();
|
|
32
|
-
close();
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
const confirm = () => {
|
|
36
|
-
if (onConfirm && onConfirm() === false) return;
|
|
37
|
-
close();
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
box.querySelector('[data-act="cancel"]').addEventListener('click', close);
|
|
41
|
-
box.querySelector('.gbp-modal__close').addEventListener('click', close);
|
|
42
|
-
box.querySelector('[data-act="ok"]').addEventListener('click', confirm);
|
|
43
|
-
overlay.addEventListener('mousedown', (e) => { if (e.target === overlay) close(); });
|
|
44
|
-
document.addEventListener('keydown', onKey, true);
|
|
45
|
-
build?.(bodyEl, close);
|
|
46
|
-
overlay.appendChild(box);
|
|
47
|
-
document.body.appendChild(overlay);
|
|
48
|
-
return { close };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function field(labelText, control) {
|
|
52
|
-
const row = document.createElement('label');
|
|
53
|
-
row.className = 'gbp-field';
|
|
54
|
-
const span = document.createElement('span');
|
|
55
|
-
span.className = 'gbp-field__label';
|
|
56
|
-
span.textContent = labelText;
|
|
57
|
-
row.append(span, control);
|
|
58
|
-
return row;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function textInput(value = '', placeholder = '') {
|
|
62
|
-
const input = document.createElement('input');
|
|
63
|
-
input.type = 'text';
|
|
64
|
-
input.className = 'gbp-input';
|
|
65
|
-
input.value = value;
|
|
66
|
-
input.placeholder = placeholder;
|
|
67
|
-
return input;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function numberInput(value, { min, max, step = 1 } = {}) {
|
|
71
|
-
const input = document.createElement('input');
|
|
72
|
-
input.type = 'number';
|
|
73
|
-
input.className = 'gbp-input';
|
|
74
|
-
if (min !== undefined) input.min = min;
|
|
75
|
-
if (max !== undefined) input.max = max;
|
|
76
|
-
input.step = step;
|
|
77
|
-
input.value = value;
|
|
78
|
-
return input;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function selectInput(options, value) {
|
|
82
|
-
const sel = document.createElement('select');
|
|
83
|
-
sel.className = 'gbp-input';
|
|
84
|
-
for (const o of options) {
|
|
85
|
-
const opt = document.createElement('option');
|
|
86
|
-
opt.value = o.value;
|
|
87
|
-
opt.textContent = o.label;
|
|
88
|
-
sel.appendChild(opt);
|
|
89
|
-
}
|
|
90
|
-
sel.value = value ?? '';
|
|
91
|
-
return sel;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** 询问一个文本值(确认返回字符串,取消返回 null) */
|
|
95
|
-
export function promptText({ title, label, value = '', placeholder = '' }) {
|
|
96
|
-
return new Promise((resolve) => {
|
|
97
|
-
let input = null;
|
|
98
|
-
let done = false;
|
|
99
|
-
openModal({
|
|
100
|
-
title: title || label || '输入',
|
|
101
|
-
width: 360,
|
|
102
|
-
onClose: () => { if (!done) resolve(null); },
|
|
103
|
-
build: (body) => {
|
|
104
|
-
input = textInput(value, placeholder);
|
|
105
|
-
body.appendChild(field(label || '', input));
|
|
106
|
-
requestAnimationFrame(() => { input.focus(); input.select(); });
|
|
107
|
-
input.addEventListener('keydown', (e) => {
|
|
108
|
-
if (e.key === 'Enter') {
|
|
109
|
-
e.preventDefault();
|
|
110
|
-
body.closest('.gbp-modal').querySelector('[data-act="ok"]').click();
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
},
|
|
114
|
-
onConfirm: () => {
|
|
115
|
-
done = true;
|
|
116
|
-
resolve(input ? input.value : '');
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
}
|
|
1
|
+
// [文件] gbpptx·对话框 —— 通用模态 + 单字段输入(供背景色/尺寸/页主题等表单复用)
|
|
2
|
+
|
|
3
|
+
/** 通用模态:build(bodyEl, close) 填充内容;onConfirm() 返回 false 可阻止关闭;onClose 在任意方式关闭后调用 */
|
|
4
|
+
export function openModal({ title, build, onConfirm, onClose, confirmText = '确定', width = 420 }) {
|
|
5
|
+
const overlay = document.createElement('div');
|
|
6
|
+
overlay.className = 'gbp-modal-overlay';
|
|
7
|
+
const box = document.createElement('div');
|
|
8
|
+
box.className = 'gbp-modal';
|
|
9
|
+
box.style.width = `${width}px`;
|
|
10
|
+
box.innerHTML = `
|
|
11
|
+
<div class="gbp-modal__head"><span class="gbp-modal__title"></span><button type="button" class="gbp-modal__close">✕</button></div>
|
|
12
|
+
<div class="gbp-modal__body"></div>
|
|
13
|
+
<div class="gbp-modal__foot">
|
|
14
|
+
<button type="button" class="gbp-btn" data-act="cancel">取消</button>
|
|
15
|
+
<button type="button" class="gbp-btn gbp-btn--primary" data-act="ok"></button>
|
|
16
|
+
</div>`;
|
|
17
|
+
box.querySelector('.gbp-modal__title').textContent = title;
|
|
18
|
+
box.querySelector('[data-act="ok"]').textContent = confirmText;
|
|
19
|
+
const bodyEl = box.querySelector('.gbp-modal__body');
|
|
20
|
+
|
|
21
|
+
let closed = false;
|
|
22
|
+
const close = () => {
|
|
23
|
+
if (closed) return;
|
|
24
|
+
closed = true;
|
|
25
|
+
document.removeEventListener('keydown', onKey, true);
|
|
26
|
+
overlay.remove();
|
|
27
|
+
onClose?.();
|
|
28
|
+
};
|
|
29
|
+
const onKey = (e) => {
|
|
30
|
+
if (e.key === 'Escape') {
|
|
31
|
+
e.stopPropagation();
|
|
32
|
+
close();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const confirm = () => {
|
|
36
|
+
if (onConfirm && onConfirm() === false) return;
|
|
37
|
+
close();
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
box.querySelector('[data-act="cancel"]').addEventListener('click', close);
|
|
41
|
+
box.querySelector('.gbp-modal__close').addEventListener('click', close);
|
|
42
|
+
box.querySelector('[data-act="ok"]').addEventListener('click', confirm);
|
|
43
|
+
overlay.addEventListener('mousedown', (e) => { if (e.target === overlay) close(); });
|
|
44
|
+
document.addEventListener('keydown', onKey, true);
|
|
45
|
+
build?.(bodyEl, close);
|
|
46
|
+
overlay.appendChild(box);
|
|
47
|
+
document.body.appendChild(overlay);
|
|
48
|
+
return { close };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function field(labelText, control) {
|
|
52
|
+
const row = document.createElement('label');
|
|
53
|
+
row.className = 'gbp-field';
|
|
54
|
+
const span = document.createElement('span');
|
|
55
|
+
span.className = 'gbp-field__label';
|
|
56
|
+
span.textContent = labelText;
|
|
57
|
+
row.append(span, control);
|
|
58
|
+
return row;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function textInput(value = '', placeholder = '') {
|
|
62
|
+
const input = document.createElement('input');
|
|
63
|
+
input.type = 'text';
|
|
64
|
+
input.className = 'gbp-input';
|
|
65
|
+
input.value = value;
|
|
66
|
+
input.placeholder = placeholder;
|
|
67
|
+
return input;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function numberInput(value, { min, max, step = 1 } = {}) {
|
|
71
|
+
const input = document.createElement('input');
|
|
72
|
+
input.type = 'number';
|
|
73
|
+
input.className = 'gbp-input';
|
|
74
|
+
if (min !== undefined) input.min = min;
|
|
75
|
+
if (max !== undefined) input.max = max;
|
|
76
|
+
input.step = step;
|
|
77
|
+
input.value = value;
|
|
78
|
+
return input;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function selectInput(options, value) {
|
|
82
|
+
const sel = document.createElement('select');
|
|
83
|
+
sel.className = 'gbp-input';
|
|
84
|
+
for (const o of options) {
|
|
85
|
+
const opt = document.createElement('option');
|
|
86
|
+
opt.value = o.value;
|
|
87
|
+
opt.textContent = o.label;
|
|
88
|
+
sel.appendChild(opt);
|
|
89
|
+
}
|
|
90
|
+
sel.value = value ?? '';
|
|
91
|
+
return sel;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 询问一个文本值(确认返回字符串,取消返回 null) */
|
|
95
|
+
export function promptText({ title, label, value = '', placeholder = '' }) {
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
let input = null;
|
|
98
|
+
let done = false;
|
|
99
|
+
openModal({
|
|
100
|
+
title: title || label || '输入',
|
|
101
|
+
width: 360,
|
|
102
|
+
onClose: () => { if (!done) resolve(null); },
|
|
103
|
+
build: (body) => {
|
|
104
|
+
input = textInput(value, placeholder);
|
|
105
|
+
body.appendChild(field(label || '', input));
|
|
106
|
+
requestAnimationFrame(() => { input.focus(); input.select(); });
|
|
107
|
+
input.addEventListener('keydown', (e) => {
|
|
108
|
+
if (e.key === 'Enter') {
|
|
109
|
+
e.preventDefault();
|
|
110
|
+
body.closest('.gbp-modal').querySelector('[data-act="ok"]').click();
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
onConfirm: () => {
|
|
115
|
+
done = true;
|
|
116
|
+
resolve(input ? input.value : '');
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
package/web/app/ui/file-io.js
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
// [文件] gbpptx·文件读写 —— 打开 / 保存 / 另存为(优先 File System Access API,回退 input[type=file] + 下载)
|
|
2
|
-
|
|
3
|
-
import { downloadBlob, ensureExt as ensureExtWith } from 'guabiutils';
|
|
4
|
-
|
|
5
|
-
export const PPTX_MIME = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
|
|
6
|
-
export const PDF_MIME = 'application/pdf';
|
|
7
|
-
|
|
8
|
-
export const hasFsAccess = () => typeof window.showOpenFilePicker === 'function';
|
|
9
|
-
|
|
10
|
-
/** 浏览器 File → Uint8Array */
|
|
11
|
-
export async function readBytes(file) {
|
|
12
|
-
return new Uint8Array(await file.arrayBuffer());
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** 文件名是不是 .pptx(只认扩展名,魔数由 isPptx 判断) */
|
|
16
|
-
export const isPptxFile = (file) => /\.pptx$/i.test(String(file?.name || ''));
|
|
17
|
-
|
|
18
|
-
/** 打开一个图片文件(返回 File;取消返回 null) */
|
|
19
|
-
export function pickImageFile() {
|
|
20
|
-
return new Promise((resolve) => {
|
|
21
|
-
const input = document.createElement('input');
|
|
22
|
-
input.type = 'file';
|
|
23
|
-
input.accept = 'image/png,image/jpeg,image/gif';
|
|
24
|
-
let settled = false;
|
|
25
|
-
const done = (file) => {
|
|
26
|
-
if (settled) return;
|
|
27
|
-
settled = true;
|
|
28
|
-
window.removeEventListener('focus', onFocus);
|
|
29
|
-
resolve(file);
|
|
30
|
-
};
|
|
31
|
-
const onFocus = () => setTimeout(() => done(null), 300);
|
|
32
|
-
input.addEventListener('change', () => done(input.files?.[0] || null), { once: true });
|
|
33
|
-
input.addEventListener('cancel', () => done(null), { once: true });
|
|
34
|
-
window.addEventListener('focus', onFocus, { once: true });
|
|
35
|
-
input.click();
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** 打开 .pptx:返回 { name, bytes, handle|null };用户取消返回 null */
|
|
40
|
-
export async function openPptx() {
|
|
41
|
-
if (hasFsAccess()) {
|
|
42
|
-
try {
|
|
43
|
-
const [handle] = await window.showOpenFilePicker({
|
|
44
|
-
types: [{ description: 'PowerPoint 演示文稿', accept: { [PPTX_MIME]: ['.pptx'] } }],
|
|
45
|
-
multiple: false,
|
|
46
|
-
});
|
|
47
|
-
if (!handle) return null;
|
|
48
|
-
const file = await handle.getFile();
|
|
49
|
-
return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle };
|
|
50
|
-
} catch (err) {
|
|
51
|
-
if (err?.name === 'AbortError') return null;
|
|
52
|
-
// 权限/跨域/浏览器策略拒绝时回落到 input[type=file]
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
const file = await pickViaInput();
|
|
56
|
-
if (!file) return null;
|
|
57
|
-
return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle: null };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function pickViaInput() {
|
|
61
|
-
return new Promise((resolve) => {
|
|
62
|
-
const input = document.createElement('input');
|
|
63
|
-
input.type = 'file';
|
|
64
|
-
input.accept = '.pptx';
|
|
65
|
-
let settled = false;
|
|
66
|
-
const done = (file) => {
|
|
67
|
-
if (settled) return;
|
|
68
|
-
settled = true;
|
|
69
|
-
window.removeEventListener('focus', onFocus);
|
|
70
|
-
resolve(file);
|
|
71
|
-
};
|
|
72
|
-
const onFocus = () => setTimeout(() => done(null), 300);
|
|
73
|
-
input.addEventListener('change', () => done(input.files?.[0] || null), { once: true });
|
|
74
|
-
input.addEventListener('cancel', () => done(null), { once: true });
|
|
75
|
-
window.addEventListener('focus', onFocus, { once: true });
|
|
76
|
-
input.click();
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** 保存到已知句柄(无句柄则触发下载) */
|
|
81
|
-
export async function savePptx(bytes, name, handle) {
|
|
82
|
-
if (handle?.createWritable) {
|
|
83
|
-
const writable = await handle.createWritable();
|
|
84
|
-
await writable.write(bytes);
|
|
85
|
-
await writable.close();
|
|
86
|
-
return { saved: true, handle };
|
|
87
|
-
}
|
|
88
|
-
downloadBlob(new Blob([bytes], { type: PPTX_MIME }), ensureExt(name, '.pptx'));
|
|
89
|
-
return { saved: true, handle: null };
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** 另存为:优先系统保存对话框,回退下载 */
|
|
93
|
-
export async function savePptxAs(bytes, name) {
|
|
94
|
-
const suggested = ensureExt(name, '.pptx');
|
|
95
|
-
if (typeof window.showSaveFilePicker === 'function') {
|
|
96
|
-
try {
|
|
97
|
-
const handle = await window.showSaveFilePicker({
|
|
98
|
-
suggestedName: suggested,
|
|
99
|
-
types: [{ description: 'PowerPoint 演示文稿', accept: { [PPTX_MIME]: ['.pptx'] } }],
|
|
100
|
-
});
|
|
101
|
-
if (!handle) return { saved: false, handle: null, name: suggested };
|
|
102
|
-
const writable = await handle.createWritable();
|
|
103
|
-
await writable.write(bytes);
|
|
104
|
-
await writable.close();
|
|
105
|
-
return { saved: true, handle, name: handle.name || suggested };
|
|
106
|
-
} catch (err) {
|
|
107
|
-
if (err?.name === 'AbortError') return { saved: false, handle: null, name: suggested };
|
|
108
|
-
// 系统保存对话框不可用时回落到下载
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
downloadBlob(new Blob([bytes], { type: PPTX_MIME }), suggested);
|
|
112
|
-
return { saved: true, handle: null, name: suggested };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** 文件名兜底扩展名(空名用「新建演示」) */
|
|
116
|
-
export const ensureExt = (name, ext) => ensureExtWith(name, ext, '新建演示');
|
|
1
|
+
// [文件] gbpptx·文件读写 —— 打开 / 保存 / 另存为(优先 File System Access API,回退 input[type=file] + 下载)
|
|
2
|
+
|
|
3
|
+
import { downloadBlob, ensureExt as ensureExtWith } from 'guabiutils';
|
|
4
|
+
|
|
5
|
+
export const PPTX_MIME = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
|
|
6
|
+
export const PDF_MIME = 'application/pdf';
|
|
7
|
+
|
|
8
|
+
export const hasFsAccess = () => typeof window.showOpenFilePicker === 'function';
|
|
9
|
+
|
|
10
|
+
/** 浏览器 File → Uint8Array */
|
|
11
|
+
export async function readBytes(file) {
|
|
12
|
+
return new Uint8Array(await file.arrayBuffer());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 文件名是不是 .pptx(只认扩展名,魔数由 isPptx 判断) */
|
|
16
|
+
export const isPptxFile = (file) => /\.pptx$/i.test(String(file?.name || ''));
|
|
17
|
+
|
|
18
|
+
/** 打开一个图片文件(返回 File;取消返回 null) */
|
|
19
|
+
export function pickImageFile() {
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
const input = document.createElement('input');
|
|
22
|
+
input.type = 'file';
|
|
23
|
+
input.accept = 'image/png,image/jpeg,image/gif';
|
|
24
|
+
let settled = false;
|
|
25
|
+
const done = (file) => {
|
|
26
|
+
if (settled) return;
|
|
27
|
+
settled = true;
|
|
28
|
+
window.removeEventListener('focus', onFocus);
|
|
29
|
+
resolve(file);
|
|
30
|
+
};
|
|
31
|
+
const onFocus = () => setTimeout(() => done(null), 300);
|
|
32
|
+
input.addEventListener('change', () => done(input.files?.[0] || null), { once: true });
|
|
33
|
+
input.addEventListener('cancel', () => done(null), { once: true });
|
|
34
|
+
window.addEventListener('focus', onFocus, { once: true });
|
|
35
|
+
input.click();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 打开 .pptx:返回 { name, bytes, handle|null };用户取消返回 null */
|
|
40
|
+
export async function openPptx() {
|
|
41
|
+
if (hasFsAccess()) {
|
|
42
|
+
try {
|
|
43
|
+
const [handle] = await window.showOpenFilePicker({
|
|
44
|
+
types: [{ description: 'PowerPoint 演示文稿', accept: { [PPTX_MIME]: ['.pptx'] } }],
|
|
45
|
+
multiple: false,
|
|
46
|
+
});
|
|
47
|
+
if (!handle) return null;
|
|
48
|
+
const file = await handle.getFile();
|
|
49
|
+
return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle };
|
|
50
|
+
} catch (err) {
|
|
51
|
+
if (err?.name === 'AbortError') return null;
|
|
52
|
+
// 权限/跨域/浏览器策略拒绝时回落到 input[type=file]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const file = await pickViaInput();
|
|
56
|
+
if (!file) return null;
|
|
57
|
+
return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle: null };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function pickViaInput() {
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
const input = document.createElement('input');
|
|
63
|
+
input.type = 'file';
|
|
64
|
+
input.accept = '.pptx';
|
|
65
|
+
let settled = false;
|
|
66
|
+
const done = (file) => {
|
|
67
|
+
if (settled) return;
|
|
68
|
+
settled = true;
|
|
69
|
+
window.removeEventListener('focus', onFocus);
|
|
70
|
+
resolve(file);
|
|
71
|
+
};
|
|
72
|
+
const onFocus = () => setTimeout(() => done(null), 300);
|
|
73
|
+
input.addEventListener('change', () => done(input.files?.[0] || null), { once: true });
|
|
74
|
+
input.addEventListener('cancel', () => done(null), { once: true });
|
|
75
|
+
window.addEventListener('focus', onFocus, { once: true });
|
|
76
|
+
input.click();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 保存到已知句柄(无句柄则触发下载) */
|
|
81
|
+
export async function savePptx(bytes, name, handle) {
|
|
82
|
+
if (handle?.createWritable) {
|
|
83
|
+
const writable = await handle.createWritable();
|
|
84
|
+
await writable.write(bytes);
|
|
85
|
+
await writable.close();
|
|
86
|
+
return { saved: true, handle };
|
|
87
|
+
}
|
|
88
|
+
downloadBlob(new Blob([bytes], { type: PPTX_MIME }), ensureExt(name, '.pptx'));
|
|
89
|
+
return { saved: true, handle: null };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 另存为:优先系统保存对话框,回退下载 */
|
|
93
|
+
export async function savePptxAs(bytes, name) {
|
|
94
|
+
const suggested = ensureExt(name, '.pptx');
|
|
95
|
+
if (typeof window.showSaveFilePicker === 'function') {
|
|
96
|
+
try {
|
|
97
|
+
const handle = await window.showSaveFilePicker({
|
|
98
|
+
suggestedName: suggested,
|
|
99
|
+
types: [{ description: 'PowerPoint 演示文稿', accept: { [PPTX_MIME]: ['.pptx'] } }],
|
|
100
|
+
});
|
|
101
|
+
if (!handle) return { saved: false, handle: null, name: suggested };
|
|
102
|
+
const writable = await handle.createWritable();
|
|
103
|
+
await writable.write(bytes);
|
|
104
|
+
await writable.close();
|
|
105
|
+
return { saved: true, handle, name: handle.name || suggested };
|
|
106
|
+
} catch (err) {
|
|
107
|
+
if (err?.name === 'AbortError') return { saved: false, handle: null, name: suggested };
|
|
108
|
+
// 系统保存对话框不可用时回落到下载
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
downloadBlob(new Blob([bytes], { type: PPTX_MIME }), suggested);
|
|
112
|
+
return { saved: true, handle: null, name: suggested };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 文件名兜底扩展名(空名用「新建演示」) */
|
|
116
|
+
export const ensureExt = (name, ext) => ensureExtWith(name, ext, '新建演示');
|