Refactor: Extract removeDocById helper for document filtering (#112)

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Horizontal42 2026-08-19 16:36:35 +03:00 • committed by GitHub
parent 12ae91da2e
commit 34d2defa82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<void> {
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);