2021-02-06 21:05:00 +09:00
|
|
|
<template>
|
2022-01-04 17:52:44 +09:00
|
|
|
<div class="_formRoot">
|
2022-05-04 10:10:52 +09:00
|
|
|
<FormInfo warn class="_formBlock">{{ i18n.ts._plugin.installWarn }}</FormInfo>
|
2021-02-06 21:05:00 +09:00
|
|
|
|
2022-01-04 17:52:44 +09:00
|
|
|
<FormTextarea v-model="code" tall class="_formBlock">
|
2022-05-04 10:10:52 +09:00
|
|
|
<template #label>{{ i18n.ts.code }}</template>
|
2022-01-04 17:52:44 +09:00
|
|
|
</FormTextarea>
|
2021-02-06 21:05:00 +09:00
|
|
|
|
2022-01-04 17:52:44 +09:00
|
|
|
<div class="_formBlock">
|
2022-05-04 10:10:52 +09:00
|
|
|
<FormButton :disabled="code == null" primary inline @click="install"><i class="fas fa-check"></i> {{ i18n.ts.install }}</FormButton>
|
2022-01-04 17:52:44 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-06 21:05:00 +09:00
|
|
|
</template>
|
|
|
|
|
2022-05-04 10:10:52 +09:00
|
|
|
<script lang="ts" setup>
|
2022-06-20 17:38:49 +09:00
|
|
|
import { defineAsyncComponent, nextTick, ref } from 'vue';
|
2021-02-06 21:05:00 +09:00
|
|
|
import { AiScript, parse } from '@syuilo/aiscript';
|
|
|
|
import { serialize } from '@syuilo/aiscript/built/serializer';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
2021-11-12 02:02:25 +09:00
|
|
|
import FormTextarea from '@/components/form/textarea.vue';
|
2022-01-04 17:52:44 +09:00
|
|
|
import FormButton from '@/components/ui/button.vue';
|
|
|
|
import FormInfo from '@/components/ui/info.vue';
|
2021-11-12 02:02:25 +09:00
|
|
|
import * as os from '@/os';
|
|
|
|
import { ColdDeviceStorage } from '@/store';
|
|
|
|
import { unisonReload } from '@/scripts/unison-reload';
|
2022-05-04 10:10:52 +09:00
|
|
|
import { i18n } from '@/i18n';
|
2022-06-20 17:38:49 +09:00
|
|
|
import { definePageMetadata } from '@/scripts/page-metadata';
|
2021-02-06 21:05:00 +09:00
|
|
|
|
2022-05-04 10:10:52 +09:00
|
|
|
const code = ref(null);
|
2021-02-06 21:05:00 +09:00
|
|
|
|
2022-05-04 10:10:52 +09:00
|
|
|
function installPlugin({ id, meta, ast, token }) {
|
|
|
|
ColdDeviceStorage.set('plugins', ColdDeviceStorage.get('plugins').concat({
|
|
|
|
...meta,
|
|
|
|
id,
|
|
|
|
active: true,
|
|
|
|
configData: {},
|
|
|
|
token: token,
|
2022-06-20 17:38:49 +09:00
|
|
|
ast: ast,
|
2022-05-04 10:10:52 +09:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function install() {
|
|
|
|
let ast;
|
|
|
|
try {
|
|
|
|
ast = parse(code.value);
|
2022-05-26 22:53:09 +09:00
|
|
|
} catch (err) {
|
2022-05-04 10:10:52 +09:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: 'Syntax error :(',
|
2022-05-04 10:10:52 +09:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const meta = AiScript.collectMetadata(ast);
|
|
|
|
if (meta == null) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: 'No metadata found :(',
|
2022-05-04 10:10:52 +09:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +09:00
|
|
|
const metadata = meta.get(null);
|
|
|
|
if (metadata == null) {
|
2022-05-04 10:10:52 +09:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: 'No metadata found :(',
|
2022-05-04 10:10:52 +09:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +09:00
|
|
|
const { name, version, author, description, permissions, config } = metadata;
|
2022-05-04 10:10:52 +09:00
|
|
|
if (name == null || version == null || author == null) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 17:38:49 +09:00
|
|
|
text: 'Required property not found :(',
|
2022-05-04 10:10:52 +09:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = permissions == null || permissions.length === 0 ? null : await new Promise((res, rej) => {
|
2022-08-31 00:24:33 +09:00
|
|
|
os.popup(defineAsyncComponent(() => import('@/components/MkTokenGenerateWindow.vue')), {
|
2022-05-04 10:10:52 +09:00
|
|
|
title: i18n.ts.tokenRequested,
|
|
|
|
information: i18n.ts.pluginTokenRequestedDescription,
|
|
|
|
initialName: name,
|
2022-06-20 17:38:49 +09:00
|
|
|
initialPermissions: permissions,
|
2022-05-04 10:10:52 +09:00
|
|
|
}, {
|
|
|
|
done: async result => {
|
|
|
|
const { name, permissions } = result;
|
|
|
|
const { token } = await os.api('miauth/gen-token', {
|
|
|
|
session: null,
|
|
|
|
name: name,
|
|
|
|
permission: permissions,
|
2021-02-06 21:05:00 +09:00
|
|
|
});
|
2022-05-04 10:10:52 +09:00
|
|
|
res(token);
|
2022-06-20 17:38:49 +09:00
|
|
|
},
|
2022-05-04 10:10:52 +09:00
|
|
|
}, 'closed');
|
|
|
|
});
|
|
|
|
|
|
|
|
installPlugin({
|
|
|
|
id: uuid(),
|
|
|
|
meta: {
|
2022-06-20 17:38:49 +09:00
|
|
|
name, version, author, description, permissions, config,
|
2022-05-04 10:10:52 +09:00
|
|
|
},
|
|
|
|
token,
|
2022-06-20 17:38:49 +09:00
|
|
|
ast: serialize(ast),
|
2022-05-04 10:10:52 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
os.success();
|
|
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
unisonReload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-20 17:38:49 +09:00
|
|
|
const headerActions = $computed(() => []);
|
|
|
|
|
|
|
|
const headerTabs = $computed(() => []);
|
|
|
|
|
|
|
|
definePageMetadata({
|
|
|
|
title: i18n.ts._plugin.install,
|
|
|
|
icon: 'fas fa-download',
|
2021-02-06 21:05:00 +09:00
|
|
|
});
|
|
|
|
</script>
|