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.
104 lines
3.0 KiB
TypeScript
104 lines
3.0 KiB
TypeScript
import { LAYOUT_MODES } from '@ray-template/admin-shell'
|
||
import { shallowRef, toRaw } from 'vue'
|
||
import type { ShallowRef } from 'vue'
|
||
import { isLayoutId } from './id'
|
||
import { readLayoutPreview } from './preview'
|
||
import type { LayoutDefinition } from './types'
|
||
|
||
const validText = (value: unknown) =>
|
||
typeof value === 'string' && value.trim().length > 0
|
||
|
||
export function layoutEntry(definition: LayoutDefinition): LayoutDefinition {
|
||
if (!isLayoutId(definition?.id)) throw new TypeError('Invalid layout ID')
|
||
|
||
const { id, label, component, fallbackMode, autoRegister, selectable } =
|
||
definition
|
||
|
||
if (
|
||
!validText(label) &&
|
||
!(
|
||
typeof label === 'object' &&
|
||
label &&
|
||
validText(label['zh-CN']) &&
|
||
validText(label['en-US'])
|
||
)
|
||
)
|
||
throw new TypeError(`Invalid label for layout "${id}"`)
|
||
|
||
if (
|
||
!component ||
|
||
(typeof component !== 'object' && typeof component !== 'function') ||
|
||
(fallbackMode !== void 0 && !LAYOUT_MODES.includes(fallbackMode)) ||
|
||
(autoRegister !== void 0 && typeof autoRegister !== 'boolean') ||
|
||
(definition.sharedFrame !== void 0 &&
|
||
typeof definition.sharedFrame !== 'boolean') ||
|
||
(selectable !== void 0 && typeof selectable !== 'boolean')
|
||
)
|
||
throw new TypeError(`Invalid layout definition "${id}"`)
|
||
|
||
return Object.freeze({
|
||
id,
|
||
label: typeof label === 'string' ? label : Object.freeze({ ...label }),
|
||
component: toRaw(component),
|
||
sharedFrame: definition.sharedFrame,
|
||
preview:
|
||
definition.preview === void 0
|
||
? void 0
|
||
: readLayoutPreview(definition.preview),
|
||
fallbackMode,
|
||
autoRegister,
|
||
get selectable() {
|
||
return definition.selectable
|
||
},
|
||
})
|
||
}
|
||
|
||
export function collectLayoutDefinitions(
|
||
modules: Record<string, LayoutDefinition>,
|
||
) {
|
||
const builtins = LAYOUT_MODES.map((id) => `./definitions/${id}.layout.ts`)
|
||
const paths = [
|
||
...builtins.filter((path) => Object.hasOwn(modules, path)),
|
||
...Object.keys(modules).filter((path) => !builtins.includes(path)),
|
||
]
|
||
const catalog = new Map<string, LayoutDefinition>()
|
||
|
||
for (const path of paths) {
|
||
const definition = modules[path]
|
||
|
||
if (definition?.autoRegister === false) continue
|
||
|
||
try {
|
||
const entry = layoutEntry(definition)
|
||
|
||
if (catalog.has(entry.id))
|
||
throw new Error(`Duplicate layout ID "${entry.id}"`)
|
||
|
||
catalog.set(entry.id, entry)
|
||
} catch (error) {
|
||
console.error(`[useLayout] ${path}`, error)
|
||
}
|
||
}
|
||
|
||
return catalog
|
||
}
|
||
|
||
const catalog = collectLayoutDefinitions(
|
||
import.meta.glob<LayoutDefinition>('./definitions/*.layout.{js,ts,jsx,tsx}', {
|
||
eager: true,
|
||
import: 'default',
|
||
}),
|
||
)
|
||
|
||
// 文件增删会重载本模块。保留 ref,使现有 Pinia 实例和实验室无需重新挂载。
|
||
export const discoveredLayouts: ShallowRef<
|
||
ReadonlyMap<string, LayoutDefinition>
|
||
> = import.meta.hot?.data.discoveredLayouts ?? shallowRef(catalog)
|
||
|
||
discoveredLayouts.value = catalog
|
||
|
||
if (import.meta.hot) {
|
||
import.meta.hot.data.discoveredLayouts = discoveredLayouts
|
||
import.meta.hot.accept()
|
||
}
|