2020-11-25 21:31:34 +09:00
|
|
|
<template>
|
2023-01-05 21:04:56 +09:00
|
|
|
<div class="_autoGap">
|
|
|
|
<FormTextarea v-model="installThemeCode">
|
2022-01-28 11:39:49 +09:00
|
|
|
<template #label>{{ i18n.ts._theme.code }}</template>
|
2022-01-02 21:35:23 +09:00
|
|
|
</FormTextarea>
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2023-01-05 21:04:56 +09:00
|
|
|
<div style="display: flex; gap: var(--margin); flex-wrap: wrap;">
|
2022-12-19 19:01:30 +09:00
|
|
|
<FormButton :disabled="installThemeCode == null" inline @click="() => preview(installThemeCode)"><i class="ti ti-eye"></i> {{ i18n.ts.preview }}</FormButton>
|
|
|
|
<FormButton :disabled="installThemeCode == null" primary inline @click="() => install(installThemeCode)"><i class="ti ti-check"></i> {{ i18n.ts.install }}</FormButton>
|
2022-01-02 21:35:23 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-11-25 21:31:34 +09:00
|
|
|
</template>
|
|
|
|
|
2022-01-15 17:58:35 +09:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { } from 'vue';
|
2022-05-01 22:51:07 +09:00
|
|
|
import JSON5 from 'json5';
|
2021-11-12 02:02:25 +09:00
|
|
|
import FormTextarea from '@/components/form/textarea.vue';
|
2022-09-06 18:21:49 +09:00
|
|
|
import FormButton from '@/components/MkButton.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';
|
2022-01-15 17:58:35 +09:00
|
|
|
import { i18n } from '@/i18n';
|
2022-06-20 17:38:49 +09:00
|
|
|
import { definePageMetadata } from '@/scripts/page-metadata';
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-01-15 17:58:35 +09:00
|
|
|
let installThemeCode = $ref(null);
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-01-15 17:58:35 +09:00
|
|
|
function parseThemeCode(code: string) {
|
|
|
|
let theme;
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-01-15 17:58:35 +09:00
|
|
|
try {
|
|
|
|
theme = JSON5.parse(code);
|
2022-05-26 22:53:09 +09:00
|
|
|
} catch (err) {
|
2022-01-15 17:58:35 +09:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: i18n.ts._theme.invalid,
|
2022-01-15 17:58:35 +09:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!validateTheme(theme)) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: i18n.ts._theme.invalid,
|
2022-01-15 17:58:35 +09:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getThemes().some(t => t.id === theme.id)) {
|
|
|
|
os.alert({
|
|
|
|
type: 'info',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: i18n.ts._theme.alreadyInstalled,
|
2022-01-15 17:58:35 +09:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-01-15 17:58:35 +09:00
|
|
|
return theme;
|
|
|
|
}
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-01-15 17:58:35 +09:00
|
|
|
function preview(code: string): void {
|
|
|
|
const theme = parseThemeCode(code);
|
|
|
|
if (theme) applyTheme(theme, false);
|
|
|
|
}
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-01-15 17:58:35 +09:00
|
|
|
async function install(code: string): Promise<void> {
|
|
|
|
const theme = parseThemeCode(code);
|
|
|
|
if (!theme) return;
|
|
|
|
await addTheme(theme);
|
|
|
|
os.alert({
|
|
|
|
type: 'success',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: i18n.t('_theme.installed', { name: theme.name }),
|
2022-01-15 17:58:35 +09:00
|
|
|
});
|
|
|
|
}
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-06-20 17:38:49 +09:00
|
|
|
const headerActions = $computed(() => []);
|
|
|
|
|
|
|
|
const headerTabs = $computed(() => []);
|
|
|
|
|
|
|
|
definePageMetadata({
|
|
|
|
title: i18n.ts._theme.install,
|
2022-12-19 19:01:30 +09:00
|
|
|
icon: 'ti ti-download',
|
2020-11-25 21:31:34 +09:00
|
|
|
});
|
|
|
|
</script>
|