Add Khanegi reset-to-default for full ui-config appearance.
Seeds an immutable app+pages snapshot on first open and restores theme, nav, and header from it. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+106
-17
@@ -1,10 +1,13 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { message, Modal } from 'ant-design-vue'
|
||||
import { ArrowRightOutlined } from '@ant-design/icons-vue'
|
||||
import api from '@/api/http'
|
||||
import { createDefaultKhanegiUiConfig } from '@/config/defaultKhanegiUiConfig'
|
||||
import {
|
||||
cloneKhanegiAppSnapshot,
|
||||
createDefaultKhanegiUiConfig,
|
||||
} from '@/config/defaultKhanegiUiConfig'
|
||||
import {
|
||||
applyKhanegiPreset,
|
||||
khanegiThemePresets,
|
||||
@@ -19,6 +22,7 @@ const route = useRoute()
|
||||
const router = useRouter()
|
||||
const loading = ref(true)
|
||||
const saving = ref(false)
|
||||
const resetting = ref(false)
|
||||
const tenantName = ref('')
|
||||
const product = ref<'hamkar' | 'khanegi'>('khanegi')
|
||||
const activeTab = ref('theme')
|
||||
@@ -27,8 +31,13 @@ const jsonError = ref('')
|
||||
const selectedPreset = ref<KhanegiPresetId>('navy-gold')
|
||||
const previewMode = ref<'dark' | 'light'>('dark')
|
||||
|
||||
const config = reactive(createDefaultKhanegiUiConfig())
|
||||
const config = reactive(createDefaultKhanegiUiConfig()) as ReturnType<
|
||||
typeof createDefaultKhanegiUiConfig
|
||||
> & {
|
||||
defaultSnapshot?: ReturnType<typeof cloneKhanegiAppSnapshot>
|
||||
}
|
||||
|
||||
const hasDefaultSnapshot = computed(() => Boolean(config.defaultSnapshot))
|
||||
const isCustom = computed(() => selectedPreset.value === 'custom')
|
||||
|
||||
const activePreviewColors = computed(() => {
|
||||
@@ -91,7 +100,13 @@ function syncAdvancedJson() {
|
||||
function applyAdvancedJson() {
|
||||
try {
|
||||
const parsed = JSON.parse(advancedJson.value)
|
||||
const keepSnapshot = config.defaultSnapshot
|
||||
? JSON.parse(JSON.stringify(config.defaultSnapshot))
|
||||
: undefined
|
||||
Object.assign(config, createDefaultKhanegiUiConfig(), parsed)
|
||||
if (keepSnapshot && !config.defaultSnapshot) {
|
||||
config.defaultSnapshot = keepSnapshot
|
||||
}
|
||||
ensureSchemes()
|
||||
selectedPreset.value = resolvePresetId(config)
|
||||
jsonError.value = ''
|
||||
@@ -128,6 +143,7 @@ async function load() {
|
||||
router.replace({ name: 'version-edit', params: { id: String(route.params.id) } })
|
||||
return
|
||||
}
|
||||
const hadSnapshot = Boolean(data.appUiConfig?.defaultSnapshot)
|
||||
if (data.appUiConfig && typeof data.appUiConfig === 'object') {
|
||||
Object.assign(config, createDefaultKhanegiUiConfig(), data.appUiConfig)
|
||||
if (!config.app?.theme?.colors) {
|
||||
@@ -135,9 +151,17 @@ async function load() {
|
||||
}
|
||||
}
|
||||
ensureSchemes()
|
||||
if (!config.defaultSnapshot) {
|
||||
config.defaultSnapshot = cloneKhanegiAppSnapshot(config)
|
||||
}
|
||||
selectedPreset.value = resolvePresetId(config)
|
||||
previewMode.value = config.app.theme.mode === 'light' ? 'light' : 'dark'
|
||||
syncAdvancedJson()
|
||||
|
||||
// Persist newly inferred factory snapshot once
|
||||
if (!hadSnapshot && config.defaultSnapshot) {
|
||||
await persistConfig()
|
||||
}
|
||||
} catch (e: any) {
|
||||
message.error(e?.response?.data?.message || 'خطا در بارگذاری')
|
||||
} finally {
|
||||
@@ -145,24 +169,41 @@ async function load() {
|
||||
}
|
||||
}
|
||||
|
||||
function buildSavePayload() {
|
||||
ensureSchemes()
|
||||
config.app.theme.presetId = selectedPreset.value
|
||||
syncActiveColorsFromMode(config)
|
||||
if (selectedPreset.value === 'custom') {
|
||||
markCustomAndSyncSchemes(config)
|
||||
}
|
||||
const payload = JSON.parse(JSON.stringify(config)) as typeof config
|
||||
// Never drop or rewrite an existing factory snapshot on normal save
|
||||
if (config.defaultSnapshot) {
|
||||
payload.defaultSnapshot = JSON.parse(JSON.stringify(config.defaultSnapshot))
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
async function persistConfig() {
|
||||
const payload = buildSavePayload()
|
||||
const colors = payload.app.theme.colors
|
||||
await api.patch(`/tenants/${route.params.id}`, {
|
||||
appUiConfig: payload,
|
||||
primaryColor: colors.primary,
|
||||
secondaryColor: colors.secondary,
|
||||
backgroundColor: colors.background,
|
||||
})
|
||||
Object.assign(config, createDefaultKhanegiUiConfig(), payload)
|
||||
ensureSchemes()
|
||||
selectedPreset.value = resolvePresetId(config)
|
||||
syncAdvancedJson()
|
||||
}
|
||||
|
||||
async function save() {
|
||||
saving.value = true
|
||||
try {
|
||||
ensureSchemes()
|
||||
config.app.theme.presetId = selectedPreset.value
|
||||
syncActiveColorsFromMode(config)
|
||||
if (selectedPreset.value === 'custom') {
|
||||
markCustomAndSyncSchemes(config)
|
||||
}
|
||||
const colors = config.app.theme.colors
|
||||
await api.patch(`/tenants/${route.params.id}`, {
|
||||
appUiConfig: JSON.parse(JSON.stringify(config)),
|
||||
primaryColor: colors.primary,
|
||||
secondaryColor: colors.secondary,
|
||||
backgroundColor: colors.background,
|
||||
})
|
||||
await persistConfig()
|
||||
message.success('ظاهر اپ ذخیره شد')
|
||||
syncAdvancedJson()
|
||||
} catch (e: any) {
|
||||
const msg = e?.response?.data?.message
|
||||
message.error(Array.isArray(msg) ? msg.join('، ') : msg || 'ذخیره ناموفق')
|
||||
@@ -171,6 +212,39 @@ async function save() {
|
||||
}
|
||||
}
|
||||
|
||||
function resetToDefault() {
|
||||
if (!config.defaultSnapshot) {
|
||||
message.warning('ظاهر پیشفرض برای این نسخه ثبت نشده است')
|
||||
return
|
||||
}
|
||||
Modal.confirm({
|
||||
title: 'بازگشت به ظاهر اولیه؟',
|
||||
content: 'تم، منو و هدر به ظاهر کارخانهٔ همین نسخه برمیگردد و ذخیره میشود.',
|
||||
okText: 'بازگشت به پیشفرض',
|
||||
cancelText: 'انصراف',
|
||||
okButtonProps: { danger: true },
|
||||
async onOk() {
|
||||
resetting.value = true
|
||||
try {
|
||||
const snap = config.defaultSnapshot!
|
||||
config.app = JSON.parse(JSON.stringify(snap.app))
|
||||
config.pages = JSON.parse(JSON.stringify(snap.pages || {}))
|
||||
ensureSchemes()
|
||||
selectedPreset.value = resolvePresetId(config)
|
||||
previewMode.value = config.app.theme.mode === 'light' ? 'light' : 'dark'
|
||||
await persistConfig()
|
||||
message.success('ظاهر اولیه بازیابی شد')
|
||||
} catch (e: any) {
|
||||
const msg = e?.response?.data?.message
|
||||
message.error(Array.isArray(msg) ? msg.join('، ') : msg || 'بازیابی ناموفق')
|
||||
throw e
|
||||
} finally {
|
||||
resetting.value = false
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
watch(
|
||||
() => config.app.theme.mode,
|
||||
() => {
|
||||
@@ -407,6 +481,15 @@ onMounted(load)
|
||||
</a-tabs>
|
||||
|
||||
<div class="sticky-actions">
|
||||
<a-button
|
||||
size="large"
|
||||
block
|
||||
:loading="resetting"
|
||||
:disabled="!hasDefaultSnapshot"
|
||||
@click="resetToDefault"
|
||||
>
|
||||
بازگشت به ظاهر اولیه
|
||||
</a-button>
|
||||
<a-button type="primary" size="large" block :loading="saving" @click="save">ذخیره ظاهر</a-button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -608,4 +691,10 @@ onMounted(load)
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace !important;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.sticky-actions {
|
||||
display: grid;
|
||||
gap: 8px;
|
||||
margin-top: 16px;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user