Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: quickopen auto focus previous active element #4272

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hacke2
Copy link
Member

@hacke2 hacke2 commented Dec 25, 2024

Types

  • 🐛 Bug Fixes

Background or solution

fixed #4271

Changelog

fix: quickopen auto focus previous active element

Summary by CodeRabbit

  • 新功能

    • 修改了快速打开界面的关闭行为,取消了自动聚焦编辑器的功能。
    • 更新了快速打开服务中活动元素的处理方式,允许其显式为null。
    • 在处理程序关闭时,增强了与编辑器聚焦的交互流程。
  • Bug 修复

    • 移除了多个类中的关闭方法,简化了关闭命令面板时的控制流。

@hacke2 hacke2 requested a review from life2015 December 25, 2024 07:01
@opensumi opensumi bot added the 🐞 bug Something isn't working label Dec 25, 2024
Copy link
Contributor

coderabbitai bot commented Dec 25, 2024

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

warning [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/[email protected]: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/[email protected]: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > [email protected]: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > [email protected]: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"

变更概述

演练

此次变更主要涉及快速打开(Quick Open)服务中的焦点管理机制。在多个文件中,移除了关闭快速打开界面时自动聚焦编辑器的代码行。同时,在 quick-open.service.tsx 中,对 previousActiveElement 属性的类型进行了调整,并在 open 方法中添加了捕获当前活动元素的逻辑。

变更

文件路径 变更摘要
packages/addons/src/browser/file-search.contribution.ts 移除 onClose 方法中自动聚焦编辑器的命令执行
packages/quick-open/src/browser/quick-open.command.service.ts 删除 QuickCommandHandler 类中的 onClose 方法
packages/quick-open/src/browser/quick-open.help.service.ts 删除 HelpQuickOpenHandler 类中的 onClose 方法
packages/quick-open/src/browser/quick-open.service.tsx previousActiveElement 属性类型更改为 Element | undefined | null,并在 open 方法中添加捕获活动元素的代码
packages/quick-open/src/browser/prefix-quick-open.service.ts 更新 corePreferences 属性的访问修饰符,并在 doOpen 方法的 onClose 回调中添加聚焦编辑器的命令执行

针对链接问题的评估

目标 是否已解决 说明
保留编辑器光标和焦点 [#4271] 变更可能影响焦点管理,但尚不确定是否完全解决问题

可能相关的 PR

建议评审人

  • erha19
  • zhanba

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 53a0fcf and 34e72de.

📒 Files selected for processing (5)
  • packages/addons/src/browser/file-search.contribution.ts (0 hunks)
  • packages/quick-open/src/browser/prefix-quick-open.service.ts (4 hunks)
  • packages/quick-open/src/browser/quick-open.command.service.ts (0 hunks)
  • packages/quick-open/src/browser/quick-open.help.service.ts (0 hunks)
  • packages/quick-open/src/browser/quick-open.service.tsx (2 hunks)
💤 Files with no reviewable changes (3)
  • packages/quick-open/src/browser/quick-open.help.service.ts
  • packages/quick-open/src/browser/quick-open.command.service.ts
  • packages/addons/src/browser/file-search.contribution.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/quick-open/src/browser/quick-open.service.tsx
🔇 Additional comments (5)
packages/quick-open/src/browser/prefix-quick-open.service.ts (5)

22-22: 导入 EDITOR_COMMANDS 看起来很合理。
引入后能够在后续实现聚焦等逻辑,不存在明显问题。


34-34: 新增 CommandService 的导入保持一致性。
这里为后续调用命令提供了所需依赖,逻辑清晰且易维护。


158-158: 注意访问修饰符变更。
corePreferences 从 private 改为 protected 提高了可见性,请确认需要由子类访问此属性的场景并确保不会带来意外风险。


160-161: 通过自动注入 CommandService 来支持命令调用。
此改动看起来十分必要,用于在 onClose 回调里执行聚焦命令,行为清晰。


257-257: 聚焦编辑器命令调用逻辑确认。
这里执行 EDITOR_COMMANDS.FOCUS.id 可确保 QuickOpen 关闭后自动回到编辑器,符合修复需求。


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/quick-open/src/browser/quick-open.service.tsx (1)

Line range hint 159-169: 建议优化焦点恢复逻辑

当前实现在取消操作时会恢复焦点,这是正确的。不过建议考虑以下优化:

  1. 可以抽取焦点管理逻辑到单独的方法
  2. 考虑添加焦点恢复失败的降级处理

建议重构为:

+  private restoreFocus(): void {
+    try {
+      if (this.previousActiveElement instanceof HTMLElement) {
+        this.previousActiveElement.focus();
+      }
+    } catch (e) {
+      console.warn('Failed to restore focus:', e);
+    } finally {
+      this.previousActiveElement = undefined;
+    }
+  }

   public get widget(): QuickOpenWidget {
     // ...
     this._widget = this.injector.get(QuickOpenWidget, [
       {
         onOk: () => {
           this.previousActiveElement = undefined;
           this.onClose(false);
         },
         onCancel: () => {
-          if (this.previousActiveElement instanceof HTMLElement) {
-            this.previousActiveElement.focus();
-          }
-          this.previousActiveElement = undefined;
+          this.restoreFocus();
           this.onClose(true);
         },
         // ...
       },
     ]);
     // ...
   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f230c4d and 53a0fcf.

📒 Files selected for processing (4)
  • packages/addons/src/browser/file-search.contribution.ts (0 hunks)
  • packages/quick-open/src/browser/quick-open.command.service.ts (0 hunks)
  • packages/quick-open/src/browser/quick-open.help.service.ts (0 hunks)
  • packages/quick-open/src/browser/quick-open.service.tsx (2 hunks)
💤 Files with no reviewable changes (3)
  • packages/quick-open/src/browser/quick-open.help.service.ts
  • packages/quick-open/src/browser/quick-open.command.service.ts
  • packages/addons/src/browser/file-search.contribution.ts
🔇 Additional comments (2)
packages/quick-open/src/browser/quick-open.service.tsx (2)

58-58: 类型定义更新合理!

previousActiveElement 的类型从 Element | undefined 扩展为 Element | undefined | null 是一个合理的改动,这样可以更准确地表达所有可能的状态。


106-106: 存储先前活动元素的时机正确!

在打开快速打开对话框时保存当前活动元素是解决焦点问题的关键。建议验证在不同场景下的焦点行为。

✅ Verification successful

焦点管理实现正确且符合代码库的最佳实践!

代码库中对 document.activeElement 的使用主要有以下几种场景:

  • 在各种树形视图组件中检查焦点状态
  • 在编辑器和调试相关组件中管理焦点
  • 在核心浏览器服务中处理剪贴板和焦点追踪

当前实现在打开快速打开对话框时存储先前的活动元素是完全合理的,并且与代码库中其他地方的焦点管理模式保持一致。这种实现方式可以确保在对话框关闭后正确恢复焦点状态。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查其他地方是否有类似的焦点管理逻辑
rg "document\.activeElement" --type ts --type tsx

Length of output: 79


Script:

#!/bin/bash
# 检查其他地方是否有类似的焦点管理逻辑,修正文件类型
rg "document\.activeElement"

Length of output: 2621

Copy link

codecov bot commented Dec 25, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 54.27%. Comparing base (1a08936) to head (34e72de).

Files with missing lines Patch % Lines
...uick-open/src/browser/prefix-quick-open.service.ts 80.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4272   +/-   ##
=======================================
  Coverage   54.27%   54.27%           
=======================================
  Files        1633     1633           
  Lines       99725    99723    -2     
  Branches    21633    21634    +1     
=======================================
+ Hits        54122    54123    +1     
+ Misses      37889    37886    -3     
  Partials     7714     7714           
Flag Coverage Δ
jsdom 49.79% <80.00%> (+<0.01%) ⬆️
node 12.26% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hacke2 hacke2 force-pushed the fix/quick-auto-focus-pre-element branch from 53a0fcf to 34e72de Compare December 26, 2024 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] 触发 QuickPick 之后会丢失 Editor 光标和焦点
1 participant