egirlskey/src/client/pages/settings/theme.install.vue

105 lines
2.5 KiB
Vue
Raw Normal View History

<template>
<FormBase>
<FormGroup>
<FormTextarea v-model:value="installThemeCode">
2020-12-26 01:47:36 +00:00
<span>{{ $ts._theme.code }}</span>
</FormTextarea>
<FormButton @click="() => preview(installThemeCode)" :disabled="installThemeCode == null" inline><i class="fas fa-eye"></i> {{ $ts.preview }}</FormButton>
</FormGroup>
<FormButton @click="() => install(installThemeCode)" :disabled="installThemeCode == null" primary inline><i class="fas fa-check"></i> {{ $ts.install }}</FormButton>
</FormBase>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import * as JSON5 from 'json5';
2021-03-23 08:30:14 +00:00
import FormTextarea from '@client/components/form/textarea.vue';
import FormSelect from '@client/components/form/select.vue';
import FormRadios from '@client/components/form/radios.vue';
import FormBase from '@client/components/form/base.vue';
import FormGroup from '@client/components/form/group.vue';
import FormLink from '@client/components/form/link.vue';
import FormButton from '@client/components/form/button.vue';
import { applyTheme, validateTheme } from '@client/scripts/theme';
import * as os from '@client/os';
import { ColdDeviceStorage } from '@client/store';
import { addTheme, getThemes } from '@client/theme-store';
2021-04-10 03:54:12 +00:00
import * as symbols from '@client/symbols';
export default defineComponent({
components: {
FormTextarea,
FormSelect,
FormRadios,
FormBase,
FormGroup,
FormLink,
FormButton,
},
emits: ['info'],
data() {
return {
2021-04-10 03:54:12 +00:00
[symbols.PAGE_INFO]: {
2020-12-26 01:47:36 +00:00
title: this.$ts._theme.install,
icon: 'fas fa-download'
},
installThemeCode: null,
}
},
mounted() {
2021-04-10 03:54:12 +00:00
this.$emit('info', this[symbols.PAGE_INFO]);
},
methods: {
parseThemeCode(code) {
let theme;
try {
theme = JSON5.parse(code);
} catch (e) {
os.dialog({
type: 'error',
2020-12-26 01:47:36 +00:00
text: this.$ts._theme.invalid
});
return false;
}
if (!validateTheme(theme)) {
os.dialog({
type: 'error',
2020-12-26 01:47:36 +00:00
text: this.$ts._theme.invalid
});
return false;
}
if (getThemes().some(t => t.id === theme.id)) {
os.dialog({
type: 'info',
2020-12-26 01:47:36 +00:00
text: this.$ts._theme.alreadyInstalled
});
return false;
}
return theme;
},
preview(code) {
const theme = this.parseThemeCode(code);
if (theme) applyTheme(theme, false);
},
async install(code) {
const theme = this.parseThemeCode(code);
if (!theme) return;
await addTheme(theme);
os.dialog({
type: 'success',
text: this.$t('_theme.installed', { name: theme.name })
});
},
}
});
</script>