rudeshark.net/packages/client/src/pages/settings/plugin.install.vue

154 lines
3.1 KiB
Vue
Raw Normal View History

2021-02-06 13:05:00 +01:00
<template>
2023-04-08 02:01:42 +02:00
<div class="_formRoot">
<FormInfo warn class="_formBlock">{{
i18n.ts._plugin.installWarn
}}</FormInfo>
<FormTextarea v-model="code" tall class="_formBlock">
<template #label>{{ i18n.ts.code }}</template>
</FormTextarea>
<div class="_formBlock">
<FormButton :disabled="code == null" primary inline @click="install"
><i class="ph-check ph-bold ph-lg"></i>
{{ i18n.ts.install }}</FormButton
>
</div>
2022-01-04 09:52:44 +01:00
</div>
2021-02-06 13:05:00 +01:00
</template>
<script lang="ts" setup>
2023-04-08 02:01:42 +02:00
import { defineAsyncComponent, nextTick, ref } from "vue";
import { AiScript, parse } from "@syuilo/aiscript";
import { serialize } from "@syuilo/aiscript/built/serializer";
import { v4 as uuid } from "uuid";
import FormTextarea from "@/components/form/textarea.vue";
import FormButton from "@/components/MkButton.vue";
import FormInfo from "@/components/MkInfo.vue";
import * as os from "@/os";
import { ColdDeviceStorage } from "@/store";
import { unisonReload } from "@/scripts/unison-reload";
import { i18n } from "@/i18n";
import { definePageMetadata } from "@/scripts/page-metadata";
2021-02-06 13:05:00 +01:00
const code = ref(null);
2021-02-06 13:05:00 +01:00
function installPlugin({ id, meta, ast, token }) {
2023-04-08 02:01:42 +02:00
ColdDeviceStorage.set(
"plugins",
ColdDeviceStorage.get("plugins").concat({
...meta,
id,
active: true,
configData: {},
token: token,
ast: ast,
})
);
}
async function install() {
let ast;
try {
ast = parse(code.value);
} catch (err) {
os.alert({
2023-04-08 02:01:42 +02:00
type: "error",
text: "Syntax error :(",
});
return;
}
const meta = AiScript.collectMetadata(ast);
if (meta == null) {
os.alert({
2023-04-08 02:01:42 +02:00
type: "error",
text: "No metadata found :(",
});
return;
}
const metadata = meta.get(null);
if (metadata == null) {
os.alert({
2023-04-08 02:01:42 +02:00
type: "error",
text: "No metadata found :(",
});
return;
}
2023-04-08 02:01:42 +02:00
const { name, version, author, description, permissions, config } =
metadata;
if (name == null || version == null || author == null) {
os.alert({
2023-04-08 02:01:42 +02:00
type: "error",
text: "Required property not found :(",
});
return;
}
2023-04-08 02:01:42 +02:00
const token =
permissions == null || permissions.length === 0
? null
: await new Promise((res, rej) => {
os.popup(
defineAsyncComponent(
() =>
import("@/components/MkTokenGenerateWindow.vue")
),
{
title: i18n.ts.tokenRequested,
information:
i18n.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"
);
});
installPlugin({
id: uuid(),
meta: {
2023-04-08 02:01:42 +02:00
name,
version,
author,
description,
permissions,
config,
},
token,
ast: serialize(ast),
});
os.success();
nextTick(() => {
unisonReload();
});
}
const headerActions = $computed(() => []);
const headerTabs = $computed(() => []);
definePageMetadata({
title: i18n.ts._plugin.install,
2023-04-08 02:01:42 +02:00
icon: "ph-download-simple ph-bold ph-lg",
2021-02-06 13:05:00 +01:00
});
</script>