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: ContentType = {
name: 'default',
name: DEFAULT_CONTENT_TYPE_NAME,
pageBundle: false,
previewPath: null,
fields: [

View File

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

View File

@@ -5,7 +5,11 @@ import { BaseListener } from './BaseListener';
import { exec } from 'child_process';
import { Extension, Logger, Settings } from '../../helpers';
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 { Terminal } from '../../services';
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)) {
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
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'));
}
} else {
@@ -209,16 +216,35 @@ export class SsgListener extends BaseListener {
} as Field;
break;
case 'ZodBoolean':
ctField = {
name: field.name,
type: 'boolean'
} as Field;
if (field.name === 'published') {
ctField = {
name: field.name,
type: 'draft'
} as Field;
} else {
ctField = {
name: field.name,
type: 'boolean'
} as Field;
}
break;
case 'ZodArray':
ctField = {
name: field.name,
type: 'list'
} as Field;
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 = {
name: field.name,
type: 'list'
} as Field;
}
break;
case 'ZodEnum':
ctField = {
@@ -227,6 +253,7 @@ export class SsgListener extends BaseListener {
choices: field.options || []
} as Field;
break;
case 'datetime':
case 'ZodDate':
ctField = {
name: field.name,

View File

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

View File

@@ -3,8 +3,7 @@ import { join } from 'path';
import { createServer } from 'vite';
import zod from 'astro/zod';
const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion, ZodArray } =
zod;
const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion } = zod;
/**
* Process the Zod field
@@ -73,26 +72,6 @@ function generateFieldInfo(name, type) {
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) {
const subFields = extractFieldInfoFromShape(fieldType);
@@ -111,16 +90,6 @@ function generateFieldInfo(name, type) {
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) {
// https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/string.ts#L45
if (fieldType._def.checks && fieldType._def.checks.length > 0) {