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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat: user facade api & onCommented & cellPointerdown & hide selection (#4342)
lumixf13e573aa7ec
diff --git a/packages/core/src/facade/f-univer.ts b/packages/core/src/facade/f-univer.tsindex afad97371a..b90df1d5f3 100644--- a/packages/core/src/facade/f-univer.ts+++ b/packages/core/src/facade/f-univer.ts@@ -26,6 +26,7 @@ import { Univer } from '../univer';import { FBase } from './f-base';import { FBlob } from './f-blob';import { FHooks } from './f-hooks';+import { FUserManager } from './f-usermanager';export class FUniver extends FBase {/**@@ -162,4 +163,8 @@ export class FUniver extends FBase {newBlob(): FBlob {return this._injector.createInstance(FBlob);}++ getUserManager(): FUserManager {+ return this._injector.createInstance(FUserManager);+ }}diff --git a/packages/core/src/facade/f-usermanager.ts b/packages/core/src/facade/f-usermanager.tsnew file mode 100644index 0000000000..b77d3ccb86--- /dev/null+++ b/packages/core/src/facade/f-usermanager.ts@@ -0,0 +1,37 @@+/**+ * 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 { IUser } from '../services/user-manager/user-manager.service';+import { Inject, Injector } from '../common/di';+import { UserManagerService } from '../services/user-manager/user-manager.service';+import { FBase } from './f-base';++export class FUserManager extends FBase {+ constructor(+ @Inject(Injector) protected readonly _injector: Injector,+ @Inject(UserManagerService) private readonly _userManagerService: UserManagerService+ ) {+ super();+ }++ /**+ * Get current user info+ * e.g. `univerAPI.getUserManager().getCurrentUser()`+ */+ getCurrentUser(): IUser {+ return this._userManagerService.getCurrentUser();+ }+}diff --git a/packages/core/src/services/user-manager/user-manager.service.ts b/packages/core/src/services/user-manager/user-manager.service.tsindex 01f12884c8..0cfc92c258 100644--- a/packages/core/src/services/user-manager/user-manager.service.ts+++ b/packages/core/src/services/user-manager/user-manager.service.ts@@ -31,6 +31,7 @@ export class UserManagerService {* @memberof UserManagerService*/public currentUser$ = this._currentUser$.asObservable();+getCurrentUser<T extends IUser>() {return this._currentUser$.getValue() as T;}diff --git a/packages/sheets-thread-comment/src/facade/f-range.ts b/packages/sheets-thread-comment/src/facade/f-range.tsindex 97679652f7..fa740e1b09 100644--- a/packages/sheets-thread-comment/src/facade/f-range.ts+++ b/packages/sheets-thread-comment/src/facade/f-range.ts@@ -15,8 +15,8 @@*/import { ICommandService, type IDocumentBody, type Nullable, Tools, UserManagerService } from '@univerjs/core';-import { FRange } from '@univerjs/sheets/facade';import { SheetsThreadCommentModel } from '@univerjs/sheets-thread-comment';+import { FRange } from '@univerjs/sheets/facade';import { AddCommentCommand, DeleteCommentTreeCommand, getDT } from '@univerjs/thread-comment';import { FThreadComment } from './f-thread-comment';diff --git a/packages/sheets-thread-comment/src/facade/f-worksheet.ts b/packages/sheets-thread-comment/src/facade/f-worksheet.tsindex 67d4683e9d..59d9e7de88 100644--- a/packages/sheets-thread-comment/src/facade/f-worksheet.ts+++ b/packages/sheets-thread-comment/src/facade/f-worksheet.ts@@ -14,8 +14,12 @@* limitations under the License.*/-import { FWorksheet } from '@univerjs/sheets/facade';+import type { IDisposable } from '@univerjs/core';+import type { IAddCommentCommandParams } from '@univerjs/thread-comment';+import { ICommandService } from '@univerjs/core';import { SheetsThreadCommentModel } from '@univerjs/sheets-thread-comment';+import { FWorksheet } from '@univerjs/sheets/facade';+import { AddCommentCommand } from '@univerjs/thread-comment';import { FThreadComment } from './f-thread-comment';export interface IFWorksheetCommentMixin {@@ -32,6 +36,20 @@ export class FWorksheetCommentMixin extends FWorksheet implements IFWorksheetComconst comments = sheetsTheadCommentModel.getSubUnitAll(this._workbook.getUnitId(), this._worksheet.getSheetId());return comments.map((comment) => this._injector.createInstance(FThreadComment, comment));}++ /**+ * Subscribe to comment events.+ * @param callback Callback function, param contains comment info and target cell.+ */+ onCommented(callback: (params: IAddCommentCommandParams) => void): IDisposable {+ const commandService = this._injector.get(ICommandService);+ return commandService.onCommandExecuted((command) => {+ if (command.id === AddCommentCommand.id) {+ const params = command.params as IAddCommentCommandParams;+ callback(params);+ }+ });+ }}FWorksheet.extend(FWorksheetCommentMixin);diff --git a/packages/sheets-ui/src/controllers/hover-render.controller.ts b/packages/sheets-ui/src/controllers/hover-render.controller.tsindex 22c62052e5..575040d21d 100644--- a/packages/sheets-ui/src/controllers/hover-render.controller.ts+++ b/packages/sheets-ui/src/controllers/hover-render.controller.ts@@ -15,13 +15,12 @@*/import type { Nullable, Workbook } from '@univerjs/core';-import { Disposable, DisposableCollection, fromEventSubject, Inject } from '@univerjs/core';import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';-import { throttleTime } from 'rxjs';-import { HoverManagerService } from '../services/hover-manager.service';import type { ISheetSkeletonManagerParam } from '../services/sheet-skeleton-manager.service';-import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';+import { Disposable, DisposableCollection, fromEventSubject, Inject } from '@univerjs/core';+import { HoverManagerService } from '../services/hover-manager.service';import { SheetScrollManagerService } from '../services/scroll-manager.service';+import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';export class HoverRenderController extends Disposable implements IRenderModule {private _active = false;@@ -60,12 +59,17 @@ export class HoverRenderController extends Disposable implements IRenderModule {this._active = true;}));- disposeSet.add(fromEventSubject(mainComponent.onPointerMove$).pipe(throttleTime(30)).subscribe((evt) => {+ disposeSet.add(fromEventSubject(mainComponent.onPointerMove$).subscribe((evt) => {this._active = true;- this._hoverManagerService.triggerMouseMove(unitId, evt.offsetX, evt.offsetY);+ this._hoverManagerService.triggerMouseMove(unitId, evt);+ }));++ disposeSet.add(mainComponent.onPointerDown$.subscribeEvent((evt) => {+ this._hoverManagerService.triggerPointerDown(unitId, evt);}));disposeSet.add(mainComponent.onPointerUp$.subscribeEvent((evt) => {+ this._hoverManagerService.triggerPointerUp(unitId, evt);this._hoverManagerService.triggerClick(unitId, evt.offsetX, evt.offsetY);}));diff --git a/packages/sheets-ui/src/facade/f-workbook.ts b/packages/sheets-ui/src/facade/f-workbook.tsindex 18cf8f19e7..45d9c468bb 100644--- a/packages/sheets-ui/src/facade/f-workbook.ts+++ b/packages/sheets-ui/src/facade/f-workbook.ts@@ -15,9 +15,9 @@*/import type { IDisposable, Nullable } from '@univerjs/core';+import type { ICellPosWithEvent, IEditorBridgeServiceVisibleParam, IHoverRichTextInfo, IHoverRichTextPosition, IScrollState } from '@univerjs/sheets-ui';import { awaitTime, ICommandService, ILogService, toDisposable } from '@univerjs/core';import { DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render';-import type { IEditorBridgeServiceVisibleParam, IHoverRichTextInfo, IHoverRichTextPosition, IScrollState } from '@univerjs/sheets-ui';import { HoverManagerService, SetCellEditVisibleOperation, SheetScrollManagerService } from '@univerjs/sheets-ui';import { FWorkbook } from '@univerjs/sheets/facade';import { type IDialogPartMethodOptions, IDialogService, type ISidebarMethodOptions, ISidebarService, KeyCode } from '@univerjs/ui';@@ -60,6 +60,22 @@ export interface IFWorkbookSheetsUIMixin {*/onCellHover(callback: (cell: IHoverRichTextPosition) => void): IDisposable;+ /**+ * Subscribe to pointer move events on workbook. Just like onCellHover, but with event information.+ * @param {function(ICellPosWithEvent): any} callback The callback function accept cell location and event.+ */+ onPointerMove(callback: (cell: Nullable<ICellPosWithEvent>, buttons: number) => void): IDisposable;+ /**+ * Subscribe to cell pointer down events.+ * @param {function(ICellPosWithEvent): any} callback The callback function accept cell location and event.+ */+ onCellPointerDown(callback: (cell: Nullable<ICellPosWithEvent>) => void): IDisposable;+ /**+ * Subscribe to cell pointer up events.+ * @param {function(ICellPosWithEvent): any} callback The callback function accept cell location and event.+ */+ onCellPointerUp(callback: (cell: Nullable<ICellPosWithEvent>) => void): IDisposable;+/*** Start the editing process* @returns A boolean value@@ -121,6 +137,31 @@ export class FWorkbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheet);}+ override onCellPointerDown(callback: (cell: Nullable<ICellPosWithEvent>) => void): IDisposable {+ const hoverManagerService = this._injector.get(HoverManagerService);+ return toDisposable(+ hoverManagerService.currentPointerDownCell$.subscribe(callback)+ );+ }++ override onCellPointerUp(callback: (cell: Nullable<ICellPosWithEvent>) => void): IDisposable {+ const hoverManagerService = this._injector.get(HoverManagerService);+ return toDisposable(+ hoverManagerService.currentPointerUpCell$.subscribe(callback)+ );+ }++ override onPointerMove(callback: (cell: Nullable<ICellPosWithEvent>, buttons: number) => void): IDisposable {+ const hoverManagerService = this._injector.get(HoverManagerService);+ return toDisposable(+ hoverManagerService.currentCellPosWithEvent$+ .pipe(filter((cell) => !!cell))+ .subscribe((cell: ICellPosWithEvent) => {+ callback(cell, cell.event.buttons);+ })+ );+ }+override startEditing(): boolean {const commandService = this._injector.get(ICommandService);return commandService.syncExecuteCommand(SetCellEditVisibleOperation.id, {diff --git a/packages/sheets-ui/src/facade/f-worksheet.ts b/packages/sheets-ui/src/facade/f-worksheet.tsindex f7dc6d0d6f..eab7bceaf5 100644--- a/packages/sheets-ui/src/facade/f-worksheet.ts+++ b/packages/sheets-ui/src/facade/f-worksheet.ts@@ -16,10 +16,10 @@import type { IDisposable, IRange, Nullable } from '@univerjs/core';import type { RenderManagerService } from '@univerjs/engine-render';-import type { IScrollState, IViewportScrollState } from '@univerjs/sheets-ui';+import type { IScrollState, IViewportScrollState, SheetSelectionRenderService } from '@univerjs/sheets-ui';import { ICommandService, toDisposable } from '@univerjs/core';import { IRenderManagerService, SHEET_VIEWPORT_KEY, sheetContentViewportKeys } from '@univerjs/engine-render';-import { ChangeZoomRatioCommand, SheetScrollManagerService, SheetSkeletonManagerService, SheetsScrollRenderController } from '@univerjs/sheets-ui';+import { ChangeZoomRatioCommand, ISheetSelectionRenderService, SheetScrollManagerService, SheetSkeletonManagerService, SheetsScrollRenderController } from '@univerjs/sheets-ui';import { FWorksheet } from '@univerjs/sheets/facade';export interface IFWorksheetSkeletonMixin {@@ -175,6 +175,30 @@ export class FWorksheetSkeletonMixin extends FWorksheet implements IFWorksheetSk}return toDisposable(() => {});}++ /**+ * Hide selection box.+ */+ disableSelection(): void {+ const unitId = this._workbook.getUnitId();+ const renderManagerService = this._injector.get(IRenderManagerService) as RenderManagerService;+ const render = renderManagerService.getRenderById(unitId);+ if (render) {+ (render.with(ISheetSelectionRenderService) as SheetSelectionRenderService).disableSelection();+ }+ }++ /**+ * Show selection box.+ */+ enableSelection(): void {+ const unitId = this._workbook.getUnitId();+ const renderManagerService = this._injector.get(IRenderManagerService) as RenderManagerService;+ const render = renderManagerService.getRenderById(unitId);+ if (render) {+ (render.with(ISheetSelectionRenderService) as SheetSelectionRenderService).enableSelection();+ }+ }}FWorksheet.extend(FWorksheetSkeletonMixin);diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.tsindex 04bee121f7..166a460ef3 100644--- a/packages/sheets-ui/src/index.ts+++ b/packages/sheets-ui/src/index.ts@@ -78,7 +78,7 @@ export type { IEditorBridgeServiceParam } from './services/editor-bridge.serviceexport { CellEditorManagerService, ICellEditorManagerService } from './services/editor/cell-editor-manager.service';export { IFormulaEditorManagerService } from './services/editor/formula-editor-manager.service';export { HoverManagerService } from './services/hover-manager.service';-export type { IHoverCellPosition, IHoverRichTextInfo, IHoverRichTextPosition } from './services/hover-manager.service';+export type { ICellPosWithEvent, IHoverCellPosition, IHoverRichTextInfo, IHoverRichTextPosition } from './services/hover-manager.service';export { IMarkSelectionService, MarkSelectionService } from './services/mark-selection/mark-selection.service';export { SheetPermissionUserManagerService } from './services/permission/sheet-permission-user-list.service';export { SheetPrintInterceptorService } from './services/print-interceptor.service';diff --git a/packages/sheets-ui/src/services/hover-manager.service.ts b/packages/sheets-ui/src/services/hover-manager.service.tsindex 0caa9a3529..1aa3750b36 100644--- a/packages/sheets-ui/src/services/hover-manager.service.ts+++ b/packages/sheets-ui/src/services/hover-manager.service.ts@@ -15,7 +15,7 @@*/import type { ICustomRange, IParagraph, IPosition, Nullable, Workbook } from '@univerjs/core';-import type { IBoundRectNoAngle, SpreadsheetSkeleton } from '@univerjs/engine-render';+import type { IBoundRectNoAngle, IMouseEvent, IPointerEvent, SpreadsheetSkeleton } from '@univerjs/engine-render';import type { ISheetLocation, ISheetLocationBase } from '@univerjs/sheets';import { Disposable, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';import { IRenderManagerService } from '@univerjs/engine-render';@@ -33,6 +33,14 @@ export interface IHoverCellPosition {location: ISheetLocationBase;}+export interface ICellWithEvent extends IHoverCellPosition {+ event: IMouseEvent | IPointerEvent;+}++export interface ICellPosWithEvent extends ISheetLocationBase {+ event: IMouseEvent | IPointerEvent;+}+export interface IHoverRichTextInfo extends IHoverCellPosition {/*** active custom range in cell, if cell is rich-text@@ -65,6 +73,8 @@ export interface IHoverRichTextPosition extends ISheetLocationBase {rect?: Nullable<IBoundRectNoAngle>;drawing?: Nullable<string>;++ event?: IMouseEvent | IPointerEvent;}export function getLocationBase(location: ISheetLocation) {@@ -77,6 +87,10 @@ export class HoverManagerService extends Disposable {private _currentRichText$ = new BehaviorSubject<Nullable<IHoverRichTextInfo>>(null);private _currentClickedCell$ = new Subject<IHoverRichTextInfo>();+ private _currentCellWithEvent$ = new Subject<Nullable<ICellWithEvent>>();+ private _currentPointerDownCell$ = new Subject<ICellPosWithEvent>();+ private _currentPointerUpCell$ = new Subject<ICellPosWithEvent>();+// Notify when hovering over different cellscurrentCell$ = this._currentCell$.asObservable().pipe(distinctUntilChanged(@@ -117,9 +131,33 @@ export class HoverManagerService extends Disposable {} as IHoverRichTextPosition));+ /**+ * Nearly same as currentRichText$, but with event+ */+ currentCellPosWithEvent$ = this._currentCellWithEvent$.pipe(+ distinctUntilChanged(++ (pre, aft) => (+ pre?.location?.unitId === aft?.location?.unitId+ && pre?.location?.subUnitId === aft?.location?.subUnitId+ && pre?.location?.row === aft?.location?.row+ && pre?.location?.col === aft?.location?.col+ )+ ),+ map((cell) => cell && {+ unitId: cell.location.unitId,+ subUnitId: cell.location.subUnitId,+ row: cell.location.row,+ col: cell.location.col,+ event: cell.event,+ } as ICellPosWithEvent)+ );+// Notify when mouse position changescurrentPosition$ = this._currentCell$.asObservable();currentClickedCell$ = this._currentClickedCell$.asObservable();+ currentPointerDownCell$ = this._currentPointerDownCell$.asObservable();+ currentPointerUpCell$ = this._currentPointerUpCell$.asObservable();constructor(@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@@ -135,6 +173,9 @@ export class HoverManagerService extends Disposable {super.dispose();this._currentCell$.complete();this._currentClickedCell$.complete();+ this._currentPointerDownCell$.complete();+ this._currentPointerUpCell$.complete();+ this._currentCellWithEvent$.complete();}private _initCellDisposableListener(): void {@@ -229,8 +270,30 @@ export class HoverManagerService extends Disposable {};}- triggerMouseMove(unitId: string, offsetX: number, offsetY: number) {- const activeCell = this._calcActiveCell(unitId, offsetX, offsetY);+ triggerPointerDown(unitId: string, event: IPointerEvent | IMouseEvent) {+ const activeCell = this._calcActiveCell(unitId, event.offsetX, event.offsetY);+ if (activeCell && activeCell.location) {+ const { unitId, subUnitId, row, col } = getLocationBase(activeCell.location);+ this._currentPointerDownCell$.next({+ unitId, subUnitId, row, col,+ event,+ });+ }+ }++ triggerPointerUp(unitId: string, event: IPointerEvent | IMouseEvent) {+ const activeCell = this._calcActiveCell(unitId, event.offsetX, event.offsetY);+ if (activeCell) {+ const location = getLocationBase(activeCell.location);+ this._currentPointerUpCell$.next({+ ...location,+ event,+ });+ }+ }++ triggerMouseMove(unitId: string, event: IPointerEvent | IMouseEvent) {+ const activeCell = this._calcActiveCell(unitId, event.offsetX, event.offsetY);this._currentCell$.next(activeCell && {location: getLocationBase(activeCell.location),position: activeCell.position,@@ -240,6 +303,12 @@ export class HoverManagerService extends Disposable {...activeCell,location: getLocationBase(activeCell.overflowLocation),});++ this._currentCellWithEvent$.next(activeCell && {+ ...activeCell,+ location: getLocationBase(activeCell.location),+ event,+ });}triggerClick(unitId: string, offsetX: number, offsetY: number) {diff --git a/packages/sheets-ui/src/services/selection/selection-render.service.ts b/packages/sheets-ui/src/services/selection/selection-render.service.tsindex ce69b3d761..8408c1013c 100644--- a/packages/sheets-ui/src/services/selection/selection-render.service.ts+++ b/packages/sheets-ui/src/services/selection/selection-render.service.ts@@ -156,6 +156,14 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl}));}+ disableSelection() {+ this._contextService.setContextValue(DISABLE_NORMAL_SELECTIONS, true);+ }++ enableSelection() {+ this._contextService.setContextValue(DISABLE_NORMAL_SELECTIONS, false);+ }+/*** Handle events in spreadsheet. (e.g. drag and move to make a selection)*/diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.tsindex 642523d37d..4c145625ca 100644--- a/packages/sheets/src/facade/f-range.ts+++ b/packages/sheets/src/facade/f-range.ts@@ -223,7 +223,11 @@ export class FRange extends FBase {}// #region editing-+ /**+ * Set background color for current range.+ * e.g. `univerAPI.getActiveWorkbook().getActiveSheet().getActiveRange().setBackgroundColor('red')+ * @param color {string}+ */setBackgroundColor(color: string): Promise<boolean> {return this._commandService.executeCommand(SetStyleCommand.id, {unitId: this._workbook.getUnitId(),@@ -238,6 +242,15 @@ export class FRange extends FBase {} as ISetStyleCommandParams<IColorStyle>);}+ /**+ * Set background color for current range.+ * e.g. `univerAPI.getActiveWorkbook().getActiveSheet().getActiveRange().setBackground('red')`+ * @param color {string}+ */+ setBackground(color: string): Promise<boolean> {+ return this.setBackgroundColor(color);+ }+/*** The value can be a number, string, boolean, or standard cell format. If it begins with `=`, it is interpreted as a formula. The value is tiled to all cells in the range.* @param valuediff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.tsindex fde7ccad40..98d16fb3a9 100644--- a/packages/sheets/src/facade/f-worksheet.ts+++ b/packages/sheets/src/facade/f-worksheet.ts@@ -1598,6 +1598,21 @@ export class FWorksheet extends FBase {return this._worksheet.getLastColumnWithContent();}+ /**+ * Returns the position of the last column that has content. Same as getLastColumns.+ * @returns {number} the last column of the sheet that contains content.+ * @example+ * ```ts+ * const fWorkbook = univerAPI.getActiveWorkbook();+ * const fWorkSheet = fWorkbook.getActiveSheet();+ * const fRange = fWorkSheet.getRange(100, 20, 1, 1);+ * console.log(fWorkSheet.getLastColumn());+ * ```+ */+ getLastColumn(): number {+ return this._worksheet.getLastColumnWithContent();+ }+/*** Returns the position of the last row that has content.* @returns {number} the last row of the sheet that contains content.@@ -1613,6 +1628,21 @@ export class FWorksheet extends FBase {return this._worksheet.getLastRowWithContent();}+ /**+ * Returns the position of the last row that has content, same as getLastRows().+ * @returns {number} the last row of the sheet that contains content.+ * @example+ * ```ts+ * const fWorkbook = univerAPI.getActiveWorkbook();+ * const fWorkSheet = fWorkbook.getActiveSheet();+ * const fRange = fWorkSheet.getRange(100,1,1,1);+ * fRange.setValue('Hello World');+ * console.log(fWorkSheet.getLastRow());+ */+ getLastRow(): number {+ return this._worksheet.getLastRowWithContent();+ }+/*** Judge whether provided FWorksheet is equal to current.* @param {FWorksheet} other the FWorksheet to compare with.