#556 #555 - better field type detection

This commit is contained in:
Elio Struyf
2023-04-02 21:22:39 +02:00
parent 78f0e7f56a
commit 61d8d606a7
2 changed files with 65 additions and 23 deletions

View File

@@ -39,6 +39,8 @@
- [#534](https://github.com/estruyf/vscode-front-matter/issues/534): Moved the `mediaDb.json` file to a `.frontmatter/database` folder instead of the `.frontmatter/content` folder
- [#536](https://github.com/estruyf/vscode-front-matter/issues/536): Set the start location from the script to the root of the workspace
- [#555](https://github.com/estruyf/vscode-front-matter/issues/555): When generating a content-type from existing content, Front Matter will better detect the type of field
- [#556](https://github.com/estruyf/vscode-front-matter/issues/556): Content values are aligned to the type of field
### 🐞 Fixes

View File

@@ -520,32 +520,30 @@ export class ContentType {
continue;
}
if (
if (field.toLowerCase() === 'tag' || field.toLowerCase() === 'tags') {
fields.push({
title: field,
name: field,
type: 'tags'
} as Field);
} else if (field.toLowerCase() === 'category' || field.toLowerCase() === 'categories') {
fields.push({
title: field,
name: field,
type: 'categories'
} as Field);
} else if (
fieldData &&
fieldData instanceof Array &&
fieldData.length > 0 &&
typeof fieldData[0] === 'string'
) {
if (field.toLowerCase() === 'tag' || field.toLowerCase() === 'tags') {
fields.push({
title: field,
name: field,
type: 'tags'
} as Field);
} else if (field.toLowerCase() === 'category' || field.toLowerCase() === 'categories') {
fields.push({
title: field,
name: field,
type: 'categories'
} as Field);
} else {
fields.push({
title: field,
name: field,
type: 'choice',
choices: fieldData
} as Field);
}
fields.push({
title: field,
name: field,
type: 'choice',
choices: fieldData
} as Field);
} else if (
fieldData &&
fieldData instanceof Array &&
@@ -568,7 +566,19 @@ export class ContentType {
fields: newFields
} as Field);
} else {
if (!isNaN(new Date(fieldData).getDate())) {
if (field.toLowerCase().includes('image')) {
fields.push({
title: field,
name: field,
type: 'image'
} as Field);
} else if (typeof fieldData === 'number') {
fields.push({
title: field,
name: field,
type: 'number'
} as Field);
} else if (!isNaN(new Date(fieldData).getDate())) {
fields.push({
title: field,
name: field,
@@ -742,7 +752,37 @@ export class ContentType {
) {
data[field.name] = true;
} else {
data[field.name] = '';
// Check the field types
switch (field.type) {
case 'choice':
if (field.multiple) {
data[field.name] = [];
} else {
data[field.name] = '';
}
break;
case 'boolean':
data[field.name] = false;
break;
case 'number':
data[field.name] = 0;
break;
case 'datetime':
data[field.name] = null;
break;
case 'list':
case 'tags':
case 'categories':
case 'taxonomy':
data[field.name] = [];
break;
case 'string':
case 'image':
case 'file':
default:
data[field.name] = '';
break;
}
}
}
}