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

feat: terminal adds drag-and-drop #4300

Merged
merged 3 commits into from
Jan 6, 2025
Merged

Conversation

lulusir
Copy link
Contributor

@lulusir lulusir commented Jan 3, 2025

Types

  • 🎉 New Features

Background or solution

#4218

屏幕录制2025-01-03 16 05 09

Summary by CodeRabbit

  • 新功能

    • 为终端标签页添加拖放功能,允许用户通过拖动重新排列终端组
    • 支持在标签项目上触发拖动开始和放置事件
  • 增强

    • 改进了终端组交互性,用户现在可以直接拖动和重新排序标签组
  • 技术改进

    • 在多个组件和服务中实现了拖放事件处理逻辑
    • 添加了在终端组之间交换位置的方法

@CLAassistant
Copy link

CLAassistant commented Jan 3, 2025

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@opensumi opensumi bot added the 🎨 feature feature required label Jan 3, 2025
Copy link
Contributor

coderabbitai bot commented Jan 3, 2025

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 eslint@8.57.1: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/config-array@0.13.0: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/object-schema@2.0.3: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > inflight@1.0.6: 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 > glob@7.2.3: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"

概述

演练

此拉取请求为终端应用程序引入了拖放功能。主要变更包括在 tab.item.tsxtab.view.tsx 和相关文件中添加拖放事件处理程序。新增了 swapGroup 方法,允许用户通过拖放重新排列终端组。这些更改提高了用户界面的交互性,使终端组的管理更加灵活。

变更

文件 变更摘要
packages/terminal-next/src/browser/component/tab.item.tsx 添加拖放事件处理程序,包括 handleDragStarthandleDragOverhandleDragLeavehandleDrop
packages/terminal-next/src/browser/component/tab.view.tsx TabItem 组件添加拖放功能,实现组交换
packages/terminal-next/src/browser/terminal.view.ts 新增 swapGroup 方法,用于交换终端组
packages/terminal-next/src/common/controller.ts ITerminalGroupViewService 接口中添加 swapGroup 方法
packages/terminal-next/src/common/render.ts ItemProps 接口添加拖放相关的可选属性

序列图

sequenceDiagram
    participant User
    participant TabItem
    participant TerminalView
    
    User->>TabItem: 开始拖动
    TabItem->>TerminalView: 触发 onDragStart
    User->>TabItem: 放置到新位置
    TabItem->>TerminalView: 触发 onDrop
    TerminalView->>TerminalView: 交换组
Loading

可能相关的 PR

建议审阅者

  • erha19
  • Ricbet
  • hacke2

📜 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 b86a8b5 and 55c18be.

📒 Files selected for processing (1)
  • packages/terminal-next/src/browser/component/tab.view.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/terminal-next/src/browser/component/tab.view.tsx

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 (4)
packages/terminal-next/src/common/render.ts (1)

21-22: 建议在文档中补充 DragEvent 参数说明
这些新增的 drag-and-drop 回调属性很好地提升了可扩展性,但可在注释或文档中说明事件对象的使用方式,以便后续维护。

packages/terminal-next/src/common/controller.ts (1)

143-144: 新增方法 swapGroup(i, j) 易用性良好,但需确保索引合法
交换功能对增强可交互性很有帮助。建议在下一步实现中对传入的索引 i、j 做边界检查,若索引不合法可进行相应的异常处理或日志记录。

packages/terminal-next/src/browser/component/tab.item.tsx (1)

77-97: 拖拽操作的视觉提示“on-drag-over”需注意类名重复
当前通过添加和移除 'on-drag-over' 类名来进行视觉提示,如有其他组件也使用该类名,可能造成冲突。建议在命名上使用更具唯一性的前缀。

-ref.current.classList.add('on-drag-over');
+ref.current.classList.add('terminal-on-drag-over');
packages/terminal-next/src/browser/terminal.view.ts (1)

278-293: 建议完善越界检查以提高稳健性

当前方法仅在 ij 等于 -1 时返回,但未对索引是否超出数组合法范围进行验证,可能导致在越界场景下出现不可预期的行为。可在交换前先检查 ij 是否在 newGroups 数组有效范围内,确保方法逻辑更加健壮。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d5f68e0 and 10264bd.

📒 Files selected for processing (5)
  • packages/terminal-next/src/browser/component/tab.item.tsx (1 hunks)
  • packages/terminal-next/src/browser/component/tab.view.tsx (1 hunks)
  • packages/terminal-next/src/browser/terminal.view.ts (1 hunks)
  • packages/terminal-next/src/common/controller.ts (1 hunks)
  • packages/terminal-next/src/common/render.ts (1 hunks)
🔇 Additional comments (2)
packages/terminal-next/src/common/render.ts (1)

25-25: 与其他属性保持一致的可读性
draggable 属性的引入遵循了可选属性的风格,命名也恰当,建议在后续维护中确保与组件其他可选属性一致,便于统一管理。

packages/terminal-next/src/browser/component/tab.view.tsx (1)

49-61: 注意在 onDrop 中处理默认行为与冒泡
在对拖拽的事件链进行处理时,最好检查是否需要调用 stopPropagation()preventDefault() 以避免与上层或同级元素的事件冲突。当前实现已调用 e.preventDefault() 在 onDragOver(见下方),但 onDrop 同样可能需要此处理以确保事件不会被冒泡到其它元素。

 onDrop={(e: React.DragEvent) => {
+  e.preventDefault();
   if (e.dataTransfer.getData('groupIndex')) {
     ...
   }
 }}
✅ Verification successful

Let me search for related drag and drop event handling in the codebase to verify if there are any potential event conflicts.


