mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 01:02:01 +03:00
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:
parent
12ae91da2e
commit
34d2defa82
1 changed files with 9 additions and 5 deletions
14
src/main.ts
14
src/main.ts
|
|
@ -71,19 +71,23 @@ function syncTitleField(): void {
|
||||||
if (document.activeElement !== field) field.value = activeDoc()?.title ?? "";
|
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 {
|
function pinDoc(id: string): void {
|
||||||
const doc = getDocById(id);
|
const doc = getDocById(id);
|
||||||
if (!doc) return;
|
if (!doc) return;
|
||||||
doc.pinned = !doc.pinned;
|
doc.pinned = !doc.pinned;
|
||||||
if (doc.pinned) {
|
if (doc.pinned) {
|
||||||
// move to end of pinned group
|
// move to end of pinned group
|
||||||
data.docs = data.docs.filter((d) => d.id !== id);
|
removeDocById(id);
|
||||||
let lastPinned = -1;
|
let lastPinned = -1;
|
||||||
for (let i = data.docs.length - 1; i >= 0; i--) { if (data.docs[i].pinned) { lastPinned = i; break; } }
|
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);
|
data.docs.splice(lastPinned + 1, 0, doc);
|
||||||
} else {
|
} else {
|
||||||
// move to start of non-pinned group
|
// 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);
|
const firstNonPinned = data.docs.findIndex((d) => !d.pinned);
|
||||||
data.docs.splice(firstNonPinned === -1 ? data.docs.length : firstNonPinned, 0, doc);
|
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 src = getDocById(srcId);
|
||||||
const target = getDocById(targetId);
|
const target = getDocById(targetId);
|
||||||
if (!src || !target || !!src.pinned !== !!target.pinned) return;
|
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);
|
const targetIdx = data.docs.findIndex((d) => d.id === targetId);
|
||||||
data.docs.splice(targetIdx, 0, src);
|
data.docs.splice(targetIdx, 0, src);
|
||||||
saveAppData(data);
|
saveAppData(data);
|
||||||
|
|
@ -199,7 +203,7 @@ function createDeleteButton(doc: DocMeta, appData: AppData, appSettings: Setting
|
||||||
if (confirmTimer) clearTimeout(confirmTimer);
|
if (confirmTimer) clearTimeout(confirmTimer);
|
||||||
void backupDeletedSheet(appSettings.dataDir, doc.title, appData.contents[doc.id] ?? "");
|
void backupDeletedSheet(appSettings.dataDir, doc.title, appData.contents[doc.id] ?? "");
|
||||||
delete 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);
|
if (appData.activeId === doc.id) switchDoc(appData.docs[0].id);
|
||||||
else renderDocList();
|
else renderDocList();
|
||||||
saveAppData(appData);
|
saveAppData(appData);
|
||||||
|
|
@ -260,7 +264,7 @@ async function closeActiveDoc(): Promise<void> {
|
||||||
const idx = data.docs.findIndex((d) => d.id === doc.id);
|
const idx = data.docs.findIndex((d) => d.id === doc.id);
|
||||||
void backupDeletedSheet(settings.dataDir, doc.title, content);
|
void backupDeletedSheet(settings.dataDir, doc.title, content);
|
||||||
delete data.contents[doc.id];
|
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;
|
const nextId = data.docs[idx] ? data.docs[idx].id : data.docs[idx - 1]?.id ?? data.docs[0].id;
|
||||||
switchDoc(nextId);
|
switchDoc(nextId);
|
||||||
saveAppData(data);
|
saveAppData(data);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue