Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 将代码迁移至TS严格模式 #684

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev:linux": "vite --mode development",
"build:test": "vite build --mode test",
"build": "vue-tsc --noEmit && vite build --mode release",
"build:type": "vue-tsc --noEmit",
"build:site": "vue-tsc --noEmit && vite build --mode site",
"preview": "vite preview",
"lint": "eslint --ext .vue,.js,.jsx,.ts,.tsx ./ --max-warnings 0",
Expand Down
34 changes: 0 additions & 34 deletions src/hooks/event/useWindowSizeFn.ts

This file was deleted.

8 changes: 5 additions & 3 deletions src/layouts/components/FrameContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
</div>
</template>
<script lang="ts" setup>
import { useWindowSize } from '@vueuse/core';
import debounce from 'lodash/debounce';
import { computed, CSSProperties, ref, unref, watch } from 'vue';

import { prefix } from '@/config/global';
import { useWindowSizeFn } from '@/hooks/event/useWindowSizeFn';
import { useSettingStore } from '@/store';

defineProps({
frameSrc: String,
});

const { width, height } = useWindowSize();

const loading = ref(true);
const heightRef = ref(window.innerHeight);
const frameRef = ref<HTMLFrameElement>();
Expand Down Expand Up @@ -69,8 +71,8 @@ function hideLoading() {
calcHeight();
}

useWindowSizeFn(calcHeight, { immediate: true });

// 如果窗口大小发生变化
watch([width, height], debounce(calcHeight, 250));
watch(
[() => settingStore.showFooter, () => settingStore.isUseTabsRouter, () => settingStore.showBreadcrumb],
debounce(calcHeight, 250),
Expand Down
16 changes: 10 additions & 6 deletions src/layouts/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
<translate-icon />
</t-button>
<t-dropdown-menu>
<t-dropdown-item v-for="(lang, index) in langList" :key="index" :value="lang.value" @click="changeLang">{{
lang.content
}}</t-dropdown-item></t-dropdown-menu
<t-dropdown-item
v-for="(lang, index) in langList"
:key="index"
:value="lang.value"
@click="(options) => changeLang(options.value as string)"
>{{ lang.content }}</t-dropdown-item
></t-dropdown-menu
>
</t-dropdown>
<t-dropdown :min-column-width="120" trigger="click">
Expand Down Expand Up @@ -85,7 +89,7 @@ import { langList } from '@/locales/index';
import { useLocale } from '@/locales/useLocale';
import { getActive } from '@/router';
import { useSettingStore, useUserStore } from '@/store';
import type { MenuRoute } from '@/types/interface';
import type { MenuRoute, ModeType } from '@/types/interface';

import MenuContent from './MenuContent.vue';
import Notice from './Notice.vue';
Expand Down Expand Up @@ -147,11 +151,11 @@ const menuCls = computed(() => {
},
];
});
const menuTheme = computed(() => props.theme as 'light' | 'dark');
const menuTheme = computed(() => props.theme as ModeType);

