summaryrefslogtreecommitdiff
path: root/src/client/plugin.ts
blob: 6bdc4fe4d57189502b4a9dcaebf894bcbe0bbca7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { AiScript, utils, values } from '@syuilo/aiscript';
import { deserialize } from '@syuilo/aiscript/built/serializer';
import { jsToVal } from '@syuilo/aiscript/built/interpreter/util';
import { createAiScriptEnv } from '@client/scripts/aiscript/api';
import { dialog } from '@client/os';
import { noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions } from '@client/store';

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,
		storageKey: 'plugins:' + plugin.id
	}), {
		in: (q) => {
			return new Promise(ok => {
				dialog({
					title: q,
					input: {}
				}).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 || {})) {
		config.set(k, jsToVal(opts.plugin.configData.hasOwnProperty(k) ? opts.plugin.configData[k] : v.default));
	}

	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);
			})]);
		}
	});
}

function registerUserAction({ pluginId, title, handler }) {
	userActions.push({
		title, handler: (user) => {
			pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(user)]);
		}
	});
}

function registerNoteAction({ pluginId, title, handler }) {
	noteActions.push({
		title, handler: (note) => {
			pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]);
		}
	});
}

function registerNoteViewInterruptor({ pluginId, handler }) {
	noteViewInterruptors.push({
		handler: async (note) => {
			return utils.valToJs(await pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]));
		}
	});
}

function registerNotePostInterruptor({ pluginId, handler }) {
	notePostInterruptors.push({
		handler: async (note) => {
			return utils.valToJs(await pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]));
		}
	});
}