summaryrefslogtreecommitdiff
path: root/packages/client/src/pages/settings/theme.install.vue
blob: 52935c75dc774c2d4f7a116b501656ae1188f93a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<template>
<div class="_formRoot">
	<FormTextarea v-model="installThemeCode" class="_formBlock">
		<template #label>{{ $ts._theme.code }}</template>
	</FormTextarea>

	<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';
import FormTextarea from '@/components/form/textarea.vue';
import FormButton from '@/components/ui/button.vue';
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 {
			[symbols.PAGE_INFO]: {
				title: this.$ts._theme.install,
				icon: 'fas fa-download',
				bg: 'var(--bg)',
			},
			installThemeCode: null,
		}
	},

	mounted() {
		this.$emit('info', this[symbols.PAGE_INFO]);
	},

	methods: {
		parseThemeCode(code) {
			let theme;

			try {
				theme = JSON5.parse(code);
			} catch (e) {
				os.alert({
					type: 'error',
					text: this.$ts._theme.invalid
				});
				return false;
			}
			if (!validateTheme(theme)) {
				os.alert({
					type: 'error',
					text: this.$ts._theme.invalid
				});
				return false;
			}
			if (getThemes().some(t => t.id === theme.id)) {
				os.alert({
					type: 'info',
					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>