92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import type { MicrositeV2FieldSchema, StaticMicrositeConfig } from '../../../services/api/types';
|
|
|
|
export function getNestedValue(obj: Record<string, unknown>, path: string): unknown {
|
|
const parts = path.split('.');
|
|
let cur: unknown = obj;
|
|
for (const p of parts) {
|
|
if (cur == null || typeof cur !== 'object') return undefined;
|
|
cur = (cur as Record<string, unknown>)[p];
|
|
}
|
|
return cur;
|
|
}
|
|
|
|
export function setNestedValue(obj: Record<string, unknown>, path: string, value: unknown): Record<string, unknown> {
|
|
const parts = path.split('.');
|
|
const out = { ...obj };
|
|
let cur: Record<string, unknown> = out;
|
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
const p = parts[i];
|
|
const next = cur[p];
|
|
cur[p] = next && typeof next === 'object' && !Array.isArray(next) ? { ...(next as Record<string, unknown>) } : {};
|
|
cur = cur[p] as Record<string, unknown>;
|
|
}
|
|
cur[parts[parts.length - 1]] = value;
|
|
return out;
|
|
}
|
|
|
|
export function defaultForField(field: MicrositeV2FieldSchema): unknown {
|
|
if (field.default !== undefined) return field.default;
|
|
switch (field.type) {
|
|
case 'boolean':
|
|
return false;
|
|
case 'array':
|
|
return [];
|
|
case 'color':
|
|
return '#5c6ac4';
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
|
|
export function buildInitialConfigFromSchema(schema: {
|
|
meta?: Record<string, unknown>;
|
|
theme?: { fields?: MicrositeV2FieldSchema[] };
|
|
sections: { id: string; show?: boolean; fields: MicrositeV2FieldSchema[] }[];
|
|
}): StaticMicrositeConfig {
|
|
const config: StaticMicrositeConfig = {
|
|
meta: { ...(schema.meta || {}) },
|
|
theme: {},
|
|
sections: {},
|
|
};
|
|
|
|
for (const field of schema.theme?.fields || []) {
|
|
if (field.key) {
|
|
config.theme = config.theme || {};
|
|
(config.theme as Record<string, unknown>)[field.key] = defaultForField(field);
|
|
}
|
|
}
|
|
|
|
for (const section of schema.sections) {
|
|
const sectionData: Record<string, unknown> = { show: section.show !== false };
|
|
for (const field of section.fields || []) {
|
|
if (field.type === 'array') {
|
|
sectionData[field.key] = defaultForField(field);
|
|
} else if (field.key?.includes('.')) {
|
|
const base = setNestedValue({}, field.key, defaultForField(field));
|
|
Object.assign(sectionData, base);
|
|
} else if (field.key) {
|
|
sectionData[field.key] = defaultForField(field);
|
|
}
|
|
}
|
|
config.sections![section.id] = sectionData;
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
export function mergeConfig(
|
|
schema: Parameters<typeof buildInitialConfigFromSchema>[0],
|
|
existing?: StaticMicrositeConfig | null,
|
|
): StaticMicrositeConfig {
|
|
const base = buildInitialConfigFromSchema(schema);
|
|
if (!existing) return base;
|
|
return {
|
|
meta: { ...base.meta, ...existing.meta },
|
|
theme: { ...base.theme, ...existing.theme },
|
|
sections: {
|
|
...base.sections,
|
|
...(existing.sections || {}),
|
|
},
|
|
};
|
|
}
|