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.
272 lines
8.4 KiB
TypeScript
272 lines
8.4 KiB
TypeScript
import { APP_CATCH_KEY } from '@/app-config'
|
|
import { collectLayoutDefinitions, discoveredLayouts } from '@/layout/discovery'
|
|
import type { LayoutDefinition } from '@/layout/types'
|
|
import { useLayout } from '@/layout/useLayout'
|
|
import { piniaSettingStore } from '@/store/modules/setting'
|
|
import { createPinia, disposePinia, setActivePinia } from 'pinia'
|
|
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
|
|
import { createApp, defineComponent, shallowReactive, watch } from 'vue'
|
|
|
|
const component = defineComponent({ render: () => null })
|
|
const definition = (id: string): LayoutDefinition => ({
|
|
id,
|
|
label: id,
|
|
component,
|
|
})
|
|
|
|
function mountWorkspace() {
|
|
const pinia = createPinia().use(piniaPluginPersistedstate)
|
|
|
|
createApp(component).use(pinia)
|
|
setActivePinia(pinia)
|
|
const layout = useLayout()
|
|
const settings = piniaSettingStore()
|
|
|
|
return { layout, settings, dispose: () => disposePinia(pinia) }
|
|
}
|
|
|
|
const key = APP_CATCH_KEY.appPiniaSettingStore
|
|
|
|
beforeEach(() => localStorage.removeItem(key))
|
|
afterEach(() => localStorage.removeItem(key))
|
|
|
|
it('discovers built-ins and custom-top and isolates explicitly registered layouts per application', () => {
|
|
const first = mountWorkspace()
|
|
const second = mountWorkspace()
|
|
|
|
try {
|
|
expect(first.layout.layouts.value.slice(0, 5).map(({ id }) => id)).toEqual([
|
|
'side',
|
|
'top',
|
|
'mixed',
|
|
'right',
|
|
'split',
|
|
])
|
|
expect(
|
|
first.layout.layouts.value.some(({ id }) => id === 'custom-top'),
|
|
).toBe(true)
|
|
|
|
first.layout.registerLayout({
|
|
...definition('private-workspace'),
|
|
autoRegister: false,
|
|
})
|
|
expect(first.layout.activeLayoutId.value).toBe('side')
|
|
expect(first.layout.updateLayout('private-workspace')).toBe(true)
|
|
expect(second.layout.updateLayout('private-workspace')).toBe(false)
|
|
expect(() => first.layout.registerLayout(definition('side'))).toThrow(
|
|
/Duplicate/,
|
|
)
|
|
expect(() =>
|
|
first.layout.registerLayout(definition('invalid/name')),
|
|
).toThrow(/ID/)
|
|
} finally {
|
|
first.dispose()
|
|
second.dispose()
|
|
}
|
|
})
|
|
|
|
it('retains the active layout when selection closes and prevents entry through code or cache restoration', () => {
|
|
const first = mountWorkspace()
|
|
const custom = shallowReactive({
|
|
...definition('optional'),
|
|
selectable: true,
|
|
})
|
|
|
|
first.layout.registerLayout(custom)
|
|
expect(first.layout.updateLayout('optional')).toBe(true)
|
|
expect(first.layout.activeLayoutId.value).toBe('optional')
|
|
custom.selectable = false
|
|
expect(first.layout.activeLayoutId.value).toBe('optional')
|
|
first.settings.$persist()
|
|
|
|
const second = mountWorkspace()
|
|
|
|
try {
|
|
second.layout.registerLayout(custom)
|
|
expect(second.layout.activeLayoutId.value).toBe('side')
|
|
expect(second.layout.updateLayout('optional')).toBe(false)
|
|
expect(first.layout.updateLayout('top')).toBe(true)
|
|
expect(first.layout.updateLayout('optional')).toBe(false)
|
|
expect(first.layout.activeLayoutId.value).toBe('top')
|
|
} finally {
|
|
first.dispose()
|
|
second.dispose()
|
|
}
|
|
})
|
|
|
|
it('uses the declared structure for discovered and explicitly registered previews', () => {
|
|
const workspace = mountWorkspace()
|
|
const preview = {
|
|
rows: [['tabs' as const]],
|
|
bottomRows: [['actions' as const]],
|
|
sidebar: 'right' as const,
|
|
footer: true,
|
|
}
|
|
|
|
try {
|
|
workspace.layout.registerLayout({ ...definition('test-preview'), preview })
|
|
const entries = workspace.layout.layouts.value
|
|
const registered = entries.find(({ id }) => id === 'test-preview')!
|
|
|
|
expect(registered.preview).toMatchObject(preview)
|
|
preview.rows[0].pop()
|
|
expect(registered.preview?.rows).toEqual([['tabs']])
|
|
expect(
|
|
entries.find(({ id }) => id === 'custom-top')?.preview?.rows,
|
|
).toEqual([['tabs', 'actions'], ['navigation']])
|
|
expect(
|
|
entries.find(({ id }) => id === 'split')?.preview?.wideHeaderRows,
|
|
).toBe(2)
|
|
expect(() =>
|
|
workspace.layout.registerLayout({
|
|
...definition('invalid-span'),
|
|
preview: { ...preview, rows: [['tabs']], wideHeaderRows: 2 },
|
|
}),
|
|
).toThrow()
|
|
} finally {
|
|
workspace.dispose()
|
|
}
|
|
})
|
|
|
|
it('restores a late registration only while its saved ID is still selected', () => {
|
|
localStorage.setItem(
|
|
key,
|
|
JSON.stringify({
|
|
customLayoutId: 'late-entry',
|
|
layoutConfig: { mode: 'right' },
|
|
}),
|
|
)
|
|
const { layout, settings, dispose } = mountWorkspace()
|
|
|
|
try {
|
|
expect(layout.selectedLayoutId.value).toBe('late-entry')
|
|
expect(layout.activeLayoutId.value).toBe('side')
|
|
layout.registerLayout(definition('late-entry'))
|
|
expect(layout.activeLayoutId.value).toBe('late-entry')
|
|
|
|
settings.updateSettingState('layoutConfig', { contentPadding: 11 })
|
|
expect(layout.activeLayoutId.value).toBe('late-entry')
|
|
settings.updateSettingState('layoutConfig', { mode: 'right' })
|
|
expect(layout.activeLayoutId.value).toBe('right')
|
|
expect(settings.customLayoutId).toBeNull()
|
|
} finally {
|
|
dispose()
|
|
}
|
|
|
|
localStorage.setItem(key, JSON.stringify({ customLayoutId: 'late-entry' }))
|
|
const next = mountWorkspace()
|
|
|
|
try {
|
|
next.layout.updateLayout('top')
|
|
next.layout.registerLayout(definition('late-entry'))
|
|
expect(next.layout.activeLayoutId.value).toBe('top')
|
|
} finally {
|
|
next.dispose()
|
|
}
|
|
})
|
|
|
|
it('round-trips a custom selection in browser storage and maps preference v1 to its built-in mode', () => {
|
|
const first = mountWorkspace()
|
|
|
|
first.layout.updateLayout('custom-top')
|
|
const file = first.settings.exportPreferences()
|
|
|
|
expect(file.version).toBe(1)
|
|
expect(file.preferences.layoutConfig.mode).toBe('top')
|
|
expect(file.preferences).not.toHaveProperty('customLayoutId')
|
|
first.settings.$persist()
|
|
first.dispose()
|
|
|
|
const next = mountWorkspace()
|
|
|
|
try {
|
|
expect(next.layout.activeLayoutId.value).toBe('custom-top')
|
|
next.settings.importPreferences(JSON.stringify(file))
|
|
expect(next.layout.activeLayoutId.value).toBe('top')
|
|
expect(next.settings.customLayoutId).toBeNull()
|
|
} finally {
|
|
next.dispose()
|
|
}
|
|
})
|
|
|
|
it('reconciles discovered files for a mounted workspace without discarding manual registrations', () => {
|
|
const original = discoveredLayouts.value
|
|
const workspace = mountWorkspace()
|
|
|
|
try {
|
|
workspace.layout.registerLayout(definition('manual-entry'))
|
|
const fresh = new Map(original)
|
|
|
|
fresh.set('generated-layout', definition('generated-layout'))
|
|
discoveredLayouts.value = fresh
|
|
expect(workspace.layout.updateLayout('generated-layout')).toBe(true)
|
|
expect(workspace.layout.activeLayoutId.value).toBe('generated-layout')
|
|
discoveredLayouts.value = original
|
|
expect(workspace.layout.updateLayout('generated-layout')).toBe(false)
|
|
expect(workspace.layout.activeLayoutId.value).toBe('side')
|
|
expect(workspace.layout.updateLayout('manual-entry')).toBe(true)
|
|
} finally {
|
|
discoveredLayouts.value = original
|
|
workspace.dispose()
|
|
}
|
|
})
|
|
|
|
it('normalizes malformed cached IDs and keeps synchronous observers on the selected layout', () => {
|
|
localStorage.setItem(
|
|
key,
|
|
JSON.stringify({
|
|
customLayoutId: { id: 'broken' },
|
|
layoutConfig: { mode: 'mixed' },
|
|
}),
|
|
)
|
|
const workspace = mountWorkspace()
|
|
|
|
try {
|
|
expect(workspace.settings.customLayoutId).toBeNull()
|
|
const seen: string[] = []
|
|
const stop = watch(
|
|
workspace.layout.selectedLayoutId,
|
|
(id) => seen.push(id),
|
|
{ flush: 'sync' },
|
|
)
|
|
|
|
workspace.layout.updateLayout('custom-top')
|
|
workspace.layout.updateLayout('right')
|
|
stop()
|
|
expect(seen).toEqual(['custom-top', 'right'])
|
|
} finally {
|
|
workspace.dispose()
|
|
}
|
|
})
|
|
|
|
it('skips closed auto-registration and detects duplicate definitions without dropping valid siblings', () => {
|
|
const report = vi.spyOn(console, 'error').mockImplementation(() => void 0)
|
|
|
|
try {
|
|
const catalog = collectLayoutDefinitions({
|
|
'./definitions/other.layout.ts': definition('side'),
|
|
'./definitions/side.layout.ts': {
|
|
...definition('side'),
|
|
label: 'Built-in',
|
|
},
|
|
'./definitions/hidden.layout.js': {
|
|
...definition('hidden'),
|
|
autoRegister: false,
|
|
},
|
|
'./definitions/closed.layout.tsx': {
|
|
...definition('closed'),
|
|
selectable: false,
|
|
},
|
|
})
|
|
|
|
expect([...catalog.keys()]).toEqual(['side', 'closed'])
|
|
expect(catalog.get('side')?.label).toBe('Built-in')
|
|
expect(report).toHaveBeenCalledExactlyOnceWith(
|
|
'[useLayout] ./definitions/other.layout.ts',
|
|
expect.any(Error),
|
|
)
|
|
} finally {
|
|
report.mockRestore()
|
|
}
|
|
})
|