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(doc): use shift + click to select text (#1217)

Ran Luoadeb8da0cac2
diff --git a/examples/src/docs/main.ts b/examples/src/docs/main.ts
index 1b51a2203e..359c134483 100644
--- a/examples/src/docs/main.ts
+++ b/examples/src/docs/main.ts
@@ -21,7 +21,7 @@ import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { UniverUIPlugin } from '@univerjs/ui';
-import { DEFAULT_DOCUMENT_DATA_SIMPLE } from '../data';
+import { DEFAULT_DOCUMENT_DATA_CN } from '../data';
import { DebuggerPlugin } from '../plugins/debugger';
import { locales } from './locales';
@@ -61,7 +61,7 @@ univer.registerPlugin(UniverDocsUIPlugin, {
},
});
-univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_SIMPLE);
+univer.createUniverDoc(DEFAULT_DOCUMENT_DATA_CN);
// use for console test
declare global {
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 77d179f63f..f4693f9d9f 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
@@ -15,7 +15,7 @@
*/
import type { Nullable, Observer } from '@univerjs/core';
-import { DataStreamTreeTokenType, RxDisposable } from '@univerjs/core';
+import { DataStreamTreeTokenType, ILogService, RxDisposable } from '@univerjs/core';
import { createIdentifier } from '@wendellhu/redi';
import type { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs';
@@ -258,7 +258,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
private _document: Nullable<Documents>;
- constructor() {
+ constructor(@ILogService private readonly _logService: ILogService) {
super();
this._initDOM();
@@ -472,6 +472,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
if (position == null) {
this._removeAllTextRanges();
+
return;
}
@@ -479,12 +480,14 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
position.isBack = true;
}
- if (evt.ctrlKey || this._isEmpty()) {
+ if (evt.shiftKey && this._getActiveRangeInstance()) {
+ this._updateActiveRangeFocusPosition(position);
+ } else if (evt.ctrlKey || this._isEmpty()) {
const newTextSelection = new TextRange(scene, this._document!, this._docSkeleton!, position);
this._addTextRange(newTextSelection);
} else {
- this._updateTextRangePosition(position);
+ this._updateTextRangeAnchorPosition(position);
}
this._activeSelectionRefresh();
@@ -786,7 +789,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._rangeList.push(textRange);
}
- private _updateTextRangePosition(position: INodePosition) {
+ private _updateTextRangeAnchorPosition(position: INodePosition) {
if (!this._scene) {
return;
}
@@ -804,6 +807,31 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._rangeList = [lastRange];
}
+ private _updateActiveRangeFocusPosition(position: INodePosition) {
+ if (!this._scene) {
+ this._logService.error('[TextSelectionRenderManager] _updateActiveRangeFocusPosition: scene is null');
+
+ return;
+ }
+
+ const activeTextRange = this._getActiveRangeInstance();
+
+ if (activeTextRange == null || activeTextRange.anchorNodePosition == null) {
+ this._logService.error(
+ '[TextSelectionRenderManager] _updateActiveRangeFocusPosition: active range has no anchor'
+ );
+
+ return;
+ }
+
+ this._removeAllTextRanges();
+ activeTextRange.activate();
+ activeTextRange.focusNodePosition = position;
+
+ this.deactivate();
+ this._rangeList = [activeTextRange];
+ }
+
private _isEmpty() {
return this._rangeList.length === 0;
}