mirror of
https://github.com/devonthesofa/obsidian-note-status.git
synced 2026-07-22 05:45:04 +00:00
Merge pull request #94 from devonthesofa/feature/default-status
feat: add support for default status on new notes
This commit is contained in:
commit
75f0619f1e
5 changed files with 86 additions and 2 deletions
|
|
@ -1,7 +1,8 @@
|
|||
import { PluginSettings } from "@/types/pluginSettings";
|
||||
import React from "react";
|
||||
import React, { useMemo } from "react";
|
||||
import { SettingItem } from "./SettingItem";
|
||||
import { FrontmatterMappingsSettings } from "./FrontmatterMappingsSettings";
|
||||
import { BaseNoteStatusService } from "@/core/noteStatusService";
|
||||
|
||||
export type Props = {
|
||||
settings: PluginSettings;
|
||||
|
|
@ -9,10 +10,51 @@ export type Props = {
|
|||
};
|
||||
|
||||
export const BehaviourSettings: React.FC<Props> = ({ settings, onChange }) => {
|
||||
const availableStatuses = useMemo(() => {
|
||||
return BaseNoteStatusService.getAllAvailableStatuses();
|
||||
}, [
|
||||
settings.templates,
|
||||
settings.customStatuses,
|
||||
settings.enabledTemplates,
|
||||
]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h3>Status tag</h3>
|
||||
|
||||
<SettingItem
|
||||
name="Default status for new notes"
|
||||
description="Automatically apply this status to newly created notes."
|
||||
>
|
||||
<select
|
||||
value={settings.defaultStatusForNewNotes || ""}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value;
|
||||
onChange(
|
||||
"defaultStatusForNewNotes",
|
||||
value === "" ? null : value,
|
||||
);
|
||||
}}
|
||||
>
|
||||
<option value="">None</option>
|
||||
{availableStatuses.map((status) => {
|
||||
const identifier =
|
||||
BaseNoteStatusService.formatStatusIdentifier({
|
||||
templateId: status.templateId,
|
||||
name: status.name,
|
||||
});
|
||||
const displayName = status.templateId
|
||||
? `${status.name} (${status.templateId})`
|
||||
: status.name;
|
||||
return (
|
||||
<option key={identifier} value={identifier}>
|
||||
{displayName}
|
||||
</option>
|
||||
);
|
||||
})}
|
||||
</select>
|
||||
</SettingItem>
|
||||
|
||||
<SettingItem
|
||||
name="Enable multiple statuses"
|
||||
description="Allow notes to have multiple statuses at the same time"
|
||||
|
|
|
|||
|
|
@ -68,4 +68,5 @@ export const DEFAULT_PLUGIN_SETTINGS: PluginSettings = {
|
|||
],
|
||||
enableNonMarkdownSync: false,
|
||||
nonMarkdownSyncPath: "_note-status-data.json",
|
||||
defaultStatusForNewNotes: null,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ export const SETTINGS_GROUPS: Record<SyncGroup, (keyof PluginSettings)[]> = {
|
|||
"singleStatusStorageMode",
|
||||
"strictStatuses",
|
||||
"enableStatusOverviewPopup",
|
||||
"defaultStatusForNewNotes",
|
||||
],
|
||||
storage: [
|
||||
"tagPrefix",
|
||||
|
|
|
|||
41
main.tsx
41
main.tsx
|
|
@ -3,7 +3,10 @@ import StatusBarIntegration from "integrations/status-bar/status-bar";
|
|||
import eventBus from "core/eventBus";
|
||||
import { PluginSettingIntegration } from "./integrations/settings/pluginSettings";
|
||||
import settingsService from "./core/settingsService";
|
||||
import { BaseNoteStatusService } from "./core/noteStatusService";
|
||||
import {
|
||||
BaseNoteStatusService,
|
||||
NoteStatusService,
|
||||
} from "./core/noteStatusService";
|
||||
import { StatusModalIntegration } from "./integrations/modals/statusModalIntegration";
|
||||
import ContextMenuIntegration from "./integrations/context-menu/contextMenuIntegration";
|
||||
import { FileExplorerIntegration } from "./integrations/file-explorer/file-explorer-integration";
|
||||
|
|
@ -240,6 +243,42 @@ export default class NoteStatusPlugin extends Plugin {
|
|||
"main-status-popup-setting-subscriptor",
|
||||
);
|
||||
|
||||
this.registerEvent(
|
||||
this.app.vault.on("create", async (file) => {
|
||||
if (
|
||||
file instanceof TFile &&
|
||||
file.extension === "md" &&
|
||||
settingsService.settings.defaultStatusForNewNotes
|
||||
) {
|
||||
// Wait a bit to ensure metadata is processed or templates are applied
|
||||
setTimeout(async () => {
|
||||
// Ensure file still exists
|
||||
if (!this.app.vault.getAbstractFileByPath(file.path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const noteStatusService = new NoteStatusService(file);
|
||||
// Populate statuses to see if it already has any
|
||||
noteStatusService.populateStatuses();
|
||||
|
||||
const groupedStatuses =
|
||||
noteStatusService.getStatusesByAllKeys();
|
||||
const hasStatuses = Object.values(groupedStatuses).some(
|
||||
(statuses) => statuses.length > 0,
|
||||
);
|
||||
|
||||
if (!hasStatuses) {
|
||||
await noteStatusService.addStatus(
|
||||
settingsService.settings.tagPrefix,
|
||||
settingsService.settings
|
||||
.defaultStatusForNewNotes as string,
|
||||
);
|
||||
}
|
||||
}, 500); // Increased delay to allow more time for templates
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
this.registerEvent(
|
||||
this.app.vault.on("rename", (file) => {
|
||||
if (file instanceof TFile) {
|
||||
|
|
|
|||
|
|
@ -84,5 +84,6 @@ export type PluginSettings = {
|
|||
syncGroups: SyncGroup[]; // Selected groups of settings to synchronize
|
||||
enableNonMarkdownSync: boolean; // Whether to sync non-markdown statuses to a vault file
|
||||
nonMarkdownSyncPath: string; // Path to the non-markdown sync file
|
||||
defaultStatusForNewNotes: string | null; // Default status to apply to new notes
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue