From d40a0392cd182049196b087e2da1e3b28fc5472a Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 21 Sep 2020 11:38:54 +0900 Subject: [PATCH] wip --- src/client/components/page/page.vue | 2 +- src/client/init.ts | 7 ++--- src/client/pages/scratchpad.vue | 2 +- src/client/scripts/aiscript/api.ts | 38 ++++++++++++++-------------- src/client/scripts/hpml/evaluator.ts | 7 ++--- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/client/components/page/page.vue b/src/client/components/page/page.vue index 3fde9d4ba6..c4fe251004 100644 --- a/src/client/components/page/page.vue +++ b/src/client/components/page/page.vue @@ -34,7 +34,7 @@ export default defineComponent({ }, created() { - this.hpml = new Hpml(this, this.page, { + this.hpml = new Hpml(this.page, { randomSeed: Math.random(), visitor: this.$store.state.i, url: url, diff --git a/src/client/init.ts b/src/client/init.ts index b5c02ba3ab..831e195077 100644 --- a/src/client/init.ts +++ b/src/client/init.ts @@ -21,7 +21,7 @@ import { applyTheme, lightTheme } from '@/scripts/theme'; import { isDeviceDarkmode } from '@/scripts/is-device-darkmode'; import { createPluginEnv } from '@/scripts/aiscript/api'; import { i18n, lang } from './i18n'; -import { stream, sound, isMobile } from '@/os'; +import { stream, sound, isMobile, dialog } from '@/os'; console.info(`Misskey v${version}`); @@ -131,6 +131,7 @@ app.use(router); app.use(i18n); app.use(VueHotkey); app.use(VAnimateCss); +// eslint-disable-next-line vue/component-definition-name-casing app.component('fa', FontAwesomeIcon); widgets(app); @@ -204,13 +205,13 @@ stream.on('emojiAdded', data => { for (const plugin of store.state.deviceUser.plugins.filter(p => p.active)) { console.info('Plugin installed:', plugin.name, 'v' + plugin.version); - const aiscript = new AiScript(createPluginEnv(app, { + const aiscript = new AiScript(createPluginEnv({ plugin: plugin, storageKey: 'plugins:' + plugin.id }), { in: (q) => { return new Promise(ok => { - app.dialog({ + dialog({ title: q, input: {} }).then(({ canceled, result: a }) => { diff --git a/src/client/pages/scratchpad.vue b/src/client/pages/scratchpad.vue index 4eedba63a8..d412a1c22a 100644 --- a/src/client/pages/scratchpad.vue +++ b/src/client/pages/scratchpad.vue @@ -73,7 +73,7 @@ export default defineComponent({ methods: { async run() { this.logs = []; - const aiscript = new AiScript(createAiScriptEnv(this, { + const aiscript = new AiScript(createAiScriptEnv({ storageKey: 'scratchpad' }), { in: (q) => { diff --git a/src/client/scripts/aiscript/api.ts b/src/client/scripts/aiscript/api.ts index 181ab8e833..e8ff977772 100644 --- a/src/client/scripts/aiscript/api.ts +++ b/src/client/scripts/aiscript/api.ts @@ -1,22 +1,23 @@ import { utils, values } from '@syuilo/aiscript'; import { jsToVal } from '@syuilo/aiscript/built/interpreter/util'; +import { store } from '@/store'; +import * as os from '@/os'; -// TODO: vm引数は消せる(各種操作がstoreに移動し、かつstoreが複数ファイルで共有されるようになったため) -export function createAiScriptEnv(vm, opts) { +export function createAiScriptEnv(opts) { let apiRequests = 0; return { - 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, + USER_ID: store.getters.isSignedIn ? values.STR(store.state.i.id) : values.NULL, + USER_NAME: store.getters.isSignedIn ? values.STR(store.state.i.name) : values.NULL, + USER_USERNAME: store.getters.isSignedIn ? values.STR(store.state.i.username) : values.NULL, 'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => { - await vm.os.dialog({ + await os.dialog({ type: type ? type.value : 'info', title: title.value, text: text.value, }); }), 'Mk:confirm': values.FN_NATIVE(async ([title, text, type]) => { - const confirm = await vm.os.dialog({ + const confirm = await os.dialog({ type: type ? type.value : 'question', showCancelButton: true, title: title.value, @@ -28,7 +29,7 @@ export function createAiScriptEnv(vm, opts) { if (token) utils.assertString(token); apiRequests++; if (apiRequests > 16) return values.NULL; - const res = await vm.os.api(ep.value, utils.valToJs(param), token ? token.value : (opts.token || null)); + const res = await os.api(ep.value, utils.valToJs(param), token ? token.value : (opts.token || null)); return utils.jsToVal(res); }), 'Mk:save': values.FN_NATIVE(([key, value]) => { @@ -43,40 +44,39 @@ export function createAiScriptEnv(vm, opts) { }; } -// TODO: vm引数は消せる(各種操作がstoreに移動し、かつstoreが複数ファイルで共有されるようになったため) -export function createPluginEnv(vm, opts) { +export function createPluginEnv(opts) { const config = new Map(); for (const [k, v] of Object.entries(opts.plugin.config || {})) { config.set(k, jsToVal(opts.plugin.configData[k] || v.default)); } return { - ...createAiScriptEnv(vm, { ...opts, token: opts.plugin.token }), + ...createAiScriptEnv({ ...opts, token: opts.plugin.token }), //#region Deprecated 'Mk:register_post_form_action': values.FN_NATIVE(([title, handler]) => { - vm.$store.commit('registerPostFormAction', { pluginId: opts.plugin.id, title: title.value, handler }); + 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 }); + 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 }); + store.commit('registerNoteAction', { pluginId: opts.plugin.id, title: title.value, handler }); }), //#endregion 'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => { - vm.$store.commit('registerPostFormAction', { pluginId: opts.plugin.id, title: title.value, handler }); + 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 }); + 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 }); + store.commit('registerNoteAction', { pluginId: opts.plugin.id, title: title.value, handler }); }), 'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => { - vm.$store.commit('registerNoteViewInterruptor', { pluginId: opts.plugin.id, handler }); + store.commit('registerNoteViewInterruptor', { pluginId: opts.plugin.id, handler }); }), 'Plugin:register_note_post_interruptor': values.FN_NATIVE(([handler]) => { - vm.$store.commit('registerNotePostInterruptor', { pluginId: opts.plugin.id, handler }); + store.commit('registerNotePostInterruptor', { pluginId: opts.plugin.id, handler }); }), 'Plugin:open_url': values.FN_NATIVE(([url]) => { window.open(url.value, '_blank'); diff --git a/src/client/scripts/hpml/evaluator.ts b/src/client/scripts/hpml/evaluator.ts index 6ca9eccc6f..01a122c0e4 100644 --- a/src/client/scripts/hpml/evaluator.ts +++ b/src/client/scripts/hpml/evaluator.ts @@ -6,6 +6,7 @@ import { AiScript, utils, values } from '@syuilo/aiscript'; import { createAiScriptEnv } from '../aiscript/api'; import { collectPageVars } from '../collect-page-vars'; import { initLib } from './lib'; +import * as os from '@/os'; type Fn = { slots: string[]; @@ -30,19 +31,19 @@ export class Hpml { enableAiScript: boolean; }; - constructor(vm: any, page: Hpml['page'], opts: Hpml['opts']) { + constructor(page: Hpml['page'], opts: Hpml['opts']) { this.page = page; this.variables = this.page.variables; this.pageVars = collectPageVars(this.page.content); this.opts = opts; if (this.opts.enableAiScript) { - this.aiscript = new AiScript({ ...createAiScriptEnv(vm, { + this.aiscript = new AiScript({ ...createAiScriptEnv({ storageKey: 'pages:' + this.page.id }), ...initLib(this)}, { in: (q) => { return new Promise(ok => { - vm.os.dialog({ + os.dialog({ title: q, input: {} }).then(({ canceled, result: a }) => {