Skip to content

Commit

Permalink
fix(sheets-drawing-ui): load float-dom from snapshot & support presis…
Browse files Browse the repository at this point in the history
…t custom data with float dom drawing object (#2841)
  • Loading branch information
weird94 authored Jul 23, 2024
1 parent f66e5a7 commit ca38723
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 11 deletions.

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions packages/core/src/common/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/

export type Serializable =
| { [property: string]: Serializable }
| readonly Serializable[]
| string
| number
| boolean
| null;
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { installShims } from './common/shims';

installShims();

export type { Serializable } from './common/json';
export { DEFAULT_DOCUMENT_SUB_COMPONENT_ID } from './docs/data-model/subdocument';
export { type UnitType, UnitModel, UniverInstanceType } from './common/unit';
export { Registry, RegistryAsMap } from './common/registry';
Expand Down
3 changes: 3 additions & 0 deletions packages/debugger/src/commands/commands/float-dom.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const CreateFloatDomCommand: ICommand = {
props: {
a: 1,
},
data: {
aa: '128',
},
});
return true;
},
Expand Down
2 changes: 0 additions & 2 deletions packages/debugger/src/debugger-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export class UniverDebuggerPlugin extends Plugin {
[PerformanceMonitorController],
[E2EMemoryController],
] as Dependency[]).forEach((d) => injector.add(d));
}

override onRendered(): void {
this._injector.add([
DebuggerController,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import type { IDisposable, IPosition, ITransformState, Nullable, Worksheet } from '@univerjs/core';
import { Disposable, DisposableCollection, DrawingTypeEnum, ICommandService, Inject, IUniverInstanceService, Tools } from '@univerjs/core';
import type { IDisposable, IPosition, ITransformState, Nullable, Serializable, Worksheet } from '@univerjs/core';
import { Disposable, DisposableCollection, DrawingTypeEnum, ICommandService, Inject, IUniverInstanceService, LifecycleStages, OnLifecycle, Tools } from '@univerjs/core';
import type { IDrawingJsonUndo1 } from '@univerjs/drawing';
import { getDrawingShapeKeyByDrawingSearch, IDrawingManagerService } from '@univerjs/drawing';
import type { BaseObject, IBoundRectNoAngle, IRectProps, IRender, Scene, SpreadsheetSkeleton } from '@univerjs/engine-render';
Expand All @@ -37,7 +37,14 @@ export interface ICanvasFloatDom {
componentKey: string;
unitId?: string;
subUnitId?: string;
/**
* props of component, wouldn't save to snapshot
*/
props?: Record<string, any>;
/**
* data of component, will save to snapshot, json-like data
*/
data?: Serializable;
}

interface ICanvasFloatDomInfo {
Expand Down Expand Up @@ -154,8 +161,9 @@ export interface ISheetCanvasFloatDomHook {
onGetFloatDomProps: (id: string) => Record<string, any>;
}

@OnLifecycle(LifecycleStages.Starting, SheetCanvasFloatDomManagerService)
export class SheetCanvasFloatDomManagerService extends Disposable {
private _domLayerMap: Map<string, Map<string, Map<string, ICanvasFloatDom>>> = new Map();
private _domLayerMap: Map<string, Map<string, Map<string, { props?: any }>>> = new Map();
private _domLayerInfoMap: Map<string, ICanvasFloatDomInfo> = new Map();

private _transformChange$ = new Subject<{ id: string; value: ITransformState }>();
Expand Down Expand Up @@ -240,7 +248,7 @@ export class SheetCanvasFloatDomManagerService extends Disposable {
return;
}

const { transform, drawingType } = floatDomParam;
const { transform, drawingType, data } = floatDomParam;

if (drawingType !== DrawingTypeEnum.DRAWING_DOM) {
return;
Expand Down Expand Up @@ -308,6 +316,7 @@ export class SheetCanvasFloatDomManagerService extends Disposable {
canvas.dispatchEvent(new WheelEvent(evt.type, evt));
},
props: map.get(drawingId)?.props ?? this._getFloatDomProps(drawingId),
data,
});

const listener = rect.onTransformChange$.subscribeEvent(() => {
Expand All @@ -322,6 +331,7 @@ export class SheetCanvasFloatDomManagerService extends Disposable {
});
listener && disposableCollection.add(listener);
this._domLayerInfoMap.set(drawingId, info);
map.set(drawingId, {});
});
})
);
Expand Down Expand Up @@ -444,7 +454,7 @@ export class SheetCanvasFloatDomManagerService extends Disposable {
}

const { unitId, subUnitId } = target;
const { initPosition, componentKey } = layer;
const { initPosition, componentKey, data } = layer;
const id = Tools.generateRandomId();

const sheetTransform = this._getPosition(initPosition, unitId);
Expand All @@ -467,6 +477,7 @@ export class SheetCanvasFloatDomManagerService extends Disposable {
width: initPosition.endX - initPosition.startX,
height: initPosition.endY - initPosition.startY,
},
data,
};

this._commandService.executeCommand(InsertSheetDrawingCommand.id, {
Expand Down
2 changes: 2 additions & 0 deletions packages/sheets-drawing/src/services/sheet-drawing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { type IDrawingParam, type IImageData, type IRotationSkewFlipTransform, type IUnitDrawingService, UnitDrawingService } from '@univerjs/drawing';
import type { Serializable } from '@univerjs/core';
import { createIdentifier } from '@univerjs/core';

interface ICellPosition {
Expand Down Expand Up @@ -53,6 +54,7 @@ export interface ISheetShape extends IDrawingParam, ISheetDrawingBase {

export interface IFloatDomData extends IDrawingParam {
componentKey: string;
data?: Serializable;
}

// TODO@wzhudev: this shouldn't be here. It should be in the sheets package
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/services/dom/canvas-dom-layer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { IPosition } from '@univerjs/core';
import type { IPosition, Serializable } from '@univerjs/core';
import { BehaviorSubject, type Observable } from 'rxjs';

export interface IFloatDomLayout extends IPosition {
Expand All @@ -36,6 +36,7 @@ export interface IFloatDom {
onPointerUp: (evt: PointerEvent | MouseEvent) => void;
onWheel: (evt: WheelEvent) => void;
props?: Record<string, any>;
data?: Serializable;
}

export class CanvasFloatDomService {
Expand Down
7 changes: 6 additions & 1 deletion packages/ui/src/views/components/dom/FloatDom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const FloatDomSingle = memo((props: { layer: IFloatDom; id: string }) => {
const position = useObservable(layer.position$);
const Component = typeof layer.componentKey === 'string' ? componentManager.get(layer.componentKey) : layer.componentKey;

const layerProps: any = {
data: layer.data,
...layer.props,
};

return position
? (
<div
Expand Down Expand Up @@ -65,7 +70,7 @@ const FloatDomSingle = memo((props: { layer: IFloatDom; id: string }) => {
...(position.absolute.top) ? { top: 0 } : { bottom: 0 },
}}
>
{Component ? <Component {...layer.props} /> : null}
{Component ? <Component {...layerProps} /> : null}
</div>
</div>
)
Expand Down

0 comments on commit ca38723

Please sign in to comment.