From 4130bf4c63dbf58b512ea66048e2b7f350a9eb40 Mon Sep 17 00:00:00 2001 From: lumixraku Date: Tue, 13 Aug 2024 20:18:40 +0800 Subject: [PATCH] fix: use focused unit when call command from UI --- .../src/render-manager/render-unit.ts | 2 +- .../commands/operations/activate.operation.ts | 11 +- .../operations/append-slide.operation.ts | 10 +- .../operations/delete-element.operation.ts | 9 +- .../operations/insert-image.operation.ts | 14 +- .../operations/insert-shape.operation.ts | 12 +- .../operations/insert-text.operation.ts | 14 +- .../operations/update-element.operation.ts | 12 +- .../slide-editing.render-controller.ts | 5 - .../controllers/slide.render-controller.ts | 136 +++++++++--------- 10 files changed, 117 insertions(+), 108 deletions(-) diff --git a/packages/engine-render/src/render-manager/render-unit.ts b/packages/engine-render/src/render-manager/render-unit.ts index 6e56edd91ae..afffb1375aa 100644 --- a/packages/engine-render/src/render-manager/render-unit.ts +++ b/packages/engine-render/src/render-manager/render-unit.ts @@ -44,7 +44,7 @@ export interface IRenderContext extends Omit { + const unitId = params.unitId; const canvasView = accessor.get(CanvasView); const univerInstanceService = accessor.get(IUniverInstanceService); - const model = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + // const model = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + + const model = univerInstanceService.getUnit(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; }, }; diff --git a/packages/slides-ui/src/commands/operations/append-slide.operation.ts b/packages/slides-ui/src/commands/operations/append-slide.operation.ts index 0d9bc2b3468..aaf68d755c7 100644 --- a/packages/slides-ui/src/commands/operations/append-slide.operation.ts +++ b/packages/slides-ui/src/commands/operations/append-slide.operation.ts @@ -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'; @@ -27,12 +27,16 @@ export const AppendSlideOperation: IOperation = { 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(UniverInstanceType.UNIVER_SLIDE); + // const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + + const slideData = univerInstanceService.getUnit(unitId); + if (!slideData) return false; const canvasView = accessor.get(CanvasView); - canvasView.appendPage(params.unitId); + canvasView.appendPage(unitId); return true; }, diff --git a/packages/slides-ui/src/commands/operations/delete-element.operation.ts b/packages/slides-ui/src/commands/operations/delete-element.operation.ts index 8c094706163..0014889735e 100644 --- a/packages/slides-ui/src/commands/operations/delete-element.operation.ts +++ b/packages/slides-ui/src/commands/operations/delete-element.operation.ts @@ -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 { @@ -29,8 +29,11 @@ export const DeleteSlideElementOperation: ICommand { if (!params?.id) return false; + const unitId = params.unitId; const univerInstanceService = accessor.get(IUniverInstanceService); - const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + // const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + + const slideData = univerInstanceService.getUnit(unitId); if (!slideData) return false; @@ -41,7 +44,7 @@ export const DeleteSlideElementOperation: ICommand(UniverInstanceType.UNIVER_SLIDE); + // const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + + const unitId = params.unitId; + const slideData = univerInstanceService.getUnit(unitId); if (!slideData) return false; const activePage = slideData.getActivePage()!; @@ -64,16 +67,13 @@ export const InsertSlideFloatImageOperation: ICommand = { 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(UniverInstanceType.UNIVER_SLIDE); + // const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + + const unitId = params.unitId; + const slideData = univerInstanceService.getUnit(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, @@ -72,11 +74,9 @@ export const InsertSlideShapeRectangleOperation: ICommand = { 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; @@ -47,7 +49,9 @@ export const SlideAddTextOperation: ICommand = { const textContent = params?.text || 'A New Text'; const univerInstanceService = accessor.get(IUniverInstanceService); - const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + // const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + + const slideData = univerInstanceService.getUnit(unitId); if (!slideData) return false; const activePage = slideData.getActivePage()!; @@ -78,10 +82,10 @@ export const SlideAddTextOperation: ICommand = { 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; diff --git a/packages/slides-ui/src/commands/operations/update-element.operation.ts b/packages/slides-ui/src/commands/operations/update-element.operation.ts index 548a5eeaf73..f1d67fb274c 100644 --- a/packages/slides-ui/src/commands/operations/update-element.operation.ts +++ b/packages/slides-ui/src/commands/operations/update-element.operation.ts @@ -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; @@ -26,19 +26,19 @@ export interface IUpdateElementOperationParams { export const UpdateSlideElementOperation: ICommand = { 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(UniverInstanceType.UNIVER_SLIDE); + // const slideData = univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE); + const unitId = params?.unitId; + const slideData = univerInstanceService.getUnit(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; }, }; diff --git a/packages/slides-ui/src/controllers/slide-editing.render-controller.ts b/packages/slides-ui/src/controllers/slide-editing.render-controller.ts index 63810b287a7..0da3f51dda5 100644 --- a/packages/slides-ui/src/controllers/slide-editing.render-controller.ts +++ b/packages/slides-ui/src/controllers/slide-editing.render-controller.ts @@ -21,7 +21,6 @@ import type { IPosition, Nullable, SlideDataModel, - UnitModel } from '@univerjs/core'; import { DEFAULT_EMPTY_DOCUMENT_VALUE, @@ -55,7 +54,6 @@ import { RichTextEditingMutation, TextSelectionManagerService, } from '@univerjs/docs'; -// import { IFunctionService, LexerTreeBuilder } from '@univerjs/engine-formula'; import type { DocBackground, Documents, @@ -81,7 +79,6 @@ import { filter } from 'rxjs'; import type { IEditorBridgeServiceVisibleParam } from '../services/slide-editor-bridge.service'; import { ISlideEditorBridgeService } from '../services/slide-editor-bridge.service'; import { ISlideEditorManagerService } from '../services/slide-editor-manager.service'; - import { SetTextEditArrowOperation } from '../commands/operations/text-edit.operation'; import { CursorChange } from '../type'; import { SLIDE_EDITOR_ID } from '../const'; @@ -106,8 +103,6 @@ export class SlideEditingRenderController extends Disposable implements IRenderM /** If the corresponding unit is active and prepared for editing. */ private _isUnitEditing = false; - // private _workbookSelections: WorkbookSelections; - private _d: Nullable; constructor( diff --git a/packages/slides-ui/src/controllers/slide.render-controller.ts b/packages/slides-ui/src/controllers/slide.render-controller.ts index 62cf62c095a..79a2654cf43 100644 --- a/packages/slides-ui/src/controllers/slide.render-controller.ts +++ b/packages/slides-ui/src/controllers/slide.render-controller.ts @@ -57,11 +57,11 @@ export class SlideRenderController extends RxDisposable implements IRenderModule */ private _addNewRender() { const { unitId, engine, scene } = this._renderContext; - const slideDataModel = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SLIDE); + const slideDataModel = this._getCurrUnitModel(); if (!slideDataModel) return; - // moved to slideRenderService@this._instanceSrv.getAllUnitsForType(UniverInstanceType.UNIVER_SLIDE).forEach((slideModel) + // createRender moved to slideRenderService@this._instanceSrv.getAllUnitsForType(UniverInstanceType.UNIVER_SLIDE).forEach((slideModel) // this._renderManagerService.createRender(unitId); //#region scene subscribe @@ -130,47 +130,6 @@ export class SlideRenderController extends RxDisposable implements IRenderModule }); } - get objectProvider() { - return this._objectProvider; - } - - activePage(_pageId?: string) { - let pageId = _pageId; - const model = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE)!; - let page: Nullable; - if (pageId) { - page = model.getPage(pageId); - } else { - const pageElements = model.getPages(); - const pageOrder = model.getPageOrder(); - if (pageOrder == null || pageElements == null) { - return; - } - page = pageElements[pageOrder[0]]; - - pageId = page.id; - } - - const render = this._currentRender(); - - if (page == null || render == null || render.mainComponent == null) { - return; - } - - const { id } = page; - - const slide = render.mainComponent as Slide; - - model.setActivePage(page); - - if (slide?.hasPage(id)) { - slide.changePage(id); - return; - } - - this.createPageScene(id, page); - } - private _scrollToCenter() { const mainScene = this._currentRender()?.scene; const viewMain = mainScene?.getViewport(SLIDE_KEY.VIEW); @@ -187,8 +146,10 @@ export class SlideRenderController extends RxDisposable implements IRenderModule } private _currentRender() { - const slideDataModel = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE)!; - return this._renderManagerService.getRenderById(slideDataModel.getUnitId()); + const renderUnit = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SLIDE); + return renderUnit; + // const slideDataModel = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE)!; + // return this._renderManagerService.getRenderById(slideDataModel.getUnitId()); } private _refreshThumb = debounce(() => { @@ -321,9 +282,58 @@ export class SlideRenderController extends RxDisposable implements IRenderModule slide.activeFirstPage(); } - createThumbs() { - const slideDataModel = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE)!; + private _createThumb(pageId: string) { + this._renderManagerService.createRender(pageId); + } + + /** + * SlideDataModel is UnitModel + */ + private _getCurrUnitModel() { + // return this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE)!; + + return this._renderContext.unit as SlideDataModel; + } + activePage(_pageId?: string) { + let pageId = _pageId; + const model = this._getCurrUnitModel(); + let page: Nullable; + if (pageId) { + page = model.getPage(pageId); + } else { + const pageElements = model.getPages(); + const pageOrder = model.getPageOrder(); + if (pageOrder == null || pageElements == null) { + return; + } + page = pageElements[pageOrder[0]]; + + pageId = page.id; + } + + const render = this._currentRender(); + + if (page == null || render == null || render.mainComponent == null) { + return; + } + + const { id } = page; + + const slide = render.mainComponent as Slide; + + model.setActivePage(page); + + if (slide?.hasPage(id)) { + slide.changePage(id); + return; + } + + this.createPageScene(id, page); + } + + createThumbs() { + const slideDataModel = this._getCurrUnitModel(); const pageOrder = slideDataModel.getPageOrder(); const render = this._currentRender(); @@ -343,13 +353,6 @@ export class SlideRenderController extends RxDisposable implements IRenderModule } } - private _createThumb(pageId: string) { - this._renderManagerService.createRender(pageId); - } - - // private _sceneMap = new Map(); - // setSceneMap$ = new Subject(); - /** * _initialize --> _create --> _addNewRender --> _createSlidePages --> page forEach --> createPageScene * _sceneMap.set(pageId, pageScene); @@ -361,7 +364,7 @@ export class SlideRenderController extends RxDisposable implements IRenderModule createPageScene(pageId: string, page: ISlidePage) { // const render = this._currentRender(); const render = this._renderContext; - if (!render || !this.objectProvider) { + if (!render || !this._objectProvider) { return; } @@ -389,7 +392,7 @@ export class SlideRenderController extends RxDisposable implements IRenderModule const { pageElements, pageBackgroundFill } = page; // SceneViewers - const objects = this.objectProvider.convertToRenderObjects(pageElements, mainScene); + const objects = this._objectProvider.convertToRenderObjects(pageElements, mainScene); if (!objects || !slide) return; this._addBackgroundRect(pageScene, pageBackgroundFill); @@ -434,15 +437,12 @@ export class SlideRenderController extends RxDisposable implements IRenderModule } createObjectToPage(element: IPageElement, pageID: PageID): Nullable { - const render = this._renderContext; //this._currentRender(); + const { scene } = this.getPageRenderUnit(pageID); - if (!render || !this.objectProvider) { + if (!scene || !this._objectProvider) { return; } - const { scene } = this.getPageRenderUnit(pageID); - if (!scene) return; - - const object = this.objectProvider.convertToRenderObject(element, scene); + const object = this._objectProvider.convertToRenderObject(element, scene); if (object) { scene.addObject(object); scene.attachTransformerTo(object); @@ -467,22 +467,22 @@ export class SlideRenderController extends RxDisposable implements IRenderModule } appendPage() { - const model = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SLIDE)!; + const model = this._getCurrUnitModel(); const page = model.getBlankPage(); - const render = this._currentRender(); if (page == null || render == null || render.mainComponent == null) { return; } - const { id } = page; + const { id: pageId } = page; const slide = render.mainComponent as Slide; + const scene = this.createPageScene(pageId, page); - const scene = this.createPageScene(id, page); - - scene && slide?.addPageScene(scene); + if (slide && scene) { + slide.addPageScene(scene); + } model.appendPage(page); model.setActivePage(page);