diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb6f89d..956817a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ - [#330](https://github.com/estruyf/vscode-front-matter/issues/330): Allow custom scripts to easily update front matter - [#331](https://github.com/estruyf/vscode-front-matter/issues/331): Added functionality to run other type of scripts - [#333](https://github.com/estruyf/vscode-front-matter/issues/333): Automatically mark Jekyll posts in `_drafts` folder as draft -- [#335](https://github.com/estruyf/vscode-front-matter/issues/331): Merge media snippets with content snippets to allow you to define multiple media snippets and use these in your content +- [#335](https://github.com/estruyf/vscode-front-matter/issues/335): Merge media snippets with content snippets to allow you to define multiple media snippets and use these in your content +- [#336](https://github.com/estruyf/vscode-front-matter/issues/336): Support added for inverting the draft field so that SSGs/authors can use a published field instead ### ⚡️ Optimizations diff --git a/package.json b/package.json index a05878e0..c1287f4f 100644 --- a/package.json +++ b/package.json @@ -137,6 +137,10 @@ "type": "string", "description": "Name of the field to use" }, + "invert": { + "type": "boolean", + "description": "By default the draft field is set to true when the content is a draft. Set this to true to set it to false." + }, "choices": { "type": "array", "description": "List of choices for the field", diff --git a/src/dashboardWebView/components/Contents/Item.tsx b/src/dashboardWebView/components/Contents/Item.tsx index b993f84e..447cf452 100644 --- a/src/dashboardWebView/components/Contents/Item.tsx +++ b/src/dashboardWebView/components/Contents/Item.tsx @@ -9,14 +9,16 @@ import { Status } from '../Status'; import { Messenger } from '@estruyf/vscode/dist/client'; import { DashboardViewType } from '../../models'; import { ContentActions } from './ContentActions'; +import { useMemo } from 'react'; export interface IItemProps extends Page {} const PREVIEW_IMAGE_FIELD = 'fmPreviewImage'; -export const Item: React.FunctionComponent = ({ fmFilePath, date, title, draft, description, type, ...pageData }: React.PropsWithChildren) => { +export const Item: React.FunctionComponent = ({ fmFilePath, date, title, description, type, ...pageData }: React.PropsWithChildren) => { const view = useRecoilValue(ViewSelector); const settings = useRecoilValue(SettingsSelector); + const draftField = useMemo(() => settings?.draftField, [settings]); const openFile = () => { Messenger.send(DashboardMessage.openFile, fmFilePath); @@ -51,7 +53,7 @@ export const Item: React.FunctionComponent = ({ fmFilePath, date, ti
- + { draftField && draftField.name && } @@ -96,7 +98,7 @@ export const Item: React.FunctionComponent = ({ fmFilePath, date, ti
- + { draftField && draftField.name && }
diff --git a/src/dashboardWebView/components/Status.tsx b/src/dashboardWebView/components/Status.tsx index 02d51346..c598ea52 100644 --- a/src/dashboardWebView/components/Status.tsx +++ b/src/dashboardWebView/components/Status.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import { useMemo } from 'react'; import { useRecoilValue } from 'recoil'; import { SettingsAtom } from '../state'; @@ -8,16 +9,30 @@ export interface IStatusProps { export const Status: React.FunctionComponent = ({draft}: React.PropsWithChildren) => { const settings = useRecoilValue(SettingsAtom); + const draftField = useMemo(() => settings?.draftField, [settings]); + const draftValue = useMemo(() => { + if (draftField && draftField.type === 'choice') { + return draft; + } else if (draftField && typeof draftField.invert !== 'undefined' && draftField.invert) { + return !draft; + } else { + return draft; + } + }, [draftField]); + + console.log('draftField', draftField, draft, draftValue); if (settings?.draftField && settings.draftField.type === "choice") { - if (draft) { - return {draft}; + if (draftValue) { + return {draftValue}; } else { return null; } } return ( - {draft ? "Draft" : "Published"} + + {draftValue ? "Draft" : "Published"} + ); }; \ No newline at end of file diff --git a/src/dashboardWebView/hooks/usePages.tsx b/src/dashboardWebView/hooks/usePages.tsx index df822e17..fbfd96f2 100644 --- a/src/dashboardWebView/hooks/usePages.tsx +++ b/src/dashboardWebView/hooks/usePages.tsx @@ -64,15 +64,19 @@ export default function usePages(pages: Page[]) { pagesToShow = searchedPages; } } else { + // Draft field is a boolean field const draftFieldName = draftField?.name || "draft"; + + const drafts = pagesToShow.filter(page => page[draftFieldName] == true || page[draftFieldName] === "true"); + const published = pagesToShow.filter(page => page[draftFieldName] == false || page[draftFieldName] === "false" || typeof page[draftFieldName] === "undefined"); - draftTypes[Tab.Draft] = pagesToShow.filter(page => page[draftFieldName] === true || page[draftFieldName] === "true").length; - draftTypes[Tab.Published] = pagesToShow.filter(page => page[draftFieldName] === false || page[draftFieldName] === "false").length; + draftTypes[Tab.Draft] = draftField?.invert ? published.length : drafts.length; + draftTypes[Tab.Published] = draftField?.invert ? drafts.length : published.length; if (tab === Tab.Published) { - pagesToShow = searchedPages.filter(page => !page[draftFieldName]); + pagesToShow = draftField?.invert ? drafts : published; } else if (tab === Tab.Draft) { - pagesToShow = searchedPages.filter(page => !!page[draftFieldName]); + pagesToShow = draftField?.invert ? published : drafts; } else { pagesToShow = searchedPages; } diff --git a/src/models/DraftField.ts b/src/models/DraftField.ts index d2edd162..4226d478 100644 --- a/src/models/DraftField.ts +++ b/src/models/DraftField.ts @@ -2,4 +2,5 @@ export interface DraftField { name: string; type: "boolean" | "choice"; choices?: string[]; + invert?: boolean; } \ No newline at end of file