@a200888213/gbdocx 0.0.0-stage → 0.4.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.
Files changed (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +206 -2
  3. package/index.html +25 -0
  4. package/package.json +48 -6
  5. package/serve.mjs +22 -0
  6. package/src/app/file-io.js +122 -0
  7. package/src/app/main.js +9 -0
  8. package/src/app/word/catalog.js +52 -0
  9. package/src/app/word/comments.js +112 -0
  10. package/src/app/word/doc.js +113 -0
  11. package/src/app/word/edit.js +72 -0
  12. package/src/app/word/file.js +162 -0
  13. package/src/app/word/hf.js +76 -0
  14. package/src/app/word/layout.js +155 -0
  15. package/src/app/word/menus.js +175 -0
  16. package/src/app/word/notes.js +110 -0
  17. package/src/app/word/shared.js +22 -0
  18. package/src/app/word/view.js +117 -0
  19. package/src/app/word-app.js +459 -0
  20. package/src/app/word-sheets.js +101 -0
  21. package/src/kernel/docx/catalog.js +41 -0
  22. package/src/kernel/docx/codec/blocks.js +123 -0
  23. package/src/kernel/docx/codec/carry.js +129 -0
  24. package/src/kernel/docx/codec/comments.js +25 -0
  25. package/src/kernel/docx/codec/docx.js +198 -0
  26. package/src/kernel/docx/codec/docxStyles.js +177 -0
  27. package/src/kernel/docx/codec/fonts.js +195 -0
  28. package/src/kernel/docx/codec/helpers.js +161 -0
  29. package/src/kernel/docx/codec/hfHtml.js +152 -0
  30. package/src/kernel/docx/codec/html.js +739 -0
  31. package/src/kernel/docx/codec/losses.js +420 -0
  32. package/src/kernel/docx/codec/markdown.js +170 -0
  33. package/src/kernel/docx/codec/parts.js +249 -0
  34. package/src/kernel/docx/codec/read.js +603 -0
  35. package/src/kernel/docx/codec/render.js +1156 -0
  36. package/src/kernel/docx/codec/roundtrip.js +236 -0
  37. package/src/kernel/docx/codec/write.js +833 -0
  38. package/src/kernel/docx/comments.js +14 -0
  39. package/src/kernel/docx/constants.js +63 -0
  40. package/src/kernel/docx/menus.js +211 -0
  41. package/src/kernel/docx/pageGeometry.js +123 -0
  42. package/src/kernel/docx/pagination.js +481 -0
  43. package/src/kernel/docx/watermark.js +45 -0
  44. package/src/kernel/docx/wordShortcuts.js +37 -0
  45. package/src/kernel/editor/constants.js +8 -0
  46. package/src/kernel/editor/engine/blocks.js +594 -0
  47. package/src/kernel/editor/engine/blocksEnter.js +109 -0
  48. package/src/kernel/editor/engine/blocksInsert.js +254 -0
  49. package/src/kernel/editor/engine/blocksStructure.js +226 -0
  50. package/src/kernel/editor/engine/commands.js +181 -0
  51. package/src/kernel/editor/engine/dom.js +646 -0
  52. package/src/kernel/editor/engine/editor.js +213 -0
  53. package/src/kernel/editor/engine/history.js +149 -0
  54. package/src/kernel/editor/engine/inline.js +656 -0
  55. package/src/kernel/editor/engine/input.js +554 -0
  56. package/src/kernel/editor/engine/interaction.js +137 -0
  57. package/src/kernel/editor/engine/pointer.js +39 -0
  58. package/src/kernel/editor/engine/revision.js +138 -0
  59. package/src/kernel/editor/engine/schema.js +417 -0
  60. package/src/kernel/editor/engine/search.js +113 -0
  61. package/src/kernel/editor/engine/selection.js +119 -0
  62. package/src/kernel/editor/engine/state.js +118 -0
  63. package/src/kernel/editor/engine/tableClip.js +154 -0
  64. package/src/kernel/editor/engine/tableGrid.js +55 -0
  65. package/src/kernel/editor/engine/tableInteraction.js +388 -0
  66. package/src/kernel/editor/engine/tableMenu.js +250 -0
  67. package/src/kernel/editor/engine/tableOps.js +174 -0
  68. package/src/kernel/editor/engine/tables.js +732 -0
  69. package/src/kernel/editor/overlayGeometry.js +20 -0
  70. package/src/kernel/editor/pasteClean.js +293 -0
  71. package/src/kernel/editor/utils.js +49 -0
  72. package/src/kernel/keyboard.js +114 -0
  73. package/src/kernel/notify.js +54 -0
  74. package/src/kernel/textStyles.js +84 -0
  75. package/src/kernel/util/clipboard.js +89 -0
  76. package/src/kernel/util/imageFile.js +91 -0
  77. package/src/kernel/util/safeValue.js +25 -0
  78. package/src/kernel/util/shortcutKey.js +33 -0
  79. package/src/surface/editor-surface.js +600 -0
  80. package/src/ui/color-panel.js +69 -0
  81. package/src/ui/context-menu.js +130 -0
  82. package/src/ui/dialogs.js +249 -0
  83. package/src/ui/find-bar.js +151 -0
  84. package/src/ui/icons.js +134 -0
  85. package/src/ui/menubar.js +28 -0
  86. package/src/ui/panels.js +115 -0
  87. package/src/ui/popup.js +79 -0
  88. package/src/ui/prompt.js +89 -0
  89. package/src/ui/ribbon.js +485 -0
  90. package/src/ui/special-chars.js +39 -0
  91. package/src/ui/statusbar.js +81 -0
  92. package/src/ui/toolbar.js +32 -0
  93. package/styles/app.css +944 -0
  94. package/styles/editor-content.css +440 -0
  95. package/styles/tokens.css +154 -0
  96. package/styles/word-panel.css +410 -0
@@ -0,0 +1,89 @@
1
+ // [通用:工具] copyText / pasteText / copyRich / readRich —— 剪贴板读写唯一通道(写入含非安全上下文降级,读取不可用时明确报错;copyRich 写 text/plain+text/html 双格式)【强制通道】
2
+ // [文件] web·clipboard·剪贴板读写统一入口
3
+ import { notify as globalNotify } from '../notify.js';
4
+
5
+ /**
6
+ * @param {string} text 待复制内容,空值直接失败返回
7
+ * @param {string} tip 成功提示文案
8
+ * @param {object} notifier 通知实例(调用方模块作用域实例,缺省用全局 notify)
9
+ */
10
+ export async function copyText(text, tip = '已复制', notifier = globalNotify) {
11
+ if (!text) {
12
+ notifier.error('没有可复制的内容');
13
+ return false;
14
+ }
15
+ try {
16
+ await writeClipboard(text);
17
+ notifier.success(tip);
18
+ return true;
19
+ } catch (e) {
20
+ notifier.error(`复制失败:${e?.message || e}`);
21
+ return false;
22
+ }
23
+ }
24
+
25
+ /** 读剪贴板(粘贴):仅安全上下文可用,不可用时抛错由调用方提示 */
26
+ export async function pasteText() {
27
+ if (!navigator.clipboard?.readText) throw new Error('当前访问方式不支持读取剪贴板,请手动粘贴');
28
+ return navigator.clipboard.readText();
29
+ }
30
+
31
+ /**
32
+ * 写富文本剪贴板(text/plain + text/html 双格式):表格等结构化内容复制用。
33
+ * ClipboardItem 不可用(非安全上下文/旧浏览器/权限拒绝)时降级为纯文本写入。
34
+ */
35
+ export async function copyRich(text, html, tip = '已复制', notifier = globalNotify) {
36
+ if (!text && !html) {
37
+ notifier.error('没有可复制的内容');
38
+ return false;
39
+ }
40
+ try {
41
+ await navigator.clipboard.write([
42
+ new ClipboardItem({
43
+ 'text/plain': new Blob([text || ''], { type: 'text/plain' }),
44
+ 'text/html': new Blob([html || text || ''], { type: 'text/html' }),
45
+ }),
46
+ ]);
47
+ notifier.success(tip);
48
+ return true;
49
+ } catch {
50
+ try {
51
+ await writeClipboard(text || '');
52
+ notifier.success(tip);
53
+ return true;
54
+ } catch (e) {
55
+ notifier.error(`复制失败:${e?.message || e}`);
56
+ return false;
57
+ }
58
+ }
59
+ }
60
+
61
+ /** 读剪贴板双格式(粘贴):返回 { text, html }(可能其一为空);剪贴板读取不可用时抛错 */
62
+ export async function readRich() {
63
+ if (!navigator.clipboard?.read) throw new Error('当前访问方式不支持读取剪贴板,请手动粘贴');
64
+ const items = await navigator.clipboard.read();
65
+ let text = '';
66
+ let html = '';
67
+ for (const item of items) {
68
+ if (!html && item.types.includes('text/html')) html = await (await item.getType('text/html')).text();
69
+ if (!text && item.types.includes('text/plain')) text = await (await item.getType('text/plain')).text();
70
+ }
71
+ if (!text && !html) throw new Error('剪贴板没有可粘贴的内容');
72
+ return { text, html };
73
+ }
74
+
75
+ // [坑] navigator.clipboard 只在安全上下文(HTTPS / localhost / 127.0.0.1)存在:经局域网 IP 访问、
76
+ // 或部分桌面壳 webview 下整个对象是 undefined,直调 writeText 即 TypeError。
77
+ // 降级走 execCommand('copy'),须在用户手势的同步调用栈内完成 select+copy(本函数 await 前不跨宏任务)。
78
+ async function writeClipboard(text) {
79
+ if (navigator.clipboard?.writeText) return navigator.clipboard.writeText(text);
80
+ const area = document.createElement('textarea');
81
+ area.value = text;
82
+ area.readOnly = true;
83
+ area.style.cssText = 'position:fixed;top:0;left:0;opacity:0';
84
+ document.body.appendChild(area);
85
+ area.select();
86
+ const copied = document.execCommand('copy');
87
+ area.remove();
88
+ if (!copied) throw new Error('当前浏览器不支持自动复制,请手动选取复制');
89
+ }
@@ -0,0 +1,91 @@
1
+ // [文件] gbdocx·图片工具 —— urlToFile / toJpgFile / fileToDataUrl / extractImageFiles / imageFilesToDataUrls / createBlankCanvas
2
+ // 说明:移植自 guabi core/utils/imageFile,去掉后端前缀依赖(独立应用直取绝对地址)与内嵌上限的外部常量。
3
+
4
+ /** 内嵌正文的图片体积上限(超过则跳过并提示) */
5
+ export const MAX_INLINE_IMAGE_SIZE = 8 * 1024 * 1024;
6
+
7
+ /** 将图片 URL 下载为 File 对象(相对路径按当前页面地址解析) */
8
+ export const urlToFile = async (url, fileName = 'image.jpg') => {
9
+ const res = await fetch(url);
10
+ if (!res.ok) throw new Error(`下载图片失败: ${res.status}`);
11
+ const blob = await res.blob();
12
+ return new File([blob], fileName, { type: blob.type || 'image/jpeg' });
13
+ };
14
+
15
+ /** 图片 File/Blob → jpg File(封面类资源的物理落点恒为 jpg) */
16
+ export const toJpgFile = (source, name = 'image') =>
17
+ new Promise((resolve, reject) => {
18
+ const img = new Image();
19
+ const url = URL.createObjectURL(source);
20
+ img.onload = () => {
21
+ URL.revokeObjectURL(url);
22
+ const canvas = document.createElement('canvas');
23
+ canvas.width = img.width;
24
+ canvas.height = img.height;
25
+ canvas.getContext('2d').drawImage(img, 0, 0);
26
+ canvas.toBlob((blob) => {
27
+ if (blob) resolve(new File([blob], `${name}.jpg`, { type: 'image/jpeg' }));
28
+ else reject(new Error('图片转码失败'));
29
+ }, 'image/jpeg', 0.9);
30
+ };
31
+ img.onerror = () => {
32
+ URL.revokeObjectURL(url);
33
+ reject(new Error('图片加载失败'));
34
+ };
35
+ img.src = url;
36
+ });
37
+
38
+ /** File → dataURL(base64 内嵌正文/预览用) */
39
+ export const fileToDataUrl = (file) =>
40
+ new Promise((resolve, reject) => {
41
+ const reader = new FileReader();
42
+ reader.onload = (event) => resolve(event.target.result);
43
+ reader.onerror = (error) => reject(error);
44
+ reader.readAsDataURL(file);
45
+ });
46
+
47
+ /** 从 DataTransfer 提取图片文件(优先 items,兼容部分浏览器不填 files 的场景) */
48
+ export const extractImageFiles = (dataTransfer) => {
49
+ const files = [];
50
+ if (!dataTransfer) return files;
51
+ if (dataTransfer.items?.length) {
52
+ for (const item of dataTransfer.items) {
53
+ if (item.kind === 'file' && item.type.startsWith('image/')) {
54
+ const file = item.getAsFile?.();
55
+ if (file) files.push(file);
56
+ }
57
+ }
58
+ } else if (dataTransfer.files?.length) {
59
+ files.push(...Array.from(dataTransfer.files).filter((file) => file.type.startsWith('image/')));
60
+ }
61
+ return files;
62
+ };
63
+
64
+ /** 图片文件批量转 dataURL:超限与读取失败各自跳过,调用方只拿成功的那几张 */
65
+ export async function imageFilesToDataUrls(files, onError) {
66
+ const report = onError || (() => {});
67
+ const out = [];
68
+ for (const file of files || []) {
69
+ if (file.size > MAX_INLINE_IMAGE_SIZE) {
70
+ report(`图片「${file.name || '未命名'}」超过 ${Math.floor(MAX_INLINE_IMAGE_SIZE / 1024 / 1024)}MB 上限,未插入`);
71
+ continue;
72
+ }
73
+ try {
74
+ out.push({ file, src: await fileToDataUrl(file) });
75
+ } catch {
76
+ report('图片读取失败');
77
+ }
78
+ }
79
+ return out;
80
+ }
81
+
82
+ /** 生成纯白空白画布的 dataURL */
83
+ export const createBlankCanvas = (width = 800, height = 600) => {
84
+ const canvas = document.createElement('canvas');
85
+ canvas.width = width;
86
+ canvas.height = height;
87
+ const ctx = canvas.getContext('2d');
88
+ ctx.fillStyle = '#ffffff';
89
+ ctx.fillRect(0, 0, width, height);
90
+ return canvas.toDataURL('image/png');
91
+ };
@@ -0,0 +1,25 @@
1
+ // [文件] web·safe value 工具(链接协议策略 / 图片协议白名单 / 字体名净化)
2
+ // [通用:安全] isSafeHref / SAFE_IMG / sanitizeFontName —— 正文取值白名单唯一源(编辑内核与 docx 编解码共用,禁各自再写一份)
3
+ //
4
+ // [口径] 判定机制在 guabiutils(剥控制符 + 协议白名单),本文件只声明**文档这一侧**的策略:
5
+ // 「允许哪些协议」是产品决策,改这里;「怎么判定」改 guabiutils,不要在这里重写。
6
+
7
+ import { DEFAULT_HREF_PROTOCOLS, isSafeHref as isSafeHrefWith } from 'guabiutils';
8
+
9
+ export { sanitizeFontName } from 'guabiutils';
10
+
11
+ /**
12
+ * 文档侧链接协议白名单:默认(http / https / mailto / tel)+ ftp。
13
+ * [口径] 存量 docx 里有 ftp 链接,收紧会让它们退化成不可点文本,所以文档侧显式多放行 ftp;
14
+ * 默认那一段直接引用 guabiutils,基线变了这里跟着变。
15
+ */
16
+ export const DOC_HREF_PROTOCOLS = [...DEFAULT_HREF_PROTOCOLS, 'ftp'];
17
+
18
+ /** 图片协议白名单:链接白名单去掉对图片无意义的 mailto/tel/# —— 粘贴清洗与装载共用这一条 */
19
+ export const SAFE_IMG = /^(?:https?:|data:image\/|blob:|\/)/i;
20
+
21
+ /**
22
+ * 链接地址能否进 href:命中白名单的绝对地址;不含协议冒号的相对形式(应用内路径)放行
23
+ * @param {string} href
24
+ */
25
+ export const isSafeHref = (href) => isSafeHrefWith(href, { protocols: DOC_HREF_PROTOCOLS });
@@ -0,0 +1,33 @@
1
+ // [文件] gbdocx·快捷键键位归一 —— eventComboKey(键盘事件 → 组合串统一口径)
2
+ // 说明:移植自 guabi core/utils/shortcutKey,仅保留内核输入层需要的组合串归一;去掉键位展示与配置比对族。
3
+
4
+ /** 平台主修饰键判定:mac 上 Mod 指 ⌘,其余平台指 Ctrl */
5
+ function isMacPlatform() {
6
+ if (typeof navigator === 'undefined') return false;
7
+ return /Mac|iP(hone|ad|od)/.test(`${navigator.userAgent || ''} ${navigator.platform || ''}`);
8
+ }
9
+
10
+ /** e.code → 归一键形态(数字按物理键位、字母取字面量、其余小写 code) */
11
+ function codeToKey(code) {
12
+ const s = String(code || '');
13
+ if (/^Digit[0-9]$/.test(s)) return `digit${s.slice(5)}`;
14
+ if (/^Numpad[0-9]$/.test(s)) return s.toLowerCase();
15
+ if (/^Key[A-Z]$/.test(s)) return s.slice(3).toLowerCase();
16
+ return s.toLowerCase();
17
+ }
18
+
19
+ /**
20
+ * 键盘事件 → 组合串(修饰键序恒为 Mod / Alt / Shift,如 'Mod-Shift-z';认不出主键返回 '')。
21
+ * [坑] 主键盘数字必须按 e.code 认:按住上档时 e.key 变成 & ( ),照 e.key 认键会让 Mod+Shift+7/8/9 永远配不上。
22
+ */
23
+ export function eventComboKey(e) {
24
+ const code = codeToKey(e?.code);
25
+ const key = /^digit\d$/.test(code) ? code.slice(5) : (typeof e?.key === 'string' ? e.key.toLowerCase() : '') || (code.length === 1 ? code : '');
26
+ if (!key) return '';
27
+ const parts = [];
28
+ if (isMacPlatform() ? e?.metaKey : e?.ctrlKey) parts.push('Mod');
29
+ if (e?.altKey) parts.push('Alt');
30
+ if (e?.shiftKey) parts.push('Shift');
31
+ parts.push(key);
32
+ return parts.join('-');
33
+ }