Files
wordpress-theme-aksrt/assets/admin/src/schema/index.js
T

108 lines
2.8 KiB
JavaScript

/**
* Schema Registry — central index for all admin section schemas.
*
* Each section's schema defines the form fields, groups, API endpoints,
* and default values for a settings page. The AksFormRenderer component
* consumes these schemas to render settings pages automatically.
*
* Schema shape per section:
* {
* key: 'global.seo',
* title: 'SEO优化',
* icon: 'fa fas fa-search',
* description: '...',
* api: { get: () => api.getSeoSettings(), save: (data) => api.saveSeoSettings(data) },
* defaults: { ... },
* groups: [
* {
* key: 'basic',
* title: '基础SEO',
* icon: 'fa fas fa-globe',
* fields: [
* { key: 'site_title', label: '站点标题', type: 'input', default: '' },
* ...
* ]
* }
* ]
* }
*/
import { globalSchemas } from './sections/global/index.js'
import { postListSchemas } from './sections/post-list/index.js'
import { permissionsSchemas } from './sections/permissions/index.js'
import { usersSchemas } from './sections/users/index.js'
import { paymentSchemas } from './sections/payment/index.js'
import { extensionsSchemas } from './sections/extensions/index.js'
import { toolsSchemas } from './sections/tools/index.js'
export function normalizeSchemaDomain(domain = '') {
const value = String(domain).trim()
if (!value) {
return 'global'
}
return value.replace(/-/g, '_')
}
export const CANONICAL_SCHEMA_DOMAINS = [
'global',
'page_display',
'post_list',
'permissions',
'users',
'payment',
'extensions',
'tools',
]
/** All schemas organized by domain */
export const ALL_SCHEMAS = Object.freeze({
global: globalSchemas,
page_display: Object.freeze({}),
post_list: postListSchemas,
permissions: permissionsSchemas,
users: usersSchemas,
payment: paymentSchemas,
extensions: extensionsSchemas,
tools: toolsSchemas,
})
/**
* Get a specific section schema by its key path.
* @param {string} domain - e.g. 'global'
* @param {string} section - e.g. 'seo'
* @returns {Object|null}
*/
export function getSchema(domain, section) {
const domainSchemas = ALL_SCHEMAS[normalizeSchemaDomain(domain)]
if (!domainSchemas) return null
return domainSchemas[section] || null
}
/**
* Get all schemas for a domain.
* @param {string} domain
* @returns {Object}
*/
export function getDomainSchemas(domain) {
return ALL_SCHEMAS[normalizeSchemaDomain(domain)] || {}
}
export function getSchemaSectionKeys(domain) {
return Object.keys(getDomainSchemas(domain))
}
export function getSchemaDefaults(domain, section = '') {
const domainKey = normalizeSchemaDomain(domain)
if (section) {
return { ...(getSchema(domainKey, section)?.defaults || {}) }
}
return Object.values(getDomainSchemas(domainKey)).reduce((defaults, schema) => {
return {
...defaults,
...(schema?.defaults || {}),
}
}, {})
}