Enhancement: Better content type creation for Astro #704

This commit is contained in:
Elio Struyf
2023-11-11 11:19:11 -08:00
parent e1f6c90fc0
commit a8407920bc
5 changed files with 43 additions and 56 deletions

View File

@@ -3,7 +3,7 @@ import { ContentType } from './../models/PanelSettings';
export const DEFAULT_CONTENT_TYPE_NAME = 'default'; export const DEFAULT_CONTENT_TYPE_NAME = 'default';
export const DEFAULT_CONTENT_TYPE: ContentType = { export const DEFAULT_CONTENT_TYPE: ContentType = {
name: 'default', name: DEFAULT_CONTENT_TYPE_NAME,
pageBundle: false, pageBundle: false,
previewPath: null, previewPath: null,
fields: [ fields: [

View File

@@ -4,7 +4,6 @@ import { CheckCircleIcon as CheckCircleIconSolid, PlusCircleIcon as PlusCircleIc
export interface ISelectItemProps { export interface ISelectItemProps {
title: string; title: string;
icon?: "add" | "select";
buttonTitle: string; buttonTitle: string;
isSelected: boolean; isSelected: boolean;
disabled?: boolean; disabled?: boolean;
@@ -13,7 +12,6 @@ export interface ISelectItemProps {
export const SelectItem: React.FunctionComponent<ISelectItemProps> = ({ export const SelectItem: React.FunctionComponent<ISelectItemProps> = ({
title, title,
icon = "select",
buttonTitle, buttonTitle,
isSelected, isSelected,
disabled, disabled,
@@ -30,17 +28,9 @@ export const SelectItem: React.FunctionComponent<ISelectItemProps> = ({
disabled={disabled} disabled={disabled}
> >
{isSelected ? ( {isSelected ? (
icon === "add" ? (
<PlusCircleIconSolid className={`h-4 w-4`} />
) : (
<CheckCircleIconSolid className={`h-4 w-4`} /> <CheckCircleIconSolid className={`h-4 w-4`} />
)
) : ( ) : (
icon === "add" ? (
<PlusCircleIcon className={`h-4 w-4`} /> <PlusCircleIcon className={`h-4 w-4`} />
) : (
<CheckCircleIcon className={`h-4 w-4`} />
)
)} )}
<span>{title}</span> <span>{title}</span>
</button> </button>

View File

@@ -5,7 +5,11 @@ import { BaseListener } from './BaseListener';
import { exec } from 'child_process'; import { exec } from 'child_process';
import { Extension, Logger, Settings } from '../../helpers'; import { Extension, Logger, Settings } from '../../helpers';
import { Folders } from '../../commands'; import { Folders } from '../../commands';
import { SETTING_TAXONOMY_CONTENT_TYPES, SsgScripts } from '../../constants'; import {
DEFAULT_CONTENT_TYPE_NAME,
SETTING_TAXONOMY_CONTENT_TYPES,
SsgScripts
} from '../../constants';
import { SettingsListener } from './SettingsListener'; import { SettingsListener } from './SettingsListener';
import { Terminal } from '../../services'; import { Terminal } from '../../services';
import { existsAsync, readFileAsync } from '../../utils'; import { existsAsync, readFileAsync } from '../../utils';
@@ -58,7 +62,10 @@ export class SsgListener extends BaseListener {
} }
} }
const contentTypes = Settings.get<ContentType[]>(SETTING_TAXONOMY_CONTENT_TYPES) || []; let contentTypes = Settings.get<ContentType[]>(SETTING_TAXONOMY_CONTENT_TYPES) || [];
// Filter out the default content type
contentTypes = contentTypes.filter((ct) => ct.name !== DEFAULT_CONTENT_TYPE_NAME);
if (contentTypes.find((ct) => ct.name === collection.name)) { if (contentTypes.find((ct) => ct.name === collection.name)) {
SsgListener.sendRequest(command as any, requestId, {}); SsgListener.sendRequest(command as any, requestId, {});
@@ -150,7 +157,7 @@ export class SsgListener extends BaseListener {
// Update the vite reference, as it is not a direct dependency of the project // Update the vite reference, as it is not a direct dependency of the project
let scriptContents = await readFileAsync(scriptPath.fsPath, 'utf8'); let scriptContents = await readFileAsync(scriptPath.fsPath, 'utf8');
scriptContents = scriptContents.replace(`"vite"`, `"${vitePath}"`); scriptContents = scriptContents.replace(`'vite'`, `'${vitePath}'`);
await workspace.fs.writeFile(tempScriptPath, Buffer.from(scriptContents, 'utf8')); await workspace.fs.writeFile(tempScriptPath, Buffer.from(scriptContents, 'utf8'));
} }
} else { } else {
@@ -209,16 +216,35 @@ export class SsgListener extends BaseListener {
} as Field; } as Field;
break; break;
case 'ZodBoolean': case 'ZodBoolean':
if (field.name === 'published') {
ctField = {
name: field.name,
type: 'draft'
} as Field;
} else {
ctField = { ctField = {
name: field.name, name: field.name,
type: 'boolean' type: 'boolean'
} as Field; } as Field;
}
break; break;
case 'ZodArray': case 'ZodArray':
if (field.name === 'tags') {
ctField = {
name: field.name,
type: 'tags'
} as Field;
} else if (field.name === 'categories') {
ctField = {
name: field.name,
type: 'categories'
} as Field;
} else {
ctField = { ctField = {
name: field.name, name: field.name,
type: 'list' type: 'list'
} as Field; } as Field;
}
break; break;
case 'ZodEnum': case 'ZodEnum':
ctField = { ctField = {
@@ -227,6 +253,7 @@ export class SsgListener extends BaseListener {
choices: field.options || [] choices: field.options || []
} as Field; } as Field;
break; break;
case 'datetime':
case 'ZodDate': case 'ZodDate':
ctField = { ctField = {
name: field.name, name: field.name,

View File

@@ -14,6 +14,7 @@ export interface AstroField {
| 'ZodEnum' | 'ZodEnum'
| 'ZodDate' | 'ZodDate'
| 'ZodObject' | 'ZodObject'
| 'datetime'
| 'email' | 'email'
| 'url' | 'url'
| 'image'; | 'image';

View File

@@ -3,8 +3,7 @@ import { join } from 'path';
import { createServer } from 'vite'; import { createServer } from 'vite';
import zod from 'astro/zod'; import zod from 'astro/zod';
const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion, ZodArray } = const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion } = zod;
zod;
/** /**
* Process the Zod field * Process the Zod field
@@ -73,26 +72,6 @@ function generateFieldInfo(name, type) {
required: !isFieldOptional required: !isFieldOptional
}; };
switch (fieldInfo.type) {
case 'ZodString':
fieldInfo.type = 'string';
break;
case 'ZodNumber':
fieldInfo.type = 'number';
break;
case 'ZodBoolean':
fieldInfo.type = 'boolean';
break;
case 'ZodDate':
fieldInfo.type = 'datetime';
break;
case 'ZodArray':
fieldInfo.type = 'choice';
break;
default:
break;
}
if (fieldType instanceof ZodObject) { if (fieldType instanceof ZodObject) {
const subFields = extractFieldInfoFromShape(fieldType); const subFields = extractFieldInfoFromShape(fieldType);
@@ -111,16 +90,6 @@ function generateFieldInfo(name, type) {
fieldInfo.options = fieldType.options; fieldInfo.options = fieldType.options;
} }
if (fieldType instanceof ZodArray) {
if (fieldInfo.name === 'tags') {
fieldInfo.type = 'tags';
} else if (fieldInfo.name === 'categories') {
fieldInfo.type = 'categories';
} else {
fieldInfo.options = fieldType.options;
}
}
if (fieldType instanceof ZodString) { if (fieldType instanceof ZodString) {
// https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/string.ts#L45 // https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/string.ts#L45
if (fieldType._def.checks && fieldType._def.checks.length > 0) { if (fieldType._def.checks && fieldType._def.checks.length > 0) {