2020-04-12 19:38:19 +09:00
|
|
|
import { utils, values } from '@syuilo/aiscript';
|
2020-07-18 14:28:32 +09:00
|
|
|
import { jsToVal } from '@syuilo/aiscript/built/interpreter/util';
|
2020-04-12 19:38:19 +09:00
|
|
|
|
2020-08-01 12:04:30 +09:00
|
|
|
// TODO: vue3に移行した折にはvmを渡す必要は無くなるはず
|
2020-04-12 19:57:18 +09:00
|
|
|
export function createAiScriptEnv(vm, opts) {
|
2020-04-13 03:23:23 +09:00
|
|
|
let apiRequests = 0;
|
2020-04-12 19:38:19 +09:00
|
|
|
return {
|
2020-04-19 16:28:19 +09:00
|
|
|
USER_ID: vm.$store.getters.isSignedIn ? values.STR(vm.$store.state.i.id) : values.NULL,
|
|
|
|
USER_NAME: vm.$store.getters.isSignedIn ? values.STR(vm.$store.state.i.name) : values.NULL,
|
|
|
|
USER_USERNAME: vm.$store.getters.isSignedIn ? values.STR(vm.$store.state.i.username) : values.NULL,
|
2020-04-12 19:38:19 +09:00
|
|
|
'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => {
|
|
|
|
await vm.$root.dialog({
|
|
|
|
type: type ? type.value : 'info',
|
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
|
|
|
}),
|
2020-07-28 19:02:28 +09:00
|
|
|
'Mk:confirm': values.FN_NATIVE(async ([title, text, type]) => {
|
2020-04-12 19:38:19 +09:00
|
|
|
const confirm = await vm.$root.dialog({
|
2020-07-28 19:02:28 +09:00
|
|
|
type: type ? type.value : 'question',
|
2020-04-12 19:38:19 +09:00
|
|
|
showCancelButton: true,
|
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
2020-05-10 17:25:16 +09:00
|
|
|
return confirm.canceled ? values.FALSE : values.TRUE;
|
2020-04-12 19:38:19 +09:00
|
|
|
}),
|
|
|
|
'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
|
2020-05-15 20:53:24 +09:00
|
|
|
if (token) utils.assertString(token);
|
2020-04-13 03:23:23 +09:00
|
|
|
apiRequests++;
|
|
|
|
if (apiRequests > 16) return values.NULL;
|
2020-07-18 14:28:32 +09:00
|
|
|
const res = await vm.$root.api(ep.value, utils.valToJs(param), token ? token.value : (opts.token || null));
|
2020-04-12 19:38:19 +09:00
|
|
|
return utils.jsToVal(res);
|
|
|
|
}),
|
2020-04-12 19:57:18 +09:00
|
|
|
'Mk:save': values.FN_NATIVE(([key, value]) => {
|
|
|
|
utils.assertString(key);
|
|
|
|
localStorage.setItem('aiscript:' + opts.storageKey + ':' + key.value, JSON.stringify(utils.valToJs(value)));
|
|
|
|
return values.NULL;
|
|
|
|
}),
|
|
|
|
'Mk:load': values.FN_NATIVE(([key]) => {
|
|
|
|
utils.assertString(key);
|
|
|
|
return utils.jsToVal(JSON.parse(localStorage.getItem('aiscript:' + opts.storageKey + ':' + key.value)));
|
|
|
|
}),
|
2020-04-12 19:38:19 +09:00
|
|
|
};
|
|
|
|
}
|
2020-07-12 00:38:55 +09:00
|
|
|
|
2020-08-01 12:04:30 +09:00
|
|
|
// TODO: vue3に移行した折にはvmを渡す必要は無くなるはず
|
2020-07-12 00:38:55 +09:00
|
|
|
export function createPluginEnv(vm, opts) {
|
2020-07-18 14:28:32 +09:00
|
|
|
const config = new Map();
|
2020-07-28 19:02:28 +09:00
|
|
|
for (const [k, v] of Object.entries(opts.plugin.config || {})) {
|
2020-07-20 01:07:02 +09:00
|
|
|
config.set(k, jsToVal(opts.plugin.configData[k] || v.default));
|
2020-07-18 14:28:32 +09:00
|
|
|
}
|
|
|
|
|
2020-07-12 00:38:55 +09:00
|
|
|
return {
|
2020-07-18 14:28:32 +09:00
|
|
|
...createAiScriptEnv(vm, { ...opts, token: opts.plugin.token }),
|
2020-07-28 19:02:28 +09:00
|
|
|
//#region Deprecated
|
2020-07-12 00:38:55 +09:00
|
|
|
'Mk:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
vm.$store.commit('registerPostFormAction', { pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
vm.$store.commit('registerUserAction', { pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
vm.$store.commit('registerNoteAction', { pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
2020-07-28 19:02:28 +09:00
|
|
|
//#endregion
|
|
|
|
'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
vm.$store.commit('registerPostFormAction', { pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
vm.$store.commit('registerUserAction', { pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
vm.$store.commit('registerNoteAction', { pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
2020-07-29 01:15:02 +09:00
|
|
|
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
|
|
|
|
vm.$store.commit('registerNoteViewInterruptor', { pluginId: opts.plugin.id, handler });
|
|
|
|
}),
|
2020-07-29 23:37:50 +09:00
|
|
|
'Plugin:register_note_post_interruptor': values.FN_NATIVE(([handler]) => {
|
|
|
|
vm.$store.commit('registerNotePostInterruptor', { pluginId: opts.plugin.id, handler });
|
|
|
|
}),
|
2020-07-29 23:10:04 +09:00
|
|
|
'Plugin:open_url': values.FN_NATIVE(([url]) => {
|
|
|
|
window.open(url.value, '_blank');
|
|
|
|
}),
|
2020-07-18 14:28:32 +09:00
|
|
|
'Plugin:config': values.OBJ(config),
|
2020-07-12 00:38:55 +09:00
|
|
|
};
|
|
|
|
}
|