1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-24 03:28:27 +09:00
MisskeyIO/packages/client/src/pages/settings/theme.install.vue

91 lines
2.1 KiB
Vue
Raw Normal View History

<template>
2022-01-02 21:35:23 +09:00
<div class="_formRoot">
<FormTextarea v-model="installThemeCode" class="_formBlock">
<template #label>{{ $ts._theme.code }}</template>
</FormTextarea>
2022-01-02 21:35:23 +09:00
<div class="_formBlock" style="display: flex; gap: var(--margin); flex-wrap: wrap;">
<FormButton :disabled="installThemeCode == null" inline @click="() => preview(installThemeCode)"><i class="fas fa-eye"></i> {{ $ts.preview }}</FormButton>
<FormButton :disabled="installThemeCode == null" primary inline @click="() => install(installThemeCode)"><i class="fas fa-check"></i> {{ $ts.install }}</FormButton>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import * as JSON5 from 'json5';
2021-11-12 02:02:25 +09:00
import FormTextarea from '@/components/form/textarea.vue';
2022-01-02 21:35:23 +09:00
import FormButton from '@/components/ui/button.vue';
2021-11-12 02:02:25 +09:00
import { applyTheme, validateTheme } from '@/scripts/theme';
import * as os from '@/os';
import { addTheme, getThemes } from '@/theme-store';
import * as symbols from '@/symbols';
export default defineComponent({
components: {
FormTextarea,
FormButton,
},
emits: ['info'],
data() {
return {
2021-04-10 12:54:12 +09:00
[symbols.PAGE_INFO]: {
2020-12-26 10:47:36 +09:00
title: this.$ts._theme.install,
2021-09-30 00:50:45 +09:00
icon: 'fas fa-download',
bg: 'var(--bg)',
},
installThemeCode: null,
}
},
methods: {
parseThemeCode(code) {
let theme;
try {
theme = JSON5.parse(code);
} catch (e) {
os.alert({
type: 'error',
2020-12-26 10:47:36 +09:00
text: this.$ts._theme.invalid
});
return false;
}
if (!validateTheme(theme)) {
os.alert({
type: 'error',
2020-12-26 10:47:36 +09:00
text: this.$ts._theme.invalid
});
return false;
}
if (getThemes().some(t => t.id === theme.id)) {
os.alert({
type: 'info',
2020-12-26 10:47:36 +09: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.alert({
type: 'success',
text: this.$t('_theme.installed', { name: theme.name })
});
},
}
});
</script>