SThemeConfig2.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. const n = `<template>
  2. <s-modal
  3. v-model:value="visible"
  4. :title="$l('主题配置')"
  5. width="620px"
  6. :save-btn-text="$l('完成')"
  7. @handSave="handleSave"
  8. @handCancel="handleCancel"
  9. >
  10. <div class="s-theme-config">
  11. <!-- 1. 预设主题:点击即切换,当前项高亮 -->
  12. <div class="section">
  13. <div class="section-title">{{ $l('预设主题') }}</div>
  14. <div class="preset-list">
  15. <div
  16. v-for="item in presets"
  17. :key="item.name"
  18. class="preset-item"
  19. :class="{ active: draft.name === item.name }"
  20. :title="item.color"
  21. @click="choosePreset(item)"
  22. >
  23. <span class="dot" :style="{ backgroundColor: item.color }"></span>
  24. <span class="name">{{ item.name }}</span>
  25. </div>
  26. </div>
  27. </div>
  28. <div class="theme-container">
  29. <div class="disabled-box" v-if="disabled"></div>
  30. <!-- 2. 主题色:只改主色,同族浅色/深色/边框色自动重算 -->
  31. <div class="section">
  32. <div class="section-title">{{ $l('主题色') }}</div>
  33. <div class="color-row">
  34. <input
  35. class="color-picker"
  36. type="color"
  37. :value="draft.color"
  38. @input="onMainColor($event.target.value)"
  39. />
  40. <a-input
  41. class="hex-input"
  42. :value="draft.color"
  43. :maxlength="7"
  44. @change="onHexInput"
  45. @blur="onHexBlur"
  46. />
  47. <span class="tip">{{ $l('修改后立即生效') }}</span>
  48. </div>
  49. <div class="theme-preview" :style="{ backgroundColor: draft.color }"></div>
  50. </div>
  51. <!-- 3. customer 主题的色值:改完自动切到 customer -->
  52. <div class="section">
  53. <div class="section-title">{{ $l('customer 颜色') }}</div>
  54. <div class="customer-grid">
  55. <div v-for="field in customerFields" :key="field.key" class="customer-cell">
  56. <span class="cell-label">{{ $l(field.label) }}</span>
  57. <input
  58. class="color-picker small"
  59. type="color"
  60. :value="validColor(customerDraft[field.key])"
  61. @input="onCustomerColor(field.key, $event.target.value)"
  62. />
  63. <span class="cell-hex">{{ customerDraft[field.key] }}</span>
  64. </div>
  65. </div>
  66. <div class="tip">{{ $l('修改 customer 颜色会自动切换到 customer 主题') }}</div>
  67. </div>
  68. </div>
  69. <div class="actions">
  70. <a-button size="small" @click="handleReset">{{ $l('恢复默认') }}</a-button>
  71. </div>
  72. </div>
  73. </s-modal>
  74. </template>
  75. <script setup>
  76. import { computed, ref, watch } from 'vue'
  77. import {
  78. CUSTOMER_FIELDS,
  79. applyTheme,
  80. deriveThemeByColor,
  81. getCustomerTheme,
  82. getPresetList,
  83. resetTheme,
  84. saveCustomerTheme
  85. } from '../../utils/theme-config'
  86. const props = defineProps({
  87. /**
  88. * 是否显示,配合 v-model:value 使用
  89. */
  90. value: {
  91. type: Boolean,
  92. default: false
  93. }
  94. })
  95. const emit = defineEmits(['update:value', 'change'])
  96. const visible = computed({
  97. get: () => props.value,
  98. set: (v) => emit('update:value', v)
  99. })
  100. const disabled = computed(() => {
  101. return draft.value.code !== 'customer'
  102. })
  103. const presets = ref(getPresetList())
  104. const customerFields = CUSTOMER_FIELDS
  105. // 弹窗内的草稿:改动直接作用到 window.$theme,所以初值就是当前主题
  106. const draft = ref({ ...(window.$theme || {}) })
  107. // customer 是唯一可改色的自定义主题,单独一份草稿,与「当前选中主题」解耦
  108. const customerDraft = ref(getCustomerTheme())
  109. const COLOR_RE = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
  110. const validColor = (val) =>
  111. COLOR_RE.test(String(val || '').trim()) ? String(val).trim() : '#000000'
  112. // 每次打开都重新对齐当前主题与预设列表(预设可能被部署目录 config.json 改过)
  113. watch(
  114. () => props.value,
  115. (v) => {
  116. if (!v) return
  117. presets.value = getPresetList()
  118. draft.value = { ...(window.$theme || {}) }
  119. customerDraft.value = getCustomerTheme()
  120. }
  121. )
  122. const commit = () => {
  123. const next = applyTheme(draft.value)
  124. draft.value = { ...next }
  125. emit('change', { ...next })
  126. }
  127. const choosePreset = (item) => {
  128. draft.value = { ...item }
  129. commit()
  130. }
  131. const onMainColor = (color) => {
  132. draft.value = deriveThemeByColor(color)
  133. commit()
  134. }
  135. const onHexInput = (e) => {
  136. const val = e.target.value
  137. draft.value = { ...draft.value, color: val }
  138. }
  139. // 失焦才应用:输入过程中非法值不能写进 :root,会把整站颜色打乱
  140. const onHexBlur = (e) => {
  141. const val = String(e.target.value || '').trim()
  142. if (!COLOR_RE.test(val)) {
  143. draft.value = { ...draft.value, color: (window.$theme && window.$theme.color) || '' }
  144. return
  145. }
  146. onMainColor(val)
  147. }
  148. const onCustomerColor = (key, color) => {
  149. // 先存定义(切走再切回来颜色不丢),再把它作为当前主题应用
  150. customerDraft.value = saveCustomerTheme({ [key]: color })
  151. draft.value = { ...customerDraft.value }
  152. presets.value = getPresetList()
  153. commit()
  154. }
  155. const handleReset = () => {
  156. const next = resetTheme()
  157. draft.value = { ...next }
  158. customerDraft.value = getCustomerTheme()
  159. presets.value = getPresetList()
  160. emit('change', { ...next })
  161. }
  162. const handleSave = () => {
  163. visible.value = false
  164. }
  165. const handleCancel = () => {
  166. visible.value = false
  167. }
  168. <\/script>
  169. <style lang="less" scoped>
  170. .s-theme-config {
  171. .section {
  172. margin-bottom: 18px;
  173. .section-title {
  174. font-weight: 500;
  175. font-size: 14px;
  176. color: var(--s-text-color);
  177. margin-bottom: 10px;
  178. }
  179. .tip {
  180. font-size: 12px;
  181. color: var(--s-text-color-secondary);
  182. }
  183. }
  184. .preset-list {
  185. display: flex;
  186. flex-wrap: wrap;
  187. gap: 10px;
  188. .preset-item {
  189. display: flex;
  190. align-items: center;
  191. padding: 6px 12px;
  192. border: 1px solid var(--s-border-color-base);
  193. border-radius: 4px;
  194. cursor: pointer;
  195. transition: all 0.2s;
  196. &:hover {
  197. border-color: var(--s-color);
  198. background-color: var(--s-bg-color-hover);
  199. }
  200. &.active {
  201. border-color: var(--s-color);
  202. background-color: var(--s-color-bg);
  203. color: var(--s-color);
  204. }
  205. .dot {
  206. width: 16px;
  207. height: 16px;
  208. border-radius: 50%;
  209. margin-right: 8px;
  210. border: 1px solid rgba(0, 0, 0, 0.06);
  211. }
  212. .name {
  213. font-size: 13px;
  214. }
  215. }
  216. }
  217. .color-row {
  218. display: flex;
  219. align-items: center;
  220. gap: 10px;
  221. .hex-input {
  222. width: 110px;
  223. }
  224. }
  225. .theme-preview {
  226. height: 8px;
  227. border-radius: 4px;
  228. margin-top: 12px;
  229. }
  230. .theme-container {
  231. position: relative;
  232. padding: 10px;
  233. box-sizing: border-box;
  234. .disabled-box {
  235. width: 100%;
  236. height: 100%;
  237. position: absolute;
  238. top: 0;
  239. left: 0;
  240. z-index: 2;
  241. background-color: #72717117;
  242. border-radius: 8px;
  243. }
  244. }
  245. .customer-grid {
  246. display: grid;
  247. grid-template-columns: repeat(2, 1fr);
  248. gap: 8px 16px;
  249. margin-bottom: 8px;
  250. .customer-cell {
  251. display: flex;
  252. align-items: center;
  253. gap: 8px;
  254. .cell-label {
  255. width: 48px;
  256. font-size: 13px;
  257. color: var(--s-text-color-regular);
  258. }
  259. .cell-hex {
  260. font-size: 12px;
  261. color: var(--s-text-color-secondary);
  262. font-family: Consolas, Monaco, monospace;
  263. }
  264. }
  265. }
  266. // 原生取色器:去掉内边距与边框,做成方形色块
  267. .color-picker {
  268. width: 32px;
  269. height: 32px;
  270. padding: 0;
  271. border: 1px solid var(--s-border-color-base);
  272. border-radius: 4px;
  273. background: transparent;
  274. cursor: pointer;
  275. &.small {
  276. width: 24px;
  277. height: 24px;
  278. }
  279. &::-webkit-color-swatch-wrapper {
  280. padding: 2px;
  281. }
  282. &::-webkit-color-swatch {
  283. border: none;
  284. border-radius: 2px;
  285. }
  286. &::-moz-color-swatch {
  287. border: none;
  288. border-radius: 2px;
  289. }
  290. }
  291. .actions {
  292. text-align: right;
  293. border-top: 1px solid var(--s-border-color-split);
  294. padding-top: 12px;
  295. }
  296. }
  297. </style>
  298. `;
  299. export {
  300. n as default
  301. };