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.ts index 5218b99e5da8..9630f851e25d 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 @@ -14,17 +14,18 @@ * limitations under the License. */ -import type { ICommand } from '@univerjs/core'; +import type { ICommand, IMutationInfo, JSONXActions } from '@univerjs/core'; import { CommandType, ICommandService, - IUndoRedoService, + IUniverInstanceService, + JSONX, + TextX, + TextXActionType, } from '@univerjs/core'; - -import { DocDrawingApplyType, IDocDrawingService, SetDocDrawingApplyMutation } from '@univerjs/docs-drawing'; import type { IAccessor } from '@wendellhu/redi'; -import type { IDrawingJsonUndo1 } from '@univerjs/drawing'; -import { ClearDocDrawingTransformerOperation } from '../operations/clear-drawing-transformer.operation'; +import type { IRichTextEditingMutationParams } from '@univerjs/docs'; +import { getRetainAndDeleteFromReplace, RichTextEditingMutation, TextSelectionManagerService } from '@univerjs/docs'; import type { IInsertDrawingCommandParams } from './interfaces'; /** @@ -32,44 +33,129 @@ import type { IInsertDrawingCommandParams } from './interfaces'; */ export const InsertDocDrawingCommand: ICommand = { id: 'doc.command.insert-doc-image', + type: CommandType.COMMAND, + + // eslint-disable-next-line max-lines-per-function handler: (accessor: IAccessor, params?: IInsertDrawingCommandParams) => { + if (params == null) { + return false; + } + const commandService = accessor.get(ICommandService); - const undoRedoService = accessor.get(IUndoRedoService); - const docDrawingService = accessor.get(IDocDrawingService); + const textSelectionManagerService = accessor.get(TextSelectionManagerService); + const univerInstanceService = accessor.get(IUniverInstanceService); - if (!params) return false; + const activeTextRange = textSelectionManagerService.getActiveRange(); + const documentDataModel = univerInstanceService.getCurrentUniverDocInstance(); + if (activeTextRange == null || documentDataModel == null) { + return false; + } // const { drawingParam, imageParam } = params; + const unitId = documentDataModel.getUnitId(); + const { drawings } = params; + const { collapsed, startOffset, endOffset, segmentId, style } = activeTextRange; + + const textX = new TextX(); + const jsonX = JSONX.getInstance(); + const rawActions: JSONXActions = []; + const drawingOrderLength = documentDataModel.getSnapshot().drawingsOrder?.length ?? 0; + + // Step 1: Insert placeholder `\b` in dataStream and add drawing to customBlocks. + if (collapsed) { + if (startOffset > 0) { + textX.push({ + t: TextXActionType.RETAIN, + len: startOffset, + segmentId, + }); + } + } else { + textX.push(...getRetainAndDeleteFromReplace(activeTextRange, segmentId)); + } + + textX.push({ + t: TextXActionType.INSERT, + body: { + dataStream: '\b'.repeat(drawings.length), + customBlocks: drawings.map((drawing, i) => ({ + startIndex: endOffset + i, + blockId: drawing.drawingId, + })), + }, + len: drawings.length, + line: 0, + segmentId, + }); + + const placeHolderAction = jsonX.editOp(textX.serialize()); + + rawActions.push(placeHolderAction!); - const drawings = params.drawings; + for (const drawing of drawings) { + const { drawingId } = drawing; + const addDrawingAction = jsonX.insertOp(['drawings', drawingId], drawing); + const addDrawingOrderAction = jsonX.insertOp(['drawingsOrder', drawingOrderLength], drawingId); + rawActions.push(addDrawingAction!); + rawActions.push(addDrawingOrderAction!); + } + + const textRanges = [ + { + startOffset: startOffset + drawings.length, + endOffset: startOffset + drawings.length, + collapsed: true, + style, + }, + ]; + + const doMutation: IMutationInfo = { + id: RichTextEditingMutation.id, + params: { + unitId, + actions: [], + textRanges, + }, + }; + + doMutation.params.actions = rawActions.reduce((acc, cur) => { + return JSONX.compose(acc, cur as JSONXActions); + }, null as JSONXActions); + + const result = commandService.syncExecuteCommand< + IRichTextEditingMutationParams, + IRichTextEditingMutationParams + >(doMutation.id, doMutation.params); + + return Boolean(result); // const sheetDrawingParams = drawings.map((param) => param.sheetDrawingParam); - const unitIds: string[] = drawings.map((param) => param.unitId); + // const unitIds = drawings.map((param) => param.unitId); // execute do mutations and add undo mutations to undo stack if completed - const jsonOp = docDrawingService.getBatchAddOp(drawings) as IDrawingJsonUndo1; - - const { unitId, subUnitId, undo, redo, objects } = jsonOp; - - const result = commandService.syncExecuteCommand(SetDocDrawingApplyMutation.id, { op: redo, unitId, subUnitId, objects, type: DocDrawingApplyType.INSERT }); - - if (result) { - undoRedoService.pushUndoRedo({ - unitID: unitId, - undoMutations: [ - { id: SetDocDrawingApplyMutation.id, params: { op: undo, unitId, subUnitId, objects, type: DocDrawingApplyType.REMOVE } }, - { id: ClearDocDrawingTransformerOperation.id, params: unitIds }, - ], - redoMutations: [ - { id: SetDocDrawingApplyMutation.id, params: { op: redo, unitId, subUnitId, objects, type: DocDrawingApplyType.INSERT } }, - { id: ClearDocDrawingTransformerOperation.id, params: unitIds }, - ], - }); - - return true; - } + // const jsonOp = docDrawingService.getBatchAddOp(drawings) as IDrawingJsonUndo1; + + // const { unitId, subUnitId, undo, redo, objects } = jsonOp; + + // const result = commandService.syncExecuteCommand(SetDocDrawingApplyMutation.id, { op: redo, unitId, subUnitId, objects, type: DocDrawingApplyType.INSERT }); + + // if (result) { + // undoRedoService.pushUndoRedo({ + // unitID: unitId, + // undoMutations: [ + // { id: SetDocDrawingApplyMutation.id, params: { op: undo, unitId, subUnitId, objects, type: DocDrawingApplyType.REMOVE } }, + // { id: ClearDocDrawingTransformerOperation.id, params: unitIds }, + // ], + // redoMutations: [ + // { id: SetDocDrawingApplyMutation.id, params: { op: redo, unitId, subUnitId, objects, type: DocDrawingApplyType.INSERT } }, + // { id: ClearDocDrawingTransformerOperation.id, params: unitIds }, + // ], + // }); + + // return true; + // } - return false; + // return false; }, }; diff --git a/packages/docs-drawing-ui/src/commands/operations/clear-drawing-transformer.operation.ts b/packages/docs-drawing-ui/src/commands/operations/clear-drawing-transformer.operation.ts index f37af42b595f..98931dfb333c 100644 --- a/packages/docs-drawing-ui/src/commands/operations/clear-drawing-transformer.operation.ts +++ b/packages/docs-drawing-ui/src/commands/operations/clear-drawing-transformer.operation.ts @@ -19,7 +19,7 @@ import { CommandType } from '@univerjs/core'; import { IRenderManagerService } from '@univerjs/engine-render'; export const ClearDocDrawingTransformerOperation: IMutation = { - id: 'sheet.operation.clear-drawing-transformer', + id: 'doc.operation.clear-drawing-transformer', type: CommandType.MUTATION, handler: (accessor, params) => { const renderManagerService = accessor.get(IRenderManagerService); diff --git a/packages/docs-drawing-ui/src/controllers/doc-drawing-update.controller.ts b/packages/docs-drawing-ui/src/controllers/doc-drawing-update.controller.ts index 014fc87952f1..4cd9a45bcc46 100644 --- a/packages/docs-drawing-ui/src/controllers/doc-drawing-update.controller.ts +++ b/packages/docs-drawing-ui/src/controllers/doc-drawing-update.controller.ts @@ -100,6 +100,7 @@ export class DocDrawingUpdateController extends Disposable { ); } + // eslint-disable-next-line max-lines-per-function private async _insertFloatImage(file: File) { let imageParam: Nullable; @@ -169,7 +170,9 @@ export class DocDrawingUpdateController extends Disposable { source, transform: docDrawingPositionToTransform(docTransform), docTransform, - title: '', description: '', layoutType: PositionedObjectLayoutType.WRAP_SQUARE, + title: '', + description: '', + layoutType: PositionedObjectLayoutType.WRAP_SQUARE, }; this._commandService.executeCommand(InsertDocDrawingCommand.id, { @@ -177,7 +180,7 @@ export class DocDrawingUpdateController extends Disposable { drawings: [docDrawingParam], } as IInsertDrawingCommandParams); - this._docSkeletonManagerService.getCurrent()?.skeleton.calculate(); + // this._docSkeletonManagerService.getCurrent()?.skeleton.calculate(); } private _getUnitInfo() { @@ -195,7 +198,9 @@ export class DocDrawingUpdateController extends Disposable { }; } - private _getImagePosition(imageWidth: number, imageHeight: number, sceneWidth: number, sceneHeight: number): Nullable { + private _getImagePosition( + imageWidth: number, imageHeight: number, _sceneWidth: number, _sceneHeight: number + ): Nullable { const activeTextRange = this._textSelectionManagerService.getActiveTextRange(); const position = activeTextRange?.getAbsolutePosition() || { left: 0, diff --git a/packages/docs-drawing/src/commands/mutations/set-drawing-apply.mutation.ts b/packages/docs-drawing/src/commands/mutations/set-drawing-apply.mutation.ts index 73643f0882e5..ff95fa59be2e 100644 --- a/packages/docs-drawing/src/commands/mutations/set-drawing-apply.mutation.ts +++ b/packages/docs-drawing/src/commands/mutations/set-drawing-apply.mutation.ts @@ -35,7 +35,9 @@ export interface ISetDrawingApplyMutationParams extends IDrawingJson1Type { export const SetDocDrawingApplyMutation: IMutation = { id: 'doc.mutation.set-drawing-apply', + type: CommandType.MUTATION, + handler: (accessor, params) => { const drawingManagerService = accessor.get(IDrawingManagerService); const docDrawingService = accessor.get(IDocDrawingService); diff --git a/packages/docs/src/commands/mutations/core-editing.mutation.ts b/packages/docs/src/commands/mutations/core-editing.mutation.ts index 2113143d5488..c62666e969dc 100644 --- a/packages/docs/src/commands/mutations/core-editing.mutation.ts +++ b/packages/docs/src/commands/mutations/core-editing.mutation.ts @@ -94,6 +94,10 @@ export const RichTextEditingMutation: IMutation