refactor(client): refactor settings/theme to use Composition API (#8595)

This commit is contained in:
Andreas Nedbal 2022-05-03 13:34:48 +02:00 committed by GitHub
parent 794c3ec520
commit f0a87b6476

View file

@ -85,12 +85,11 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { computed, defineComponent, onActivated, onMounted, ref, watch } from 'vue'; import { computed, onActivated, ref, watch } from 'vue';
import JSON5 from 'json5'; import JSON5 from 'json5';
import FormSwitch from '@/components/form/switch.vue'; import FormSwitch from '@/components/form/switch.vue';
import FormSelect from '@/components/form/select.vue'; import FormSelect from '@/components/form/select.vue';
import FormGroup from '@/components/form/group.vue';
import FormSection from '@/components/form/section.vue'; import FormSection from '@/components/form/section.vue';
import FormLink from '@/components/form/link.vue'; import FormLink from '@/components/form/link.vue';
import FormButton from '@/components/ui/button.vue'; import FormButton from '@/components/ui/button.vue';
@ -101,100 +100,78 @@ import { ColdDeviceStorage } from '@/store';
import { i18n } from '@/i18n'; import { i18n } from '@/i18n';
import { defaultStore } from '@/store'; import { defaultStore } from '@/store';
import { instance } from '@/instance'; import { instance } from '@/instance';
import { concat, uniqueBy } from '@/scripts/array'; import { uniqueBy } from '@/scripts/array';
import { fetchThemes, getThemes } from '@/theme-store'; import { fetchThemes, getThemes } from '@/theme-store';
import * as symbols from '@/symbols'; import * as symbols from '@/symbols';
export default defineComponent({ const installedThemes = ref(getThemes());
components: { const instanceThemes = [];
FormSwitch,
FormSelect, if (instance.defaultLightTheme != null) instanceThemes.push(JSON5.parse(instance.defaultLightTheme));
FormGroup, if (instance.defaultDarkTheme != null) instanceThemes.push(JSON5.parse(instance.defaultDarkTheme));
FormSection,
FormLink, const themes = computed(() => uniqueBy(instanceThemes.concat(builtinThemes.concat(installedThemes.value)), theme => theme.id));
FormButton, const darkThemes = computed(() => themes.value.filter(t => t.base === 'dark' || t.kind === 'dark'));
const lightThemes = computed(() => themes.value.filter(t => t.base === 'light' || t.kind === 'light'));
const darkTheme = ColdDeviceStorage.ref('darkTheme');
const darkThemeId = computed({
get() {
return darkTheme.value.id;
}, },
set(id) {
ColdDeviceStorage.set('darkTheme', themes.value.find(x => x.id === id))
}
});
const lightTheme = ColdDeviceStorage.ref('lightTheme');
const lightThemeId = computed({
get() {
return lightTheme.value.id;
},
set(id) {
ColdDeviceStorage.set('lightTheme', themes.value.find(x => x.id === id))
}
});
const darkMode = computed(defaultStore.makeGetterSetter('darkMode'));
const syncDeviceDarkMode = computed(ColdDeviceStorage.makeGetterSetter('syncDeviceDarkMode'));
const wallpaper = ref(localStorage.getItem('wallpaper'));
const themesCount = installedThemes.value.length;
emits: ['info'], watch(syncDeviceDarkMode, () => {
if (syncDeviceDarkMode.value) {
defaultStore.set('darkMode', isDeviceDarkmode());
}
});
setup(props, { emit }) { watch(wallpaper, () => {
const INFO = { if (wallpaper.value == null) {
title: i18n.ts.theme, localStorage.removeItem('wallpaper');
icon: 'fas fa-palette', } else {
bg: 'var(--bg)', localStorage.setItem('wallpaper', wallpaper.value);
}; }
location.reload();
});
const installedThemes = ref(getThemes()); onActivated(() => {
const instanceThemes = []; fetchThemes().then(() => {
if (instance.defaultLightTheme != null) instanceThemes.push(JSON5.parse(instance.defaultLightTheme)); installedThemes.value = getThemes();
if (instance.defaultDarkTheme != null) instanceThemes.push(JSON5.parse(instance.defaultDarkTheme)); });
const themes = computed(() => uniqueBy(instanceThemes.concat(builtinThemes.concat(installedThemes.value)), theme => theme.id)); });
const darkThemes = computed(() => themes.value.filter(t => t.base === 'dark' || t.kind === 'dark'));
const lightThemes = computed(() => themes.value.filter(t => t.base === 'light' || t.kind === 'light'));
const darkTheme = ColdDeviceStorage.ref('darkTheme');
const darkThemeId = computed({
get() {
return darkTheme.value.id;
},
set(id) {
ColdDeviceStorage.set('darkTheme', themes.value.find(x => x.id === id))
}
});
const lightTheme = ColdDeviceStorage.ref('lightTheme');
const lightThemeId = computed({
get() {
return lightTheme.value.id;
},
set(id) {
ColdDeviceStorage.set('lightTheme', themes.value.find(x => x.id === id))
}
});
const darkMode = computed(defaultStore.makeGetterSetter('darkMode'));
const syncDeviceDarkMode = computed(ColdDeviceStorage.makeGetterSetter('syncDeviceDarkMode'));
const wallpaper = ref(localStorage.getItem('wallpaper'));
const themesCount = installedThemes.value.length;
watch(syncDeviceDarkMode, () => { fetchThemes().then(() => {
if (syncDeviceDarkMode.value) { installedThemes.value = getThemes();
defaultStore.set('darkMode', isDeviceDarkmode()); });
}
});
watch(wallpaper, () => { function setWallpaper(event) {
if (wallpaper.value == null) { selectFile(event.currentTarget ?? event.target, null).then(file => {
localStorage.removeItem('wallpaper'); wallpaper.value = file.url;
} else { });
localStorage.setItem('wallpaper', wallpaper.value); }
}
location.reload();
});
onActivated(() => { defineExpose({
fetchThemes().then(() => { [symbols.PAGE_INFO]: {
installedThemes.value = getThemes(); title: i18n.ts.theme,
}); icon: 'fas fa-palette',
}); bg: 'var(--bg)',
fetchThemes().then(() => {
installedThemes.value = getThemes();
});
return {
[symbols.PAGE_INFO]: INFO,
darkThemes,
lightThemes,
darkThemeId,
lightThemeId,
darkMode,
syncDeviceDarkMode,
themesCount,
wallpaper,
setWallpaper(e) {
selectFile(e.currentTarget ?? e.target, null).then(file => {
wallpaper.value = file.url;
});
},
};
} }
}); });
</script> </script>