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): editor scroll bar (#314)

Univercbd8f318548a
diff --git a/packages/base-render/src/Component/Docs/DocSkeleton.ts b/packages/base-render/src/Component/Docs/DocSkeleton.ts
index 2ff5dd9bfd..ae6d895498 100644
--- a/packages/base-render/src/Component/Docs/DocSkeleton.ts
+++ b/packages/base-render/src/Component/Docs/DocSkeleton.ts
@@ -1,7 +1,6 @@
import {
ColumnSeparatorType,
DocumentModelOrSimple,
- fromObservable,
GridType,
HorizontalAlign,
ISectionBreak,
@@ -61,13 +60,6 @@ export class DocumentSkeleton extends Skeleton {
super(localeService);
this._docModel = docModel;
- this.disposeWithMe(
- fromObservable(
- this._docModel.bodyModel?.modelChange$.subscribe(() => {
- this.calculate();
- })
- )
- );
}
static create(docModel: DocumentModelOrSimple, localeService: LocaleService) {
diff --git a/packages/base-render/src/Component/Docs/text-selection-render-manager.ts b/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
index a202c468aa..7188760281 100644
--- a/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
+++ b/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
@@ -16,6 +16,7 @@ import {
import { checkStyle, injectStyle } from '../../Basics/Tools';
import { Transform } from '../../Basics/Transform';
import { Vector2 } from '../../Basics/Vector2';
+import { Engine } from '../../Engine';
import { Scene } from '../../Scene';
import { ScrollTimer } from '../../ScrollTimer';
import { IScrollObserverParam, Viewport } from '../../Viewport';
@@ -23,6 +24,24 @@ import { TextSelection } from './Common/TextSelection';
import { DocumentSkeleton } from './DocSkeleton';
import { IDocumentOffsetConfig } from './Document';
+export function getCanvasOffsetByEngine(engine: Nullable<Engine>) {
+ const canvas = engine?.getCanvasElement();
+
+ if (!canvas) {
+ return {
+ left: 0,
+ top: 0,
+ };
+ }
+
+ const { top, left } = getOffsetRectForDom(canvas);
+
+ return {
+ left,
+ top,
+ };
+}
+
export interface ITextSelectionRenderManager {
readonly onInputBefore$: Observable<Nullable<IEditorInputConfig>>;
readonly onKeydown$: Observable<Nullable<IEditorInputConfig>>;
@@ -665,22 +684,8 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
}
private _getCanvasOffset() {
- const engine = this._scene?.getEngine();
- const canvas = engine?.getCanvasElement();
-
- if (!canvas) {
- return {
- left: 0,
- top: 0,
- };
- }
-
- const { top, left } = getOffsetRectForDom(canvas);
-
- return {
- left,
- top,
- };
+ const engine = this._scene?.getEngine() as Engine;
+ return getCanvasOffsetByEngine(engine);
}
// Let the selection show on the current screen.
diff --git a/packages/base-render/src/Component/Sheets/Extensions/Font.ts b/packages/base-render/src/Component/Sheets/Extensions/Font.ts
index 16524cd6fa..06f5ddaba4 100644
--- a/packages/base-render/src/Component/Sheets/Extensions/Font.ts
+++ b/packages/base-render/src/Component/Sheets/Extensions/Font.ts
@@ -165,6 +165,8 @@ export class Font extends SheetExtension {
if (wrapStrategy === WrapStrategy.WRAP && angle === 0) {
documentSkeleton.getModel().updateDocumentDataPageSize(cellWidth);
documentSkeleton.calculate();
+ } else {
+ documentSkeleton.getModel().updateDocumentDataPageSize(Infinity);
}
documentSkeleton.makeDirty(false);
diff --git a/packages/base-render/src/Shape/BaseScrollBar.ts b/packages/base-render/src/Shape/BaseScrollBar.ts
index 5b0bf20097..75d6082cc8 100644
--- a/packages/base-render/src/Shape/BaseScrollBar.ts
+++ b/packages/base-render/src/Shape/BaseScrollBar.ts
@@ -1,4 +1,4 @@
-import { IKeyValue, Nullable } from '@univerjs/core';
+import { Nullable } from '@univerjs/core';
import { Vector2 } from '../Basics/Vector2';
import { ThinScene } from '../ThinScene';
@@ -34,29 +34,25 @@ export class BaseScrollBar {
verticalMinusMiniThumb: number = 0;
- horizonBarRect!: Rect;
+ horizonBarRect: Nullable<Rect>;
- horizonThumbRect!: Rect;
+ horizonThumbRect: Nullable<Rect>;
- verticalBarRect!: Rect;
+ verticalBarRect: Nullable<Rect>;
- verticalThumbRect!: Rect;
+ verticalThumbRect: Nullable<Rect>;
- placeholderBarRect!: Rect;
-
- constructor(props?: IScrollBarProps) {
- this.setProps(props);
- }
+ placeholderBarRect: Nullable<Rect>;
get limitX() {
- if (!this.horizonThumbRect.visible) {
+ if (!this.horizonThumbRect?.visible) {
return 0;
}
return this.horizontalBarWidth - this.horizontalThumbWidth;
}
get limitY() {
- if (!this.verticalThumbRect.visible) {
+ if (!this.verticalThumbRect?.visible) {
return 0;
}
return this.verticalBarHeight - this.verticalThumbHeight;
@@ -97,23 +93,6 @@ export class BaseScrollBar {
return limit / actual;
}
- setProps(props?: IScrollBarProps) {
- if (!props) {
- return;
- }
-
- const themeKeys = Object.keys(props);
- if (themeKeys.length === 0) {
- return;
- }
-
- themeKeys.forEach((key) => {
- if (props[key as keyof IScrollBarProps] !== undefined) {
- (this as IKeyValue)[`_${key}`] = props[key as keyof IScrollBarProps];
- }
- });
- }
-
pick(coord: Vector2) {
if (this.horizonThumbRect?.isHit(coord)) {
return this.horizonThumbRect;
@@ -135,19 +114,19 @@ export class BaseScrollBar {
}
dispose() {
- this.horizonBarRect.dispose();
- this.horizonThumbRect.dispose();
- this.verticalBarRect.dispose();
- this.verticalThumbRect.dispose();
- this.placeholderBarRect.dispose();
+ this.horizonBarRect?.dispose();
+ this.horizonThumbRect?.dispose();
+ this.verticalBarRect?.dispose();
+ this.verticalThumbRect?.dispose();
+ this.placeholderBarRect?.dispose();
}
hasHorizonThumb() {
- return this.horizonThumbRect.visible;
+ return this.horizonThumbRect?.visible || false;
}
hasVerticalThumb() {
- return this.verticalThumbRect.visible;
+ return this.verticalThumbRect?.visible || false;
}
resize(
diff --git a/packages/base-render/src/Shape/ScrollBar.ts b/packages/base-render/src/Shape/ScrollBar.ts
index 51bec9996d..332776cee5 100644
--- a/packages/base-render/src/Shape/ScrollBar.ts
+++ b/packages/base-render/src/Shape/ScrollBar.ts
@@ -1,4 +1,4 @@
-import { EventState, Nullable, Observer } from '@univerjs/core';
+import { EventState, IKeyValue, Nullable, Observer } from '@univerjs/core';
import { COLORS, EVENT_TYPE } from '../Basics/Const';
import { IMouseEvent, IPointerEvent } from '../Basics/IEvents';
@@ -56,10 +56,11 @@ export class ScrollBar extends BaseScrollBar {
private _verticalPointerUpObserver: Nullable<Observer<IPointerEvent | IMouseEvent>>;
constructor(view: Viewport, props?: IScrollBarProps) {
- super(props);
+ super();
if (!view) {
console.warn('Missing viewport');
}
+ this.setProps(props);
this._view = view;
this._initialScrollRect();
this._view.setScrollBar(this);
@@ -85,11 +86,20 @@ export class ScrollBar extends BaseScrollBar {
const transform = new Transform([1, 0, 0, 1, left, top]);
const m = transform.getMatrix();
ctx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
- this.horizonBarRect.render(ctx);
- this.horizonThumbRect.translate(scrollX).render(ctx);
- this.verticalBarRect.render(ctx);
- this.verticalThumbRect.translate(undefined, scrollY).render(ctx);
- this.placeholderBarRect.render(ctx);
+ if (this._enableHorizontal) {
+ this.horizonBarRect!.render(ctx);
+ this.horizonThumbRect!.translate(scrollX).render(ctx);
+ }
+
+ if (this._enableVertical) {
+ this.verticalBarRect!.render(ctx);
+ this.verticalThumbRect!.translate(undefined, scrollY).render(ctx);
+ }
+
+ if (this._enableHorizontal && this._enableVertical) {
+ this.placeholderBarRect!.render(ctx);
+ }
+
ctx.restore();
}
@@ -131,6 +141,10 @@ export class ScrollBar extends BaseScrollBar {
this._view.makeDirty(state, true);
}
+ get barSize() {
+ return this._barSize;
+ }
+
override pick(coord: Vector2) {
if (this.horizonThumbRect?.isHit(coord)) {
return this.horizonThumbRect;
@@ -151,6 +165,23 @@ export class ScrollBar extends BaseScrollBar {
return null;
}
+ setProps(props?: IScrollBarProps) {
+ if (!props) {
+ return;
+ }
+
+ const themeKeys = Object.keys(props);
+ if (themeKeys.length === 0) {
+ return;
+ }
+
+ themeKeys.forEach((key) => {
+ if (props[key as keyof IScrollBarProps] !== undefined) {
+ (this as IKeyValue)[`_${key}`] = props[key as keyof IScrollBarProps];
+ }
+ });
+ }
+
private _resizeHorizontal(parentWidth: number, parentHeight: number, contentWidth: number) {
// ratioScrollY = 内容可视区高度/内容实际区高度= 滑动条的高度/滑道高度=滚动条的顶部距离/实际内容区域顶部距离;
if (!this._enableHorizontal) {
@@ -284,17 +315,17 @@ export class ScrollBar extends BaseScrollBar {
const mainScene = this._mainScene || this._view.scene;
- this.verticalThumbRect.on(
+ this.verticalThumbRect?.on(
EVENT_TYPE.PointerEnter,
this._hoverFunc(this._thumbHoverBackgroundColor!, this.verticalThumbRect)
);
- this.verticalThumbRect.on(
+ this.verticalThumbRect?.on(
EVENT_TYPE.PointerLeave,
this._hoverFunc(this._thumbBackgroundColor!, this.verticalThumbRect)
);
// 垂直滚动条槽的点击滚动事件
- this.verticalBarRect.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
+ this.verticalBarRect?.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
const e = evt as IPointerEvent | IMouseEvent;
this._view.scrollTo({
y: e.offsetY - this._view.top - this.verticalThumbHeight / 2,
@@ -303,14 +334,14 @@ export class ScrollBar extends BaseScrollBar {
});
// 垂直滚动条的拖拽事件
- this.verticalThumbRect.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
+ this.verticalThumbRect?.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
const e = evt as IPointerEvent | IMouseEvent;
const srcElement = this.verticalThumbRect;
this._isVerticalMove = true;
this._lastX = e.offsetX;
this._lastY = e.offsetY;
// srcElement.fill = this._thumbHoverBackgroundColor!;
- srcElement.setProps({
+ srcElement?.setProps({
fill: this._thumbActiveBackgroundColor!,
});
mainScene.disableEvent();
@@ -333,7 +364,7 @@ export class ScrollBar extends BaseScrollBar {
this._isVerticalMove = false;
// srcElement.fill = this._thumbBackgroundColor!;
mainScene.enableEvent();
- srcElement.setProps({
+ srcElement?.setProps({
fill: this._thumbBackgroundColor!,
});
// srcElement.makeDirty(true);
@@ -364,17 +395,17 @@ export class ScrollBar extends BaseScrollBar {
const mainScene = this._mainScene || this._view.scene;
- this.horizonThumbRect.on(
+ this.horizonThumbRect?.on(
EVENT_TYPE.PointerEnter,
this._hoverFunc(this._thumbHoverBackgroundColor!, this.horizonThumbRect)
);
- this.horizonThumbRect.on(
+ this.horizonThumbRect?.on(
EVENT_TYPE.PointerLeave,
this._hoverFunc(this._thumbBackgroundColor!, this.horizonThumbRect)
);
// 水平滚动条槽的点击滚动事件
- this.horizonBarRect.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
+ this.horizonBarRect?.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
const e = evt as IPointerEvent | IMouseEvent;
this._view.scrollTo({
x: e.offsetX - this._view.left - this.horizontalThumbWidth / 2,
@@ -383,13 +414,13 @@ export class ScrollBar extends BaseScrollBar {
});
// 水平滚动条的拖拽事件
- this.horizonThumbRect.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
+ this.horizonThumbRect?.on(EVENT_TYPE.PointerDown, (evt: unknown, state: EventState) => {
const e = evt as IPointerEvent | IMouseEvent;
this._isHorizonMove = true;
this._lastX = e.offsetX;
this._lastY = e.offsetY;
// this.fill = this._thumbHoverBackgroundColor!;
- this.horizonThumbRect.setProps({
+ this.horizonThumbRect?.setProps({
fill: this._thumbActiveBackgroundColor!,
});
this.makeViewDirty(true);
@@ -412,7 +443,7 @@ export class ScrollBar extends BaseScrollBar {
this._isHorizonMove = false;
mainScene.enableEvent();
// srcElement.fill = this._thumbBackgroundColor!;
- srcElement.setProps({
+ srcElement?.setProps({
fill: this._thumbBackgroundColor!,
});
// srcElement.makeDirty(true);
diff --git a/packages/base-sheets/src/Basics/Const/COMMAND_LISTENER_CONST.ts b/packages/base-sheets/src/Basics/Const/COMMAND_LISTENER_CONST.ts
new file mode 100644
index 0000000000..bb2956c3d8
--- /dev/null
+++ b/packages/base-sheets/src/Basics/Const/COMMAND_LISTENER_CONST.ts
@@ -0,0 +1,31 @@
+import { AddWorksheetMergeMutation } from '../../commands/mutations/add-worksheet-merge.mutation';
+import { InsertColMutation, InsertRowMutation } from '../../commands/mutations/insert-row-col.mutation';
+import { MoveRowsMutation } from '../../commands/mutations/move-rows-cols.mutation';
+import { RemoveColMutation, RemoveRowMutation } from '../../commands/mutations/remove-row-col.mutation';
+import { RemoveWorksheetMergeMutation } from '../../commands/mutations/remove-worksheet-merge.mutation';
+import { SetBorderStylesMutation } from '../../commands/mutations/set-border-styles.mutation';
+import { SetColHiddenMutation, SetColVisibleMutation } from '../../commands/mutations/set-col-visible.mutation';
+import { SetRangeValuesMutation } from '../../commands/mutations/set-range-values.mutation';
+import { SetRowHiddenMutation, SetRowVisibleMutation } from '../../commands/mutations/set-row-visible.mutation';
+import { SetWorksheetActivateMutation } from '../../commands/mutations/set-worksheet-activate.mutation';
+import { SetWorksheetColWidthMutation } from '../../commands/mutations/set-worksheet-col-width.mutation';
+import { SetWorksheetRowHeightMutation } from '../../commands/mutations/set-worksheet-row-height.mutation';
+
+export const COMMAND_LISTENER_SKELETON_CHANGE = [
+ SetWorksheetRowHeightMutation.id,
+ SetWorksheetColWidthMutation.id,
+ SetWorksheetActivateMutation.id,
+ InsertRowMutation.id,
+ RemoveRowMutation.id,
+ InsertColMutation.id,
+ RemoveColMutation.id,
+ AddWorksheetMergeMutation.id,
+ RemoveWorksheetMergeMutation.id,
+ MoveRowsMutation.id,
+ SetRangeValuesMutation.id,
+ SetBorderStylesMutation.id,
+ SetColHiddenMutation.id,
+ SetColVisibleMutation.id,
+ SetRowHiddenMutation.id,
+ SetRowVisibleMutation.id,
+];
diff --git a/packages/base-sheets/src/Basics/Const/index.ts b/packages/base-sheets/src/Basics/Const/index.ts
index f9a1f312cd..6786c2fabb 100644
--- a/packages/base-sheets/src/Basics/Const/index.ts
+++ b/packages/base-sheets/src/Basics/Const/index.ts
@@ -1,4 +1,5 @@
-// @index('./*.ts', f => `export * from '${f.path}'`)
-export * from './DEFAULT_SPREADSHEET_PLUGIN_DATA';
-export * from './DEFAULT_SPREADSHEET_VIEW';
-// @endindex
+// @index('./*.ts', f => `export * from '${f.path}'`)
+export * from './COMMAND_LISTENER_CONST';
+export * from './DEFAULT_SPREADSHEET_PLUGIN_DATA';
+export * from './DEFAULT_SPREADSHEET_VIEW';
+// @endindex
diff --git a/packages/base-sheets/src/Controller/Selection.controller.ts b/packages/base-sheets/src/Controller/Selection.controller.ts
index 1bae088c2a..de171becfc 100644
--- a/packages/base-sheets/src/Controller/Selection.controller.ts
+++ b/packages/base-sheets/src/Controller/Selection.controller.ts
@@ -18,7 +18,6 @@ import { Inject } from '@wendellhu/redi';
import { getSheetObject, ISheetObjectParam } from '../Basics/component-tools';
import { VIEWPORT_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
-import { SetWorksheetActivateMutation } from '../commands/mutations/set-worksheet-activate.mutation';
import { SetSelectionsOperation } from '../commands/operations/selection.operation';
import { ISetZoomRatioOperationParams, SetZoomRatioOperation } from '../commands/operations/set-zoom-ratio.operation';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../services/selection-manager.service';
@@ -195,10 +194,6 @@ export class SelectionController extends Disposable {
}
const { unitId, sheetId, skeleton, commandId } = param;
- if (commandId != null && commandId !== SetWorksheetActivateMutation.id) {
- return;
- }
-
const currentRender = this._renderManagerService.getRenderById(unitId);
if (currentRender == null) {
diff --git a/packages/base-sheets/src/Controller/header-resize.controller.ts b/packages/base-sheets/src/Controller/header-resize.controller.ts
index 2b884b2d75..0884872ed9 100644
--- a/packages/base-sheets/src/Controller/header-resize.controller.ts
+++ b/packages/base-sheets/src/Controller/header-resize.controller.ts
@@ -297,9 +297,9 @@ export class HeaderResizeController extends Disposable {
const viewPort = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
- const scrollBarHorizontalHeight = (viewPort?.getScrollBar()?.horizonBarRect.height || 0) + 10;
+ const scrollBarHorizontalHeight = (viewPort?.getScrollBar()?.horizonBarRect?.height || 0) + 10;
- const scrollBarVerticalWidth = (viewPort?.getScrollBar()?.verticalBarRect.width || 0) + 10;
+ const scrollBarVerticalWidth = (viewPort?.getScrollBar()?.verticalBarRect?.width || 0) + 10;
const transformCoord = getTransformCoord(evt.offsetX, evt.offsetY, scene, skeleton);
diff --git a/packages/base-sheets/src/Controller/sheet-render.controller.ts b/packages/base-sheets/src/Controller/sheet-render.controller.ts
index 194c9848a6..7c7012bb0c 100644
--- a/packages/base-sheets/src/Controller/sheet-render.controller.ts
+++ b/packages/base-sheets/src/Controller/sheet-render.controller.ts
@@ -16,19 +16,9 @@ import {
} from '@univerjs/core';
import { Inject } from '@wendellhu/redi';
+import { COMMAND_LISTENER_SKELETON_CHANGE } from '../Basics/Const/COMMAND_LISTENER_CONST';
import { SHEET_VIEW_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
-import { AddWorksheetMergeMutation } from '../commands/mutations/add-worksheet-merge.mutation';
-import { InsertColMutation, InsertRowMutation } from '../commands/mutations/insert-row-col.mutation';
-import { MoveRowsMutation } from '../commands/mutations/move-rows-cols.mutation';
-import { RemoveColMutation, RemoveRowMutation } from '../commands/mutations/remove-row-col.mutation';
-import { RemoveWorksheetMergeMutation } from '../commands/mutations/remove-worksheet-merge.mutation';
-import { SetBorderStylesMutation } from '../commands/mutations/set-border-styles.mutation';
-import { SetColHiddenMutation, SetColVisibleMutation } from '../commands/mutations/set-col-visible.mutation';
-import { SetRangeValuesMutation } from '../commands/mutations/set-range-values.mutation';
-import { SetRowHiddenMutation, SetRowVisibleMutation } from '../commands/mutations/set-row-visible.mutation';
import { SetWorksheetActivateMutation } from '../commands/mutations/set-worksheet-activate.mutation';
-import { SetWorksheetColWidthMutation } from '../commands/mutations/set-worksheet-col-width.mutation';
-import { SetWorksheetRowHeightMutation } from '../commands/mutations/set-worksheet-row-height.mutation';
import { SelectionManagerService } from '../services/selection-manager.service';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
@@ -101,24 +91,7 @@ export class SheetRenderController extends Disposable {
}
private _commandExecutedListener() {
- const updateCommandList = [
- SetWorksheetRowHeightMutation.id,
- SetWorksheetColWidthMutation.id,
- SetWorksheetActivateMutation.id,
- InsertRowMutation.id,
- RemoveRowMutation.id,
- InsertColMutation.id,
- RemoveColMutation.id,
- AddWorksheetMergeMutation.id,
- RemoveWorksheetMergeMutation.id,
- MoveRowsMutation.id,
- SetRangeValuesMutation.id,
- SetBorderStylesMutation.id,
- SetColHiddenMutation.id,
- SetColVisibleMutation.id,
- SetRowHiddenMutation.id,
- SetRowVisibleMutation.id,
- ];
+ const updateCommandList = COMMAND_LISTENER_SKELETON_CHANGE;
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 8016f9a878..0d8912f922 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -104,6 +104,7 @@ export { SetWorksheetOrderCommand } from './commands/commands/set-worksheet-orde
export { SetRowHeightCommand as SetWorksheetRowHeightCommand } from './commands/commands/set-worksheet-row-height.command';
export { SetWorksheetShowCommand } from './commands/commands/set-worksheet-show.command';
export { ChangeZoomRatioCommand, SetZoomRatioCommand } from './commands/commands/set-zoom-ratio.command';
+export { AddWorksheetMergeMutation } from './commands/mutations/add-worksheet-merge.mutation';
export {
InsertColMutation,
InsertColMutationUndoFactory as InsertColMutationFactory,
@@ -111,7 +112,11 @@ export {
InsertRowMutationUndoFactory as InsertRowMutationFactory,
} from './commands/mutations/insert-row-col.mutation';
export { InsertSheetMutation, InsertSheetUndoMutationFactory } from './commands/mutations/insert-sheet.mutation';
+export { MoveRowsMutation } from './commands/mutations/move-rows-cols.mutation';
+export { RemoveColMutation, RemoveRowMutation } from './commands/mutations/remove-row-col.mutation';
export { RemoveSheetMutation, RemoveSheetUndoMutationFactory } from './commands/mutations/remove-sheet.mutation';
+export { RemoveWorksheetMergeMutation } from './commands/mutations/remove-worksheet-merge.mutation';
+export { SetBorderStylesMutation } from './commands/mutations/set-border-styles.mutation';
export { SetColHiddenMutation, SetColVisibleMutation } from './commands/mutations/set-col-visible.mutation';
export { SetFrozenMutation } from './commands/mutations/set-frozen.mutation';
export type { ISetRangeStyleMutationParams } from './commands/mutations/set-range-styles.mutation';
@@ -141,4 +146,5 @@ export { SetSelectionsOperation } from './commands/operations/selection.operatio
export { SetZoomRatioOperation } from './commands/operations/set-zoom-ratio.operation';
export * from './Locale';
export { SheetSkeletonManagerService } from './services/sheet-skeleton-manager.service';
+
// #endregion
diff --git a/packages/base-sheets/src/services/selection-manager.service.ts b/packages/base-sheets/src/services/selection-manager.service.ts
index 22e2b4aea8..275be46e62 100644
--- a/packages/base-sheets/src/services/selection-manager.service.ts
+++ b/packages/base-sheets/src/services/selection-manager.service.ts
@@ -40,6 +40,8 @@ export class SelectionManagerService implements IDisposable {
// eslint-disable-next-line @typescript-eslint/member-ordering
readonly selectionInfo$ = this._selectionInfo$.asObservable();
+ private _dirty: boolean = true;
+
// get isSelectionEnabled() {
// return this._isSelectionEnabled;
// }
@@ -76,6 +78,10 @@ export class SelectionManagerService implements IDisposable {
// this._currentSelection$.complete();
}
+ makeDirty(dirty: boolean = true) {
+ this._dirty = dirty;
+ }
+
refreshSelection() {
if (this._currentSelection == null) {
return;
@@ -84,6 +90,10 @@ export class SelectionManagerService implements IDisposable {
}
setCurrentSelection(param: ISelectionManagerSearchParam) {
+ if (this._dirty === false) {
+ return;
+ }
+
this._currentSelection = param;
this.refresh(param);
diff --git a/packages/core/src/Docs/Domain/DocumentBodyModel.ts b/packages/core/src/Docs/Domain/DocumentBodyModel.ts
index 51e9f6c3bc..0d897004e6 100644
--- a/packages/core/src/Docs/Domain/DocumentBodyModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentBodyModel.ts
@@ -1,5 +1,4 @@
import { IDisposable } from '@wendellhu/redi';
-import { Observable, Subject } from 'rxjs';
import { Nullable } from '../../Shared/Types';
import { IDocumentBody, ITextRun } from '../../Types/Interfaces/IDocumentData';
@@ -11,10 +10,6 @@ export type DocumentBodyModelOrSimple = DocumentBodyModelSimple | DocumentBodyMo
export class DocumentBodyModelSimple implements IDisposable {
children: DataStreamTreeNode[] = [];
- protected readonly _modelChange$: Subject<void> = new Subject();
-
- readonly modelChange$: Observable<void> = this._modelChange$.asObservable();
-
constructor(
/** @deprecated this does not hold true fact about the text model, do not use this directly */
public body: IDocumentBody
@@ -27,7 +22,6 @@ export class DocumentBodyModelSimple implements IDisposable {
}
dispose(): void {
- this._modelChange$.complete();
this.children.forEach((child) => {
child.dispose();
});
@@ -233,7 +227,6 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {
reset(body: IDocumentBody) {
this.children = this._transformToTree(body.dataStream);
this.body = body;
- this._modelChange$.next();
}
insert(insertBody: IDocumentBody, insertIndex = 0) {
@@ -319,14 +312,11 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {
}
});
}
-
- this._modelChange$.next();
}
delete(currentIndex: number, textLength: number) {
const nodes = this.children;
this.deleteTree(nodes, currentIndex, textLength);
- this._modelChange$.next();
}
getParagraphByTree(nodes: DataStreamTreeNode[], insertIndex: number): Nullable<DataStreamTreeNode> {
diff --git a/packages/ui-plugin-sheets/src/View/SheetContainer/index.module.less b/packages/ui-plugin-sheets/src/View/SheetContainer/index.module.less
index 707919e948..a858572988 100644
--- a/packages/ui-plugin-sheets/src/View/SheetContainer/index.module.less
+++ b/packages/ui-plugin-sheets/src/View/SheetContainer/index.module.less
@@ -1,7 +1,13 @@
@import '@univerjs/base-ui/src/Basics/CSS/Skin/default.module.less';
+@sheet-footer-bar-height: 40px;
+
+:export {
+ --sheet-footer-bar-height: unit(@sheet-footer-bar-height);
+}
+
.sheet-container {
- height: 40px;
+ height: @sheet-footer-bar-height;
padding: 0 @padding-xl;
border-top: 1px solid rgb(var(--border-color));
box-sizing: border-box;
diff --git a/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts b/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts
index 036403163c..114d1e9765 100644
--- a/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts
@@ -1,7 +1,18 @@
-import { DeviceInputEventType, IRenderManagerService, ISelectionTransformerShapeManager } from '@univerjs/base-render';
-import { getSheetObject, SelectionManagerService, SheetSkeletonManagerService } from '@univerjs/base-sheets';
+import {
+ DeviceInputEventType,
+ getCanvasOffsetByEngine,
+ IRenderManagerService,
+ ISelectionTransformerShapeManager,
+} from '@univerjs/base-render';
+import {
+ COMMAND_LISTENER_SKELETON_CHANGE,
+ getSheetObject,
+ SelectionManagerService,
+ SheetSkeletonManagerService,
+} from '@univerjs/base-sheets';
import {
Disposable,
+ ICommandInfo,
ICommandService,
IUniverInstanceService,
LifecycleStages,
@@ -14,6 +25,11 @@ import { SetActivateCellEditOperation } from '../commands/operations/activate-ce
import { SetCellEditOperation } from '../commands/operations/cell-edit.operation';
import { IEditorBridgeService } from '../services/editor-bridge.service';
+interface ISetWorksheetMutationParams {
+ workbookId: string;
+ worksheetId: string;
+}
+
@OnLifecycle(LifecycleStages.Rendered, EditorBridgeController)
export class EditorBridgeController extends Disposable {
constructor(
@@ -53,7 +69,7 @@ export class EditorBridgeController extends Disposable {
const { skeleton, unitId, sheetId } = currentSkeletonManager;
- const { scene } = sheetObject;
+ const { scene, engine } = sheetObject;
if (params == null || params.length === 0 || skeleton == null) {
return;
@@ -79,6 +95,8 @@ export class EditorBridgeController extends Disposable {
return;
}
+ const canvasOffset = getCanvasOffsetByEngine(engine);
+
let { startX, startY, endX, endY } = actualRangeWithCoord;
const { scaleX, scaleY } = scene.getAncestorScale();
@@ -98,26 +116,6 @@ export class EditorBridgeController extends Disposable {
documentLayoutObject = skeleton.getBlankCellDocumentModel(startRow, startColumn, true);
}
- // let documentModel = documentModelObject?.documentModel;
-
- // if (documentModel == null) {
- // documentModel = skeleton.getBlankCellDocumentModel(startRow, startColumn).documentModel;
- // }
-
- // documentModelObject.documentModel = documentModel;
-
- // this._editorBridgeService.setState({
- // position: {
- // startX,
- // startY,
- // endX,
- // endY,
- // },
- // unitId,
- // sheetId,
- // documentLayoutObject,
- // });
-
this._commandService.executeCommand(SetActivateCellEditOperation.id, {
position: {
startX,
@@ -125,6 +123,7 @@ export class EditorBridgeController extends Disposable {
endX,
endY,
},
+ canvasOffset,
row: startRow,
column: startColumn,
unitId,
@@ -160,9 +159,13 @@ export class EditorBridgeController extends Disposable {
private _hideEditor() {
if (this._editorBridgeService.isVisible().visible === true) {
+ this._selectionManagerService.makeDirty(false);
this._commandService.executeCommand(SetCellEditOperation.id, {
visible: false,
});
+ setTimeout(() => {
+ this._selectionManagerService.makeDirty(true);
+ }, 0);
}
}
@@ -170,5 +173,15 @@ export class EditorBridgeController extends Disposable {
return getSheetObject(this._currentUniverService, this._renderManagerService);
}
- private _commandExecutedListener() {}
+ private _commandExecutedListener() {
+ const updateCommandList = COMMAND_LISTENER_SKELETON_CHANGE;
+
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (updateCommandList.includes(command.id)) {
+ this._hideEditor();
+ }
+ })
+ );
+ }
}
diff --git a/packages/ui-plugin-sheets/src/controller/editor/editing.controller.ts b/packages/ui-plugin-sheets/src/controller/editor/editing.controller.ts
index 9af0c3bf6e..c04c9df552 100644
--- a/packages/ui-plugin-sheets/src/controller/editor/editing.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/editor/editing.controller.ts
@@ -1,12 +1,7 @@
import { DocSkeletonManagerService, getDocObject, TextSelectionManagerService } from '@univerjs/base-docs';
import { IRenderManagerService, ITextSelectionRenderManager } from '@univerjs/base-render';
-import { SetRangeValuesCommand } from '@univerjs/base-sheets';
-import { KeyCode } from '@univerjs/base-ui';
import {
Disposable,
- DocumentModel,
- ICellData,
- ICommandInfo,
ICommandService,
IUniverInstanceService,
LifecycleStages,
@@ -16,8 +11,7 @@ import {
import { Inject } from '@wendellhu/redi';
import { Subscription } from 'rxjs';
-import { SetCellEditOperation } from '../../commands/operations/cell-edit.operation';
-import { IEditorBridgeService, IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
+import { IEditorBridgeService } from '../../services/editor-bridge.service';
@OnLifecycle(LifecycleStages.Steady, EditingController)
export class EditingController extends Disposable {
@@ -49,76 +43,7 @@ export class EditingController extends Disposable {
private _initialNormalInput() {}
- private _commandExecutedListener() {
- const updateCommandList = [SetCellEditOperation.id];
-
- this.disposeWithMe(
- this._commandService.onCommandExecuted((command: ICommandInfo) => {
- if (updateCommandList.includes(command.id)) {
- const params = command.params as IEditorBridgeServiceVisibleParam;
- const { visible, keycode, eventType } = params;
-
- if (visible === true) {
- return;
- }
-
- if (keycode === KeyCode.ESC) {
- return;
- }
-
- const state = this._editorBridgeService.getState();
-
- if (state == null) {
- return;
- }
-
- const { unitId, sheetId, row, column, documentLayoutObject } = state;
-
- if (documentLayoutObject == null) {
- return;
- }
-
- const workbook = this._currentUniverService.getUniverSheetInstance(unitId);
-
- const worksheet = workbook?.getSheetBySheetId(sheetId);
-
- if (worksheet == null) {
- return;
- }
-
- const cellData: ICellData = worksheet.getCell(row, column) || {};
-
- const documentModel = documentLayoutObject.documentModel as DocumentModel;
-
- const snapshot = documentModel.getSnapshot();
-
- const body = snapshot.body;
-
- if (body == null) {
- return;
- }
-
- if (body.textRuns && body.textRuns.length > 1) {
- cellData.p = snapshot;
- } else {
- cellData.v = body.dataStream;
- }
-
- this._commandService.executeCommand(SetRangeValuesCommand.id, {
- worksheetId: sheetId,
- workbookId: unitId,
- range: {
- startRow: row,
- startColumn: column,
- endRow: row,
- endColumn: column,
- },
- value: cellData,
- });
- }
- })
- );
- }
+ private _commandExecutedListener() {}
private _getDocObject() {
return getDocObject(this._currentUniverService, this._renderManagerService);
diff --git a/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts b/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
index 1b7f1eac70..9dae6a12de 100644
--- a/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
@@ -5,6 +5,7 @@ import {
NORMAL_TEXT_SELECTION_PLUGIN_NAME,
RichTextEditingMutation,
TextSelectionManagerService,
+ VIEWPORT_KEY,
} from '@univerjs/base-docs';
import {
DeviceInputEventType,
@@ -17,6 +18,7 @@ import {
ITextSelectionRenderManager,
Rect,
Scene,
+ ScrollBar,
} from '@univerjs/base-render';
import {
Disposable,
@@ -46,11 +48,17 @@ import { getEditorObject } from '../../Basics/editor/get-editor-object';
import { SetCellEditOperation } from '../../commands/operations/cell-edit.operation';
import { ICellEditorManagerService } from '../../services/editor/cell-editor-manager.service';
import { IEditorBridgeService } from '../../services/editor-bridge.service';
+import styles from '../../View/SheetContainer/index.module.less';
const HIDDEN_EDITOR_POSITION = -1000;
const EDITOR_INPUT_SELF_EXTEND_GAP = 5;
+interface ICanvasOffset {
+ left: number;
+ top: number;
+}
+
@OnLifecycle(LifecycleStages.Steady, StartEditController)
export class StartEditController extends Disposable {
private _onInputSubscription: Nullable<Subscription>;
@@ -157,6 +165,7 @@ export class StartEditController extends Disposable {
private _predictingSize(
actualRangeWithCoord: IPosition,
+ canvasOffset: ICanvasOffset,
documentSkeleton: DocumentSkeleton,
documentLayoutObject: IDocumentLayoutObject
) {
@@ -171,7 +180,7 @@ export class StartEditController extends Disposable {
if (wrapStrategy === WrapStrategy.WRAP && angle === 0) {
return documentSkeleton.getActualSize();
}
- documentSkeleton.getModel().updateDocumentDataPageSize(clientWidth - startX);
+ documentSkeleton.getModel().updateDocumentDataPageSize(clientWidth - startX - canvasOffset.left);
documentSkeleton.calculate();
const size = documentSkeleton.getActualSize();
@@ -196,20 +205,14 @@ export class StartEditController extends Disposable {
private _fitTextSize(
actualRangeWithCoord: IPosition,
+ canvasOffset: ICanvasOffset,
documentSkeleton: DocumentSkeleton,
documentLayoutObject: IDocumentLayoutObject
) {
- const editorObject = this._getEditorObject();
-
- if (editorObject == null) {
- return;
- }
-
- const { document, scene, engine } = editorObject;
-
const { startX, startY, endX, endY } = actualRangeWithCoord;
const { actualWidth, actualHeight } = this._predictingSize(
actualRangeWithCoord,
+ canvasOffset,
documentSkeleton,
documentLayoutObject
);
@@ -246,27 +249,76 @@ export class StartEditController extends Disposable {
// re-calculate skeleton(viewModel for component)
documentSkeleton.calculate();
- // resize canvas
- engine.resizeBySize(editorWidth, editorHeight);
+ this._editAreaProcessing(editorWidth, editorHeight, actualRangeWithCoord, canvasOffset, fill);
+
+ // this._textSelectionRenderManager.sync();
+ }
+
+ private _editAreaProcessing(
+ editorWidth: number,
+ editorHeight: number,
+ actualRangeWithCoord: IPosition,
+ canvasOffset: ICanvasOffset,
+ fill: Nullable<string>
+ ) {
+ const editorObject = this._getEditorObject();
+
+ if (editorObject == null) {
+ return;
+ }
+
+ const { startX, startY, endX, endY } = actualRangeWithCoord;
+
+ const { document: documentComponent, scene, engine } = editorObject;
+
+ const viewportMain = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+
+ const scrollBar = viewportMain?.getScrollBar() as Nullable<ScrollBar>;
+
+ // Todo: @
+ const clientHeight =
+ document.body.clientHeight - startY - parseFloat(styles.sheetFooterBarHeight) - canvasOffset.top;
+
+ const clientWidth = document.body.clientWidth - startX - canvasOffset.left;
+
+ editorWidth += scrollBar?.barSize || 0;
+
+ if (editorWidth > clientWidth) {
+ editorWidth = clientWidth;
+ }
+
+ let physicHeight = editorHeight;
scene.transformByState({
width: editorWidth,
height: editorHeight,
});
- document.resize(editorWidth, editorHeight);
+ documentComponent.resize(editorWidth, editorHeight);
+
+ if (physicHeight > clientHeight) {
+ physicHeight = clientHeight;
+ if (scrollBar == null) {
+ viewportMain && new ScrollBar(viewportMain);
+ } else {
+ viewportMain?.resetSizeAndScrollBar();
+ }
+ } else {
+ viewportMain?.getScrollBar()?.dispose();
+ }
this._addBackground(scene, editorWidth, editorHeight, fill);
+ // resize canvas
+ engine.resizeBySize(editorWidth, physicHeight);
+
this._cellEditorManagerService.setState({
startX,
startY,
endX: editorWidth + startX,
- endY: editorHeight + startY,
+ endY: physicHeight + startY,
show: true,
});
-
- // this._textSelectionRenderManager.sync();
}
private _addBackground(scene: Scene, editorWidth: number, editorHeight: number, fill?: Nullable<string>) {
@@ -320,7 +372,7 @@ export class StartEditController extends Disposable {
return;
}
- const { position, documentLayoutObject } = state;
+ const { position, documentLayoutObject, canvasOffset } = state;
const editorObject = this._getEditorObject();
@@ -328,7 +380,7 @@ export class StartEditController extends Disposable {
return;
}
- const { document } = editorObject;
+ const { document, scene } = editorObject;
this._contextService.setContextValue(FOCUSING_EDITOR, true);
@@ -342,7 +394,7 @@ export class StartEditController extends Disposable {
const documentModel = skeleton.getModel() as DocumentModel;
- this._fitTextSize(position, skeleton, documentLayoutObject);
+ this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject);
// move selection
if (eventType === DeviceInputEventType.Keyboard) {
@@ -366,6 +418,10 @@ export class StartEditController extends Disposable {
} else {
const cursor = documentModel.getBodyModel().getLastIndex() - 1 || 0;
+ scene.getViewport(VIEWPORT_KEY.VIEW_MAIN)?.scrollTo({
+ y: Infinity,
+ });
+
this._textSelectionManagerService.replace([
{
cursorStart: cursor,
@@ -476,9 +532,9 @@ export class StartEditController extends Disposable {
return;
}
- const { position, documentLayoutObject } = param;
+ const { position, documentLayoutObject, canvasOffset } = param;
- this._fitTextSize(position, skeleton, documentLayoutObject);
+ this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject);
// const editorObject = this._getEditorObject();
diff --git a/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts b/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts
index c3239fe53d..a55fdd8925 100644
--- a/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts
+++ b/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts
@@ -16,6 +16,7 @@ export interface IEditorBridgeServiceParam {
row: number;
column: number;
position: IPosition;
+ canvasOffset: { left: number; top: number };
documentLayoutObject: IDocumentLayoutObject;
}
diff --git a/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts b/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts
index c5ecb38b79..9c3a0942b9 100644
--- a/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts
+++ b/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts
@@ -5,6 +5,7 @@ import { filter } from 'rxjs/operators';
import { SHEET_UI_PLUGIN_NAME } from './Basics';
import { SheetClipboardController } from './controller/clipboard/clipboard.controller';
import { SheetContextMenuController } from './controller/contextmenu/contextmenu.controller';
+import { EditingController } from './controller/editor/editing.controller';
import { EndEditController } from './controller/editor/end-edit.controller';
import { InitializeEditorController } from './controller/editor/initialize-editor.controller';
import { StartEditController } from './controller/editor/start-edit.controller';
@@ -51,6 +52,7 @@ export class SheetUIPlugin extends Plugin {
[SheetUIController],
[InitializeEditorController],
[StartEditController],
+ [EditingController],
[EndEditController],
] as Dependency[]
).forEach((d) => injector.add(d));