Skip to content

Commit

Permalink
#591 - Added date format support on datetime field
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Jun 22, 2023
1 parent c9095ca commit 143599b
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [#568](https://github.com/estruyf/vscode-front-matter/issues/568): Update the preview URL if the slug changes
- [#586](https://github.com/estruyf/vscode-front-matter/issues/586): Allow to specify the content card fields
- [#588](https://github.com/estruyf/vscode-front-matter/issues/588): Added extensibility support to override card fields
- [#591](https://github.com/estruyf/vscode-front-matter/issues/591): Support for date format in the `datetime` field

### ⚡️ Optimizations

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,11 @@
"default": false,
"description": "Specify if the field should encode emoji"
},
"dateFormat": {
"type": "string",
"default": "",
"description": "Specify the date format to use"
},
"required": {
"type": "boolean",
"default": false,
Expand Down
6 changes: 4 additions & 2 deletions src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,12 @@ export class Article {
/**
* Format the date to the defined format
*/
public static formatDate(dateValue: Date): string {
public static formatDate(dateValue: Date, fieldDateFormat?: string): string {
const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;

if (dateFormat && typeof dateFormat === 'string') {
if (fieldDateFormat) {
return format(dateValue, DateHelper.formatUpdate(fieldDateFormat) as string);
} else if (dateFormat && typeof dateFormat === 'string') {
return format(dateValue, DateHelper.formatUpdate(dateFormat) as string);
} else {
return typeof dateValue.toISOString === 'function'
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/ArticleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class ArticleHelper {

for (const dateField of dateFields) {
if (typeof metadata[dateField.name] !== 'undefined') {
metadata[dateField.name] = Article.formatDate(new Date());
metadata[dateField.name] = Article.formatDate(new Date(), dateField.dateFormat);
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/helpers/ContentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,11 @@ export class ContentType {
for (const field of obj.fields) {
if (field.name === 'title') {
if (field.default) {
data[field.name] = processKnownPlaceholders(field.default, titleValue, dateFormat);
data[field.name] = processKnownPlaceholders(
field.default,
titleValue,
field.dateFormat || dateFormat
);
data[field.name] = await ArticleHelper.processCustomPlaceholders(
data[field.name],
titleValue,
Expand All @@ -812,7 +816,11 @@ export class ContentType {
const defaultValue = field.default;

if (typeof defaultValue === 'string') {
data[field.name] = processKnownPlaceholders(defaultValue, titleValue, dateFormat);
data[field.name] = processKnownPlaceholders(
defaultValue,
titleValue,
field.dateFormat || dateFormat
);
data[field.name] = await ArticleHelper.processCustomPlaceholders(
data[field.name],
titleValue,
Expand Down
6 changes: 1 addition & 5 deletions src/listeners/panel/DataListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,10 @@ export class DataListener extends BaseListener {
}
});

// const isDateField = dateFields.some((f) => f.name === field);
// const isMultiImageField = imageFields.some((f) => f.name === field);
// const isMultiFileField = fileFields.some((f) => f.name === field);

if (dateFieldsArray && dateFieldsArray.length > 0) {
for (const dateField of dateFieldsArray) {
if (field === dateField.name && value) {
parentObj[field] = Article.formatDate(new Date(value));
parentObj[field] = Article.formatDate(new Date(value), dateField.dateFormat);
}
}
} else if (multiImageFieldsArray || multiFileFieldsArray) {
Expand Down
1 change: 1 addition & 0 deletions src/models/PanelSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export interface Field {
// Date fields
isPublishDate?: boolean;
isModifiedDate?: boolean;
dateFormat?: string;

// Data file
dataFileId?: string;
Expand Down
15 changes: 14 additions & 1 deletion src/panelWebView/components/Fields/DateTimeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ export const DateTimeField: React.FunctionComponent<IDateTimeFieldProps> = ({
return required && !dateValue;
}, [required, dateValue]);

const hasTimeFormat = useMemo(() => {
if (!format) {
return true;
}

return format?.toLowerCase().includes('h') ||
format?.includes('m') ||
format?.toLowerCase().includes('s') ||
format?.toLowerCase().includes('a') ||
format?.toLowerCase().includes('b') ||
format?.toLowerCase().includes('k');
}, [format]);

useEffect(() => {
const crntValue = DateHelper.tryParse(value, format);
const stateValue = DateHelper.tryParse(dateValue, format);
Expand All @@ -61,7 +74,7 @@ export const DateTimeField: React.FunctionComponent<IDateTimeFieldProps> = ({
timeInputLabel="Time:"
dateFormat={DateHelper.formatUpdate(format) || 'MM/dd/yyyy HH:mm'}
customInput={<CustomInput />}
showTimeInput
showTimeInput={hasTimeFormat}
/>

<button
Expand Down
2 changes: 1 addition & 1 deletion src/panelWebView/components/Fields/WrapperField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
description={field.description}
value={fieldValue}
required={!!field.required}
format={settings?.date?.format}
format={field.dateFormat || settings?.date?.format}
onChange={(date) => onSendUpdate(field.name, date, parentFields)}
/>
</FieldBoundary>
Expand Down

0 comments on commit 143599b

Please sign in to comment.