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(hook): ✨ 添加光标位置保存与恢复功能 #152

Merged
merged 1 commit into from
Jan 7, 2025

Conversation

CHIMOOO
Copy link
Contributor

@CHIMOOO CHIMOOO commented Jan 7, 2025

  • 新增响应式变量 lastCursorPosition 用于保存和恢复输入框的光标位置
  • 在输入框失去焦点时保存当前光标位置
  • 在输入框获得焦点时恢复上次保存的光标位置
  • 更新光标位置的逻辑以确保用户体验流畅

💻 变更类型 | Change Type

  • ✨ feat | 新增功能
  • 🐛 fix | 修复缺陷
  • ♻️ refactor | 代码重构(不包括 bug 修复、功能新增)
  • 💄 style | 代码格式(不影响功能,例如空格、分号等格式修正)
  • 📦️ build | 构建流程、外部依赖变更(如升级 npm 包、修改 vite 配置等)
  • 🚀 perf | 性能优化
  • 📝 docs | 文档变更
  • 🧪 test | 添加疏漏测试或已有测试改动
  • ⚙️ ci | 修改 CI 配置、脚本
  • ↩️ revert | 回滚 commit
  • 🛠️ chore | 对构建过程或辅助工具和库的更改(不影响源文件、测试用例)

🔀 变更说明 | Description of Change

📝 补充信息 | Additional Information

- 新增响应式变量 `lastCursorPosition` 用于保存和恢复输入框的光标位置
- 在输入框失去焦点时保存当前光标位置
- 在输入框获得焦点时恢复上次保存的光标位置
- 更新光标位置的逻辑以确保用户体验流畅
@github-actions github-actions bot added the 前端 关于前端的代码修改 label Jan 7, 2025
@CHIMOOO CHIMOOO requested a review from nongyehong January 7, 2025 10:01
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🎉 感谢你提交第一个 PR!我们很高兴你参与到 HuLa 项目中来。我们会尽快审查你的贡献。

Copy link

github-actions bot commented Jan 7, 2025

PR 代码分析

### 代码变更分析

1. 代码逻辑的改动

  • 新增响应式变量 lastCursorPosition:在文件顶部添加了一个新的响应式变量 lastCursorPosition,用于保存光标的最后位置,包括 rangeselection
  • 保存和恢复光标位置
    • 在插入节点或处理编辑器范围时,会保存当前的光标位置到 lastCursorPosition
    • 在重新获取焦点时,尝试恢复上次保存的光标位置。
  • 失焦事件监听:为 messageInputDom 添加了 blur 事件监听器,在输入框失去焦点时保存当前光标位置。

2. 潜在的问题或优化空间

  • 空值检查:在多个地方直接使用 getEditorRange()!,假设其返回值不会为 nullundefined。建议增加空值检查以避免潜在的运行时错误。
  • 性能影响:每次光标位置变化都会触发 watchEffect,可能会导致频繁的重绘或重排。可以考虑减少不必要的更新或优化响应式依赖。
  • 事件监听器管理:在组件卸载时应移除 blur 事件监听器,以防止内存泄漏。

3. TypeScript 类型定义的准确性

  • 类型定义准确lastCursorPosition 的类型定义是合理的,明确指定了 rangeselection 的类型分别为 Range | nullSelection | null
  • 非空断言:使用了非空断言(!),这在某些情况下可能是合理的,但需要注意确保这些断言的安全性。

4. Vue 组件的性能影响

  • 频繁的 DOM 操作:频繁地保存和恢复光标位置可能会导致额外的 DOM 操作,影响性能。可以通过批量更新或节流来优化。
  • 响应式依赖watchEffect 的使用会导致每次依赖项变化时都触发副作用函数,可能会影响性能。可以考虑使用更细粒度的 watch 来减少不必要的触发。

5. Rust 代码的安全性和性能

  • 不适用:此代码变更与 Rust 无关,因此不涉及 Rust 代码的安全性和性能问题。

总结

主要改动集中在引入了光标位置的保存和恢复功能,并增加了失焦事件监听。虽然这些改动增强了用户体验,但也需要注意潜在的性能问题和空值处理。建议增加适当的空值检查和事件监听器管理,以确保代码的健壮性和性能。

*这是由通义千问 AI 自动生成的 PR 分析,仅供参考。*

Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 16.66667% with 25 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/hooks/useMsgInput.ts 16.66% 25 Missing ⚠️
Files with missing lines Coverage Δ
src/hooks/useMsgInput.ts 33.12% <16.66%> (-1.12%) ⬇️

@nongyehong nongyehong merged commit 53b5935 into master Jan 7, 2025
14 checks passed
@nongyehong nongyehong deleted the fix/chimoo branch January 7, 2025 10:46
nongyehong pushed a commit that referenced this pull request Jan 8, 2025
* fix(hook): 🐛 回滚useMsgInput
* Merge remote-tracking branch 'refs/remotes/origin/master' into fix/group
* fix(component): 🐛 修复群聊
* fix(typing): 🐛 群聊分页
* feat(hook): ✨ 添加光标位置保存与恢复功能 (#152)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
前端 关于前端的代码修改
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants