Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(docs-ui): preserve metadata offsets when copying table cells (#7878)

Lucienbe346595e4f2
diff --git a/packages/docs-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts b/packages/docs-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts
index fc9ca6fd9f..a5c02162e7 100644
--- a/packages/docs-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts
+++ b/packages/docs-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts
@@ -15,15 +15,19 @@
*/
import type { IDocumentBody, IDocumentData } from '@univerjs/core';
+import type { IDocImage } from '@univerjs/docs-drawing';
import type { IRectRangeWithStyle, ITextRangeWithStyle } from '@univerjs/engine-render';
import type { IDocClipboardPasteAdapter } from '../doc-paste-mutation-adapter.service';
import {
BooleanNumber,
createInternalEditorID,
+ CustomDecorationType,
+ CustomRangeType,
DataStreamTreeTokenType,
DOC_RANGE_TYPE,
DocumentBlockRangeType,
DocumentDataModel,
+ DrawingTypeEnum,
EDITOR_ACTIVATED,
FOCUSING_SHAPE_TEXT_EDITOR,
ICommandService,
@@ -32,6 +36,9 @@ import {
IPermissionService,
IUndoRedoService,
IUniverInstanceService,
+ ObjectRelativeFromH,
+ ObjectRelativeFromV,
+ PositionedObjectLayoutType,
RedoCommand,
SliceBodyType,
Tools,
@@ -69,10 +76,12 @@ import {
} from '../doc-paste-mutation-adapter.service';
import { HtmlToUDMService } from '../html-to-udm/converter';
import {
+ createInternalClipboardDocData,
createInternalClipboardFragment,
DOC_INTERNAL_FRAGMENT_MIME,
parseInternalClipboardFragment,
} from '../internal-fragment';
+import { getBodySliceHtml } from '../udm-to-html/convertor';
import { DocHtmlExportService } from '../udm-to-html/doc-html-export.service';
class TestClipboardInterfaceService {
@@ -257,6 +266,125 @@ describe('DocClipboardService table copy helpers', () => {
expect(slice.paragraphs?.[0].startIndex).toBe(4);
});
+ it('preserves hyperlinks and inline images in a copied cell and its clipboard exports', () => {
+ const tokens = DataStreamTreeTokenType;
+ const prefix = `Intro\r${tokens.TABLE_START}${tokens.TABLE_ROW_START}${tokens.TABLE_CELL_START}`;
+ const cellStart = prefix.length;
+ const cellContent = `link${tokens.CUSTOM_BLOCK}\r\n`;
+ const body: IDocumentBody = {
+ dataStream: `${prefix}${cellContent}${tokens.TABLE_CELL_END}${tokens.TABLE_CELL_START}Other\r\n${tokens.TABLE_CELL_END}${tokens.TABLE_ROW_END}${tokens.TABLE_END}`,
+ paragraphs: [{ paragraphId: 'linked-cell', startIndex: cellStart + 5 }],
+ sectionBreaks: [{ sectionId: 'linked-cell-section', startIndex: cellStart + 6 }],
+ customRanges: [{
+ rangeId: 'cell-link',
+ rangeType: CustomRangeType.HYPERLINK,
+ startIndex: cellStart,
+ endIndex: cellStart + 3,
+ properties: { url: 'https://univer.ai/' },
+ }],
+ customBlocks: [{ blockId: 'cell-image', startIndex: cellStart + 4 }],
+ customDecorations: [{ id: 'cell-comment', type: CustomDecorationType.COMMENT, startIndex: cellStart, endIndex: cellStart + 3 }],
+ };
+ const original = Tools.deepClone(body);
+ const slice = getTableClipboardBodySlice(body, {
+ startOffset: cellStart,
+ endOffset: cellStart + 5,
+ collapsed: false,
+ rangeType: DOC_RANGE_TYPE.RECT,
+ tableId: 'table-1',
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 0,
+ spanEntireRow: false,
+ spanEntireColumn: false,
+ spanEntireTable: false,
+ });
+ const image: IDocImage = {
+ drawingId: 'cell-image',
+ unitId: 'copied-cell',
+ subUnitId: '',
+ drawingType: DrawingTypeEnum.DRAWING_IMAGE,
+ imageSourceType: ImageSourceType.URL,
+ source: 'https://univer.ai/image.png',
+ layoutType: PositionedObjectLayoutType.INLINE,
+ docTransform: {
+ size: { width: 20, height: 10 },
+ angle: 0,
+ positionH: { relativeFrom: ObjectRelativeFromH.CHARACTER, posOffset: 0 },
+ positionV: { relativeFrom: ObjectRelativeFromV.LINE, posOffset: 0 },
+ },
+ };
+ const doc: IDocumentData = {
+ id: 'copied-cell',
+ body: slice,
+ documentStyle: {},
+ drawings: { 'cell-image': image },
+ };
+
+ expect(slice.customRanges?.[0]).toMatchObject({ startIndex: 3, endIndex: 6 });
+ expect(slice.customBlocks?.[0].startIndex).toBe(7);
+ expect(slice.dataStream[slice.customBlocks![0].startIndex]).toBe(tokens.CUSTOM_BLOCK);
+ expect(slice.customDecorations?.[0]).toMatchObject({ startIndex: 3, endIndex: 6 });
+ expect(slice.sectionBreaks?.[0].startIndex).toBe(9);
+ expect(slice.dataStream[slice.sectionBreaks![0].startIndex]).toBe('\n');
+
+ const html = new DOMParser().parseFromString(getBodySliceHtml(doc, 3, 8), 'text/html');
+ expect(html.querySelector('a')?.getAttribute('href')).toBe('https://univer.ai/');
+ expect(html.querySelector('a')?.textContent).toBe('link');
+ expect(html.body.textContent?.trim()).toBe('link');
+ expect(html.querySelector('img')?.getAttribute('src')).toBe('https://univer.ai/image.png');
+
+ const internalDoc = parseInternalClipboardFragment(createInternalClipboardFragment(createInternalClipboardDocData(doc)));
+ expect(internalDoc?.body?.customRanges?.[0]).toMatchObject({ startIndex: 3, endIndex: 6 });
+ expect(internalDoc?.body?.customBlocks?.[0].startIndex).toBe(7);
+ expect(internalDoc?.drawings?.[internalDoc.body!.customBlocks![0].blockId]).toMatchObject({ source: 'https://univer.ai/image.png' });
+ expect(body).toEqual(original);
+ });
+
+ it('keeps structural and imported metadata anchored to the wrapped cell content', () => {
+ const tokens = DataStreamTreeTokenType;
+ const prefix = `${tokens.TABLE_START}${tokens.TABLE_ROW_START}${tokens.TABLE_CELL_START}`;
+ const cellContent = `${tokens.BLOCK_START}${tokens.COLUMN_GROUP_START}${tokens.COLUMN_START}${tokens.CUSTOM_BLOCK}${tokens.PAGE_BREAK}\r${tokens.COLUMN_END}${tokens.COLUMN_GROUP_END}${tokens.BLOCK_END}\r\n`;
+ const cellStart = prefix.length;
+ const body: IDocumentBody = {
+ dataStream: `${prefix}${cellContent}${tokens.TABLE_CELL_END}${tokens.TABLE_ROW_END}${tokens.TABLE_END}`,
+ blockRanges: [{ blockId: 'quote', blockType: DocumentBlockRangeType.QUOTE, startIndex: cellStart, endIndex: cellStart + 8 }],
+ columnGroups: [{ columnGroupId: 'columns', startIndex: cellStart + 1, endIndex: cellStart + 7 }],
+ docxRawCustomBlocks: [{ blockId: 'opaque-run', startIndex: cellStart + 3, docxRawXml: '<w:r/>' }],
+ docxRawBlocks: [{ startIndex: cellStart + 3, xml: '<w:p/>' }],
+ docxExportExcludedRanges: [{ start: cellStart + 3, end: cellStart + 4 }],
+ renderedPageBreaks: [cellStart + 4],
+ };
+ const slice = getTableClipboardBodySlice(body, {
+ startOffset: cellStart,
+ endOffset: cellStart + cellContent.length - 2,
+ collapsed: false,
+ rangeType: DOC_RANGE_TYPE.RECT,
+ tableId: 'table-1',
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 0,
+ spanEntireRow: false,
+ spanEntireColumn: false,
+ spanEntireTable: false,
+ });
+
+ expect(slice.blockRanges?.[0]).toMatchObject({ startIndex: 3, endIndex: 11 });
+ expect(slice.dataStream[slice.blockRanges![0].startIndex]).toBe(tokens.BLOCK_START);
+ expect(slice.dataStream[slice.blockRanges![0].endIndex]).toBe(tokens.BLOCK_END);
+ expect(slice.columnGroups?.[0]).toMatchObject({ startIndex: 4, endIndex: 10 });
+ expect(slice.dataStream[slice.columnGroups![0].startIndex]).toBe(tokens.COLUMN_GROUP_START);
+ expect(slice.dataStream[slice.columnGroups![0].endIndex]).toBe(tokens.COLUMN_GROUP_END);
+ expect(slice.docxRawCustomBlocks?.[0]).toMatchObject({ startIndex: 6, docxRawXml: '<w:r/>' });
+ expect(slice.dataStream[slice.docxRawCustomBlocks![0].startIndex]).toBe(tokens.CUSTOM_BLOCK);
+ expect(slice.docxRawBlocks?.[0]).toEqual({ startIndex: 6, xml: '<w:p/>' });
+ expect(slice.docxExportExcludedRanges).toEqual([{ start: 6, end: 7 }]);
+ expect(slice.renderedPageBreaks).toEqual([7]);
+ expect(slice.dataStream[slice.renderedPageBreaks![0]]).toBe(tokens.PAGE_BREAK);
+ });
+
it('copies the selected document text as plain text, html, and internal clipboard data', async () => {
const documentData: IDocumentData = {
id: 'copy-doc',
diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
index dc6d10932e..6001be95ec 100644
--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
@@ -264,31 +264,46 @@ export function getTableClipboardBodySlice(body: IDocumentBody, range: IRectRang
function getTableCellContentClipboardBodySlice(body: IDocumentBody, start: number, end: number): IDocumentBody {
const bodySlice = getBodySlice(body, start, end + 2); // +2 for '\r\n in last cell'
- const dataStream = DataStreamTreeTokenType.TABLE_START +
+ const prefix = DataStreamTreeTokenType.TABLE_START +
DataStreamTreeTokenType.TABLE_ROW_START +
- DataStreamTreeTokenType.TABLE_CELL_START +
- bodySlice.dataStream +
+ DataStreamTreeTokenType.TABLE_CELL_START;
+ bodySlice.dataStream = prefix + bodySlice.dataStream +
DataStreamTreeTokenType.TABLE_CELL_END +
DataStreamTreeTokenType.TABLE_ROW_END +
DataStreamTreeTokenType.TABLE_END;
- bodySlice.dataStream = dataStream;
+ const offset = prefix.length;
+ // Every retained metadata offset is relative to the cell content before wrapping.
bodySlice.textRuns?.forEach((textRun) => {
- const { st, ed } = textRun;
- textRun.st = st + 3;
- textRun.ed = ed + 3;
+ textRun.st += offset;
+ textRun.ed += offset;
});
-
- bodySlice.tables?.forEach((table) => {
- const { startIndex, endIndex } = table;
- table.startIndex = startIndex + 3;
- table.endIndex = endIndex + 3;
- });
-
- bodySlice.paragraphs?.forEach((paragraph) => {
- const { startIndex } = paragraph;
- paragraph.startIndex = startIndex + 3;
+ [
+ bodySlice.paragraphs,
+ bodySlice.sectionBreaks,
+ bodySlice.customBlocks,
+ bodySlice.docxRawCustomBlocks,
+ bodySlice.docxRawBlocks,
+ ].forEach((items) => items?.forEach((item) => {
+ item.startIndex += offset;
+ }));
+ [
+ bodySlice.tables,
+ bodySlice.columnGroups,
+ bodySlice.blockRanges,
+ bodySlice.customRanges,
+ bodySlice.customDecorations,
+ ].forEach((ranges) => ranges?.forEach((range) => {
+ range.startIndex += offset;
+ range.endIndex += offset;
+ }));
+ bodySlice.docxExportExcludedRanges?.forEach((range) => {
+ range.start += offset;
+ range.end += offset;
});
+ if (bodySlice.renderedPageBreaks) {
+ bodySlice.renderedPageBreaks = bodySlice.renderedPageBreaks.map((index) => index + offset);
+ }
return bodySlice;
}