mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
🧹 Extract UI bindings from initUI
Extracted various inline event bindings and setups from the monolithic `initUI` function into separate, well-named helper functions. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
f5b1fc038b
commit
93485fa840
1 changed files with 37 additions and 10 deletions
47
src/main.ts
47
src/main.ts
|
|
@ -800,13 +800,7 @@ async function initDataAndEngine(): Promise<void> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function initUI(): Promise<void> {
|
function bindTitleUI(): void {
|
||||||
|
|
||||||
applySettings(); // sets the language before bindSettingsUI renders dynamic labels
|
|
||||||
bindSettingsUI();
|
|
||||||
renderDocList();
|
|
||||||
syncTitleField();
|
|
||||||
|
|
||||||
const titleField = $<HTMLInputElement>("#doc-title");
|
const titleField = $<HTMLInputElement>("#doc-title");
|
||||||
titleField.addEventListener("change", () => {
|
titleField.addEventListener("change", () => {
|
||||||
const doc = activeDoc();
|
const doc = activeDoc();
|
||||||
|
|
@ -830,7 +824,9 @@ async function initUI(): Promise<void> {
|
||||||
titleField.addEventListener("keydown", (e) => {
|
titleField.addEventListener("keydown", (e) => {
|
||||||
if (e.key === "Enter" || e.key === "Escape") titleField.blur();
|
if (e.key === "Enter" || e.key === "Escape") titleField.blur();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function bindMiscUI(): void {
|
||||||
$("#total-line").addEventListener("click", () => {
|
$("#total-line").addEventListener("click", () => {
|
||||||
const v = $("#total-line").dataset.value;
|
const v = $("#total-line").dataset.value;
|
||||||
if (v) {
|
if (v) {
|
||||||
|
|
@ -841,6 +837,10 @@ async function initUI(): Promise<void> {
|
||||||
|
|
||||||
$("#open-search").addEventListener("click", () => search.open());
|
$("#open-search").addEventListener("click", () => search.open());
|
||||||
|
|
||||||
|
window.addEventListener("focus", () => { lastWindowFocusAt = performance.now(); });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function bindWindowControls(): Promise<void> {
|
||||||
if (isTauri()) {
|
if (isTauri()) {
|
||||||
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
||||||
const win = getCurrentWindow();
|
const win = getCurrentWindow();
|
||||||
|
|
@ -849,7 +849,9 @@ async function initUI(): Promise<void> {
|
||||||
} else {
|
} else {
|
||||||
$("#win-controls").style.display = "none";
|
$("#win-controls").style.display = "none";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function bindSidebarUI(): void {
|
||||||
$("#toggle-sidebar").addEventListener("click", () => {
|
$("#toggle-sidebar").addEventListener("click", () => {
|
||||||
settings.sidebarVisible = !settings.sidebarVisible;
|
settings.sidebarVisible = !settings.sidebarVisible;
|
||||||
$("#sidebar").classList.toggle("hidden", !settings.sidebarVisible);
|
$("#sidebar").classList.toggle("hidden", !settings.sidebarVisible);
|
||||||
|
|
@ -869,7 +871,9 @@ async function initUI(): Promise<void> {
|
||||||
$("#sidebar").classList.add("hidden");
|
$("#sidebar").classList.add("hidden");
|
||||||
void saveSettings(settings);
|
void saveSettings(settings);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function bindDividerUI(): void {
|
||||||
const divider = $("#col-divider");
|
const divider = $("#col-divider");
|
||||||
const wrap = $("#editor-wrap");
|
const wrap = $("#editor-wrap");
|
||||||
divider.addEventListener("mousedown", (e) => {
|
divider.addEventListener("mousedown", (e) => {
|
||||||
|
|
@ -893,9 +897,9 @@ async function initUI(): Promise<void> {
|
||||||
window.addEventListener("mousemove", onMove);
|
window.addEventListener("mousemove", onMove);
|
||||||
window.addEventListener("mouseup", onUp);
|
window.addEventListener("mouseup", onUp);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener("focus", () => { lastWindowFocusAt = performance.now(); });
|
function bindKeyboardShortcuts(): void {
|
||||||
|
|
||||||
document.addEventListener("keydown", (e) => {
|
document.addEventListener("keydown", (e) => {
|
||||||
if (e.ctrlKey && !e.shiftKey && e.key.toLowerCase() === "n") {
|
if (e.ctrlKey && !e.shiftKey && e.key.toLowerCase() === "n") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
@ -919,8 +923,9 @@ async function initUI(): Promise<void> {
|
||||||
toast(t("copied"));
|
toast(t("copied"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// export button + dropdown
|
function bindExportUI(): void {
|
||||||
const exportBtn = $("#export-btn");
|
const exportBtn = $("#export-btn");
|
||||||
const exportMenu = $("#export-menu");
|
const exportMenu = $("#export-menu");
|
||||||
exportBtn.addEventListener("click", (e) => {
|
exportBtn.addEventListener("click", (e) => {
|
||||||
|
|
@ -960,12 +965,16 @@ async function initUI(): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function bindRatesAndMarketUI(): void {
|
||||||
$("#rates-info").addEventListener("click", () => void refreshRates(true));
|
$("#rates-info").addEventListener("click", () => void refreshRates(true));
|
||||||
$("#market-info").addEventListener("click", () => void refreshMarket(true));
|
$("#market-info").addEventListener("click", () => void refreshMarket(true));
|
||||||
setInterval(renderRatesInfo, 60_000);
|
setInterval(renderRatesInfo, 60_000);
|
||||||
setInterval(renderMarketInfo, 60_000);
|
setInterval(renderMarketInfo, 60_000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function bindLifecycle(): Promise<void> {
|
||||||
void onFileDrop((content) => newDoc(content));
|
void onFileDrop((content) => newDoc(content));
|
||||||
|
|
||||||
if (!(await registerHotkey(null, settings.hotkey))) toast(t("hotkeyFailed"));
|
if (!(await registerHotkey(null, settings.hotkey))) toast(t("hotkeyFailed"));
|
||||||
|
|
@ -1000,6 +1009,24 @@ async function initUI(): Promise<void> {
|
||||||
setInterval(() => { if (settings.autoUpdateEnabled) void checkUpdate(); }, 6 * 60 * 60 * 1000);
|
setInterval(() => { if (settings.autoUpdateEnabled) void checkUpdate(); }, 6 * 60 * 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function initUI(): Promise<void> {
|
||||||
|
|
||||||
|
applySettings(); // sets the language before bindSettingsUI renders dynamic labels
|
||||||
|
bindSettingsUI();
|
||||||
|
renderDocList();
|
||||||
|
syncTitleField();
|
||||||
|
|
||||||
|
bindTitleUI();
|
||||||
|
bindMiscUI();
|
||||||
|
await bindWindowControls();
|
||||||
|
bindSidebarUI();
|
||||||
|
bindDividerUI();
|
||||||
|
bindKeyboardShortcuts();
|
||||||
|
bindExportUI();
|
||||||
|
bindRatesAndMarketUI();
|
||||||
|
await bindLifecycle();
|
||||||
|
}
|
||||||
|
|
||||||
async function boot(): Promise<void> {
|
async function boot(): Promise<void> {
|
||||||
await initSettings();
|
await initSettings();
|
||||||
await initDataAndEngine();
|
await initDataAndEngine();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue