Skip to content

Commit

Permalink
perf(i18n): improve circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Jun 6, 2021
1 parent d81481c commit d677729
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 57 deletions.
20 changes: 2 additions & 18 deletions src/components/Application/src/AppDarkModeToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@
export default defineComponent({
name: 'DarkModeToggle',
components: { SvgIcon },
// props: {
// size: {
// type: String,
// default: 'default',
// validate: (val) => ['default', 'large'].includes(val),
// },
// },
setup() {
const { prefixCls } = useDesign('dark-mode-toggle');
const { getDarkMode, setDarkMode, getShowDarkModeToggle } = useRootSetting();
const isDark = computed(() => getDarkMode.value === ThemeEnum.DARK);
function toggleDarkMode() {
const darkMode = getDarkMode.value === ThemeEnum.DARK ? ThemeEnum.LIGHT : ThemeEnum.DARK;
setDarkMode(darkMode);
Expand Down Expand Up @@ -95,16 +90,5 @@
transform: translateX(calc(100% + 2px));
}
}
// &--large {
// width: 70px;
// height: 34px;
// padding: 0 10px;
// .@{prefix-cls}-inner {
// width: 26px;
// height: 26px;
// }
// }
}
</style>
9 changes: 0 additions & 9 deletions src/components/Application/src/search/AppSearch.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<script lang="tsx">
import { defineComponent, ref, unref } from 'vue';
import { Tooltip } from 'ant-design-vue';
import { SearchOutlined } from '@ant-design/icons-vue';
import AppSearchModal from './AppSearchModal.vue';
import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
import { useI18n } from '/@/hooks/web/useI18n';
export default defineComponent({
name: 'AppSearch',
components: { AppSearchModal, Tooltip },
setup() {
const showModal = ref(false);
const { getShowSearch } = useHeaderSetting();
const { t } = useI18n();
function changeModal(show: boolean) {
showModal.value = show;
}
return () => {
if (!unref(getShowSearch)) {
return null;
}
return (
<div class="p-1" onClick={changeModal.bind(null, true)}>
<Tooltip>
Expand Down
4 changes: 0 additions & 4 deletions src/components/Application/src/search/AppSearchFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
<div :class="`${prefixCls}`">
<AppSearchKeyItem :class="`${prefixCls}__item`" icon="ant-design:enter-outlined" />
<span>{{ t('component.app.toSearch') }}</span>

<AppSearchKeyItem :class="`${prefixCls}__item`" icon="ion:arrow-up-outline" />
<AppSearchKeyItem :class="`${prefixCls}__item`" icon="ion:arrow-down-outline" />
<span>{{ t('component.app.toNavigate') }}</span>
<AppSearchKeyItem :class="`${prefixCls}__item`" icon="mdi:keyboard-esc" />

<span>{{ t('common.closeText') }}</span>
</div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import AppSearchKeyItem from './AppSearchKeyItem.vue';
import { useDesign } from '/@/hooks/web/useDesign';
import { useI18n } from '/@/hooks/web/useI18n';
export default defineComponent({
Expand Down
18 changes: 8 additions & 10 deletions src/components/Application/src/search/AppSearchModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
visible: propTypes.bool,
},
emits: ['close'],
setup(_, { emit }) {
setup(props, { emit }) {
const scrollWrap = ref<ElRef>(null);
const { prefixCls } = useDesign('app-search-modal');
const { t } = useI18n();
Expand All @@ -96,9 +96,7 @@
const { handleSearch, searchResult, keyword, activeIndex, handleEnter, handleMouseenter } =
useMenuSearch(refs, scrollWrap, emit);
const getIsNotData = computed(() => {
return !keyword || unref(searchResult).length === 0;
});
const getIsNotData = computed(() => !keyword || unref(searchResult).length === 0);
const getClass = computed(() => {
return [
Expand All @@ -109,13 +107,8 @@
];
});
function handleClose() {
searchResult.value = [];
emit('close');
}
watch(
() => _.visible,
() => props.visible,
(v: boolean) => {
v &&
nextTick(() => {
Expand All @@ -124,6 +117,11 @@
}
);
function handleClose() {
searchResult.value = [];
emit('close');
}
return {
t,
prefixCls,
Expand Down
4 changes: 3 additions & 1 deletion src/layouts/default/header/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<!-- action -->
<div :class="`${prefixCls}-action`">
<AppSearch :class="`${prefixCls}-action__item `" />
<AppSearch :class="`${prefixCls}-action__item `" v-if="getShowSearch" />

<ErrorAction v-if="getUseErrorHandle" :class="`${prefixCls}-action__item error-action`" />

Expand Down Expand Up @@ -123,6 +123,7 @@
getShowBread,
getShowHeaderLogo,
getShowHeader,
getShowSearch,
} = useHeaderSetting();
const { getShowLocalePicker } = useLocale();
Expand Down Expand Up @@ -190,6 +191,7 @@
getIsMixSidebar,
getShowSettingButton,
getShowSetting,
getShowSearch,
};
},
});
Expand Down
12 changes: 12 additions & 0 deletions src/locales/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { LocaleType } from '/#/config';

import { set } from 'lodash-es';

export const loadLocalePool: LocaleType[] = [];

export function setHtmlPageLang(locale: LocaleType) {
document.querySelector('html')?.setAttribute('lang', locale);
}

export function setLoadLocalePool(cb: (loadLocalePool: LocaleType[]) => void) {
cb(loadLocalePool);
}

export function genMessage(langs: Record<string, Record<string, any>>, prefix = 'lang') {
const obj: Recordable = {};

Expand Down
7 changes: 3 additions & 4 deletions src/locales/setupI18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { App } from 'vue';
import type { I18n, I18nOptions } from 'vue-i18n';

import { createI18n } from 'vue-i18n';

import { setLoadLocalePool, setHtmlPageLang } from './useLocale';
import { setHtmlPageLang, setLoadLocalePool } from './helper';
import { localeSetting } from '/@/settings/localeSetting';
import { useLocaleStoreWithOut } from '/@/store/modules/locale';

Expand All @@ -16,8 +15,8 @@ async function createI18nOptions(): Promise<I18nOptions> {
const locale = localeStore.getLocale;
const defaultLocal = await import(`./lang/${locale}.ts`);
const message = defaultLocal.default?.message ?? {};
setHtmlPageLang(locale)

setHtmlPageLang(locale);
setLoadLocalePool((loadLocalePool) => {
loadLocalePool.push(locale);
});
Expand Down
13 changes: 2 additions & 11 deletions src/locales/useLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@ import moment from 'moment';
import { i18n } from './setupI18n';
import { useLocaleStoreWithOut } from '/@/store/modules/locale';
import { unref, computed } from 'vue';
import { loadLocalePool, setHtmlPageLang } from './helper';

interface LangModule {
message: Recordable;
momentLocale: Recordable;
momentLocaleName: string;
}

const loadLocalePool: LocaleType[] = [];

export function setHtmlPageLang(locale: LocaleType) {
document.querySelector('html')?.setAttribute('lang', locale);
}

export function setLoadLocalePool(cb: (loadLocalePool: LocaleType[]) => void) {
cb(loadLocalePool);
}

function setI18nLanguage(locale: LocaleType) {
const localeStore = useLocaleStoreWithOut();

Expand All @@ -34,7 +25,7 @@ function setI18nLanguage(locale: LocaleType) {
(i18n.global.locale as any).value = locale;
}
localeStore.setLocaleInfo({ locale });
setHtmlPageLang(locale)
setHtmlPageLang(locale);
}

export function useLocale() {
Expand Down

0 comments on commit d677729

Please sign in to comment.