From 34d2defa82a81550c79ef5ad68e4c980769c23cd Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Wed, 19 Aug 2026 16:36:35 +0300 Subject: [PATCH] Refactor: Extract `removeDocById` helper for document filtering (#112) Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/main.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main.ts b/src/main.ts index 839a9bb..e3f0729 100644 --- a/src/main.ts +++ b/src/main.ts @@ -71,19 +71,23 @@ function syncTitleField(): void { if (document.activeElement !== field) field.value = activeDoc()?.title ?? ""; } +function removeDocById(id: string): void { + data.docs = data.docs.filter((d) => d.id !== id); +} + function pinDoc(id: string): void { const doc = getDocById(id); if (!doc) return; doc.pinned = !doc.pinned; if (doc.pinned) { // move to end of pinned group - data.docs = data.docs.filter((d) => d.id !== id); + removeDocById(id); let lastPinned = -1; for (let i = data.docs.length - 1; i >= 0; i--) { if (data.docs[i].pinned) { lastPinned = i; break; } } data.docs.splice(lastPinned + 1, 0, doc); } else { // move to start of non-pinned group - data.docs = data.docs.filter((d) => d.id !== id); + removeDocById(id); const firstNonPinned = data.docs.findIndex((d) => !d.pinned); data.docs.splice(firstNonPinned === -1 ? data.docs.length : firstNonPinned, 0, doc); } @@ -97,7 +101,7 @@ function reorderDoc(srcId: string, targetId: string): void { const src = getDocById(srcId); const target = getDocById(targetId); if (!src || !target || !!src.pinned !== !!target.pinned) return; - data.docs = data.docs.filter((d) => d.id !== srcId); + removeDocById(srcId); const targetIdx = data.docs.findIndex((d) => d.id === targetId); data.docs.splice(targetIdx, 0, src); saveAppData(data); @@ -199,7 +203,7 @@ function createDeleteButton(doc: DocMeta, appData: AppData, appSettings: Setting if (confirmTimer) clearTimeout(confirmTimer); void backupDeletedSheet(appSettings.dataDir, doc.title, appData.contents[doc.id] ?? ""); delete appData.contents[doc.id]; - appData.docs = appData.docs.filter((d) => d.id !== doc.id); + removeDocById(doc.id); if (appData.activeId === doc.id) switchDoc(appData.docs[0].id); else renderDocList(); saveAppData(appData); @@ -260,7 +264,7 @@ async function closeActiveDoc(): Promise { const idx = data.docs.findIndex((d) => d.id === doc.id); void backupDeletedSheet(settings.dataDir, doc.title, content); delete data.contents[doc.id]; - data.docs = data.docs.filter((d) => d.id !== doc.id); + removeDocById(doc.id); const nextId = data.docs[idx] ? data.docs[idx].id : data.docs[idx - 1]?.id ?? data.docs[0].id; switchDoc(nextId); saveAppData(data);