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): copy paste image form univer doc to univer doc (#3227)

Ran Luoc2f417c6fea0
diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
index f27ec35ddb..3d41a08e2e 100644
--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { DocumentDataModel, ICustomRange, IDisposable, IDocumentBody, IDocumentData, IParagraph } from '@univerjs/core';
+import type { DocumentDataModel, ICustomRange, IDisposable, IDocumentBody, IDocumentData, IParagraph, Nullable } from '@univerjs/core';
import { createIdentifier, CustomRangeType, DataStreamTreeTokenType, Disposable, getBodySlice, ICommandService, ILogService, Inject, IUniverInstanceService, normalizeBody, SliceBodyType, toDisposable, Tools, UniverInstanceType } from '@univerjs/core';
import { HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui';
@@ -100,9 +100,9 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
}
async copy(sliceType: SliceBodyType = SliceBodyType.copy): Promise<boolean> {
- const { bodyList, needCache } = this._getDocumentBodyInRanges(sliceType);
+ const { bodyList = [], needCache = false, snapshot } = this._getDocumentBodyInRanges(sliceType) ?? {};
- if (bodyList.length === 0) {
+ if (bodyList.length === 0 || snapshot == null) {
return false;
}
@@ -110,7 +110,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
const activeRange = this._textSelectionManagerService.getActiveTextRangeWithStyle();
const isCopyInHeaderFooter = !!activeRange?.segmentId;
- this._setClipboardData(bodyList, !isCopyInHeaderFooter && needCache);
+ this._setClipboardData(bodyList, snapshot, !isCopyInHeaderFooter && needCache);
} catch (e) {
this._logService.error('[DocClipboardService] copy failed', e);
return false;
@@ -261,7 +261,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
}
}
- private async _setClipboardData(documentBodyList: IDocumentBody[], needCache = true): Promise<void> {
+ private async _setClipboardData(documentBodyList: IDocumentBody[], snapshot: IDocumentData, needCache = true): Promise<void> {
const copyId = genId();
const text =
documentBodyList.length > 1
@@ -272,7 +272,30 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
// Only cache copy content when the range is 1.
if (documentBodyList.length === 1 && needCache) {
html = html.replace(/(<[a-z]+)/, (_p0, p1) => `${p1} data-copy-id="${copyId}"`);
- copyContentCache.set(copyId, { body: documentBodyList[0] });
+ const body = documentBodyList[0];
+ const cache: Partial<IDocumentData> = { body };
+
+ if (body.customBlocks?.length) {
+ cache.drawings = {};
+
+ for (const block of body.customBlocks) {
+ const { blockId } = block;
+ const drawing = snapshot.drawings?.[blockId];
+
+ if (drawing) {
+ const id = Tools.generateRandomId(6);
+
+ block.blockId = id;
+
+ cache.drawings[id] = {
+ ...Tools.deepClone(drawing),
+ drawingId: id,
+ };
+ }
+ }
+ }
+
+ copyContentCache.set(copyId, cache);
}
return this._clipboardInterfaceService.write(text, html);
@@ -291,10 +314,11 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
}
// eslint-disable-next-line max-lines-per-function
- private _getDocumentBodyInRanges(sliceType: SliceBodyType): {
+ private _getDocumentBodyInRanges(sliceType: SliceBodyType): Nullable<{
bodyList: IDocumentBody[];
needCache: boolean;
- } {
+ snapshot: IDocumentData;
+ }> {
const docDataModel = this._univerInstanceService.getCurrentUniverDocInstance();
const allRanges = this._textSelectionManagerService.getDocRanges();
@@ -302,21 +326,17 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
let needCache = true;
if (docDataModel == null || allRanges.length === 0) {
- return {
- bodyList: results,
- needCache,
- };
+ return;
}
const segmentId = allRanges[0].segmentId;
const body = docDataModel?.getSelfOrHeaderFooterModel(segmentId)?.getBody();
+ const snapshot = docDataModel.getSnapshot();
+
if (body == null) {
- return {
- bodyList: results,
- needCache,
- };
+ return;
}
for (const range of allRanges) {
@@ -375,6 +395,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
return {
bodyList: results,
needCache,
+ snapshot,
};
}
diff --git a/packages/docs/src/commands/commands/clipboard.inner.command.ts b/packages/docs/src/commands/commands/clipboard.inner.command.ts
index 1fcf324aa6..9a2785a6ce 100644
--- a/packages/docs/src/commands/commands/clipboard.inner.command.ts
+++ b/packages/docs/src/commands/commands/clipboard.inner.command.ts
@@ -86,14 +86,14 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
id: 'doc.command.inner-paste',
type: CommandType.COMMAND,
- // eslint-disable-next-line max-lines-per-function
+ // eslint-disable-next-line max-lines-per-function, complexity
handler: async (accessor, params: IInnerPasteCommandParams) => {
const { segmentId, textRanges, doc } = params;
const commandService = accessor.get(ICommandService);
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const selections = textSelectionManagerService.getCurrentTextRanges();
- const { body, tableSource } = doc;
+ const { body, tableSource, drawings } = doc;
if (!Array.isArray(selections) || selections.length === 0 || body == null) {
return false;
}
@@ -124,6 +124,7 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
const rawActions: JSONXActions = [];
const hasTable = !!body.tables?.length;
+ const hasCustomBlock = !!body.customBlocks?.length;
// TODO: @JOCS A feature that has not yet been implemented,
// and it is currently not possible to paste tables in the header and footer.
@@ -160,6 +161,27 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
}
}
+ if (hasCustomBlock && drawings) {
+ const drawingLen = docDataModel.getSnapshot().drawingsOrder?.length ?? 0;
+
+ for (const block of cloneBody.customBlocks!) {
+ const { blockId } = block;
+
+ const drawingId = Tools.generateRandomId(6);
+
+ block.blockId = drawingId;
+
+ const drawing = Tools.deepClone(drawings[blockId]);
+ drawing.drawingId = drawingId;
+
+ const action = jsonX.insertOp(['drawings', drawingId], drawing);
+ const orderAction = jsonX.insertOp(['drawingsOrder', drawingLen], drawingId);
+
+ rawActions.push(action!);
+ rawActions.push(orderAction!);
+ }
+ }
+
if (collapsed) {
textX.push({
t: TextXActionType.RETAIN,