Skip to content

Commit

Permalink
feat: arrange
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Jun 19, 2024
1 parent 028e560 commit 1333ffe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,41 @@
* limitations under the License.
*/

import type { ICommand, Nullable } from '@univerjs/core';
import type { ICommand, IMutationInfo, JSONXActions, Nullable } from '@univerjs/core';
import {
CommandType,
ICommandService,
IUndoRedoService,
JSONX,
Tools,
} from '@univerjs/core';
import { DocDrawingApplyType, IDocDrawingService, SetDocDrawingApplyMutation } from '@univerjs/docs-drawing';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import { RichTextEditingMutation } from '@univerjs/docs';
import { IDocDrawingService } from '@univerjs/docs-drawing';
import type { IDrawingJsonUndo1, IDrawingOrderMapParam } from '@univerjs/drawing';
import { ArrangeTypeEnum } from '@univerjs/drawing';
import { ArrangeTypeEnum, IDrawingManagerService } from '@univerjs/drawing';
import type { IAccessor } from '@wendellhu/redi';

export interface ISetDrawingArrangeCommandParams extends IDrawingOrderMapParam {
arrangeType: ArrangeTypeEnum;
}

/**
* The command to insert new defined name
* The command to arrange drawings.
*/
export const SetDocDrawingArrangeCommand: ICommand = {
id: 'doc.command.set-drawing-arrange',

type: CommandType.COMMAND,

handler: (accessor: IAccessor, params?: ISetDrawingArrangeCommandParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const docDrawingService = accessor.get(IDocDrawingService);
const drawingManagerService = accessor.get(IDrawingManagerService);

if (params == null) {
return false;
}

const docDrawingService = accessor.get(IDocDrawingService);

const { unitId, subUnitId, drawingIds, arrangeType } = params;

const drawingOrderMapParam = { unitId, subUnitId, drawingIds } as IDrawingOrderMapParam;
Expand All @@ -64,24 +68,44 @@ export const SetDocDrawingArrangeCommand: ICommand = {
return false;
}

const { objects, redo, undo } = jsonOp;
const { objects, redo } = jsonOp;

if (redo == null) {
return false;
}

// TODO: @JOCS, Data model changes, automatically notifying drawingManagerService of modifications, and then updating the render layer?
drawingManagerService.applyJson1(unitId, subUnitId, redo);
docDrawingService.applyJson1(unitId, subUnitId, redo);
drawingManagerService.orderNotification(objects as IDrawingOrderMapParam);
docDrawingService.orderNotification(objects as IDrawingOrderMapParam);

const result = commandService.syncExecuteCommand(SetDocDrawingApplyMutation.id, { op: redo, unitId, subUnitId, objects, type: DocDrawingApplyType.ARRANGE });
const rawActions: JSONXActions = [];

if (result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: [
{ id: SetDocDrawingApplyMutation.id, params: { op: undo, unitId, subUnitId, objects, type: DocDrawingApplyType.ARRANGE } },
],
redoMutations: [
{ id: SetDocDrawingApplyMutation.id, params: { op: redo, unitId, subUnitId, objects, type: DocDrawingApplyType.ARRANGE } },
],
});
// TODO: @JOCS, It's best to build the actions yourself.
let redoCopy = Tools.deepClone(redo)! as JSONXActions;
redoCopy = redoCopy!.slice(3)! as JSONXActions;
redoCopy!.unshift('drawingsOrder');
rawActions.push(redoCopy!);

return true;
}
const doMutation: IMutationInfo<IRichTextEditingMutationParams> = {
id: RichTextEditingMutation.id,
params: {
unitId,
actions: [],
textRanges: null,
},
};

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 false;
return Boolean(result);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ export class DocDrawingAddRemoveController extends Disposable {
drawingManagerService.removeNotification(objects as IDrawingSearch[]);
docDrawingService.removeNotification(objects as IDrawingSearch[]);
}

private _updateDrawingsOrder() {

}
}
2 changes: 1 addition & 1 deletion packages/docs-drawing-ui/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { DocDrawingPopupMenuController } from './controllers/drawing-popup-menu.
import { DocDrawingUIController } from './controllers/doc-drawing.controller';
import { DocDrawingUpdateController } from './controllers/doc-drawing-update.controller';
import { DocDrawingTransformUpdateController } from './controllers/doc-drawing-transform-update.controller';
import { DocDrawingAddRemoveController } from './controllers/doc-drawing-add-remove.controller';
import { DocDrawingAddRemoveController } from './controllers/doc-drawing-notification.controller';
import { DocDrawingTransformerController } from './controllers/doc-drawing-transformer-update.controller';

const PLUGIN_NAME = 'Docs_Drawing_UI_PLUGIN';
Expand Down
12 changes: 11 additions & 1 deletion packages/drawing/src/services/drawing-manager-impl.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { Nullable } from '@univerjs/core';
import { sortRules, sortRulesByDesc } from '@univerjs/core';
import type { JSONOp, JSONOpList } from 'ot-json1';
import * as json1 from 'ot-json1';
import type { IDrawingGroupUpdateParam, IDrawingMap, IDrawingOrderMapParam, IDrawingOrderUpdateParam, IDrawingParam, IDrawingSearch, IDrawingSubunitMap, IDrawingVisibleParam, IUnitDrawingService } from './drawing-manager.service';
import type { IDrawingGroupUpdateParam, IDrawingMap, IDrawingMapItemData, IDrawingOrderMapParam, IDrawingOrderUpdateParam, IDrawingParam, IDrawingSearch, IDrawingSubunitMap, IDrawingVisibleParam, IUnitDrawingService } from './drawing-manager.service';

export interface IDrawingJsonUndo1 {
undo: JSONOp;
Expand Down Expand Up @@ -188,6 +188,11 @@ export class UnitDrawingService<T extends IDrawingParam> implements IUnitDrawing
return this._getDrawingData(unitId, subUnitId);
}

// Use in doc only.
setDrawingData(unitId: string, subUnitId: string, data: IDrawingMapItemData<T>) {
this.drawingManagerData[unitId][subUnitId].data = data;
}

getBatchAddOp(insertParams: T[]): IDrawingJsonUndo1 {
const objects: IDrawingSearch[] = [];
const ops: JSONOp[] = [];
Expand Down Expand Up @@ -482,6 +487,11 @@ export class UnitDrawingService<T extends IDrawingParam> implements IUnitDrawing
return this._getDrawingOrder(unitId, subUnitId);
}

// Use in doc only.
setDrawingOrder(unitId: string, subUnitId: string, order: string[]) {
this.drawingManagerData[unitId][subUnitId].order = order;
}

orderUpdateNotification(orderParams: IDrawingOrderMapParam) {
this._order$.next(orderParams);
}
Expand Down

0 comments on commit 1333ffe

Please sign in to comment.