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: fix comment editor behavior (#6251)

Shiyun Hua098dbf6c7a5d
diff --git a/packages/core/src/common/const.ts b/packages/core/src/common/const.ts
index 7649c4193e..42b37d2460 100644
--- a/packages/core/src/common/const.ts
+++ b/packages/core/src/common/const.ts
@@ -22,6 +22,8 @@ export const DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY = `${PREFIX}DOCS_FORMULA_BAR`;
export const DOCS_ZEN_EDITOR_UNIT_ID_KEY = `${PREFIX}ZEN_EDITOR`;
+export const DOCS_COMMENT_EDITOR_UNIT_ID_KEY = `${PREFIX}COMMENT_EDITOR`;
+
export const DEFAULT_EMPTY_DOCUMENT_VALUE = '\r\n';
export const IS_ROW_STYLE_PRECEDE_COLUMN_STYLE = 'isRowStylePrecedeColumnStyle';
@@ -35,3 +37,7 @@ export function createInternalEditorID(id: string) {
export function isInternalEditorID(id: string) {
return id.startsWith(PREFIX);
}
+
+export function isCommentEditorID(id: string) {
+ return id === DOCS_COMMENT_EDITOR_UNIT_ID_KEY;
+}
diff --git a/packages/core/src/services/context/context.ts b/packages/core/src/services/context/context.ts
index c0fb2e1778..fa70029f19 100644
--- a/packages/core/src/services/context/context.ts
+++ b/packages/core/src/services/context/context.ts
@@ -42,6 +42,9 @@ export const FOCUSING_UNIVER_EDITOR = 'FOCUSING_UNIVER_EDITOR';
export const FOCUSING_EDITOR_STANDALONE = 'FOCUSING_EDITOR_INPUT_FORMULA';
+/** The focusing state of the comment editor. */
+export const FOCUSING_COMMENT_EDITOR = 'FOCUSING_COMMENT_EDITOR';
+
/** The focusing state of the editor in side panel, such as Chart Editor Panel. */
export const FOCUSING_PANEL_EDITOR = 'FOCUSING_PANEL_EDITOR';
diff --git a/packages/docs-ui/src/services/editor/editor-manager.service.ts b/packages/docs-ui/src/services/editor/editor-manager.service.ts
index a2867a81a1..284751d39c 100644
--- a/packages/docs-ui/src/services/editor/editor-manager.service.ts
+++ b/packages/docs-ui/src/services/editor/editor-manager.service.ts
@@ -18,7 +18,7 @@ import type { DocumentDataModel, IDisposable, IDocumentBody, IDocumentData, Null
import type { ISuccinctDocRangeParam, Scene } from '@univerjs/engine-render';
import type { Observable } from 'rxjs';
import type { IEditorConfigParams } from './editor';
-import { createIdentifier, DEFAULT_EMPTY_DOCUMENT_VALUE, Disposable, EDITOR_ACTIVATED, FOCUSING_EDITOR_STANDALONE, HorizontalAlign, ICommandService, IContextService, Inject, Injector, isInternalEditorID, IUndoRedoService, IUniverInstanceService, toDisposable, UniverInstanceType, VerticalAlign } from '@univerjs/core';
+import { createIdentifier, DEFAULT_EMPTY_DOCUMENT_VALUE, Disposable, EDITOR_ACTIVATED, FOCUSING_COMMENT_EDITOR, FOCUSING_EDITOR_STANDALONE, HorizontalAlign, ICommandService, IContextService, Inject, Injector, isCommentEditorID, isInternalEditorID, IUndoRedoService, IUniverInstanceService, toDisposable, UniverInstanceType, VerticalAlign } from '@univerjs/core';
import { DocSelectionManagerService } from '@univerjs/docs';
import { IRenderManagerService } from '@univerjs/engine-render';
import { fromEvent, Subject } from 'rxjs';
@@ -145,6 +145,7 @@ export class EditorService extends Disposable implements IEditorService, IDispos
this._contextService.setContextValue(EDITOR_ACTIVATED, false);
this._contextService.setContextValue(FOCUSING_EDITOR_STANDALONE, false);
+ this._contextService.setContextValue(FOCUSING_COMMENT_EDITOR, false);
this._setFocusId(null);
this._blur$.next(null);
}
@@ -173,6 +174,10 @@ export class EditorService extends Disposable implements IEditorService, IDispos
this._contextService.setContextValue(FOCUSING_EDITOR_STANDALONE, true);
}
+ if (isCommentEditorID(editorUnitId)) {
+ this._contextService.setContextValue(FOCUSING_COMMENT_EDITOR, true);
+ }
+
editor.focus();
this._setFocusId(editorUnitId);
diff --git a/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts b/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts
index 1b3a839443..1b4b42f435 100644
--- a/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts
+++ b/packages/docs-ui/src/views/rich-text-editor/hooks/use-editor.ts
@@ -72,7 +72,7 @@ export function useEditor(opts: IUseEditorProps) {
setEditor(editor);
if (autoFocus) {
- editor.focus();
+ editorService.focus(editorId);
const end = (snapshot.body?.dataStream.length ?? 2) - 2;
editor.setSelectionRanges([{ startOffset: end, endOffset: end }]);
}
diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts
index 88b97d186c..6c6debaa90 100644
--- a/packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-check-ui.controller.ts
@@ -22,7 +22,7 @@ import type { IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandP
import type { IAutoFillCommandParams } from '../../commands/commands/auto-fill.command';
import type { ISheetPasteParams } from '../../commands/commands/clipboard.command';
import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
-import { Disposable, DisposableCollection, FOCUSING_EDITOR_STANDALONE, ICommandService, IContextService, Inject, IPermissionService, IUniverInstanceService, LocaleService, Rectangle, UniverInstanceType } from '@univerjs/core';
+import { Disposable, DisposableCollection, FOCUSING_COMMENT_EDITOR, FOCUSING_EDITOR_STANDALONE, ICommandService, IContextService, Inject, IPermissionService, IUniverInstanceService, LocaleService, Rectangle, UniverInstanceType } from '@univerjs/core';
import { IMEInputCommand, InsertCommand } from '@univerjs/docs-ui';
import { UnitAction } from '@univerjs/protocol';
import { getSheetCommandTarget, RangeProtectionPermissionEditPoint, RangeProtectionPermissionViewPoint, RangeProtectionRuleModel, SetBackgroundColorCommand, SheetPermissionCheckController, WorkbookCopyPermission, WorkbookEditablePermission, WorksheetCopyPermission, WorksheetEditPermission, WorksheetSetCellStylePermission, WorksheetSetCellValuePermission, WorksheetSetColumnStylePermission } from '@univerjs/sheets';
@@ -99,7 +99,7 @@ export class SheetPermissionCheckUIController extends Disposable {
switch (id) {
case InsertCommand.id:
case IMEInputCommand.id:
- if (this._contextService.getContextValue(FOCUSING_EDITOR_STANDALONE) === true) {
+ if (this._contextService.getContextValue(FOCUSING_EDITOR_STANDALONE) === true || this._contextService.getContextValue(FOCUSING_COMMENT_EDITOR) === true) {
break;
}
permission = this._sheetPermissionCheckController.permissionCheckWithoutRange({
diff --git a/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx b/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx
index 90bb24492a..aafa72910a 100644
--- a/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx
+++ b/packages/thread-comment-ui/src/views/thread-comment-editor/index.tsx
@@ -17,7 +17,7 @@
import type { IDocumentBody, IDocumentData, IUser } from '@univerjs/core';
import type { Editor, IKeyboardEventConfig } from '@univerjs/docs-ui';
import type { IThreadComment } from '@univerjs/thread-comment';
-import { BuildTextUtils, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, ICommandService, LocaleService, Tools, UniverInstanceType } from '@univerjs/core';
+import { BuildTextUtils, DOCS_COMMENT_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, ICommandService, LocaleService, Tools, UniverInstanceType } from '@univerjs/core';
import { Button, clsx } from '@univerjs/design';
import { BreakLineCommand, IEditorService, RichTextEditor } from '@univerjs/docs-ui';
import { KeyCode, useDependency } from '@univerjs/ui';
@@ -115,6 +115,7 @@ export const ThreadCommentEditor = forwardRef<IThreadCommentEditorInstance, IThr
<RichTextEditor
className="univer-w-full"
editorRef={editor}
+ editorId={DOCS_COMMENT_EDITOR_UNIT_ID_KEY}
autoFocus={autoFocus}
keyboardEventConfig={keyboardEventConfig}
placeholder={localeService.t('threadCommentUI.editor.placeholder')}