Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(slides): add update element operation (#3000)

白熱eb087796fc70
diff --git a/package.json b/package.json
index 75b74ce1d3..1f1ea62e0a 100644
--- a/package.json
+++ b/package.json
@@ -65,7 +65,7 @@
"eslint-plugin-no-barrel-import": "^0.0.2",
"eslint-plugin-no-penetrating-import": "^0.0.1",
"eslint-plugin-react": "^7.35.0",
- "eslint-plugin-react-hooks": "^4.6.2",
+ "eslint-plugin-react-hooks": "5.1.0-rc-107a2f8c3e-20240617",
"eslint-plugin-react-refresh": "^0.4.9",
"husky": "^9.1.4",
"lint-staged": "^15.2.8",
diff --git a/packages/slides-ui/src/commands/operations/insert-shape.operation.ts b/packages/slides-ui/src/commands/operations/insert-shape.operation.ts
index ff272cab3a..cb0bb8c78a 100644
--- a/packages/slides-ui/src/commands/operations/insert-shape.operation.ts
+++ b/packages/slides-ui/src/commands/operations/insert-shape.operation.ts
@@ -33,10 +33,10 @@ export const InsertSlideShapeRectangleOperation: ICommand<IInsertShapeOperationP
id,
zIndex: 20,
left: 378,
- top: 0,
- width: 204,
- height: 144,
- title: 'mask',
+ top: 142,
+ width: 250,
+ height: 250,
+ title: id,
description: '',
type: PageElementType.SHAPE,
shape: {
@@ -44,7 +44,7 @@ export const InsertSlideShapeRectangleOperation: ICommand<IInsertShapeOperationP
text: '',
shapeProperties: {
shapeBackgroundFill: {
- rgb: 'rgb(0,79,86)',
+ rgb: 'rgb(0,0,255)',
},
},
},
diff --git a/packages/slides-ui/src/commands/operations/update-element.operation.ts b/packages/slides-ui/src/commands/operations/update-element.operation.ts
new file mode 100644
index 0000000000..c24773830d
--- /dev/null
+++ b/packages/slides-ui/src/commands/operations/update-element.operation.ts
@@ -0,0 +1,43 @@
+/**
+ * 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, Tools, UniverInstanceType } from '@univerjs/core';
+
+export interface IUpdateElementOperationParams {
+ oKey: string;
+ props: Record<string, any>;
+};
+
+export const UpdateSlideElementOperation: ICommand<IUpdateElementOperationParams> = {
+ id: 'slide.operation.update-element',
+ type: CommandType.OPERATION,
+ handler: (accessor, params) => {
+ const { oKey, props } = params!;
+
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const slideData = univerInstanceService.getCurrentUnitForType<SlideDataModel>(UniverInstanceType.UNIVER_SLIDE);
+
+ 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/components/panels/ArrangePanel.tsx b/packages/slides-ui/src/components/panels/ArrangePanel.tsx
index c2d4b0f193..34697aa5fe 100644
--- a/packages/slides-ui/src/components/panels/ArrangePanel.tsx
+++ b/packages/slides-ui/src/components/panels/ArrangePanel.tsx
@@ -17,12 +17,13 @@
import React from 'react';
import type { Nullable } from '@univerjs/core';
-import { LocaleService, useDependency } from '@univerjs/core';
+import { ICommandService, LocaleService, useDependency } from '@univerjs/core';
import clsx from 'clsx';
import { Button } from '@univerjs/design';
import { BottomSingle, MoveDownSingle, MoveUpSingle, TopmostSingle } from '@univerjs/icons';
import { CanvasView } from '@univerjs/slides';
import type { Image, Rect, RichText } from '@univerjs/engine-render';
+import { UpdateSlideElementOperation } from '../../commands/operations/update-element.operation';
import styles from './index.module.less';
enum ArrangeTypeEnum {
@@ -41,6 +42,7 @@ export default function ArrangePanel(props: IProps) {
const localeService = useDependency(LocaleService);
const canvasView = useDependency(CanvasView);
+ const commandService = useDependency(ICommandService);
const page = canvasView.getRenderUnitByPageId(unitId);
const scene = page?.scene;
@@ -64,15 +66,27 @@ export default function ArrangePanel(props: IProps) {
return [minZIndex, maxZIndex];
}, [0, 0]);
+ let zIndex = object.zIndex;
if (arrangeType === ArrangeTypeEnum.back) {
- object.setProps({ zIndex: minZIndex - 1 });
+ zIndex = minZIndex - 1;
} else if (arrangeType === ArrangeTypeEnum.front) {
- object.setProps({ zIndex: maxZIndex + 1 });
+ zIndex = maxZIndex + 1;
} else if (arrangeType === ArrangeTypeEnum.forward) {
- object.setProps({ zIndex: object.zIndex + 1 });
+ zIndex = object.zIndex + 1;
} else if (arrangeType === ArrangeTypeEnum.backward) {
- object.setProps({ zIndex: object.zIndex - 1 });
+ zIndex = object.zIndex - 1;
}
+
+ object.setProps({
+ zIndex,
+ });
+
+ commandService.executeCommand(UpdateSlideElementOperation.id, {
+ oKey: object?.oKey,
+ props: {
+ zIndex,
+ },
+ });
};
return (
diff --git a/packages/slides-ui/src/components/panels/FillPanel.tsx b/packages/slides-ui/src/components/panels/FillPanel.tsx
index 3629da10f8..c1e2ce0d86 100644
--- a/packages/slides-ui/src/components/panels/FillPanel.tsx
+++ b/packages/slides-ui/src/components/panels/FillPanel.tsx
@@ -17,12 +17,13 @@
import React from 'react';
import type { Nullable } from '@univerjs/core';
-import { LocaleService, useDependency } from '@univerjs/core';
+import { ICommandService, LocaleService, useDependency } from '@univerjs/core';
import clsx from 'clsx';
import { ColorPicker, Dropdown } from '@univerjs/design';
import { MoreDownSingle, PaintBucket } from '@univerjs/icons';
import { CanvasView } from '@univerjs/slides';
import type { Rect } from '@univerjs/engine-render';
+import { UpdateSlideElementOperation } from '../../commands/operations/update-element.operation';
import styles from './index.module.less';
interface IProps {
@@ -34,6 +35,7 @@ export default function ArrangePanel(props: IProps) {
const localeService = useDependency(LocaleService);
const canvasView = useDependency(CanvasView);
+ const commandService = useDependency(ICommandService);
const page = canvasView.getRenderUnitByPageId(unitId);
const scene = page?.scene;
@@ -49,7 +51,21 @@ export default function ArrangePanel(props: IProps) {
const [color, setColor] = React.useState<string>(object.fill?.toString() ?? '');
function handleChangeColor(color: string) {
- object?.setProps({ fill: color });
+ object?.setProps({
+ fill: color,
+ });
+ commandService.executeCommand(UpdateSlideElementOperation.id, {
+ oKey: object?.oKey,
+ props: {
+ shape: {
+ shapeProperties: {
+ shapeBackgroundFill: {
+ rgb: color,
+ },
+ },
+ },
+ },
+ });
setColor(color);
}
diff --git a/packages/slides-ui/src/components/panels/TransformPanel.tsx b/packages/slides-ui/src/components/panels/TransformPanel.tsx
index a60feb5924..d4fe016daa 100644
--- a/packages/slides-ui/src/components/panels/TransformPanel.tsx
+++ b/packages/slides-ui/src/components/panels/TransformPanel.tsx
@@ -17,11 +17,12 @@
import React, { useEffect, useState } from 'react';
import type { Nullable } from '@univerjs/core';
-import { LocaleService, useDependency } from '@univerjs/core';
+import { ICommandService, LocaleService, useDependency } from '@univerjs/core';
import clsx from 'clsx';
import { InputNumber } from '@univerjs/design';
import type { BaseObject, IChangeObserverConfig, Image, Rect, RichText } from '@univerjs/engine-render';
import { CanvasView } from '@univerjs/slides';
+import { UpdateSlideElementOperation } from '../../commands/operations/update-element.operation';
import styles from './index.module.less';
interface IProps {
@@ -33,6 +34,7 @@ export default function TransformPanel(props: IProps) {
const localeService = useDependency(LocaleService);
const canvasView = useDependency(CanvasView);
+ const commandService = useDependency(ICommandService);
const page = canvasView.getRenderUnitByPageId(unitId);
const scene = page?.scene;
@@ -197,6 +199,12 @@ export default function TransformPanel(props: IProps) {
function handleWidthChange(val: number | null) {
if (!val || !object) return;
+ commandService.executeCommand(UpdateSlideElementOperation.id, {
+ oKey: object.oKey,
+ props: {
+ width: val,
+ },
+ });
object?.resize(val, object.height);
setWidth(val);
transformer?.refreshControls();
@@ -205,6 +213,12 @@ export default function TransformPanel(props: IProps) {
function handleHeightChange(val: number | null) {
if (!val || !object) return;
+ commandService.executeCommand(UpdateSlideElementOperation.id, {
+ oKey: object.oKey,
+ props: {
+ height: val,
+ },
+ });
object?.resize(object.width, val);
setHeight(val);
transformer?.refreshControls();
@@ -213,6 +227,12 @@ export default function TransformPanel(props: IProps) {
function handleXChange(val: number | null) {
if (!val || !object) return;
+ commandService.executeCommand(UpdateSlideElementOperation.id, {
+ oKey: object.oKey,
+ props: {
+ left: val,
+ },
+ });
object?.translate(val, object.top);
setXPosition(val);
transformer?.refreshControls();
@@ -221,14 +241,26 @@ export default function TransformPanel(props: IProps) {
function handleYChange(val: number | null) {
if (!val || !object) return;
+ commandService.executeCommand(UpdateSlideElementOperation.id, {
+ oKey: object.oKey,
+ props: {
+ right: val,
+ },
+ });
object?.translate(object.left, val);
setYPosition(val);
transformer?.refreshControls();
}
function handleChangeRotation(val: number | null) {
- if (!val) return;
+ if (!val || !object) return;
+ commandService.executeCommand(UpdateSlideElementOperation.id, {
+ oKey: object.oKey,
+ props: {
+ angle: val,
+ },
+ });
object?.transformByState({
angle: val,
});
diff --git a/packages/slides-ui/src/controllers/slide-ui.controller.ts b/packages/slides-ui/src/controllers/slide-ui.controller.ts
index 0db0ced686..21e461f1e9 100644
--- a/packages/slides-ui/src/controllers/slide-ui.controller.ts
+++ b/packages/slides-ui/src/controllers/slide-ui.controller.ts
@@ -34,6 +34,7 @@ import { EditorContainer } from '../views/editor-container';
import { SlideSideBar } from '../components/slide-bar/SlideBar';
import Sidebar, { COMPONENT_SLIDE_SIDEBAR } from '../components/sidebar/Sidebar';
import { AppendSlideOperation } from '../commands/operations/append-slide.operation';
+import { UpdateSlideElementOperation } from '../commands/operations/update-element.operation';
import { IMAGE_UPLOAD_ICON, SlideImageMenuFactory, UploadSlideFloatImageMenuFactory } from './image.menu';
import { GRAPH_SINGLE_ICON, SlideShapeMenuFactory, UploadSlideFloatShapeMenuFactory } from './shape.menu';
import { EditorDeleteLeftShortcut, generateArrowSelectionShortCutItem } from './shortcuts/editor.shortcuts';
@@ -104,6 +105,7 @@ export class SlidesUIController extends Disposable {
InsertSlideShapeRectangleOperation,
ToggleSlideEditSidebarOperation,
DeleteSlideElementOperation,
+ UpdateSlideElementOperation,
// commands for editor
SetTextEditArrowOperation,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b022556685..55ed5f923b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -10,7 +10,7 @@ importers:
devDependencies:
'@antfu/eslint-config':
specifier: 2.24.1
'@commitlint/cli':
specifier: ^19.3.0
version: 19.3.0(@types/[email protected])([email protected])
@@ -66,8 +66,8 @@ importers:
specifier: ^7.35.0
version: 7.35.0([email protected])
eslint-plugin-react-hooks:
- specifier: ^4.6.2
- version: 4.6.2([email protected])
+ specifier: 5.1.0-rc-107a2f8c3e-20240617
+ version: 5.1.0-rc-107a2f8c3e-20240617([email protected])
eslint-plugin-react-refresh:
specifier: ^0.4.9
version: 0.4.9([email protected])
@@ -133,7 +133,7 @@ importers:
'@storybook/addon-interactions':
specifier: 8.2.7
'@storybook/addon-links':
specifier: 8.2.7
@@ -7095,11 +7095,11 @@ packages:
typescript:
optional: true
- resolution: {integrity: sha512-QzliNJq4GinDBcD8gPB5v0wh6g8q3SUi6EFF0x8N/BL9PoVs0atuGc47ozMRyOWAKdwaZ5OnbOEa3WR+dSGKuQ==}
+ resolution: {integrity: sha512-FqbGPZg0U97cOqiZuGm8yplPYtTNTa2YhEDFyL/8qtxQjwQX7KVbCWA1DiAdiNOUi37KrAqZ0TiqrnbQ4QOSTQ==}
engines: {node: '>=10'}
peerDependencies:
- eslint: ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0
+ eslint: ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0 || ^9.0.0
resolution: {integrity: sha512-kvuaQFEEQZq4ZTsIxNJccqNnUkt22lkZDp+hPZILC9PS5C6xrACLoVwAIl7k0P6Tv7WsZzoRbxCcanX9F7rtBQ==}
@@ -10906,7 +10906,7 @@ snapshots:
dependencies:
'@babel/runtime': 7.24.7
dependencies:
'@antfu/install-pkg': 0.3.3
'@clack/prompts': 0.7.0
@@ -10931,7 +10931,7 @@ snapshots:
eslint-plugin-toml: 0.11.1([email protected])
eslint-plugin-unicorn: 55.0.0([email protected])
eslint-plugin-unused-imports: 4.0.1(@typescript-eslint/[email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]))([email protected])
eslint-plugin-vue: 9.27.0([email protected])
eslint-plugin-yml: 1.14.0([email protected])
eslint-processor-vue-blocks: 0.1.2(@vue/[email protected])([email protected])
@@ -10947,7 +10947,7 @@ snapshots:
optionalDependencies:
'@eslint-react/eslint-plugin': 1.9.0([email protected])([email protected])
eslint-plugin-format: 0.1.2([email protected])
- eslint-plugin-react-hooks: 4.6.2([email protected])
+ eslint-plugin-react-hooks: 5.1.0-rc-107a2f8c3e-20240617([email protected])
eslint-plugin-react-refresh: 0.4.9([email protected])
transitivePeerDependencies:
- '@vue/compiler-sfc'
@@ -12799,11 +12799,11 @@ snapshots:
'@storybook/global': 5.0.0
storybook: 8.2.7(@babel/[email protected](@babel/[email protected]))
dependencies:
'@storybook/global': 5.0.0
'@storybook/instrumenter': 8.2.7([email protected](@babel/[email protected](@babel/[email protected])))
polished: 4.3.1
storybook: 8.2.7(@babel/[email protected](@babel/[email protected]))
ts-dedent: 2.2.0
@@ -13094,12 +13094,12 @@ snapshots:
optionalDependencies:
typescript: 5.5.4
dependencies:
'@storybook/csf': 0.1.11
'@storybook/instrumenter': 8.2.7([email protected](@babel/[email protected](@babel/[email protected])))
'@testing-library/dom': 10.1.0
'@testing-library/user-event': 14.5.2(@testing-library/[email protected])
'@vitest/expect': 1.6.0
'@vitest/spy': 1.6.0
@@ -13234,7 +13234,7 @@ snapshots:
lz-string: 1.5.0
pretty-format: 27.5.1
dependencies:
'@adobe/css-tools': 4.4.0
'@babel/runtime': 7.24.7
@@ -15571,7 +15571,7 @@ snapshots:
transitivePeerDependencies:
- supports-color
dependencies:
eslint: 9.8.0
@@ -15689,7 +15689,7 @@ snapshots:
optionalDependencies:
'@typescript-eslint/eslint-plugin': 8.0.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])
dependencies:
'@typescript-eslint/utils': 7.18.0([email protected])([email protected])
eslint: 9.8.0