XiaoDaiGua-Ray 22dfe441e0
feat(layout): add a composable workspace designer
Add layout registration, visual editing, and source generation.

Preserve embedded pages and wait for asynchronous layout renderers.

Add workspace lint and formatting commands.
2026-12-01 17:34:27 +08:00

104 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()
}