@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.
package/package.json CHANGED
@@ -1,48 +1,49 @@
1
- {
2
- "name": "@a200888213/gbdocx",
3
- "version": "0.6.2",
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.3.0",
40
- "guabiutils": "^0.3.0"
41
- },
42
- "devDependencies": {
43
- "jsdom": "^30.1.3"
44
- },
45
- "publishConfig": {
46
- "access": "public"
47
- }
48
- }
1
+ {
2
+ "name": "@a200888213/gbdocx",
3
+ "version": "0.6.3",
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.3.0",
40
+ "guabiutils": "^0.3.0",
41
+ "gbicons": "^0.1.0"
42
+ },
43
+ "devDependencies": {
44
+ "jsdom": "^30.1.3"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ }
49
+ }
package/serve.mjs CHANGED
@@ -1,23 +1,27 @@
1
- // [文件] gbdocx·serve —— 静态服务器(把 gbdocx 与同仓的 gbooxml / guabiutils 一起按 URL 空间供给,免打包免安装)
2
- // 用法:node serve.mjs [端口]
3
- import { fileURLToPath } from 'node:url';
4
- import { startStaticServer } from 'guabiutils/dev-server';
5
-
6
- const ooxmlRoot = fileURLToPath(new URL('../gbooxml/', import.meta.url));
7
- const utilsSrc = fileURLToPath(new URL('../../guabiutils/src/', import.meta.url));
8
- const officeUi = fileURLToPath(new URL('../gbofficeui/', import.meta.url));
9
-
10
- const { origin } = await startStaticServer({
11
- root: fileURLToPath(new URL('.', import.meta.url)),
12
- index: 'index.html',
13
- port: Number(process.argv[2] || process.env.GBDOCX_PORT || 5273),
14
- mounts: [
15
- { prefix: '/vendor/gbooxml/', dir: ooxmlRoot },
16
- { prefix: '/vendor/guabiutils/', dir: utilsSrc },
17
- // gbofficeui:共享文档头模块(src/head.js)与顶栏皮肤(styles/)同属一个包根
18
- { prefix: '/vendor/gbofficeui/', dir: officeUi },
19
- ],
20
- });
21
-
22
- console.log(`gbdocx 运行中:${origin}`);
23
- console.log(`gbooxml 映射自:${ooxmlRoot}`);
1
+ // [文件] gbdocx·serve —— 静态服务器(把 gbdocx 与同仓的 gbooxml / guabiutils 一起按 URL 空间供给,免打包免安装)
2
+ // 用法:node serve.mjs [端口]
3
+ import { fileURLToPath } from 'node:url';
4
+ import { startStaticServer } from 'guabiutils/dev-server';
5
+
6
+ const ooxmlRoot = fileURLToPath(new URL('../gbooxml/', import.meta.url));
7
+ const utilsSrc = fileURLToPath(new URL('../../guabiutils/src/', import.meta.url));
8
+ const officeUi = fileURLToPath(new URL('../gbofficeui/', import.meta.url));
9
+ const gbiconsPkg = fileURLToPath(new URL('../../gbicons/', import.meta.url));
10
+ // lucide:图标库 ESM 产物(workspace 提升到仓根 node_modules)
11
+ const lucideEsm = fileURLToPath(new URL('../../../node_modules/lucide/dist/esm/', import.meta.url));
12
+
13
+ const { origin } = await startStaticServer({
14
+ root: fileURLToPath(new URL('.', import.meta.url)),
15
+ index: 'index.html',
16
+ port: Number(process.argv[2] || process.env.GBDOCX_PORT || 5273),
17
+ mounts: [
18
+ { prefix: '/vendor/gbooxml/', dir: ooxmlRoot },
19
+ { prefix: '/vendor/guabiutils/', dir: utilsSrc },
20
+ // gbofficeui:共享文档头模块(src/head.js)与顶栏皮肤(styles/)同属一个包根
21
+ { prefix: '/vendor/gbofficeui/', dir: officeUi },
22
+ { prefix: '/vendor/gbicons/', dir: gbiconsPkg },
23
+ ],
24
+ });
25
+
26
+ console.log(`gbdocx 运行中:${origin}`);
27
+ console.log(`gbooxml 映射自:${ooxmlRoot}`);
@@ -1,191 +1,191 @@
1
- // [文件] gbdocx·word·file —— 文件动作 —— 新建/打开/保存/另存/导出/打印/丢弃确认
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { downloadBlob } from 'guabiutils';
7
- import { parseDocx } from '../../kernel/docx/codec/docx.js';
8
- import { buildDocxVerified } from '../../kernel/docx/codec/roundtrip.js';
9
- import { docToMarkdown } from '../../kernel/docx/codec/markdown.js';
10
- import { lossText } from '../../kernel/docx/codec/losses.js';
11
- import { notify } from '../../kernel/notify.js';
12
- import { openDocx, saveDocx, saveDocxAs, ensureExt, DOCX_MIME } from '.././file-io.js';
13
- import { MD_MIME } from './shared.js';
14
- async function openFile() {
15
- try {
16
- const picked = await openDocx();
17
- if (!picked) return;
18
- if (this.dirty && !(await this.confirmDiscard('当前文档有未保存的修改,打开将丢弃这些修改。'))) return;
19
- const bytes = picked.bytes;
20
- this.doc = { name: picked.name, handle: picked.handle };
21
- await this.applyDoc(bytes.length ? await parseDocx(bytes) : null);
22
- notify.success(`已打开 ${picked.name}`);
23
- } catch (err) {
24
- notify.error(`打开失败:${err.message || err}`);
25
- }
26
- }
27
-
28
- function newDoc() {
29
- const start = async () => {
30
- this.doc = { name: '新建文档', handle: null };
31
- await this.applyDoc(null);
32
- notify.info('已新建空白文档');
33
- };
34
- if (this.dirty && !this.confirmDiscardSync('当前文档有未保存的修改,新建将丢弃这些修改。')) return;
35
- start();
36
- }
37
-
38
- async function confirmDiscard(message) {
39
- return this.confirmDiscardSync(message);
40
- }
41
-
42
- function confirmDiscardSync(message) {
43
- return window.confirm(`${message}\n\n确定继续吗?`);
44
- }
45
-
46
- async function save(silent = false) {
47
- const model = this.currentDoc();
48
- if (!this.hasContent(model.html)) {
49
- if (!silent) notify.warning('正文为空,未保存');
50
- return false;
51
- }
52
- if (!(await this.ensureLossAck({ silent }))) return false;
53
- if (!this.doc.handle) {
54
- return this.saveAs();
55
- }
56
- this.saving = true;
57
- this.statusBar.setDirty(this.dirty, true);
58
- try {
59
- const bytes = await buildDocxVerified(model);
60
- await saveDocx(bytes, this.doc.name, this.doc.handle);
61
- this.setDirty(false);
62
- if (!silent) notify.success('已保存');
63
- return true;
64
- } catch (err) {
65
- notify.error(`保存失败:${err.message || err}`);
66
- return false;
67
- } finally {
68
- this.saving = false;
69
- this.statusBar.setDirty(this.dirty, false);
70
- }
71
- }
72
-
73
- async function saveAs() {
74
- const model = this.currentDoc();
75
- if (!this.hasContent(model.html)) {
76
- notify.warning('正文为空,未另存');
77
- return false;
78
- }
79
- if (!(await this.ensureLossAck())) return false;
80
- try {
81
- const bytes = await buildDocxVerified(model);
82
- const result = await saveDocxAs(bytes, this.doc.name);
83
- if (!result.saved) return false;
84
- if (result.handle) this.doc = { name: result.name || this.doc.name, handle: result.handle };
85
- else this.doc = { name: result.name || this.doc.name, handle: null };
86
- this.setDirty(false);
87
- this.renderHead();
88
- notify.success('已另存为');
89
- return true;
90
- } catch (err) {
91
- notify.error(`另存失败:${err.message || err}`);
92
- return false;
93
- }
94
- }
95
-
96
- async function confirmLoss() {
97
- const msg = `该文档含有编辑器不支持的内容,保存后将丢失:${lossText(this.state.losses)}。`;
98
- if (this.io?.confirmLoss) return this.io.confirmLoss(msg);
99
- return window.confirm(msg);
100
- }
101
-
102
- /** 嵌入模式:宿主给字节即装载(name/pathText 只用于标题与路径展示,落点由宿主持有) */
103
- async function loadBytes(bytes, name = '文档.docx', pathText = '') {
104
- this.doc = { name, handle: null, pathText };
105
- await this.applyDoc(bytes?.length ? await parseDocx(bytes) : null);
106
- }
107
-
108
- /** 嵌入模式:保存前统一出口——空文/丢失确认都过,返回 null 表示宿主不该写盘 */
109
- async function buildBytes() {
110
- const model = this.currentDoc();
111
- if (!this.hasContent(model.html)) return null;
112
- if (!(await this.ensureLossAck())) return null;
113
- return buildDocxVerified(model);
114
- }
115
-
116
- /** 嵌入模式:Markdown 导出内容(宿主自己决定下载/落盘) */
117
- function buildMarkdown() {
118
- return docToMarkdown(this.currentDoc());
119
- }
120
-
121
- async function ensureLossAck({ silent = false } = {}) {
122
- if (!this.state.losses.length || this.lossAck) return true;
123
- if (silent) {
124
- if (!this.lossAutoWarned) {
125
- this.lossAutoWarned = true;
126
- notify.warning('当前文档含有编辑器不支持的内容,自动保存已暂停;请手动保存并确认丢失清单');
127
- }
128
- return false;
129
- }
130
- if (!(await this.confirmLoss())) return false;
131
- this.lossAck = true;
132
- return true;
133
- }
134
-
135
- async function exportDocx() {
136
- const model = this.currentDoc();
137
- if (!this.hasContent(model.html)) return notify.warning('正文为空,未导出');
138
- if (!(await this.ensureLossAck())) return false;
139
- try {
140
- const bytes = await buildDocxVerified(model);
141
- downloadBlob(new Blob([bytes], { type: DOCX_MIME }), ensureExt(this.doc.name, '.docx'));
142
- } catch (err) {
143
- notify.error(`导出失败:${err.message || err}`);
144
- }
145
- }
146
-
147
- function exportMarkdown() {
148
- const model = this.currentDoc();
149
- if (!this.hasContent(model.html)) return notify.warning('正文为空,未导出');
150
- try {
151
- downloadBlob(new Blob([docToMarkdown(model)], { type: MD_MIME }), ensureExt(this.doc.name, '.md'));
152
- } catch (err) {
153
- notify.error(`导出失败:${err.message || err}`);
154
- }
155
- }
156
-
157
- function print() {
158
- this.findBar.close();
159
- window.print();
160
- }
161
-
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 };
1
+ // [文件] gbdocx·word·file —— 文件动作 —— 新建/打开/保存/另存/导出/打印/丢弃确认
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { downloadBlob } from 'guabiutils';
7
+ import { parseDocx } from '../../kernel/docx/codec/docx.js';
8
+ import { buildDocxVerified } from '../../kernel/docx/codec/roundtrip.js';
9
+ import { docToMarkdown } from '../../kernel/docx/codec/markdown.js';
10
+ import { lossText } from '../../kernel/docx/codec/losses.js';
11
+ import { notify } from '../../kernel/notify.js';
12
+ import { openDocx, saveDocx, saveDocxAs, ensureExt, DOCX_MIME } from '.././file-io.js';
13
+ import { MD_MIME } from './shared.js';
14
+ async function openFile() {
15
+ try {
16
+ const picked = await openDocx();
17
+ if (!picked) return;
18
+ if (this.dirty && !(await this.confirmDiscard('当前文档有未保存的修改,打开将丢弃这些修改。'))) return;
19
+ const bytes = picked.bytes;
20
+ this.doc = { name: picked.name, handle: picked.handle };
21
+ await this.applyDoc(bytes.length ? await parseDocx(bytes) : null);
22
+ notify.success(`已打开 ${picked.name}`);
23
+ } catch (err) {
24
+ notify.error(`打开失败:${err.message || err}`);
25
+ }
26
+ }
27
+
28
+ function newDoc() {
29
+ const start = async () => {
30
+ this.doc = { name: '新建文档', handle: null };
31
+ await this.applyDoc(null);
32
+ notify.info('已新建空白文档');
33
+ };
34
+ if (this.dirty && !this.confirmDiscardSync('当前文档有未保存的修改,新建将丢弃这些修改。')) return;
35
+ start();
36
+ }
37
+
38
+ async function confirmDiscard(message) {
39
+ return this.confirmDiscardSync(message);
40
+ }
41
+
42
+ function confirmDiscardSync(message) {
43
+ return window.confirm(`${message}\n\n确定继续吗?`);
44
+ }
45
+
46
+ async function save(silent = false) {
47
+ const model = this.currentDoc();
48
+ if (!this.hasContent(model.html)) {
49
+ if (!silent) notify.warning('正文为空,未保存');
50
+ return false;
51
+ }
52
+ if (!(await this.ensureLossAck({ silent }))) return false;
53
+ if (!this.doc.handle) {
54
+ return this.saveAs();
55
+ }
56
+ this.saving = true;
57
+ this.statusBar.setDirty(this.dirty, true);
58
+ try {
59
+ const bytes = await buildDocxVerified(model);
60
+ await saveDocx(bytes, this.doc.name, this.doc.handle);
61
+ this.setDirty(false);
62
+ if (!silent) notify.success('已保存');
63
+ return true;
64
+ } catch (err) {
65
+ notify.error(`保存失败:${err.message || err}`);
66
+ return false;
67
+ } finally {
68
+ this.saving = false;
69
+ this.statusBar.setDirty(this.dirty, false);
70
+ }
71
+ }
72
+
73
+ async function saveAs() {
74
+ const model = this.currentDoc();
75
+ if (!this.hasContent(model.html)) {
76
+ notify.warning('正文为空,未另存');
77
+ return false;
78
+ }
79
+ if (!(await this.ensureLossAck())) return false;
80
+ try {
81
+ const bytes = await buildDocxVerified(model);
82
+ const result = await saveDocxAs(bytes, this.doc.name);
83
+ if (!result.saved) return false;
84
+ if (result.handle) this.doc = { name: result.name || this.doc.name, handle: result.handle };
85
+ else this.doc = { name: result.name || this.doc.name, handle: null };
86
+ this.setDirty(false);
87
+ this.renderHead();
88
+ notify.success('已另存为');
89
+ return true;
90
+ } catch (err) {
91
+ notify.error(`另存失败:${err.message || err}`);
92
+ return false;
93
+ }
94
+ }
95
+
96
+ async function confirmLoss() {
97
+ const msg = `该文档含有编辑器不支持的内容,保存后将丢失:${lossText(this.state.losses)}。`;
98
+ if (this.io?.confirmLoss) return this.io.confirmLoss(msg);
99
+ return window.confirm(msg);
100
+ }
101
+
102
+ /** 嵌入模式:宿主给字节即装载(name/pathText 只用于标题与路径展示,落点由宿主持有) */
103
+ async function loadBytes(bytes, name = '文档.docx', pathText = '') {
104
+ this.doc = { name, handle: null, pathText };
105
+ await this.applyDoc(bytes?.length ? await parseDocx(bytes) : null);
106
+ }
107
+
108
+ /** 嵌入模式:保存前统一出口——空文/丢失确认都过,返回 null 表示宿主不该写盘 */
109
+ async function buildBytes() {
110
+ const model = this.currentDoc();
111
+ if (!this.hasContent(model.html)) return null;
112
+ if (!(await this.ensureLossAck())) return null;
113
+ return buildDocxVerified(model);
114
+ }
115
+
116
+ /** 嵌入模式:Markdown 导出内容(宿主自己决定下载/落盘) */
117
+ function buildMarkdown() {
118
+ return docToMarkdown(this.currentDoc());
119
+ }
120
+
121
+ async function ensureLossAck({ silent = false } = {}) {
122
+ if (!this.state.losses.length || this.lossAck) return true;
123
+ if (silent) {
124
+ if (!this.lossAutoWarned) {
125
+ this.lossAutoWarned = true;
126
+ notify.warning('当前文档含有编辑器不支持的内容,自动保存已暂停;请手动保存并确认丢失清单');
127
+ }
128
+ return false;
129
+ }
130
+ if (!(await this.confirmLoss())) return false;
131
+ this.lossAck = true;
132
+ return true;
133
+ }
134
+
135
+ async function exportDocx() {
136
+ const model = this.currentDoc();
137
+ if (!this.hasContent(model.html)) return notify.warning('正文为空,未导出');
138
+ if (!(await this.ensureLossAck())) return false;
139
+ try {
140
+ const bytes = await buildDocxVerified(model);
141
+ downloadBlob(new Blob([bytes], { type: DOCX_MIME }), ensureExt(this.doc.name, '.docx'));
142
+ } catch (err) {
143
+ notify.error(`导出失败:${err.message || err}`);
144
+ }
145
+ }
146
+
147
+ function exportMarkdown() {
148
+ const model = this.currentDoc();
149
+ if (!this.hasContent(model.html)) return notify.warning('正文为空,未导出');
150
+ try {
151
+ downloadBlob(new Blob([docToMarkdown(model)], { type: MD_MIME }), ensureExt(this.doc.name, '.md'));
152
+ } catch (err) {
153
+ notify.error(`导出失败:${err.message || err}`);
154
+ }
155
+ }
156
+
157
+ function print() {
158
+ this.findBar.close();
159
+ window.print();
160
+ }
161
+
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 };