2020-10-17 14:12:00 +03:00
|
|
|
import { AiScript, utils, values } from '@syuilo/aiscript';
|
|
|
|
import { deserialize } from '@syuilo/aiscript/built/serializer';
|
|
|
|
import { jsToVal } from '@syuilo/aiscript/built/interpreter/util';
|
2021-11-11 19:02:25 +02:00
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
2021-11-18 16:36:04 +02:00
|
|
|
import { inputText } from '@/os';
|
2021-11-11 19:02:25 +02:00
|
|
|
import { noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions } from '@/store';
|
2020-10-17 14:12:00 +03:00
|
|
|
|
|
|
|
const pluginContexts = new Map<string, AiScript>();
|
|
|
|
|
|
|
|
export function install(plugin) {
|
|
|
|
console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
|
|
|
|
|
|
|
|
const aiscript = new AiScript(createPluginEnv({
|
|
|
|
plugin: plugin,
|
2022-12-22 09:01:59 +02:00
|
|
|
storageKey: 'plugins:' + plugin.id,
|
2020-10-17 14:12:00 +03:00
|
|
|
}), {
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise(ok => {
|
2021-11-18 16:36:04 +02:00
|
|
|
inputText({
|
2020-10-17 14:12:00 +03:00
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
|
|
|
ok(a);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
out: (value) => {
|
|
|
|
console.log(value);
|
|
|
|
},
|
|
|
|
log: (type, params) => {
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
initPlugin({ plugin, aiscript });
|
|
|
|
|
|
|
|
aiscript.exec(deserialize(plugin.ast));
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPluginEnv(opts) {
|
|
|
|
const config = new Map();
|
|
|
|
for (const [k, v] of Object.entries(opts.plugin.config || {})) {
|
2022-07-04 17:39:04 +03:00
|
|
|
config.set(k, jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...createAiScriptEnv({ ...opts, token: opts.plugin.token }),
|
|
|
|
//#region Deprecated
|
|
|
|
'Mk:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
//#endregion
|
|
|
|
'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
|
|
|
|
registerNoteViewInterruptor({ pluginId: opts.plugin.id, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_post_interruptor': values.FN_NATIVE(([handler]) => {
|
|
|
|
registerNotePostInterruptor({ pluginId: opts.plugin.id, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:open_url': values.FN_NATIVE(([url]) => {
|
|
|
|
window.open(url.value, '_blank');
|
|
|
|
}),
|
|
|
|
'Plugin:config': values.OBJ(config),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function initPlugin({ plugin, aiscript }) {
|
|
|
|
pluginContexts.set(plugin.id, aiscript);
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerPostFormAction({ pluginId, title, handler }) {
|
|
|
|
postFormActions.push({
|
|
|
|
title, handler: (form, update) => {
|
|
|
|
pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => {
|
|
|
|
update(key.value, value.value);
|
|
|
|
})]);
|
2022-12-22 09:01:59 +02:00
|
|
|
},
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerUserAction({ pluginId, title, handler }) {
|
|
|
|
userActions.push({
|
|
|
|
title, handler: (user) => {
|
|
|
|
pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(user)]);
|
2022-12-22 09:01:59 +02:00
|
|
|
},
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerNoteAction({ pluginId, title, handler }) {
|
|
|
|
noteActions.push({
|
|
|
|
title, handler: (note) => {
|
|
|
|
pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]);
|
2022-12-22 09:01:59 +02:00
|
|
|
},
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerNoteViewInterruptor({ pluginId, handler }) {
|
|
|
|
noteViewInterruptors.push({
|
|
|
|
handler: async (note) => {
|
|
|
|
return utils.valToJs(await pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]));
|
2022-12-22 09:01:59 +02:00
|
|
|
},
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerNotePostInterruptor({ pluginId, handler }) {
|
|
|
|
notePostInterruptors.push({
|
|
|
|
handler: async (note) => {
|
|
|
|
return utils.valToJs(await pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]));
|
2022-12-22 09:01:59 +02:00
|
|
|
},
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
}
|