Skip to content

Commit 7945ae9

Browse files
committed
refactor: enhance token management 😈
1 parent 8128c0b commit 7945ae9

File tree

25 files changed

+682
-763
lines changed

25 files changed

+682
-763
lines changed

packages/theme-generator/src/Generator.vue

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import {
1515
applyTokenFromLocal,
1616
initGeneratorVars,
17-
initThemeStyleSheet,
1817
syncModeToGenerator,
1918
syncThemeToIframe,
2019
themeStore,
@@ -39,11 +38,6 @@ export default {
3938
default: 'web',
4039
},
4140
},
42-
provide() {
43-
return {
44-
$device: this.device,
45-
};
46-
},
4741
data() {
4842
return {
4943
visible: 0,
@@ -55,9 +49,9 @@ export default {
5549
},
5650
},
5751
mounted() {
52+
themeStore.updateDevice(this.device);
5853
syncModeToGenerator();
5954
initGeneratorVars();
60-
initThemeStyleSheet(this.$theme.enName);
6155
applyTokenFromLocal();
6256
syncThemeToIframe(this.device);
6357
},
@@ -87,7 +81,8 @@ export default {
8781
</style>
8882
<style>
8983
:host {
90-
--td-bg-color-container: var(--bg-color-container);
84+
--td-brand-main-hover: var(--brand-main-hover);
85+
--td-bg-color-container: var(--bg-color-theme-secondary);
9186
--td-bg-color-secondarycomponent: var(--bg-color-theme-secondary);
9287
--td-bg-color-container-hover: var(--bg-color-container-hover);
9388
--td-bg-color-component-active: var(--bg-color-component-hover);

packages/theme-generator/src/color-panel/components/ColorColumn/index.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ export default {
118118
},
119119
},
120120
mounted() {
121-
this.$root.$on('refresh-color-tokens', this.$forceUpdate);
121+
this.$root.$on('refresh-color-tokens', () => {
122+
this.$forceUpdate();
123+
});
122124
},
123125
methods: {
124126
getTokenValue,

packages/theme-generator/src/color-panel/index.vue

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
<div
1818
:class="{
1919
'color-content__block': true,
20-
'is-active': $brandColor.toLocaleLowerCase() === color.value.toLocaleLowerCase() && !isMoreVisible,
20+
'is-active': $brandColor.toLowerCase() === color.value.toLowerCase() && !isMoreVisible,
2121
}"
2222
:style="{ paddingBottom: '4px', color: 'var(--text-secondary)' }"
2323
>
2424
<div
2525
@click="changeBrandColor(color.value)"
26-
:class="{ 'is-active': $brandColor.toLocaleLowerCase() === color.value.toLocaleLowerCase() }"
26+
:class="{ 'is-active': $brandColor.toLowerCase() === color.value.toLowerCase() }"
2727
>
2828
<div
2929
:style="{
@@ -159,8 +159,8 @@
159159
'background-color': 'var(--brand-main)',
160160
}"
161161
>
162-
<p>hsv: {{ covert2Hex(brandDisplayedColor, 'hsv') }}</p>
163-
<p>rgb: {{ covert2Hex(brandDisplayedColor, 'rgb') }}</p>
162+
<p>hsv: {{ convertFromHex(brandDisplayedColor, 'hsv') }}</p>
163+
<p>rgb: {{ convertFromHex(brandDisplayedColor, 'rgb') }}</p>
164164
</div>
165165
<div class="color-content__custom-bottom">
166166
<div>
@@ -331,14 +331,17 @@ import { ColorPicker } from '../common/components';
331331
import { langMixin } from '../common/i18n';
332332
import {
333333
collectTokenIndexes,
334-
covert2Hex,
334+
convertFromHex,
335335
generateBrandPalette,
336336
generateFunctionalPalette,
337337
generateNeutralPalette,
338338
getOptionFromLocal,
339+
isMobile,
339340
modifyToken,
340341
syncColorTokensToStyle,
341342
themeStore,
343+
updateLocalOption,
344+
updateLocalToken,
342345
updateStyleSheetColor,
343346
} from '../common/themes';
344347
import { colorAnimation, getThemeMode, getTokenValue, handleAttach, setUpModeObserver } from '../common/utils';
@@ -387,20 +390,23 @@ export default {
387390
dark: 8,
388391
},
389392
currentBrandIdx: 7,
390-
brandTokenMap: [''], // `-td-brand-x` 系列的 token 映射需要根据 brandIdx 动态计算;其它功能色都是固定的
393+
brandTokenMap: [], // `-td-brand-x` 系列的 token 映射需要根据 brandIdx 动态计算;其它功能色都是固定的
391394
grayMainColor: getOptionFromLocal('gray') || getTokenValue('--td-bg-color-secondarycontainer-active'),
392395
successMainColor: getOptionFromLocal('success') || getTokenValue('--td-success-color'),
393396
errorMainColor: getOptionFromLocal('error') || getTokenValue('--td-error-color'),
394397
warningMainColor: getOptionFromLocal('warning') || getTokenValue('--td-warning-color'),
395-
generationMode: 'remain',
396-
isGrayRelatedToTheme: getOptionFromLocal('neutral') == true,
398+
generationMode: getOptionFromLocal('recommend') === 'true' ? 'recommend' : 'remain', // remain: 保留输入, recommend: 智能推荐
399+
isGrayRelatedToTheme: getOptionFromLocal('neutral') == 'true',
397400
isMoreVisible: false,
398401
};
399402
},
400403
computed: {
401404
$theme() {
402405
return themeStore.theme;
403406
},
407+
$device() {
408+
return themeStore.device;
409+
},
404410
isRemainMode() {
405411
return this.generationMode === 'remain';
406412
},
@@ -419,9 +425,6 @@ export default {
419425
},
420426
},
421427
watch: {
422-
$theme(newTheme) {
423-
this.changeBrandColor(newTheme.value);
424-
},
425428
generationMode() {
426429
this.changeBrandColor(this.brandDisplayedColor);
427430
},
@@ -441,20 +444,20 @@ export default {
441444
},
442445
methods: {
443446
handleAttach,
444-
covert2Hex,
447+
convertFromHex,
445448
generateBrandTokenMap(brandIdx) {
446449
const hoverIdx = brandIdx - 1;
447450
const activeIdx = brandIdx > 8 ? brandIdx : brandIdx + 1;
448451
return [
449-
{ name: '--td-brand-color-hover', idx: hoverIdx },
452+
...(!isMobile(this.$device) ? [{ name: '--td-brand-color-hover', idx: hoverIdx }] : []),
450453
{ name: '--td-brand-color', idx: brandIdx },
451454
{ name: '--td-brand-color-active', idx: activeIdx },
452455
];
453456
},
454457
updateBrandTokenMap() {
455458
const brandIdx = this.currentBrandIdx;
456-
const extraTokens = this.generateBrandTokenMap(brandIdx);
457-
this.brandTokenMap = BRAND_TOKEN_MAP.concat(extraTokens);
459+
const extraBrandTokens = this.generateBrandTokenMap(brandIdx);
460+
this.brandTokenMap = BRAND_TOKEN_MAP.concat(extraBrandTokens);
458461
},
459462
updateFunctionTokenMap() {
460463
Object.keys(FUNCTION_TOKENS).forEach((type) => {
@@ -474,43 +477,64 @@ export default {
474477
};
475478
476479
const newBrandColor = lightPalette[lightBrandIdx - 1].toUpperCase();
477-
themeStore.setBrandColorState(newBrandColor);
478-
479-
this.currentBrandIdx = this.brandIndexes[getThemeMode()];
480-
updateStyleSheetColor('brand', lightPalette, darkPalette);
480+
themeStore.updateBrandColor(newBrandColor);
481+
updateLocalOption(
482+
'color',
483+
newBrandColor.toLowerCase() !== this.$theme.value.toLowerCase() ? this.brandInputColor : null,
484+
);
485+
console.log(this.isRemainMode, this.generationMode);
486+
updateLocalOption('recommend', !this.isRemainMode ? 'true' : null);
481487
482488
const lightExtraTokens = this.generateBrandTokenMap(lightBrandIdx);
483489
const darkExtraTokens = this.generateBrandTokenMap(darkBrandIdx);
484-
syncColorTokensToStyle(lightExtraTokens, darkExtraTokens);
490+
491+
this.currentBrandIdx = this.brandIndexes[getThemeMode()];
492+
493+
if (this.$brandColor != this.$theme.value) {
494+
// 只在用户手动修改主题色时同步 stylesheet,避免覆盖内置主题自身的逻辑
495+
updateStyleSheetColor('brand', lightPalette, darkPalette);
496+
syncColorTokensToStyle(lightExtraTokens, darkExtraTokens);
497+
this.changeNeutralColor(this.isGrayRelatedToTheme);
498+
}
485499
486500
this.updateBrandTokenMap();
487-
this.changeNeutralColor(this.isGrayRelatedToTheme);
488501
},
489502
changeNeutralColor(related) {
490-
// updateLocalOption('neutral', true, related);
491-
// updateLocalOption('gray', this.grayMainColor, !related && this.grayMainColor !== DEFAULT_FUNCTION_COLORS['gray']);
503+
updateLocalOption('neutral', related ? 'true' : null);
492504
const inputHex = related ? this.$brandColor : this.grayMainColor;
493505
const palette = generateNeutralPalette(inputHex, related);
494506
updateStyleSheetColor('gray', palette, palette);
495-
this.$nextTick(this.refreshAllTokens);
507+
this.$nextTick(this.refreshColorTokens);
496508
},
497509
changeFunctionColor(hex, type) {
498-
this[`${type}MainColor`] = hex;
510+
const oldColor = getOptionFromLocal(type);
511+
updateLocalOption(type, oldColor !== hex ? hex : null);
512+
499513
if (type === 'gray') {
500514
this.changeNeutralColor(false);
501515
return;
502516
}
517+
this[`${type}MainColor`] = hex;
503518
const { lightPalette, darkPalette } = generateFunctionalPalette(hex);
504519
updateStyleSheetColor(type, lightPalette, darkPalette);
505-
// updateLocalOption(type, hex, DEFAULT_FUNCTION_COLORS[type] !== hex);
506-
this.$nextTick(this.refreshAllTokens);
520+
this.$nextTick(this.refreshColorTokens);
507521
},
508522
changeGradation(hex, idx, type, saveToLocal = true) {
509-
const tokenName = `--td-${type}-color-${idx + 1}`;
523+
const tokenName = `--td-${type}-color-${idx}`;
510524
modifyToken(tokenName, hex, saveToLocal);
511-
this.$forceUpdate();
525+
this.$nextTick(this.refreshColorTokens);
512526
},
513527
recoverGradation(type) {
528+
Array(14) // 最长的情况为 gray 的 14 个色阶(虽然理论上有些 token 用户无法直接在 UI 中修改)
529+
.fill(0)
530+
.forEach((_, idx) => {
531+
const tokenName = `--td-${type}-color-${idx + 1}`;
532+
updateLocalToken(tokenName, undefined, false); // 清空本地缓存值
533+
});
534+
if (type === 'brand') {
535+
this.changeBrandColor(this.brandInputColor);
536+
return;
537+
}
514538
this.changeFunctionColor(this[`${type}MainColor`], type);
515539
},
516540
refreshColorTokens() {

packages/theme-generator/src/common/components/SegmentSelection/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</div>
77
<div class="segment-panel__round-slider">
88
<div
9-
v-for="(v, i) in selectOptions.slice(0, 5)"
9+
v-for="(_, i) in selectOptions.slice(0, 5)"
1010
:key="i"
1111
class="slider-split"
1212
:style="{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
:root {
2+
--td-font-family: pingfang sc, microsoft yahei, arial regular;
3+
--td-font-family-medium: pingfang sc, microsoft yahei, arial medium;
4+
--td-font-size-link-small: 12px;
5+
--td-font-size-link-medium: 14px;
6+
--td-font-size-link-large: 16px;
7+
--td-font-size-mark-extra-small: 10px;
8+
--td-font-size-mark-small: 12px;
9+
--td-font-size-mark-medium: 14px;
10+
--td-font-size-mark-large: 16px;
11+
--td-font-size-body-extra-small: 10px;
12+
--td-font-size-body-small: 12px;
13+
--td-font-size-body-medium: 14px;
14+
--td-font-size-body-large: 16px;
15+
--td-font-size-title-small: 14px;
16+
--td-font-size-title-medium: 16px;
17+
--td-font-size-title-large: 18px;
18+
--td-font-size-title-extra-large: 20px;
19+
--td-font-size-headline-small: 24px;
20+
--td-font-size-headline-medium: 28px;
21+
--td-font-size-headline-large: 36px;
22+
--td-font-size-display-medium: 48px;
23+
--td-font-size-display-large: 64px;
24+
--td-font-size: 10px;
25+
--td-font-size-xs: var(--td-font-size-body-extra-small);
26+
--td-font-size-s: var(--td-font-size-body-small);
27+
--td-font-size-base: var(--td-font-size-title-small);
28+
--td-font-size-m: var(--td-font-size-title-medium);
29+
--td-font-size-l: var(--td-font-size-title-large);
30+
--td-font-size-xl: var(--td-font-size-title-extra-large);
31+
--td-font-size-xxl: var(--td-font-size-headline-large);
32+
33+
--td-radius-small: 3px;
34+
--td-radius-default: 6px;
35+
--td-radius-large: 9px;
36+
--td-radius-extraLarge: 12px;
37+
--td-radius-round: 999px;
38+
--td-radius-circle: 50%;
39+
}

0 commit comments

Comments
 (0)