2023-01-03 06:51:49 +00:00
|
|
|
import { Interpreter, Parser, utils, values } from '@syuilo/aiscript';
|
2021-11-11 17:02:25 +00:00
|
|
|
import { createAiScriptEnv } from '@/scripts/aiscript/api';
|
2021-11-18 14:36:04 +00:00
|
|
|
import { inputText } from '@/os';
|
2023-03-01 06:19:20 +00:00
|
|
|
import { Plugin, noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions } from '@/store';
|
2020-10-17 11:12:00 +00:00
|
|
|
|
2023-01-03 06:51:49 +00:00
|
|
|
const parser = new Parser();
|
|
|
|
const pluginContexts = new Map<string, Interpreter>();
|
2020-10-17 11:12:00 +00:00
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
export function install(plugin: Plugin): void {
|
2023-01-03 06:51:49 +00:00
|
|
|
// 後方互換性のため
|
|
|
|
if (plugin.src == null) return;
|
2020-10-17 11:12:00 +00:00
|
|
|
console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
|
|
|
|
|
2023-01-03 06:51:49 +00:00
|
|
|
const aiscript = new Interpreter(createPluginEnv({
|
2020-10-17 11:12:00 +00:00
|
|
|
plugin: plugin,
|
2022-12-22 07:01:59 +00:00
|
|
|
storageKey: 'plugins:' + plugin.id,
|
2020-10-17 11:12:00 +00:00
|
|
|
}), {
|
2023-03-01 06:19:20 +00:00
|
|
|
in: (q): Promise<string> => {
|
2020-10-17 11:12:00 +00:00
|
|
|
return new Promise(ok => {
|
2021-11-18 14:36:04 +00:00
|
|
|
inputText({
|
2020-10-17 11:12:00 +00:00
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
2023-02-05 11:29:10 +00:00
|
|
|
if (canceled) {
|
|
|
|
ok('');
|
|
|
|
} else {
|
|
|
|
ok(a);
|
|
|
|
}
|
2020-10-17 11:12:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2023-03-01 06:19:20 +00:00
|
|
|
out: (value): void => {
|
2020-10-17 11:12:00 +00:00
|
|
|
console.log(value);
|
|
|
|
},
|
2023-03-01 06:19:20 +00:00
|
|
|
log: (): void => {
|
2020-10-17 11:12:00 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
initPlugin({ plugin, aiscript });
|
|
|
|
|
2023-01-03 06:51:49 +00:00
|
|
|
aiscript.exec(parser.parse(plugin.src));
|
2020-10-17 11:12:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Record<string, values.Value> {
|
|
|
|
const config = new Map<string, values.Value>();
|
|
|
|
for (const [k, v] of Object.entries(opts.plugin.config ?? {})) {
|
2023-01-03 06:51:49 +00:00
|
|
|
config.set(k, utils.jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
|
2020-10-17 11:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...createAiScriptEnv({ ...opts, token: opts.plugin.token }),
|
|
|
|
//#region Deprecated
|
|
|
|
'Mk:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
utils.assertString(title);
|
2020-10-17 11:12:00 +00:00
|
|
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
utils.assertString(title);
|
2020-10-17 11:12:00 +00:00
|
|
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Mk:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
utils.assertString(title);
|
2020-10-17 11:12:00 +00:00
|
|
|
registerNoteAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
//#endregion
|
|
|
|
'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
utils.assertString(title);
|
2020-10-17 11:12:00 +00:00
|
|
|
registerPostFormAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
utils.assertString(title);
|
2020-10-17 11:12:00 +00:00
|
|
|
registerUserAction({ pluginId: opts.plugin.id, title: title.value, handler });
|
|
|
|
}),
|
|
|
|
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
utils.assertString(title);
|
2020-10-17 11:12:00 +00:00
|
|
|
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]) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
utils.assertString(url);
|
2020-10-17 11:12:00 +00:00
|
|
|
window.open(url.value, '_blank');
|
|
|
|
}),
|
|
|
|
'Plugin:config': values.OBJ(config),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
function initPlugin({ plugin, aiscript }): void {
|
2020-10-17 11:12:00 +00:00
|
|
|
pluginContexts.set(plugin.id, aiscript);
|
|
|
|
}
|
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
function registerPostFormAction({ pluginId, title, handler }): void {
|
2020-10-17 11:12:00 +00:00
|
|
|
postFormActions.push({
|
|
|
|
title, handler: (form, update) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => {
|
|
|
|
if (!key || !value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update(utils.valToJs(key), utils.valToJs(value));
|
2020-10-17 11:12:00 +00:00
|
|
|
})]);
|
2022-12-22 07:01:59 +00:00
|
|
|
},
|
2020-10-17 11:12:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
function registerUserAction({ pluginId, title, handler }): void {
|
2020-10-17 11:12:00 +00:00
|
|
|
userActions.push({
|
|
|
|
title, handler: (user) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(user)]);
|
2022-12-22 07:01:59 +00:00
|
|
|
},
|
2020-10-17 11:12:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
function registerNoteAction({ pluginId, title, handler }): void {
|
2020-10-17 11:12:00 +00:00
|
|
|
noteActions.push({
|
|
|
|
title, handler: (note) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(note)]);
|
2022-12-22 07:01:59 +00:00
|
|
|
},
|
2020-10-17 11:12:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
function registerNoteViewInterruptor({ pluginId, handler }): void {
|
2020-10-17 11:12:00 +00:00
|
|
|
noteViewInterruptors.push({
|
|
|
|
handler: async (note) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(note)]));
|
2022-12-22 07:01:59 +00:00
|
|
|
},
|
2020-10-17 11:12:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-01 06:19:20 +00:00
|
|
|
function registerNotePostInterruptor({ pluginId, handler }): void {
|
2020-10-17 11:12:00 +00:00
|
|
|
notePostInterruptors.push({
|
|
|
|
handler: async (note) => {
|
2023-03-01 06:19:20 +00:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(note)]));
|
2022-12-22 07:01:59 +00:00
|
|
|
},
|
2020-10-17 11:12:00 +00:00
|
|
|
});
|
|
|
|
}
|