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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(docs): pictures support writing to the clipboard (#4086)
Gpound.liubf8329f01581
diff --git a/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts b/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.tsindex 53d539cd5f..022532af5b 100644--- a/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts+++ b/packages/docs-drawing-ui/src/controllers/render-controllers/doc-drawing-update.render-controller.ts@@ -16,7 +16,7 @@import type { DocumentDataModel, ICommandInfo, IDocDrawingPosition, Nullable } from '@univerjs/core';import type { IDocDrawing } from '@univerjs/docs-drawing';-import type { IImageIoServiceParam } from '@univerjs/drawing';+import type { IDrawingParam, IImageIoServiceParam } from '@univerjs/drawing';import type { Documents, Image, IRenderContext, IRenderModule } from '@univerjs/engine-render';import type { IInsertDrawingCommandParams } from '../../commands/commands/interfaces';import { BooleanNumber, Disposable, FOCUSING_COMMON_DRAWINGS, ICommandService, IContextService, Inject, LocaleService, ObjectRelativeFromH, ObjectRelativeFromV, PositionedObjectLayoutType, WrapTextType } from '@univerjs/core';@@ -28,6 +28,7 @@ import { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_ALLOW_SIZE, DRAWING_IMAGEimport { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';import { ILocalFileService, IMessageService } from '@univerjs/ui';+import { debounceTime } from 'rxjs';import { GroupDocDrawingCommand } from '../../commands/commands/group-doc-drawing.command';import { InsertDocDrawingCommand } from '../../commands/commands/insert-doc-drawing.command';import { type ISetDrawingArrangeCommandParams, SetDocDrawingArrangeCommand } from '../../commands/commands/set-drawing-arrange.command';@@ -55,7 +56,7 @@ export class DocDrawingUpdateRenderController extends Disposable implements IRenthis._updateOrderListener();this._groupDrawingListener();this._focusDrawingListener();-+ this._transformDrawingListener();this._editAreaChangeListener();}@@ -250,6 +251,17 @@ export class DocDrawingUpdateRenderController extends Disposable implements IRenreturn { scene, transformer, docsLeft, docsTop };}+ private _transformDrawingListener() {+ const res = this._getCurrentSceneAndTransformer();+ if (res && res.transformer) {+ this.disposeWithMe(res.transformer.changeEnd$.pipe(debounceTime(30)).subscribe((params) => {+ this._docSelectionManagerService.refreshSelection();+ }));+ } else {+ throw new Error('transformer is not init');+ }+ }+private _focusDrawingListener() {this.disposeWithMe(this._drawingManagerService.focus$.subscribe((params) => {@@ -267,12 +279,7 @@ export class DocDrawingUpdateRenderController extends Disposable implements IRen} else {this._contextService.setContextValue(FOCUSING_COMMON_DRAWINGS, true);this._docDrawingService.focusDrawing(params);- // Need to remove text selections when focus drawings.- const activeTextRange = this._docSelectionManagerService.getActiveTextRange();- if (activeTextRange) {- this._docSelectionManagerService.replaceTextRanges([]);- }-+ this._setDrawingSelections(params);const prevSegmentId = this._docSelectionRenderService.getSegment();const segmentId = this._findSegmentIdByDrawingId(params[0].drawingId);@@ -396,4 +403,18 @@ export class DocDrawingUpdateRenderController extends Disposable implements IRen}));}++ private _setDrawingSelections(params: IDrawingParam[]) {+ const { unit } = this._context;+ const customBlocks = unit.getSnapshot().body?.customBlocks ?? [];+ const ranges = params.map((item) => {+ const id = item.drawingId;+ const block = customBlocks.find((b) => b.blockId === id);+ if (block) {+ return block.startIndex;+ }+ return null;+ }).filter((e) => e !== null).map((offset) => ({ startOffset: offset, endOffset: offset + 1 }));+ this._docSelectionManagerService.replaceDocRanges(ranges);+ }}diff --git a/packages/docs-drawing/src/index.ts b/packages/docs-drawing/src/index.tsindex bd2906f556..7445d386d1 100644--- a/packages/docs-drawing/src/index.ts+++ b/packages/docs-drawing/src/index.ts@@ -15,6 +15,6 @@*/export { UniverDocsDrawingPlugin } from './plugin';-export { DocDrawingService, type IDocDrawing, IDocDrawingService } from './services/doc-drawing.service';+export { DocDrawingService, type IDocDrawing, IDocDrawingService, type IDocImage } from './services/doc-drawing.service';export { DOCS_DRAWING_PLUGIN, type IDocDrawingModel } from './controllers/doc-drawing.controller';diff --git a/packages/docs-ui/package.json b/packages/docs-ui/package.jsonindex 7a7f7f045f..c7e809867f 100644--- a/packages/docs-ui/package.json+++ b/packages/docs-ui/package.json@@ -77,6 +77,8 @@"@univerjs/core": "workspace:*","@univerjs/design": "workspace:*","@univerjs/docs": "workspace:*",+ "@univerjs/docs-drawing": "workspace:*",+ "@univerjs/drawing": "workspace:*","@univerjs/engine-formula": "workspace:*","@univerjs/engine-render": "workspace:*","@univerjs/icons": "^0.2.7",diff --git a/packages/docs-ui/src/controllers/render-controllers/doc-clipboard.controller.ts b/packages/docs-ui/src/controllers/render-controllers/doc-clipboard.controller.tsindex a25fbfb337..c6bdd4c14e 100644--- a/packages/docs-ui/src/controllers/render-controllers/doc-clipboard.controller.ts+++ b/packages/docs-ui/src/controllers/render-controllers/doc-clipboard.controller.ts@@ -23,6 +23,14 @@ import {Inject,RxDisposable,} from '@univerjs/core';+import {+ FILE__BMP_CLIPBOARD_MIME_TYPE,+ FILE__JPEG_CLIPBOARD_MIME_TYPE,+ FILE__WEBP_CLIPBOARD_MIME_TYPE,+ FILE_PNG_CLIPBOARD_MIME_TYPE,+ HTML_CLIPBOARD_MIME_TYPE,+ PLAIN_TEXT_CLIPBOARD_MIME_TYPE,+} from '@univerjs/ui';import { takeUntil } from 'rxjs';import { whenDocOrEditor } from '../../commands/commands/clipboard.command';import { IDocClipboardService } from '../../services/clipboard/clipboard.service';@@ -37,6 +45,7 @@ export class DocClipboardController extends RxDisposable implements IRenderModul@Inject(DocSelectionRenderService) private readonly _docSelectionRenderService: DocSelectionRenderService,@IContextService private readonly _contextService: IContextService,@IEditorService private readonly _editorService: IEditorService+) {super();@@ -55,8 +64,20 @@ export class DocClipboardController extends RxDisposable implements IRenderModulconfig!.event.preventDefault();const clipboardEvent = config!.event as ClipboardEvent;- let htmlContent = clipboardEvent.clipboardData?.getData('text/html');- const textContent = clipboardEvent.clipboardData?.getData('text/plain');+ let htmlContent = clipboardEvent.clipboardData?.getData(HTML_CLIPBOARD_MIME_TYPE);+ const textContent = clipboardEvent.clipboardData?.getData(PLAIN_TEXT_CLIPBOARD_MIME_TYPE);++ const imageTypes = [+ FILE__BMP_CLIPBOARD_MIME_TYPE,+ FILE__JPEG_CLIPBOARD_MIME_TYPE,+ FILE__WEBP_CLIPBOARD_MIME_TYPE,+ FILE_PNG_CLIPBOARD_MIME_TYPE,+ ];++ const files = [...(clipboardEvent.clipboardData?.items || [])]+ .filter((item) => imageTypes.includes(item.type))+ .map((item) => item.getAsFile()!)+ .filter((e) => !!e);// TODO: @JOCS, work around to fix https://github.com/dream-num/univer-pro/issues/2006. and then when you paste it,// you need to distinguish between different editors,@@ -67,7 +88,7 @@ export class DocClipboardController extends RxDisposable implements IRenderModulhtmlContent = '';}- this._docClipboardService.legacyPaste(htmlContent, textContent);+ this._docClipboardService.legacyPaste({ html: htmlContent, text: textContent, files });});}}diff --git a/packages/docs-ui/src/services/clipboard/__test__/html-and-udm-convert.spec.ts b/packages/docs-ui/src/services/clipboard/__test__/html-and-udm-convert.spec.tsindex 8ef84250a5..7375257a56 100644--- a/packages/docs-ui/src/services/clipboard/__test__/html-and-udm-convert.spec.ts+++ b/packages/docs-ui/src/services/clipboard/__test__/html-and-udm-convert.spec.ts@@ -123,7 +123,7 @@ describe('test case in html and udm convert', () => {describe('test cases in udm-to-html', () => {it('should paste the case when convert udm to html', async () => {const convertor = new UDMToHtmlService();- const html = await convertor.convert([body!]);+ const html = await convertor.convert([{ body: body!, id: '', documentStyle: {} }]);expect(html).toBe('<p class="UniverNormal" ><strong>hello</strong><strong><i>world</i></strong></p>');});diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.tsindex 0ef15d023b..db82b1103e 100644--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts@@ -15,11 +15,21 @@*/import type { IDisposable, IDocumentBody, IDocumentData, Nullable } from '@univerjs/core';+import type { IDocImage } from '@univerjs/docs-drawing';import type { IRectRangeWithStyle, ITextRangeWithStyle } from '@univerjs/engine-render';-import { BuildTextUtils, createIdentifier, DataStreamTreeTokenType, Disposable, DOC_RANGE_TYPE, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, getBodySlice, ICommandService, ILogService, Inject, IUniverInstanceService, normalizeBody, SliceBodyType, toDisposable, Tools, UniverInstanceType } from '@univerjs/core';+import { BuildTextUtils, createIdentifier, DataStreamTreeTokenType, Disposable, DOC_RANGE_TYPE, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, getBodySlice, ICommandService, ILogService, Inject, IUniverInstanceService, normalizeBody, ObjectRelativeFromH, ObjectRelativeFromV, PositionedObjectLayoutType, SliceBodyType, toDisposable, Tools, UniverInstanceType } from '@univerjs/core';import { DocSelectionManagerService } from '@univerjs/docs';-import { HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui';+import { DrawingTypeEnum, ImageSourceType } from '@univerjs/drawing';+import {+ FILE__BMP_CLIPBOARD_MIME_TYPE,+ FILE__JPEG_CLIPBOARD_MIME_TYPE,+ FILE__WEBP_CLIPBOARD_MIME_TYPE,+ FILE_PNG_CLIPBOARD_MIME_TYPE,+ HTML_CLIPBOARD_MIME_TYPE,+ IClipboardInterfaceService,+ PLAIN_TEXT_CLIPBOARD_MIME_TYPE,+} from '@univerjs/ui';import { CutContentCommand, InnerPasteCommand } from '../../commands/commands/clipboard.inner.command';import { getCursorWhenDelete } from '../../commands/commands/doc-delete.command';import { copyContentCache, extractId, genId } from './copy-content-cache';@@ -39,13 +49,14 @@ export interface IDocClipboardHook {onCopyProperty?(start: number, end: number): IClipboardPropertyItem;onCopyContent?(start: number, end: number): string;onBeforePaste?: (body: IDocumentBody) => IDocumentBody;+ onBeforePasteImage?: (file: File) => Promise<{ source: string; imageSourceType: ImageSourceType }>;}export interface IDocClipboardService {copy(sliceType?: SliceBodyType): Promise<boolean>;cut(): Promise<boolean>;paste(items: ClipboardItem[]): Promise<boolean>;- legacyPaste(html?: string, text?: string): Promise<boolean>;+ legacyPaste(options: { html?: string; text?: string; files: File[] }): Promise<boolean>;addClipboardHook(hook: IDocClipboardHook): IDisposable;}@@ -100,9 +111,9 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ}async copy(sliceType: SliceBodyType = SliceBodyType.copy): Promise<boolean> {- const { bodyList = [], needCache = false, snapshot } = this._getDocumentBodyInRanges(sliceType) ?? {};+ const { newSnapshotList = [], needCache = false, snapshot } = this._getDocumentBodyInRanges(sliceType) ?? {};- if (bodyList.length === 0 || snapshot == null) {+ if (newSnapshotList.length === 0 || snapshot == null) {return false;}@@ -110,7 +121,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServconst activeRange = this._docSelectionManagerService.getActiveTextRange();const isCopyInHeaderFooter = !!activeRange?.segmentId;- this._setClipboardData(bodyList, snapshot, !isCopyInHeaderFooter && needCache);+ this._setClipboardData(newSnapshotList, !isCopyInHeaderFooter && needCache);} catch (e) {this._logService.error('[DocClipboardService] copy failed', e);return false;@@ -129,10 +140,18 @@ export class DocClipboardService extends Disposable implements IDocClipboardServreturn this._paste(partDocData);}- async legacyPaste(html?: string, text?: string): Promise<boolean> {- const partDocData = this._genDocDataFromHtmlAndText(html, text);+ async legacyPaste(options: {+ html?: string; text?: string; files: File[];+ }): Promise<boolean> {+ let { html, text, files } = options;+ const currentDocInstance = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC);+ const docUnitId = currentDocInstance?.getUnitId() || '';+ if (!html && !text && files.length) {+ html = await this._createImagePasteHtml(files);+ }+ const partDocData = this._genDocDataFromHtmlAndText(html, text, docUnitId);// Paste in sheet editing mode without paste style, so we give textRuns empty array;- if (this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC)?.getUnitId() === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {+ if (docUnitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {if (text) {const textDocData = BuildTextUtils.transform.fromPlainText(text);return this._paste({ body: textDocData });@@ -271,12 +290,12 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ}}- private async _setClipboardData(documentBodyList: IDocumentBody[], snapshot: IDocumentData, needCache = true): Promise<void> {+ private async _setClipboardData(documentList: IDocumentData[], needCache = true): Promise<void> {const copyId = genId();const text =- (documentBodyList.length > 1- ? documentBodyList.map((body) => body.dataStream).join('\n')- : documentBodyList[0].dataStream)+ (documentList.length > 1+ ? documentList.map((doc) => doc.body?.dataStream || '').join('\n')+ : documentList[0].body?.dataStream || '').replaceAll(DataStreamTreeTokenType.TABLE_START, '').replaceAll(DataStreamTreeTokenType.TABLE_END, '').replaceAll(DataStreamTreeTokenType.TABLE_ROW_START, '')@@ -286,20 +305,20 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ// Replace `\r\n` in table cell to white space..replaceAll('\r\n', ' ');- let html = this._umdToHtml.convert(documentBodyList);+ let html = this._umdToHtml.convert(documentList);// Only cache copy content when the range is 1.- if (documentBodyList.length === 1 && needCache) {+ if (documentList.length === 1 && needCache) {html = html.replace(/(<[a-z]+)/, (_p0, p1) => `${p1} data-copy-id="${copyId}"`);- const body = documentBodyList[0];- const cache: Partial<IDocumentData> = { body };+ const doc = documentList[0];+ const cache: Partial<IDocumentData> = { body: doc.body };- if (body.customBlocks?.length) {+ if (doc.body?.customBlocks?.length) {cache.drawings = {};- for (const block of body.customBlocks) {+ for (const block of doc.body.customBlocks) {const { blockId } = block;- const drawing = snapshot.drawings?.[blockId];+ const drawing = doc.drawings?.[blockId];if (drawing) {const id = Tools.generateRandomId(6);@@ -333,14 +352,14 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ}private _getDocumentBodyInRanges(sliceType: SliceBodyType): Nullable<{- bodyList: IDocumentBody[];+ newSnapshotList: IDocumentData[];needCache: boolean;snapshot: IDocumentData;}> {const docDataModel = this._univerInstanceService.getCurrentUniverDocInstance();const allRanges = this._docSelectionManagerService.getDocRanges();- const results: IDocumentBody[] = [];+ const results: IDocumentData['body'][] = [];let needCache = true;if (docDataModel == null || allRanges.length === 0) {@@ -391,7 +410,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServresults.push(docBody);}return {- bodyList: results,+ newSnapshotList: results.map((e) => ({ ...snapshot, body: e })),needCache,snapshot,};@@ -399,20 +418,35 @@ export class DocClipboardService extends Disposable implements IDocClipboardServprivate async _genDocDataFromClipboardItems(items: ClipboardItem[]): Promise<Partial<IDocumentData>> {try {- // TODO: support paste image.-let html = '';let text = '';-+ const files: File[] = [];for (const clipboardItem of items) {for (const type of clipboardItem.types) {- if (type === PLAIN_TEXT_CLIPBOARD_MIME_TYPE) {- text = await clipboardItem.getType(type).then((blob) => blob && blob.text());- } else if (type === HTML_CLIPBOARD_MIME_TYPE) {- html = await clipboardItem.getType(type).then((blob) => blob && blob.text());+ switch (type) {+ case PLAIN_TEXT_CLIPBOARD_MIME_TYPE: {+ text = await clipboardItem.getType(type).then((blob) => blob && blob.text());+ break;+ }+ case HTML_CLIPBOARD_MIME_TYPE: {+ html = await clipboardItem.getType(type).then((blob) => blob && blob.text());+ break;+ }+ case FILE__BMP_CLIPBOARD_MIME_TYPE:+ case FILE__JPEG_CLIPBOARD_MIME_TYPE:+ case FILE__WEBP_CLIPBOARD_MIME_TYPE:+ case FILE_PNG_CLIPBOARD_MIME_TYPE: {+ const blob = await clipboardItem.getType(type);+ const file = new File([blob], `pasted_image.${type.split('/')[1]}`, { type });+ files.push(file);+ break;+ }}}}+ if (!html && !text && files.length) {+ html = await this._createImagePasteHtml(files);+ }return this._genDocDataFromHtmlAndText(html, text);} catch (e) {@@ -420,7 +454,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ}}- private _genDocDataFromHtmlAndText(html?: string, text?: string): Partial<IDocumentData> {+ private _genDocDataFromHtmlAndText(html?: string, text?: string, _unitId?: string): Partial<IDocumentData> {if (!html) {if (text) {const body = BuildTextUtils.transform.fromPlainText(text);@@ -439,8 +473,90 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ}}- const doc = this._htmlToUDM.convert(html);+ if (!_unitId) {+ const currentDocInstance = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC);+ const docUnitId = currentDocInstance?.getUnitId() || '';+ _unitId = docUnitId;+ }+ const doc = this._htmlToUDM.convert(html, { unitId: _unitId });++ if (copyId) {+ copyContentCache.set(copyId, doc);+ }return doc;}++ private async _createImagePasteHtml(files: File[]) {+ const doc: IDocumentData = {+ id: '',+ documentStyle: {},+ body: {+ dataStream: '',+ customBlocks: [],+ },+ drawings: {},+ };+ const fileToBase64 = async (file: File): Promise<{ source: string; imageSourceType: ImageSourceType }> => {+ const reader = new FileReader();+ return new Promise((res) => {+ reader.onloadend = function () {+ res({+ source: reader.result as string,+ imageSourceType: ImageSourceType.BASE64,+ });+ };+ reader.readAsDataURL(file);+ });+ };+ const getImageSize = (base64: string | File): Promise<{ width: number; height: number }> => {+ const img = new Image();+ const maxWidth = 500;+ return new Promise((resolve) => {+ img.src = typeof base64 === 'string' ? base64 : URL.createObjectURL(base64);+ img.onload = () => {+ const width = Math.min(maxWidth, img.naturalWidth);+ const scale = img.naturalHeight / img.naturalWidth;+ resolve({ width, height: width * scale });+ };+ });+ };+ // clipboardHooks 应该被重新设计,用来处理多个 hook 处理同一个节点的能力+ // 参考 interceptor+ const onBeforePasteImage = (this._clipboardHooks.find((e) => e.onBeforePasteImage)?.onBeforePasteImage!) ?? fileToBase64;++ await Promise.all(files.map(async (file, index) => {+ const image = await onBeforePasteImage(file);+ const { width = 100, height = 100 } = await getImageSize(file);+ const itemId = `paste_image_id_${index}`;+ const body = doc.body!;+ const drawings = doc.drawings!;+ body.dataStream += '\b';+ body.customBlocks?.push({ startIndex: index, blockId: itemId });+ drawings[itemId] = {+ drawingId: itemId,+ unitId: '',+ subUnitId: '',+ imageSourceType: image.imageSourceType,+ title: '',+ source: image.source,+ description: '',+ layoutType: PositionedObjectLayoutType.INLINE,+ drawingType: DrawingTypeEnum.DRAWING_IMAGE,+ transform: {+ width,+ height,+ angle: 0,+ },+ docTransform: {+ angle: 0,+ size: { width, height },+ positionH: { relativeFrom: ObjectRelativeFromH.CHARACTER, posOffset: 0 },+ positionV: { relativeFrom: ObjectRelativeFromV.LINE, posOffset: 0 },+ },+ } as IDocImage;+ }));+ const html = this._umdToHtml.convert([doc]);+ return html;+ }}diff --git a/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts b/packages/docs-ui/src/services/clipboard/html-to-udm/converter.tsindex 75776d2c32..eb8b35ba7d 100644--- a/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts+++ b/packages/docs-ui/src/services/clipboard/html-to-udm/converter.ts@@ -17,7 +17,8 @@import type { IDocumentBody, IDocumentData, ITable, ITextStyle, Nullable } from '@univerjs/core';import type { IAfterProcessRule, IPastePlugin, IStyleRule } from './paste-plugins/type';-import { CustomRangeType, DataStreamTreeTokenType, generateRandomId, skipParseTagNames, Tools } from '@univerjs/core';+import { CustomRangeType, DataStreamTreeTokenType, generateRandomId, ObjectRelativeFromH, ObjectRelativeFromV, PositionedObjectLayoutType, skipParseTagNames, Tools } from '@univerjs/core';+import { DrawingTypeEnum } from '@univerjs/drawing';import { genTableSource, getEmptyTableCell, getEmptyTableRow, getTableColumn } from '../../../commands/commands/table/table';import { extractNodeStyle } from './parse-node-style';import parseToDom from './parse-to-dom';@@ -67,7 +68,7 @@ export class HtmlToUDMService {private _afterProcessRules: IAfterProcessRule[] = [];- convert(html: string): Partial<IDocumentData> {+ convert(html: string, metaConfig: { unitId?: string } = {}): Partial<IDocumentData> {const pastePlugin = HtmlToUDMService._pluginList.find((plugin) => plugin.checkPasteType(html));const dom = parseToDom(html)!;@@ -77,11 +78,13 @@ export class HtmlToUDMService {sectionBreaks: [],tables: [],textRuns: [],+ customBlocks: [],};const docData: Partial<IDocumentData> = {body,tableSource: {},+ id: metaConfig?.unitId ?? '',};if (pastePlugin) {@@ -99,9 +102,9 @@ export class HtmlToUDMService {return docData;}+ // eslint-disable-next-line max-lines-per-function, complexityprivate _process(parent: Nullable<ChildNode>, nodes: NodeListOf<ChildNode>, doc: Partial<IDocumentData>) {const body = doc.body!;-for (const node of nodes) {if (node.nodeType === Node.TEXT_NODE) {if (node.nodeValue?.trim() === '') {@@ -125,6 +128,48 @@ export class HtmlToUDMService {ts: style,});}+ } else if (node.nodeName === 'IMG') {+ const element = node as HTMLImageElement;+ const source = element.src;+ const imageSourceType = element.dataset.imageSourceType;++ if (source && imageSourceType) {+ const width = Number(element.dataset.width || 100);+ const height = Number(element.dataset.height || 100);+ const docTransformWidth = Number(element.dataset.docTransformWidth || width);+ const docTransformHeight = Number(element.dataset.docTransformHeight || height);+ // 外部会进行替换.+ const id = Tools.generateRandomId(6);+ doc.body?.customBlocks?.push({ startIndex: body.dataStream.length, blockId: id });+ body.dataStream += '\b';+ if (!doc.drawings) {+ doc.drawings = {};+ }+ doc.drawings[id] = {+ drawingId: id,+ title: '',+ description: '',+ imageSourceType,+ source,+ transform: { width, height, left: 0 },+ docTransform: {+ size: { width: docTransformWidth, height: docTransformHeight },+ angle: 0,+ positionH: {+ relativeFrom: ObjectRelativeFromH.PAGE,+ posOffset: 0,+ },+ positionV: {+ relativeFrom: ObjectRelativeFromV.PARAGRAPH,+ posOffset: 0,+ },+ },+ layoutType: PositionedObjectLayoutType.INLINE,+ drawingType: DrawingTypeEnum.DRAWING_IMAGE,+ unitId: doc.id || '',+ subUnitId: doc.id || '',+ } as any;+ }} else if (skipParseTagNames.includes(node.nodeName.toLowerCase())) {continue;} else if (node.nodeType === Node.ELEMENT_NODE) {diff --git a/packages/docs-ui/src/services/clipboard/udm-to-html/__test__/convertor.spec.ts b/packages/docs-ui/src/services/clipboard/udm-to-html/__test__/convertor.spec.tsindex cbfd83e78a..e6761fde42 100644--- a/packages/docs-ui/src/services/clipboard/udm-to-html/__test__/convertor.spec.ts+++ b/packages/docs-ui/src/services/clipboard/udm-to-html/__test__/convertor.spec.ts@@ -14,9 +14,9 @@* limitations under the License.*/-import { describe, expect, it } from 'vitest';import type { IDocumentBody } from '@univerjs/core';import { BooleanNumber } from '@univerjs/core';+import { describe, expect, it } from 'vitest';import { convertBodyToHtml, covertTextRunToHtml, getBodySliceHtml, UDMToHtmlService } from '../convertor';function getTestBody() {@@ -85,7 +85,7 @@ describe('test case in html and udm convert', () => {},];- const html = convertor.convert(bodyList);+ const html = convertor.convert(bodyList.map((b) => ({ body: b, id: '', documentStyle: {} })));expect(html).toBe('<p class="UniverNormal" >=SUM(<span style="color: #9e6de3;">F15:G18</span>)</p>');});@@ -104,19 +104,19 @@ describe('test case in html and udm convert', () => {let expectedHtml = '<span style="font-family: Microsoft YaHei; color: rgb(0, 0, 0); font-size: 24pt;"><strong>塘月</strong></span>';- expect(getBodySliceHtml(documentBody, startIndex, endIndex)).toEqual(expectedHtml);+ expect(getBodySliceHtml({ body: documentBody, id: '', documentStyle: {} }, startIndex, endIndex)).toEqual(expectedHtml);startIndex = 0;endIndex = 4;expectedHtml = '荷<span style="font-family: Microsoft YaHei; color: rgb(0, 0, 0); font-size: 24pt;"><strong>塘月</strong></span>色';- expect(getBodySliceHtml(documentBody, startIndex, endIndex)).toEqual(expectedHtml);+ expect(getBodySliceHtml({ body: documentBody, id: '', documentStyle: {} }, startIndex, endIndex)).toEqual(expectedHtml);});it('Should convert document body To Html(convertBodyToHtml)', () => {const documentBody = getTestBody();const expectedHtml = '<p class="UniverNormal" style="margin-top: 10px; margin-bottom: 0px; line-height: 2;">荷<span style="font-family: Microsoft YaHei; color: rgb(0, 0, 0); font-size: 24pt;"><strong>塘月</strong></span>色</p><p class="UniverNormal" style="margin-top: 10px; margin-bottom: 0px; line-height: 2;">作者:朱自清</p>';- expect(convertBodyToHtml(documentBody)).toEqual(expectedHtml);+ expect(convertBodyToHtml({ body: documentBody, id: '', documentStyle: {} })).toEqual(expectedHtml);});});diff --git a/packages/docs-ui/src/services/clipboard/udm-to-html/convertor.ts b/packages/docs-ui/src/services/clipboard/udm-to-html/convertor.tsindex 42b86b71f0..2845038dcb 100644--- a/packages/docs-ui/src/services/clipboard/udm-to-html/convertor.ts+++ b/packages/docs-ui/src/services/clipboard/udm-to-html/convertor.ts@@ -14,11 +14,17 @@* limitations under the License.*/-import type { IDocumentBody, IParagraph, ITextRun } from '@univerjs/core';+import type { IDocumentBody, IDocumentData, IParagraph, ITextRun } from '@univerjs/core';+import type { IDocImage } from '@univerjs/docs-drawing';import type { DataStreamTreeNode } from '@univerjs/engine-render';import { BaselineOffset, BooleanNumber, CustomRangeType, DataStreamTreeNodeType, Tools } from '@univerjs/core';+import { DrawingTypeEnum } from '@univerjs/drawing';import { parseDataStreamToTree } from '@univerjs/engine-render';+function covertImageToHtml(item: IDocImage) {+ return `<img data-doc-transform-height="${item.docTransform.size.height}" data-doc-transform-width="${item.docTransform.size.width}" data-width="${item.transform?.width}" data-height="${item.transform?.height}" data-image-source-type="${item.imageSourceType}" src="${item.source}"></img>`;+}+export function covertTextRunToHtml(dataStream: string, textRun: ITextRun): string {const { st: start, ed, ts = {} } = textRun;const { ff, fs, it, bl, ul, st, ol, bg, cl, va } = ts;@@ -83,7 +89,9 @@ export function covertTextRunToHtml(dataStream: string, textRun: ITextRun): strifunction getBodyInlineSlice(body: IDocumentBody, startIndex: number, endIndex: number) {const { dataStream, textRuns = [] } = body;-+ if (startIndex === endIndex) {+ return '';+ }let cursorIndex = startIndex;const spanList: string[] = [];@@ -116,32 +124,66 @@ function getBodyInlineSlice(body: IDocumentBody, startIndex: number, endIndex: nreturn spanList.join('');}-export function getBodySliceHtml(body: IDocumentBody, startIndex: number, endIndex: number) {- const { customRanges = [] } = body;+export function getBodySliceHtml(doc: IDocumentData, startIndex: number, endIndex: number) {+ const body = doc.body!;+ const drawings = doc.drawings || {};+ const { customRanges = [], customBlocks = [] } = body || {};+ const cloneCustomBlocks = [...customBlocks];const customRangesInRange = customRanges.filter((range) => range.startIndex >= startIndex && range.endIndex <= endIndex);-let cursorIndex = startIndex;let html = '';+ const handleCustomBlock = (startIndex: number, endIndex: number) => {+ let sliceHtml = '';+ let customBlockLength = 0;+ let handleCustomBlockCursorIndex = startIndex;+ let blockItemIndex = cloneCustomBlocks.findIndex((block) => startIndex <= block.startIndex && endIndex >= block.startIndex);++ if (blockItemIndex === -1) {+ sliceHtml = getBodyInlineSlice(body, startIndex, endIndex);+ return { sliceHtml, customBlockLength };+ }++ while (blockItemIndex !== -1) {+ const blockItem = cloneCustomBlocks[blockItemIndex];+ cloneCustomBlocks.splice(blockItemIndex, 1);+ sliceHtml += getBodyInlineSlice(body, handleCustomBlockCursorIndex, blockItem.startIndex);+ const drawingItem = drawings[blockItem.blockId];+ if (drawingItem) {+ switch (drawingItem.drawingType) {+ case DrawingTypeEnum.DRAWING_IMAGE: {+ sliceHtml += covertImageToHtml(drawingItem as unknown as IDocImage);+ customBlockLength++;+ break;+ }+ }+ }++ handleCustomBlockCursorIndex = blockItem.startIndex + 1;+ blockItemIndex = cloneCustomBlocks.findIndex((block) => handleCustomBlockCursorIndex <= block.startIndex && endIndex >= block.startIndex);+ }+ sliceHtml = sliceHtml + getBodyInlineSlice(body, handleCustomBlockCursorIndex, endIndex + 1);+ return { sliceHtml, customBlockLength };+ };customRangesInRange.forEach((range) => {const { startIndex, endIndex, rangeType, rangeId } = range;- const preHtml = getBodyInlineSlice(body, cursorIndex, startIndex);- html += preHtml;- const sliceHtml = getBodyInlineSlice(body, startIndex, endIndex + 1);+ const preHtml = handleCustomBlock(cursorIndex, startIndex);+ html += preHtml.sliceHtml;+ const sliceHtml = handleCustomBlock(startIndex, endIndex + 1);switch (rangeType) {case CustomRangeType.HYPERLINK: {- html += `<a data-rangeid="${rangeId}" href="${range.properties?.url ?? ''}">${sliceHtml}</a>`;+ html += `<a data-rangeid="${rangeId}" href="${range.properties?.url ?? ''}">${sliceHtml.sliceHtml}</a>`;break;}-default: {- html += sliceHtml;+ html += sliceHtml.sliceHtml;break;}}- cursorIndex = endIndex + 1;+ // 如果涉及 customBlock,需要跳过其占位符.+ cursorIndex = endIndex + 1 + (preHtml.customBlockLength + sliceHtml.customBlockLength);});- const endHtml = getBodyInlineSlice(body, cursorIndex, endIndex);- html += endHtml;+ const endHtml = handleCustomBlock(cursorIndex, endIndex);+ html += endHtml.sliceHtml;return html;}@@ -149,9 +191,10 @@ interface IHtmlResult {html: string;}-export function convertBodyToHtml(body: IDocumentBody): string {+export function convertBodyToHtml(doc: IDocumentData): string {+ const body = doc.body || {} as IDocumentBody;const { paragraphs = [], sectionBreaks = [] } = body;- let { dataStream } = body;+ let { dataStream = '' } = body;if (!dataStream.endsWith('\r\n')) {dataStream += '\r\n';@@ -174,18 +217,18 @@ export function convertBodyToHtml(body: IDocumentBody): string {const nodeList = parseDataStreamToTree(dataStream);for (const node of nodeList) {- processNode(node, body, result);+ processNode(node, doc, result);}return result.html;}-// eslint-disable-next-line max-lines-per-function-function processNode(node: DataStreamTreeNode, body: IDocumentBody, result: IHtmlResult) {+// eslint-disable-next-line max-lines-per-function, complexity+function processNode(node: DataStreamTreeNode, doc: IDocumentData, result: IHtmlResult) {switch (node.nodeType) {case DataStreamTreeNodeType.SECTION_BREAK: {for (const n of node.children) {- processNode(n, body, result);+ processNode(n, doc, result);}break;@@ -193,7 +236,7 @@ function processNode(node: DataStreamTreeNode, body: IDocumentBody, result: IHtmcase DataStreamTreeNodeType.PARAGRAPH: {const { children, startIndex, endIndex } = node;- const paragraph = body.paragraphs!.find((p) => p.startIndex === endIndex) ?? {} as IParagraph;+ const paragraph = doc.body?.paragraphs!.find((p) => p.startIndex === endIndex) ?? {} as IParagraph;const { paragraphStyle = {} } = paragraph;const { spaceAbove, spaceBelow, lineSpacing } = paragraphStyle;const style = [];@@ -222,11 +265,11 @@ function processNode(node: DataStreamTreeNode, body: IDocumentBody, result: IHtmif (children.length) {for (const table of children) {- processNode(table, body, result);+ processNode(table, doc, result);}}- result.html += `${getBodySliceHtml(body, startIndex, endIndex)}</p>`;+ result.html += `${getBodySliceHtml(doc, startIndex, endIndex)}</p>`;break;}@@ -237,7 +280,7 @@ function processNode(node: DataStreamTreeNode, body: IDocumentBody, result: IHtmresult.html += '<table class="UniverTable" style="width: 100%; border-collapse: collapse;"><tbody>';for (const row of children) {- processNode(row, body, result);+ processNode(row, doc, result);}result.html += '</tbody></table>';@@ -250,7 +293,7 @@ function processNode(node: DataStreamTreeNode, body: IDocumentBody, result: IHtmresult.html += '<tr class="UniverTableRow">';for (const cell of children) {- processNode(cell, body, result);+ processNode(cell, doc, result);}result.html += '</tr>';@@ -262,7 +305,7 @@ function processNode(node: DataStreamTreeNode, body: IDocumentBody, result: IHtmresult.html += '<td class="UniverTableCell">';for (const n of children) {- processNode(n, body, result);+ processNode(n, doc, result);}result.html += '</td>';break;@@ -275,15 +318,15 @@ function processNode(node: DataStreamTreeNode, body: IDocumentBody, result: IHtm}export class UDMToHtmlService {- convert(bodyList: IDocumentBody[]): string {- if (bodyList.length === 0) {+ convert(docList: IDocumentData[]): string {+ if (docList.length === 0) {throw new Error('The bodyList length at least to be 1');}let html = '';- for (const body of Tools.deepClone(bodyList)) {- html += convertBodyToHtml(body);+ for (const doc of Tools.deepClone(docList)) {+ html += convertBodyToHtml(doc);}return html;diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.tsindex 99743a4d5f..e5a3fd8a70 100644--- a/packages/ui/src/index.ts+++ b/packages/ui/src/index.ts@@ -45,6 +45,11 @@ export { DesktopBeforeCloseService, IBeforeCloseService } from './services/beforexport { CopyCommand, CutCommand, PasteCommand } from './services/clipboard/clipboard.command';export {BrowserClipboardService,+ FILE__BMP_CLIPBOARD_MIME_TYPE,+ FILE__JPEG_CLIPBOARD_MIME_TYPE,+ FILE__WEBP_CLIPBOARD_MIME_TYPE,+ FILE_PNG_CLIPBOARD_MIME_TYPE,+ FILE_SVG_XML_CLIPBOARD_MIME_TYPE,HTML_CLIPBOARD_MIME_TYPE,IClipboardInterfaceService,PLAIN_TEXT_CLIPBOARD_MIME_TYPE,diff --git a/packages/ui/src/services/clipboard/clipboard-interface.service.ts b/packages/ui/src/services/clipboard/clipboard-interface.service.tsindex 3c6f448a68..3716c09ed8 100644--- a/packages/ui/src/services/clipboard/clipboard-interface.service.ts+++ b/packages/ui/src/services/clipboard/clipboard-interface.service.ts@@ -21,6 +21,11 @@ import { supportClipboardAPI } from './clipboard-utils';export const PLAIN_TEXT_CLIPBOARD_MIME_TYPE = 'text/plain';export const HTML_CLIPBOARD_MIME_TYPE = 'text/html';+export const FILE_PNG_CLIPBOARD_MIME_TYPE = 'image/png';+export const FILE__JPEG_CLIPBOARD_MIME_TYPE = 'image/jpeg';+export const FILE__BMP_CLIPBOARD_MIME_TYPE = 'image/bmp';+export const FILE__WEBP_CLIPBOARD_MIME_TYPE = 'image/webp';+export const FILE_SVG_XML_CLIPBOARD_MIME_TYPE = 'image/svg+xml';/*** This interface provides an interface to access system's clipboard.@@ -162,7 +167,7 @@ export class BrowserClipboardService extends Disposable implements IClipboardIntselect(container);document.execCommand('copy');} finally {- // reset previous elements focus state+ // reset previous elements focus stateif (activeElement instanceof HTMLElement) {activeElement.focus();}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex e55b207caa..9bd75d3599 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1460,6 +1460,12 @@ importers:'@univerjs/docs':specifier: workspace:*version: link:../docs+ '@univerjs/docs-drawing':+ specifier: workspace:*+ version: link:../docs-drawing+ '@univerjs/drawing':+ specifier: workspace:*+ version: link:../drawing'@univerjs/engine-formula':specifier: workspace:*version: link:../engine-formula