@a200888213/gbdocx 0.3.0 → 0.5.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 +4 -3
- package/index.html +5 -0
- package/package.json +48 -47
- package/serve.mjs +3 -1
- package/src/app/word/edit.js +2 -2
- package/src/app/word/layout.js +5 -2
- package/src/app/word/menus.js +26 -0
- package/src/app/word/view.js +31 -1
- package/src/app/word-app.js +36 -21
- package/src/kernel/docx/menus.js +144 -7
- package/src/kernel/docx/pageGeometry.js +2 -0
- package/src/kernel/editor/engine/blocksStructure.js +19 -1
- package/src/kernel/editor/engine/commands.js +1 -0
- package/src/kernel/editor/engine/dom.js +42 -18
- package/src/kernel/editor/engine/tableInteraction.js +17 -0
- package/src/kernel/editor/engine/tableOps.js +1 -5
- package/src/surface/editor-surface.js +131 -0
- package/src/ui/context-menu.js +4 -1
- package/src/ui/icons.js +99 -2
- package/src/ui/ribbon.js +485 -0
- package/src/ui/toolbar.js +2 -169
- package/styles/app.css +139 -123
- package/styles/tokens.css +5 -5
- package/styles/word-panel.css +9 -49
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ npm start # node serve.mjs,默认 http://localhost:5273/
|
|
|
36
36
|
|
|
37
37
|
页面本身只有 `index.html`,它加载四个样式表(顺序有意义):`tokens.css` → `editor-content.css` → `word-panel.css` → `app.css`。自己装配页面时请全部引入,否则正文排版和面板都会走形。
|
|
38
38
|
|
|
39
|
-
`index.html` 里有一条 importmap,把裸标识符 `gbooxml` / `guabiutils` 映射到 `/vendor/*`,而 `serve.mjs` 把 `/vendor/gbooxml/*`、`/vendor/guabiutils/*`
|
|
39
|
+
`index.html` 里有一条 importmap,把裸标识符 `gbooxml` / `guabiutils` 映射到 `/vendor/*`,而 `serve.mjs` 把 `/vendor/gbooxml/*`、`/vendor/gbofficeui/*` 指向 `packages/gboffice/` 下的同级源码,把 `/vendor/guabiutils/*` 指向仓库的 `packages/guabiutils/src`。这意味着这个自带 demo 只在仓库的 monorepo 布局下能跑;从 npm 单独安装后要用 demo 页面,请自行把 `gbooxml` 映射到 `node_modules/gbooxml/src/index.js`,并按下方顺序引入四份样式(顶栏皮肤来自 `gbofficeui`)。
|
|
40
40
|
|
|
41
41
|
## 嵌入到自己的页面
|
|
42
42
|
|
|
@@ -46,6 +46,7 @@ import '@a200888213/gbdocx/styles/tokens.css';
|
|
|
46
46
|
import '@a200888213/gbdocx/styles/editor-content.css';
|
|
47
47
|
import '@a200888213/gbdocx/styles/word-panel.css';
|
|
48
48
|
import '@a200888213/gbdocx/styles/app.css';
|
|
49
|
+
import 'gbofficeui/styles/office-chrome.css'; // 菜单栏/文档头/功能区/状态栏共享皮肤
|
|
49
50
|
|
|
50
51
|
const app = new WordApp(document.getElementById('app'));
|
|
51
52
|
await app.start();
|
|
@@ -81,7 +82,7 @@ app.setReadOnly(false); // 回到可编辑
|
|
|
81
82
|
app.setMenuBarVisible(true); // 再显示菜单栏
|
|
82
83
|
```
|
|
83
84
|
|
|
84
|
-
只读档下:内核切到 `editMode='readonly'`(正文 `contenteditable` 关闭、`run()` 拒绝写命令),工具栏与文件头 `inert`,编辑/插入/表格/格式菜单禁用,状态栏的模式/页边距/分栏开关与查找栏的替换行一并锁定;选区、滚动、复制、查找、缩放、导出与打印仍可用。`hideMenuBar` 只隐藏 `.gbd-menubar-host`
|
|
85
|
+
只读档下:内核切到 `editMode='readonly'`(正文 `contenteditable` 关闭、`run()` 拒绝写命令),工具栏与文件头 `inert`,编辑/插入/表格/格式菜单禁用,状态栏的模式/页边距/分栏开关与查找栏的替换行一并锁定;选区、滚动、复制、查找、缩放、导出与打印仍可用。`hideMenuBar` 只隐藏 `.gbd-menubar-host` 那一行,文件头与正文不受影响。
|
|
85
86
|
|
|
86
87
|
## 只读写文件(codec)
|
|
87
88
|
|
|
@@ -199,7 +200,7 @@ const { parseDocx } = await import('@a200888213/gbdocx/kernel/docx/codec/docx');
|
|
|
199
200
|
2. `new WordApp(mount)` 的挂载元素必须已在文档中,构造期间就会测量布局;先 `append` 再 `new`。
|
|
200
201
|
3. `WordApp` 不对外派发任何事件,它只消费编辑器内核的事件总线。要做联动,取 `app.editor` 后 `app.editor.on('state' | 'selection', fn)`。
|
|
201
202
|
4. 偏好(缩放、面板开关)持久化在 `localStorage['gbdocx.prefs']`,SSR 环境请在客户端实例化。
|
|
202
|
-
5. 自带 demo 的 `/vendor/gbooxml`
|
|
203
|
+
5. 自带 demo 的 `/vendor/gbooxml` 依赖仓库 `packages/gboffice/` 的分组布局,从 npm 单独安装需自己改 importmap。
|
|
203
204
|
|
|
204
205
|
## 许可
|
|
205
206
|
|
package/index.html
CHANGED
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
<link rel="stylesheet" href="/styles/editor-content.css" />
|
|
9
9
|
<link rel="stylesheet" href="/styles/word-panel.css" />
|
|
10
10
|
<link rel="stylesheet" href="/styles/app.css" />
|
|
11
|
+
<link rel="stylesheet" href="/vendor/gbofficeui/office-chrome.css" />
|
|
12
|
+
<style>
|
|
13
|
+
/* 独立运行时撑满视口;嵌入宿主由宿主容器给高度,不依赖这条 */
|
|
14
|
+
html, body { height: 100%; margin: 0; }
|
|
15
|
+
</style>
|
|
11
16
|
<script type="importmap">
|
|
12
17
|
{
|
|
13
18
|
"imports": {
|
package/package.json
CHANGED
|
@@ -1,47 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@a200888213/gbdocx",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "挂逼文档:独立 .docx 编辑器(内核移植自 guabi office/docx 编解码 + 自研 contenteditable 编辑器内核,UI 用原生 HTML 重写),只依赖 gbooxml。",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"main": "./src/app/word-app.js",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": "./src/app/word-app.js",
|
|
10
|
-
"./app": "./src/app/word-app.js",
|
|
11
|
-
"./styles/*": "./styles/*",
|
|
12
|
-
"./*": "./src/*.js",
|
|
13
|
-
"./package.json": "./package.json"
|
|
14
|
-
},
|
|
15
|
-
"files": [
|
|
16
|
-
"src",
|
|
17
|
-
"styles",
|
|
18
|
-
"index.html",
|
|
19
|
-
"serve.mjs",
|
|
20
|
-
"LICENSE"
|
|
21
|
-
],
|
|
22
|
-
"keywords": [
|
|
23
|
-
"docx",
|
|
24
|
-
"document",
|
|
25
|
-
"word",
|
|
26
|
-
"ooxml",
|
|
27
|
-
"editor",
|
|
28
|
-
"contenteditable"
|
|
29
|
-
],
|
|
30
|
-
"engines": {
|
|
31
|
-
"node": ">=18"
|
|
32
|
-
},
|
|
33
|
-
"scripts": {
|
|
34
|
-
"start": "node serve.mjs",
|
|
35
|
-
"test": "node --test test/roundtrip.test.js test/ui.test.js test/mode.test.js"
|
|
36
|
-
},
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"gbooxml": "^0.1.1",
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@a200888213/gbdocx",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "挂逼文档:独立 .docx 编辑器(内核移植自 guabi office/docx 编解码 + 自研 contenteditable 编辑器内核,UI 用原生 HTML 重写),只依赖 gbooxml。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./src/app/word-app.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/app/word-app.js",
|
|
10
|
+
"./app": "./src/app/word-app.js",
|
|
11
|
+
"./styles/*": "./styles/*",
|
|
12
|
+
"./*": "./src/*.js",
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"styles",
|
|
18
|
+
"index.html",
|
|
19
|
+
"serve.mjs",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"docx",
|
|
24
|
+
"document",
|
|
25
|
+
"word",
|
|
26
|
+
"ooxml",
|
|
27
|
+
"editor",
|
|
28
|
+
"contenteditable"
|
|
29
|
+
],
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"start": "node serve.mjs",
|
|
35
|
+
"test": "node --test test/roundtrip.test.js test/ui.test.js test/mode.test.js"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"gbooxml": "^0.1.1",
|
|
39
|
+
"gbofficeui": "^0.2.0",
|
|
40
|
+
"guabiutils": "^0.3.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"jsdom": "^30.1.3"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/serve.mjs
CHANGED
|
@@ -4,7 +4,8 @@ import { fileURLToPath } from 'node:url';
|
|
|
4
4
|
import { startStaticServer } from 'guabiutils/dev-server';
|
|
5
5
|
|
|
6
6
|
const ooxmlRoot = fileURLToPath(new URL('../gbooxml/', import.meta.url));
|
|
7
|
-
const utilsSrc = fileURLToPath(new URL('
|
|
7
|
+
const utilsSrc = fileURLToPath(new URL('../../guabiutils/src/', import.meta.url));
|
|
8
|
+
const officeChrome = fileURLToPath(new URL('../gbofficeui/styles/', import.meta.url));
|
|
8
9
|
|
|
9
10
|
const { origin } = await startStaticServer({
|
|
10
11
|
root: fileURLToPath(new URL('.', import.meta.url)),
|
|
@@ -13,6 +14,7 @@ const { origin } = await startStaticServer({
|
|
|
13
14
|
mounts: [
|
|
14
15
|
{ prefix: '/vendor/gbooxml/', dir: ooxmlRoot },
|
|
15
16
|
{ prefix: '/vendor/guabiutils/', dir: utilsSrc },
|
|
17
|
+
{ prefix: '/vendor/gbofficeui/', dir: officeChrome },
|
|
16
18
|
],
|
|
17
19
|
});
|
|
18
20
|
|
package/src/app/word/edit.js
CHANGED
|
@@ -18,7 +18,7 @@ function setEditMode(mode) {
|
|
|
18
18
|
this.surface.setEditable(mode !== 'readonly');
|
|
19
19
|
this.surface.setSuggesting(mode === 'suggest');
|
|
20
20
|
this.statusBar.setMode(mode);
|
|
21
|
-
this.
|
|
21
|
+
this.ribbon?.refresh();
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
function refreshStats() {
|
|
@@ -61,7 +61,7 @@ function insertLink() {
|
|
|
61
61
|
|
|
62
62
|
function openTableSpec(e) {
|
|
63
63
|
if (this.editMode === 'readonly') return;
|
|
64
|
-
const anchor = e?.currentTarget || e?.target || this.
|
|
64
|
+
const anchor = e?.currentTarget || e?.target || this.headEl;
|
|
65
65
|
openMenu({
|
|
66
66
|
anchor,
|
|
67
67
|
items: [2, 3, 4, 5].map((n) => ({ label: `${n} × ${n} 表格`, onClick: () => this.run('insertTable', { rows: n, cols: n, withHeaderRow: true }) })),
|
package/src/app/word/layout.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
|
|
4
4
|
// 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
|
|
5
5
|
|
|
6
|
-
import { pageGeometry, layoutOf, GUTTER, fromSheetTop, pageNumberStyle, bandStyle, sheetStyle } from '../../kernel/docx/pageGeometry.js';
|
|
6
|
+
import { pageGeometry, layoutOf, GUTTER, SHEET_TOP_GAP, fromSheetTop, pageNumberStyle, bandStyle, sheetStyle } from '../../kernel/docx/pageGeometry.js';
|
|
7
7
|
import { repaginate } from '../../kernel/docx/pagination.js';
|
|
8
8
|
import { savePrefs } from './shared.js';
|
|
9
9
|
function geo() { return pageGeometry(this.state.page); }
|
|
@@ -53,6 +53,7 @@ function applyEditorGeometry() {
|
|
|
53
53
|
const geo = this.geo();
|
|
54
54
|
const layout = this.layout();
|
|
55
55
|
this.editorWrap.style.zoom = String(this.zoom / 100);
|
|
56
|
+
this.editorWrap.style.setProperty('--gbd-wd-sheet-top', `${SHEET_TOP_GAP}px`);
|
|
56
57
|
const style = {
|
|
57
58
|
'--gbd-re-content-w': `${geo.w}mm`,
|
|
58
59
|
'--gbd-re-content-m': '0 auto',
|
|
@@ -145,7 +146,9 @@ function refreshScroll() {
|
|
|
145
146
|
const pageH = this.layout().pageH;
|
|
146
147
|
if (!pageH) return;
|
|
147
148
|
const step = pageH + GUTTER;
|
|
148
|
-
|
|
149
|
+
// 第一页不从滚动原点起:扣掉纸层顶部留白再折算页序,否则页数会提前一页翻
|
|
150
|
+
const offset = this.surface.scrollEl.scrollTop - SHEET_TOP_GAP;
|
|
151
|
+
this.visiblePage = Math.max(1, Math.min(this.pageCount, Math.floor(Math.max(0, offset) / step) + 1));
|
|
149
152
|
this.statusBar.setPage(this.visiblePage, this.pageCount);
|
|
150
153
|
}
|
|
151
154
|
|
package/src/app/word/menus.js
CHANGED
|
@@ -51,6 +51,19 @@ function menuCtx() {
|
|
|
51
51
|
get align() { return app.editorState?.textAlign; },
|
|
52
52
|
get lineHeight() { return app.editorState?.lineHeight; },
|
|
53
53
|
get firstLineIndent() { return app.editorState?.firstLineIndent; },
|
|
54
|
+
get bold() { return app.editorState?.bold; },
|
|
55
|
+
get italic() { return app.editorState?.italic; },
|
|
56
|
+
get underline() { return app.editorState?.underline; },
|
|
57
|
+
get strike() { return app.editorState?.strike; },
|
|
58
|
+
get superscript() { return app.editorState?.superscript; },
|
|
59
|
+
get subscript() { return app.editorState?.subscript; },
|
|
60
|
+
get fontSize() { return app.editorState?.fontSize; },
|
|
61
|
+
get fontFamily() { return app.editorState?.fontFamily; },
|
|
62
|
+
get color() { return app.editorState?.color; },
|
|
63
|
+
get backgroundColor() { return app.editorState?.backgroundColor; },
|
|
64
|
+
get bulletList() { return app.editorState?.bulletList; },
|
|
65
|
+
get orderedList() { return app.editorState?.orderedList; },
|
|
66
|
+
get taskList() { return app.editorState?.taskList; },
|
|
54
67
|
get inTable() { return app.editorState?.inTable; },
|
|
55
68
|
get hasSelection() { return app.editorState?.hasSelection; },
|
|
56
69
|
get columns() { return app.state.page.columns || 1; },
|
|
@@ -132,6 +145,19 @@ function menuCtx() {
|
|
|
132
145
|
align: (v) => this.run('setBlockAlign', v),
|
|
133
146
|
setLineHeight: (v) => this.run('setBlockLineHeight', v),
|
|
134
147
|
firstLine: () => this.run('toggleFirstLineIndent'),
|
|
148
|
+
bold: () => this.run('toggleBold'),
|
|
149
|
+
italic: () => this.run('toggleItalic'),
|
|
150
|
+
underline: () => this.run('toggleUnderline'),
|
|
151
|
+
strike: () => this.run('toggleStrike'),
|
|
152
|
+
superscript: () => this.run('toggleSuperscript'),
|
|
153
|
+
subscript: () => this.run('toggleSubscript'),
|
|
154
|
+
setFontFamily: (v) => this.run('setFontFamily', v),
|
|
155
|
+
setFontSize: (v) => this.run('setFontSize', v),
|
|
156
|
+
setColor: (v) => this.run('setColor', v),
|
|
157
|
+
unsetColor: () => this.run('unsetColor'),
|
|
158
|
+
setHighlight: (v) => this.run('setBackgroundColor', v),
|
|
159
|
+
unsetHighlight: () => this.run('unsetBackgroundColor'),
|
|
160
|
+
indent: (command) => this.run(command),
|
|
135
161
|
setHfMode: (mode) => { this.state.hfMode = mode; this.onDocUpdate(); },
|
|
136
162
|
list: (command) => this.run(command),
|
|
137
163
|
clearFormat: () => this.run('clearFormat'),
|
package/src/app/word/view.js
CHANGED
|
@@ -6,6 +6,35 @@
|
|
|
6
6
|
import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
|
|
7
7
|
import { openColumnDialog as openColumnDialogUI, openMarginDialog as openMarginDialogUI, openPageSetupDialog as openPageSetupDialogUI } from '../../ui/dialogs.js';
|
|
8
8
|
import { savePrefs } from './shared.js';
|
|
9
|
+
|
|
10
|
+
/** 预览模式:只读 + 收起编辑界面;按钮留在文件头行原位,再点或按 Esc 退出 */
|
|
11
|
+
function setPreview(on) {
|
|
12
|
+
const next = !!on;
|
|
13
|
+
if (next === this.preview) return;
|
|
14
|
+
if (next) {
|
|
15
|
+
this.previewRestore = { readOnly: this.readOnly, mode: this.editMode, menuBar: this.menuBarVisible };
|
|
16
|
+
this.preview = true;
|
|
17
|
+
this.root.classList.add('gb-is-preview');
|
|
18
|
+
this.previewBtn.setAttribute('aria-pressed', 'true');
|
|
19
|
+
this.previewBtn.title = '退出预览';
|
|
20
|
+
this.previewBtn.querySelector('span').textContent = '退出预览';
|
|
21
|
+
this.setReadOnly(true);
|
|
22
|
+
this.setMenuBarVisible(false);
|
|
23
|
+
} else {
|
|
24
|
+
const restore = this.previewRestore || { readOnly: false, mode: 'edit', menuBar: true };
|
|
25
|
+
this.preview = false;
|
|
26
|
+
this.root.classList.remove('gb-is-preview');
|
|
27
|
+
this.previewBtn.setAttribute('aria-pressed', 'false');
|
|
28
|
+
this.previewBtn.title = '预览模式';
|
|
29
|
+
this.previewBtn.querySelector('span').textContent = '预览';
|
|
30
|
+
this.setReadOnly(restore.readOnly);
|
|
31
|
+
if (!restore.readOnly) this.setEditMode(restore.mode);
|
|
32
|
+
this.setMenuBarVisible(restore.menuBar);
|
|
33
|
+
this.previewRestore = null;
|
|
34
|
+
}
|
|
35
|
+
this.previewBtn.blur();
|
|
36
|
+
}
|
|
37
|
+
|
|
9
38
|
function setView(key, on) {
|
|
10
39
|
const next = on === undefined ? !this.flags[key] : !!on;
|
|
11
40
|
this.flags = { ...this.flags, [key]: next };
|
|
@@ -24,6 +53,7 @@ function applyView() {
|
|
|
24
53
|
if (!hasComments && this.commentsPanel.el.isConnected) this.commentsPanel.el.remove();
|
|
25
54
|
if (hasCatalog) this.refreshCatalog();
|
|
26
55
|
this.renderPanels();
|
|
56
|
+
this.ribbon?.refresh();
|
|
27
57
|
}
|
|
28
58
|
|
|
29
59
|
function renderPanels() {
|
|
@@ -85,4 +115,4 @@ function openColumnDialog() {
|
|
|
85
115
|
});
|
|
86
116
|
}
|
|
87
117
|
|
|
88
|
-
export const viewMethods = { setView, applyView, renderPanels, renderHead, openPageSetupDialog, openMarginDialog, openColumnDialog };
|
|
118
|
+
export const viewMethods = { setView, applyView, renderPanels, renderHead, openPageSetupDialog, openMarginDialog, openColumnDialog, setPreview };
|
package/src/app/word-app.js
CHANGED
|
@@ -18,7 +18,8 @@ import { notify } from '../kernel/notify.js';
|
|
|
18
18
|
import { useKeyboard, KEY_PRIORITY } from '../kernel/keyboard.js';
|
|
19
19
|
import { buildMenuBar } from '../ui/menubar.js';
|
|
20
20
|
import { closeAll } from '../ui/popup.js';
|
|
21
|
-
import {
|
|
21
|
+
import { buildRibbon } from '../ui/ribbon.js';
|
|
22
|
+
import { icon } from '../ui/icons.js';
|
|
22
23
|
import { StatusBar } from '../ui/statusbar.js';
|
|
23
24
|
import { CatalogPanel, CommentsPanel } from '../ui/panels.js';
|
|
24
25
|
import { PromptPopover } from '../ui/prompt.js';
|
|
@@ -83,6 +84,8 @@ export class WordApp {
|
|
|
83
84
|
this.pageCount = 1;
|
|
84
85
|
this.visiblePage = 1;
|
|
85
86
|
this.editMode = 'edit';
|
|
87
|
+
this.preview = false;
|
|
88
|
+
this.previewRestore = null;
|
|
86
89
|
this.dirty = false;
|
|
87
90
|
this.saving = false;
|
|
88
91
|
this.suggestionCount = 0;
|
|
@@ -111,17 +114,18 @@ export class WordApp {
|
|
|
111
114
|
root.className = 'word-app word-skin';
|
|
112
115
|
if (this.embed) root.classList.add('gbd-is-embed');
|
|
113
116
|
root.innerHTML = `
|
|
114
|
-
<div class="
|
|
115
|
-
|
|
116
|
-
<span class="
|
|
117
|
-
<span class="word-head__path"></span>
|
|
117
|
+
<div class="gbo-head word-head">
|
|
118
|
+
<span class="gbo-head__title"></span>
|
|
119
|
+
<span class="gbo-head__sub"></span>
|
|
118
120
|
<span class="word-head__dirty" hidden>●</span>
|
|
119
|
-
<span class="
|
|
120
|
-
<
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
<span class="gbo-head__spacer"></span>
|
|
122
|
+
<span class="gbo-head__actions">
|
|
123
|
+
<label class="gbo-head__auto"><input type="checkbox" class="gbo-head__autosave" />自动保存</label>
|
|
124
|
+
<button type="button" class="gbo-head__btn gbo-head__btn--primary" data-act="save">保存</button>
|
|
125
|
+
</span>
|
|
126
|
+
<button type="button" class="gbo-head__btn gbo-head__preview" data-act="preview" title="预览模式" aria-pressed="false">${icon('eye')}<span>预览</span></button>
|
|
124
127
|
</div>
|
|
128
|
+
<div class="gbd-menubar-host"></div>
|
|
125
129
|
<div class="gbd-toolbar-host"></div>
|
|
126
130
|
<div class="word-main">
|
|
127
131
|
<div class="word-editor"><div class="word-editor-mount"></div></div>
|
|
@@ -134,12 +138,15 @@ export class WordApp {
|
|
|
134
138
|
this.menubarHost = root.querySelector('.gbd-menubar-host');
|
|
135
139
|
this.headEl = root.querySelector('.word-head');
|
|
136
140
|
this.toolbarHost = root.querySelector('.gbd-toolbar-host');
|
|
137
|
-
this.
|
|
141
|
+
this.headActionsEl = root.querySelector('.gbo-head__actions');
|
|
142
|
+
// 预览按钮不随只读被 inert:它要能在预览档里继续点「退出预览」
|
|
143
|
+
this.guard = createReadOnlyGuard(root, { editZones: () => [this.headActionsEl, this.toolbarHost] });
|
|
138
144
|
this.editorWrap = root.querySelector('.word-editor');
|
|
139
|
-
this.titleEl = root.querySelector('.
|
|
140
|
-
this.pathEl = root.querySelector('.
|
|
145
|
+
this.titleEl = root.querySelector('.gbo-head__title');
|
|
146
|
+
this.pathEl = root.querySelector('.gbo-head__sub');
|
|
141
147
|
this.dirtyEl = root.querySelector('.word-head__dirty');
|
|
142
|
-
this.autosaveEl = root.querySelector('.
|
|
148
|
+
this.autosaveEl = root.querySelector('.gbo-head__autosave');
|
|
149
|
+
this.previewBtn = root.querySelector('[data-act="preview"]');
|
|
143
150
|
|
|
144
151
|
this.catalogPanel = new CatalogPanel((key) => this.locateCatalog(key));
|
|
145
152
|
this.commentsPanel = new CommentsPanel({
|
|
@@ -161,17 +168,22 @@ export class WordApp {
|
|
|
161
168
|
});
|
|
162
169
|
this.editorState = this.editor.state();
|
|
163
170
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
getState: () => this.editorState,
|
|
171
|
+
// 选项卡栏(WPS 风格):tab 行 + 各页分组工具面板,完全接管原图标工具栏
|
|
172
|
+
this.ribbon = buildRibbon({
|
|
173
|
+
getState: () => ({ ...this.editorState, ...this.menuCtx().state }),
|
|
174
|
+
getCtx: () => this.menuCtx(),
|
|
167
175
|
isDisabled: () => this.editMode === 'readonly',
|
|
176
|
+
run: (name, ...args) => this.run(name, ...args),
|
|
168
177
|
actions: {
|
|
169
178
|
insertTable: (e) => this.openTableSpec(e),
|
|
170
179
|
insertImage: () => this.pickImage(),
|
|
171
180
|
insertLink: () => this.insertLink(),
|
|
172
181
|
},
|
|
182
|
+
find: (replace) => this.findBar.open(!!replace),
|
|
183
|
+
openMargin: () => this.openMarginDialog(),
|
|
184
|
+
openColumn: () => this.openColumnDialog(),
|
|
173
185
|
});
|
|
174
|
-
|
|
186
|
+
this.toolbarHost.appendChild(this.ribbon);
|
|
175
187
|
|
|
176
188
|
this.statusBar = new StatusBar({
|
|
177
189
|
onMode: (m) => this.setEditMode(m),
|
|
@@ -233,9 +245,8 @@ export class WordApp {
|
|
|
233
245
|
return hostAction ? hostAction() : fallback.call(this);
|
|
234
246
|
});
|
|
235
247
|
};
|
|
236
|
-
bind('open', 'open', this.openFile);
|
|
237
248
|
bind('save', 'save', this.save);
|
|
238
|
-
|
|
249
|
+
this.previewBtn.addEventListener('click', () => this.setPreview(!this.preview));
|
|
239
250
|
this.autosaveEl.addEventListener('change', () => {
|
|
240
251
|
const on = this.autosaveEl.checked;
|
|
241
252
|
if (this.embed && this.io?.setAutosave) return this.io.setAutosave(on);
|
|
@@ -276,7 +287,7 @@ export class WordApp {
|
|
|
276
287
|
// 编辑器状态刷新 → 工具栏/状态栏
|
|
277
288
|
const refresh = () => {
|
|
278
289
|
this.editorState = this.editor.state();
|
|
279
|
-
this.
|
|
290
|
+
this.ribbon.refresh();
|
|
280
291
|
this.statusBar.setCaret(this.editorState.line || 0, this.editorState.column || 0);
|
|
281
292
|
};
|
|
282
293
|
this.editor.on('state', refresh);
|
|
@@ -288,6 +299,10 @@ export class WordApp {
|
|
|
288
299
|
// 嵌入模式:非激活页签不消费任何快捷键(多页签同页共存时键盘归属只看 active)
|
|
289
300
|
if (this.isActive && !this.isActive()) return false;
|
|
290
301
|
if (!e.ctrlKey && !e.metaKey) {
|
|
302
|
+
if (e.key === 'Escape' && this.preview) {
|
|
303
|
+
this.setPreview(false);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
291
306
|
if (e.key === 'Escape' && !this.charsPop.hidden) {
|
|
292
307
|
this.charsPop.hidden = true;
|
|
293
308
|
return true;
|
package/src/kernel/docx/menus.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { LINE_HEIGHTS } from '../editor/constants.js';
|
|
11
11
|
import { tableMenuItems as buildTableMenuItems } from '../editor/engine/tableMenu.js';
|
|
12
|
+
import { FONT_FAMILIES, FONT_SIZES } from '../textStyles.js';
|
|
12
13
|
import { HF_MODES, PAGE_NUMBER_ALIGNS, PARA_STYLES } from './constants.js';
|
|
13
14
|
|
|
14
15
|
/** 行距预设(空串 = 跟随正文默认行高;档位复用内核) */
|
|
@@ -25,6 +26,32 @@ const ALIGN_ITEMS = [
|
|
|
25
26
|
{ label: '两端对齐', v: 'justify' },
|
|
26
27
|
];
|
|
27
28
|
|
|
29
|
+
/** 右键菜单色板:常用 12 色,够用又不把子菜单撑出屏幕 */
|
|
30
|
+
const TEXT_PALETTE = [
|
|
31
|
+
{ label: '黑色', value: '#000000' },
|
|
32
|
+
{ label: '深灰', value: '#666666' },
|
|
33
|
+
{ label: '浅灰', value: '#999999' },
|
|
34
|
+
{ label: '白色', value: '#ffffff' },
|
|
35
|
+
{ label: '红色', value: '#ff0000' },
|
|
36
|
+
{ label: '橙色', value: '#ff9900' },
|
|
37
|
+
{ label: '黄色', value: '#ffd966' },
|
|
38
|
+
{ label: '绿色', value: '#38761d' },
|
|
39
|
+
{ label: '青色', value: '#00b0f0' },
|
|
40
|
+
{ label: '蓝色', value: '#2a6bff' },
|
|
41
|
+
{ label: '紫色', value: '#9900ff' },
|
|
42
|
+
{ label: '粉色', value: '#c27ba0' },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
/** 突出显示色板:浅底色,避免正文看不清 */
|
|
46
|
+
const HIGHLIGHT_PALETTE = [
|
|
47
|
+
{ label: '黄色', value: '#fff3b0' },
|
|
48
|
+
{ label: '绿色', value: '#d3f2b3' },
|
|
49
|
+
{ label: '青色', value: '#b3e9e9' },
|
|
50
|
+
{ label: '蓝色', value: '#d0e4f7' },
|
|
51
|
+
{ label: '粉色', value: '#fddede' },
|
|
52
|
+
{ label: '灰色', value: '#e7e9eb' },
|
|
53
|
+
];
|
|
54
|
+
|
|
28
55
|
/** 页眉页脚重复模式 */
|
|
29
56
|
const HF_ITEMS = HF_MODES.map((m) => ({ label: m.label, v: m.value }));
|
|
30
57
|
|
|
@@ -36,20 +63,130 @@ export const VIEW_TOGGLES = [
|
|
|
36
63
|
|
|
37
64
|
/**
|
|
38
65
|
* 正文右键菜单(编辑面非表格区的右键;条目与菜单栏「编辑 / 格式 / 插入」同源,禁各写一份)
|
|
66
|
+
* [设计] 字体 / 段落 / 项目符号和编号 / 更换背景用二级菜单承载,打开即见当前状态勾选;
|
|
67
|
+
* 只挂真实可执行的命令,没有实现的能力(AI 排版、划词翻译、格式刷)不摆假入口。
|
|
39
68
|
* @param {Object} ctx 页面注入的 menuCtx(state/edit/format/insert 与菜单栏同一份)
|
|
40
69
|
*/
|
|
41
70
|
export const textMenuItems = (ctx) => {
|
|
42
71
|
const has = () => Boolean(ctx.state.hasSelection);
|
|
72
|
+
const state = () => ctx.state;
|
|
43
73
|
return [
|
|
44
|
-
{ label: '剪切', shortcut: 'Ctrl+X', disabled: !has(), onClick: ctx.edit.cut },
|
|
45
|
-
{ label: '复制', shortcut: 'Ctrl+C', disabled: !has(), onClick: ctx.edit.copy },
|
|
46
|
-
{ label: '粘贴', shortcut: 'Ctrl+V', onClick: ctx.edit.paste },
|
|
74
|
+
{ label: '剪切', icon: 'cut', shortcut: 'Ctrl+X', disabled: !has(), onClick: ctx.edit.cut },
|
|
75
|
+
{ label: '复制', icon: 'copy', shortcut: 'Ctrl+C', disabled: !has(), onClick: ctx.edit.copy },
|
|
76
|
+
{ label: '粘贴', icon: 'paste', shortcut: 'Ctrl+V', onClick: ctx.edit.paste },
|
|
47
77
|
{ divided: true },
|
|
48
|
-
{
|
|
49
|
-
|
|
78
|
+
{
|
|
79
|
+
label: '字体',
|
|
80
|
+
icon: 'fontCollection',
|
|
81
|
+
children: [
|
|
82
|
+
{
|
|
83
|
+
label: '字体',
|
|
84
|
+
children: FONT_FAMILIES.map((f) => ({
|
|
85
|
+
label: f.label,
|
|
86
|
+
checked: state().fontFamily === f.value,
|
|
87
|
+
onClick: () => ctx.format.setFontFamily(f.value),
|
|
88
|
+
})),
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
label: '字号',
|
|
92
|
+
children: FONT_SIZES.map((v) => ({
|
|
93
|
+
label: String(v),
|
|
94
|
+
checked: state().fontSize === `${v}px`,
|
|
95
|
+
onClick: () => ctx.format.setFontSize(`${v}px`),
|
|
96
|
+
})),
|
|
97
|
+
},
|
|
98
|
+
{ divided: true },
|
|
99
|
+
{ label: '加粗', checked: state().bold, onClick: ctx.format.bold },
|
|
100
|
+
{ label: '斜体', checked: state().italic, onClick: ctx.format.italic },
|
|
101
|
+
{ label: '下划线', checked: state().underline, onClick: ctx.format.underline },
|
|
102
|
+
{ label: '删除线', checked: state().strike, onClick: ctx.format.strike },
|
|
103
|
+
{ label: '上标', checked: state().superscript, onClick: ctx.format.superscript },
|
|
104
|
+
{ label: '下标', checked: state().subscript, onClick: ctx.format.subscript },
|
|
105
|
+
{ divided: true },
|
|
106
|
+
{
|
|
107
|
+
label: '字体颜色',
|
|
108
|
+
icon: 'colorIcon',
|
|
109
|
+
children: [
|
|
110
|
+
{ label: '自动', onClick: ctx.format.unsetColor },
|
|
111
|
+
...TEXT_PALETTE.map((c) => ({
|
|
112
|
+
label: c.label,
|
|
113
|
+
colorDot: c.value,
|
|
114
|
+
checked: state().color === c.value,
|
|
115
|
+
onClick: () => ctx.format.setColor(c.value),
|
|
116
|
+
})),
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
label: '段落',
|
|
123
|
+
icon: 'lineSpacing',
|
|
124
|
+
children: [
|
|
125
|
+
{
|
|
126
|
+
label: '对齐方式',
|
|
127
|
+
children: ALIGN_ITEMS.map((a) => ({
|
|
128
|
+
label: a.label,
|
|
129
|
+
checked: state().align === a.v,
|
|
130
|
+
onClick: () => ctx.format.align(a.v),
|
|
131
|
+
})),
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
label: '行距',
|
|
135
|
+
children: LINE_SPACING_ITEMS.map((s) => ({
|
|
136
|
+
label: s.label,
|
|
137
|
+
checked: state().lineHeight === s.v,
|
|
138
|
+
onClick: () => ctx.format.setLineHeight(s.v),
|
|
139
|
+
})),
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
label: '段落样式',
|
|
143
|
+
children: PARA_STYLES.map((s) => ({
|
|
144
|
+
label: s.label,
|
|
145
|
+
checked: state().blockType === s.value,
|
|
146
|
+
onClick: () => ctx.format.paragraphStyle(s.value),
|
|
147
|
+
})),
|
|
148
|
+
},
|
|
149
|
+
{ divided: true },
|
|
150
|
+
{ label: '首行缩进 2 字符', checked: state().firstLineIndent, onClick: ctx.format.firstLine },
|
|
151
|
+
{ label: '增加缩进', onClick: () => ctx.format.indent('increaseBlockIndent') },
|
|
152
|
+
{ label: '减少缩进', onClick: () => ctx.format.indent('decreaseBlockIndent') },
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
label: '项目符号和编号',
|
|
157
|
+
icon: 'multiLevel',
|
|
158
|
+
children: [
|
|
159
|
+
{ label: '无序列表', checked: state().bulletList, onClick: () => ctx.format.list('toggleBulletList') },
|
|
160
|
+
{ label: '有序列表', checked: state().orderedList, onClick: () => ctx.format.list('toggleOrderedList') },
|
|
161
|
+
{ label: '待办列表', checked: state().taskList, onClick: () => ctx.format.list('toggleTaskList') },
|
|
162
|
+
],
|
|
163
|
+
},
|
|
164
|
+
{ divided: true },
|
|
165
|
+
{
|
|
166
|
+
label: '更换背景',
|
|
167
|
+
icon: 'background',
|
|
168
|
+
children: [
|
|
169
|
+
{
|
|
170
|
+
label: '突出显示',
|
|
171
|
+
icon: 'highlight',
|
|
172
|
+
children: [
|
|
173
|
+
{ label: '无', onClick: ctx.format.unsetHighlight },
|
|
174
|
+
...HIGHLIGHT_PALETTE.map((c) => ({
|
|
175
|
+
label: c.label,
|
|
176
|
+
colorDot: c.value,
|
|
177
|
+
checked: state().backgroundColor === c.value,
|
|
178
|
+
onClick: () => ctx.format.setHighlight(c.value),
|
|
179
|
+
})),
|
|
180
|
+
],
|
|
181
|
+
},
|
|
182
|
+
{ label: '页面水印...', onClick: ctx.insert.watermark },
|
|
183
|
+
],
|
|
184
|
+
},
|
|
185
|
+
{ label: '超链接...', icon: 'linkIcon', shortcut: 'Ctrl+K', disabled: !has(), onClick: ctx.insert.link },
|
|
186
|
+
{ label: '插入批注...', icon: 'comment', disabled: !has(), onClick: ctx.insert.comment },
|
|
50
187
|
{ divided: true },
|
|
51
|
-
{ label: '
|
|
52
|
-
{ label: '
|
|
188
|
+
{ label: '全选', icon: 'selectAll', shortcut: 'Ctrl+A', onClick: ctx.edit.selectAll },
|
|
189
|
+
{ label: '清除格式', icon: 'eraser', disabled: !has(), onClick: ctx.format.clearFormat },
|
|
53
190
|
];
|
|
54
191
|
};
|
|
55
192
|
|
|
@@ -7,6 +7,8 @@ import { clamp } from 'guabiutils';
|
|
|
7
7
|
|
|
8
8
|
/** 纸间缝(px):屏幕态一张张纸分开,打印态由 --wd-gutter 归零连排 */
|
|
9
9
|
export const GUTTER = 14;
|
|
10
|
+
/** 纸层顶部留白(px):屏幕态第一页上沿与滚动原点之间露出画布,纸的上边缘与阴影才看得见(打印态归零) */
|
|
11
|
+
export const SHEET_TOP_GAP = 26;
|
|
10
12
|
/** 页眉页脚距纸张边缘的缺省值(mm):与后端 pgMar 的 720 twips 同为 Word 默认 */
|
|
11
13
|
const DEFAULT_HF_MM = 12.7;
|
|
12
14
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// [文件] web·editor-kernel·blocks·结构操作 —— 复制/删除/移动/缩进出缩/光标拆块(含块收尾 settleBlock)
|
|
2
|
-
import { blockLength, blockOf, charOffset, domPoint, hasBlockBody, isBlockEmpty, isCell, isCodeContent, isRuntimeBlock, isTaskItem, rangeOf, moveChildren, setCaret } from './dom.js';
|
|
2
|
+
import { blockLength, blockOf, charOffset, domPoint, hasBlockBody, isBlockEmpty, isCell, isCodeContent, isRuntimeBlock, isTaskItem, mergeSplitFamily, rangeOf, moveChildren, setCaret, RUNTIME_SPLIT_ATTR } from './dom.js';
|
|
3
3
|
import { copyBlockStyle, liftOneOutOf, listItemOf, liveTarget, makeCheckbox, noteContainerOf, lastContainerBlock, pruneEmpty, quoteOf, releaseListItem, structureTargets, taskContentHost } from './blocks.js';
|
|
4
4
|
import { emptyParagraph } from './blocksInsert.js';
|
|
5
5
|
import { insertHardBreak, insertNewlineInCode } from './blocksEnter.js';
|
|
@@ -72,6 +72,24 @@ export function moveBlockTo(el, ref, before) {
|
|
|
72
72
|
return true;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* 表格拖拽落点:跨页续表先并回首块再搬家
|
|
77
|
+
* [坑] 只搬当前碎片会丢行序:下次重排 flatten 按文档序并回,被搬走的碎片若落到尾块之后,
|
|
78
|
+
* 接续行会被并到错误的表上——必须先整家族并回,用首块整体搬
|
|
79
|
+
* @param {HTMLElement} root 编辑面根
|
|
80
|
+
* @param {HTMLElement} table 命中的表(可能是续表碎片)
|
|
81
|
+
* @param {HTMLElement} ref 落点块
|
|
82
|
+
* @param {boolean} before true=插到 ref 前,false=插到 ref 后
|
|
83
|
+
*/
|
|
84
|
+
export function moveTableTo(root, table, ref, before) {
|
|
85
|
+
if (!table?.isConnected || !ref?.isConnected) return false;
|
|
86
|
+
if (table === ref || table.contains(ref) || ref.contains(table)) return false;
|
|
87
|
+
// 落点还是同一家族的续表碎片:并回后它会消失,等于搬了个空——先拒绝
|
|
88
|
+
const split = table.getAttribute(RUNTIME_SPLIT_ATTR);
|
|
89
|
+
if (split && ref.getAttribute(RUNTIME_SPLIT_ATTR) === split) return false;
|
|
90
|
+
return moveBlockTo(mergeSplitFamily(root, table), ref, before);
|
|
91
|
+
}
|
|
92
|
+
|
|
75
93
|
/* ===================== 列表升降级(Tab / Shift+Tab) ===================== */
|
|
76
94
|
|
|
77
95
|
export function sinkListItem(li) {
|