2021-02-06 12:05:00 +00:00
|
|
|
<template>
|
2022-01-04 08:52:44 +00:00
|
|
|
<div class="_formRoot">
|
|
|
|
<FormInfo warn class="_formBlock">{{ $ts._plugin.installWarn }}</FormInfo>
|
2021-02-06 12:05:00 +00:00
|
|
|
|
2022-01-04 08:52:44 +00:00
|
|
|
<FormTextarea v-model="code" tall class="_formBlock">
|
|
|
|
<template #label>{{ $ts.code }}</template>
|
|
|
|
</FormTextarea>
|
2021-02-06 12:05:00 +00:00
|
|
|
|
2022-01-04 08:52:44 +00:00
|
|
|
<div class="_formBlock">
|
|
|
|
<FormButton :disabled="code == null" primary inline @click="install"><i class="fas fa-check"></i> {{ $ts.install }}</FormButton>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-06 12:05:00 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-05-01 13:51:07 +00:00
|
|
|
import { defineAsyncComponent, defineComponent } from 'vue';
|
2021-02-06 12:05:00 +00:00
|
|
|
import { AiScript, parse } from '@syuilo/aiscript';
|
|
|
|
import { serialize } from '@syuilo/aiscript/built/serializer';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
2021-11-11 17:02:25 +00:00
|
|
|
import FormTextarea from '@/components/form/textarea.vue';
|
2022-01-04 08:52:44 +00:00
|
|
|
import FormButton from '@/components/ui/button.vue';
|
|
|
|
import FormInfo from '@/components/ui/info.vue';
|
2021-11-11 17:02:25 +00:00
|
|
|
import * as os from '@/os';
|
|
|
|
import { ColdDeviceStorage } from '@/store';
|
|
|
|
import { unisonReload } from '@/scripts/unison-reload';
|
|
|
|
import * as symbols from '@/symbols';
|
2021-02-06 12:05:00 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
components: {
|
|
|
|
FormTextarea,
|
|
|
|
FormButton,
|
2021-04-13 16:41:49 +00:00
|
|
|
FormInfo,
|
2021-02-06 12:05:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
emits: ['info'],
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
2021-04-10 03:54:12 +00:00
|
|
|
[symbols.PAGE_INFO]: {
|
2021-02-06 12:05:00 +00:00
|
|
|
title: this.$ts._plugin.install,
|
2021-09-29 15:50:45 +00:00
|
|
|
icon: 'fas fa-download',
|
|
|
|
bg: 'var(--bg)',
|
2021-02-06 12:05:00 +00:00
|
|
|
},
|
|
|
|
code: null,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
installPlugin({ id, meta, ast, token }) {
|
|
|
|
ColdDeviceStorage.set('plugins', ColdDeviceStorage.get('plugins').concat({
|
|
|
|
...meta,
|
|
|
|
id,
|
|
|
|
active: true,
|
|
|
|
configData: {},
|
|
|
|
token: token,
|
|
|
|
ast: ast
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
async install() {
|
|
|
|
let ast;
|
|
|
|
try {
|
|
|
|
ast = parse(this.code);
|
|
|
|
} catch (e) {
|
2021-11-18 09:45:58 +00:00
|
|
|
os.alert({
|
2021-02-06 12:05:00 +00:00
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const meta = AiScript.collectMetadata(ast);
|
|
|
|
if (meta == null) {
|
2021-11-18 09:45:58 +00:00
|
|
|
os.alert({
|
2021-02-06 12:05:00 +00:00
|
|
|
type: 'error',
|
|
|
|
text: 'No metadata found :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = meta.get(null);
|
|
|
|
if (data == null) {
|
2021-11-18 09:45:58 +00:00
|
|
|
os.alert({
|
2021-02-06 12:05:00 +00:00
|
|
|
type: 'error',
|
|
|
|
text: 'No metadata found :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { name, version, author, description, permissions, config } = data;
|
|
|
|
if (name == null || version == null || author == null) {
|
2021-11-18 09:45:58 +00:00
|
|
|
os.alert({
|
2021-02-06 12:05:00 +00:00
|
|
|
type: 'error',
|
|
|
|
text: 'Required property not found :('
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = permissions == null || permissions.length === 0 ? null : await new Promise((res, rej) => {
|
2022-05-01 13:51:07 +00:00
|
|
|
os.popup(defineAsyncComponent(() => import('@/components/token-generate-window.vue')), {
|
2021-02-06 12:05:00 +00:00
|
|
|
title: this.$ts.tokenRequested,
|
|
|
|
information: this.$ts.pluginTokenRequestedDescription,
|
|
|
|
initialName: name,
|
|
|
|
initialPermissions: permissions
|
|
|
|
}, {
|
|
|
|
done: async result => {
|
|
|
|
const { name, permissions } = result;
|
|
|
|
const { token } = await os.api('miauth/gen-token', {
|
|
|
|
session: null,
|
|
|
|
name: name,
|
|
|
|
permission: permissions,
|
|
|
|
});
|
|
|
|
|
|
|
|
res(token);
|
|
|
|
}
|
|
|
|
}, 'closed');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.installPlugin({
|
|
|
|
id: uuid(),
|
|
|
|
meta: {
|
|
|
|
name, version, author, description, permissions, config
|
|
|
|
},
|
|
|
|
token,
|
|
|
|
ast: serialize(ast)
|
|
|
|
});
|
|
|
|
|
|
|
|
os.success();
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
2021-02-17 12:36:56 +00:00
|
|
|
unisonReload();
|
2021-02-06 12:05:00 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|