// 切换语言
const { changeLocale } = useLocale();
const changeLang = ({ value: lang }: { value: string }) => {
const changeLang = (lang: string) => {
changeLocale(lang);
};

Expand Down
2 changes: 1 addition & 1 deletion src/layouts/components/LayoutContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
:class="`${prefix}-layout-tabs-nav`"
:value="$route.path"
:style="{ position: 'sticky', top: 0, width: '100%' }"
@change="handleChangeCurrentTab"
@change="(value) => handleChangeCurrentTab(value as string)"
@remove="handleRemove"
@drag-sort="handleDragend"
>
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/components/SideNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import AssetLogo from '@/assets/assets-t-logo.svg?component';
import { prefix } from '@/config/global';
import { getActive, getRoutesExpanded } from '@/router';
import { useSettingStore } from '@/store';
import type { MenuRoute } from '@/types/interface';
import type { MenuRoute, ModeType } from '@/types/interface';

import pgk from '../../../package.json';
import MenuContent from './MenuContent.vue';
Expand Down Expand Up @@ -55,7 +55,7 @@ const props = defineProps({
default: '64px',
},
theme: {
type: String as PropType<'light' | 'dark'>,
type: String as PropType<ModeType>,
default: 'light',
},
isCompact: {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dashboard/base/components/MiddleChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
theme="primary"
mode="date"
:default-value="LAST_7_DAYS"
@change="onCurrencyChange"
@change="(value) => onCurrencyChange(value as string[])"
/>
</div>
</template>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dashboard/base/components/OutputOverview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
theme="primary"
mode="date"
:default-value="LAST_7_DAYS"
@change="onStokeDataChange"
@change="(value) => onStokeDataChange(value as string[])"
/>
</template>
<div id="stokeContainer" style="width: 100%; height: 351px" class="dashboard-chart-container"></div>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dashboard/detail/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
theme="primary"
mode="date"
style="width: 248px"
@change="onMaterialChange"
@change="(value) => onMaterialChange(value as string[])"
/>
</template>
<div id="lineContainer" style="width: 100%; height: 416px" />
Expand Down
2 changes: 1 addition & 1 deletion src/pages/list/base/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
:header-affixed-top="headerAffixedTop"
@page-change="rehandlePageChange"
@change="rehandleChange"
@select-change="rehandleSelectChange"
@select-change="(value) => rehandleSelectChange(value as number[])"
>
<template #status="{ row }">
<t-tag v-if="row.status === CONTRACT_STATUS.FAIL" theme="danger" variant="light">
Expand Down
2 changes: 1 addition & 1 deletion src/pages/list/card/components/DialogForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const formVisible = ref(false);
const formData = ref({ ...INITIAL_DATA });
const textareaValue = ref('');

const onSubmit = ({ validateResult, firstError }: SubmitContext<FormData>) => {
const onSubmit = ({ validateResult, firstError }: SubmitContext) => {
if (!firstError) {
MessagePlugin.success('提交成功');
formVisible.value = false;
Expand Down
29 changes: 15 additions & 14 deletions src/store/modules/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import keys from 'lodash/keys';
import { defineStore } from 'pinia';
import { Color } from 'tvision-color';

import { DARK_CHART_COLORS, LIGHT_CHART_COLORS } from '@/config/color';
import { DARK_CHART_COLORS, LIGHT_CHART_COLORS, TColorSeries } from '@/config/color';
import STYLE_CONFIG from '@/config/style';
import { store } from '@/store';
import { ModeType } from '@/types/interface';
import { generateColorMap, insertThemeStylesheet } from '@/utils/color';

const state = {
const state: Record<string, any> = {
...STYLE_CONFIG,
showSettingPanel: false,
colorList: {},
colorList: {} as TColorSeries,
chartColors: LIGHT_CHART_COLORS,
};

Expand All @@ -23,22 +24,22 @@ export const useSettingStore = defineStore('setting', {
showSidebar: (state) => state.layout !== 'top',
showSidebarLogo: (state) => state.layout === 'side',
showHeaderLogo: (state) => state.layout !== 'side',
displayMode: (state): 'dark' | 'light' => {
displayMode: (state): ModeType => {
if (state.mode === 'auto') {
const media = window.matchMedia('(prefers-color-scheme:dark)');
if (media.matches) {
return 'dark';
}
return 'light';
}
return state.mode as 'dark' | 'light';
return state.mode as ModeType;
},
displaySideMode: (state): 'dark' | 'light' => {
return state.sideMode as 'dark' | 'light';
displaySideMode: (state): ModeType => {
return state.sideMode as ModeType;
},
},
actions: {
async changeMode(mode: 'dark' | 'light' | 'auto') {
async changeMode(mode: ModeType | 'auto') {
let theme = mode;

if (mode === 'auto') {
Expand All @@ -50,7 +51,7 @@ export const useSettingStore = defineStore('setting', {

this.chartColors = isDarkMode ? DARK_CHART_COLORS : LIGHT_CHART_COLORS;
},
async changeSideMode(mode: 'dark' | 'light') {
async changeSideMode(mode: ModeType) {
const isDarkMode = mode === 'dark';

document.documentElement.setAttribute('side-mode', isDarkMode ? 'dark' : '');
Expand All @@ -75,23 +76,23 @@ export const useSettingStore = defineStore('setting', {
step: 10,
remainInput: false, // 是否保留输入 不保留会矫正不合适的主题色
});
colorMap = generateColorMap(brandTheme, newPalette, mode as 'light' | 'dark', brandColorIndex);
colorMap = generateColorMap(brandTheme, newPalette, mode, brandColorIndex);
this.colorList[colorKey] = colorMap;
}
// TODO 需要解决不停切换时有反复插入 style 的问题
insertThemeStylesheet(brandTheme, colorMap, mode as 'light' | 'dark');
insertThemeStylesheet(brandTheme, colorMap, mode);
document.documentElement.setAttribute('theme-color', brandTheme);
},
updateConfig(payload: Partial<TState>) {
for (const key in payload) {
if (payload[key as TStateKey] !== undefined) {
this[key] = payload[key as TStateKey];
this[key as TStateKey] = payload[key as TStateKey];
}
if (key === 'mode') {
this.changeMode(payload[key]);
this.changeMode(payload[key] as ModeType);
}
if (key === 'sideMode') {
this.changeSideMode(payload[key]);
this.changeSideMode(payload[key] as ModeType);
}
if (key === 'brandTheme') {
this.changeBrandTheme(payload[key]);
Expand Down
10 changes: 0 additions & 10 deletions src/types/interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { TabValue } from 'tdesign-vue-next';
import { LocationQueryRaw, RouteRecordName } from 'vue-router';

import STYLE_CONFIG from '@/config/style';

export interface RouteMeta {
title?: string | Record<string, string>;
icon?: string;
Expand Down Expand Up @@ -32,14 +30,6 @@ export interface MenuRoute {

export type ModeType = 'dark' | 'light';

export type SettingType = typeof STYLE_CONFIG;

export type ClassName = { [className: string]: any } | ClassName[] | string;

export type CommonObjType = {
[key: string]: string | number;
};

export interface UserInfo {
name: string;
roles: string[];
Expand Down
12 changes: 4 additions & 8 deletions src/utils/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import trim from 'lodash/trim';
import { Color } from 'tvision-color';

import { TColorToken } from '@/config/color';
import { ModeType } from '@/types/interface';

/**
* 依据主题类型获取颜色
Expand Down Expand Up @@ -58,12 +59,7 @@ export function changeChartsTheme(chartsList: echarts.EChartsType[]): void {
/**
* 根据当前主题色、模式等情景 计算最后生成的色阶
*/
export function generateColorMap(
theme: string,
colorPalette: Array<string>,
mode: 'light' | 'dark',
brandColorIdx: number,
) {
export function generateColorMap(theme: string, colorPalette: Array<string>, mode: ModeType, brandColorIdx: number) {
const isDarkMode = mode === 'dark';

if (isDarkMode) {
Expand All @@ -76,7 +72,7 @@ export function generateColorMap(
colorPalette[0] = `${colorPalette[brandColorIdx]}20`;
}

const colorMap = {
const colorMap: TColorToken = {
'--td-brand-color': colorPalette[brandColorIdx], // 主题色
'--td-brand-color-1': colorPalette[0], // light
'--td-brand-color-2': colorPalette[1], // focus
Expand All @@ -95,7 +91,7 @@ export function generateColorMap(
/**
* 将生成的样式嵌入头部
*/
export function insertThemeStylesheet(theme: string, colorMap: TColorToken, mode: 'light' | 'dark') {
export function insertThemeStylesheet(theme: string, colorMap: TColorToken, mode: ModeType) {
const isDarkMode = mode === 'dark';
const root = !isDarkMode ? `:root[theme-color='${theme}']` : `:root[theme-color='${theme}'][theme-mode='dark']`;

Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
"paths": {
"@/*": ["src/*"]
},
"noImplicitAny": true
"noImplicitAny": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"alwaysStrict": true,
},
"include": [
"**/*.ts",
Expand Down
Loading