Skip to content

Commit

Permalink
fix: use focused unit when call command from UI
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Aug 14, 2024
1 parent 1772fd9 commit d2b52da
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 103 deletions.
2 changes: 1 addition & 1 deletion packages/engine-render/src/render-manager/render-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface IRenderContext<T extends UnitModel = UnitModel> extends Omit<IR
}

/**
* This class is responsible
* RenderUnit impl IRender
*/
export class RenderUnit extends Disposable implements IRender {
readonly isRenderUnit: boolean = true;
Expand Down
11 changes: 7 additions & 4 deletions packages/slides-ui/src/commands/operations/activate.operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { IAccessor, IOperation, SlideDataModel } from '@univerjs/core';
import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { CommandType, IUniverInstanceService } from '@univerjs/core';
import { CanvasView } from '../../controllers/canvas-view';

export interface IActiveSlidePageOperationParams {
Expand All @@ -26,21 +26,24 @@ export const ActivateSlidePageOperation: IOperation<IActiveSlidePageOperationPar
id: 'slide.operation.activate-slide',
type: CommandType.OPERATION,
handler: (accessor: IAccessor, params: IActiveSlidePageOperationParams) => {
const unitId = params.unitId;
const canvasView = accessor.get(CanvasView);
const univerInstanceService = accessor.get(IUniverInstanceService);
const model = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
// const model = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);

const model = univerInstanceService.getUnit<SlideDataModel>(unitId);
const pageId = model?.getActivePage()?.id;

if (!pageId) return false;

const page = canvasView.getRenderUnitByPageId(pageId, params.unitId);
const page = canvasView.getRenderUnitByPageId(pageId, unitId);
if (!page) return false;
const transformer = page.scene?.getTransformer();
if (transformer) {
transformer.clearControls();
}

canvasView.activePage(params.id, params.unitId);
canvasView.activePage(params.id, unitId);
return true;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { IOperation, SlideDataModel } from '@univerjs/core';
import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { CommandType, IUniverInstanceService } from '@univerjs/core';

import { CanvasView } from '../../controllers/canvas-view';

Expand All @@ -27,12 +27,16 @@ export const AppendSlideOperation: IOperation<IAppendSlideOperationParams> = {
id: 'slide.operation.append-slide',
type: CommandType.OPERATION,
handler: (accessor, params: IAppendSlideOperationParams) => {
const unitId = params.unitId;
const univerInstanceService = accessor.get(IUniverInstanceService);
const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
// const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);

const slideData = univerInstanceService.getUnit<SlideDataModel>(unitId);

if (!slideData) return false;

const canvasView = accessor.get(CanvasView);
canvasView.appendPage(params.unitId);
canvasView.appendPage(unitId);

return true;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICommand, SlideDataModel } from '@univerjs/core';
import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { CommandType, IUniverInstanceService } from '@univerjs/core';
import { CanvasView } from '../../controllers/canvas-view';

export interface IDeleteElementOperationParams {
Expand All @@ -29,8 +29,11 @@ export const DeleteSlideElementOperation: ICommand<IDeleteElementOperationParams
handler: (accessor, params) => {
if (!params?.id) return false;

const unitId = params.unitId;
const univerInstanceService = accessor.get(IUniverInstanceService);
const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
// const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);

const slideData = univerInstanceService.getUnit<SlideDataModel>(unitId);

if (!slideData) return false;

Expand All @@ -41,7 +44,7 @@ export const DeleteSlideElementOperation: ICommand<IDeleteElementOperationParams
slideData.updatePage(activePage.id, activePage);

const canvasview = accessor.get(CanvasView);
canvasview.removeObjectById(params.id, activePage.id, params.unitId);
canvasview.removeObjectById(params.id, activePage.id, unitId);

return true;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICommand, Nullable, SlideDataModel } from '@univerjs/core';
import { CommandType, IUniverInstanceService, PageElementType, UniverInstanceType } from '@univerjs/core';
import { CommandType, IUniverInstanceService, PageElementType } from '@univerjs/core';
import { getImageSize, IImageIoService } from '@univerjs/drawing';
import { CanvasView } from '../../controllers/canvas-view';

Expand All @@ -38,7 +38,10 @@ export const InsertSlideFloatImageOperation: ICommand<IInsertImageOperationParam
const { width, height, image } = await getImageSize(base64Cache || '');

const univerInstanceService = accessor.get(IUniverInstanceService);
const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
// const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);

const unitId = params.unitId;
const slideData = univerInstanceService.getUnit<SlideDataModel>(unitId);
if (!slideData) return false;

const activePage = slideData.getActivePage()!;
Expand All @@ -64,16 +67,13 @@ export const InsertSlideFloatImageOperation: ICommand<IInsertImageOperationParam
},
},
};

activePage.pageElements[imageId] = data;
// console.log(activePage.id);

slideData.updatePage(activePage.id, activePage);

const canvasview = accessor.get(CanvasView);
const sceneObject = canvasview.createObjectToPage(data, activePage.id, params.unitId);
const sceneObject = canvasview.createObjectToPage(data, activePage.id, unitId);
if (sceneObject) {
canvasview.setObjectActiveByPage(sceneObject, activePage.id, params.unitId);
canvasview.setObjectActiveByPage(sceneObject, activePage.id, unitId);
}
// console.log(slideData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { IAccessor, ICommand, SlideDataModel } from '@univerjs/core';
import { BasicShapes, CommandType, generateRandomId, ICommandService, IUniverInstanceService, LocaleService, PageElementType, UniverInstanceType } from '@univerjs/core';
import { BasicShapes, CommandType, generateRandomId, ICommandService, IUniverInstanceService, LocaleService, PageElementType } from '@univerjs/core';
import { ObjectType } from '@univerjs/engine-render';

import { ISidebarService } from '@univerjs/ui';
Expand All @@ -40,18 +40,20 @@ export const InsertSlideShapeRectangleCommand: ICommand = {
export const InsertSlideShapeRectangleOperation: ICommand<IInsertShapeOperationParams> = {
id: 'slide.operation.insert-float-shape',
type: CommandType.OPERATION,
handler: async (accessor, params) => {
handler: async (accessor, params: IInsertShapeOperationParams) => {
const id = generateRandomId(6);

const univerInstanceService = accessor.get(IUniverInstanceService);
const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
// const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);

const unitId = params.unitId;
const slideData = univerInstanceService.getUnit<SlideDataModel>(unitId);

if (!slideData) return false;

const activePage = slideData.getActivePage()!;
const elements = Object.values(activePage.pageElements);
const maxIndex = (elements?.length) ? Math.max(...elements.map((element) => element.zIndex)) : 20;

const data = {
id,
zIndex: maxIndex + 1,
Expand All @@ -72,11 +74,9 @@ export const InsertSlideShapeRectangleOperation: ICommand<IInsertShapeOperationP
},
},
};

activePage.pageElements[id] = data;
slideData.updatePage(activePage.id, activePage);

const unitId = params?.unitId || '';
const canvasview = accessor.get(CanvasView);
const sceneObject = canvasview.createObjectToPage(data, activePage.id, unitId);
if (sceneObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICommand, IPageElement, SlideDataModel } from '@univerjs/core';
import { CommandType, ICommandService, IUniverInstanceService, PageElementType, Tools, UniverInstanceType } from '@univerjs/core';
import { CommandType, generateRandomId, ICommandService, IUniverInstanceService, PageElementType } from '@univerjs/core';
import { CanvasView } from '../../controllers/canvas-view';

export interface ISlideAddTextParam {
Expand All @@ -39,15 +39,19 @@ export const SlideAddTextOperation: ICommand<ISlideAddTextParam> = {
id: 'slide.operation.add-text',
type: CommandType.OPERATION,
handler: async (accessor, params: ISlideAddTextParam) => {
const elementId = Tools.generateRandomId(6);
const unitId = params.unitId;

const elementId = generateRandomId(6);
const defaultWidth = 220;
const defaultheight = 40;
const left = 230;
const top = 142;
const textContent = params?.text || 'A New Text';

const univerInstanceService = accessor.get(IUniverInstanceService);
const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
// const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);

const slideData = univerInstanceService.getUnit<SlideDataModel>(unitId);
if (!slideData) return false;

const activePage = slideData.getActivePage()!;
Expand Down Expand Up @@ -78,10 +82,10 @@ export const SlideAddTextOperation: ICommand<ISlideAddTextParam> = {
slideData.updatePage(activePage.id, activePage);

const canvasview = accessor.get(CanvasView);
const sceneObject = canvasview.createObjectToPage(elementData, activePage.id, params.unitId);
const sceneObject = canvasview.createObjectToPage(elementData, activePage.id, unitId);
// make object active: a control rect wrap the object.
if (sceneObject) {
canvasview.setObjectActiveByPage(sceneObject, activePage.id, params.unitId);
canvasview.setObjectActiveByPage(sceneObject, activePage.id, unitId);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICommand, SlideDataModel } from '@univerjs/core';
import { CommandType, IUniverInstanceService, Tools, UniverInstanceType } from '@univerjs/core';
import { CommandType, IUniverInstanceService, Tools } from '@univerjs/core';

export interface IUpdateElementOperationParams {
unitId: string;
Expand All @@ -26,19 +26,19 @@ export interface IUpdateElementOperationParams {
export const UpdateSlideElementOperation: ICommand<IUpdateElementOperationParams> = {
id: 'slide.operation.update-element',
type: CommandType.OPERATION,
handler: (accessor, params) => {
handler: (accessor, params: IUpdateElementOperationParams) => {
const { oKey, props } = params!;

const univerInstanceService = accessor.get(IUniverInstanceService);
const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
// const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);

const unitId = params?.unitId;
const slideData = univerInstanceService.getUnit<SlideDataModel>(unitId);
if (!slideData) return false;

const activePage = slideData.getActivePage()!;

activePage.pageElements[oKey] = Tools.deepMerge(activePage.pageElements[oKey], props);

slideData.updatePage(activePage.id, activePage);

return true;
},
};
Loading

0 comments on commit d2b52da

Please sign in to comment.