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: selection render and insert char (#2982)

Ran Luo411d32820bdc
diff --git a/packages/docs/src/commands/commands/core-editing.command.ts b/packages/docs/src/commands/commands/core-editing.command.ts
index 6ee69d52ab..a2e71dd789 100644
--- a/packages/docs/src/commands/commands/core-editing.command.ts
+++ b/packages/docs/src/commands/commands/core-editing.command.ts
@@ -38,7 +38,6 @@ export interface IInsertCommandParams {
unitId: string;
body: IDocumentBody;
range: ITextRange;
- textRanges?: ITextRangeWithStyle[];
segmentId?: string;
cursorOffset?: number;
}
@@ -52,11 +51,10 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {
id: EditorInsertTextCommandId,
type: CommandType.COMMAND,
- // eslint-disable-next-line max-lines-per-function
handler: async (accessor, params: IInsertCommandParams) => {
const commandService = accessor.get(ICommandService);
- const { range, segmentId, body, unitId, textRanges: propTextRanges, cursorOffset } = params;
+ const { range, segmentId, body, unitId, cursorOffset } = params;
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const docDataModel = univerInstanceService.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);
@@ -88,7 +86,7 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {
params: {
unitId,
actions: [],
- textRanges: propTextRanges ?? textRanges,
+ textRanges,
debounce: true,
},
};
@@ -107,13 +105,11 @@ export const InsertCommand: ICommand<IInsertCommandParams> = {
} else {
const { dos, retain } = getRetainAndDeleteFromReplace(actualRange, segmentId, 0, originBody);
textX.push(...dos);
- if (!propTextRanges) {
- doMutation.params.textRanges = [{
- startOffset: startOffset + cursorMove + retain,
- endOffset: startOffset + cursorMove + retain,
- collapsed,
- }];
- }
+ doMutation.params.textRanges = [{
+ startOffset: startOffset + cursorMove + retain,
+ endOffset: startOffset + cursorMove + retain,
+ collapsed,
+ }];
}
textX.push({
diff --git a/packages/docs/src/controllers/normal-input.controller.ts b/packages/docs/src/controllers/normal-input.controller.ts
index 7e25101ac2..b35641c0c1 100644
--- a/packages/docs/src/controllers/normal-input.controller.ts
+++ b/packages/docs/src/controllers/normal-input.controller.ts
@@ -76,26 +76,13 @@ export class NormalInputController extends Disposable {
return;
}
- const { startOffset, segmentId, style, segmentPage } = activeRange;
-
- const len = content.length;
-
- const textRanges = [
- {
- startOffset: startOffset + len,
- endOffset: startOffset + len,
- segmentId,
- segmentPage,
- style,
- },
- ];
+ const { segmentId } = activeRange;
await this._commandService.executeCommand(InsertCommand.id, {
unitId,
body: {
dataStream: content,
},
- textRanges,
range: activeRange,
segmentId,
});
diff --git a/packages/engine-render/src/components/docs/layout/doc-skeleton.ts b/packages/engine-render/src/components/docs/layout/doc-skeleton.ts
index 0e3517a72f..c668224519 100644
--- a/packages/engine-render/src/components/docs/layout/doc-skeleton.ts
+++ b/packages/engine-render/src/components/docs/layout/doc-skeleton.ts
@@ -694,6 +694,16 @@ export class DocumentSkeleton extends Skeleton {
const { sections, skeTables } = segmentPage;
this._findLiquid.translateSave();
+ const pageLeft = this._findLiquid.x;
+ const pageRight = pageLeft + segmentPage.pageWidth;
+ const pageTop = this._findLiquid.y + (pageType === DocumentSkeletonPageType.FOOTER ? page.pageHeight - segmentPage.pageHeight : 0);
+ const pageBottom = pageTop + segmentPage.pageHeight;
+
+ const pointInPage = x >= pageLeft
+ && x <= pageRight
+ && y >= pageTop
+ && y <= pageBottom;
+
switch (pageType) {
case DocumentSkeletonPageType.HEADER: {
this._findLiquid.translatePagePadding({
@@ -715,11 +725,6 @@ export class DocumentSkeleton extends Skeleton {
}
}
- const pointInPage = x >= this._findLiquid.x
- && x <= this._findLiquid.x + segmentPage.pageWidth
- && y >= this._findLiquid.y
- && y <= this._findLiquid.y + segmentPage.pageHeight;
-
if (pointInPage) {
for (const section of sections) {
const { columns } = section;
diff --git a/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts b/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts
index 5c89db7521..a2253424c2 100644
--- a/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts
+++ b/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts
@@ -313,10 +313,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
position.isBack = true;
}
- // TODO: @Jocs It's better to create a new textRange after remove all text ranges? because segment id will change.
- this._updateTextRangeAnchorPosition(position);
-
- this._activeSelectionRefresh();
+ this._createTextRangeByAnchorPosition(position);
this._textSelectionInner$.next({
textRanges: this._getAllTextRanges(),
@@ -856,22 +853,16 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._rectRangeList.push(...rectRanges);
}
- private _updateTextRangeAnchorPosition(position: INodePosition) {
+ private _createTextRangeByAnchorPosition(position: INodePosition) {
if (!this._scene) {
return;
}
- let lastRange = this._rangeList.pop();
+ this._removeAllRanges();
- if (!lastRange) {
- lastRange = new TextRange(this._scene, this._document!, this._docSkeleton!, position, undefined, this._selectionStyle, this._currentSegmentId);
- }
+ const lastRange = new TextRange(this._scene, this._document!, this._docSkeleton!, position, undefined, this._selectionStyle, this._currentSegmentId);
- this._removeAllTextRanges();
- lastRange.activate();
- lastRange.anchorNodePosition = position;
- lastRange.focusNodePosition = null;
- this._rangeList = [lastRange];
+ this._addTextRange(lastRange);
}
private _updateActiveRangePosition(position: INodePosition) {
diff --git a/packages/slides-ui/src/controllers/shortcuts/editor.shortcuts.ts b/packages/slides-ui/src/controllers/shortcuts/editor.shortcuts.ts
index 4fc8c52e41..1e3ace106c 100644
--- a/packages/slides-ui/src/controllers/shortcuts/editor.shortcuts.ts
+++ b/packages/slides-ui/src/controllers/shortcuts/editor.shortcuts.ts
@@ -125,8 +125,9 @@ export function generateArrowSelectionShortCutItem() {
export const EditorDeleteLeftShortcut: IShortcutItem = {
id: DeleteLeftCommand.id,
- preconditions: (contextService) =>
- whenEditorActivated(contextService) || whenFormulaEditorFocused(contextService),
+ preconditions: (contextService) => {
+ return whenEditorActivated(contextService) || whenFormulaEditorFocused(contextService);
+ },
binding: KeyCode.BACKSPACE,
};