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.

refactor: adjust scroll top when selection change (#1458)

Ran Luo77a9806cbf1f
diff --git a/packages/docs-ui/src/controllers/back-scroll.controller.ts b/packages/docs-ui/src/controllers/back-scroll.controller.ts
new file mode 100644
index 0000000000..389f64633e
--- /dev/null
+++ b/packages/docs-ui/src/controllers/back-scroll.controller.ts
@@ -0,0 +1,122 @@
+/**
+ * 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 { IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable } from '@univerjs/core';
+import { DocSkeletonManagerService, getDocObject, TextSelectionManagerService, VIEWPORT_KEY } from '@univerjs/docs';
+import { getAnchorBounding, IRenderManagerService, NodePositionConvertToCursor } from '@univerjs/engine-render';
+import { IEditorService } from '@univerjs/ui';
+import { Inject } from '@wendellhu/redi';
+import { takeUntil } from 'rxjs';
+
+const ANCHOR_WIDTH = 1.5;
+
+@OnLifecycle(LifecycleStages.Rendered, BackScrollController)
+export class BackScrollController extends RxDisposable {
+ constructor(
+ @Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,
+ @Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
+ @IEditorService private readonly _editorService: IEditorService,
+ @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,
+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService
+ ) {
+ super();
+
+ this._init();
+ }
+
+ private _init() {
+ this._textSelectionManagerService.textSelection$.pipe(takeUntil(this.dispose$)).subscribe((params) => {
+ if (params == null) {
+ return;
+ }
+
+ const { isEditing, unitId } = params;
+
+ if (isEditing) {
+ this._scrollToSelection(unitId);
+ }
+ });
+ }
+
+ // Let the selection show on the current screen.
+ private _scrollToSelection(unitId: string) {
+ const activeTextRange = this._textSelectionManagerService.getActiveRange();
+ const docObject = this._getDocObject();
+ const skeleton = this._docSkeletonManagerService.getCurrent()?.skeleton;
+
+ if (activeTextRange == null || docObject == null || skeleton == null) {
+ return;
+ }
+
+ const { collapsed, startNodePosition } = activeTextRange;
+
+ if (!collapsed) {
+ return;
+ }
+
+ const documentOffsetConfig = docObject.document.getOffsetConfig();
+ const { docsLeft, docsTop } = documentOffsetConfig;
+
+ const convertor = new NodePositionConvertToCursor(documentOffsetConfig, skeleton);
+
+ const { contentBoxPointGroup } = convertor.getRangePointData(startNodePosition, startNodePosition);
+
+ const { left: aLeft, top: aTop, height } = getAnchorBounding(contentBoxPointGroup);
+
+ const left = aLeft + docsLeft;
+
+ const top = aTop + docsTop;
+
+ const viewportMain = docObject.scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+
+ const isEditor = !!this._editorService.getEditor(unitId);
+
+ if (viewportMain == null) {
+ return;
+ }
+
+ const {
+ left: boundLeft,
+ top: boundTop,
+ right: boundRight,
+ bottom: boundBottom,
+ } = viewportMain.getBounding().viewBound;
+
+ let offsetY = 0;
+ let offsetX = 0;
+
+ const delta = isEditor ? 0 : 100;
+
+ if (top < boundTop) {
+ offsetY = top - boundTop;
+ } else if (top > boundBottom - height) {
+ offsetY = top - boundBottom + height + delta;
+ }
+
+ if (left < boundLeft) {
+ offsetX = left - boundLeft;
+ } else if (left > boundRight - ANCHOR_WIDTH) {
+ offsetX = left - boundRight + ANCHOR_WIDTH;
+ }
+
+ const config = viewportMain.getBarScroll(offsetX, offsetY);
+ viewportMain.scrollBy(config);
+ }
+
+ private _getDocObject() {
+ return getDocObject(this._currentUniverService, this._renderManagerService);
+ }
+}
diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.ts
index b0be093ca0..faca1acf2a 100644
--- a/packages/docs-ui/src/docs-ui-plugin.ts
+++ b/packages/docs-ui/src/docs-ui-plugin.ts
@@ -54,6 +54,7 @@ import { DocCanvasView } from './views/doc-canvas-view';
import { FloatingObjectController } from './controllers/floating-object.controller';
import { PageRenderController } from './controllers/page-render.controller';
import { ZoomController } from './controllers/zoom.controller';
+import { BackScrollController } from './controllers/back-scroll.controller';
export class UniverDocsUIPlugin extends Plugin {
static override type = PluginType.Doc;
@@ -111,6 +112,7 @@ export class UniverDocsUIPlugin extends Plugin {
[FloatingObjectController],
[PageRenderController],
[ZoomController],
+ [BackScrollController],
[
// controllers
AppUIController,
diff --git a/packages/engine-render/src/components/docs/common/text-range.ts b/packages/engine-render/src/components/docs/common/text-range.ts
index 2c8bb2ca86..8abaf51210 100644
--- a/packages/engine-render/src/components/docs/common/text-range.ts
+++ b/packages/engine-render/src/components/docs/common/text-range.ts
@@ -65,6 +65,23 @@ export function cursorConvertToTextRange(
return textRange;
}
+export function getAnchorBounding(pointsGroup: IPoint[][]) {
+ const points = pointsGroup[0];
+ const startPoint = points[0];
+ const endPoint = points[2];
+
+ const { x: startX, y: startY } = startPoint;
+
+ const { x: endX, y: endY } = endPoint;
+
+ return {
+ left: startX,
+ top: startY,
+ width: endX - startX,
+ height: endY - startY,
+ };
+}
+
export class TextRange {
// Identifies whether the range is the current one, most of which is the last range.
private _current = false;
@@ -327,25 +344,8 @@ export class TextRange {
this._scene.addObject(polygon, TEXT_RANGE_LAYER_INDEX);
}
- private _getAnchorBounding(pointsGroup: IPoint[][]) {
- const points = pointsGroup[0];
- const startPoint = points[0];
- const endPoint = points[2];
-
- const { x: startX, y: startY } = startPoint;
-
- const { x: endX, y: endY } = endPoint;
-
- return {
- left: startX,
- top: startY,
- width: endX - startX,
- height: endY - startY,
- };
- }
-
private _createOrUpdateAnchor(pointsGroup: IPoint[][], docsLeft: number, docsTop: number) {
- const bounding = this._getAnchorBounding(pointsGroup);
+ const bounding = getAnchorBounding(pointsGroup);
const { left, top, height } = bounding;
if (this._anchorShape) {
diff --git a/packages/engine-render/src/components/docs/text-selection-render-manager.ts b/packages/engine-render/src/components/docs/text-selection-render-manager.ts
index db57b4a33b..b0676888cb 100644
--- a/packages/engine-render/src/components/docs/text-selection-render-manager.ts
+++ b/packages/engine-render/src/components/docs/text-selection-render-manager.ts
@@ -299,10 +299,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
});
this._updateInputPosition();
-
- if (isEditing) {
- this._scrollToSelection();
- }
}
// Sync canvas selection to dom selection.
@@ -757,50 +753,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
return getCanvasOffsetByEngine(engine);
}
- // Let the selection show on the current screen.
- private _scrollToSelection() {
- const activeRangeInstance = this._getActiveRangeInstance();
- const anchor = activeRangeInstance?.getAnchor();
-
- if (!anchor || (anchor && !anchor.visible) || this._activeViewport == null) {
- return;
- }
-
- // const { scaleX, scaleY } = this._scene?.getAncestorScale() || { scaleX: 1, scaleY: 1 };
-
- const { left, top, height, width } = anchor;
-
- // left *= scaleX;
-
- // top *= scaleY;
-
- const {
- left: boundLeft,
- top: boundTop,
- right: boundRight,
- bottom: boundBottom,
- } = this._activeViewport.getBounding().viewBound;
- const constantOffsetWidth = width;
- const constantOffsetHeight = height;
- let offsetY = 0;
- let offsetX = 0;
-
- if (top < boundTop) {
- offsetY = top - boundTop;
- } else if (top > boundBottom - constantOffsetHeight) {
- offsetY = top - boundBottom + constantOffsetHeight;
- }
-
- if (left < boundLeft) {
- offsetX = left - boundLeft;
- } else if (left > boundRight - constantOffsetWidth) {
- offsetX = left - boundRight + constantOffsetWidth;
- }
-
- const config = this._activeViewport.getBarScroll(offsetX, offsetY);
- this._activeViewport.scrollBy(config);
- }
-
private _updateInputPosition() {
const activeRangeInstance = this._getActiveRangeInstance();
const anchor = activeRangeInstance?.getAnchor();