|
|
@@ -99,16 +99,155 @@ export function rebalanceBlockOrders(blocks: DocumentBlock[]): DocumentBlock[] {
|
|
99
|
99
|
// ══════════════════════════════════════════════════════════════════════════════
|
|
100
|
100
|
|
|
101
|
101
|
/**
|
|
|
102
|
+ * 将 text 字段从富文本数组转换为纯字符串
|
|
|
103
|
+ *
|
|
|
104
|
+ * @param text 文本内容(字符串或富文本数组)
|
|
|
105
|
+ * @returns 纯字符串
|
|
|
106
|
+ */
|
|
|
107
|
+export function flattenTextToString(text: string | RichText[]): string {
|
|
|
108
|
+ if (typeof text === 'string') {
|
|
|
109
|
+ return text;
|
|
|
110
|
+ }
|
|
|
111
|
+ // 如果是 RichText 数组,提取所有的纯文本并连接
|
|
|
112
|
+ return text.map(seg => seg.text).join('');
|
|
|
113
|
+}
|
|
|
114
|
+
|
|
|
115
|
+/**
|
|
|
116
|
+ * 序列化表格单元格为后端格式
|
|
|
117
|
+ * 将富文本数组的 text 字段转换为纯字符串
|
|
|
118
|
+ *
|
|
|
119
|
+ * @param cell 前端单元格数据
|
|
|
120
|
+ * @param colIndex 列索引(从1开始)
|
|
|
121
|
+ * @param defaultWidth 默认宽度(磅)
|
|
|
122
|
+ * @returns 序列化后的单元格(text为纯字符串)
|
|
|
123
|
+ */
|
|
|
124
|
+export function serializeTableCell(
|
|
|
125
|
+ cell: TableCell,
|
|
|
126
|
+ colIndex: number,
|
|
|
127
|
+ defaultWidth: number = 100
|
|
|
128
|
+): any {
|
|
|
129
|
+ return {
|
|
|
130
|
+ text: flattenTextToString(cell.text), // 转换为纯字符串
|
|
|
131
|
+ rowspan: cell.rowspan || 1,
|
|
|
132
|
+ colspan: cell.colspan || 1,
|
|
|
133
|
+ col_index: cell.col_index !== undefined ? cell.col_index : colIndex,
|
|
|
134
|
+ style: {
|
|
|
135
|
+ align: cell.style?.align || 'center',
|
|
|
136
|
+ font_size: cell.style?.font_size || 12,
|
|
|
137
|
+ font_name: cell.style?.font_name || '黑体',
|
|
|
138
|
+ ...cell.style, // 保留其他自定义样式
|
|
|
139
|
+ },
|
|
|
140
|
+ word_style: cell.word_style || 'Normal',
|
|
|
141
|
+ width: cell.width !== undefined ? cell.width : defaultWidth,
|
|
|
142
|
+ };
|
|
|
143
|
+}
|
|
|
144
|
+
|
|
|
145
|
+/**
|
|
|
146
|
+ * 序列化表格块为后端格式
|
|
|
147
|
+ * 将所有单元格的 text 从富文本数组转换为纯字符串
|
|
|
148
|
+ *
|
|
|
149
|
+ * @param table 表格块
|
|
|
150
|
+ * @returns 序列化后的表格块
|
|
|
151
|
+ */
|
|
|
152
|
+export function serializeTableBlock(table: TableBlock): any {
|
|
|
153
|
+ // 计算每列的平均宽度作为默认值
|
|
|
154
|
+ const avgWidth = table.metadata.col_widths.length > 0
|
|
|
155
|
+ ? table.metadata.col_widths.reduce((sum, w) => sum + w, 0) / table.metadata.col_widths.length
|
|
|
156
|
+ : 100;
|
|
|
157
|
+
|
|
|
158
|
+ // 默认行高(磅)
|
|
|
159
|
+ const defaultHeight = 58;
|
|
|
160
|
+
|
|
|
161
|
+ const rows = table.content.rows.map((row) => ({
|
|
|
162
|
+ cells: row.cells.map((cell, colIdx) =>
|
|
|
163
|
+ serializeTableCell(cell, colIdx + 1, avgWidth)
|
|
|
164
|
+ ),
|
|
|
165
|
+ // 保留原有的 height,只有在没有 height 时才使用默认值
|
|
|
166
|
+ height: row.height !== undefined ? row.height : defaultHeight,
|
|
|
167
|
+ }));
|
|
|
168
|
+
|
|
|
169
|
+ return {
|
|
|
170
|
+ ...table,
|
|
|
171
|
+ content: { rows },
|
|
|
172
|
+ };
|
|
|
173
|
+}
|
|
|
174
|
+
|
|
|
175
|
+/**
|
|
|
176
|
+ * 规范化表格单元格数据结构
|
|
|
177
|
+ * 确保单元格包含所有必需字段,与后端期望的格式一致
|
|
|
178
|
+ *
|
|
|
179
|
+ * @param cell 原始单元格数据
|
|
|
180
|
+ * @param colIndex 列索引(从1开始)
|
|
|
181
|
+ * @param defaultWidth 默认宽度(磅)
|
|
|
182
|
+ * @returns 规范化后的单元格
|
|
|
183
|
+ */
|
|
|
184
|
+export function normalizeTableCell(
|
|
|
185
|
+ cell: Partial<TableCell>,
|
|
|
186
|
+ colIndex: number,
|
|
|
187
|
+ defaultWidth: number = 100
|
|
|
188
|
+): TableCell {
|
|
|
189
|
+ return {
|
|
|
190
|
+ text: cell.text || '',
|
|
|
191
|
+ rowspan: cell.rowspan || 1,
|
|
|
192
|
+ colspan: cell.colspan || 1,
|
|
|
193
|
+ col_index: cell.col_index !== undefined ? cell.col_index : colIndex,
|
|
|
194
|
+ style: {
|
|
|
195
|
+ align: cell.style?.align || 'center',
|
|
|
196
|
+ font_size: cell.style?.font_size || 12,
|
|
|
197
|
+ font_name: cell.style?.font_name || '黑体',
|
|
|
198
|
+ ...cell.style, // 保留其他自定义样式
|
|
|
199
|
+ },
|
|
|
200
|
+ word_style: cell.word_style || 'Normal',
|
|
|
201
|
+ width: cell.width !== undefined ? cell.width : defaultWidth,
|
|
|
202
|
+ };
|
|
|
203
|
+}
|
|
|
204
|
+
|
|
|
205
|
+/**
|
|
|
206
|
+ * 规范化整个表格的数据结构
|
|
|
207
|
+ * 确保所有单元格都包含完整的字段信息
|
|
|
208
|
+ *
|
|
|
209
|
+ * @param table 表格块
|
|
|
210
|
+ * @returns 规范化后的表格块
|
|
|
211
|
+ */
|
|
|
212
|
+export function normalizeTableBlock(table: TableBlock): TableBlock {
|
|
|
213
|
+ // 计算每列的平均宽度作为默认值
|
|
|
214
|
+ const avgWidth = table.metadata.col_widths.length > 0
|
|
|
215
|
+ ? table.metadata.col_widths.reduce((sum, w) => sum + w, 0) / table.metadata.col_widths.length
|
|
|
216
|
+ : 100;
|
|
|
217
|
+
|
|
|
218
|
+ // 默认行高(磅)
|
|
|
219
|
+ const defaultHeight = 58;
|
|
|
220
|
+
|
|
|
221
|
+ const rows = table.content.rows.map((row) => ({
|
|
|
222
|
+ cells: row.cells.map((cell, colIdx) =>
|
|
|
223
|
+ normalizeTableCell(cell, colIdx + 1, avgWidth)
|
|
|
224
|
+ ),
|
|
|
225
|
+ // 保留原有的 height,只有在没有 height 时才使用默认值
|
|
|
226
|
+ height: row.height !== undefined ? row.height : defaultHeight,
|
|
|
227
|
+ }));
|
|
|
228
|
+
|
|
|
229
|
+ return {
|
|
|
230
|
+ ...table,
|
|
|
231
|
+ content: { rows },
|
|
|
232
|
+ };
|
|
|
233
|
+}
|
|
|
234
|
+
|
|
|
235
|
+/**
|
|
102
|
236
|
* 创建空的表格单元格
|
|
103
|
237
|
*
|
|
|
238
|
+ * @param colIndex 列索引(从1开始,可选)
|
|
|
239
|
+ * @param width 单元格宽度(磅,可选)
|
|
104
|
240
|
* @returns 空单元格
|
|
105
|
241
|
*/
|
|
106
|
|
-export function createEmptyCell(): TableCell {
|
|
|
242
|
+export function createEmptyCell(colIndex?: number, width?: number): TableCell {
|
|
107
|
243
|
return {
|
|
108
|
244
|
text: '',
|
|
109
|
245
|
rowspan: 1,
|
|
110
|
246
|
colspan: 1,
|
|
|
247
|
+ col_index: colIndex,
|
|
111
|
248
|
style: {},
|
|
|
249
|
+ word_style: 'Normal',
|
|
|
250
|
+ width: width,
|
|
112
|
251
|
};
|
|
113
|
252
|
}
|
|
114
|
253
|
|
|
|
@@ -116,11 +255,19 @@ export function createEmptyCell(): TableCell {
|
|
116
|
255
|
* 创建空的表格行
|
|
117
|
256
|
*
|
|
118
|
257
|
* @param cols 列数
|
|
|
258
|
+ * @param colWidths 列宽数组(磅,可选)
|
|
|
259
|
+ * @param height 行高(磅,可选,默认58)
|
|
119
|
260
|
* @returns 表格行
|
|
120
|
261
|
*/
|
|
121
|
|
-export function createEmptyRow(cols: number): TableRow {
|
|
|
262
|
+export function createEmptyRow(cols: number, colWidths?: number[], height?: number): TableRow {
|
|
122
|
263
|
return {
|
|
123
|
|
- cells: Array(cols).fill(null).map(() => createEmptyCell()),
|
|
|
264
|
+ cells: Array(cols).fill(null).map((_, index) =>
|
|
|
265
|
+ createEmptyCell(
|
|
|
266
|
+ index + 1, // col_index从1开始
|
|
|
267
|
+ colWidths?.[index]
|
|
|
268
|
+ )
|
|
|
269
|
+ ),
|
|
|
270
|
+ height: height !== undefined ? height : 58, // 默认行高58磅
|
|
124
|
271
|
};
|
|
125
|
272
|
}
|
|
126
|
273
|
|
|
|
@@ -137,7 +284,10 @@ export function createEmptyRow(cols: number): TableRow {
|
|
137
|
284
|
* ```
|
|
138
|
285
|
*/
|
|
139
|
286
|
export function insertTableRow(table: TableBlock, afterRow: number): TableBlock {
|
|
140
|
|
- const newRow = createEmptyRow(table.metadata.cols);
|
|
|
287
|
+ // 使用相邻行的高度作为新行的高度
|
|
|
288
|
+ const referenceHeight = table.content.rows[afterRow]?.height || 58;
|
|
|
289
|
+
|
|
|
290
|
+ const newRow = createEmptyRow(table.metadata.cols, table.metadata.col_widths, referenceHeight);
|
|
141
|
291
|
const rows = [...table.content.rows];
|
|
142
|
292
|
rows.splice(afterRow + 1, 0, newRow);
|
|
143
|
293
|
|
|
|
@@ -166,8 +316,27 @@ export function insertTableRow(table: TableBlock, afterRow: number): TableBlock
|
|
166
|
316
|
export function insertTableColumn(table: TableBlock, afterCol: number): TableBlock {
|
|
167
|
317
|
const rows = table.content.rows.map((row) => {
|
|
168
|
318
|
const cells = [...row.cells];
|
|
169
|
|
- cells.splice(afterCol + 1, 0, createEmptyCell());
|
|
170
|
|
- return { cells };
|
|
|
319
|
+
|
|
|
320
|
+ // 计算新列的宽度
|
|
|
321
|
+ const avgWidth = table.metadata.col_widths.reduce((sum, w) => sum + w, 0) / table.metadata.col_widths.length;
|
|
|
322
|
+
|
|
|
323
|
+ // 插入新单元格,并设置正确的 col_index 和 width
|
|
|
324
|
+ cells.splice(afterCol + 1, 0, createEmptyCell(afterCol + 2, avgWidth));
|
|
|
325
|
+
|
|
|
326
|
+ // 更新后续单元格的 col_index
|
|
|
327
|
+ for (let i = afterCol + 2; i < cells.length; i++) {
|
|
|
328
|
+ if (cells[i].col_index !== undefined) {
|
|
|
329
|
+ cells[i] = {
|
|
|
330
|
+ ...cells[i],
|
|
|
331
|
+ col_index: i + 1,
|
|
|
332
|
+ };
|
|
|
333
|
+ }
|
|
|
334
|
+ }
|
|
|
335
|
+
|
|
|
336
|
+ return {
|
|
|
337
|
+ cells,
|
|
|
338
|
+ height: row.height, // 保留行高
|
|
|
339
|
+ };
|
|
171
|
340
|
});
|
|
172
|
341
|
|
|
173
|
342
|
const colWidths = [...table.metadata.col_widths];
|
|
|
@@ -221,9 +390,18 @@ export function deleteTableColumn(table: TableBlock, colIndex: number): TableBlo
|
|
221
|
390
|
throw new Error('表格至少需要一列');
|
|
222
|
391
|
}
|
|
223
|
392
|
|
|
224
|
|
- const rows = table.content.rows.map((row) => ({
|
|
225
|
|
- cells: row.cells.filter((_, i) => i !== colIndex),
|
|
226
|
|
- }));
|
|
|
393
|
+ const rows = table.content.rows.map((row) => {
|
|
|
394
|
+ const cells = row.cells.filter((_, i) => i !== colIndex);
|
|
|
395
|
+
|
|
|
396
|
+ // 更新剩余单元格的 col_index
|
|
|
397
|
+ return {
|
|
|
398
|
+ cells: cells.map((cell, i) => ({
|
|
|
399
|
+ ...cell,
|
|
|
400
|
+ col_index: i + 1,
|
|
|
401
|
+ })),
|
|
|
402
|
+ height: row.height, // 保留行高
|
|
|
403
|
+ };
|
|
|
404
|
+ });
|
|
227
|
405
|
|
|
228
|
406
|
const colWidths = table.metadata.col_widths.filter((_, i) => i !== colIndex);
|
|
229
|
407
|
|
|
|
@@ -242,7 +420,8 @@ export function deleteTableColumn(table: TableBlock, colIndex: number): TableBlo
|
|
242
|
420
|
* 合并单元格
|
|
243
|
421
|
*
|
|
244
|
422
|
* 支持横向合并(colspan)和纵向合并(rowspan)
|
|
245
|
|
- * 合并时会保存所有被合并单元格的原始内容和样式,以便拆分时恢复
|
|
|
423
|
+ * 合并后的主单元格会保存所有被合并单元格的文本内容(用空格连接)
|
|
|
424
|
+ * 被合并的单元格会被标记为隐藏(rowspan=0, colspan=0)
|
|
246
|
425
|
*
|
|
247
|
426
|
* @param table 表格块
|
|
248
|
427
|
* @param startRow 起始行
|
|
|
@@ -255,11 +434,11 @@ export function deleteTableColumn(table: TableBlock, colIndex: number): TableBlo
|
|
255
|
434
|
* ```ts
|
|
256
|
435
|
* // 横向合并: a + b (第0行,第0-1列)
|
|
257
|
436
|
* mergeCells(table, 0, 0, 0, 1);
|
|
258
|
|
- * // 结果: a单元格 colspan=2, 保存b的内容和样式到 _mergedCells
|
|
|
437
|
+ * // 结果: a单元格 colspan=2, text包含a和b的内容
|
|
259
|
438
|
*
|
|
260
|
439
|
* // 纵向合并: a + d (第0-1行,第0列)
|
|
261
|
440
|
* mergeCells(table, 0, 0, 1, 0);
|
|
262
|
|
- * // 结果: a单元格 rowspan=2, 保存d的内容和样式到 _mergedCells
|
|
|
441
|
+ * // 结果: a单元格 rowspan=2, text包含a和d的内容
|
|
263
|
442
|
* ```
|
|
264
|
443
|
*/
|
|
265
|
444
|
export function mergeCells(
|
|
|
@@ -269,32 +448,30 @@ export function mergeCells(
|
|
269
|
448
|
endRow: number,
|
|
270
|
449
|
endCol: number
|
|
271
|
450
|
): TableBlock {
|
|
272
|
|
- // 收集所有被合并单元格的内容和样式(保存位置偏移量)
|
|
273
|
|
- const mergedCells: Array<{
|
|
274
|
|
- rowOffset: number;
|
|
275
|
|
- colOffset: number;
|
|
276
|
|
- text: string | RichText[];
|
|
277
|
|
- style: any; // 保存原始样式
|
|
278
|
|
- }> = [];
|
|
|
451
|
+ // 计算合并范围
|
|
|
452
|
+ const rowSpan = endRow - startRow + 1;
|
|
|
453
|
+ const colSpan = endCol - startCol + 1;
|
|
|
454
|
+
|
|
|
455
|
+ // 获取主单元格(左上角)
|
|
|
456
|
+ const mainCell = table.content.rows[startRow].cells[startCol];
|
|
279
|
457
|
|
|
280
|
458
|
// 收集主单元格内容用于显示
|
|
281
|
459
|
const displayTexts: string[] = [];
|
|
282
|
460
|
|
|
|
461
|
+ // 辅助函数:将 text 字段规范化为纯字符串
|
|
|
462
|
+ const normalizeText = (text: string | RichText[]): string => {
|
|
|
463
|
+ if (typeof text === 'string') {
|
|
|
464
|
+ return text;
|
|
|
465
|
+ }
|
|
|
466
|
+ // 如果是 RichText 数组,提取所有的纯文本
|
|
|
467
|
+ return text.map(seg => seg.text).join('');
|
|
|
468
|
+ };
|
|
|
469
|
+
|
|
283
|
470
|
table.content.rows.forEach((row, rowIdx) => {
|
|
284
|
471
|
if (rowIdx >= startRow && rowIdx <= endRow) {
|
|
285
|
472
|
row.cells.forEach((cell, colIdx) => {
|
|
286
|
473
|
if (colIdx >= startCol && colIdx <= endCol) {
|
|
287
|
|
- const text = typeof cell.text === 'string'
|
|
288
|
|
- ? cell.text
|
|
289
|
|
- : cell.text.map(seg => seg.text).join('');
|
|
290
|
|
-
|
|
291
|
|
- // 保存所有单元格的原始数据和样式(包括主单元格)
|
|
292
|
|
- mergedCells.push({
|
|
293
|
|
- rowOffset: rowIdx - startRow,
|
|
294
|
|
- colOffset: colIdx - startCol,
|
|
295
|
|
- text: cell.text,
|
|
296
|
|
- style: { ...cell.style }, // 深拷贝样式
|
|
297
|
|
- });
|
|
|
474
|
+ const text = normalizeText(cell.text);
|
|
298
|
475
|
|
|
299
|
476
|
// 用于显示的文本
|
|
300
|
477
|
if (text.trim()) {
|
|
|
@@ -316,29 +493,44 @@ export function mergeCells(
|
|
316
|
493
|
}
|
|
317
|
494
|
|
|
318
|
495
|
if (rowIdx === startRow && colIdx === startCol) {
|
|
319
|
|
- // 主单元格,设置rowspan和colspan,保存原始数据
|
|
|
496
|
+ // 主单元格:设置完整的标准格式
|
|
|
497
|
+ // 合并后的文本统一为纯字符串格式
|
|
|
498
|
+ const mergedText = displayTexts.join(' ');
|
|
|
499
|
+
|
|
|
500
|
+ // 构建标准格式的单元格对象(字段顺序与后端一致)
|
|
320
|
501
|
return {
|
|
321
|
|
- ...cell,
|
|
322
|
|
- text: displayTexts.join(' ') || cell.text,
|
|
323
|
|
- rowspan: endRow - startRow + 1,
|
|
324
|
|
- colspan: endCol - startCol + 1,
|
|
|
502
|
+ text: mergedText || normalizeText(cell.text),
|
|
|
503
|
+ rowspan: rowSpan,
|
|
|
504
|
+ colspan: colSpan,
|
|
|
505
|
+ col_index: startCol + 1, // 列索引从1开始
|
|
325
|
506
|
style: {
|
|
326
|
|
- ...cell.style,
|
|
327
|
|
- _mergedCells: mergedCells, // 保存所有单元格的原始内容和样式
|
|
|
507
|
+ // 保留原有样式,确保必要字段存在
|
|
|
508
|
+ align: cell.style?.align || 'center',
|
|
|
509
|
+ font_size: cell.style?.font_size || 12,
|
|
|
510
|
+ font_name: cell.style?.font_name || '黑体',
|
|
|
511
|
+ ...cell.style, // 其他自定义样式
|
|
328
|
512
|
},
|
|
|
513
|
+ word_style: cell.word_style || 'Normal',
|
|
|
514
|
+ width: cell.width !== undefined ? cell.width : 100, // 保留原有width或使用默认值
|
|
329
|
515
|
};
|
|
330
|
516
|
}
|
|
331
|
517
|
|
|
332
|
|
- // 被合并的单元格,标记为隐藏
|
|
|
518
|
+ // 被合并的单元格,标记为隐藏
|
|
333
|
519
|
return {
|
|
334
|
|
- ...cell,
|
|
335
|
520
|
text: '', // 清空显示内容
|
|
336
|
521
|
rowspan: 0,
|
|
337
|
522
|
colspan: 0,
|
|
|
523
|
+ col_index: colIdx + 1, // 保持列索引
|
|
|
524
|
+ style: cell.style || {},
|
|
|
525
|
+ word_style: cell.word_style || 'Normal',
|
|
|
526
|
+ width: cell.width,
|
|
338
|
527
|
};
|
|
339
|
528
|
});
|
|
340
|
529
|
|
|
341
|
|
- return { cells };
|
|
|
530
|
+ return {
|
|
|
531
|
+ cells,
|
|
|
532
|
+ height: row.height, // 保留行高
|
|
|
533
|
+ };
|
|
342
|
534
|
});
|
|
343
|
535
|
|
|
344
|
536
|
return {
|
|
|
@@ -350,7 +542,8 @@ export function mergeCells(
|
|
350
|
542
|
/**
|
|
351
|
543
|
* 拆分单元格
|
|
352
|
544
|
*
|
|
353
|
|
- * 将已合并的单元格拆分回独立单元格,并恢复原始内容和样式到对应位置
|
|
|
545
|
+ * 将已合并的单元格拆分回独立单元格
|
|
|
546
|
+ * 主单元格保留原有内容,其他单元格恢复为空单元格
|
|
354
|
547
|
*
|
|
355
|
548
|
* @param table 表格块
|
|
356
|
549
|
* @param rowIndex 单元格所在行
|
|
|
@@ -361,11 +554,11 @@ export function mergeCells(
|
|
361
|
554
|
* ```ts
|
|
362
|
555
|
* // 拆分横向合并的单元格 (a+b)
|
|
363
|
556
|
* splitCell(table, 0, 0);
|
|
364
|
|
- * // 结果: a单元格恢复为普通单元格(内容为原始a,样式为原始a样式), b单元格恢复(内容为原始b,样式为原始b样式)
|
|
|
557
|
+ * // 结果: a单元格保留内容,b单元格变为空单元格
|
|
365
|
558
|
*
|
|
366
|
559
|
* // 拆分纵向合并的单元格 (a+d)
|
|
367
|
560
|
* splitCell(table, 0, 0);
|
|
368
|
|
- * // 结果: a单元格恢复为普通单元格(内容为原始a,样式为原始a样式), d单元格恢复(内容为原始d,样式为原始d样式)
|
|
|
561
|
+ * // 结果: a单元格保留内容,d单元格变为空单元格
|
|
369
|
562
|
* ```
|
|
370
|
563
|
*/
|
|
371
|
564
|
export function splitCell(
|
|
|
@@ -387,16 +580,6 @@ export function splitCell(
|
|
387
|
580
|
const rowspan = targetCell.rowspan || 1;
|
|
388
|
581
|
const colspan = targetCell.colspan || 1;
|
|
389
|
582
|
|
|
390
|
|
- // 获取保存的原始单元格数据(包含text和style)
|
|
391
|
|
- const mergedCells = targetCell.style._mergedCells || [];
|
|
392
|
|
-
|
|
393
|
|
- // 创建一个映射,用于快速查找原始内容和样式
|
|
394
|
|
- const cellDataMap = new Map<string, { text: string | RichText[]; style: any }>();
|
|
395
|
|
- mergedCells.forEach(({ rowOffset, colOffset, text, style }) => {
|
|
396
|
|
- const key = `${rowOffset}-${colOffset}`;
|
|
397
|
|
- cellDataMap.set(key, { text, style: style || {} });
|
|
398
|
|
- });
|
|
399
|
|
-
|
|
400
|
583
|
const rows = table.content.rows.map((row, rowIdx) => {
|
|
401
|
584
|
// 不在合并范围内的行直接返回
|
|
402
|
585
|
if (rowIdx < rowIndex || rowIdx >= rowIndex + rowspan) {
|
|
|
@@ -409,42 +592,28 @@ export function splitCell(
|
|
409
|
592
|
return cell;
|
|
410
|
593
|
}
|
|
411
|
594
|
|
|
412
|
|
- // 计算当前单元格在合并区域中的偏移量
|
|
413
|
|
- const rowOffset = rowIdx - rowIndex;
|
|
414
|
|
- const colOffset = colIdx - colIndex;
|
|
415
|
|
- const key = `${rowOffset}-${colOffset}`;
|
|
416
|
|
-
|
|
417
|
|
- // 从保存的数据中恢复原始内容和样式
|
|
418
|
|
- const cellData = cellDataMap.get(key);
|
|
419
|
|
- const originalText = cellData?.text || '';
|
|
420
|
|
- const originalStyle = cellData?.style || {};
|
|
421
|
|
-
|
|
422
|
|
- // 主单元格:恢复为普通单元格,清除合并标记
|
|
|
595
|
+ // 主单元格:恢复为普通单元格,清除合并标记
|
|
423
|
596
|
if (rowIdx === rowIndex && colIdx === colIndex) {
|
|
424
|
|
- const newStyle = { ...originalStyle };
|
|
425
|
|
- delete newStyle._mergedCells; // 清除保存的合并数据
|
|
426
|
|
-
|
|
427
|
597
|
return {
|
|
428
|
598
|
...cell,
|
|
429
|
|
- text: originalText,
|
|
430
|
599
|
rowspan: 1,
|
|
431
|
600
|
colspan: 1,
|
|
432
|
|
- style: newStyle, // 使用原始样式
|
|
|
601
|
+ col_index: colIdx + 1, // 确保 col_index 存在
|
|
|
602
|
+ word_style: cell.word_style || 'Normal', // 确保 word_style 存在
|
|
433
|
603
|
};
|
|
434
|
604
|
}
|
|
435
|
605
|
|
|
436
|
|
- // 被合并的单元格:恢复为独立单元格,并恢复原始内容和样式
|
|
437
|
|
- const recoveredStyle = { ...originalStyle };
|
|
438
|
|
- delete recoveredStyle._mergedCells;
|
|
439
|
|
-
|
|
440
|
|
- return {
|
|
441
|
|
- ...createEmptyCell(),
|
|
442
|
|
- text: originalText,
|
|
443
|
|
- style: recoveredStyle, // 使用原始样式
|
|
444
|
|
- };
|
|
|
606
|
+ // 被合并的单元格:恢复为独立空单元格
|
|
|
607
|
+ return createEmptyCell(
|
|
|
608
|
+ colIdx + 1, // col_index从1开始
|
|
|
609
|
+ cell.width // 保留原有宽度
|
|
|
610
|
+ );
|
|
445
|
611
|
});
|
|
446
|
612
|
|
|
447
|
|
- return { cells };
|
|
|
613
|
+ return {
|
|
|
614
|
+ cells,
|
|
|
615
|
+ height: row.height, // 保留行高
|
|
|
616
|
+ };
|
|
448
|
617
|
});
|
|
449
|
618
|
|
|
450
|
619
|
return {
|