From 19ef005f84a786214943a4beea9da69098221bfc Mon Sep 17 00:00:00 2001 From: DecafDev <40307803+decaf-dev@users.noreply.github.com> Date: Tue, 10 Sep 2024 00:50:41 -0600 Subject: [PATCH] Update default values (#327) * feat: add default value to properties * feat: rename to File system * refactor: update value * chore: update guardgen --- src/constants.ts | 7 ++-- src/obsidian/utils.ts | 39 ++++++++++++----- src/obsidian/vault-explorer-settings-tab.ts | 42 ++++++++----------- src/svelte/app/index.svelte | 8 ---- src/types/index.guard.ts | 1 - src/types/index.ts | 1 - .../plugins/vault-explorer/data.json | 4 +- 7 files changed, 50 insertions(+), 52 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 569796c..8075c94 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -7,10 +7,9 @@ export const HOVER_LINK_SOURCE_ID = "vault-explorer-preview"; export const DEFAULT_SETTINGS: VaultExplorerPluginSettings = { properties: { - favorite: "", - url: "", - image: "", - coverImageFit: "", + url: "url", + image: "image", + coverImageFit: "image-fit", createdDate: "", modifiedDate: "", custom1: "", diff --git a/src/obsidian/utils.ts b/src/obsidian/utils.ts index 07f2a01..07f1dfc 100644 --- a/src/obsidian/utils.ts +++ b/src/obsidian/utils.ts @@ -1,4 +1,4 @@ -import { App } from "obsidian" +import { App } from "obsidian"; import { ObsidianProperty, ObsidianPropertyType } from "./types"; /** @@ -6,17 +6,34 @@ import { ObsidianProperty, ObsidianPropertyType } from "./types"; * NOTE: This is an undocumented API function and may break in future versions of Obsidian */ export const getAllObsidianProperties = (app: App) => { - const properties = (app as any).metadataTypeManager.getAllProperties() as Record; - return Object.values(properties).sort((a: ObsidianProperty, b: ObsidianProperty) => a.name.localeCompare(b.name)); + const properties = ( + app as any + ).metadataTypeManager.getAllProperties() as Record< + string, + ObsidianProperty + >; + return Object.values(properties).sort( + (a: ObsidianProperty, b: ObsidianProperty) => + a.name.localeCompare(b.name) + ); }; -export const getObsidianPropertiesByType = (app: App, type: ObsidianPropertyType) => { +export const getObsidianPropertiesByType = ( + app: App, + type: ObsidianPropertyType +) => { return getAllObsidianProperties(app).filter((p) => p.type === type); -} +}; -export const getDropdownOptionsForProperties = (properties: ObsidianProperty[]) => { - return properties.reduce((acc: Record, cur: ObsidianProperty) => { - acc[cur.name] = cur.name; - return acc; - }, { "": "Select a property" }); -} +export const getDropdownOptionsForProperties = ( + properties: ObsidianProperty[], + defaultValue: Record = { "": "Select a property" } +) => { + return properties.reduce( + (acc: Record, cur: ObsidianProperty) => { + acc[cur.name] = cur.name; + return acc; + }, + defaultValue + ); +}; diff --git a/src/obsidian/vault-explorer-settings-tab.ts b/src/obsidian/vault-explorer-settings-tab.ts index 6f7f687..1d8425b 100644 --- a/src/obsidian/vault-explorer-settings-tab.ts +++ b/src/obsidian/vault-explorer-settings-tab.ts @@ -446,26 +446,6 @@ export default class VaultExplorerSettingsTab extends PluginSettingTab { new Setting(containerEl).setName("Built-in properties").setHeading(); - new Setting(containerEl) - .setName("Favorite property") - .setDesc( - "Property used to mark a note as a favorite. This must be a checkbox property." - ) - .addDropdown((dropdown) => - dropdown - .addOptions( - getDropdownOptionsForProperties(checkboxProperties) - ) - .setValue(this.plugin.settings.properties.favorite) - .onChange(async (value) => { - this.plugin.settings.properties.favorite = value; - await this.plugin.saveSettings(); - EventManager.getInstance().emit( - PluginEvent.PROPERTY_SETTING_CHANGE - ); - }) - ); - new Setting(containerEl) .setName("Cover image property") .setDesc( @@ -473,7 +453,11 @@ export default class VaultExplorerSettingsTab extends PluginSettingTab { ) .addDropdown((dropdown) => dropdown - .addOptions(getDropdownOptionsForProperties(textProperties)) + .addOptions( + getDropdownOptionsForProperties(textProperties, { + image: "image", + }) + ) .setValue(this.plugin.settings.properties.image) .onChange(async (value) => { this.plugin.settings.properties.image = value; @@ -491,7 +475,11 @@ export default class VaultExplorerSettingsTab extends PluginSettingTab { ) .addDropdown((dropdown) => dropdown - .addOptions(getDropdownOptionsForProperties(textProperties)) + .addOptions( + getDropdownOptionsForProperties(textProperties, { + "image-fit": "image-fit", + }) + ) .setValue(this.plugin.settings.properties.coverImageFit) .onChange(async (value) => { this.plugin.settings.properties.coverImageFit = value; @@ -509,7 +497,11 @@ export default class VaultExplorerSettingsTab extends PluginSettingTab { ) .addDropdown((dropdown) => dropdown - .addOptions(getDropdownOptionsForProperties(textProperties)) + .addOptions( + getDropdownOptionsForProperties(textProperties, { + url: "url", + }) + ) .setValue(this.plugin.settings.properties.url) .onChange(async (value) => { this.plugin.settings.properties.url = value; @@ -525,7 +517,7 @@ export default class VaultExplorerSettingsTab extends PluginSettingTab { text: "Property used to store a creation date. This must be a date or datetime property.", }); creationDateDesc.createDiv({ - text: "If set to 'Select a property', the file's created at date will be used.", + text: "If set, the property will be preferred over the file's creation date.", }); new Setting(containerEl) @@ -554,7 +546,7 @@ export default class VaultExplorerSettingsTab extends PluginSettingTab { text: "Property used to store a modification date. This must be a date or datetime property.", }); modificationDateDesc.createDiv({ - text: "If set to 'Select a property', the file's modified at date will be used.", + text: "If set, the property will be preferred over the file's modification date.", }); new Setting(containerEl) diff --git a/src/svelte/app/index.svelte b/src/svelte/app/index.svelte index a98eaff..999c333 100644 --- a/src/svelte/app/index.svelte +++ b/src/svelte/app/index.svelte @@ -754,14 +754,6 @@ const { properties } = plugin.settings; const { coverImageFit: coverImageFitProperty } = properties; - //If the favorite property is not set, return - if (coverImageFitProperty === "") { - new Notice( - "Vault Explorer: Please set a cover image fit property in the plugin settings to use this feature", - ); - return; - } - const file = plugin.app.vault.getFileByPath(filePath); if (!file) { Logger.error({ diff --git a/src/types/index.guard.ts b/src/types/index.guard.ts index 1f44294..e0bee03 100644 --- a/src/types/index.guard.ts +++ b/src/types/index.guard.ts @@ -13,7 +13,6 @@ export function isVaultExplorerPluginSettings(obj: unknown): obj is VaultExplore (typedObj["properties"] !== null && typeof typedObj["properties"] === "object" || typeof typedObj["properties"] === "function") && - typeof typedObj["properties"]["favorite"] === "string" && typeof typedObj["properties"]["url"] === "string" && typeof typedObj["properties"]["image"] === "string" && typeof typedObj["properties"]["coverImageFit"] === "string" && diff --git a/src/types/index.ts b/src/types/index.ts index fd3b80f..5e53d50 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,7 +1,6 @@ /** @see {isVaultExplorerPluginSettings} ts-auto-guard:type-guard */ export interface VaultExplorerPluginSettings { properties: { - favorite: string; url: string; image: string; coverImageFit: string; diff --git a/test-vault/.obsidian/plugins/vault-explorer/data.json b/test-vault/.obsidian/plugins/vault-explorer/data.json index 91db4cf..d48786c 100644 --- a/test-vault/.obsidian/plugins/vault-explorer/data.json +++ b/test-vault/.obsidian/plugins/vault-explorer/data.json @@ -9,7 +9,7 @@ "modifiedDate": "modification", "createdDate": "creation", "image": "image", - "coverImageFit": "" + "coverImageFit": "image-fit" }, "filters": { "search": { @@ -18,7 +18,7 @@ }, "sort": { "isEnabled": true, - "value": "file-name-asc" + "value": "modified-desc" }, "custom": { "isEnabled": true,