Skip to content

Commit db4f81b

Browse files
authored
fix(quicktools): preserve editor focus during rapid taps and stale issue (#2408)
1 parent 6a52900 commit db4f81b

2 files changed

Lines changed: 49 additions & 19 deletions

File tree

src/handlers/quickTools.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ export const key = {
201201
* @returns {boolean} Whether the action was performed
202202
*/
203203
export default function actions(action, value) {
204+
setQuicktoolsUsed();
205+
204206
const { editor } = editorManager;
205207
const { $input, $replaceInput } = quickTools;
206208

@@ -233,14 +235,13 @@ export default function actions(action, value) {
233235

234236
case "key": {
235237
value = Number.parseInt(value, 10);
236-
if (value > 40 && value < 37) {
238+
if (value < 37 || value > 40) {
237239
resetKeys();
238240
}
239241
setInput();
240242
getInput().dispatchEvent(
241243
KeyboardEvent("keydown", getKeys({ keyCode: value })),
242244
);
243-
setQuicktoolsUsed();
244245
return true;
245246
}
246247

src/handlers/quickToolsInit.js

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import actions, { key } from "./quickTools";
66

77
const CONTEXT_MENU_TIMEOUT = 500;
88
const MOVE_X_THRESHOLD = 50;
9+
const TOUCH_EVENT_OPTIONS = { passive: false };
910

1011
let time;
1112
let moveX;
@@ -73,17 +74,19 @@ export default function init() {
7374
else $footer.removeAttribute("data-meta");
7475
});
7576

76-
editorManager.on(["file-content-changed", "switch-file"], () => {
77-
if (editorManager.activeFile?.isUnsaved) {
78-
$footer.setAttribute("data-unsaved", "true");
79-
} else {
80-
$footer.removeAttribute("data-unsaved");
81-
}
82-
updateHistoryButtons();
83-
});
77+
editorManager.on(
78+
[
79+
"file-content-changed",
80+
"switch-file",
81+
"new-file",
82+
"file-loaded",
83+
"remove-file",
84+
],
85+
scheduleUpdateQuickToolsState,
86+
);
8487

8588
editorManager.on("save-file", () => {
86-
$footer.removeAttribute("data-unsaved");
89+
scheduleUpdateQuickToolsState();
8790
});
8891

8992
editorManager.on("editor-state-changed", updateHistoryButtons);
@@ -97,7 +100,7 @@ export default function init() {
97100
root.appendOuter($toggler);
98101
}
99102
document.body.append($input);
100-
setTimeout(updateHistoryButtons, 0);
103+
scheduleUpdateQuickToolsState();
101104

102105
if (
103106
appSettings.value.quickToolsTriggerMode ===
@@ -108,7 +111,7 @@ export default function init() {
108111
$footer.addEventListener("contextmenu", oncontextmenu, true);
109112
$footer.addEventListener("wheel", onwheel, { passive: false });
110113
} else {
111-
$footer.addEventListener("touchstart", touchstart);
114+
$footer.addEventListener("touchstart", touchstart, TOUCH_EVENT_OPTIONS);
112115
$footer.addEventListener("keydown", touchstart);
113116
}
114117

@@ -124,7 +127,7 @@ export default function init() {
124127
$footer.removeEventListener("click", onclick);
125128
$footer.removeEventListener("wheel", onwheel);
126129
$footer.addEventListener("keydown", touchstart);
127-
$footer.addEventListener("touchstart", touchstart);
130+
$footer.addEventListener("touchstart", touchstart, TOUCH_EVENT_OPTIONS);
128131
}
129132
});
130133
}
@@ -374,6 +377,22 @@ function click($el) {
374377
actions(action, value);
375378
}
376379

380+
function scheduleUpdateQuickToolsState() {
381+
setTimeout(updateQuickToolsState, 0);
382+
}
383+
384+
function updateQuickToolsState() {
385+
const { $footer } = quickTools;
386+
387+
if (editorManager.activeFile?.isUnsaved) {
388+
$footer.setAttribute("data-unsaved", "true");
389+
} else {
390+
$footer.removeAttribute("data-unsaved");
391+
}
392+
393+
updateHistoryButtons();
394+
}
395+
377396
function updateHistoryButtons() {
378397
const { editor, activeFile } = editorManager;
379398
const disabled = !editor || activeFile?.type !== "editor";
@@ -383,10 +402,20 @@ function updateHistoryButtons() {
383402
}
384403

385404
function updateHistoryButton(id, disabled) {
386-
quickTools.$footer
387-
.querySelectorAll(`[data-id="${id}"]`)
388-
.forEach(($button) => {
389-
$button.disabled = disabled;
390-
$button.setAttribute("aria-disabled", String(disabled));
405+
const buttons = new Set();
406+
407+
for (const $container of [
408+
quickTools.$footer,
409+
quickTools.$row1,
410+
quickTools.$row2,
411+
]) {
412+
$container?.querySelectorAll(`[data-id="${id}"]`)?.forEach(($button) => {
413+
buttons.add($button);
391414
});
415+
}
416+
417+
buttons.forEach(($button) => {
418+
$button.disabled = disabled;
419+
$button.setAttribute("aria-disabled", String(disabled));
420+
});
392421
}

0 commit comments

Comments
 (0)