@a200888213/gbdocx 0.5.2 → 0.6.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 +215 -207
- package/index.html +3 -2
- package/package.json +48 -48
- package/serve.mjs +3 -2
- package/src/app/file-io.js +122 -122
- package/src/app/main.js +9 -9
- package/src/app/word/catalog.js +52 -52
- package/src/app/word/comments.js +112 -112
- package/src/app/word/doc.js +113 -113
- package/src/app/word/edit.js +72 -72
- package/src/app/word/file.js +30 -1
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +6 -2
- package/src/app/word-app.js +486 -469
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/codec/blocks.js +123 -123
- package/src/kernel/docx/codec/carry.js +129 -129
- package/src/kernel/docx/codec/comments.js +25 -25
- package/src/kernel/docx/codec/docx.js +198 -198
- package/src/kernel/docx/codec/docxStyles.js +177 -177
- package/src/kernel/docx/codec/fonts.js +195 -195
- package/src/kernel/docx/codec/helpers.js +161 -161
- package/src/kernel/docx/codec/hfHtml.js +152 -152
- package/src/kernel/docx/codec/html.js +739 -739
- package/src/kernel/docx/codec/losses.js +420 -420
- package/src/kernel/docx/codec/markdown.js +170 -170
- package/src/kernel/docx/codec/parts.js +249 -249
- package/src/kernel/docx/codec/read.js +603 -603
- package/src/kernel/docx/codec/render.js +1156 -1156
- package/src/kernel/docx/codec/roundtrip.js +236 -236
- package/src/kernel/docx/codec/write.js +833 -833
- package/src/kernel/docx/comments.js +14 -14
- package/src/kernel/docx/pageGeometry.js +123 -123
- package/src/kernel/docx/pagination.js +481 -481
- package/src/kernel/docx/watermark.js +45 -45
- package/src/kernel/editor/constants.js +8 -8
- package/src/kernel/editor/engine/blocks.js +594 -594
- package/src/kernel/editor/engine/blocksEnter.js +109 -109
- package/src/kernel/editor/engine/blocksInsert.js +254 -254
- package/src/kernel/editor/engine/blocksStructure.js +226 -226
- package/src/kernel/editor/engine/commands.js +181 -181
- package/src/kernel/editor/engine/dom.js +646 -646
- package/src/kernel/editor/engine/history.js +149 -149
- package/src/kernel/editor/engine/inline.js +656 -656
- package/src/kernel/editor/engine/input.js +554 -554
- package/src/kernel/editor/engine/interaction.js +137 -137
- package/src/kernel/editor/engine/pointer.js +39 -39
- package/src/kernel/editor/engine/revision.js +138 -138
- package/src/kernel/editor/engine/schema.js +417 -417
- package/src/kernel/editor/engine/selection.js +119 -119
- package/src/kernel/editor/engine/state.js +118 -118
- package/src/kernel/editor/engine/tableClip.js +154 -154
- package/src/kernel/editor/engine/tableGrid.js +55 -55
- package/src/kernel/editor/engine/tableInteraction.js +388 -388
- package/src/kernel/editor/engine/tableMenu.js +250 -250
- package/src/kernel/editor/engine/tableOps.js +174 -174
- package/src/kernel/editor/engine/tables.js +732 -732
- package/src/kernel/editor/overlayGeometry.js +20 -20
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +600 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +133 -133
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +137 -135
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/prompt.js +89 -89
- package/src/ui/ribbon.js +485 -485
- package/src/ui/special-chars.js +39 -39
- package/src/ui/statusbar.js +81 -81
- package/src/ui/toolbar.js +32 -32
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
package/src/app/word/doc.js
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
// [文件] gbdocx·word·doc —— 文档状态 —— 应用/读取当前文档、脏标记与自动保存
|
|
2
|
-
|
|
3
|
-
// [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
|
|
4
|
-
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
-
|
|
6
|
-
import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
|
|
7
|
-
import { resetPagination } from '../../kernel/docx/pagination.js';
|
|
8
|
-
import { HF_MODES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../../kernel/docx/constants.js';
|
|
9
|
-
import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
|
|
10
|
-
import { countRevisions } from '../../kernel/editor/engine/revision.js';
|
|
11
|
-
function onDocUpdate() {
|
|
12
|
-
this.setDirty(true);
|
|
13
|
-
// 嵌入模式:自动保存归宿主(宿主自己排期与写盘),包内不按文件句柄自动保存
|
|
14
|
-
if (!this.embed && this.prefs.autosave) this.scheduleAutosave();
|
|
15
|
-
const root = this.editor?.root;
|
|
16
|
-
this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
|
|
17
|
-
this.suggestionCount = countRevisions(root);
|
|
18
|
-
this.statusBar.setSuggestions(this.suggestionCount);
|
|
19
|
-
if (this.flags.catalog) this.refreshCatalog();
|
|
20
|
-
this.schedulePaginate();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function setDirty(on) {
|
|
24
|
-
this.dirty = on;
|
|
25
|
-
if (this.dirtyEl) this.dirtyEl.hidden = !on;
|
|
26
|
-
this.statusBar?.setDirty(on, this.saving);
|
|
27
|
-
// 嵌入模式:脏态回调宿主(页签脏标与自动保存排期由宿主决定)
|
|
28
|
-
this.onDirty?.(on);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function scheduleAutosave() {
|
|
32
|
-
clearTimeout(this._autoTimer);
|
|
33
|
-
this._autoTimer = setTimeout(() => {
|
|
34
|
-
if (this.dirty && this.doc.handle) this.save(true);
|
|
35
|
-
}, 2000);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function applyDoc(model) {
|
|
39
|
-
const s = this.state;
|
|
40
|
-
const list = Array.isArray(model?.sections) ? model.sections : [];
|
|
41
|
-
s.sections = list;
|
|
42
|
-
const base = list[list.length - 1] || {};
|
|
43
|
-
s.page = { ...defaultPageConf(), ...(base.page || {}) };
|
|
44
|
-
s.hfMode = base.hfMode || HF_MODES[0].value;
|
|
45
|
-
s.header = cleanHfHtml(base.header || '');
|
|
46
|
-
s.footer = cleanHfHtml(base.footer || '');
|
|
47
|
-
s.headerEven = cleanHfHtml(base.headerEven || '');
|
|
48
|
-
s.footerEven = cleanHfHtml(base.footerEven || '');
|
|
49
|
-
s.headerFirst = cleanHfHtml(base.headerFirst || '');
|
|
50
|
-
s.footerFirst = cleanHfHtml(base.footerFirst || '');
|
|
51
|
-
s.pageNumbers = { ...defaultPageNumbers(), ...(base.pageNumbers || {}) };
|
|
52
|
-
s.watermark = { ...defaultWatermark(), ...(base.watermark || {}) };
|
|
53
|
-
s.footnotes = model?.footnotes || [];
|
|
54
|
-
s.endnotes = model?.endnotes || [];
|
|
55
|
-
s.props = model?.props || null;
|
|
56
|
-
s.defaultTabStop = model?.defaultTabStop > 0 ? model.defaultTabStop : 720;
|
|
57
|
-
// 标点挤压是排版设置:存回去时得带着,否则保存一次版式就回退(每行少一个字、整篇多一页)
|
|
58
|
-
s.compressPunctuation = model?.compressPunctuation === true;
|
|
59
|
-
s.carry = model?.carry || [];
|
|
60
|
-
s.losses = model?.losses || [];
|
|
61
|
-
this.lossAck = false;
|
|
62
|
-
this.lossAutoWarned = false;
|
|
63
|
-
this.comments = model?.comments || [];
|
|
64
|
-
this.activeCommentKey = '';
|
|
65
|
-
this.surface.setHTML(model?.html?.trim() ? model.html : '<p><br></p>');
|
|
66
|
-
this.renumberFootnotes();
|
|
67
|
-
const ed = this.editor;
|
|
68
|
-
if (ed?.root) ed.quietly(() => resetPagination(ed.root));
|
|
69
|
-
this.setEditMode(this.editMode);
|
|
70
|
-
this.setDirty(false);
|
|
71
|
-
this.refreshStats();
|
|
72
|
-
this.paginate();
|
|
73
|
-
this.renderPanels();
|
|
74
|
-
this.renderHead();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function currentDoc() {
|
|
78
|
-
const s = this.state;
|
|
79
|
-
const base = {
|
|
80
|
-
type: s.sections[s.sections.length - 1]?.type || '',
|
|
81
|
-
page: s.page,
|
|
82
|
-
header: s.header,
|
|
83
|
-
footer: s.footer,
|
|
84
|
-
headerEven: s.headerEven,
|
|
85
|
-
footerEven: s.footerEven,
|
|
86
|
-
headerFirst: s.headerFirst,
|
|
87
|
-
footerFirst: s.footerFirst,
|
|
88
|
-
hfMode: s.hfMode,
|
|
89
|
-
pageNumbers: s.pageNumbers,
|
|
90
|
-
watermark: s.watermark,
|
|
91
|
-
};
|
|
92
|
-
const list = s.sections.slice(0, -1);
|
|
93
|
-
list.push(base);
|
|
94
|
-
return {
|
|
95
|
-
html: this.surface.flush() ?? '',
|
|
96
|
-
sections: list,
|
|
97
|
-
comments: this.comments,
|
|
98
|
-
footnotes: s.footnotes,
|
|
99
|
-
endnotes: s.endnotes,
|
|
100
|
-
props: s.props,
|
|
101
|
-
defaultTabStop: s.defaultTabStop,
|
|
102
|
-
compressPunctuation: s.compressPunctuation === true,
|
|
103
|
-
carry: s.carry,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function hasContent(raw) {
|
|
108
|
-
const probe = document.createElement('div');
|
|
109
|
-
probe.innerHTML = raw || '';
|
|
110
|
-
return Boolean(probe.textContent.trim() || probe.querySelector(`img,table,hr,.${DOC_CLASS.PAGEBREAK}`));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export const docMethods = { onDocUpdate, setDirty, scheduleAutosave, applyDoc, currentDoc, hasContent };
|
|
1
|
+
// [文件] gbdocx·word·doc —— 文档状态 —— 应用/读取当前文档、脏标记与自动保存
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
|
|
4
|
+
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
+
|
|
6
|
+
import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
|
|
7
|
+
import { resetPagination } from '../../kernel/docx/pagination.js';
|
|
8
|
+
import { HF_MODES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../../kernel/docx/constants.js';
|
|
9
|
+
import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
|
|
10
|
+
import { countRevisions } from '../../kernel/editor/engine/revision.js';
|
|
11
|
+
function onDocUpdate() {
|
|
12
|
+
this.setDirty(true);
|
|
13
|
+
// 嵌入模式:自动保存归宿主(宿主自己排期与写盘),包内不按文件句柄自动保存
|
|
14
|
+
if (!this.embed && this.prefs.autosave) this.scheduleAutosave();
|
|
15
|
+
const root = this.editor?.root;
|
|
16
|
+
this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
|
|
17
|
+
this.suggestionCount = countRevisions(root);
|
|
18
|
+
this.statusBar.setSuggestions(this.suggestionCount);
|
|
19
|
+
if (this.flags.catalog) this.refreshCatalog();
|
|
20
|
+
this.schedulePaginate();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function setDirty(on) {
|
|
24
|
+
this.dirty = on;
|
|
25
|
+
if (this.dirtyEl) this.dirtyEl.hidden = !on;
|
|
26
|
+
this.statusBar?.setDirty(on, this.saving);
|
|
27
|
+
// 嵌入模式:脏态回调宿主(页签脏标与自动保存排期由宿主决定)
|
|
28
|
+
this.onDirty?.(on);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function scheduleAutosave() {
|
|
32
|
+
clearTimeout(this._autoTimer);
|
|
33
|
+
this._autoTimer = setTimeout(() => {
|
|
34
|
+
if (this.dirty && this.doc.handle) this.save(true);
|
|
35
|
+
}, 2000);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function applyDoc(model) {
|
|
39
|
+
const s = this.state;
|
|
40
|
+
const list = Array.isArray(model?.sections) ? model.sections : [];
|
|
41
|
+
s.sections = list;
|
|
42
|
+
const base = list[list.length - 1] || {};
|
|
43
|
+
s.page = { ...defaultPageConf(), ...(base.page || {}) };
|
|
44
|
+
s.hfMode = base.hfMode || HF_MODES[0].value;
|
|
45
|
+
s.header = cleanHfHtml(base.header || '');
|
|
46
|
+
s.footer = cleanHfHtml(base.footer || '');
|
|
47
|
+
s.headerEven = cleanHfHtml(base.headerEven || '');
|
|
48
|
+
s.footerEven = cleanHfHtml(base.footerEven || '');
|
|
49
|
+
s.headerFirst = cleanHfHtml(base.headerFirst || '');
|
|
50
|
+
s.footerFirst = cleanHfHtml(base.footerFirst || '');
|
|
51
|
+
s.pageNumbers = { ...defaultPageNumbers(), ...(base.pageNumbers || {}) };
|
|
52
|
+
s.watermark = { ...defaultWatermark(), ...(base.watermark || {}) };
|
|
53
|
+
s.footnotes = model?.footnotes || [];
|
|
54
|
+
s.endnotes = model?.endnotes || [];
|
|
55
|
+
s.props = model?.props || null;
|
|
56
|
+
s.defaultTabStop = model?.defaultTabStop > 0 ? model.defaultTabStop : 720;
|
|
57
|
+
// 标点挤压是排版设置:存回去时得带着,否则保存一次版式就回退(每行少一个字、整篇多一页)
|
|
58
|
+
s.compressPunctuation = model?.compressPunctuation === true;
|
|
59
|
+
s.carry = model?.carry || [];
|
|
60
|
+
s.losses = model?.losses || [];
|
|
61
|
+
this.lossAck = false;
|
|
62
|
+
this.lossAutoWarned = false;
|
|
63
|
+
this.comments = model?.comments || [];
|
|
64
|
+
this.activeCommentKey = '';
|
|
65
|
+
this.surface.setHTML(model?.html?.trim() ? model.html : '<p><br></p>');
|
|
66
|
+
this.renumberFootnotes();
|
|
67
|
+
const ed = this.editor;
|
|
68
|
+
if (ed?.root) ed.quietly(() => resetPagination(ed.root));
|
|
69
|
+
this.setEditMode(this.editMode);
|
|
70
|
+
this.setDirty(false);
|
|
71
|
+
this.refreshStats();
|
|
72
|
+
this.paginate();
|
|
73
|
+
this.renderPanels();
|
|
74
|
+
this.renderHead();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function currentDoc() {
|
|
78
|
+
const s = this.state;
|
|
79
|
+
const base = {
|
|
80
|
+
type: s.sections[s.sections.length - 1]?.type || '',
|
|
81
|
+
page: s.page,
|
|
82
|
+
header: s.header,
|
|
83
|
+
footer: s.footer,
|
|
84
|
+
headerEven: s.headerEven,
|
|
85
|
+
footerEven: s.footerEven,
|
|
86
|
+
headerFirst: s.headerFirst,
|
|
87
|
+
footerFirst: s.footerFirst,
|
|
88
|
+
hfMode: s.hfMode,
|
|
89
|
+
pageNumbers: s.pageNumbers,
|
|
90
|
+
watermark: s.watermark,
|
|
91
|
+
};
|
|
92
|
+
const list = s.sections.slice(0, -1);
|
|
93
|
+
list.push(base);
|
|
94
|
+
return {
|
|
95
|
+
html: this.surface.flush() ?? '',
|
|
96
|
+
sections: list,
|
|
97
|
+
comments: this.comments,
|
|
98
|
+
footnotes: s.footnotes,
|
|
99
|
+
endnotes: s.endnotes,
|
|
100
|
+
props: s.props,
|
|
101
|
+
defaultTabStop: s.defaultTabStop,
|
|
102
|
+
compressPunctuation: s.compressPunctuation === true,
|
|
103
|
+
carry: s.carry,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function hasContent(raw) {
|
|
108
|
+
const probe = document.createElement('div');
|
|
109
|
+
probe.innerHTML = raw || '';
|
|
110
|
+
return Boolean(probe.textContent.trim() || probe.querySelector(`img,table,hr,.${DOC_CLASS.PAGEBREAK}`));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const docMethods = { onDocUpdate, setDirty, scheduleAutosave, applyDoc, currentDoc, hasContent };
|
package/src/app/word/edit.js
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
// [文件] gbdocx·word·edit —— 编辑动作 —— 命令执行、编辑模式、统计、图片/链接/表格插入
|
|
2
|
-
|
|
3
|
-
// [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
|
|
4
|
-
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
-
|
|
6
|
-
import { countRevisions } from '../../kernel/editor/engine/revision.js';
|
|
7
|
-
import { notify } from '../../kernel/notify.js';
|
|
8
|
-
import { openMenu } from '../../ui/context-menu.js';
|
|
9
|
-
function run(name, ...args) {
|
|
10
|
-
const ed = this.editor;
|
|
11
|
-
if (!ed || ed.destroyed || this.editMode === 'readonly') return false;
|
|
12
|
-
ed.focus();
|
|
13
|
-
return ed.exec(name, ...args);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function setEditMode(mode) {
|
|
17
|
-
this.editMode = mode;
|
|
18
|
-
this.surface.setEditable(mode !== 'readonly');
|
|
19
|
-
this.surface.setSuggesting(mode === 'suggest');
|
|
20
|
-
this.statusBar.setMode(mode);
|
|
21
|
-
this.ribbon?.refresh();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function refreshStats() {
|
|
25
|
-
const root = this.editor?.root;
|
|
26
|
-
this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
|
|
27
|
-
this.suggestionCount = countRevisions(root);
|
|
28
|
-
this.statusBar.setSuggestions(this.suggestionCount);
|
|
29
|
-
if (this.flags.catalog) this.refreshCatalog();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function pickImage() {
|
|
33
|
-
if (this.editMode === 'readonly') return;
|
|
34
|
-
this.imageInput.click();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function onImagePicked() {
|
|
38
|
-
const files = Array.from(this.imageInput.files || []);
|
|
39
|
-
this.imageInput.value = '';
|
|
40
|
-
if (!files.length) return;
|
|
41
|
-
this.editor?.focus();
|
|
42
|
-
this.surface.insertImages(files);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function insertLink() {
|
|
46
|
-
if (this.editMode === 'readonly') return;
|
|
47
|
-
if (!this.editorState.hasSelection) return notify.warning('请先选择要加链接的文字');
|
|
48
|
-
this.editor.focus();
|
|
49
|
-
this.prompt.openAt(this.titleEl, {
|
|
50
|
-
title: '链接地址',
|
|
51
|
-
placeholder: 'https://…(留空取消链接)',
|
|
52
|
-
maxlength: 500,
|
|
53
|
-
required: false,
|
|
54
|
-
submit: (value) => {
|
|
55
|
-
if (this.run('setLink', value)) return true;
|
|
56
|
-
notify.warning('链接地址仅支持 http/https/mailto/tel/ftp 协议');
|
|
57
|
-
return false;
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function openTableSpec(e) {
|
|
63
|
-
if (this.editMode === 'readonly') return;
|
|
64
|
-
const anchor = e?.currentTarget || e?.target || this.headEl;
|
|
65
|
-
openMenu({
|
|
66
|
-
anchor,
|
|
67
|
-
items: [2, 3, 4, 5].map((n) => ({ label: `${n} × ${n} 表格`, onClick: () => this.run('insertTable', { rows: n, cols: n, withHeaderRow: true }) })),
|
|
68
|
-
placement: 'bottom-start',
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export const editMethods = { run, setEditMode, refreshStats, pickImage, onImagePicked, insertLink, openTableSpec };
|
|
1
|
+
// [文件] gbdocx·word·edit —— 编辑动作 —— 命令执行、编辑模式、统计、图片/链接/表格插入
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
|
|
4
|
+
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
+
|
|
6
|
+
import { countRevisions } from '../../kernel/editor/engine/revision.js';
|
|
7
|
+
import { notify } from '../../kernel/notify.js';
|
|
8
|
+
import { openMenu } from '../../ui/context-menu.js';
|
|
9
|
+
function run(name, ...args) {
|
|
10
|
+
const ed = this.editor;
|
|
11
|
+
if (!ed || ed.destroyed || this.editMode === 'readonly') return false;
|
|
12
|
+
ed.focus();
|
|
13
|
+
return ed.exec(name, ...args);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function setEditMode(mode) {
|
|
17
|
+
this.editMode = mode;
|
|
18
|
+
this.surface.setEditable(mode !== 'readonly');
|
|
19
|
+
this.surface.setSuggesting(mode === 'suggest');
|
|
20
|
+
this.statusBar.setMode(mode);
|
|
21
|
+
this.ribbon?.refresh();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function refreshStats() {
|
|
25
|
+
const root = this.editor?.root;
|
|
26
|
+
this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
|
|
27
|
+
this.suggestionCount = countRevisions(root);
|
|
28
|
+
this.statusBar.setSuggestions(this.suggestionCount);
|
|
29
|
+
if (this.flags.catalog) this.refreshCatalog();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function pickImage() {
|
|
33
|
+
if (this.editMode === 'readonly') return;
|
|
34
|
+
this.imageInput.click();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function onImagePicked() {
|
|
38
|
+
const files = Array.from(this.imageInput.files || []);
|
|
39
|
+
this.imageInput.value = '';
|
|
40
|
+
if (!files.length) return;
|
|
41
|
+
this.editor?.focus();
|
|
42
|
+
this.surface.insertImages(files);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function insertLink() {
|
|
46
|
+
if (this.editMode === 'readonly') return;
|
|
47
|
+
if (!this.editorState.hasSelection) return notify.warning('请先选择要加链接的文字');
|
|
48
|
+
this.editor.focus();
|
|
49
|
+
this.prompt.openAt(this.titleEl, {
|
|
50
|
+
title: '链接地址',
|
|
51
|
+
placeholder: 'https://…(留空取消链接)',
|
|
52
|
+
maxlength: 500,
|
|
53
|
+
required: false,
|
|
54
|
+
submit: (value) => {
|
|
55
|
+
if (this.run('setLink', value)) return true;
|
|
56
|
+
notify.warning('链接地址仅支持 http/https/mailto/tel/ftp 协议');
|
|
57
|
+
return false;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function openTableSpec(e) {
|
|
63
|
+
if (this.editMode === 'readonly') return;
|
|
64
|
+
const anchor = e?.currentTarget || e?.target || this.headEl;
|
|
65
|
+
openMenu({
|
|
66
|
+
anchor,
|
|
67
|
+
items: [2, 3, 4, 5].map((n) => ({ label: `${n} × ${n} 表格`, onClick: () => this.run('insertTable', { rows: n, cols: n, withHeaderRow: true }) })),
|
|
68
|
+
placement: 'bottom-start',
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const editMethods = { run, setEditMode, refreshStats, pickImage, onImagePicked, insertLink, openTableSpec };
|
package/src/app/word/file.js
CHANGED
|
@@ -159,4 +159,33 @@ function print() {
|
|
|
159
159
|
window.print();
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
/** 文档头「重命名」:嵌入模式交给宿主(宿主改的是服务端文档名),独立模式改本地文档名 */
|
|
163
|
+
function renameDoc(event) {
|
|
164
|
+
if (this.embed) {
|
|
165
|
+
this.io?.rename?.(event);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this.renamePrompt.openAt(event?.currentTarget, {
|
|
169
|
+
title: '重命名文档',
|
|
170
|
+
placeholder: '文档名称',
|
|
171
|
+
value: this.doc.name,
|
|
172
|
+
submit: (value) => {
|
|
173
|
+
const name = String(value).trim();
|
|
174
|
+
if (!name) return false;
|
|
175
|
+
this.doc.name = name;
|
|
176
|
+
this.renderHead();
|
|
177
|
+
return true;
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** 文档头「下载」:嵌入模式交给宿主(走后端二进制流),独立模式导出当前 .docx */
|
|
183
|
+
function downloadDoc() {
|
|
184
|
+
if (this.embed) {
|
|
185
|
+
this.io?.download?.();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
this.exportDocx();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export const fileMethods = { openFile, newDoc, confirmDiscard, confirmDiscardSync, save, saveAs, confirmLoss, ensureLossAck, exportDocx, exportMarkdown, print, loadBytes, buildBytes, buildMarkdown, renameDoc, downloadDoc };
|
package/src/app/word/hf.js
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
// [文件] gbdocx·word·hf —— 页眉页脚带 —— 槽位读写、显示、页码文案与就地编辑
|
|
2
|
-
|
|
3
|
-
// [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
|
|
4
|
-
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
-
|
|
6
|
-
import { hfUsesEven, hfUsesFirst } from '../../kernel/docx/codec/docx.js';
|
|
7
|
-
import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
|
|
8
|
-
import { pageNumberText as pageNumberTextOf } from '../../kernel/docx/pageGeometry.js';
|
|
9
|
-
function bandSlot(which, p) {
|
|
10
|
-
const mode = this.state.hfMode;
|
|
11
|
-
if (hfUsesFirst(mode) && p === 1) return which === 'header' ? 'headerFirst' : 'footerFirst';
|
|
12
|
-
const even = hfUsesEven(mode) && p % 2 === 0;
|
|
13
|
-
if (which === 'header') return even ? 'headerEven' : 'header';
|
|
14
|
-
return even ? 'footerEven' : 'footer';
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function slotValue(slot) {
|
|
18
|
-
return this.state[slot] ?? '';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function hfTextAt(which, p) {
|
|
22
|
-
return this.slotValue(this.bandSlot(which, p));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function showHf(p, which) {
|
|
26
|
-
if (this.state.hfMode === 'none') return false;
|
|
27
|
-
const raw = String(this.hfTextAt(which, p));
|
|
28
|
-
return raw.replace(/<[^>]*>/g, '').trim() !== '' || /data-fld=/i.test(raw);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function showPageNumber(p) {
|
|
32
|
-
if (!this.state.pageNumbers.enabled || this.state.hfMode === 'none') return false;
|
|
33
|
-
if (hfUsesFirst(this.state.hfMode) && p === 1) return /data-fld="PAGE"/i.test(this.state.footerFirst || '');
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function hfDisplayHtml(which, p) {
|
|
38
|
-
const raw = this.hfTextAt(which, p);
|
|
39
|
-
if (!raw) return '';
|
|
40
|
-
return String(raw).replace(/<span[^>]*data-fld="(PAGE|NUMPAGES|SECTIONPAGES)"[^>]*><\/span>/gi, (_, kind) => {
|
|
41
|
-
if (kind.toUpperCase() === 'PAGE') return this.pageNumberText(p);
|
|
42
|
-
return String(this.pageCount);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function pageNumberText(p) {
|
|
47
|
-
const { format, start } = this.state.pageNumbers;
|
|
48
|
-
// 文案口径(阿拉伯/中文/「- 1 -」短横)只在 pageGeometry 里实现一份,页面浮层与页脚域共用
|
|
49
|
-
return pageNumberTextOf(p, format, start);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function startEditBand(slot, el) {
|
|
53
|
-
if (this.editMode === 'readonly') return;
|
|
54
|
-
this.bandEditSlot = slot;
|
|
55
|
-
el.contentEditable = 'true';
|
|
56
|
-
el.classList.add('gbd-is-editing');
|
|
57
|
-
requestAnimationFrame(() => el.focus());
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function commitBand() {
|
|
61
|
-
const slot = this.bandEditSlot;
|
|
62
|
-
this.bandEditSlot = '';
|
|
63
|
-
if (!slot) return;
|
|
64
|
-
const el = this.root.querySelector('.word-sheet-hf.gbd-is-editing');
|
|
65
|
-
if (el) {
|
|
66
|
-
const clean = cleanHfHtml(el.innerHTML);
|
|
67
|
-
if (clean !== this.slotValue(slot)) {
|
|
68
|
-
this.state[slot] = clean;
|
|
69
|
-
if ((clean.replace(/<[^>]*>/g, '').trim()) && this.state.hfMode === 'none') this.state.hfMode = 'all';
|
|
70
|
-
this.onDocUpdate();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
this.renderBackdrop();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export const hfMethods = { bandSlot, slotValue, hfTextAt, showHf, showPageNumber, hfDisplayHtml, pageNumberText, startEditBand, commitBand };
|
|
1
|
+
// [文件] gbdocx·word·hf —— 页眉页脚带 —— 槽位读写、显示、页码文案与就地编辑
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
|
|
4
|
+
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
|
+
|
|
6
|
+
import { hfUsesEven, hfUsesFirst } from '../../kernel/docx/codec/docx.js';
|
|
7
|
+
import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
|
|
8
|
+
import { pageNumberText as pageNumberTextOf } from '../../kernel/docx/pageGeometry.js';
|
|
9
|
+
function bandSlot(which, p) {
|
|
10
|
+
const mode = this.state.hfMode;
|
|
11
|
+
if (hfUsesFirst(mode) && p === 1) return which === 'header' ? 'headerFirst' : 'footerFirst';
|
|
12
|
+
const even = hfUsesEven(mode) && p % 2 === 0;
|
|
13
|
+
if (which === 'header') return even ? 'headerEven' : 'header';
|
|
14
|
+
return even ? 'footerEven' : 'footer';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function slotValue(slot) {
|
|
18
|
+
return this.state[slot] ?? '';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function hfTextAt(which, p) {
|
|
22
|
+
return this.slotValue(this.bandSlot(which, p));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function showHf(p, which) {
|
|
26
|
+
if (this.state.hfMode === 'none') return false;
|
|
27
|
+
const raw = String(this.hfTextAt(which, p));
|
|
28
|
+
return raw.replace(/<[^>]*>/g, '').trim() !== '' || /data-fld=/i.test(raw);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function showPageNumber(p) {
|
|
32
|
+
if (!this.state.pageNumbers.enabled || this.state.hfMode === 'none') return false;
|
|
33
|
+
if (hfUsesFirst(this.state.hfMode) && p === 1) return /data-fld="PAGE"/i.test(this.state.footerFirst || '');
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hfDisplayHtml(which, p) {
|
|
38
|
+
const raw = this.hfTextAt(which, p);
|
|
39
|
+
if (!raw) return '';
|
|
40
|
+
return String(raw).replace(/<span[^>]*data-fld="(PAGE|NUMPAGES|SECTIONPAGES)"[^>]*><\/span>/gi, (_, kind) => {
|
|
41
|
+
if (kind.toUpperCase() === 'PAGE') return this.pageNumberText(p);
|
|
42
|
+
return String(this.pageCount);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function pageNumberText(p) {
|
|
47
|
+
const { format, start } = this.state.pageNumbers;
|
|
48
|
+
// 文案口径(阿拉伯/中文/「- 1 -」短横)只在 pageGeometry 里实现一份,页面浮层与页脚域共用
|
|
49
|
+
return pageNumberTextOf(p, format, start);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function startEditBand(slot, el) {
|
|
53
|
+
if (this.editMode === 'readonly') return;
|
|
54
|
+
this.bandEditSlot = slot;
|
|
55
|
+
el.contentEditable = 'true';
|
|
56
|
+
el.classList.add('gbd-is-editing');
|
|
57
|
+
requestAnimationFrame(() => el.focus());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function commitBand() {
|
|
61
|
+
const slot = this.bandEditSlot;
|
|
62
|
+
this.bandEditSlot = '';
|
|
63
|
+
if (!slot) return;
|
|
64
|
+
const el = this.root.querySelector('.word-sheet-hf.gbd-is-editing');
|
|
65
|
+
if (el) {
|
|
66
|
+
const clean = cleanHfHtml(el.innerHTML);
|
|
67
|
+
if (clean !== this.slotValue(slot)) {
|
|
68
|
+
this.state[slot] = clean;
|
|
69
|
+
if ((clean.replace(/<[^>]*>/g, '').trim()) && this.state.hfMode === 'none') this.state.hfMode = 'all';
|
|
70
|
+
this.onDocUpdate();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
this.renderBackdrop();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const hfMethods = { bandSlot, slotValue, hfTextAt, showHf, showPageNumber, hfDisplayHtml, pageNumberText, startEditBand, commitBand };
|