From 816a2fefe7b175a5ebb223801be3039189ff0b07 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Sun, 21 Nov 2021 17:56:49 +0100 Subject: [PATCH] #182 - Support default sort option --- package.json | 16 ++++++++++++++++ src/commands/Dashboard.ts | 8 +++++--- src/constants/settings.ts | 3 +++ .../components/Header/Sorting.tsx | 10 +++++++++- src/dashboardWebView/models/Settings.ts | 1 + src/models/SortingSetting.ts | 1 + 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 75b41184..6db7b906 100644 --- a/package.json +++ b/package.json @@ -182,6 +182,10 @@ "items": { "type": "object", "properties": { + "id": { + "type": "string", + "description": "The ID of the sorting option. This will be used for the storing the last used sorting option or the default option." + }, "title": { "type": "string", "description": "Name of the sorting label" @@ -217,6 +221,18 @@ }, "scope": "Content" }, + "frontMatter.content.defaultSorting": { + "type": "string", + "default": "", + "markdownDescription": "Specify the default sorting option for the content dashboard. [Check in the docs](https://frontmatter.codes/docs/settings#frontMatter.content.sorting.default)", + "scope": "Content" + }, + "frontMatter.media.defaultSorting": { + "type": "string", + "default": "", + "markdownDescription": "Specify the default sorting option for the media dashboard. [Check in the docs](https://frontmatter.codes/docs/settings#frontMatter.media.sorting.default)", + "scope": "Content" + }, "frontMatter.custom.scripts": { "type": "array", "default": [], diff --git a/src/commands/Dashboard.ts b/src/commands/Dashboard.ts index 3a9be624..f6b18760 100644 --- a/src/commands/Dashboard.ts +++ b/src/commands/Dashboard.ts @@ -1,4 +1,4 @@ -import { SETTINGS_CONTENT_STATIC_FOLDER, SETTING_DATE_FIELD, SETTING_SEO_DESCRIPTION_FIELD, SETTINGS_DASHBOARD_OPENONSTART, SETTINGS_DASHBOARD_MEDIA_SNIPPET, SETTING_TAXONOMY_CONTENT_TYPES, DefaultFields, HOME_PAGE_NAVIGATION_ID, ExtensionState, COMMAND_NAME, SETTINGS_FRAMEWORK_ID, SETTINGS_CONTENT_DRAFT_FIELD, SETTINGS_CONTENT_SORTING, CONTEXT, SETTING_CUSTOM_SCRIPTS } from '../constants'; +import { SETTINGS_CONTENT_STATIC_FOLDER, SETTING_DATE_FIELD, SETTING_SEO_DESCRIPTION_FIELD, SETTINGS_DASHBOARD_OPENONSTART, SETTINGS_DASHBOARD_MEDIA_SNIPPET, SETTING_TAXONOMY_CONTENT_TYPES, DefaultFields, HOME_PAGE_NAVIGATION_ID, ExtensionState, COMMAND_NAME, SETTINGS_FRAMEWORK_ID, SETTINGS_CONTENT_DRAFT_FIELD, SETTINGS_CONTENT_SORTING, CONTEXT, SETTING_CUSTOM_SCRIPTS, SETTINGS_CONTENT_SORTING_DEFAULT, SETTINGS_MEDIA_SORTING_DEFAULT } from '../constants'; import { ArticleHelper } from './../helpers/ArticleHelper'; import { basename, dirname, extname, join, parse } from "path"; import { existsSync, readdirSync, statSync, unlinkSync, writeFileSync } from "fs"; @@ -321,10 +321,12 @@ export class Dashboard { scripts: (SettingsHelper.get(SETTING_CUSTOM_SCRIPTS) || []).filter(s => s.type && s.type !== ScriptType.Article), dashboardState: { contents: { - sorting: await ext.getState(ExtensionState.Dashboard.Contents.Sorting, "workspace") + sorting: await ext.getState(ExtensionState.Dashboard.Contents.Sorting, "workspace"), + defaultSorting: SettingsHelper.get(SETTINGS_CONTENT_SORTING_DEFAULT) }, media: { - sorting: await ext.getState(ExtensionState.Dashboard.Media.Sorting, "workspace") + sorting: await ext.getState(ExtensionState.Dashboard.Media.Sorting, "workspace"), + defaultSorting: SettingsHelper.get(SETTINGS_MEDIA_SORTING_DEFAULT) } } } as Settings diff --git a/src/constants/settings.ts b/src/constants/settings.ts index b0a0f2e2..0192daed 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -42,6 +42,9 @@ export const SETTINGS_CONTENT_FRONTMATTER_HIGHLIGHT = "content.fmHighlight"; export const SETTINGS_CONTENT_DRAFT_FIELD = "content.draftField"; export const SETTINGS_CONTENT_SORTING = "content.sorting"; +export const SETTINGS_CONTENT_SORTING_DEFAULT = "content.defaultSorting"; +export const SETTINGS_MEDIA_SORTING_DEFAULT = "content.defaultSorting"; + export const SETTINGS_DASHBOARD_OPENONSTART = "dashboard.openOnStart"; export const SETTINGS_DASHBOARD_MEDIA_SNIPPET = "dashboard.mediaSnippet"; diff --git a/src/dashboardWebView/components/Header/Sorting.tsx b/src/dashboardWebView/components/Header/Sorting.tsx index cc1f4207..bd136ab7 100644 --- a/src/dashboardWebView/components/Header/Sorting.tsx +++ b/src/dashboardWebView/components/Header/Sorting.tsx @@ -42,7 +42,7 @@ export const Sorting: React.FunctionComponent = ({disableCustomSo allOptions = [...allOptions, ...settings.customSorting.map((s) => ({ title: s.title || s.name, name: s.name, - id: `${s.name}-${s.order}`, + id: s.id || `${s.name}-${s.order}`, order: s.order, type: s.type }))]; @@ -55,6 +55,14 @@ export const Sorting: React.FunctionComponent = ({disableCustomSo } else if (view === ViewType.Media) { crntSortingOption = settings?.dashboardState?.media?.sorting || null; } + + if (crntSortingOption === null) { + if (view === ViewType.Contents && settings?.dashboardState.contents.defaultSorting) { + crntSortingOption = allOptions.find(f => f.id === settings?.dashboardState.contents.defaultSorting) || null; + } else if (view === ViewType.Media && settings?.dashboardState.contents.defaultSorting) { + crntSortingOption = allOptions.find(f => f.id === settings?.dashboardState.contents.defaultSorting) || null; + } + } } let crntSort = allOptions.find(x => x.id === crntSortingOption?.id) || sortOptions[0]; diff --git a/src/dashboardWebView/models/Settings.ts b/src/dashboardWebView/models/Settings.ts index 1acd6d75..65120692 100644 --- a/src/dashboardWebView/models/Settings.ts +++ b/src/dashboardWebView/models/Settings.ts @@ -33,4 +33,5 @@ export interface DashboardState { export interface ViewState { sorting: SortingOption | null | undefined; + defaultSorting: string | null | undefined; } \ No newline at end of file diff --git a/src/models/SortingSetting.ts b/src/models/SortingSetting.ts index e0c5e6c9..8a82455b 100644 --- a/src/models/SortingSetting.ts +++ b/src/models/SortingSetting.ts @@ -5,4 +5,5 @@ export interface SortingSetting { name: string; order: SortOrder; type: SortType; + id?: string; } \ No newline at end of file