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

perf: Added theme preview functionality #1477

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
40 changes: 39 additions & 1 deletion ui/src/components/ThemeConfig/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<n-grid :cols="24">
<n-grid-item :span="20">
<n-select
class="pr-[20px]"
v-model:value="theme"
:placeholder="t('SelectTheme')"
:options="themes"
@update:value="setTheme"
class="pr-[20px]"
@keydown="handlePreviewTheme"
/>
</n-grid-item>
<n-grid-item :span="4">
Expand Down Expand Up @@ -160,11 +161,47 @@ const colors = computed(() => {
}
});

/**
* 设置主题
*
* @param value
*/
const setTheme = (value: string) => {
theme.value = value;
props.preview(theme.value);
mittBus.emit('set-theme', { themeName: value });
};

/**
* 处理当使用键盘上下键时的主题预览功能
*
* @param event
*/
const handlePreviewTheme = (event: KeyboardEvent) => {
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
const currentIndex = themes.value.findIndex(option => option.value === theme.value);

let nextIndex = currentIndex;

if (event.key === 'ArrowUp') {
// 如果当前索引为 0,则跳转到最后一个选项,否则向上移动
nextIndex = currentIndex === 0 ? themes.value.length - 1 : currentIndex - 1;
} else if (event.key === 'ArrowDown') {
// 如果当前索引为最后一个,则跳转到第一个选项,否则向下移动
nextIndex = currentIndex === themes.value.length - 1 ? 0 : currentIndex + 1;
}

const nextValue = themes.value[nextIndex]?.value;

if (nextValue) {
setTheme(nextValue);
}
}
};

/**
* 点击同步按钮的回调
*/
const syncTheme = () => {
loading.value = true;

Expand All @@ -191,6 +228,7 @@ onMounted(() => {
showThemeConfig.value = !showThemeConfig.value;
});
});

onUnmounted(() => {
mittBus.off('show-theme-config');
});
Expand Down
Loading