mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-12-02 17:53:47 +08:00
Add layout registration, visual editing, and source generation. Preserve embedded pages and wait for asynchronous layout renderers. Add workspace lint and formatting commands.
177 lines
4.9 KiB
TypeScript
177 lines
4.9 KiB
TypeScript
import * as v from 'valibot'
|
|
import { layoutIdMaxLength, layoutIdPattern } from './id.ts'
|
|
import { layoutBlocks, layoutRowsSchema, sidebarSchema } from './preview.ts'
|
|
|
|
export const layoutDefinitionsDirectory = 'apps/admin/src/layout/definitions'
|
|
export const layoutWriteEndpoint = '/__ray-layout-lab/write'
|
|
export { layoutBlocks }
|
|
export type LayoutBlock = (typeof layoutBlocks)[number]
|
|
|
|
const idSchema = v.pipe(
|
|
v.string(),
|
|
v.maxLength(layoutIdMaxLength),
|
|
v.regex(layoutIdPattern),
|
|
)
|
|
const layoutDesignSchema = v.pipe(
|
|
v.strictObject({
|
|
id: idSchema,
|
|
label: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(80)),
|
|
selectable: v.boolean(),
|
|
autoRegister: v.optional(v.boolean(), true),
|
|
sidebar: v.optional(sidebarSchema, 'none'),
|
|
footer: v.optional(v.boolean(), false),
|
|
rows: layoutRowsSchema,
|
|
bottomRows: v.optional(layoutRowsSchema, []),
|
|
}),
|
|
v.check(({ rows, bottomRows }) => {
|
|
const blocks = [...rows.flat(), ...bottomRows.flat()]
|
|
|
|
return new Set(blocks).size === blocks.length
|
|
}),
|
|
)
|
|
|
|
export type LayoutDesign = v.InferInput<typeof layoutDesignSchema>
|
|
|
|
const components = {
|
|
actions: 'LayoutHeaderActions',
|
|
breadcrumb: 'LayoutBreadcrumb',
|
|
navigation: 'LayoutTopNavigation',
|
|
tabs: 'LayoutTabs',
|
|
} satisfies Record<LayoutBlock, string>
|
|
|
|
export function layoutFileName(id: string) {
|
|
return `${v.parse(idSchema, id)}.layout.tsx`
|
|
}
|
|
|
|
export function parseLayoutDesign(input: unknown) {
|
|
return v.parse(layoutDesignSchema, input)
|
|
}
|
|
|
|
function labelLiteral(value: string) {
|
|
// 模板字符串在长名称与宽字符下都保持单行;插值符号和不可见字符需要显式转义。
|
|
const escaped = JSON.stringify(value)
|
|
.slice(1, -1)
|
|
.replaceAll('\\"', '"')
|
|
.replaceAll('`', '\\`')
|
|
.replaceAll('${', '\\${')
|
|
.replace(
|
|
/[^\S ]|[\p{Cc}\p{Cf}]/gu,
|
|
(char) => `\\u{${char.codePointAt(0)!.toString(16)}}`,
|
|
)
|
|
|
|
return '`' + escaped + '`'
|
|
}
|
|
|
|
export function generateLayoutSource(input: unknown): string {
|
|
const design = parseLayoutDesign(input)
|
|
const placedBlocks = [...design.rows.flat(), ...design.bottomRows.flat()]
|
|
const imports = [
|
|
'LayoutFrame',
|
|
'LayoutOutlet',
|
|
...(placedBlocks.length ? ['LayoutRow'] : []),
|
|
...(design.sidebar === 'none' ? [] : ['LayoutNavigation']),
|
|
...(design.footer ? ['LayoutFooter'] : []),
|
|
...placedBlocks.map((block) => components[block]),
|
|
].sort()
|
|
const symbols = imports.join(', ')
|
|
const compactImport = `import { ${symbols} } from '@/layout/blocks'`
|
|
const blockImport =
|
|
compactImport.length <= 80
|
|
? compactImport
|
|
: `import {\n${imports.map((item) => ` ${item},`).join('\n')}\n} from '@/layout/blocks'`
|
|
const name =
|
|
design.id
|
|
.split('-')
|
|
.map((part) => part[0].toUpperCase() + part.slice(1))
|
|
.join('') + 'Layout'
|
|
|
|
const slots: string[] = []
|
|
|
|
const rowRegions = [
|
|
['header', design.rows],
|
|
['bottom', design.bottomRows],
|
|
] as const
|
|
|
|
for (const [region, rows] of rowRegions) {
|
|
if (!rows.length) continue
|
|
|
|
const markup = rows
|
|
.map((row) =>
|
|
[
|
|
' <LayoutRow>',
|
|
...row.map((block) => ` <${components[block]} />`),
|
|
' </LayoutRow>',
|
|
].join('\n'),
|
|
)
|
|
.join('\n')
|
|
|
|
slots.push(` ${region}: () => (
|
|
<>
|
|
${markup}
|
|
</>
|
|
),`)
|
|
}
|
|
|
|
let frameAttributes = ''
|
|
let fallbackMode = 'top'
|
|
|
|
if (design.sidebar !== 'none') {
|
|
frameAttributes = ` sidebarPlacement="${design.sidebar}"`
|
|
fallbackMode = design.sidebar === 'right' ? 'right' : 'side'
|
|
const end = design.sidebar === 'right' ? ' end' : ''
|
|
|
|
slots.push(` sidebar: () => <LayoutNavigation${end} />,`)
|
|
}
|
|
|
|
if (design.footer) slots.push(' footer: () => <LayoutFooter />,')
|
|
|
|
slots.push(' default: () => <LayoutOutlet />,')
|
|
|
|
const rowLiteral = (rows: LayoutBlock[][]) => {
|
|
const lines = rows.map((row) => {
|
|
const blocks = row.map((block) => `'${block}'`)
|
|
|
|
return `[${blocks.join(', ')}]`
|
|
})
|
|
|
|
return `[${lines.join(', ')}]`
|
|
}
|
|
const preview = [
|
|
` rows: ${rowLiteral(design.rows)},`,
|
|
...(design.bottomRows.length
|
|
? [` bottomRows: ${rowLiteral(design.bottomRows)},`]
|
|
: []),
|
|
` sidebar: '${design.sidebar}',`,
|
|
` footer: ${design.footer},`,
|
|
].join('\n')
|
|
|
|
return `${blockImport}
|
|
import type { LayoutDefinition } from '@/layout/types'
|
|
import { defineComponent } from 'vue'
|
|
|
|
export default {
|
|
id: '${design.id}',
|
|
label: ${labelLiteral(design.label)},
|
|
selectable: ${design.selectable},
|
|
autoRegister: ${design.autoRegister},
|
|
sharedFrame: true,
|
|
fallbackMode: '${fallbackMode}',
|
|
preview: {
|
|
${preview}
|
|
},
|
|
component: defineComponent({
|
|
name: '${name}',
|
|
setup() {
|
|
return () => (
|
|
<LayoutFrame${frameAttributes}>
|
|
{{
|
|
${slots.join('\n')}
|
|
}}
|
|
</LayoutFrame>
|
|
)
|
|
},
|
|
}),
|
|
} satisfies LayoutDefinition
|
|
`
|
|
}
|