Skip to content

fix: sync first line when windows get focus #447

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

Merged
merged 1 commit into from
Jul 15, 2025

Conversation

HardyNLee
Copy link

介绍

fix #437

  • 修复使用实时预览时, 窗口重新获得焦点后, 总是会预览第一句的问题
  • 现在窗口重新获得焦点后, text editor 的撤销历史不会被清空

主要更改

修复窗口重新获得焦点时总是预览第一行

TextEditorhandleChange 不再使用改变的行数作为同步行数, 而是使用储存的行数进行同步;
此举是由于, updateEditData 会完全赋予新文字, 这使得其触发的 handleChange => ev.changes[0].range.startLineNumber 永远是1, 即从头到尾;
通常来说, onDidChangeCursorPosition 会及时记录光标位置, 而光标位置目前绝大多数清空都是与 ev.changes[0].range.startLineNumber 一致, 故此更改应该是可以接受的

窗口重新获得焦点时保留撤销历史

model.setValue 会完全清空撤销历史, 如果用户出于某些原因使浏览器失焦后, 重新获得焦点, 会失去所有撤销历史, 因此改成 ApplyEdit, 使更新的文本作为一步操作;

  • 目前发现, 全文 ApplyEdit 会刷新语法高亮, 因而短时间内会没有高亮, 应该不影响使用

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @HardyNLee, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the user experience within the text editor, particularly when the application window regains focus. The changes address two key issues: ensuring that the live preview accurately reflects the current cursor position rather than resetting to the first line, and preserving the editor's undo history during content refreshes.

Highlights

  • Live Preview Synchronization Fix: I've updated the handleChange function in TextEditor.tsx to use a stored cursor position (editorLineHolder.getSceneLine) for live preview synchronization. This change addresses an issue where the live preview would incorrectly jump to the first line when the editor content was updated, especially after the application window regained focus. Previously, it relied on ev.changes[0].range.startLineNumber, which could be 1 if the entire document was replaced, leading to the incorrect synchronization.
  • Undo History Preservation: To prevent the loss of undo history when the editor content is refreshed (e.g., after the window regains focus), I've modified the content update mechanism in TextEditor.tsx. Instead of using editorRef.current?.getModel()?.setValue(), which clears the undo stack, I've switched to model?.applyEdits(). This treats the content update as a single edit operation, thereby preserving the user's undo history.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses issues related to the editor's behavior when the window regains focus: preventing the view from jumping to the first line and preserving the undo history. The approach to use a stored line number for syncing is a good solution for the first issue. The switch from setValue to applyEdits correctly addresses the undo history problem.

I've found one critical issue that could lead to a runtime crash. Please see my detailed comments below.

Comment on lines +113 to +120
const model = editorRef.current?.getModel();
model?.applyEdits([
{
range: model.getFullModelRange(),
text: currentText.value,
forceMoveMarkers: true
}
]);

Choose a reason for hiding this comment

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

critical

There is a potential TypeError here that could crash the application. The range property of the object inside the applyEdits array is evaluated before applyEdits is called. If model is null or undefined (which can happen if editorRef.current is not yet available), the expression model.getFullModelRange() will throw an error. The optional chaining on model?.applyEdits does not prevent this.

To fix this, you should wrap the applyEdits call in a conditional block that checks if model exists.

    const model = editorRef.current?.getModel();
    if (model) {
      model.applyEdits([
        {
          range: model.getFullModelRange(),
          text: currentText.value,
          forceMoveMarkers: true
        }
      ]);
    }

@MakinoharaShoko MakinoharaShoko merged commit 96dbbd5 into OpenWebGAL:dev Jul 15, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants