@a200888213/gbdocx 0.6.2 → 0.6.3

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.
@@ -1,159 +1,159 @@
1
- // [文件] gbdocx·word·view —— 视图与对话框 —— 视图开关、面板、页面设置/页边距/分栏
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
7
- import { openColumnDialog as openColumnDialogUI, openMarginDialog as openMarginDialogUI, openPageSetupDialog as openPageSetupDialogUI } from '../../ui/dialogs.js';
8
- import { icon } from '../../ui/icons.js';
9
- import { savePrefs } from './shared.js';
10
-
11
- /** 预览模式:只读 + 收起编辑界面;按钮留在文件头行原位,再点或按 Esc 退出 */
12
- function setPreview(on) {
13
- const next = !!on;
14
- if (next === this.preview) return;
15
- if (next) {
16
- this.previewRestore = { readOnly: this.readOnly, mode: this.editMode, menuBar: this.menuBarVisible };
17
- this.preview = true;
18
- this.root.classList.add('gb-is-preview');
19
- this.previewBtn.setAttribute('aria-pressed', 'true');
20
- this.previewBtn.title = '退出预览';
21
- this.previewBtn.querySelector('span').textContent = '退出预览';
22
- this.setReadOnly(true);
23
- this.setMenuBarVisible(false);
24
- } else {
25
- const restore = this.previewRestore || { readOnly: false, mode: 'edit', menuBar: true };
26
- this.preview = false;
27
- this.root.classList.remove('gb-is-preview');
28
- this.previewBtn.setAttribute('aria-pressed', 'false');
29
- this.previewBtn.title = '预览模式';
30
- this.previewBtn.querySelector('span').textContent = '预览';
31
- this.setReadOnly(restore.readOnly);
32
- if (!restore.readOnly) this.setEditMode(restore.mode);
33
- this.setMenuBarVisible(restore.menuBar);
34
- this.previewRestore = null;
35
- }
36
- this.previewBtn.blur();
37
- }
38
-
39
- /** 全屏/恢复:整个 Word 应用铺满网页(页面内覆盖;不用原生 Fullscreen,
40
- * 否则 append 到 body 的菜单/弹窗在全屏层外不可见) */
41
- function toggleFullscreen() {
42
- this.setPageFullscreen(!this.pageFullscreen);
43
- }
44
-
45
- function setPageFullscreen(on) {
46
- const next = !!on;
47
- if (next === this.pageFullscreen) return;
48
- this.pageFullscreen = next;
49
- if (next) {
50
- // [坑] 页面主容器常是 position:fixed(自带层叠上下文),留在原位压不过全局顶栏;
51
- // 整个 .word-scope 挂到 body 顶层,作用域样式随节点一起走
52
- this.fsHome = { parent: this.scopeEl.parentNode, next: this.scopeEl.nextSibling };
53
- this.root.classList.add('gbd-is-pagefullscreen');
54
- document.body.appendChild(this.scopeEl);
55
- document.addEventListener('keydown', this.onFullscreenEsc, true);
56
- } else {
57
- this.root.classList.remove('gbd-is-pagefullscreen');
58
- if (this.fsHome?.parent) this.fsHome.parent.insertBefore(this.scopeEl, this.fsHome.next);
59
- this.fsHome = null;
60
- document.removeEventListener('keydown', this.onFullscreenEsc, true);
61
- }
62
- this.renderFullscreenBtn();
63
- }
64
-
65
- function renderFullscreenBtn() {
66
- const active = this.pageFullscreen;
67
- this.fullscreenBtn.title = active ? '退出全屏' : '全屏';
68
- this.fullscreenBtn.setAttribute('aria-pressed', String(active));
69
- this.fullscreenBtn.innerHTML = `${icon(active ? 'fullscreenExit' : 'fullscreen')}<span>${active ? '恢复' : '全屏'}</span>`;
70
- }
71
-
72
- function setView(key, on) {
73
- const next = on === undefined ? !this.flags[key] : !!on;
74
- this.flags = { ...this.flags, [key]: next };
75
- this.prefs.flags = this.flags;
76
- savePrefs(this.prefs);
77
- this.applyView();
78
- }
79
-
80
- function applyView() {
81
- const main = this.root.querySelector('.word-main');
82
- const hasCatalog = this.flags.catalog;
83
- const hasComments = this.flags.commentsPanel;
84
- if (hasCatalog && !this.catalogPanel.el.isConnected) main.insertBefore(this.catalogPanel.el, this.editorWrap);
85
- if (!hasCatalog && this.catalogPanel.el.isConnected) this.catalogPanel.el.remove();
86
- if (hasComments && !this.commentsPanel.el.isConnected) main.appendChild(this.commentsPanel.el);
87
- if (!hasComments && this.commentsPanel.el.isConnected) this.commentsPanel.el.remove();
88
- if (hasCatalog) this.refreshCatalog();
89
- this.renderPanels();
90
- this.ribbon?.refresh();
91
- }
92
-
93
- function renderPanels() {
94
- if (this.flags.commentsPanel) {
95
- this.commentsPanel.activeKey = this.activeCommentKey;
96
- this.commentsPanel.setItems(this.comments);
97
- }
98
- }
99
-
100
- function renderHead() {
101
- // 嵌入模式:标题与路径由宿主给(服务端文档名/所在目录),本地 doc 只作兜底
102
- const hostTitle = this.embed ? this.io?.getTitle?.() : '';
103
- this.titleEl.textContent = hostTitle || this.doc.name || '新建文档';
104
- const hostPath = this.embed ? this.io?.getPath?.() : '';
105
- this.pathEl.textContent = hostPath
106
- || (this.embed ? '' : this.doc.pathText || (this.doc.handle ? '(已关联文件)' : '(未关联文件,保存将另存为)'));
107
- // 嵌入模式:保存状态由宿主给(落盘结果只有宿主知道)
108
- const hostStatus = this.embed ? this.io?.getStatus?.() : null;
109
- this.statusEl.textContent = hostStatus?.text || '';
110
- this.dirtyEl.hidden = !this.dirty;
111
- }
112
-
113
- function openPageSetupDialog() {
114
- openPageSetupDialogUI({
115
- page: this.state.page,
116
- pageNumbers: this.state.pageNumbers,
117
- watermark: this.state.watermark,
118
- hfMode: this.state.hfMode,
119
- header: this.state.header,
120
- footer: this.state.footer,
121
- headerFirst: this.state.headerFirst,
122
- footerFirst: this.state.footerFirst,
123
- }, (out) => {
124
- const prevHf = { header: this.state.header, footer: this.state.footer, headerFirst: this.state.headerFirst, footerFirst: this.state.footerFirst };
125
- this.state.page = { ...this.state.page, ...out.page };
126
- this.state.pageNumbers = out.pageNumbers;
127
- this.state.watermark = out.watermark;
128
- this.state.hfMode = out.hfMode;
129
- this.state.header = cleanHfHtml(out.header);
130
- this.state.footer = cleanHfHtml(out.footer);
131
- this.state.headerFirst = cleanHfHtml(out.headerFirst);
132
- this.state.footerFirst = cleanHfHtml(out.footerFirst);
133
- // 只有「新写入的文字」才把「不显示」自动拉回显示;用户明确选「不显示」而文字没变时必须尊重选择
134
- const changedToText = ['header', 'footer', 'headerFirst', 'footerFirst']
135
- .some((key) => this.state[key] !== prevHf[key] && this.state[key].replace(/<[^>]*>/g, '').trim());
136
- if (changedToText && this.state.hfMode === 'none') this.state.hfMode = 'all';
137
- this.onDocUpdate();
138
- this.applyEditorGeometry();
139
- this.paginate();
140
- });
141
- }
142
-
143
- function openMarginDialog() {
144
- openMarginDialogUI(this.state.page.margins, (margins) => {
145
- this.state.page = { ...this.state.page, margins };
146
- this.onDocUpdate();
147
- this.paginate();
148
- });
149
- }
150
-
151
- function openColumnDialog() {
152
- openColumnDialogUI(this.state.page, (patch) => {
153
- this.state.page = { ...this.state.page, ...patch };
154
- this.onDocUpdate();
155
- this.paginate();
156
- });
157
- }
158
-
159
- export const viewMethods = { setView, applyView, renderPanels, renderHead, openPageSetupDialog, openMarginDialog, openColumnDialog, setPreview, toggleFullscreen, setPageFullscreen, renderFullscreenBtn };
1
+ // [文件] gbdocx·word·view —— 视图与对话框 —— 视图开关、面板、页面设置/页边距/分栏
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
7
+ import { openColumnDialog as openColumnDialogUI, openMarginDialog as openMarginDialogUI, openPageSetupDialog as openPageSetupDialogUI } from '../../ui/dialogs.js';
8
+ import { icon } from '../../ui/icons.js';
9
+ import { savePrefs } from './shared.js';
10
+
11
+ /** 预览模式:只读 + 收起编辑界面;按钮留在文件头行原位,再点或按 Esc 退出 */
12
+ function setPreview(on) {
13
+ const next = !!on;
14
+ if (next === this.preview) return;
15
+ if (next) {
16
+ this.previewRestore = { readOnly: this.readOnly, mode: this.editMode, menuBar: this.menuBarVisible };
17
+ this.preview = true;
18
+ this.root.classList.add('gb-is-preview');
19
+ this.previewBtn.setAttribute('aria-pressed', 'true');
20
+ this.previewBtn.title = '退出预览';
21
+ this.previewBtn.querySelector('span').textContent = '退出预览';
22
+ this.setReadOnly(true);
23
+ this.setMenuBarVisible(false);
24
+ } else {
25
+ const restore = this.previewRestore || { readOnly: false, mode: 'edit', menuBar: true };
26
+ this.preview = false;
27
+ this.root.classList.remove('gb-is-preview');
28
+ this.previewBtn.setAttribute('aria-pressed', 'false');
29
+ this.previewBtn.title = '预览模式';
30
+ this.previewBtn.querySelector('span').textContent = '预览';
31
+ this.setReadOnly(restore.readOnly);
32
+ if (!restore.readOnly) this.setEditMode(restore.mode);
33
+ this.setMenuBarVisible(restore.menuBar);
34
+ this.previewRestore = null;
35
+ }
36
+ this.previewBtn.blur();
37
+ }
38
+
39
+ /** 全屏/恢复:整个 Word 应用铺满网页(页面内覆盖;不用原生 Fullscreen,
40
+ * 否则 append 到 body 的菜单/弹窗在全屏层外不可见) */
41
+ function toggleFullscreen() {
42
+ this.setPageFullscreen(!this.pageFullscreen);
43
+ }
44
+
45
+ function setPageFullscreen(on) {
46
+ const next = !!on;
47
+ if (next === this.pageFullscreen) return;
48
+ this.pageFullscreen = next;
49
+ if (next) {
50
+ // [坑] 页面主容器常是 position:fixed(自带层叠上下文),留在原位压不过全局顶栏;
51
+ // 整个 .word-scope 挂到 body 顶层,作用域样式随节点一起走
52
+ this.fsHome = { parent: this.scopeEl.parentNode, next: this.scopeEl.nextSibling };
53
+ this.root.classList.add('gbd-is-pagefullscreen');
54
+ document.body.appendChild(this.scopeEl);
55
+ document.addEventListener('keydown', this.onFullscreenEsc, true);
56
+ } else {
57
+ this.root.classList.remove('gbd-is-pagefullscreen');
58
+ if (this.fsHome?.parent) this.fsHome.parent.insertBefore(this.scopeEl, this.fsHome.next);
59
+ this.fsHome = null;
60
+ document.removeEventListener('keydown', this.onFullscreenEsc, true);
61
+ }
62
+ this.renderFullscreenBtn();
63
+ }
64
+
65
+ function renderFullscreenBtn() {
66
+ const active = this.pageFullscreen;
67
+ this.fullscreenBtn.title = active ? '退出全屏' : '全屏';
68
+ this.fullscreenBtn.setAttribute('aria-pressed', String(active));
69
+ this.fullscreenBtn.innerHTML = `${icon(active ? 'fullscreenExit' : 'fullscreen')}<span>${active ? '恢复' : '全屏'}</span>`;
70
+ }
71
+
72
+ function setView(key, on) {
73
+ const next = on === undefined ? !this.flags[key] : !!on;
74
+ this.flags = { ...this.flags, [key]: next };
75
+ this.prefs.flags = this.flags;
76
+ savePrefs(this.prefs);
77
+ this.applyView();
78
+ }
79
+
80
+ function applyView() {
81
+ const main = this.root.querySelector('.word-main');
82
+ const hasCatalog = this.flags.catalog;
83
+ const hasComments = this.flags.commentsPanel;
84
+ if (hasCatalog && !this.catalogPanel.el.isConnected) main.insertBefore(this.catalogPanel.el, this.editorWrap);
85
+ if (!hasCatalog && this.catalogPanel.el.isConnected) this.catalogPanel.el.remove();
86
+ if (hasComments && !this.commentsPanel.el.isConnected) main.appendChild(this.commentsPanel.el);
87
+ if (!hasComments && this.commentsPanel.el.isConnected) this.commentsPanel.el.remove();
88
+ if (hasCatalog) this.refreshCatalog();
89
+ this.renderPanels();
90
+ this.ribbon?.refresh();
91
+ }
92
+
93
+ function renderPanels() {
94
+ if (this.flags.commentsPanel) {
95
+ this.commentsPanel.activeKey = this.activeCommentKey;
96
+ this.commentsPanel.setItems(this.comments);
97
+ }
98
+ }
99
+
100
+ function renderHead() {
101
+ // 嵌入模式:标题与路径由宿主给(服务端文档名/所在目录),本地 doc 只作兜底
102
+ const hostTitle = this.embed ? this.io?.getTitle?.() : '';
103
+ this.titleEl.textContent = hostTitle || this.doc.name || '新建文档';
104
+ const hostPath = this.embed ? this.io?.getPath?.() : '';
105
+ this.pathEl.textContent = hostPath
106
+ || (this.embed ? '' : this.doc.pathText || (this.doc.handle ? '(已关联文件)' : '(未关联文件,保存将另存为)'));
107
+ // 嵌入模式:保存状态由宿主给(落盘结果只有宿主知道)
108
+ const hostStatus = this.embed ? this.io?.getStatus?.() : null;
109
+ this.statusEl.textContent = hostStatus?.text || '';
110
+ this.dirtyEl.hidden = !this.dirty;
111
+ }
112
+
113
+ function openPageSetupDialog() {
114
+ openPageSetupDialogUI({
115
+ page: this.state.page,
116
+ pageNumbers: this.state.pageNumbers,
117
+ watermark: this.state.watermark,
118
+ hfMode: this.state.hfMode,
119
+ header: this.state.header,
120
+ footer: this.state.footer,
121
+ headerFirst: this.state.headerFirst,
122
+ footerFirst: this.state.footerFirst,
123
+ }, (out) => {
124
+ const prevHf = { header: this.state.header, footer: this.state.footer, headerFirst: this.state.headerFirst, footerFirst: this.state.footerFirst };
125
+ this.state.page = { ...this.state.page, ...out.page };
126
+ this.state.pageNumbers = out.pageNumbers;
127
+ this.state.watermark = out.watermark;
128
+ this.state.hfMode = out.hfMode;
129
+ this.state.header = cleanHfHtml(out.header);
130
+ this.state.footer = cleanHfHtml(out.footer);
131
+ this.state.headerFirst = cleanHfHtml(out.headerFirst);
132
+ this.state.footerFirst = cleanHfHtml(out.footerFirst);
133
+ // 只有「新写入的文字」才把「不显示」自动拉回显示;用户明确选「不显示」而文字没变时必须尊重选择
134
+ const changedToText = ['header', 'footer', 'headerFirst', 'footerFirst']
135
+ .some((key) => this.state[key] !== prevHf[key] && this.state[key].replace(/<[^>]*>/g, '').trim());
136
+ if (changedToText && this.state.hfMode === 'none') this.state.hfMode = 'all';
137
+ this.onDocUpdate();
138
+ this.applyEditorGeometry();
139
+ this.paginate();
140
+ });
141
+ }
142
+
143
+ function openMarginDialog() {
144
+ openMarginDialogUI(this.state.page.margins, (margins) => {
145
+ this.state.page = { ...this.state.page, margins };
146
+ this.onDocUpdate();
147
+ this.paginate();
148
+ });
149
+ }
150
+
151
+ function openColumnDialog() {
152
+ openColumnDialogUI(this.state.page, (patch) => {
153
+ this.state.page = { ...this.state.page, ...patch };
154
+ this.onDocUpdate();
155
+ this.paginate();
156
+ });
157
+ }
158
+
159
+ export const viewMethods = { setView, applyView, renderPanels, renderHead, openPageSetupDialog, openMarginDialog, openColumnDialog, setPreview, toggleFullscreen, setPageFullscreen, renderFullscreenBtn };