@a200888213/gbdocx 0.6.1 → 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/LICENSE +21 -21
- package/README.md +215 -215
- package/index.html +31 -30
- package/package.json +49 -48
- package/serve.mjs +27 -23
- 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 +191 -191
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/menus.js +201 -201
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +159 -159
- package/src/app/word-app.js +487 -487
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/catalog.js +41 -41
- 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/menus.js +348 -348
- 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/docx/wordShortcuts.js +37 -37
- 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/editor.js +213 -213
- 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/search.js +113 -113
- 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/pasteClean.js +293 -293
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/notify.js +54 -54
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/clipboard.js +89 -89
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/safeValue.js +25 -25
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +603 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +135 -133
- package/src/ui/dialogs.js +249 -249
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +245 -137
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/popup.js +79 -79
- 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/app.css +970 -970
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
- package/styles/word-panel.css +410 -410
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·state —— 内核状态快照:工具栏 / 气泡菜单 / 块菜单共用的单一口径(混入 EditorCore.prototype)
|
|
2
|
-
// [设计] 状态由内核一次算出(state()),各 UI 只消费快照,不再各自推测激活态。
|
|
3
|
-
import * as D from './dom.js';
|
|
4
|
-
import * as I from './inline.js';
|
|
5
|
-
import * as B from './blocks.js';
|
|
6
|
-
import { INDENT_MAX_LEVEL } from '../constants.js';
|
|
7
|
-
|
|
8
|
-
/** 状态快照方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
9
|
-
export const stateMethods = {
|
|
10
|
-
inlineSnapshot() {
|
|
11
|
-
const ranges = this.selectionRanges();
|
|
12
|
-
return I.inlineState(this.root, ranges).marks;
|
|
13
|
-
},
|
|
14
|
-
|
|
15
|
-
/** 工具栏 / 气泡菜单 / 块菜单共用的状态快照(单一口径) */
|
|
16
|
-
state() {
|
|
17
|
-
const root = this.root;
|
|
18
|
-
const ranges = this.selectionRanges();
|
|
19
|
-
// 折叠光标时待定标记就是「此处接下来打的字长什么样」,工具栏亮灭必须同源,否则按了加粗按钮不亮
|
|
20
|
-
const marks = (ranges.length && this.pending) || I.inlineState(root, ranges).marks;
|
|
21
|
-
const hosts = B.styleHosts(root, ranges);
|
|
22
|
-
const host = hosts[0] || null;
|
|
23
|
-
const style = host ? B.blockStyleOf(host) : { lineHeight: '', textAlign: null, indentLevel: 0, firstLineIndent: false };
|
|
24
|
-
const block = ranges[0]?.block || null;
|
|
25
|
-
const structure = block ? D.structureTarget(block, root) : null;
|
|
26
|
-
const kind = B.kindOf(structure);
|
|
27
|
-
const listKind = block ? B.listKindOf(block, root) : '';
|
|
28
|
-
const inTable = !!D.tableOf(block || root, root);
|
|
29
|
-
const headingLevel = Number(/^heading([1-6])$/.exec(kind)?.[1]) || 0;
|
|
30
|
-
const text = root.textContent.replace(/[\n]/g, '');
|
|
31
|
-
return {
|
|
32
|
-
ready: true,
|
|
33
|
-
canUndo: this.undoStack.length > 0,
|
|
34
|
-
canRedo: this.redoStack.length > 0,
|
|
35
|
-
bold: marks.bold,
|
|
36
|
-
italic: marks.italic,
|
|
37
|
-
underline: marks.underline,
|
|
38
|
-
strike: marks.strike,
|
|
39
|
-
code: marks.code,
|
|
40
|
-
superscript: marks.superscript,
|
|
41
|
-
subscript: marks.subscript,
|
|
42
|
-
link: Boolean(marks.link),
|
|
43
|
-
linkHref: marks.link || '',
|
|
44
|
-
color: marks.color,
|
|
45
|
-
backgroundColor: marks.backgroundColor,
|
|
46
|
-
fontSize: marks.fontSize,
|
|
47
|
-
fontFamily: marks.fontFamily,
|
|
48
|
-
textStroke: marks.textStroke,
|
|
49
|
-
textShadow: marks.textShadow,
|
|
50
|
-
// 结构标记与建议修改模式(Word 应用消费;普通富文本忽略)
|
|
51
|
-
rev: marks.rev || '',
|
|
52
|
-
cmt: marks.cmt || '',
|
|
53
|
-
suggesting: this.suggesting,
|
|
54
|
-
editable: this.isEditable(),
|
|
55
|
-
blockType: kind === 'title' ? 'title' : headingLevel ? `heading${headingLevel}` : kind === 'codeBlock' || kind === 'blockquote' || kind === 'callout' || kind === 'superBlock' ? kind : 'paragraph',
|
|
56
|
-
headingLevel,
|
|
57
|
-
lineHeight: style.lineHeight,
|
|
58
|
-
textAlign: style.textAlign || 'left',
|
|
59
|
-
textAlignRaw: style.textAlign,
|
|
60
|
-
indentLevel: style.indentLevel,
|
|
61
|
-
firstLineIndent: style.firstLineIndent,
|
|
62
|
-
canIndent: style.indentLevel < INDENT_MAX_LEVEL,
|
|
63
|
-
canOutdent: style.indentLevel > 0,
|
|
64
|
-
blockquote: kind === 'blockquote',
|
|
65
|
-
codeBlock: kind === 'codeBlock',
|
|
66
|
-
bulletList: listKind === 'bulletList',
|
|
67
|
-
orderedList: listKind === 'orderedList',
|
|
68
|
-
taskList: listKind === 'taskList',
|
|
69
|
-
inTable,
|
|
70
|
-
hasSelection: ranges.some((item) => item.end > item.start),
|
|
71
|
-
isEmpty: !text.trim() && !root.querySelector('img'),
|
|
72
|
-
// [设计] line/column 是给状态栏补的纯派生读数:由选区几何与字符偏移即时算出,
|
|
73
|
-
// 不参与任何编辑逻辑(命令/历史/序列化都不读它),故新增字段对既有消费方零影响。
|
|
74
|
-
...this.caretPosition(style),
|
|
75
|
-
};
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 光标位置读数:line = 视觉行号(从 1 起),column = 当前段落内字符偏移(从 1 起,即光标前字符数)
|
|
80
|
-
* [设计] 纯读:只测量不写 DOM;取不到(未挂载 / root 为 null / 无选区 / 无 caretRect)时两者回退 0,绝不抛错
|
|
81
|
-
* @param {{lineHeight:string}} style state() 已算好的块样式(复用,避免重复计算)
|
|
82
|
-
* @returns {{line:number, column:number}}
|
|
83
|
-
*/
|
|
84
|
-
caretPosition(style) {
|
|
85
|
-
const fallback = { line: 0, column: 0 };
|
|
86
|
-
const root = this.root;
|
|
87
|
-
if (!root) return fallback;
|
|
88
|
-
try {
|
|
89
|
-
const rect = this.caretRect();
|
|
90
|
-
const rootRect = root.getBoundingClientRect();
|
|
91
|
-
let line = 0;
|
|
92
|
-
if (rect && rootRect) {
|
|
93
|
-
let lineHeightPx = parseFloat(style?.lineHeight);
|
|
94
|
-
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = parseFloat(getComputedStyle(root).lineHeight);
|
|
95
|
-
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = 16;
|
|
96
|
-
// caretRect().top 与 rootRect.top 同受外层 CSS zoom 影响,相减/相除比值不变,故无需乘除 zoom 系数
|
|
97
|
-
const relTop = rect.top - rootRect.top;
|
|
98
|
-
line = Math.floor(relTop / lineHeightPx) + 1;
|
|
99
|
-
if (line < 1) line = 1;
|
|
100
|
-
}
|
|
101
|
-
const first = this.selectionRanges()[0];
|
|
102
|
-
const column = first ? Math.max(0, Math.round(first.start)) + 1 : 0;
|
|
103
|
-
return { line, column };
|
|
104
|
-
} catch {
|
|
105
|
-
return fallback;
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
syncState() {
|
|
110
|
-
this.emit('state', this.state());
|
|
111
|
-
},
|
|
112
|
-
|
|
113
|
-
/** 块自定义属性(el 缺省=光标所在最外层结构块,与 setBlockAttrs 命令同口径;元素已不在编辑面内返回 null) */
|
|
114
|
-
blockAttrsOf(el = null) {
|
|
115
|
-
const target = (el && B.liveTarget(this.root, el)) || B.currentStructureBlock(this.root);
|
|
116
|
-
return target ? B.readBlockAttrs(target) : null;
|
|
117
|
-
},
|
|
118
|
-
};
|
|
1
|
+
// [文件] web·editor-kernel·engine·state —— 内核状态快照:工具栏 / 气泡菜单 / 块菜单共用的单一口径(混入 EditorCore.prototype)
|
|
2
|
+
// [设计] 状态由内核一次算出(state()),各 UI 只消费快照,不再各自推测激活态。
|
|
3
|
+
import * as D from './dom.js';
|
|
4
|
+
import * as I from './inline.js';
|
|
5
|
+
import * as B from './blocks.js';
|
|
6
|
+
import { INDENT_MAX_LEVEL } from '../constants.js';
|
|
7
|
+
|
|
8
|
+
/** 状态快照方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
9
|
+
export const stateMethods = {
|
|
10
|
+
inlineSnapshot() {
|
|
11
|
+
const ranges = this.selectionRanges();
|
|
12
|
+
return I.inlineState(this.root, ranges).marks;
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
/** 工具栏 / 气泡菜单 / 块菜单共用的状态快照(单一口径) */
|
|
16
|
+
state() {
|
|
17
|
+
const root = this.root;
|
|
18
|
+
const ranges = this.selectionRanges();
|
|
19
|
+
// 折叠光标时待定标记就是「此处接下来打的字长什么样」,工具栏亮灭必须同源,否则按了加粗按钮不亮
|
|
20
|
+
const marks = (ranges.length && this.pending) || I.inlineState(root, ranges).marks;
|
|
21
|
+
const hosts = B.styleHosts(root, ranges);
|
|
22
|
+
const host = hosts[0] || null;
|
|
23
|
+
const style = host ? B.blockStyleOf(host) : { lineHeight: '', textAlign: null, indentLevel: 0, firstLineIndent: false };
|
|
24
|
+
const block = ranges[0]?.block || null;
|
|
25
|
+
const structure = block ? D.structureTarget(block, root) : null;
|
|
26
|
+
const kind = B.kindOf(structure);
|
|
27
|
+
const listKind = block ? B.listKindOf(block, root) : '';
|
|
28
|
+
const inTable = !!D.tableOf(block || root, root);
|
|
29
|
+
const headingLevel = Number(/^heading([1-6])$/.exec(kind)?.[1]) || 0;
|
|
30
|
+
const text = root.textContent.replace(/[\n]/g, '');
|
|
31
|
+
return {
|
|
32
|
+
ready: true,
|
|
33
|
+
canUndo: this.undoStack.length > 0,
|
|
34
|
+
canRedo: this.redoStack.length > 0,
|
|
35
|
+
bold: marks.bold,
|
|
36
|
+
italic: marks.italic,
|
|
37
|
+
underline: marks.underline,
|
|
38
|
+
strike: marks.strike,
|
|
39
|
+
code: marks.code,
|
|
40
|
+
superscript: marks.superscript,
|
|
41
|
+
subscript: marks.subscript,
|
|
42
|
+
link: Boolean(marks.link),
|
|
43
|
+
linkHref: marks.link || '',
|
|
44
|
+
color: marks.color,
|
|
45
|
+
backgroundColor: marks.backgroundColor,
|
|
46
|
+
fontSize: marks.fontSize,
|
|
47
|
+
fontFamily: marks.fontFamily,
|
|
48
|
+
textStroke: marks.textStroke,
|
|
49
|
+
textShadow: marks.textShadow,
|
|
50
|
+
// 结构标记与建议修改模式(Word 应用消费;普通富文本忽略)
|
|
51
|
+
rev: marks.rev || '',
|
|
52
|
+
cmt: marks.cmt || '',
|
|
53
|
+
suggesting: this.suggesting,
|
|
54
|
+
editable: this.isEditable(),
|
|
55
|
+
blockType: kind === 'title' ? 'title' : headingLevel ? `heading${headingLevel}` : kind === 'codeBlock' || kind === 'blockquote' || kind === 'callout' || kind === 'superBlock' ? kind : 'paragraph',
|
|
56
|
+
headingLevel,
|
|
57
|
+
lineHeight: style.lineHeight,
|
|
58
|
+
textAlign: style.textAlign || 'left',
|
|
59
|
+
textAlignRaw: style.textAlign,
|
|
60
|
+
indentLevel: style.indentLevel,
|
|
61
|
+
firstLineIndent: style.firstLineIndent,
|
|
62
|
+
canIndent: style.indentLevel < INDENT_MAX_LEVEL,
|
|
63
|
+
canOutdent: style.indentLevel > 0,
|
|
64
|
+
blockquote: kind === 'blockquote',
|
|
65
|
+
codeBlock: kind === 'codeBlock',
|
|
66
|
+
bulletList: listKind === 'bulletList',
|
|
67
|
+
orderedList: listKind === 'orderedList',
|
|
68
|
+
taskList: listKind === 'taskList',
|
|
69
|
+
inTable,
|
|
70
|
+
hasSelection: ranges.some((item) => item.end > item.start),
|
|
71
|
+
isEmpty: !text.trim() && !root.querySelector('img'),
|
|
72
|
+
// [设计] line/column 是给状态栏补的纯派生读数:由选区几何与字符偏移即时算出,
|
|
73
|
+
// 不参与任何编辑逻辑(命令/历史/序列化都不读它),故新增字段对既有消费方零影响。
|
|
74
|
+
...this.caretPosition(style),
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 光标位置读数:line = 视觉行号(从 1 起),column = 当前段落内字符偏移(从 1 起,即光标前字符数)
|
|
80
|
+
* [设计] 纯读:只测量不写 DOM;取不到(未挂载 / root 为 null / 无选区 / 无 caretRect)时两者回退 0,绝不抛错
|
|
81
|
+
* @param {{lineHeight:string}} style state() 已算好的块样式(复用,避免重复计算)
|
|
82
|
+
* @returns {{line:number, column:number}}
|
|
83
|
+
*/
|
|
84
|
+
caretPosition(style) {
|
|
85
|
+
const fallback = { line: 0, column: 0 };
|
|
86
|
+
const root = this.root;
|
|
87
|
+
if (!root) return fallback;
|
|
88
|
+
try {
|
|
89
|
+
const rect = this.caretRect();
|
|
90
|
+
const rootRect = root.getBoundingClientRect();
|
|
91
|
+
let line = 0;
|
|
92
|
+
if (rect && rootRect) {
|
|
93
|
+
let lineHeightPx = parseFloat(style?.lineHeight);
|
|
94
|
+
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = parseFloat(getComputedStyle(root).lineHeight);
|
|
95
|
+
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = 16;
|
|
96
|
+
// caretRect().top 与 rootRect.top 同受外层 CSS zoom 影响,相减/相除比值不变,故无需乘除 zoom 系数
|
|
97
|
+
const relTop = rect.top - rootRect.top;
|
|
98
|
+
line = Math.floor(relTop / lineHeightPx) + 1;
|
|
99
|
+
if (line < 1) line = 1;
|
|
100
|
+
}
|
|
101
|
+
const first = this.selectionRanges()[0];
|
|
102
|
+
const column = first ? Math.max(0, Math.round(first.start)) + 1 : 0;
|
|
103
|
+
return { line, column };
|
|
104
|
+
} catch {
|
|
105
|
+
return fallback;
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
syncState() {
|
|
110
|
+
this.emit('state', this.state());
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
/** 块自定义属性(el 缺省=光标所在最外层结构块,与 setBlockAttrs 命令同口径;元素已不在编辑面内返回 null) */
|
|
114
|
+
blockAttrsOf(el = null) {
|
|
115
|
+
const target = (el && B.liveTarget(this.root, el)) || B.currentStructureBlock(this.root);
|
|
116
|
+
return target ? B.readBlockAttrs(target) : null;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
@@ -1,154 +1,154 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·tableClip —— 表内格间复制 / 粘贴(Excel 口径:选区 ⇄ 二维数据)
|
|
2
|
-
// [通用:能力] copySelection / clipboardRows / pasteCells —— 表格多格剪贴板的唯一通道
|
|
3
|
-
// [设计] 本文件只做「选区 → 二维数据」与「二维数据 → 选区」两段纯 DOM 变换;
|
|
4
|
-
// 系统剪贴板读写归调用方走 core/utils/clipboard【强制通道】——内核不认识剪贴板。
|
|
5
|
-
// 与 tables.js 的分工:那边管网格结构与行列增删,这边只管数据进出单元格。
|
|
6
|
-
|
|
7
|
-
import { stripRuntimeClasses } from './dom.js';
|
|
8
|
-
import { growTable, toGrid } from './tables.js';
|
|
9
|
-
|
|
10
|
-
/** 格内正文 → 纯文本(br 当换行、图片当占位符;内核 dom.blockText 刻意跳过 br,故此处不能用它) */
|
|
11
|
-
function cellText(el) {
|
|
12
|
-
let text = '';
|
|
13
|
-
const walk = (node) => {
|
|
14
|
-
for (const child of Array.from(node.childNodes)) {
|
|
15
|
-
if (child.nodeType === Node.TEXT_NODE) text += child.data;
|
|
16
|
-
else if (child.tagName === 'BR') text += '\n';
|
|
17
|
-
else if (child.tagName === 'IMG') text += '';
|
|
18
|
-
else if (child.nodeType === Node.ELEMENT_NODE) walk(child);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
walk(el);
|
|
22
|
-
return text;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** 纯文本 → 可安全塞进单元格的 HTML 片段 */
|
|
26
|
-
function textToHtml(value) {
|
|
27
|
-
const box = document.createElement('span');
|
|
28
|
-
box.textContent = value;
|
|
29
|
-
return box.innerHTML;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 选区 → 复制数据(TSV 文本 + `<table>` HTML)
|
|
34
|
-
* [设计] 合并格按「与选区相交的部分」裁剪跨度重算(等价思源 getTableRangeHTML 的口径):
|
|
35
|
-
* 直接克隆原格会把选区外的跨度一并带出去,粘到别处就撕网格。
|
|
36
|
-
* @param {object} ctx tableContext 产物
|
|
37
|
-
* @returns {{text:string, html:string}}
|
|
38
|
-
*/
|
|
39
|
-
export function copySelection(ctx) {
|
|
40
|
-
const { grid, rect } = ctx;
|
|
41
|
-
const rows = rect.r2 - rect.r1 + 1;
|
|
42
|
-
const cols = rect.c2 - rect.c1 + 1;
|
|
43
|
-
const matrix = Array.from({ length: rows }, () => new Array(cols).fill(null));
|
|
44
|
-
for (const cell of grid.cells) {
|
|
45
|
-
const r1 = Math.max(cell.r, rect.r1);
|
|
46
|
-
const r2 = Math.min(cell.r + cell.rowspan - 1, rect.r2);
|
|
47
|
-
const c1 = Math.max(cell.c, rect.c1);
|
|
48
|
-
const c2 = Math.min(cell.c + cell.colspan - 1, rect.c2);
|
|
49
|
-
if (r1 > r2 || c1 > c2) continue;
|
|
50
|
-
matrix[r1 - rect.r1][c1 - rect.c1] = { el: cell.el, rowspan: r2 - r1 + 1, colspan: c2 - c1 + 1 };
|
|
51
|
-
}
|
|
52
|
-
const text = matrix.map((row) => row.map((cell) => (cell ? cellText(cell.el) : '')).join('\t')).join('\n');
|
|
53
|
-
const table = document.createElement('table');
|
|
54
|
-
for (let r = 0; r < rows; r += 1) {
|
|
55
|
-
const tr = document.createElement('tr');
|
|
56
|
-
for (let c = 0; c < cols; c += 1) {
|
|
57
|
-
const item = matrix[r][c];
|
|
58
|
-
if (!item) continue;
|
|
59
|
-
const clone = item.el.cloneNode(true);
|
|
60
|
-
// 运行期类(框选高亮等)绝不外带;跨度只按裁剪结果重写
|
|
61
|
-
stripRuntimeClasses(clone);
|
|
62
|
-
clone.removeAttribute('contenteditable');
|
|
63
|
-
if (item.colspan > 1) clone.setAttribute('colspan', String(item.colspan));
|
|
64
|
-
else clone.removeAttribute('colspan');
|
|
65
|
-
if (item.rowspan > 1) clone.setAttribute('rowspan', String(item.rowspan));
|
|
66
|
-
else clone.removeAttribute('rowspan');
|
|
67
|
-
tr.appendChild(clone);
|
|
68
|
-
}
|
|
69
|
-
if (tr.childNodes.length) table.appendChild(tr);
|
|
70
|
-
}
|
|
71
|
-
return { text, html: table.outerHTML };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* `<table>` → 二维数据(按 rowspan/colspan 展开占位,合并格内容只落左上角,其余位为空串)
|
|
76
|
-
* [设计] 只走 querySelectorAll + children,不用 HTMLTableElement 那套富 API(rows/cells):
|
|
77
|
-
* 这里解析的是外来 HTML 片段,结构未必完整,富 API 在残缺结构下会直接不认。
|
|
78
|
-
* [设计] 跨度读**属性**而非 `rowSpan/colSpan` 属性值:来源是 HTML 文本,跨度就是属性本身
|
|
79
|
-
*/
|
|
80
|
-
function tableRows(table) {
|
|
81
|
-
const grid = [];
|
|
82
|
-
const rowAt = (r) => {
|
|
83
|
-
if (!grid[r]) grid[r] = [];
|
|
84
|
-
return grid[r];
|
|
85
|
-
};
|
|
86
|
-
Array.from(table.querySelectorAll('tr')).forEach((row, r) => {
|
|
87
|
-
const cells = rowAt(r);
|
|
88
|
-
let c = 0;
|
|
89
|
-
Array.from(row.children).forEach((cell) => {
|
|
90
|
-
if (cell.tagName !== 'TD' && cell.tagName !== 'TH') return;
|
|
91
|
-
while (cells[c] !== undefined) c += 1;
|
|
92
|
-
const rowspan = Math.max(1, Number(cell.getAttribute('rowspan')) || 1);
|
|
93
|
-
const colspan = Math.max(1, Number(cell.getAttribute('colspan')) || 1);
|
|
94
|
-
for (let i = 0; i < rowspan; i += 1) {
|
|
95
|
-
for (let j = 0; j < colspan; j += 1) rowAt(r + i)[c + j] = i === 0 && j === 0 ? cell.innerHTML : '';
|
|
96
|
-
}
|
|
97
|
-
c += colspan;
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
return grid.map((row) => (row || []).map((value) => value ?? ''));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 剪贴板内容 → 二维数据(HTML 表格优先,其次 TSV / 多行纯文本)
|
|
105
|
-
* @param {{text?:string, html?:string}} payload 已清洗过的剪贴板内容
|
|
106
|
-
* @returns {string[][]|null} 每格为 HTML 片段;无可识别结构返回 null
|
|
107
|
-
*/
|
|
108
|
-
export function clipboardRows(payload = {}) {
|
|
109
|
-
const html = String(payload.html || '');
|
|
110
|
-
const text = String(payload.text || '');
|
|
111
|
-
if (html) {
|
|
112
|
-
const box = document.createElement('div');
|
|
113
|
-
box.innerHTML = html;
|
|
114
|
-
const table = box.querySelector('table');
|
|
115
|
-
if (table) return tableRows(table);
|
|
116
|
-
}
|
|
117
|
-
if (!text) return null;
|
|
118
|
-
return text
|
|
119
|
-
.replace(/\r\n?/g, '\n')
|
|
120
|
-
.replace(/\n$/, '')
|
|
121
|
-
.split('\n')
|
|
122
|
-
.map((line) => (line.includes('\t') ? line.split('\t') : [line]).map(textToHtml));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* 把二维数据写入选区(起点 = 框选左上角 / 命中格),不够时先向右下扩表
|
|
127
|
-
* [坑] 必须先扩表再写:扩表会重建 tbody,扩表前取到的单元格引用全部脱离文档,
|
|
128
|
-
* 所以扩完必须重新读一次网格再定位。
|
|
129
|
-
* @param {object} ctx tableContext 产物
|
|
130
|
-
* @param {string[][]} rows 二维单元格 HTML
|
|
131
|
-
* @returns {boolean} 是否有内容被写入
|
|
132
|
-
*/
|
|
133
|
-
export function pasteCells(ctx, rows) {
|
|
134
|
-
if (!ctx || !rows?.length) return false;
|
|
135
|
-
const width = rows.reduce((max, row) => Math.max(max, row.length), 0);
|
|
136
|
-
if (!width) return false;
|
|
137
|
-
const { table, rect } = ctx;
|
|
138
|
-
growTable(ctx, rows.length, width);
|
|
139
|
-
const live = toGrid(table);
|
|
140
|
-
let changed = false;
|
|
141
|
-
for (let r = 0; r < rows.length; r += 1) {
|
|
142
|
-
for (let c = 0; c < rows[r].length; c += 1) {
|
|
143
|
-
const cell = live.matrix[rect.r1 + r]?.[rect.c1 + c];
|
|
144
|
-
// 落在合并格覆盖位上(不属于自己的左上角)就跳过:写进去只会把别人的内容改掉
|
|
145
|
-
if (!cell || cell.r !== rect.r1 + r || cell.c !== rect.c1 + c) continue;
|
|
146
|
-
const html = rows[r][c] ?? '';
|
|
147
|
-
if (cell.el.innerHTML === html) continue;
|
|
148
|
-
cell.el.innerHTML = html;
|
|
149
|
-
if (!cell.el.childNodes.length) cell.el.appendChild(document.createElement('br'));
|
|
150
|
-
changed = true;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return changed;
|
|
154
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·engine·tableClip —— 表内格间复制 / 粘贴(Excel 口径:选区 ⇄ 二维数据)
|
|
2
|
+
// [通用:能力] copySelection / clipboardRows / pasteCells —— 表格多格剪贴板的唯一通道
|
|
3
|
+
// [设计] 本文件只做「选区 → 二维数据」与「二维数据 → 选区」两段纯 DOM 变换;
|
|
4
|
+
// 系统剪贴板读写归调用方走 core/utils/clipboard【强制通道】——内核不认识剪贴板。
|
|
5
|
+
// 与 tables.js 的分工:那边管网格结构与行列增删,这边只管数据进出单元格。
|
|
6
|
+
|
|
7
|
+
import { stripRuntimeClasses } from './dom.js';
|
|
8
|
+
import { growTable, toGrid } from './tables.js';
|
|
9
|
+
|
|
10
|
+
/** 格内正文 → 纯文本(br 当换行、图片当占位符;内核 dom.blockText 刻意跳过 br,故此处不能用它) */
|
|
11
|
+
function cellText(el) {
|
|
12
|
+
let text = '';
|
|
13
|
+
const walk = (node) => {
|
|
14
|
+
for (const child of Array.from(node.childNodes)) {
|
|
15
|
+
if (child.nodeType === Node.TEXT_NODE) text += child.data;
|
|
16
|
+
else if (child.tagName === 'BR') text += '\n';
|
|
17
|
+
else if (child.tagName === 'IMG') text += '';
|
|
18
|
+
else if (child.nodeType === Node.ELEMENT_NODE) walk(child);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
walk(el);
|
|
22
|
+
return text;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 纯文本 → 可安全塞进单元格的 HTML 片段 */
|
|
26
|
+
function textToHtml(value) {
|
|
27
|
+
const box = document.createElement('span');
|
|
28
|
+
box.textContent = value;
|
|
29
|
+
return box.innerHTML;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 选区 → 复制数据(TSV 文本 + `<table>` HTML)
|
|
34
|
+
* [设计] 合并格按「与选区相交的部分」裁剪跨度重算(等价思源 getTableRangeHTML 的口径):
|
|
35
|
+
* 直接克隆原格会把选区外的跨度一并带出去,粘到别处就撕网格。
|
|
36
|
+
* @param {object} ctx tableContext 产物
|
|
37
|
+
* @returns {{text:string, html:string}}
|
|
38
|
+
*/
|
|
39
|
+
export function copySelection(ctx) {
|
|
40
|
+
const { grid, rect } = ctx;
|
|
41
|
+
const rows = rect.r2 - rect.r1 + 1;
|
|
42
|
+
const cols = rect.c2 - rect.c1 + 1;
|
|
43
|
+
const matrix = Array.from({ length: rows }, () => new Array(cols).fill(null));
|
|
44
|
+
for (const cell of grid.cells) {
|
|
45
|
+
const r1 = Math.max(cell.r, rect.r1);
|
|
46
|
+
const r2 = Math.min(cell.r + cell.rowspan - 1, rect.r2);
|
|
47
|
+
const c1 = Math.max(cell.c, rect.c1);
|
|
48
|
+
const c2 = Math.min(cell.c + cell.colspan - 1, rect.c2);
|
|
49
|
+
if (r1 > r2 || c1 > c2) continue;
|
|
50
|
+
matrix[r1 - rect.r1][c1 - rect.c1] = { el: cell.el, rowspan: r2 - r1 + 1, colspan: c2 - c1 + 1 };
|
|
51
|
+
}
|
|
52
|
+
const text = matrix.map((row) => row.map((cell) => (cell ? cellText(cell.el) : '')).join('\t')).join('\n');
|
|
53
|
+
const table = document.createElement('table');
|
|
54
|
+
for (let r = 0; r < rows; r += 1) {
|
|
55
|
+
const tr = document.createElement('tr');
|
|
56
|
+
for (let c = 0; c < cols; c += 1) {
|
|
57
|
+
const item = matrix[r][c];
|
|
58
|
+
if (!item) continue;
|
|
59
|
+
const clone = item.el.cloneNode(true);
|
|
60
|
+
// 运行期类(框选高亮等)绝不外带;跨度只按裁剪结果重写
|
|
61
|
+
stripRuntimeClasses(clone);
|
|
62
|
+
clone.removeAttribute('contenteditable');
|
|
63
|
+
if (item.colspan > 1) clone.setAttribute('colspan', String(item.colspan));
|
|
64
|
+
else clone.removeAttribute('colspan');
|
|
65
|
+
if (item.rowspan > 1) clone.setAttribute('rowspan', String(item.rowspan));
|
|
66
|
+
else clone.removeAttribute('rowspan');
|
|
67
|
+
tr.appendChild(clone);
|
|
68
|
+
}
|
|
69
|
+
if (tr.childNodes.length) table.appendChild(tr);
|
|
70
|
+
}
|
|
71
|
+
return { text, html: table.outerHTML };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* `<table>` → 二维数据(按 rowspan/colspan 展开占位,合并格内容只落左上角,其余位为空串)
|
|
76
|
+
* [设计] 只走 querySelectorAll + children,不用 HTMLTableElement 那套富 API(rows/cells):
|
|
77
|
+
* 这里解析的是外来 HTML 片段,结构未必完整,富 API 在残缺结构下会直接不认。
|
|
78
|
+
* [设计] 跨度读**属性**而非 `rowSpan/colSpan` 属性值:来源是 HTML 文本,跨度就是属性本身
|
|
79
|
+
*/
|
|
80
|
+
function tableRows(table) {
|
|
81
|
+
const grid = [];
|
|
82
|
+
const rowAt = (r) => {
|
|
83
|
+
if (!grid[r]) grid[r] = [];
|
|
84
|
+
return grid[r];
|
|
85
|
+
};
|
|
86
|
+
Array.from(table.querySelectorAll('tr')).forEach((row, r) => {
|
|
87
|
+
const cells = rowAt(r);
|
|
88
|
+
let c = 0;
|
|
89
|
+
Array.from(row.children).forEach((cell) => {
|
|
90
|
+
if (cell.tagName !== 'TD' && cell.tagName !== 'TH') return;
|
|
91
|
+
while (cells[c] !== undefined) c += 1;
|
|
92
|
+
const rowspan = Math.max(1, Number(cell.getAttribute('rowspan')) || 1);
|
|
93
|
+
const colspan = Math.max(1, Number(cell.getAttribute('colspan')) || 1);
|
|
94
|
+
for (let i = 0; i < rowspan; i += 1) {
|
|
95
|
+
for (let j = 0; j < colspan; j += 1) rowAt(r + i)[c + j] = i === 0 && j === 0 ? cell.innerHTML : '';
|
|
96
|
+
}
|
|
97
|
+
c += colspan;
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
return grid.map((row) => (row || []).map((value) => value ?? ''));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 剪贴板内容 → 二维数据(HTML 表格优先,其次 TSV / 多行纯文本)
|
|
105
|
+
* @param {{text?:string, html?:string}} payload 已清洗过的剪贴板内容
|
|
106
|
+
* @returns {string[][]|null} 每格为 HTML 片段;无可识别结构返回 null
|
|
107
|
+
*/
|
|
108
|
+
export function clipboardRows(payload = {}) {
|
|
109
|
+
const html = String(payload.html || '');
|
|
110
|
+
const text = String(payload.text || '');
|
|
111
|
+
if (html) {
|
|
112
|
+
const box = document.createElement('div');
|
|
113
|
+
box.innerHTML = html;
|
|
114
|
+
const table = box.querySelector('table');
|
|
115
|
+
if (table) return tableRows(table);
|
|
116
|
+
}
|
|
117
|
+
if (!text) return null;
|
|
118
|
+
return text
|
|
119
|
+
.replace(/\r\n?/g, '\n')
|
|
120
|
+
.replace(/\n$/, '')
|
|
121
|
+
.split('\n')
|
|
122
|
+
.map((line) => (line.includes('\t') ? line.split('\t') : [line]).map(textToHtml));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 把二维数据写入选区(起点 = 框选左上角 / 命中格),不够时先向右下扩表
|
|
127
|
+
* [坑] 必须先扩表再写:扩表会重建 tbody,扩表前取到的单元格引用全部脱离文档,
|
|
128
|
+
* 所以扩完必须重新读一次网格再定位。
|
|
129
|
+
* @param {object} ctx tableContext 产物
|
|
130
|
+
* @param {string[][]} rows 二维单元格 HTML
|
|
131
|
+
* @returns {boolean} 是否有内容被写入
|
|
132
|
+
*/
|
|
133
|
+
export function pasteCells(ctx, rows) {
|
|
134
|
+
if (!ctx || !rows?.length) return false;
|
|
135
|
+
const width = rows.reduce((max, row) => Math.max(max, row.length), 0);
|
|
136
|
+
if (!width) return false;
|
|
137
|
+
const { table, rect } = ctx;
|
|
138
|
+
growTable(ctx, rows.length, width);
|
|
139
|
+
const live = toGrid(table);
|
|
140
|
+
let changed = false;
|
|
141
|
+
for (let r = 0; r < rows.length; r += 1) {
|
|
142
|
+
for (let c = 0; c < rows[r].length; c += 1) {
|
|
143
|
+
const cell = live.matrix[rect.r1 + r]?.[rect.c1 + c];
|
|
144
|
+
// 落在合并格覆盖位上(不属于自己的左上角)就跳过:写进去只会把别人的内容改掉
|
|
145
|
+
if (!cell || cell.r !== rect.r1 + r || cell.c !== rect.c1 + c) continue;
|
|
146
|
+
const html = rows[r][c] ?? '';
|
|
147
|
+
if (cell.el.innerHTML === html) continue;
|
|
148
|
+
cell.el.innerHTML = html;
|
|
149
|
+
if (!cell.el.childNodes.length) cell.el.appendChild(document.createElement('br'));
|
|
150
|
+
changed = true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return changed;
|
|
154
|
+
}
|