2020-11-25 12:31:34 +00:00
|
|
|
<template>
|
2022-01-02 12:35:23 +00:00
|
|
|
<div class="_formRoot">
|
|
|
|
<FormTextarea v-model="installThemeCode" class="_formBlock">
|
2022-01-28 02:39:49 +00:00
|
|
|
<template #label>{{ i18n.ts._theme.code }}</template>
|
2022-01-02 12:35:23 +00:00
|
|
|
</FormTextarea>
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-02 12:35:23 +00:00
|
|
|
<div class="_formBlock" style="display: flex; gap: var(--margin); flex-wrap: wrap;">
|
2022-01-28 02:39:49 +00:00
|
|
|
<FormButton :disabled="installThemeCode == null" inline @click="() => preview(installThemeCode)"><i class="fas fa-eye"></i> {{ i18n.ts.preview }}</FormButton>
|
|
|
|
<FormButton :disabled="installThemeCode == null" primary inline @click="() => install(installThemeCode)"><i class="fas fa-check"></i> {{ i18n.ts.install }}</FormButton>
|
2022-01-02 12:35:23 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-11-25 12:31:34 +00:00
|
|
|
</template>
|
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { } from 'vue';
|
2020-11-25 12:31:34 +00:00
|
|
|
import * as JSON5 from 'json5';
|
2021-11-11 17:02:25 +00:00
|
|
|
import FormTextarea from '@/components/form/textarea.vue';
|
2022-01-02 12:35:23 +00:00
|
|
|
import FormButton from '@/components/ui/button.vue';
|
2021-11-11 17:02:25 +00:00
|
|
|
import { applyTheme, validateTheme } from '@/scripts/theme';
|
|
|
|
import * as os from '@/os';
|
|
|
|
import { addTheme, getThemes } from '@/theme-store';
|
|
|
|
import * as symbols from '@/symbols';
|
2022-01-15 08:58:35 +00:00
|
|
|
import { i18n } from '@/i18n';
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
let installThemeCode = $ref(null);
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
function parseThemeCode(code: string) {
|
|
|
|
let theme;
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
try {
|
|
|
|
theme = JSON5.parse(code);
|
|
|
|
} catch (e) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-01-28 02:39:49 +00:00
|
|
|
text: i18n.ts._theme.invalid
|
2022-01-15 08:58:35 +00:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!validateTheme(theme)) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-01-28 02:39:49 +00:00
|
|
|
text: i18n.ts._theme.invalid
|
2022-01-15 08:58:35 +00:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getThemes().some(t => t.id === theme.id)) {
|
|
|
|
os.alert({
|
|
|
|
type: 'info',
|
2022-01-28 02:39:49 +00:00
|
|
|
text: i18n.ts._theme.alreadyInstalled
|
2022-01-15 08:58:35 +00:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
return theme;
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
function preview(code: string): void {
|
|
|
|
const theme = parseThemeCode(code);
|
|
|
|
if (theme) applyTheme(theme, false);
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
async function install(code: string): Promise<void> {
|
|
|
|
const theme = parseThemeCode(code);
|
|
|
|
if (!theme) return;
|
|
|
|
await addTheme(theme);
|
|
|
|
os.alert({
|
|
|
|
type: 'success',
|
|
|
|
text: i18n.t('_theme.installed', { name: theme.name })
|
|
|
|
});
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2022-01-15 08:58:35 +00:00
|
|
|
defineExpose({
|
|
|
|
[symbols.PAGE_INFO]: {
|
2022-01-28 02:39:49 +00:00
|
|
|
title: i18n.ts._theme.install,
|
2022-01-15 08:58:35 +00:00
|
|
|
icon: 'fas fa-download',
|
|
|
|
bg: 'var(--bg)',
|
|
|
|
},
|
2020-11-25 12:31:34 +00:00
|
|
|
});
|
|
|
|
</script>
|