ray-template/packages/admin-shell/src/LayoutSettings.tsx
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

171 lines
5.7 KiB
TypeScript

import './LayoutSettings.scss'
import {
NButton,
NFlex,
NForm,
NFormItem,
NInputNumber,
NSelect,
NSwitch,
} from 'naive-ui'
import { defineComponent } from 'vue'
import type { PropType } from 'vue'
import { LAYOUT_MODES } from './AdminShell'
import type { LayoutConfig, LayoutMode } from './AdminShell'
import { renderLayoutPreview } from './layout-preview'
import type { LayoutPreview } from './layout-preview'
export interface LayoutChoice {
id: string
label: string
preview: LayoutMode | LayoutPreview
disabled?: boolean
hint?: string
}
export const LayoutSettings = defineComponent({
name: 'LayoutSettings',
props: {
showDescription: Boolean,
choices: { type: Array as PropType<LayoutChoice[]>, default: void 0 },
selectedId: { type: String, default: void 0 },
selectionNotice: { type: String, default: void 0 },
onSelect: {
type: Function as PropType<(id: string) => void>,
default: void 0,
},
layout: { type: Object as PropType<LayoutConfig>, required: true },
defaults: { type: Object as PropType<LayoutConfig>, required: true },
translate: {
type: Function as PropType<(key: string) => string>,
required: true,
},
update: {
type: Function as PropType<(value: Partial<LayoutConfig>) => void>,
required: true,
},
},
setup(props) {
const text = props.translate
const update = props.update
return () => {
const selected = props.selectedId ?? props.layout.mode
const choices =
props.choices ??
LAYOUT_MODES.map((mode) => ({
id: mode,
preview: mode,
label: text(mode),
hint: text(`modeHint.${mode}`),
disabled: false,
}))
const hint = choices.find(({ id }) => id === selected)?.hint
return (
<NFlex vertical size={16} class="r-layout-settings">
{props.showDescription ? (
<p class="r-layout-settings__hint">{text('description')}</p>
) : null}
<div class="r-layout-presets" role="group" aria-label={text('tab')}>
{choices.map((choice) => {
const mode =
typeof choice.preview === 'string'
? choice.preview
: LAYOUT_MODES.find((item) => item === choice.id)
return (
<button
type="button"
class="r-layout-preset"
key={choice.id}
aria-label={choice.label}
title={choice.label}
aria-pressed={selected === choice.id}
disabled={choice.disabled || (!props.onSelect && !mode)}
onClick={() => {
if (props.onSelect) props.onSelect(choice.id)
else if (mode) update({ mode })
}}
>
{renderLayoutPreview(choice.preview)}
<span class="r-layout-preset__label">{choice.label}</span>
{selected === choice.id ? (
<span class="r-layout-preset__selected" aria-hidden="true">
✓
</span>
) : null}
</button>
)
})}
</div>
{props.selectionNotice || (props.showDescription && hint) ? (
<p class="r-layout-settings__hint">
{props.selectionNotice || hint}
</p>
) : null}
<NForm labelPlacement="top" showFeedback={false}>
<NFormItem label={text('contentWidth')}>
<NSelect
aria-label={text('contentWidth')}
value={props.layout.contentWidth}
options={['fluid', 'fixed'].map((value) => ({
value,
label: text(value),
}))}
onUpdateValue={(value: LayoutConfig['contentWidth']) =>
update({ contentWidth: value })
}
/>
</NFormItem>
<NFormItem label={text('maxWidth')}>
<NInputNumber
inputProps={{ 'aria-label': text('maxWidth') }}
value={props.layout.contentMaxWidth}
disabled={props.layout.contentWidth !== 'fixed'}
min={640}
max={2560}
step={40}
precision={0}
onUpdateValue={(value) => {
if (value !== null) update({ contentMaxWidth: value })
}}
/>
</NFormItem>
<NFormItem label={text('padding')}>
<NInputNumber
inputProps={{ 'aria-label': text('padding') }}
value={props.layout.contentPadding}
min={0}
max={48}
step={4}
precision={0}
onUpdateValue={(value) => {
if (value !== null) update({ contentPadding: value })
}}
/>
</NFormItem>
<NFormItem label={text('fixedHeader')}>
<NSwitch
aria-label={text('fixedHeader')}
value={props.layout.fixedHeader}
onUpdateValue={(fixedHeader) => update({ fixedHeader })}
/>
</NFormItem>
<NFormItem label={text('fixedFooter')}>
<NSwitch
aria-label={text('fixedFooter')}
value={props.layout.fixedFooter}
onUpdateValue={(fixedFooter) => update({ fixedFooter })}
/>
</NFormItem>
</NForm>
<NButton onClick={() => update({ ...props.defaults })}>
{text('reset')}
</NButton>
</NFlex>
)
}
},
})