| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- const n = `<template>
- <s-modal
- v-model:value="visible"
- :title="$l('主题配置')"
- width="620px"
- :save-btn-text="$l('完成')"
- @handSave="handleSave"
- @handCancel="handleCancel"
- >
- <div class="s-theme-config">
- <!-- 1. 预设主题:点击即切换,当前项高亮 -->
- <div class="section">
- <div class="section-title">{{ $l('预设主题') }}</div>
- <div class="preset-list">
- <div
- v-for="item in presets"
- :key="item.name"
- class="preset-item"
- :class="{ active: draft.name === item.name }"
- :title="item.color"
- @click="choosePreset(item)"
- >
- <span class="dot" :style="{ backgroundColor: item.color }"></span>
- <span class="name">{{ item.name }}</span>
- </div>
- </div>
- </div>
- <div class="theme-container">
- <div class="disabled-box" v-if="disabled"></div>
- <!-- 2. 主题色:只改主色,同族浅色/深色/边框色自动重算 -->
- <div class="section">
- <div class="section-title">{{ $l('主题色') }}</div>
- <div class="color-row">
- <input
- class="color-picker"
- type="color"
- :value="draft.color"
- @input="onMainColor($event.target.value)"
- />
- <a-input
- class="hex-input"
- :value="draft.color"
- :maxlength="7"
- @change="onHexInput"
- @blur="onHexBlur"
- />
- <span class="tip">{{ $l('修改后立即生效') }}</span>
- </div>
- <div class="theme-preview" :style="{ backgroundColor: draft.color }"></div>
- </div>
- <!-- 3. customer 主题的色值:改完自动切到 customer -->
- <div class="section">
- <div class="section-title">{{ $l('customer 颜色') }}</div>
- <div class="customer-grid">
- <div v-for="field in customerFields" :key="field.key" class="customer-cell">
- <span class="cell-label">{{ $l(field.label) }}</span>
- <input
- class="color-picker small"
- type="color"
- :value="validColor(customerDraft[field.key])"
- @input="onCustomerColor(field.key, $event.target.value)"
- />
- <span class="cell-hex">{{ customerDraft[field.key] }}</span>
- </div>
- </div>
- <div class="tip">{{ $l('修改 customer 颜色会自动切换到 customer 主题') }}</div>
- </div>
- </div>
- <div class="actions">
- <a-button size="small" @click="handleReset">{{ $l('恢复默认') }}</a-button>
- </div>
- </div>
- </s-modal>
- </template>
- <script setup>
- import { computed, ref, watch } from 'vue'
- import {
- CUSTOMER_FIELDS,
- applyTheme,
- deriveThemeByColor,
- getCustomerTheme,
- getPresetList,
- resetTheme,
- saveCustomerTheme
- } from '../../utils/theme-config'
- const props = defineProps({
- /**
- * 是否显示,配合 v-model:value 使用
- */
- value: {
- type: Boolean,
- default: false
- }
- })
- const emit = defineEmits(['update:value', 'change'])
- const visible = computed({
- get: () => props.value,
- set: (v) => emit('update:value', v)
- })
- const disabled = computed(() => {
- return draft.value.code !== 'customer'
- })
- const presets = ref(getPresetList())
- const customerFields = CUSTOMER_FIELDS
- // 弹窗内的草稿:改动直接作用到 window.$theme,所以初值就是当前主题
- const draft = ref({ ...(window.$theme || {}) })
- // customer 是唯一可改色的自定义主题,单独一份草稿,与「当前选中主题」解耦
- const customerDraft = ref(getCustomerTheme())
- const COLOR_RE = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
- const validColor = (val) =>
- COLOR_RE.test(String(val || '').trim()) ? String(val).trim() : '#000000'
- // 每次打开都重新对齐当前主题与预设列表(预设可能被部署目录 config.json 改过)
- watch(
- () => props.value,
- (v) => {
- if (!v) return
- presets.value = getPresetList()
- draft.value = { ...(window.$theme || {}) }
- customerDraft.value = getCustomerTheme()
- }
- )
- const commit = () => {
- const next = applyTheme(draft.value)
- draft.value = { ...next }
- emit('change', { ...next })
- }
- const choosePreset = (item) => {
- draft.value = { ...item }
- commit()
- }
- const onMainColor = (color) => {
- draft.value = deriveThemeByColor(color)
- commit()
- }
- const onHexInput = (e) => {
- const val = e.target.value
- draft.value = { ...draft.value, color: val }
- }
- // 失焦才应用:输入过程中非法值不能写进 :root,会把整站颜色打乱
- const onHexBlur = (e) => {
- const val = String(e.target.value || '').trim()
- if (!COLOR_RE.test(val)) {
- draft.value = { ...draft.value, color: (window.$theme && window.$theme.color) || '' }
- return
- }
- onMainColor(val)
- }
- const onCustomerColor = (key, color) => {
- // 先存定义(切走再切回来颜色不丢),再把它作为当前主题应用
- customerDraft.value = saveCustomerTheme({ [key]: color })
- draft.value = { ...customerDraft.value }
- presets.value = getPresetList()
- commit()
- }
- const handleReset = () => {
- const next = resetTheme()
- draft.value = { ...next }
- customerDraft.value = getCustomerTheme()
- presets.value = getPresetList()
- emit('change', { ...next })
- }
- const handleSave = () => {
- visible.value = false
- }
- const handleCancel = () => {
- visible.value = false
- }
- <\/script>
- <style lang="less" scoped>
- .s-theme-config {
- .section {
- margin-bottom: 18px;
- .section-title {
- font-weight: 500;
- font-size: 14px;
- color: var(--s-text-color);
- margin-bottom: 10px;
- }
- .tip {
- font-size: 12px;
- color: var(--s-text-color-secondary);
- }
- }
- .preset-list {
- display: flex;
- flex-wrap: wrap;
- gap: 10px;
- .preset-item {
- display: flex;
- align-items: center;
- padding: 6px 12px;
- border: 1px solid var(--s-border-color-base);
- border-radius: 4px;
- cursor: pointer;
- transition: all 0.2s;
- &:hover {
- border-color: var(--s-color);
- background-color: var(--s-bg-color-hover);
- }
- &.active {
- border-color: var(--s-color);
- background-color: var(--s-color-bg);
- color: var(--s-color);
- }
- .dot {
- width: 16px;
- height: 16px;
- border-radius: 50%;
- margin-right: 8px;
- border: 1px solid rgba(0, 0, 0, 0.06);
- }
- .name {
- font-size: 13px;
- }
- }
- }
- .color-row {
- display: flex;
- align-items: center;
- gap: 10px;
- .hex-input {
- width: 110px;
- }
- }
- .theme-preview {
- height: 8px;
- border-radius: 4px;
- margin-top: 12px;
- }
- .theme-container {
- position: relative;
- padding: 10px;
- box-sizing: border-box;
- .disabled-box {
- width: 100%;
- height: 100%;
- position: absolute;
- top: 0;
- left: 0;
- z-index: 2;
- background-color: #72717117;
- border-radius: 8px;
- }
- }
- .customer-grid {
- display: grid;
- grid-template-columns: repeat(2, 1fr);
- gap: 8px 16px;
- margin-bottom: 8px;
- .customer-cell {
- display: flex;
- align-items: center;
- gap: 8px;
- .cell-label {
- width: 48px;
- font-size: 13px;
- color: var(--s-text-color-regular);
- }
- .cell-hex {
- font-size: 12px;
- color: var(--s-text-color-secondary);
- font-family: Consolas, Monaco, monospace;
- }
- }
- }
- // 原生取色器:去掉内边距与边框,做成方形色块
- .color-picker {
- width: 32px;
- height: 32px;
- padding: 0;
- border: 1px solid var(--s-border-color-base);
- border-radius: 4px;
- background: transparent;
- cursor: pointer;
- &.small {
- width: 24px;
- height: 24px;
- }
- &::-webkit-color-swatch-wrapper {
- padding: 2px;
- }
- &::-webkit-color-swatch {
- border: none;
- border-radius: 2px;
- }
- &::-moz-color-swatch {
- border: none;
- border-radius: 2px;
- }
- }
- .actions {
- text-align: right;
- border-top: 1px solid var(--s-border-color-split);
- padding-top: 12px;
- }
- }
- </style>
- `;
- export {
- n as default
- };
|