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
fix: pointer event (#4456)
lumix58d8551bd82e
diff --git a/packages/sheets-ui/src/facade/f-event.ts b/packages/sheets-ui/src/facade/f-event.tsindex 6e2e13cd20..4a3d66ef3c 100644--- a/packages/sheets-ui/src/facade/f-event.ts+++ b/packages/sheets-ui/src/facade/f-event.ts@@ -133,7 +133,17 @@ export interface IBeforeSheetEditEndEventParams extends IEventBase {isConfirm: boolean;}-interface IFSheetsUIEventNameMixin {+export const CellFEventName = {+ CellClicked: 'CellClicked',+ CellPointerDown: 'CellPointerDown',+ CellPointerUp: 'CellPointerUp',+ CellPointerMove: 'CellPointerMove',+ CellHover: 'CellHover',+ DragOver: 'DragOver',+ Drop: 'Drop',+} as const;++export interface IFSheetsUIEventNameMixin {/*** Trigger this event before the clipboard content changes.* Type of the event parameter is {@link IBeforeClipboardChangeParam}@@ -243,6 +253,66 @@ interface IFSheetsUIEventNameMixin {* ```*/readonly SheetEditEnded: 'SheetEditEnded';++ /**+ * Event fired when a cell is clicked+ * @example+ * ```ts+ * univerAPI.getActiveWorkbook().addEvent('CellClicked', (p)=> console.log(p));+ * ```+ */+ readonly CellClicked: 'CellClicked';+ /**+ * Event fired when a cell is pointer down+ * @example+ * ```ts+ * univerAPI.getActiveWorkbook().addEvent('CellPointerDown', (p)=> console.log(p));+ * ```+ */+ readonly CellPointerDown: 'CellPointerDown';++ /**+ * Event fired when a cell is pointer up+ * @example+ * ```ts+ * univerAPI.getActiveWorkbook().addEvent('CellPointerUp', (p)=> console.log(p));+ * ```+ */+ readonly CellPointerUp: 'CellPointerUp';++ /**+ * Event fired when a cell is hovered+ * @example+ * ```ts+ * univerAPI.getActiveWorkbook().addEvent('CellHover', (p)=> console.log(p));+ * ```+ */+ readonly CellHover: 'CellHover';+ /**+ * Event fired when move on spreadsheet cells+ * @example+ * ```ts+ * univerAPI.getActiveWorkbook().addEvent('CellPointerMove', (p)=> console.log(p));+ * ```+ */+ readonly CellPointerMove: 'CellPointerMove';+ /**+ * Event fired when drag over spreadsheet cells+ * @example+ * ```ts+ * univerAPI.getActiveWorkbook().addEvent('DragOver', (p)=> console.log(p));+ * ```+ */+ readonly DragOver: 'DragOver';++ /**+ * Event fired when drop on spreadsheet cells+ * @example+ * ```ts+ * univerAPI.getActiveWorkbook().addEvent('Drop', (p)=> console.log(p));+ * ```+ */+ readonly Drop: 'Drop';}export class FSheetsUIEventName extends FEventName implements IFSheetsUIEventNameMixin {@@ -281,8 +351,46 @@ export class FSheetsUIEventName extends FEventName implements IFSheetsUIEventNamoverride get SheetEditEnded(): 'SheetEditEnded' {return 'SheetEditEnded';}++ override get CellClicked(): 'CellClicked' {+ return CellFEventName.CellClicked;+ }++ override get CellHover(): 'CellHover' {+ return CellFEventName.CellHover;+ }++ override get CellPointerDown(): 'CellPointerDown' {+ return CellFEventName.CellPointerDown;+ }++ override get CellPointerUp(): 'CellPointerUp' {+ return CellFEventName.CellPointerUp;+ }++ override get CellPointerMove(): 'CellPointerMove' {+ return CellFEventName.CellPointerMove;+ }++ override get DragOver(): 'DragOver' {+ return 'DragOver' as const;+ }++ override get Drop(): 'Drop' {+ return 'Drop' as const;+ }}+export interface IUIEventBase extends IEventBase {+ /**+ * The workbook instance currently being operated on. {@link FWorkbook}+ */+ workbook: FWorkbook;+ /**+ * The worksheet instance currently being operated on. {@link FWorksheet}+ */+ worksheet: FWorksheet;+}export interface IBeforeClipboardChangeParam extends IEventBase {/*** The workbook instance currently being operated on. {@link FWorkbook}@@ -333,7 +441,11 @@ export interface IBeforeClipboardPasteParam extends IEventBase {export type IClipboardPastedParam = IBeforeClipboardPasteParam;-interface IFSheetsUIEventParamConfig {+export interface ICellEventParam extends IUIEventBase {+ row: number;+ column: number;+}+export interface IFSheetsUIEventParamConfig {BeforeClipboardChange: IBeforeClipboardChangeParam;ClipboardChanged: IClipboardChangedParam;BeforeClipboardPaste: IBeforeClipboardPasteParam;@@ -344,6 +456,14 @@ interface IFSheetsUIEventParamConfig {SheetEditChanging: ISheetEditChangingEventParams;BeforeSheetEditEnd: IBeforeSheetEditEndEventParams;SheetEditEnded: ISheetEditEndedEventParams;++ CellClicked: ICellEventParam;+ CellHover: ICellEventParam;+ CellPointerDown: ICellEventParam;+ CellPointerUp: ICellEventParam;+ CellPointerMove: ICellEventParam;+ Drop: ICellEventParam;+ DragOver: ICellEventParam;}FEventName.extend(FSheetsUIEventName);diff --git a/packages/sheets-ui/src/facade/f-workbook.ts b/packages/sheets-ui/src/facade/f-workbook.tsindex a992eedd5c..bcb4f0a7b2 100644--- a/packages/sheets-ui/src/facade/f-workbook.ts+++ b/packages/sheets-ui/src/facade/f-workbook.ts@@ -15,14 +15,15 @@*/import type { IDisposable, Nullable } from '@univerjs/core';-import type { RenderManagerService } from '@univerjs/engine-render';-import type { ICellPosWithEvent, IEditorBridgeServiceVisibleParam, IHoverRichTextInfo, IHoverRichTextPosition, IScrollState, SheetSelectionRenderService } from '@univerjs/sheets-ui';+import type { IMouseEvent, IPointerEvent, RenderManagerService } from '@univerjs/engine-render';+import type { ICellPosWithEvent, IDragCellPosition, IEditorBridgeServiceVisibleParam, IHoverRichTextInfo, IHoverRichTextPosition, IScrollState, SheetSelectionRenderService } from '@univerjs/sheets-ui';import { awaitTime, ICommandService, ILogService, toDisposable } from '@univerjs/core';import { DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render';-import { HoverManagerService, ISheetSelectionRenderService, SetCellEditVisibleOperation, SheetScrollManagerService } from '@univerjs/sheets-ui';+import { DragManagerService, HoverManagerService, ISheetSelectionRenderService, SetCellEditVisibleOperation, SheetScrollManagerService } from '@univerjs/sheets-ui';import { FWorkbook } from '@univerjs/sheets/facade';import { type IDialogPartMethodOptions, IDialogService, type ISidebarMethodOptions, ISidebarService, KeyCode } from '@univerjs/ui';import { filter } from 'rxjs';+import { CellFEventName, type ICellEventParam, type IFSheetsUIEventParamConfig, type IUIEventBase } from './f-event';export interface IFWorkbookSheetsUIMixin {/**@@ -59,17 +60,21 @@ export interface IFWorkbookSheetsUIMixin {* 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;+ onCellPointerMove(callback: (cell: ICellPosWithEvent, event: IPointerEvent | IMouseEvent) => 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;+ onCellPointerDown(callback: (cell: 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;+ onCellPointerUp(callback: (cell: ICellPosWithEvent) => void): IDisposable;++ onDragOver(callback: (cell: IDragCellPosition) => void): IDisposable;++ onDrop(callback: (cell: IDragCellPosition) => void): IDisposable;/*** Start the editing process@@ -177,12 +182,85 @@ export class FWorkbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheetlogService.warn('[FWorkbook]', `${name} is deprecated. Please use the function of the same name on "FUniver".`);}+ override addUIEvent(event: keyof IFSheetsUIEventParamConfig, _callback: (params: IFSheetsUIEventParamConfig[typeof event]) => void): IDisposable {+ const worksheet = this.getActiveSheet();+ const baseParams: IUIEventBase = {+ workbook: this,+ worksheet,+ };++ switch (event) {+ case CellFEventName.CellClicked:+ this.onCellClick((cell) => {+ this.fireEvent(this.Event.CellClicked, {+ row: cell.location.row,+ column: cell.location.col,+ ...baseParams,+ } as ICellEventParam);+ });+ break;+ case CellFEventName.CellPointerDown:+ this.onCellPointerDown((cell) => {+ this.fireEvent(this.Event.CellPointerDown, this.generateCellParams(cell));+ });+ break;+ case CellFEventName.CellPointerUp:+ this.onCellPointerUp((cell) => {+ this.fireEvent(this.Event.CellPointerUp, this.generateCellParams(cell));+ });+ break;+ case CellFEventName.CellPointerMove:+ this.onCellPointerMove((cell) => {+ this.fireEvent(this.Event.CellPointerMove, this.generateCellParams(cell));+ });+ break;+ case CellFEventName.CellHover:+ this.onCellHover((cell) => {+ this.fireEvent(this.Event.CellHover, this.generateCellParams(cell));+ });+ break;+ case CellFEventName.DragOver:+ this.onDragOver((cell) => {+ this.fireEvent(this.Event.DragOver, {+ row: cell.location.row,+ column: cell.location.col,+ ...baseParams,+ });+ });+ break;+ case CellFEventName.Drop:+ this.onDrop((cell) => {+ this.fireEvent(this.Event.Drop, {+ row: cell.location.row,+ column: cell.location.col,+ ...baseParams,+ });+ });+ }++ return toDisposable(() => {+ //+ });+ }++ generateCellParams(cell: IHoverRichTextPosition | ICellPosWithEvent): ICellEventParam {+ const worksheet = this.getActiveSheet();+ return {+ row: cell.row,+ column: cell.col,+ workbook: this,+ worksheet,+ };+ }+override onCellClick(callback: (cell: IHoverRichTextInfo) => void): IDisposable {const hoverManagerService = this._injector.get(HoverManagerService);return toDisposable(hoverManagerService.currentClickedCell$.pipe(filter((cell) => !!cell))- .subscribe(callback)+ .subscribe((cell) => {+ callback(cell);+ }));}@@ -195,27 +273,49 @@ export class FWorkbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheet);}- override onCellPointerDown(callback: (cell: Nullable<ICellPosWithEvent>) => void): IDisposable {+ override onCellPointerDown(callback: (cell: ICellPosWithEvent) => void): IDisposable {const hoverManagerService = this._injector.get(HoverManagerService);return toDisposable(hoverManagerService.currentPointerDownCell$.subscribe(callback));}- override onCellPointerUp(callback: (cell: Nullable<ICellPosWithEvent>) => void): IDisposable {+ override onCellPointerUp(callback: (cell: 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 {+ override onCellPointerMove(callback: (cell: ICellPosWithEvent, event: IPointerEvent | IMouseEvent) => void): IDisposable {const hoverManagerService = this._injector.get(HoverManagerService);return toDisposable(hoverManagerService.currentCellPosWithEvent$.pipe(filter((cell) => !!cell)).subscribe((cell: ICellPosWithEvent) => {- callback(cell, cell.event.buttons);+ callback(cell, cell.event);+ })+ );+ }++ override onDragOver(callback: (cell: IDragCellPosition) => void): IDisposable {+ const dragManagerService = this._injector.get(DragManagerService);+ return toDisposable(+ dragManagerService.currentCell$+ .pipe(filter((cell) => !!cell))+ .subscribe((cell: IDragCellPosition) => {+ callback(cell);+ })+ );+ }++ override onDrop(callback: (cell: IDragCellPosition) => void): IDisposable {+ const dragManagerService = this._injector.get(DragManagerService);+ return toDisposable(+ dragManagerService.endCell$+ .pipe(filter((cell) => !!cell))+ .subscribe((cell: IDragCellPosition) => {+ callback(cell);}));}diff --git a/packages/sheets-ui/src/services/hover-manager.service.ts b/packages/sheets-ui/src/services/hover-manager.service.tsindex 1aa3750b36..a62d081d50 100644--- a/packages/sheets-ui/src/services/hover-manager.service.ts+++ b/packages/sheets-ui/src/services/hover-manager.service.ts@@ -311,6 +311,12 @@ export class HoverManagerService extends Disposable {});}+ /**+ * Trigger by pointerup.+ * @param unitId+ * @param offsetX+ * @param offsetY+ */triggerClick(unitId: string, offsetX: number, offsetY: number) {const activeCell = this._calcActiveCell(unitId, offsetX, offsetY);if (activeCell) {diff --git a/packages/sheets/src/facade/f-workbook.ts b/packages/sheets/src/facade/f-workbook.tsindex 70ef6ab3bd..1cea7ceea1 100644--- a/packages/sheets/src/facade/f-workbook.ts+++ b/packages/sheets/src/facade/f-workbook.ts@@ -14,11 +14,11 @@* limitations under the License.*/-import type { CommandListener, CustomData, ICommandInfo, IDisposable, IRange, IWorkbookData, LocaleType, Workbook } from '@univerjs/core';+import type { CommandListener, CustomData, ICommandInfo, IDisposable, IEventParamConfig, IRange, IWorkbookData, LocaleType, Workbook } from '@univerjs/core';import type { ISetDefinedNameMutationParam } from '@univerjs/engine-formula';import type { ISetSelectionsOperationParams, ISheetCommandSharedParams, RangeThemeStyle } from '@univerjs/sheets';import type { FontLine as _FontLine } from './f-range';-import { FBaseInitialable, ICommandService, ILogService, Inject, Injector, IPermissionService, IResourceLoaderService, IUniverInstanceService, LocaleService, mergeWorksheetSnapshotWithDefault, RedoCommand, toDisposable, UndoCommand, UniverInstanceType } from '@univerjs/core';+import { FBaseInitialable, FEnum, FEventName, ICommandService, ILogService, Inject, Injector, IPermissionService, IResourceLoaderService, IUniverInstanceService, LocaleService, mergeWorksheetSnapshotWithDefault, RedoCommand, Registry, toDisposable, UndoCommand, UniverInstanceType } from '@univerjs/core';import { IDefinedNamesService } from '@univerjs/engine-formula';import { CopySheetCommand, getPrimaryForRange, InsertSheetCommand, RegisterWorksheetRangeThemeStyleCommand, RemoveSheetCommand, SCOPE_WORKBOOK_VALUE_DEFINED_NAME, SetDefinedNameCommand, SetSelectionsOperation, SetWorksheetActiveOperation, SetWorksheetOrderCommand, SheetRangeThemeService, SheetsSelectionsService, UnregisterWorksheetRangeThemeStyleCommand, WorkbookEditablePermission } from '@univerjs/sheets';import { FDefinedName, FDefinedNameBuilder } from './f-defined-name';@@ -50,6 +50,68 @@ export class FWorkbook extends FBaseInitialable {return this._workbook;}+ private _eventRegistry: Map<string, Registry<(param: any) => void>> = new Map();++ private _ensureEventRegistry(event: string): Registry<(param: any) => void> {+ if (!this._eventRegistry.has(event)) {+ this._eventRegistry.set(event, new Registry());+ }++ return this._eventRegistry.get(event)!;+ }++ /**+ * Add an event listener+ * @param event key of event+ * @param callback callback when event triggered+ * @returns {Disposable} The Disposable instance, for remove the listener+ * @example+ * ```ts+ * univerAPI.addEvent(univerAPI.event.UnitCreated, (params) => {+ * console.log('unit created', params);+ * });+ * ```+ */+ addEvent(event: keyof IEventParamConfig, callback: (params: IEventParamConfig[typeof event]) => void): IDisposable {+ this._ensureEventRegistry(event).add(callback);+ this.addUIEvent(event, callback);++ return toDisposable(() => this._ensureEventRegistry(event).delete(callback));+ }++ /**+ * Fire an event, used in internal only.+ * @param event {string} key of event+ * @param params {any} params of event+ * @returns {boolean} should cancel+ * @example+ * ```ts+ * this.fireEvent(univerAPI.event.UnitCreated, params);+ * ```+ */+ protected fireEvent<T extends keyof IEventParamConfig>(event: T, params: IEventParamConfig[T]): boolean | undefined {+ this._eventRegistry.get(event)?.getData().forEach((callback) => {+ callback(params);+ });++ return params.cancel;+ }++ addUIEvent(event: keyof IEventParamConfig, _callback: (params: IEventParamConfig[typeof event]) => void): void {+ //+ }++ get Enum(): FEnum {+ return FEnum.get();+ }++ /**+ * @returns {FEventName} The event name.+ */+ get Event(): FEventName {+ return FEventName.get();+ }+/*** Get the id of the workbook.* @returns {string} The id of the workbook.@@ -829,6 +891,7 @@ export class FWorkbook extends FBaseInitialable {/*** Set custom metadata of workbook* @param {CustomData | undefined} custom custom metadata+ * @returns {FWorkbook} FWorkbook* @example* ```ts* const fWorkbook = univerAPI.getActiveWorkbook();diff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.tsindex dfaa1b0435..142d79326b 100644--- a/packages/sheets/src/facade/f-worksheet.ts+++ b/packages/sheets/src/facade/f-worksheet.ts@@ -14,11 +14,11 @@* limitations under the License.*/-import type { CustomData, ICellData, IColumnData, IColumnRange, IDisposable, IFreeze, IObjectArrayPrimitiveType, IRange, IRowData, IRowRange, IStyleData, Nullable, Workbook, Worksheet } from '@univerjs/core';+import type { CustomData, ICellData, IColumnData, IColumnRange, IDisposable, IEventParamConfig, IFreeze, IObjectArrayPrimitiveType, IRange, IRowData, IRowRange, IStyleData, Nullable, Workbook, Worksheet } from '@univerjs/core';import type { ISetColDataCommandParams, ISetGridlinesColorCommandParams, ISetRangeValuesMutationParams, ISetRowDataCommandParams, IToggleGridlinesCommandParams } from '@univerjs/sheets';import type { FDefinedName } from './f-defined-name';import type { FWorkbook } from './f-workbook';-import { BooleanNumber, Direction, FBaseInitialable, ICommandService, ILogService, Inject, Injector, ObjectMatrix, RANGE_TYPE } from '@univerjs/core';+import { BooleanNumber, Direction, FBaseInitialable, ICommandService, ILogService, Inject, Injector, ObjectMatrix, RANGE_TYPE, Registry, toDisposable } from '@univerjs/core';import { deserializeRangeWithSheet } from '@univerjs/engine-formula';import { CancelFrozenCommand, ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, copyRangeStyles, InsertColByRangeCommand, InsertRowByRangeCommand, MoveColsCommand, MoveRowsCommand, RemoveColByRangeCommand, RemoveRowByRangeCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetGridlinesColorCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetTabColorCommand, SetWorksheetDefaultStyleMutation, SetWorksheetHideCommand, SetWorksheetNameCommand, SetWorksheetRowIsAutoHeightCommand, SetWorksheetShowCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets';import { FDefinedNameBuilder } from './f-defined-name';@@ -51,6 +51,51 @@ export class FWorksheet extends FBaseInitialable {return this._worksheet;}+ private _eventRegistry: Map<string, Registry<(param: any) => void>> = new Map();++ private _ensureEventRegistry(event: string): Registry<(param: any) => void> {+ if (!this._eventRegistry.has(event)) {+ this._eventRegistry.set(event, new Registry());+ }++ return this._eventRegistry.get(event)!;+ }++ /**+ * Add an event listener+ * @param event key of event+ * @param callback callback when event triggered+ * @returns {Disposable} The Disposable instance, for remove the listener+ * @example+ * ```ts+ * univerAPI.addEvent(univerAPI.event.UnitCreated, (params) => {+ * console.log('unit created', params);+ * });+ * ```+ */+ addEvent(event: keyof IEventParamConfig, callback: (params: IEventParamConfig[typeof event]) => void): IDisposable {+ this._ensureEventRegistry(event).add(callback);+ return toDisposable(() => this._ensureEventRegistry(event).delete(callback));+ }++ /**+ * Fire an event, used in internal only.+ * @param event {string} key of event+ * @param params {any} params of event+ * @returns {boolean} should cancel+ * @example+ * ```ts+ * this.fireEvent(univerAPI.event.UnitCreated, params);+ * ```+ */+ protected fireEvent<T extends keyof IEventParamConfig>(event: T, params: IEventParamConfig[T]): boolean | undefined {+ this._eventRegistry.get(event)?.getData().forEach((callback) => {+ callback(params);+ });++ return params.cancel;+ }+/*** Returns the injector* @returns The injector