Skip to content

Commit

Permalink
Immediate gutter update for caretOffsetY changes (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jan 9, 2025
1 parent 7f605e0 commit a027374
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions CoreEditor/src/modules/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function interceptInputs() {
export function observeChanges() {
return EditorView.updateListener.of(update => {
// Ignore all events when the editor is idle
if (editingState.isIdle && window.editor.state.doc.length === 0) {
if (editingState.isIdle && update.state.doc.length === 0) {
return;
}

Expand Down Expand Up @@ -154,10 +154,19 @@ export function observeChanges() {

// To handle a case where line number rects are not correctly updated
if (update.docChanged) {
window.editor.requestMeasure();
update.view.requestMeasure();
}

storage.gutterUpdater = setTimeout(adjustGutterPositions, 15);
const caretOffsetY = storage.caretOffsetY;
storage.caretOffsetY = update.view.coordsAtPos(update.state.selection.main.to)?.bottom;

if (caretOffsetY !== undefined && caretOffsetY !== storage.caretOffsetY) {
// Re-layout immediately when the y-axis of the caret position changes
adjustGutterPositions();
} else {
// Otherwise, the update is throttled with a small delay
storage.gutterUpdater = setTimeout(adjustGutterPositions, 15);
}
}

// Gutter update triggered by fold or unfold actions (immediately)
Expand All @@ -173,7 +182,9 @@ export function observeChanges() {
}

const storage: {
caretOffsetY: number | undefined;
gutterUpdater: ReturnType<typeof setTimeout> | undefined;
} = {
caretOffsetY: undefined,
gutterUpdater: undefined,
};

0 comments on commit a027374

Please sign in to comment.