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(editor): paste and selection error (#2843)

Univere6869e49fd12
diff --git a/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts b/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
index cd808c62d3..f547fc761d 100644
--- a/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
+++ b/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
@@ -137,7 +137,7 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
this._calculatePagePosition(docObject, zoomRatio);
- if (needRefreshSelection) {
+ if (needRefreshSelection && !this._editorService.isEditor(this._context.unitId)) {
this._textSelectionManagerService.refreshSelection();
}
diff --git a/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts
index e9cbeafb9e..cbc2ec9bba 100644
--- a/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts
+++ b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts
@@ -89,7 +89,7 @@ export class SheetsThreadCommentController extends Disposable {
}
const params = commandInfo.params as ISetSelectionsOperationParams;
const { unitId, subUnitId, selections, type } = params;
- if ((type === SelectionMoveType.MOVE_END || type === undefined) && selections[0].primary) {
+ if ((type === SelectionMoveType.MOVE_END || type === undefined) && selections[0]?.primary) {
const range = selections[0].range;
if (range.endColumn - range.startColumn > 0 || range.endRow - range.startRow > 0) {
return;
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index bbacddd538..7b4406ae59 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -471,15 +471,16 @@ export class EditingRenderController extends Disposable implements IRenderModule
documentComponent.resize(editorWidth / scaleX, editorHeight / scaleY);
/**
+ * sometimes requestIdleCallback is invalid, so use setTimeout to ensure the successful execution of the resizeBySize method.
* resize canvas
* When modifying the selection area for a formula, it is necessary to add a setTimeout to ensure successful updating.
*/
- requestIdleCallback(() => {
+ setTimeout(() => {
docEngine.resizeBySize(
fixLineWidthByScale(editorWidth, precisionScaleX),
fixLineWidthByScale(physicHeight, precisionScaleY)
);
- });
+ }, 0);
// const canvasElement = this._context.engine.getCanvasElement();
// const canvasBoundingRect = canvasElement.getBoundingClientRect();
diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts
index 771a97087a..81bc62103c 100644
--- a/packages/sheets-ui/src/services/editor-bridge.service.ts
+++ b/packages/sheets-ui/src/services/editor-bridge.service.ts
@@ -21,6 +21,7 @@ import {
createInterceptorKey,
Disposable,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
+ EDITOR_ACTIVATED,
FOCUSING_EDITOR_STANDALONE,
FOCUSING_UNIVER_EDITOR_STANDALONE_SINGLE_MODE,
IContextService,
@@ -179,6 +180,10 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
*/
if (!this._editorService.getFocusEditor()) {
this._editorService.focus(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
+ /**
+ * Fix: When the sheet loads for the first time, copying and pasting triggers the editor, and the edits are ineffective.
+ */
+ this._contextService.setContextValue(EDITOR_ACTIVATED, false);
this._contextService.setContextValue(FOCUSING_EDITOR_STANDALONE, false);
this._contextService.setContextValue(FOCUSING_UNIVER_EDITOR_STANDALONE_SINGLE_MODE, false);
}
diff --git a/packages/ui/src/services/layout/layout.service.ts b/packages/ui/src/services/layout/layout.service.ts
index 8bd6c6c03b..167942fd7a 100644
--- a/packages/ui/src/services/layout/layout.service.ts
+++ b/packages/ui/src/services/layout/layout.service.ts
@@ -29,6 +29,18 @@ const givingBackFocusElements = [
'univer-button',
'univer-sheet-bar-btn',
'univer-render-canvas',
+ 'univer-workbench-layout',
+];
+
+/**
+ * Not these elements will be considered as editor blur.
+ */
+const editorFocusInElements = [
+ 'univer-editor',
+ 'univer-range-selector',
+ 'univer-range-selector-editor',
+ 'univer-render-canvas',
+ 'univer-text-editor-container-placeholder',
];
export interface ILayoutService {
@@ -164,8 +176,10 @@ export class DesktopLayoutService extends Disposable implements ILayoutService {
this.disposeWithMe(
fromEvent(window, 'focusin').subscribe((event) => {
const target = event.target as HTMLElement;
+
+ this._blurSheetEditor(target);
+
if (givingBackFocusElements.some((item) => target.classList.contains(item))) {
- this._blurSheetEditor();
queueMicrotask(() => this.focus());
return;
}
@@ -186,7 +200,11 @@ export class DesktopLayoutService extends Disposable implements ILayoutService {
this._contextService.setContextValue(FOCUSING_UNIVER_EDITOR, getFocusingUniverEditorStatus());
}
- private _blurSheetEditor() {
+ private _blurSheetEditor(target: HTMLElement) {
+ if (editorFocusInElements.some((item) => target.classList.contains(item))) {
+ return;
+ }
+
// NOTE: Note that the focus editor will not be docs' editor but calling `this._editorService.blur()` will blur doc's editor.
const focusEditor = this._editorService.getFocusEditor();
if (focusEditor && focusEditor.isSheetEditor() !== true) {