Skip to content

Commit

Permalink
feat: addtext slide (#2866)
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku authored Jul 27, 2024
1 parent 094b646 commit d87df25
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/engine-render/src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class Layer extends Disposable {
* insert o to _objects[]
* if o is a group, insert all its children and group itself to _objects[]
* @param o
* @returns
* @returns this
*/
addObject(o: BaseObject) {
if (o.classType === RENDER_CLASS_TYPE.GROUP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import type { Dependency, DependencyIdentifier, IDisposable, Nullable, UnitModel, UnitType, UniverInstanceType } from '@univerjs/core';
import { createIdentifier, Disposable, Inject, Injector, IUniverInstanceService, remove, toDisposable } from '@univerjs/core';
import type { Dependency, DependencyIdentifier, IDisposable, Nullable, UnitModel, UnitType } from '@univerjs/core';
import { createIdentifier, Disposable, Inject, Injector, IUniverInstanceService, remove, toDisposable, UniverInstanceType } from '@univerjs/core';
import type { Observable } from 'rxjs';
import { BehaviorSubject, Subject } from 'rxjs';

Expand Down Expand Up @@ -148,9 +148,17 @@ export class RenderManagerService extends Disposable implements IRenderManagerSe
dependencies.push(ctor);

for (const [renderUnitId, render] of this._renderMap) {
const renderType = this._univerInstanceService.getUnitType(renderUnitId);
if (renderType === type) {
(render as RenderUnit).addRenderDependencies([ctor]);
if (type === UniverInstanceType.UNIVER_SLIDE) {
// const renderType = this._univerInstanceService.getUnitType(renderUnitId);
// renderUnitId slide_test cover_1 rect_1....
if (this._univerInstanceService.getUnit(renderUnitId)) {
(render as RenderUnit).addRenderDependencies([ctor]);
}
} else {
const renderType = this._univerInstanceService.getUnitType(renderUnitId);
if (renderType === type) {
(render as RenderUnit).addRenderDependencies([ctor]);
}
}
}

Expand Down Expand Up @@ -217,7 +225,7 @@ export class RenderManagerService extends Disposable implements IRenderManagerSe
height,
});

const unit = this._univerInstanceService.getUnit(unitId)!;
const unit = this._univerInstanceService.getUnit(unitId);
let renderUnit: IRender;

if (unit) {
Expand Down
4 changes: 4 additions & 0 deletions packages/engine-render/src/scene.transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,10 @@ export class Transformer extends Disposable implements ITransformerConfig {
return currentScene.getEngine()?.activeScene as Nullable<Scene>;
}

activeAnObject(applyObject: BaseObject) {
this._updateActiveObjectList(applyObject, {} as IPointerEvent);
}

private _updateActiveObjectList(applyObject: BaseObject, evt: IPointerEvent | IMouseEvent) {
const { isCropper } = this._getConfig(applyObject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class SheetRenderController extends RxDisposable implements IRenderModule

private _addNewRender() {
const { scene, engine, unit: workbook } = this._context;

scene.addLayer(new Layer(scene, [], 0), new Layer(scene, [], 2));

this._addComponent(workbook);
Expand Down
3 changes: 2 additions & 1 deletion packages/slides-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"@univerjs/slides": "workspace:*",
"@univerjs/ui": "workspace:*",
"clsx": ">=2.0.0",
"react": "^16.9.0 || ^17.0.0 || ^18.0.0"
"react": "^16.9.0 || ^17.0.0 || ^18.0.0",
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.65"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

export interface ISlideAddTextParam {
text: string;
};

export const SlideAddTextOperation: ICommand<ISlideAddTextParam> = {
id: 'slide.operation.add-text',
type: CommandType.OPERATION,
handler: async (accessor, params) => {
const elementId = Tools.generateRandomId(6);
const defaultWidth = 220;
const defaultheight = 40;

const textContent = params?.text || 'A New Text';
const elmentData = {
id: elementId,
zIndex: 2,
left: 230,
top: 142,
width: defaultWidth,
height: defaultheight,
title: 'text',
description: '',
type: PageElementType.TEXT,
richText: {
text: textContent,
fs: 30,
cl: {
rgb: 'rgb(51, 51, 51)',
},
bl: 1,
},
};

const univerInstanceService = accessor.get(IUniverInstanceService);
const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
if (!slideData) return false;

const activePage = slideData.getActivePage()!;
activePage.pageElements[elementId] = elmentData;
slideData.updatePage(activePage.id, activePage);

const canvasview = accessor.get(CanvasView);
const sceneObject = canvasview.createObjectToPage(elmentData, activePage.id);
if (sceneObject) {
canvasview.setObjectActiveByPage(sceneObject, activePage.id);
}

return true;
},
};
3 changes: 2 additions & 1 deletion packages/slides-ui/src/controllers/image.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { getMenuHiddenObservable, type IMenuItem, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';
import type { IMenuItem } from '@univerjs/ui';
import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';
import type { IAccessor } from '@univerjs/core';
import { UniverInstanceType } from '@univerjs/core';
import { COMPONENT_UPLOAD_FILE_MENU, UploadFileType } from '../components/upload-component/component-name';
Expand Down
13 changes: 9 additions & 4 deletions packages/slides-ui/src/controllers/slide-ui.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@

import type { DependencyOverride } from '@univerjs/core';
import { connectInjector, Disposable, ICommandService, Inject, Injector, LifecycleStages, OnLifecycle } from '@univerjs/core';
import { AddImageSingle, GraphSingle, TextSingle } from '@univerjs/icons';
import type { MenuConfig } from '@univerjs/ui';
import { BuiltInUIPart, ComponentManager, IMenuService, IUIPartsService } from '@univerjs/ui';
import { AddImageSingle, GraphSingle } from '@univerjs/icons';
import { SlideSideBar } from '../views/slide-bar/SlideBar';
import { ActivateSlidePageOperation } from '../commands/operations/activate.operation';
import { InsertSlideFloatImageOperation } from '../commands/operations/insert-image.operation';
import { InsertSlideShapeRectangleOperation } from '../commands/operations/insert-shape.operation';
import { SlideAddTextOperation } from '../commands/operations/insert-text.operation';
import { SetSlidePageThumbOperation } from '../commands/operations/set-thumb.operation';
import { UploadFileMenu } from '../components/upload-component/UploadFile';
import { COMPONENT_UPLOAD_FILE_MENU } from '../components/upload-component/component-name';
import { InsertSlideFloatImageOperation } from '../commands/operations/insert-image.operation';
import { InsertSlideShapeRectangleOperation } from '../commands/operations/insert-shape.operation';
import { SlideSideBar } from '../views/slide-bar/SlideBar';
import { IMAGE_UPLOAD_ICON, SlideImageMenuFactory, UploadSlideFloatImageMenuFactory } from './image.menu';
import { GRAPH_SINGLE_ICON, SlideShapeMenuFactory, UploadSlideFloatShapeMenuFactory } from './shape.menu';
import { AddTextMenuItemFactory, TEXT_ICON_ID } from './text.menu';

export interface IUniverSlidesDrawingConfig {
menu?: MenuConfig;
Expand Down Expand Up @@ -66,6 +68,7 @@ export class SlideUIController extends Disposable {
UploadSlideFloatImageMenuFactory,
SlideShapeMenuFactory,
UploadSlideFloatShapeMenuFactory,
AddTextMenuItemFactory,
].forEach((menuFactory) => {
this._menuService.addMenuItem(menuFactory(this._injector), menu);
});
Expand All @@ -74,6 +77,7 @@ export class SlideUIController extends Disposable {
private _initCustomComponents(): void {
const componentManager = this._componentManager;
this.disposeWithMe(componentManager.register(IMAGE_UPLOAD_ICON, AddImageSingle));
this.disposeWithMe(componentManager.register(TEXT_ICON_ID, TextSingle));
this.disposeWithMe(componentManager.register(GRAPH_SINGLE_ICON, GraphSingle));
this.disposeWithMe(componentManager.register(COMPONENT_UPLOAD_FILE_MENU, UploadFileMenu));
// this.disposeWithMe(componentManager.register(COMPONENT_SHEET_DRAWING_PANEL, SheetDrawingPanel));
Expand All @@ -84,6 +88,7 @@ export class SlideUIController extends Disposable {
ActivateSlidePageOperation,
SetSlidePageThumbOperation,
InsertSlideFloatImageOperation,
SlideAddTextOperation,
InsertSlideShapeRectangleOperation,
].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}
Expand Down
28 changes: 28 additions & 0 deletions packages/slides-ui/src/controllers/slide.render-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { RxDisposable } from '@univerjs/core';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';

export class SlideRenderController extends RxDisposable implements IRenderModule {
constructor(private readonly _context: IRenderContext<any>) {
super();
}

override dispose() {
//...
}
}
36 changes: 36 additions & 0 deletions packages/slides-ui/src/controllers/text.menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IAccessor } from '@univerjs/core';
import { UniverInstanceType } from '@univerjs/core';
import type { IMenuButtonItem } from '@univerjs/ui';
import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';
import { SlideAddTextOperation } from '../commands/operations/insert-text.operation';

export const TEXT_ICON_ID = 'text-single';

export function AddTextMenuItemFactory(_accessor: IAccessor): IMenuButtonItem {
return {
id: SlideAddTextOperation.id,
group: MenuGroup.TOOLBAR_FORMULAS_INSERT,
type: MenuItemType.BUTTON,
icon: TEXT_ICON_ID,
title: 'addText',
tooltip: 'addText',
positions: [MenuPosition.TOOLBAR_START],
hidden$: getMenuHiddenObservable(_accessor, UniverInstanceType.UNIVER_SLIDE),
};
}
19 changes: 13 additions & 6 deletions packages/slides-ui/src/slides-ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
import type { Dependency, SlideDataModel } from '@univerjs/core';
import { Inject, Injector, IUniverInstanceService, Plugin, UniverInstanceType } from '@univerjs/core';

import type { MenuConfig } from '@univerjs/ui';
import { IImageIoService, ImageIoService } from '@univerjs/drawing';
import { IRenderManagerService } from '@univerjs/engine-render';
import { SlideUIController } from './controllers/slide-ui.controller';
import type { IUniverSlidesDrawingConfig } from './controllers/slide-ui.controller';
import { SlideRenderController } from './controllers/slide.render-controller';

export const SLIDE_UI_PLUGIN_NAME = 'SLIDE_UI';

export interface IUniverSlidesUIConfig {
menu: MenuConfig;
}

export class UniverSlidesUIPlugin extends Plugin {
static override pluginName = SLIDE_UI_PLUGIN_NAME;
static override type = UniverInstanceType.UNIVER_SLIDE;

constructor(
private readonly _config: Partial<IUniverSlidesUIConfig> = {},
private readonly _config: Partial<IUniverSlidesDrawingConfig> = {},
@Inject(Injector) override readonly _injector: Injector,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService
) {
super();
Expand All @@ -51,6 +50,14 @@ export class UniverSlidesUIPlugin extends Plugin {
] as Dependency[]).forEach((d) => injector.add(d));
}

override onReady(): void {
([
[SlideRenderController],
] as Dependency[]).forEach((m) => {
this.disposeWithMe(this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SLIDE, m));
});
}

override onRendered(): void {
this._markSlideAsFocused();
}
Expand Down
19 changes: 13 additions & 6 deletions packages/slides/src/slides-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Inject, Injector, Plugin, UniverInstanceType } from '@univerjs/core';
import type { Engine } from '@univerjs/engine-render';
import { IRenderingEngine } from '@univerjs/engine-render';
import { IRenderingEngine, IRenderManagerService } from '@univerjs/engine-render';
import type { Dependency } from '@univerjs/core';

import { CanvasView } from './views/render';
Expand All @@ -35,11 +35,12 @@ export class UniverSlidesPlugin extends Plugin {

private _canvasEngine: Engine | null = null;

private _canvasView: CanvasView | null = null;
// private _canvasView: CanvasView | null = null;

constructor(
config: Partial<IUniverSlidesConfig> = {},
@Inject(Injector) override readonly _injector: Injector
@Inject(Injector) override readonly _injector: Injector,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService
) {
super();

Expand All @@ -51,6 +52,10 @@ export class UniverSlidesPlugin extends Plugin {
this.initCanvasEngine();
}

override onReady(): void {

}

getConfig() {
return this._config;
}
Expand All @@ -67,9 +72,11 @@ export class UniverSlidesPlugin extends Plugin {
return this._canvasEngine;
}

getCanvasView() {
return this._canvasView;
}

// no one is using this!
// getCanvasView() {
// return this._canvasView;
// }

private _initializeDependencies(slideInjector: Injector) {
const dependencies: Dependency[] = [[CanvasView]];
Expand Down
6 changes: 3 additions & 3 deletions packages/slides/src/views/render/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import type { Injector, IPageElement, PageElementType } from '@univerjs/core';
import type { Injector, IPageElement, Nullable, PageElementType } from '@univerjs/core';
import { Registry } from '@univerjs/core';
import type { Scene } from '@univerjs/engine-render';
import type { BaseObject, Scene } from '@univerjs/engine-render';

export abstract class ObjectAdaptor {
zIndex = 0;
Expand All @@ -30,7 +30,7 @@ export abstract class ObjectAdaptor {
return this;
}

abstract convert(pageElement: IPageElement, mainScene: Scene): void;
abstract convert(pageElement: IPageElement, mainScene: Scene): Nullable<BaseObject>;

create(injector: Injector): void {
// FIXME: should not be empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class RichTextAdaptor extends ObjectAdaptor {
return this;
}

override convert(pageElement: IPageElement, mainScene: Scene) {
override convert(pageElement: IPageElement, _mainScene: Scene) {
const {
id,
zIndex,
Expand Down
Loading

0 comments on commit d87df25

Please sign in to comment.