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
fix(docs): remove content and insert drawing (#3198)
Ran Luof68e8ebe85db
diff --git a/packages/docs-drawing-ui/src/commands/commands/insert-doc-drawing.command.ts b/packages/docs-drawing-ui/src/commands/commands/insert-doc-drawing.command.tsindex ce40768b25..59aeb2ca8f 100644--- a/packages/docs-drawing-ui/src/commands/commands/insert-doc-drawing.command.ts+++ b/packages/docs-drawing-ui/src/commands/commands/insert-doc-drawing.command.ts@@ -24,7 +24,7 @@ import {TextXActionType,} from '@univerjs/core';import type { IRichTextEditingMutationParams } from '@univerjs/docs';-import { getRetainAndDeleteFromReplace, getRichTextEditPath, RichTextEditingMutation, TextSelectionManagerService } from '@univerjs/docs';+import { getCustomBlockIdsInSelections, getRetainAndDeleteFromReplace, getRichTextEditPath, RichTextEditingMutation, TextSelectionManagerService } from '@univerjs/docs';import type { IInsertDrawingCommandParams } from './interfaces';/**@@ -64,6 +64,7 @@ export const InsertDocDrawingCommand: ICommand = {const jsonX = JSONX.getInstance();const rawActions: JSONXActions = [];const drawingOrderLength = documentDataModel.getSnapshot().drawingsOrder?.length ?? 0;+ let removeDrawingLen = 0;// Step 1: Insert placeholder `\b` in dataStream and add drawing to customBlocks.if (collapsed) {@@ -77,6 +78,37 @@ export const InsertDocDrawingCommand: ICommand = {} else {const { dos } = getRetainAndDeleteFromReplace(activeTextRange, segmentId, 0, body);textX.push(...dos);++ const removedCustomBlockIds = getCustomBlockIdsInSelections(body, [activeTextRange]);+ const drawings = documentDataModel.getDrawings() ?? {};+ const drawingOrder = documentDataModel.getDrawingsOrder() ?? [];+ const sortedRemovedCustomBlockIds = removedCustomBlockIds.sort((a, b) => {+ if (drawingOrder.indexOf(a) > drawingOrder.indexOf(b)) {+ return -1;+ } else if (drawingOrder.indexOf(a) < drawingOrder.indexOf(b)) {+ return 1;+ }++ return 0;+ });++ if (sortedRemovedCustomBlockIds.length > 0) {+ for (const blockId of sortedRemovedCustomBlockIds) {+ const drawing = drawings[blockId];+ const drawingIndex = drawingOrder.indexOf(blockId);+ if (drawing == null || drawingIndex < 0) {+ continue;+ }++ const removeDrawingAction = jsonX.removeOp(['drawings', blockId], drawing);+ const removeDrawingOrderAction = jsonX.removeOp(['drawingsOrder', drawingIndex], blockId);++ rawActions.push(removeDrawingAction!);+ rawActions.push(removeDrawingOrderAction!);++ removeDrawingLen++;+ }+ }}textX.push({@@ -102,7 +134,7 @@ export const InsertDocDrawingCommand: ICommand = {for (const drawing of drawings) {const { drawingId } = drawing;const addDrawingAction = jsonX.insertOp(['drawings', drawingId], drawing);- const addDrawingOrderAction = jsonX.insertOp(['drawingsOrder', drawingOrderLength], drawingId);+ const addDrawingOrderAction = jsonX.insertOp(['drawingsOrder', drawingOrderLength - removeDrawingLen], drawingId);rawActions.push(addDrawingAction!);rawActions.push(addDrawingOrderAction!);diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.tsindex 131c32a98a..944bc4a6d9 100644--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts@@ -161,6 +161,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServif (rectRanges.length > 0) {cursor = getCursorWhenDelete(textRanges as Readonly<TextRange[]>, rectRanges);} else if (activeEndOffset != null) {+ cursor = activeEndOffset;for (const range of textRanges) {const { startOffset, endOffset } = range;@@ -183,7 +184,8 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ];return this._commandService.executeCommand(CutContentCommand.id, { segmentId, textRanges: newTextRanges });- } catch (e) {+ // eslint-disable-next-line unused-imports/no-unused-vars+ } catch (_e) {this._logService.error('[DocClipboardController] cut content failed');return false;}diff --git a/packages/docs/src/commands/commands/clipboard.inner.command.ts b/packages/docs/src/commands/commands/clipboard.inner.command.tsindex 0079523cb5..1fcf324aa6 100644--- a/packages/docs/src/commands/commands/clipboard.inner.command.ts+++ b/packages/docs/src/commands/commands/clipboard.inner.command.ts@@ -22,6 +22,7 @@ import type {IMutationInfo,ITextRange,JSONXActions,+ Nullable,} from '@univerjs/core';import {CommandType,@@ -43,7 +44,30 @@ import { getCommandSkeleton, getRichTextEditPath } from '../util';import { getRetainAndDeleteAndExcludeLineBreak } from '../../basics/replace';import { getDeleteRowContentActionParams, getDeleteRowsActionsParams, getDeleteTableActionParams } from './table/table';-function hasRangeInTable(ranges: TextRange[]) {+export function getCustomBlockIdsInSelections(body: IDocumentBody, selections: ITextRange[]): string[] {+ const customBlockIds: string[] = [];+ const { customBlocks = [] } = body;++ for (const selection of selections) {+ const { startOffset, endOffset } = selection;++ if (startOffset == null || endOffset == null) {+ continue;+ }++ for (const customBlock of customBlocks) {+ const { startIndex } = customBlock;++ if (startIndex >= startOffset && startIndex < endOffset) {+ customBlockIds.push(customBlock.blockId);+ }+ }+ }++ return customBlockIds;+}++function hasRangeInTable(ranges: TextRange[]): boolean {return ranges.some((range) => {const { anchorNodePosition } = range;@@ -377,6 +401,33 @@ function getCutActionsFromRectRanges(}, null as JSONXActions);}+export function getCutActionsFromDocRanges(+ textRanges: Readonly<Nullable<(ITextRange | TextRange)[]>>,+ rectRanges: Readonly<Nullable<RectRange[]>>,+ docDataModel: DocumentDataModel,+ viewModel: DocumentViewModel,+ segmentId: string+): JSONXActions {+ let rawActions: JSONXActions = [];++ if (Array.isArray(textRanges) && textRanges?.length !== 0) {+ rawActions = getCutActionsFromTextRanges(textRanges, docDataModel, segmentId);+ }+ if (Array.isArray(rectRanges) && rectRanges?.length !== 0) {+ const actions = getCutActionsFromRectRanges(rectRanges, docDataModel, viewModel, segmentId);+ if (rawActions == null || rawActions.length === 0) {+ rawActions = actions;+ } else {+ rawActions = JSONX.compose(+ rawActions,+ JSONX.transform(actions, rawActions, 'right')!+ ) as JSONXActions;+ }+ }++ return rawActions;+}+export interface IInnerCutCommandParams {segmentId: string;textRanges: ITextRangeWithStyle[];@@ -431,20 +482,7 @@ export const CutContentCommand: ICommand<IInnerCutCommandParams> = {},};- if (Array.isArray(selections) && selections?.length !== 0) {- doMutation.params.actions = getCutActionsFromTextRanges(selections, docDataModel, segmentId);- }- if (Array.isArray(rectRanges) && rectRanges?.length !== 0) {- const actions = getCutActionsFromRectRanges(rectRanges, docDataModel, viewModel, segmentId);- if (doMutation.params.actions?.length === 0 || doMutation.params.actions == null) {- doMutation.params.actions = actions;- } else {- doMutation.params.actions = JSONX.compose(- doMutation.params.actions,- JSONX.transform(actions, doMutation.params.actions, 'right')!- ) as JSONXActions;- }- }+ doMutation.params.actions = getCutActionsFromDocRanges(selections, rectRanges, docDataModel, viewModel, segmentId);const result = commandService.syncExecuteCommand<IRichTextEditingMutationParams,@@ -455,26 +493,3 @@ export const CutContentCommand: ICommand<IInnerCutCommandParams> = {},};-function getCustomBlockIdsInSelections(body: IDocumentBody, selections: ITextRange[]): string[] {- const customBlockIds: string[] = [];- const { customBlocks = [] } = body;-- for (const selection of selections) {- const { startOffset, endOffset } = selection;-- if (startOffset == null || endOffset == null) {- continue;- }-- for (const customBlock of customBlocks) {- const { startIndex } = customBlock;-- if (startIndex >= startOffset && startIndex < endOffset) {- customBlockIds.push(customBlock.blockId);- }- }- }-- return customBlockIds;-}-diff --git a/packages/docs/src/index.ts b/packages/docs/src/index.tsindex 4690d8eb88..c8c366b38e 100644--- a/packages/docs/src/index.ts+++ b/packages/docs/src/index.ts@@ -106,6 +106,7 @@ export { ChangeListNestingLevelType } from './commands/commands/list.command';export { getCommandSkeleton } from './commands/util';export { generateParagraphs } from './commands/commands/break-line.command';export type { IInnerCutCommandParams } from './commands/commands/clipboard.inner.command';+export { getCutActionsFromDocRanges, getCustomBlockIdsInSelections } from './commands/commands/clipboard.inner.command';export { CreateDocTableCommand, type ICreateDocTableCommandParams } from './commands/commands/table/doc-table-create.command';export { DocTableDeleteRowsCommand, DocTableDeleteColumnsCommand, DocTableDeleteTableCommand } from './commands/commands/table/doc-table-delete.command';export type { IDocTableDeleteRowsCommandParams, IDocTableDeleteColumnsCommandParams, IDocTableDeleteTableCommandParams } from './commands/commands/table/doc-table-delete.command';