devonthesofa_obsidian-note-.../core/noteStatusService.ts

474 lines
11 KiB
TypeScript
Raw Normal View History

2025-07-15 15:30:17 +00:00
import { App, TFile } from "obsidian";
import {
GroupedStatuses,
NoteStatus,
NoteStatus as NoteStatusType,
2025-07-20 07:39:50 +00:00
StatusIdentifier,
ScopedStatusName,
2025-07-15 15:30:17 +00:00
} from "@/types/noteStatus";
import settingsService from "@/core/settingsService";
import eventBus from "./eventBus";
2025-11-21 11:51:08 +00:00
import statusStoreManager from "./statusStoreManager";
import type { StatusStore } from "./statusStores/types";
2025-07-15 15:30:17 +00:00
export abstract class BaseNoteStatusService {
static app: App;
statuses: GroupedStatuses;
constructor() {
this.statuses = {};
}
static initialize(app: App) {
BaseNoteStatusService.app = app;
}
2025-07-20 07:39:50 +00:00
static parseStatusIdentifier(
identifier: StatusIdentifier,
): ScopedStatusName {
if (typeof identifier === "string") {
if (identifier.includes(":")) {
const [templateId, name] = identifier.split(":", 2);
return { templateId, name };
}
return { name: identifier };
}
return identifier;
}
static formatStatusIdentifier(scopedName: ScopedStatusName): string {
if (scopedName.templateId) {
return `${scopedName.templateId}:${scopedName.name}`;
}
return scopedName.name;
}
static resolveStatusFromIdentifier(
identifier: StatusIdentifier,
): NoteStatus | undefined {
const parsed = BaseNoteStatusService.parseStatusIdentifier(identifier);
const availableStatuses =
BaseNoteStatusService.getAllAvailableStatuses();
if (parsed.templateId) {
return availableStatuses.find(
(s) =>
s.name === parsed.name &&
s.templateId === parsed.templateId,
);
}
return availableStatuses.find((s) => s.name === parsed.name);
}
2025-07-15 15:30:17 +00:00
private static allEnabledTemplatesStatuses(): NoteStatusType[] {
if (settingsService.settings.useCustomStatusesOnly) {
return [];
}
const enabledTemplates = settingsService.settings.templates.filter(
(template) =>
settingsService.settings.enabledTemplates.includes(template.id),
2025-07-15 15:30:17 +00:00
);
return enabledTemplates.flatMap((t) => t.statuses);
}
static getAllAvailableStatuses(): NoteStatus[] {
const availableStatuses = [
...settingsService.settings.customStatuses,
...BaseNoteStatusService.allEnabledTemplatesStatuses(),
];
return availableStatuses;
}
// static async insertStatusMetadataInEditor(file: TFile): Promise<void> {
// const tagPrefix = settingsService.settings.tagPrefix;
//
// await BaseNoteStatusService.app.fileManager.processFrontMatter(
// file,
// (frontmatter) => {
// const noteStatusFrontmatter =
// (frontmatter?.[tagPrefix] as string[]) || [];
// if (!noteStatusFrontmatter.length) {
// frontmatter[tagPrefix] = [];
// }
// },
// );
// }
2025-07-15 15:30:17 +00:00
2025-07-20 07:39:50 +00:00
protected statusNameToObject(statusName: string | StatusIdentifier) {
if (typeof statusName === "string") {
return BaseNoteStatusService.resolveStatusFromIdentifier(
statusName,
);
}
return BaseNoteStatusService.resolveStatusFromIdentifier(statusName);
2025-07-15 15:30:17 +00:00
}
protected getStatusMetadataKeys(): string[] {
return [settingsService.settings.tagPrefix];
}
abstract populateStatuses(): void;
abstract removeStatus(
frontmatterTagName: string,
status: NoteStatus,
): Promise<boolean>;
abstract addStatus(
frontmatterTagName: string,
2025-07-20 07:39:50 +00:00
statusIdentifier: StatusIdentifier,
2025-07-15 15:30:17 +00:00
): Promise<boolean>;
}
export class NoteStatusService extends BaseNoteStatusService {
private file: TFile;
2025-11-21 11:51:08 +00:00
private statusStore: StatusStore;
2025-07-15 15:30:17 +00:00
constructor(file: TFile) {
super();
this.file = file;
2025-11-21 11:51:08 +00:00
this.statusStore = statusStoreManager.getStoreForFile(file);
2025-07-15 15:30:17 +00:00
}
private shouldStoreStatusesAsArray(): boolean {
if (settingsService.settings.useMultipleStatuses) {
return true;
}
const mode = settingsService.settings.singleStatusStorageMode || "list";
return mode === "list";
}
2025-11-21 11:51:08 +00:00
private getStoreOptions() {
return { storeAsArray: this.shouldStoreStatusesAsArray() };
}
2025-11-21 11:51:08 +00:00
private statusesAreEqual(a: string[], b: string[]): boolean {
if (a.length !== b.length) return false;
return a.every((value, index) => value === b[index]);
}
2025-07-15 15:30:17 +00:00
public populateStatuses(): void {
const STATUS_METADATA_KEYS = this.getStatusMetadataKeys();
this.statuses = Object.fromEntries(
STATUS_METADATA_KEYS.map((key) => [key, []]),
);
if (!this.file) {
return;
}
STATUS_METADATA_KEYS.forEach((key) => {
2025-11-21 11:51:08 +00:00
const identifiers = this.statusStore.getStatuses(this.file, key);
if (!identifiers.length) {
return;
}
2025-07-15 15:30:17 +00:00
2025-11-21 11:51:08 +00:00
let statuses = identifiers
.map((identifier) =>
this.statusNameToObject(identifier.toString()),
)
.filter(
(status): status is NoteStatusType => status !== undefined,
);
if (
statuses.length &&
!settingsService.settings.useMultipleStatuses
) {
statuses = statuses.slice(0, 1);
2025-07-15 15:30:17 +00:00
}
2025-11-21 11:51:08 +00:00
this.statuses[key] = statuses;
2025-07-15 15:30:17 +00:00
});
}
async removeStatus(
frontmatterTagName: string,
status: NoteStatus,
): Promise<boolean> {
2025-07-20 07:39:50 +00:00
const targetIdentifier = status.templateId
? BaseNoteStatusService.formatStatusIdentifier({
templateId: status.templateId,
name: status.name,
})
: status.name;
2025-11-21 11:51:08 +00:00
const removed = await this.statusStore.mutateStatuses(
2025-07-15 15:30:17 +00:00
this.file,
2025-11-21 11:51:08 +00:00
frontmatterTagName,
(current) => {
if (!current.length) {
return { hasChanged: false };
}
2025-11-21 11:51:08 +00:00
let i = current.findIndex(
(statusName: string) => statusName === targetIdentifier,
);
if (i === -1 && status.templateId) {
2025-11-21 11:51:08 +00:00
i = current.findIndex(
(statusName: string) => statusName === status.name,
2025-07-15 15:30:17 +00:00
);
}
2025-07-20 08:32:41 +00:00
2025-11-21 11:51:08 +00:00
if (i === -1) {
return { hasChanged: false };
2025-07-15 15:30:17 +00:00
}
2025-11-21 11:51:08 +00:00
const nextStatuses = [...current];
nextStatuses.splice(i, 1);
return { hasChanged: true, nextStatuses };
2025-07-15 15:30:17 +00:00
},
2025-11-21 11:51:08 +00:00
this.getStoreOptions(),
2025-07-15 15:30:17 +00:00
);
if (removed) {
2025-11-21 11:51:08 +00:00
eventBus.publish("status-changed", {
2025-07-15 15:30:17 +00:00
file: this.file,
});
}
return removed;
}
async clearStatus(frontmatterTagName: string): Promise<boolean> {
2025-11-21 11:51:08 +00:00
const cleared = await this.statusStore.mutateStatuses(
2025-07-15 15:30:17 +00:00
this.file,
2025-11-21 11:51:08 +00:00
frontmatterTagName,
(current) => {
if (!current.length) {
return { hasChanged: false };
2025-07-15 15:30:17 +00:00
}
2025-11-21 11:51:08 +00:00
return { hasChanged: true, nextStatuses: [] };
2025-07-15 15:30:17 +00:00
},
2025-11-21 11:51:08 +00:00
this.getStoreOptions(),
2025-07-15 15:30:17 +00:00
);
2025-11-21 11:51:08 +00:00
if (cleared) {
eventBus.publish("status-changed", {
file: this.file,
});
}
return cleared;
2025-07-15 15:30:17 +00:00
}
async overrideStatuses(
frontmatterTagName: string,
2025-07-20 07:39:50 +00:00
statusIdentifiers: StatusIdentifier[],
2025-07-15 15:30:17 +00:00
): Promise<boolean> {
2025-07-20 07:39:50 +00:00
const formattedIdentifiers = statusIdentifiers.map((id) =>
typeof id === "string"
? id
: BaseNoteStatusService.formatStatusIdentifier(id),
);
2025-11-21 11:51:08 +00:00
const statusesToStore = settingsService.settings.useMultipleStatuses
? formattedIdentifiers
: formattedIdentifiers.slice(0, 1);
const overridden = await this.statusStore.mutateStatuses(
2025-07-15 15:30:17 +00:00
this.file,
2025-11-21 11:51:08 +00:00
frontmatterTagName,
(current) => {
if (this.statusesAreEqual(current, statusesToStore)) {
return { hasChanged: false };
}
return { hasChanged: true, nextStatuses: statusesToStore };
2025-07-15 15:30:17 +00:00
},
2025-11-21 11:51:08 +00:00
this.getStoreOptions(),
2025-07-15 15:30:17 +00:00
);
2025-11-21 11:51:08 +00:00
if (overridden) {
eventBus.publish("status-changed", {
file: this.file,
});
}
return overridden;
2025-07-15 15:30:17 +00:00
}
async addStatus(
frontmatterTagName: string,
2025-07-20 07:39:50 +00:00
statusIdentifier: StatusIdentifier,
2025-07-15 15:30:17 +00:00
): Promise<boolean> {
2025-07-20 07:39:50 +00:00
const formattedIdentifier =
typeof statusIdentifier === "string"
? statusIdentifier
: BaseNoteStatusService.formatStatusIdentifier(
statusIdentifier,
);
2025-11-21 11:51:08 +00:00
const added = await this.statusStore.mutateStatuses(
2025-07-15 15:30:17 +00:00
this.file,
2025-11-21 11:51:08 +00:00
frontmatterTagName,
(current) => {
2025-07-15 15:30:17 +00:00
if (!settingsService.settings.useMultipleStatuses) {
const newValue = [formattedIdentifier];
2025-11-21 11:51:08 +00:00
if (this.statusesAreEqual(current, newValue)) {
return { hasChanged: false };
}
return { hasChanged: true, nextStatuses: newValue };
}
const exactMatch = current.findIndex(
(statusName: string) => statusName === formattedIdentifier,
);
if (exactMatch !== -1) {
return { hasChanged: false };
}
let legacyIndex = -1;
if (
typeof statusIdentifier !== "string" &&
statusIdentifier.templateId
) {
legacyIndex = current.findIndex(
2025-07-20 07:39:50 +00:00
(statusName: string) =>
2025-11-21 11:51:08 +00:00
statusName === statusIdentifier.name,
2025-07-15 15:30:17 +00:00
);
2025-11-21 11:51:08 +00:00
}
2025-07-20 08:32:41 +00:00
2025-11-21 11:51:08 +00:00
const nextStatuses = [...current];
if (legacyIndex !== -1) {
nextStatuses[legacyIndex] = formattedIdentifier;
} else {
nextStatuses.push(formattedIdentifier);
2025-07-15 15:30:17 +00:00
}
2025-11-21 11:51:08 +00:00
return { hasChanged: true, nextStatuses };
2025-07-15 15:30:17 +00:00
},
2025-11-21 11:51:08 +00:00
this.getStoreOptions(),
2025-07-15 15:30:17 +00:00
);
2025-11-21 11:51:08 +00:00
2025-07-15 15:30:17 +00:00
if (added) {
2025-11-21 11:51:08 +00:00
eventBus.publish("status-changed", {
2025-07-15 15:30:17 +00:00
file: this.file,
});
}
return added;
}
}
export class MultipleNoteStatusService extends BaseNoteStatusService {
private files: TFile[];
constructor(files: TFile[]) {
super();
this.files = files;
}
selectedFilesQTY() {
return this.files.length;
}
public populateStatuses(): void {
const STATUS_METADATA_KEYS = this.getStatusMetadataKeys();
this.statuses = Object.fromEntries(
STATUS_METADATA_KEYS.map((key) => [key, []]),
);
const allStatuses = new Map<string, Set<string>>();
this.files.forEach((file) => {
2025-11-21 11:51:08 +00:00
let store: StatusStore;
try {
store = statusStoreManager.getStoreForFile(file);
} catch {
return;
}
2025-07-15 15:30:17 +00:00
STATUS_METADATA_KEYS.forEach((key) => {
2025-11-21 11:51:08 +00:00
const identifiers = store.getStatuses(file, key);
if (!identifiers.length) return;
2025-07-15 15:30:17 +00:00
2025-11-21 11:51:08 +00:00
if (!allStatuses.has(key)) {
allStatuses.set(key, new Set());
2025-07-15 15:30:17 +00:00
}
2025-11-21 11:51:08 +00:00
identifiers.forEach((name) =>
allStatuses.get(key)!.add(name.toString()),
);
2025-07-15 15:30:17 +00:00
});
});
STATUS_METADATA_KEYS.forEach((key) => {
const statusNames = allStatuses.get(key);
if (statusNames) {
const statuses = Array.from(statusNames)
.map((name) => this.statusNameToObject(name))
.filter((s) => s !== undefined) as NoteStatusType[];
this.statuses[key] = statuses;
}
});
}
async removeStatus(
frontmatterTagName: string,
status: NoteStatus,
): Promise<boolean> {
const removalResults = await Promise.all(
this.files.map(async (file) => {
const noteStatusService = new NoteStatusService(file);
return noteStatusService.removeStatus(
frontmatterTagName,
status,
);
}),
);
2025-07-15 15:30:17 +00:00
const removedFromAny = removalResults.some((result) => result);
2025-07-15 15:30:17 +00:00
if (removedFromAny) {
this.populateStatuses();
2025-07-15 15:30:17 +00:00
}
return removedFromAny;
}
async addStatus(
frontmatterTagName: string,
2025-07-20 07:39:50 +00:00
statusIdentifier: StatusIdentifier,
2025-07-15 15:30:17 +00:00
): Promise<boolean> {
const results = await Promise.all(
this.files.map(async (file) => {
const noteStatusService = new NoteStatusService(file);
return noteStatusService.addStatus(
frontmatterTagName,
statusIdentifier,
);
}),
);
2025-07-15 15:30:17 +00:00
const addedToAny = results.some((result) => result);
2025-07-15 15:30:17 +00:00
if (addedToAny) {
this.populateStatuses();
2025-07-15 15:30:17 +00:00
}
return addedToAny;
}
getFilesWithStatus(
frontmatterTagName: string,
status: NoteStatus,
): TFile[] {
2025-07-20 07:39:50 +00:00
const targetIdentifier = status.templateId
? BaseNoteStatusService.formatStatusIdentifier({
templateId: status.templateId,
name: status.name,
})
: status.name;
2025-07-15 15:30:17 +00:00
return this.files.filter((file) => {
2025-11-21 11:51:08 +00:00
let store: StatusStore;
try {
store = statusStoreManager.getStoreForFile(file);
} catch {
return false;
2025-07-15 15:30:17 +00:00
}
2025-11-21 11:51:08 +00:00
const identifiers = store.getStatuses(file, frontmatterTagName);
if (!identifiers.length) return false;
return identifiers.some(
(identifier) =>
identifier === targetIdentifier ||
(status.templateId && identifier === status.name),
2025-07-20 08:32:41 +00:00
);
2025-07-15 15:30:17 +00:00
});
}
}