diff --git a/src/commands/Dashboard.ts b/src/commands/Dashboard.ts index 3fa1abed..6ebda4df 100644 --- a/src/commands/Dashboard.ts +++ b/src/commands/Dashboard.ts @@ -10,12 +10,13 @@ import { DashboardCommand } from '../pagesView/DashboardCommand'; import { DashboardMessage } from '../pagesView/DashboardMessage'; import { Page } from '../pagesView/models/Page'; import { openFileInEditor } from '../helpers/openFileInEditor'; -import { COMMAND_NAME } from '../constants/Extension'; +import { COMMAND_NAME, EXTENSION_STATE_PAGES_VIEW } from '../constants/Extension'; import { Template } from './Template'; import { Notifications } from '../helpers/Notifications'; import { Settings } from '../pagesView/models/Settings'; import { Extension } from '../helpers/Extension'; import { parseJSON } from 'date-fns'; +import { ViewType } from '../pagesView/state'; export class Dashboard { @@ -114,10 +115,10 @@ export class Dashboard { case DashboardMessage.updateSetting: Dashboard.updateSetting(msg.data); break; - case DashboardMessage.InitializeProject: + case DashboardMessage.initializeProject: await commands.executeCommand(COMMAND_NAME.init, Dashboard.getSettings); break; - case DashboardMessage.Reload: + case DashboardMessage.reload: if (!Dashboard.isDisposed) { Dashboard.webview?.dispose(); setTimeout(() => { @@ -125,6 +126,9 @@ export class Dashboard { }, 100); } break; + case DashboardMessage.setPageViewType: + Extension.getInstance().setState(EXTENSION_STATE_PAGES_VIEW, msg.data); + break; } }); } @@ -133,6 +137,8 @@ export class Dashboard { * Retrieve the settings for the dashboard */ private static async getSettings() { + const ext = Extension.getInstance(); + Dashboard.postWebviewMessage({ command: DashboardCommand.settings, data: { @@ -141,7 +147,8 @@ export class Dashboard { tags: SettingsHelper.getTaxonomy(TaxonomyType.Tag), categories: SettingsHelper.getTaxonomy(TaxonomyType.Category), openOnStart: SettingsHelper.getConfig().get(SETTINGS_DASHBOARD_OPENONSTART), - versionInfo: Extension.getInstance().getVersion() + versionInfo: ext.getVersion(), + pageViewType: await ext.getState(EXTENSION_STATE_PAGES_VIEW) } as Settings }); } @@ -202,7 +209,7 @@ export class Dashboard { pages.push(page); } - } catch (error) { + } catch (error: any) { Notifications.error(`File error: ${file.filePath} - ${error?.message || error}`); } } diff --git a/src/constants/Extension.ts b/src/constants/Extension.ts index 94badb96..3fbb60cb 100644 --- a/src/constants/Extension.ts +++ b/src/constants/Extension.ts @@ -1,7 +1,9 @@ const extensionName = "frontMatter"; export const EXTENSION_ID = 'eliostruyf.vscode-front-matter'; + export const EXTENSION_STATE_VERSION = 'frontMatter:Version'; +export const EXTENSION_STATE_PAGES_VIEW = 'frontMatter:Pages:ViewType'; export const getCommandName = (command: string) => { return `${extensionName}.${command}`; diff --git a/src/helpers/Extension.ts b/src/helpers/Extension.ts index 9c8a922b..655a93bd 100644 --- a/src/helpers/Extension.ts +++ b/src/helpers/Extension.ts @@ -73,4 +73,12 @@ export class Extension { await config.update(`${SETTINGS_CONTENT_PAGE_FOLDERS}`, paths); } } + + public async setState(propKey: string, propValue: string): Promise { + await this.globalState.update(propKey, propValue); + } + + public async getState(propKey: string): Promise { + return await this.globalState.get(propKey); + } } \ No newline at end of file diff --git a/src/pagesView/DashboardMessage.ts b/src/pagesView/DashboardMessage.ts index 9ccdac81..2c923a42 100644 --- a/src/pagesView/DashboardMessage.ts +++ b/src/pagesView/DashboardMessage.ts @@ -4,6 +4,7 @@ export enum DashboardMessage { getTheme = 'getTheme', createContent = 'createContent', updateSetting = 'updateSetting', - InitializeProject = 'InitializeProject', - Reload = 'Reload', + initializeProject = 'initializeProject', + reload = 'reload', + setPageViewType = 'setPageViewType', } \ No newline at end of file diff --git a/src/pagesView/components/Dashboard.tsx b/src/pagesView/components/Dashboard.tsx index ff00256e..1a024005 100644 --- a/src/pagesView/components/Dashboard.tsx +++ b/src/pagesView/components/Dashboard.tsx @@ -43,7 +43,7 @@ export const Dashboard: React.FunctionComponent = ({showWelcome { loading ? : } - + ); diff --git a/src/pagesView/components/Header/Header.tsx b/src/pagesView/components/Header/Header.tsx index 248b2b46..e992283a 100644 --- a/src/pagesView/components/Header/Header.tsx +++ b/src/pagesView/components/Header/Header.tsx @@ -49,7 +49,7 @@ export const Header: React.FunctionComponent = ({totalPages, folde -
+
setCrntTag(value)} /> diff --git a/src/pagesView/components/Header/ViewSwitch.tsx b/src/pagesView/components/Header/ViewSwitch.tsx index be79633f..2067a4c3 100644 --- a/src/pagesView/components/Header/ViewSwitch.tsx +++ b/src/pagesView/components/Header/ViewSwitch.tsx @@ -1,20 +1,29 @@ import * as React from 'react'; -import { useRecoilState } from 'recoil'; +import { useRecoilState, useRecoilValue } from 'recoil'; import { ViewAtom, ViewType } from '../../state'; import { ViewGridIcon, ViewListIcon } from '@heroicons/react/solid'; -import { STORAGE_VIEW_TYPE } from '../../constants/Storage'; +import { SettingsAtom } from '../../state/atom/SettingsAtom'; +import { MessageHelper } from '../../../helpers/MessageHelper'; +import { DashboardMessage } from '../../DashboardMessage'; export interface IViewSwitchProps {} export const ViewSwitch: React.FunctionComponent = (props: React.PropsWithChildren) => { const [ view, setView ] = useRecoilState(ViewAtom); - + const settings = useRecoilValue(SettingsAtom); + const toggleView = () => { const newView = view === ViewType.Grid ? ViewType.List : ViewType.Grid; - window.localStorage.setItem(STORAGE_VIEW_TYPE, newView.toString()); setView(newView); + MessageHelper.sendMessage(DashboardMessage.setPageViewType, newView); }; + React.useEffect(() => { + if (settings?.pageViewType) { + setView(settings?.pageViewType); + } + }, [settings?.pageViewType]); + return (