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.

fix(sheets-ui): lag when editing (#3837)

zhangw251186ec0a1e
diff --git a/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts b/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts
index 1915982485..090a8b8874 100644
--- a/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts
+++ b/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts
@@ -22,7 +22,7 @@ import { DocSelectionManagerService } from '@univerjs/docs';
import { DeleteLeftCommand, DocEventManagerService, IEditorService, InsertCommand, MoveCursorOperation } from '@univerjs/docs-ui';
import { IRenderManagerService } from '@univerjs/engine-render';
-import { filter, map, mergeMap } from 'rxjs';
+import { filter, map, switchMap } from 'rxjs';
import { AddDocUniFormulaCommand, RemoveDocUniFormulaCommand, UpdateDocUniFormulaCommand } from '../commands/commands/doc.command';
import { CloseFormulaPopupOperation, ShowFormulaPopupOperation } from '../commands/operations/operation';
import { UniFormulaPopupService } from '../services/formula-popup.service';
@@ -96,7 +96,7 @@ export class DocUniFormulaInputController extends Disposable {
map((focused) => focused ? this._instanceSrv.getUnit<DocumentDataModel>(focused, UniverInstanceType.UNIVER_DOC) : null),
map((doc) => doc && { doc, docEventManagerService: this._renderManagerService.getRenderById(doc!.getUnitId())?.with(DocEventManagerService) }),
filter((info) => !!info),
- mergeMap((info) => info.docEventManagerService!.hoverCustomRanges$.pipe(map((ranges) => ({ doc: info.doc, ranges }))))
+ switchMap((info) => info.docEventManagerService!.hoverCustomRanges$.pipe(map((ranges) => ({ doc: info.doc, ranges }))))
);
this.disposeWithMe(rangesWithDoc$.subscribe(({ doc, ranges: customRanges }) => {
diff --git a/packages/docs-ui/src/services/doc-event-manager.service.ts b/packages/docs-ui/src/services/doc-event-manager.service.ts
index 299a44314b..ae615912fd 100644
--- a/packages/docs-ui/src/services/doc-event-manager.service.ts
+++ b/packages/docs-ui/src/services/doc-event-manager.service.ts
@@ -14,12 +14,12 @@
* limitations under the License.
*/
+import type { DocumentDataModel, ICustomRange, IParagraph, ITextRangeParam, Nullable } from '@univerjs/core';
+import type { Documents, DocumentSkeleton, IBoundRectNoAngle, IDocumentSkeletonGlyph, IDocumentSkeletonPage, IMouseEvent, IPointerEvent, IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { Disposable, fromEventSubject, Inject } from '@univerjs/core';
import { DocSkeletonManagerService } from '@univerjs/docs';
import { CURSOR_TYPE, TRANSFORM_CHANGE_OBSERVABLE_TYPE } from '@univerjs/engine-render';
-import { BehaviorSubject, distinctUntilChanged, filter, map, mergeMap, Subject, take, throttleTime } from 'rxjs';
-import type { DocumentDataModel, ICustomRange, IParagraph, ITextRangeParam, Nullable } from '@univerjs/core';
-import type { Documents, DocumentSkeleton, IBoundRectNoAngle, IDocumentSkeletonGlyph, IDocumentSkeletonPage, IMouseEvent, IPointerEvent, IRenderContext, IRenderModule } from '@univerjs/engine-render';
+import { BehaviorSubject, distinctUntilChanged, filter, map, Subject, switchMap, take, throttleTime } from 'rxjs';
import { DOC_VERTICAL_PADDING } from '../types/const/padding';
import { transformOffset2Bound } from './doc-popup-manager.service';
import { NodePositionConvertToCursor } from './selection/convert-text-range';
@@ -199,7 +199,7 @@ export class DocEventManagerService extends Disposable implements IRenderModule
const onPointerDown$ = fromEventSubject(this._context.mainComponent!.onPointerDown$);
const onPointerUp$ = fromEventSubject(this._context.scene!.onPointerUp$);
this.disposeWithMe(onPointerDown$.pipe(
- mergeMap((down) => onPointerUp$.pipe(take(1), map((up) => ({ down, up })))),
+ switchMap((down) => onPointerUp$.pipe(take(1), map((up) => ({ down, up })))),
filter(({ down, up }) => down.target === up.target && up.timeStamp - down.timeStamp < 300)
// filter(({ down, up }) => down.offsetX === up.offsetX && down.offsetY === up.offsetY)
).subscribe(({ down }) => {
diff --git a/packages/sheets-hyper-link-ui/src/controllers/menu.ts b/packages/sheets-hyper-link-ui/src/controllers/menu.ts
index d138cb48bb..90b051935f 100644
--- a/packages/sheets-hyper-link-ui/src/controllers/menu.ts
+++ b/packages/sheets-hyper-link-ui/src/controllers/menu.ts
@@ -18,40 +18,39 @@ import type { IAccessor, Nullable, Workbook } from '@univerjs/core';
import type { IEditorBridgeServiceVisibleParam } from '@univerjs/sheets-ui';
import type { IMenuItem, IShortcutItem } from '@univerjs/ui';
import { DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_ZEN_EDITOR_UNIT_ID_KEY, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-import { DocSelectionManagerService } from '@univerjs/docs';
+import { DocSelectionRenderService } from '@univerjs/docs-ui';
+import { IRenderManagerService } from '@univerjs/engine-render';
import { getSheetCommandTarget, RangeProtectionPermissionEditPoint, SheetsSelectionsService, WorkbookEditablePermission, WorksheetEditPermission, WorksheetInsertHyperlinkPermission, WorksheetSetCellValuePermission } from '@univerjs/sheets';
import { getCurrentRangeDisable$, IEditorBridgeService, whenSheetEditorFocused } from '@univerjs/sheets-ui';
import { getMenuHiddenObservable, KeyCode, MenuGroup, MenuItemType, MenuPosition, MetaKeys } from '@univerjs/ui';
-import { map, mergeMap, Observable, of } from 'rxjs';
+import { distinctUntilChanged, filter, map, of, switchMap } from 'rxjs';
import { InsertHyperLinkOperation, InsertHyperLinkToolbarOperation } from '../commands/operations/popup.operations';
import { getShouldDisableCellLink, shouldDisableAddLink } from '../utils';
-const getZenLinkDisable$ = (accessor: IAccessor, unitId = DOCS_ZEN_EDITOR_UNIT_ID_KEY) => {
+const getEditingLinkDisable$ = (accessor: IAccessor, unitId = DOCS_ZEN_EDITOR_UNIT_ID_KEY) => {
const univerInstanceService = accessor.get(IUniverInstanceService);
- const textSelectionManagerService = accessor.get(DocSelectionManagerService);
-
- return textSelectionManagerService.textSelection$.pipe(
- map((selection) => {
- if (!selection || selection.unitId !== unitId) {
- return true;
- }
- const editorBridgeService = accessor.get(IEditorBridgeService);
- const state = editorBridgeService.getEditCellState();
- if (!state) {
- return true;
- }
- const target = getSheetCommandTarget(univerInstanceService, { unitId: state.unitId, subUnitId: state.sheetId });
- if (!target?.worksheet) {
- return true;
- }
-
- if (getShouldDisableCellLink(target.worksheet, state.row, state.column)) {
- return true;
- }
-
- return shouldDisableAddLink(accessor);
- })
- );
+ const docSelctionService = accessor.get(IRenderManagerService).getRenderById(unitId)?.with(DocSelectionRenderService);
+ if (!docSelctionService) {
+ return of(true);
+ }
+
+ return docSelctionService.textSelectionInner$.pipe(map(() => {
+ const editorBridgeService = accessor.get(IEditorBridgeService);
+ const state = editorBridgeService.getEditCellState();
+ if (!state) {
+ return true;
+ }
+ const target = getSheetCommandTarget(univerInstanceService, { unitId: state.unitId, subUnitId: state.sheetId });
+ if (!target?.worksheet) {
+ return true;
+ }
+
+ if (getShouldDisableCellLink(target.worksheet, state.row, state.column)) {
+ return true;
+ }
+
+ return shouldDisableAddLink(accessor);
+ }));
};
const getLinkDisable$ = (accessor: IAccessor) => {
@@ -61,22 +60,11 @@ const getLinkDisable$ = (accessor: IAccessor) => {
const editorBridgeService = accessor.has(IEditorBridgeService) ? accessor.get(IEditorBridgeService) : null;
const disableCell$ = univerInstanceService.focused$.pipe(
- map((focused) => {
- if (!focused) {
- return null;
- }
- const unit = univerInstanceService.getUnit<Workbook>(focused, UniverInstanceType.UNIVER_SHEET);
- return unit;
- }),
- mergeMap((unit) => {
- if (!unit) {
- return new Observable<null>((sub) => {
- sub.next(null);
- });
- }
- return unit.activeSheet$;
- }),
- mergeMap((sheet) => sheetSelectionService.selectionMoveEnd$.pipe(map((selections) => sheet && { selections, sheet }))),
+ filter((focused) => Boolean(focused)),
+ map((focused) => univerInstanceService.getUnit<Workbook>(focused!, UniverInstanceType.UNIVER_SHEET)),
+ filter((unit) => Boolean(unit)),
+ switchMap((unit) => unit!.activeSheet$),
+ switchMap((sheet) => sheetSelectionService.selectionMoveEnd$.pipe(map((selections) => sheet && { selections, sheet }))),
map((sheetWithSelection) => {
if (!sheetWithSelection) {
return true;
@@ -87,26 +75,33 @@ const getLinkDisable$ = (accessor: IAccessor) => {
}
const row = selections[0].range.startRow;
const col = selections[0].range.startColumn;
- return {
- sheet,
- row,
- col,
- };
+
+ if (getShouldDisableCellLink(sheet, row, col)) {
+ return true;
+ }
}),
- mergeMap((editingCell) => {
- if (editingCell === true) {
+ switchMap((disableCell) => {
+ if (disableCell) {
return of(true);
}
- const { sheet, row, col } = editingCell;
const isEditing$ = (editorBridgeService ? editorBridgeService.visible$ : of<Nullable<IEditorBridgeServiceVisibleParam>>(null))
.pipe(map((visible) => visible?.visible ? DOCS_NORMAL_EDITOR_UNIT_ID_KEY : undefined));
- return editorBridgeService ? isEditing$.pipe(mergeMap((editing) => (editing ? getZenLinkDisable$(accessor, editing) : of(getShouldDisableCellLink(sheet, row, col))))) : of(getShouldDisableCellLink(sheet, row, col));
+ return isEditing$.pipe(
+ switchMap(
+ (editing) => editing ?
+ getEditingLinkDisable$(accessor, editing)
+ : of(false)
+ )
+ );
})
);
- return disableRange$.pipe(mergeMap(((disableRange) => disableCell$.pipe(map((disableCell) => disableRange || disableCell)))));
+ return disableRange$.pipe(
+ distinctUntilChanged(),
+ switchMap(((disableRange) => disableCell$.pipe(map((disableCell) => disableRange || disableCell))))
+ );
};
const linkMenu = {
@@ -134,7 +129,7 @@ export const zenEditorInsertLinkMenuFactory = (accessor: IAccessor) => {
...linkMenu,
id: genZenEditorMenuId(linkMenu.commandId),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC, DOCS_ZEN_EDITOR_UNIT_ID_KEY),
- disabled$: getZenLinkDisable$(accessor),
+ disabled$: getEditingLinkDisable$(accessor),
} as IMenuItem;
};
@@ -161,7 +156,7 @@ export const zenEditorInsertLinkMenuToolbarFactory = (accessor: IAccessor) => {
...linkToolbarMenu,
id: genZenEditorMenuId(linkToolbarMenu.commandId),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC, DOCS_ZEN_EDITOR_UNIT_ID_KEY),
- disabled$: getZenLinkDisable$(accessor),
+ disabled$: getEditingLinkDisable$(accessor),
};
};
diff --git a/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts b/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts
index 510edba291..110b69d316 100644
--- a/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts
+++ b/packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts
@@ -37,7 +37,7 @@ import {
} from '@univerjs/sheets';
import { HoverManagerService, HoverRenderController, IEditorBridgeService, SheetPermissionInterceptorBaseController, SheetSkeletonManagerService } from '@univerjs/sheets-ui';
import { IZenZoneService } from '@univerjs/ui';
-import { debounceTime, map, mergeMap, Observable } from 'rxjs';
+import { debounceTime, map, Observable, switchMap } from 'rxjs';
import { SheetsHyperLinkPopupService } from '../services/popup.service';
import { HyperLinkEditSourceType } from '../types/enums/edit-source';
@@ -160,7 +160,7 @@ export class SheetsHyperLinkPopupController extends Disposable {
let subscribe: Nullable<Subscription> = null;
this.disposeWithMe(
this._editorBridgeService.currentEditCellState$
- .pipe(mergeMap((state) => this._editorBridgeService.visible$.pipe(map((visible) => ({ visible, state })))))
+ .pipe(switchMap((state) => this._editorBridgeService.visible$.pipe(map((visible) => ({ visible, state })))))
.subscribe(({ visible, state }) => {
if (!state) {
return;
@@ -245,7 +245,7 @@ export class SheetsHyperLinkPopupController extends Disposable {
this.disposeWithMe(
this._univerInstanceService.focused$.pipe(
- mergeMap((id) => {
+ switchMap((id) => {
const render = id === DOCS_ZEN_EDITOR_UNIT_ID_KEY ? this._renderManagerService.getRenderById(id) : null;
if (render) {
return render.with(DocEventManagerService).hoverCustomRanges$.pipe(debounceTime(200));
diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts
index a1057b2efa..4da73dc8d4 100644
--- a/packages/sheets-ui/src/services/editor-bridge.service.ts
+++ b/packages/sheets-ui/src/services/editor-bridge.service.ts
@@ -41,7 +41,7 @@ import {
import { getCanvasOffsetByEngine, IEditorService } from '@univerjs/docs-ui';
import { convertTextRotation, DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render';
import { IRefSelectionsService } from '@univerjs/sheets';
-import { BehaviorSubject, map, mergeMap } from 'rxjs';
+import { BehaviorSubject, map, switchMap } from 'rxjs';
import { ISheetSelectionRenderService } from './selection/base-selection-render.service';
import { attachPrimaryWithCoord } from './selection/util';
import { SheetSkeletonManagerService } from './sheet-skeleton-manager.service';
@@ -148,7 +148,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
readonly currentEditCellLayout$ = this._currentEditCellLayout$.asObservable();
readonly currentEditCell$ = this._currentEditCellState$.pipe(
- mergeMap((editCellState) => this._currentEditCellLayout$.pipe(map((layout) => (editCellState && layout ? { ...editCellState, ...layout } : null))))
+ switchMap((editCellState) => this._currentEditCellLayout$.pipe(map((layout) => (editCellState && layout ? { ...editCellState, ...layout } : null))))
);
private readonly _visible$ = new BehaviorSubject<IEditorBridgeServiceVisibleParam>(this._visible);