Let me try a different search approach without specifying file types.


根据代码库搜索结果,我可以得出结论了。

建议在拖拽事件处理中添加 preventDefault() 以确保正确行为

从代码库中可以看到:

  1. tab.item.tsx 中的 onDragOver 已经调用了 e.preventDefault(),这是正确的。
  2. 在其他组件的拖拽实现中,如 editor.view.tsxtab.view.tsx 等,都同时使用了 preventDefault() 来阻止默认行为。
  3. 特别是在 tab.view.tsx 中,拖拽事件的处理不仅使用了 preventDefault(),还使用了 stopPropagation() 来阻止事件冒泡。

因此,为了保持一致性并确保拖拽功能的正确性,建议在 onDrop 处理程序中添加 preventDefault()

onDrop={(e: React.DragEvent) => {
+  e.preventDefault();
   if (e.dataTransfer.getData('groupIndex')) {
     {
       const i = e.dataTransfer.getData('groupIndex');
       view.swapGroup(Number(i), index);
       view.selectGroup(index);
     }
   }
}}
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Search for drag and drop event handlers in the codebase
rg -A 5 "onDrop|onDragOver|onDragStart" --type tsx --type jsx

Length of output: 91


Script:

#!/bin/bash
# Search for drag and drop event handlers in the codebase
rg -A 5 "onDrop|onDragOver|onDragStart"

Length of output: 37022

@hacke2
Copy link
Member

hacke2 commented Jan 3, 2025

ternimal -> terminal

@Ricbet
Copy link
Member

Ricbet commented Jan 3, 2025

@lulusir 感谢 PR,麻烦签署一下 CLA

@lulusir lulusir changed the title feat: ternimal adds drag-and-drop feat: terminal adds drag-and-drop Jan 3, 2025
Copy link

codecov bot commented Jan 3, 2025

Codecov Report

Attention: Patch coverage is 0% with 10 lines in your changes missing coverage. Please review.

Project coverage is 54.20%. Comparing base (d5f68e0) to head (55c18be).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...ackages/terminal-next/src/browser/terminal.view.ts 0.00% 9 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4300      +/-   ##
==========================================
- Coverage   54.21%   54.20%   -0.01%     
==========================================
  Files        1634     1634              
  Lines       99917    99927      +10     
  Branches    21699    21700       +1     
==========================================
+ Hits        54166    54167       +1     
- Misses      38008    38016       +8     
- Partials     7743     7744       +1     
Flag Coverage Δ
jsdom 49.74% <0.00%> (-0.01%) ⬇️
node 12.24% <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.

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: 1

🧹 Nitpick comments (2)
packages/terminal-next/src/browser/component/tab.item.tsx (2)

75-106: 优化拖拽处理器的实现

建议进行以下优化:

  1. 将 'on-drag-over' CSS 类名定义在 CSS 模块中,保持一致性
  2. 添加错误处理,防止 DOM 操作失败
  3. 优化重复的 classList 操作
  4. 考虑使用 React 的 state 来管理拖拽状态,而不是直接操作 DOM

建议按如下方式重构:

+ const [isDragOver, setIsDragOver] = useState(false);

- const handleDragOver = useCallback(
-   (e: React.DragEvent<HTMLDivElement>) => {
-     e.preventDefault();
-     if (ref.current) {
-       ref.current.classList.add('on-drag-over');
-     }
-   },
-   [ref],
- );
+ const handleDragOver = useCallback((e: React.DragEvent<HTMLDivElement>) => {
+   e.preventDefault();
+   setIsDragOver(true);
+ }, []);

- const handleDragLeave = useCallback(() => {
-   if (ref.current) {
-     ref.current.classList.remove('on-drag-over');
-   }
- }, [ref]);
+ const handleDragLeave = useCallback(() => {
+   setIsDragOver(false);
+ }, []);

- const handleDrop = useCallback(
-   (e: React.DragEvent<HTMLDivElement>) => {
-     if (ref.current) {
-       ref.current.classList.remove('on-drag-over');
-     }
-     props.onDrop?.(e);
-   },
-   [ref, props.onDrop],
- );
+ const handleDrop = useCallback((e: React.DragEvent<HTMLDivElement>) => {
+   setIsDragOver(false);
+   props.onDrop?.(e);
+ }, [props.onDrop]);

然后在样式中添加:

+ .dragOver {
+   /* 定义拖拽悬停样式 */
+ }

在组件的 className 中使用:

  className={cls({
    [styles_item_container]: true,
    [styles_tab_item_selected]: !!props.selected,
+   [styles.dragOver]: isDragOver,
  })}

110-114: 优化拖拽事件绑定

建议将拖拽事件处理器通过对象传递,提高代码可读性。

  <div
    draggable={props.draggable}
-   onDragStart={handleDragStart}
-   onDragOver={handleDragOver}
-   onDragLeave={handleDragLeave}
-   onDrop={handleDrop}
+   {...{
+     onDragStart: handleDragStart,
+     onDragOver: handleDragOver,
+     onDragLeave: handleDragLeave,
+     onDrop: handleDrop,
+   }}
    // ...其他属性
  >
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 10264bd and b86a8b5.

📒 Files selected for processing (2)
  • packages/terminal-next/src/browser/component/tab.item.tsx (1 hunks)
  • packages/terminal-next/src/browser/component/tab.view.tsx (1 hunks)

packages/terminal-next/src/browser/component/tab.view.tsx Outdated Show resolved Hide resolved
@erha19 erha19 merged commit 826fcf0 into opensumi:main Jan 6, 2025
11 of 12 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jan 23, 2025
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎨 feature feature required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants