2020-07-10 20:12:35 -07:00
|
|
|
<template>
|
|
|
|
<div class="t9makv94">
|
2020-10-17 04:12:00 -07:00
|
|
|
<section class="_section">
|
2020-07-10 20:12:35 -07:00
|
|
|
<div class="_content">
|
2020-10-17 04:12:00 -07:00
|
|
|
<details>
|
|
|
|
<summary>{{ $t('import') }}</summary>
|
|
|
|
<MkTextarea v-model:value="themeToImport">
|
|
|
|
{{ $t('_theme.importInfo') }}
|
|
|
|
</MkTextarea>
|
|
|
|
<MkButton :disabled="!themeToImport.trim()" @click="importTheme">{{ $t('import') }}</MkButton>
|
|
|
|
</details>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<section class="_section">
|
|
|
|
<div class="_content _card _vMargin">
|
|
|
|
<div class="_content">
|
|
|
|
<MkInput v-model:value="name" required><span>{{ $t('name') }}</span></MkInput>
|
|
|
|
<MkInput v-model:value="author" required><span>{{ $t('author') }}</span></MkInput>
|
|
|
|
<MkTextarea v-model:value="description"><span>{{ $t('description') }}</span></MkTextarea>
|
|
|
|
<div class="_inputs">
|
|
|
|
<div v-text="$t('_theme.base')" />
|
|
|
|
<MkRadio v-model="baseTheme" value="light">{{ $t('light') }}</MkRadio>
|
|
|
|
<MkRadio v-model="baseTheme" value="dark">{{ $t('dark') }}</MkRadio>
|
|
|
|
</div>
|
2020-07-10 20:12:35 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-10-17 04:12:00 -07:00
|
|
|
<div class="_content _card _vMargin">
|
|
|
|
<div class="list-view _content">
|
2020-07-10 20:12:35 -07:00
|
|
|
<div class="item" v-for="([ k, v ], i) in theme" :key="k">
|
|
|
|
<div class="_inputs">
|
|
|
|
<div>
|
|
|
|
{{ k.startsWith('$') ? `${k} (${$t('_theme.constant')})` : $t('_theme.keys.' + k) }}
|
|
|
|
<button v-if="k.startsWith('$')" class="_button _link" @click="del(i)" v-text="$t('delete')" />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div class="type" @click="chooseType($event, i)">
|
2020-10-17 04:12:00 -07:00
|
|
|
{{ getTypeOf(v) }} <Fa :icon="faChevronDown"/>
|
2020-07-10 20:12:35 -07:00
|
|
|
</div>
|
|
|
|
<!-- default -->
|
|
|
|
<div v-if="v === null" v-text="baseProps[k]" class="default-value" />
|
|
|
|
<!-- color -->
|
|
|
|
<div v-else-if="typeof v === 'string'" class="color">
|
|
|
|
<input type="color" :value="v" @input="colorChanged($event.target.value, i)"/>
|
2020-10-17 04:12:00 -07:00
|
|
|
<MkInput class="select" :value="v" @update:value="colorChanged($event, i)"/>
|
2020-07-10 20:12:35 -07:00
|
|
|
</div>
|
|
|
|
<!-- ref const -->
|
2020-10-17 04:12:00 -07:00
|
|
|
<MkInput v-else-if="v.type === 'refConst'" v-model:value="v.key">
|
2020-07-10 20:12:35 -07:00
|
|
|
<template #prefix>$</template>
|
2020-07-24 09:56:52 -07:00
|
|
|
<span>{{ $t('name') }}</span>
|
2020-10-17 04:12:00 -07:00
|
|
|
</MkInput>
|
2020-07-10 20:12:35 -07:00
|
|
|
<!-- ref props -->
|
2020-10-17 04:12:00 -07:00
|
|
|
<MkSelect class="select" v-else-if="v.type === 'refProp'" v-model:value="v.key">
|
2020-07-10 20:12:35 -07:00
|
|
|
<option v-for="key in themeProps" :value="key" :key="key">{{ $t('_theme.keys.' + key) }}</option>
|
2020-10-17 04:12:00 -07:00
|
|
|
</MkSelect>
|
2020-07-10 20:12:35 -07:00
|
|
|
<!-- func -->
|
|
|
|
<template v-else-if="v.type === 'func'">
|
2020-10-17 04:12:00 -07:00
|
|
|
<MkSelect class="select" v-model:value="v.name">
|
2020-07-10 20:12:35 -07:00
|
|
|
<template #label>{{ $t('_theme.funcKind') }}</template>
|
|
|
|
<option v-for="n in ['alpha', 'darken', 'lighten']" :value="n" :key="n">{{ $t('_theme.' + n) }}</option>
|
2020-10-17 04:12:00 -07:00
|
|
|
</MkSelect>
|
|
|
|
<MkInput type="number" v-model:value="v.arg"><span>{{ $t('_theme.argument') }}</span></MkInput>
|
|
|
|
<MkSelect class="select" v-model:value="v.value">
|
2020-07-10 20:12:35 -07:00
|
|
|
<template #label>{{ $t('_theme.basedProp') }}</template>
|
|
|
|
<option v-for="key in themeProps" :value="key" :key="key">{{ $t('_theme.keys.' + key) }}</option>
|
2020-10-17 04:12:00 -07:00
|
|
|
</MkSelect>
|
2020-07-10 20:12:35 -07:00
|
|
|
</template>
|
2020-10-17 04:12:00 -07:00
|
|
|
<!-- CSS -->
|
|
|
|
<MkInput v-else-if="v.type === 'css'" v-model:value="v.value">
|
|
|
|
<span>CSS</span>
|
|
|
|
</MkInput>
|
2020-07-10 20:12:35 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-10-17 04:12:00 -07:00
|
|
|
<MkButton primary @click="addConst">{{ $t('_theme.addConstant') }}</MkButton>
|
2020-07-10 20:12:35 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-10-17 04:12:00 -07:00
|
|
|
</section>
|
2020-10-17 08:49:02 -07:00
|
|
|
<section class="_section">
|
|
|
|
<details class="_content">
|
|
|
|
<summary>{{ $t('sample') }}</summary>
|
|
|
|
<MkSample/>
|
|
|
|
</details>
|
|
|
|
</section>
|
2020-10-17 04:12:00 -07:00
|
|
|
<section class="_section">
|
2020-07-10 20:12:35 -07:00
|
|
|
<div class="_content">
|
2020-10-17 04:12:00 -07:00
|
|
|
<MkButton inline @click="preview">{{ $t('preview') }}</MkButton>
|
|
|
|
<MkButton inline primary :disabled="!name || !author" @click="save">{{ $t('save') }}</MkButton>
|
2020-07-10 20:12:35 -07:00
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 04:12:00 -07:00
|
|
|
import { defineComponent } from 'vue';
|
2020-07-10 20:12:35 -07:00
|
|
|
import { faPalette, faChevronDown, faKeyboard } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import * as JSON5 from 'json5';
|
2020-10-17 08:49:02 -07:00
|
|
|
import { toUnicode } from 'punycode';
|
2020-07-10 20:12:35 -07:00
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
import MkRadio from '@/components/ui/radio.vue';
|
|
|
|
import MkButton from '@/components/ui/button.vue';
|
|
|
|
import MkInput from '@/components/ui/input.vue';
|
|
|
|
import MkTextarea from '@/components/ui/textarea.vue';
|
|
|
|
import MkSelect from '@/components/ui/select.vue';
|
2020-10-17 08:49:02 -07:00
|
|
|
import MkSample from '@/components/sample.vue';
|
2020-07-10 20:12:35 -07:00
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
import { convertToMisskeyTheme, ThemeValue, convertToViewModel, ThemeViewModel } from '@/scripts/theme-editor';
|
|
|
|
import { Theme, applyTheme, lightTheme, darkTheme, themeProps, validateTheme } from '@/scripts/theme';
|
|
|
|
import { host } from '@/config';
|
|
|
|
import * as os from '@/os';
|
2020-07-10 20:12:35 -07:00
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
export default defineComponent({
|
2020-07-10 20:12:35 -07:00
|
|
|
components: {
|
|
|
|
MkRadio,
|
|
|
|
MkButton,
|
|
|
|
MkInput,
|
|
|
|
MkTextarea,
|
2020-10-17 08:49:02 -07:00
|
|
|
MkSelect,
|
|
|
|
MkSample,
|
2020-07-10 20:12:35 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
2020-10-17 04:12:00 -07:00
|
|
|
INFO: {
|
|
|
|
header: [{
|
|
|
|
title: this.$t('themeEditor'),
|
|
|
|
icon: faPalette,
|
|
|
|
}],
|
|
|
|
},
|
2020-07-10 20:12:35 -07:00
|
|
|
theme: [] as ThemeViewModel,
|
|
|
|
name: '',
|
|
|
|
description: '',
|
|
|
|
baseTheme: 'light' as 'dark' | 'light',
|
|
|
|
author: `@${this.$store.state.i.username}@${toUnicode(host)}`,
|
|
|
|
themeToImport: '',
|
|
|
|
changed: false,
|
|
|
|
lightTheme, darkTheme, themeProps,
|
2020-10-17 04:12:00 -07:00
|
|
|
faPalette, faChevronDown, faKeyboard,
|
2020-07-10 20:12:35 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
baseProps() {
|
|
|
|
return this.baseTheme === 'light' ? this.lightTheme.props : this.darkTheme.props;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
beforeUnmount() {
|
2020-07-10 20:12:35 -07:00
|
|
|
window.removeEventListener('beforeunload', this.beforeunload);
|
|
|
|
},
|
|
|
|
|
|
|
|
async beforeRouteLeave(to, from, next) {
|
|
|
|
if (this.changed && !(await this.confirm())) {
|
|
|
|
next(false);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
|
|
|
this.init();
|
|
|
|
window.addEventListener('beforeunload', this.beforeunload);
|
|
|
|
const changed = () => this.changed = true;
|
|
|
|
this.$watch('name', changed);
|
|
|
|
this.$watch('description', changed);
|
|
|
|
this.$watch('baseTheme', changed);
|
|
|
|
this.$watch('author', changed);
|
|
|
|
this.$watch('theme', changed);
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
beforeunload(e: BeforeUnloadEvent) {
|
|
|
|
if (this.changed) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.returnValue = '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async confirm(): Promise<boolean> {
|
2020-10-17 04:12:00 -07:00
|
|
|
const { canceled } = await os.dialog({
|
2020-07-10 20:12:35 -07:00
|
|
|
type: 'warning',
|
|
|
|
text: this.$t('leaveConfirm'),
|
|
|
|
showCancelButton: true
|
|
|
|
});
|
|
|
|
return !canceled;
|
|
|
|
},
|
|
|
|
|
|
|
|
init() {
|
|
|
|
const t: ThemeViewModel = [];
|
|
|
|
for (const key of themeProps) {
|
|
|
|
t.push([ key, null ]);
|
|
|
|
}
|
|
|
|
this.theme = t;
|
|
|
|
},
|
|
|
|
|
|
|
|
async del(i: number) {
|
2020-10-17 04:12:00 -07:00
|
|
|
const { canceled } = await os.dialog({
|
2020-07-10 20:12:35 -07:00
|
|
|
type: 'warning',
|
|
|
|
showCancelButton: true,
|
|
|
|
text: this.$t('_theme.deleteConstantConfirm', { const: this.theme[i][0] }),
|
|
|
|
});
|
|
|
|
if (canceled) return;
|
|
|
|
Vue.delete(this.theme, i);
|
|
|
|
},
|
|
|
|
|
|
|
|
async addConst() {
|
2020-10-17 04:12:00 -07:00
|
|
|
const { canceled, result } = await os.dialog({
|
2020-07-10 20:12:35 -07:00
|
|
|
title: this.$t('_theme.inputConstantName'),
|
|
|
|
input: true
|
|
|
|
});
|
|
|
|
if (canceled) return;
|
|
|
|
this.theme.push([ '$' + result, '#000000']);
|
|
|
|
},
|
|
|
|
|
|
|
|
save() {
|
|
|
|
const theme = convertToMisskeyTheme(this.theme, this.name, this.description, this.author, this.baseTheme);
|
|
|
|
const themes = this.$store.state.device.themes.concat(theme);
|
|
|
|
this.$store.commit('device/set', {
|
|
|
|
key: 'themes', value: themes
|
|
|
|
});
|
2020-10-17 04:12:00 -07:00
|
|
|
os.dialog({
|
2020-07-10 20:12:35 -07:00
|
|
|
type: 'success',
|
|
|
|
text: this.$t('_theme.installed', { name: theme.name })
|
|
|
|
});
|
|
|
|
this.changed = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
preview() {
|
|
|
|
const theme = convertToMisskeyTheme(this.theme, this.name, this.description, this.author, this.baseTheme);
|
|
|
|
try {
|
|
|
|
applyTheme(theme, false);
|
|
|
|
} catch (e) {
|
2020-10-17 04:12:00 -07:00
|
|
|
os.dialog({
|
2020-07-10 20:12:35 -07:00
|
|
|
type: 'error',
|
|
|
|
text: e.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async importTheme() {
|
|
|
|
if (this.changed && (!await this.confirm())) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const theme = JSON5.parse(this.themeToImport) as Theme;
|
|
|
|
if (!validateTheme(theme)) throw new Error(this.$t('_theme.invalid'));
|
|
|
|
|
|
|
|
this.name = theme.name;
|
|
|
|
this.description = theme.desc || '';
|
|
|
|
this.author = theme.author;
|
|
|
|
this.baseTheme = theme.base || 'light';
|
|
|
|
this.theme = convertToViewModel(theme);
|
|
|
|
this.themeToImport = '';
|
|
|
|
} catch (e) {
|
2020-10-17 04:12:00 -07:00
|
|
|
os.dialog({
|
2020-07-10 20:12:35 -07:00
|
|
|
type: 'error',
|
|
|
|
text: e.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
colorChanged(color: string, i: number) {
|
2020-10-17 04:12:00 -07:00
|
|
|
this.theme[i] = [this.theme[i][0], color];
|
2020-07-10 20:12:35 -07:00
|
|
|
},
|
2020-10-17 04:12:00 -07:00
|
|
|
|
2020-07-10 20:12:35 -07:00
|
|
|
getTypeOf(v: ThemeValue) {
|
|
|
|
return v === null
|
|
|
|
? this.$t('_theme.defaultValue')
|
|
|
|
: typeof v === 'string'
|
|
|
|
? this.$t('_theme.color')
|
|
|
|
: this.$t('_theme.' + v.type);
|
|
|
|
},
|
|
|
|
|
|
|
|
async chooseType(e: MouseEvent, i: number) {
|
|
|
|
const newValue = await this.showTypeMenu(e);
|
2020-10-17 04:12:00 -07:00
|
|
|
this.theme[i] = [ this.theme[i][0], newValue ];
|
2020-07-10 20:12:35 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
showTypeMenu(e: MouseEvent) {
|
|
|
|
return new Promise<ThemeValue>((resolve) => {
|
2020-10-17 04:12:00 -07:00
|
|
|
os.modalMenu([{
|
|
|
|
text: this.$t('_theme.defaultValue'),
|
|
|
|
action: () => resolve(null),
|
|
|
|
}, {
|
|
|
|
text: this.$t('_theme.color'),
|
|
|
|
action: () => resolve('#000000'),
|
|
|
|
}, {
|
|
|
|
text: this.$t('_theme.func'),
|
|
|
|
action: () => resolve({
|
|
|
|
type: 'func', name: 'alpha', arg: 1, value: 'accent'
|
|
|
|
}),
|
|
|
|
}, {
|
|
|
|
text: this.$t('_theme.refProp'),
|
|
|
|
action: () => resolve({
|
|
|
|
type: 'refProp', key: 'accent',
|
|
|
|
}),
|
|
|
|
}, {
|
|
|
|
text: this.$t('_theme.refConst'),
|
|
|
|
action: () => resolve({
|
|
|
|
type: 'refConst', key: '',
|
|
|
|
}),
|
|
|
|
}, {
|
|
|
|
text: 'CSS',
|
|
|
|
action: () => resolve({
|
|
|
|
type: 'css', value: '',
|
|
|
|
}),
|
|
|
|
}], e.currentTarget || e.target);
|
2020-07-10 20:12:35 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.t9makv94 {
|
2020-10-17 04:12:00 -07:00
|
|
|
> ._section {
|
2020-07-10 20:12:35 -07:00
|
|
|
> ._content {
|
|
|
|
> .list-view {
|
|
|
|
> .item {
|
|
|
|
min-height: 48px;
|
|
|
|
word-break: break-all;
|
|
|
|
|
|
|
|
&:not(:last-child) {
|
2020-10-17 04:12:00 -07:00
|
|
|
margin-bottom: 8px;
|
2020-07-10 20:12:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
.select {
|
|
|
|
margin: 24px 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.type {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
.default-value {
|
|
|
|
opacity: 0.6;
|
|
|
|
pointer-events: none;
|
|
|
|
user-select: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.color {
|
|
|
|
> input {
|
|
|
|
display: inline-block;
|
|
|
|
width: 1.5em;
|
|
|
|
height: 1.5em;
|
|
|
|
}
|
|
|
|
|
|
|
|
> div {
|
|
|
|
margin-left: 8px;
|
|
|
|
display: inline-block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|