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(sheet): transformer alpha finished

dream-num0c09ef023634
diff --git a/packages/base-render/src/BaseObject.ts b/packages/base-render/src/BaseObject.ts
index f3032421c1..b2a4f09652 100644
--- a/packages/base-render/src/BaseObject.ts
+++ b/packages/base-render/src/BaseObject.ts
@@ -134,10 +134,12 @@ export abstract class BaseObject {
}
translate(x?: number | string, y?: number | string) {
+ const preTop = this.top;
if (y !== undefined) {
this.top = y;
}
+ const preLeft = this.left;
if (x !== undefined) {
this.left = x;
}
@@ -146,17 +148,19 @@ export abstract class BaseObject {
this.onTransformChangeObservable.notifyObservers({
type: TRANSFORM_CHANGE_OBSERVABLE_TYPE.translate,
- value: { x: this._top, y: this._left },
+ value: { top: this._top, left: this._left },
+ preValue: { top: preTop, left: preLeft },
});
return this;
}
resize(width?: number | string, height?: number | string) {
+ const preWidth = this.width;
if (width !== undefined) {
this.width = width;
}
-
+ const preHeight = this.height;
if (height !== undefined) {
this.height = height;
}
@@ -165,17 +169,20 @@ export abstract class BaseObject {
this.onTransformChangeObservable.notifyObservers({
type: TRANSFORM_CHANGE_OBSERVABLE_TYPE.resize,
- value: { x: this._width, y: this._height },
+ value: { width: this._width, height: this._height },
+ preValue: { width: preWidth, height: preHeight },
});
return this;
}
scale(scaleX?: number, scaleY?: number) {
+ const preScaleX = this.scaleX;
if (scaleX !== undefined) {
this.scaleX = scaleX;
}
+ const preScaleY = this.scaleY;
if (scaleY !== undefined) {
this.scaleY = scaleY;
}
@@ -184,17 +191,20 @@ export abstract class BaseObject {
this.onTransformChangeObservable.notifyObservers({
type: TRANSFORM_CHANGE_OBSERVABLE_TYPE.scale,
- value: { x: this._scaleX, y: this._scaleY },
+ value: { scaleX: this._scaleX, scaleY: this._scaleY },
+ preValue: { scaleX: preScaleX, scaleY: preScaleY },
});
return this;
}
skew(skewX?: number, skewY?: number) {
+ const preSkewX = skewX;
if (skewX !== undefined) {
this.skewX = skewX;
}
+ const preSkewY = skewY;
if (skewY !== undefined) {
this.skewY = skewY;
}
@@ -203,17 +213,19 @@ export abstract class BaseObject {
this.onTransformChangeObservable.notifyObservers({
type: TRANSFORM_CHANGE_OBSERVABLE_TYPE.skew,
- value: { x: this._skewX, y: this._skewY },
+ value: { skewX: this._skewX, skewY: this._skewY },
+ preValue: { skewX: preSkewX, skewY: preSkewY },
});
return this;
}
flip(flipX?: boolean, flipY?: boolean) {
+ const preFlipX = flipX;
if (flipX !== undefined) {
this.flipX = flipX;
}
-
+ const preFlipY = flipY;
if (flipY !== undefined) {
this.flipY = flipY;
}
@@ -222,7 +234,8 @@ export abstract class BaseObject {
this.onTransformChangeObservable.notifyObservers({
type: TRANSFORM_CHANGE_OBSERVABLE_TYPE.flip,
- value: { x: this._flipX, y: this._flipY },
+ value: { flipX: this._flipX, flipY: this._flipY },
+ preValue: { flipX: preFlipX, flipY: preFlipY },
});
return this;
@@ -230,11 +243,13 @@ export abstract class BaseObject {
transformByState(option: IObjectFullState) {
const optionKeys = Object.keys(option);
+ const preKeys: IObjectFullState = {};
if (optionKeys.length === 0) {
return;
}
optionKeys.forEach((pKey) => {
if (option[pKey] !== undefined) {
+ preKeys[pKey] = this[pKey];
this[pKey] = option[pKey];
}
});
@@ -244,12 +259,13 @@ export abstract class BaseObject {
this.onTransformChangeObservable.notifyObservers({
type: TRANSFORM_CHANGE_OBSERVABLE_TYPE.all,
value: option,
+ preValue: preKeys,
});
return this;
}
- private _setTransForm() {
+ protected _setTransForm() {
const composeResult = Transform.create().composeMatrix({
left: this.left + this.strokeWidth / 2,
top: this.top + this.strokeWidth / 2,
@@ -431,6 +447,22 @@ export abstract class BaseObject {
this._isTransformer = state;
}
+ getState() {
+ return {
+ left: this.left,
+ top: this.top,
+ width: this.width,
+ height: this.height,
+ scaleX: this.scaleX,
+ scaleY: this.scaleY,
+ angle: this.angle,
+ skewX: this.skewX,
+ skewY: this.skewY,
+ flipX: this.flipX,
+ flipY: this.flipY,
+ };
+ }
+
hide() {
this._visible = false;
}
diff --git a/packages/base-render/src/Basics/Interfaces.ts b/packages/base-render/src/Basics/Interfaces.ts
index fc0317f0b3..9313aeb813 100644
--- a/packages/base-render/src/Basics/Interfaces.ts
+++ b/packages/base-render/src/Basics/Interfaces.ts
@@ -58,6 +58,7 @@ export enum TRANSFORM_CHANGE_OBSERVABLE_TYPE {
export interface ITransformChangeState {
type: TRANSFORM_CHANGE_OBSERVABLE_TYPE;
value: number | string | boolean | { x: number | string | boolean; y: number | string | boolean } | IObjectFullState | ISceneTransformState;
+ preValue: number | string | boolean | { x: number | string | boolean; y: number | string | boolean } | IObjectFullState | ISceneTransformState;
}
export interface IFontLocale {
diff --git a/packages/base-render/src/Scene.Transformer.ts b/packages/base-render/src/Scene.Transformer.ts
index ffe43bdda1..6f1f9dc219 100644
--- a/packages/base-render/src/Scene.Transformer.ts
+++ b/packages/base-render/src/Scene.Transformer.ts
@@ -193,7 +193,7 @@ export class Transformer implements ITransformerConfig {
private _updateControlChildren() {
this._updateControlIterator((control, applyObject) => {
- const { left, top, width, height, scaleX, scaleY } = applyObject;
+ const { left, top, width, height, scaleX, scaleY } = applyObject.getState();
const children = control.getObjects();
children.forEach((o: BaseObject) => {
const key = o.oKey;
@@ -352,9 +352,9 @@ export class Transformer implements ITransformerConfig {
}
private _getRotateAnchorPosition(type: TransformerManagerType, height: number, width: number, scaleX: number, scaleY: number) {
- // width *= scaleX;
+ // width /= scaleX;
- // height *= scaleY;
+ // height /= scaleY;
let left = -this.anchorSize / 2;
let top = -this.anchorSize / 2;
@@ -414,14 +414,14 @@ export class Transformer implements ITransformerConfig {
}
return {
- left: left * scaleX,
- top: top * scaleY,
+ left,
+ top,
cursor,
};
}
private _createResizeAnchor(type: TransformerManagerType, applyObject: BaseObject, zIndex: number) {
- let { height, width, scaleX, scaleY } = applyObject;
+ let { height, width, scaleX, scaleY } = applyObject.getState();
const { left, top, cursor } = this._getRotateAnchorPosition(type, height, width, scaleX, scaleY);
@@ -494,7 +494,7 @@ export class Transformer implements ITransformerConfig {
private _updateControl() {
this._updateControlIterator((control, applyObject) => {
- const { left, top } = applyObject;
+ const { left, top } = applyObject.getState();
control.transformByState({
left,
top,
@@ -529,15 +529,15 @@ export class Transformer implements ITransformerConfig {
}
private _createControl(applyObject: BaseObject) {
- let { left, top, height, width, angle, scaleX, scaleY, skewX, skewY, flipX, flipY, oKey } = applyObject;
-
+ let { left, top, height, width, angle, scaleX, scaleY, skewX, skewY, flipX, flipY } = applyObject.getState();
+ const oKey = applyObject.oKey;
const zIndex = this._selectedObjectMap.size;
const groupElements: BaseObject[] = [];
- width *= scaleX;
+ // width *= scaleX;
- height *= scaleY;
+ // height *= scaleY;
if (this.borderEnabled) {
const outline = new Rect(`${TransformerManagerType.OUTLINE}_${zIndex}`, {
diff --git a/packages/base-render/src/Shape/Circle.ts b/packages/base-render/src/Shape/Circle.ts
index d6ab57e6b6..74738a67da 100644
--- a/packages/base-render/src/Shape/Circle.ts
+++ b/packages/base-render/src/Shape/Circle.ts
@@ -1,6 +1,7 @@
// import { IShapeProps, Shape, IObjectFullState, Group, Scene } from '.';
import { IKeyValue } from '@univer/core';
+import { TRANSFORM_CHANGE_OBSERVABLE_TYPE } from '../Basics';
import { IShapeProps, Shape } from './Shape';
export interface ICircleProps extends IShapeProps {
@@ -21,6 +22,17 @@ export class Circle extends Shape<ICircleProps> {
this._radius = props?.radius || 10;
this._setFixBoundingBox();
+
+ this.onTransformChangeObservable.add((changeState) => {
+ const { type, value, preValue } = changeState;
+ if (type === TRANSFORM_CHANGE_OBSERVABLE_TYPE.resize || type === TRANSFORM_CHANGE_OBSERVABLE_TYPE.all) {
+ const value = Math.min(this.width, this.height);
+ this._radius = value / 2;
+ this.width = value;
+ this.height = value;
+ this._setTransForm();
+ }
+ });
}
private _setFixBoundingBox() {
diff --git a/packages/base-render/src/Shape/Path.ts b/packages/base-render/src/Shape/Path.ts
index 3abeaba6d7..7fb8beea9f 100644
--- a/packages/base-render/src/Shape/Path.ts
+++ b/packages/base-render/src/Shape/Path.ts
@@ -1,6 +1,7 @@
// import { IShapeProps, Shape, IObjectFullState, Group, Scene } from '.';
import { IKeyValue, Nullable } from '@univer/core';
+import { IObjectFullState, TRANSFORM_CHANGE_OBSERVABLE_TYPE } from '../Basics';
import { IShapeProps, Shape } from './Shape';
interface IPathDataArray {
@@ -32,9 +33,9 @@ export class Path extends Shape<IPathProps> {
private pathLength: number = 0;
- private _fixLeft = 0;
+ private _selfRectCache: IPathFixConfig;
- private _fixTop = 0;
+ private _reCalculateCache: boolean = true;
get dataArray() {
return this._dataArray;
@@ -53,6 +54,59 @@ export class Path extends Shape<IPathProps> {
}
this._setFixBoundingBox();
+
+ this.onTransformChangeObservable.add((changeState) => {
+ const { type, value, preValue } = changeState;
+ if (type === TRANSFORM_CHANGE_OBSERVABLE_TYPE.resize || type === TRANSFORM_CHANGE_OBSERVABLE_TYPE.all) {
+ this._reCalculateCache = true;
+
+ const { left, top, width, height } = this._getSelfRect();
+
+ const { width: preWidth, height: preHeight } = preValue as IObjectFullState;
+
+ let fixX;
+ let fixY;
+
+ if (!preWidth) {
+ fixX = 0;
+ } else {
+ fixX = (this.width as number) - preWidth;
+ }
+
+ if (!preHeight) {
+ fixY = 0;
+ } else {
+ fixY = (this.height as number) - preHeight;
+ }
+
+ const increaseScaleX = fixX / width;
+
+ const increaseScaleY = fixY / height;
+
+ this.scaleX += increaseScaleX;
+ this.scaleY += increaseScaleY;
+
+ this.left = (this.left as number) - left * increaseScaleX;
+ this.top = (this.top as number) - top * increaseScaleY;
+
+ // console.log({
+ // left: this.left,
+ // top: this.top,
+ // scaleX: this.scaleX,
+ // scaleY: this.scaleY,
+ // width: this.width,
+ // preWidth,
+ // height: this.height,
+ // preHeight,
+ // originWidth: width,
+ // originHeight: height,
+ // originLeft: left,
+ // originTop: top,
+ // });
+
+ this._setTransForm();
+ }
+ });
}
protected _draw(ctx: CanvasRenderingContext2D) {
@@ -125,17 +179,14 @@ export class Path extends Shape<IPathProps> {
const fixScaleX = (this.width as number) / width;
const fixScaleY = (this.height as number) / height;
- this.transformByState({
- left: (this.left as number) - left * fixScaleX,
- top: (this.top as number) - top * fixScaleY,
- scaleX: fixScaleX,
- scaleY: fixScaleY,
- width,
- height,
- });
+ this.left = (this.left as number) - left * fixScaleX;
+ this.top = (this.top as number) - top * fixScaleY;
+ this.scaleX = fixScaleX;
+ this.scaleY = fixScaleY;
+ this.width = width;
+ this.height = height;
- this._fixLeft = left;
- this._fixTop = top;
+ this._setTransForm();
}
toJson() {
@@ -151,17 +202,37 @@ export class Path extends Shape<IPathProps> {
};
}
+ getState() {
+ const { left, top, width, height } = this.getRect();
+ return {
+ left,
+ top,
+ width,
+ height,
+ scaleX: this.scaleX,
+ scaleY: this.scaleY,
+ angle: this.angle,
+ skewX: this.skewX,
+ skewY: this.skewY,
+ flipX: this.flipX,
+ flipY: this.flipY,
+ };
+ }
+
getRect() {
const { left, top, width, height } = this._getSelfRect();
return {
left: left * this.scaleX + this.left,
- top: top * this.scaleX + this.top,
+ top: top * this.scaleY + this.top,
width: width * this.scaleX,
height: height * this.scaleY,
};
}
private _getSelfRect() {
+ if (!this._reCalculateCache) {
+ return this._selfRectCache;
+ }
let points: number[] = [];
this.dataArray.forEach((data) => {
if (data.command === 'A') {
@@ -230,12 +301,17 @@ export class Path extends Shape<IPathProps> {
maxY = Math.max(maxY, y);
}
}
- return {
+
+ const cache = {
left: minX,
top: minY,
width: maxX - minX,
height: maxY - minY,
};
+
+ this._selfRectCache = cache;
+
+ return cache;
}
/**
diff --git a/packages/base-render/src/Shape/Picture.ts b/packages/base-render/src/Shape/Picture.ts
index 9f0379c3af..b6a7dc7b09 100644
--- a/packages/base-render/src/Shape/Picture.ts
+++ b/packages/base-render/src/Shape/Picture.ts
@@ -61,7 +61,14 @@ export class Picture extends Shape<IPictureProps> {
this.makeDirty(true);
};
this._native.onerror = () => {
- config.fail?.();
+ if (config.fail) {
+ config.fail();
+ } else {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DguMDY0bDc1LjI2NC0xNzYuNjR6TTMwNC42NCA3OTIuMDY0SDE2NS44ODhjLTE2LjM4NCAwLTMwLjIwOC0xMy4zMTItMzAuMjA4LTMwLjIwOHYtOS43MjhsMTM4Ljc1Mi0xNjQuMzUyIDEwNC45NiAxMjQuNDE2LTc0Ljc1MiA3OS44NzJ6bTgxLjkyLTM1NS44NGwzNy4zNzYgMjI4Ljg2NC0uNTEyLjUxMi0xNDIuODQ4LTE2OS45ODRjLTMuMDcyLTMuNTg0LTkuMjE2LTMuNTg0LTEyLjI4OCAwTDEzNS42OCA2NTIuOFYyNjIuMTQ0YzAtMTYuMzg0IDEzLjMxMi0zMC4yMDggMzAuMjA4LTMwLjIwOGg0NzQuNjI0TDM4Ni41NiA0MzYuMjI0em01MDEuMjQ4IDMyNS42MzJjMCAxNi44OTYtMTMuMzEyIDMwLjIwOC0yOS42OTYgMzAuMjA4SDY4MC45Nmw1Ny4zNDQtOTMuMTg0LTg3LjU1Mi0yMDIuMjQgNy4xNjgtNy42OCAyMjkuODg4IDI3Mi44OTZ6IiBmaWxsPSIjZTZlNmU2Ii8+PC9zdmc+';
+ this._init();
+ this.makeDirty(true);
+ }
};
}
}
diff --git a/packages/base-render/src/Shape/RichText.ts b/packages/base-render/src/Shape/RichText.ts
index 9a32d8302f..2e33e97460 100644
--- a/packages/base-render/src/Shape/RichText.ts
+++ b/packages/base-render/src/Shape/RichText.ts
@@ -3,7 +3,7 @@
import { BlockType, SheetContext, IDocumentData, IKeyValue, ParagraphElementType } from '@univer/core';
import { Canvas } from '..';
import { BaseObject } from '../BaseObject';
-import { IBoundRect, IObjectFullState } from '../Basics';
+import { IBoundRect, IObjectFullState, TRANSFORM_CHANGE_OBSERVABLE_TYPE } from '../Basics';
import { Documents, DocumentSkeleton } from '../Component';
export interface IRichTextProps extends IObjectFullState {
@@ -52,6 +52,18 @@ export class RichText extends BaseObject {
this._documents = new Documents(`${this.oKey}_DOCUMENTS`, this._documentSkeleton);
this._initialProps(props);
+
+ this.onTransformChangeObservable.add((changeState) => {
+ const { type, value, preValue } = changeState;
+ if (type === TRANSFORM_CHANGE_OBSERVABLE_TYPE.resize || type === TRANSFORM_CHANGE_OBSERVABLE_TYPE.all) {
+ this._documentSkeleton.updateDocumentDataPageSize(this.width);
+ this._documentSkeleton.makeDirty(true);
+ this._documentSkeleton.calculate();
+ const size = this._documentSkeleton.getLastPageSize();
+ this.height = size?.height || this.height;
+ this._setTransForm();
+ }
+ });
}
private convertToDocumentData(text: string) {