From a19e252c9e7e1d892a383641a50e1455bb68e332 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 13 Apr 2020 23:46:53 +0900 Subject: [PATCH] feat(client): Improve pages aiscript --- package.json | 2 +- src/client/components/page/page.vue | 19 ++++--- src/client/pages/preferences/index.vue | 6 +++ src/client/scripts/aoiscript/evaluator.ts | 60 +++++++++++++---------- src/client/store.ts | 1 + yarn.lock | 10 ++-- 6 files changed, 61 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 8fa0129cb..9146d73c7 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@koa/cors": "3.0.0", "@koa/multer": "2.0.2", "@koa/router": "8.0.8", - "@syuilo/aiscript": "0.1.4", + "@syuilo/aiscript": "0.2.0", "@types/bcryptjs": "2.4.2", "@types/bull": "3.12.1", "@types/cbor": "5.0.0", diff --git a/src/client/components/page/page.vue b/src/client/components/page/page.vue index 0f1769fc8..3723fcd3c 100644 --- a/src/client/components/page/page.vue +++ b/src/client/components/page/page.vue @@ -26,7 +26,7 @@ class Script { this.aoiScript = aoiScript; this.onError = onError; - if (this.page.script) { + if (this.page.script && this.aoiScript.aiscript) { let ast; try { ast = parse(this.page.script); @@ -49,8 +49,10 @@ class Script { });*/ }); } else { - this.eval(); - cb(); + setTimeout(() => { + this.eval(); + cb(); + }, 1); } } @@ -71,7 +73,7 @@ class Script { } public callAiScript(fn: string) { - this.aoiScript.aiscript.execFn(this.aoiScript.aiscript.scope.get(fn), []); + if (this.aoiScript.aiscript) this.aoiScript.aiscript.execFn(this.aoiScript.aiscript.scope.get(fn), []); } } @@ -103,18 +105,23 @@ export default Vue.extend({ randomSeed: Math.random(), visitor: this.$store.state.i, page: this.page, - url: url + url: url, + enableAiScript: !this.$store.state.device.disablePagesScript }), e => { console.dir(e); }, () => { this.script = s; }); - s.aoiScript.aiscript.scope.opts.onUpdated = (name, value) => { + if (s.aoiScript.aiscript) s.aoiScript.aiscript.scope.opts.onUpdated = (name, value) => { s.eval(); }; }, + beforeDestroy() { + if (this.script.aoiScript.aiscript) this.script.aoiScript.aiscript.abort(); + }, + methods: { getPageVars() { return collectPageVars(this.page.content); diff --git a/src/client/pages/preferences/index.vue b/src/client/pages/preferences/index.vue index 2b1b3083a..e6d0fd0fe 100644 --- a/src/client/pages/preferences/index.vue +++ b/src/client/pages/preferences/index.vue @@ -65,6 +65,7 @@ {{ $t('showFixedPostForm') }} + {{ $t('disablePagesScript') }}
@@ -171,6 +172,11 @@ export default Vue.extend({ set(value) { this.$store.commit('device/set', { key: 'imageNewTab', value }); } }, + disablePagesScript: { + get() { return this.$store.state.device.disablePagesScript; }, + set(value) { this.$store.commit('device/set', { key: 'disablePagesScript', value }); } + }, + showFixedPostForm: { get() { return this.$store.state.device.showFixedPostForm; }, set(value) { this.$store.commit('device/set', { key: 'showFixedPostForm', value }); } diff --git a/src/client/scripts/aoiscript/evaluator.ts b/src/client/scripts/aoiscript/evaluator.ts index 8d8a5b2e0..40e62210b 100644 --- a/src/client/scripts/aoiscript/evaluator.ts +++ b/src/client/scripts/aoiscript/evaluator.ts @@ -17,41 +17,45 @@ export class ASEvaluator { private variables: Variable[]; private pageVars: PageVar[]; private envVars: Record; - public aiscript: AiScript; + public aiscript: AiScript | undefined; private pageVarUpdatedCallback; private opts: { randomSeed: string; visitor?: any; page?: any; url?: string; + enableAiScript: boolean; }; constructor(vm: any, variables: Variable[], pageVars: PageVar[], opts: ASEvaluator['opts']) { this.variables = variables; this.pageVars = pageVars; this.opts = opts; - this.aiscript = new AiScript({ ...createAiScriptEnv(vm, { - storageKey: 'pages:' + opts.page.id - }), ...{ - 'MkPages:updated': values.FN_NATIVE(([callback]) => { - this.pageVarUpdatedCallback = callback; - }) - }}, { - in: (q) => { - return new Promise(ok => { - vm.$root.dialog({ - title: q, - input: {} - }).then(({ canceled, result: a }) => { - ok(a); + + if (this.opts.enableAiScript) { + this.aiscript = new AiScript({ ...createAiScriptEnv(vm, { + storageKey: 'pages:' + opts.page.id + }), ...{ + 'MkPages:updated': values.FN_NATIVE(([callback]) => { + this.pageVarUpdatedCallback = callback; + }) + }}, { + in: (q) => { + return new Promise(ok => { + vm.$root.dialog({ + title: q, + input: {} + }).then(({ canceled, result: a }) => { + ok(a); + }); }); - }); - }, - out: (value) => { - console.log(value); - }, - log: (type, params) => { - }, - maxStep: 16384 - }); + }, + out: (value) => { + console.log(value); + }, + log: (type, params) => { + }, + maxStep: 16384 + }); + } const date = new Date(); @@ -79,7 +83,7 @@ export class ASEvaluator { if (pageVar !== undefined) { pageVar.value = value; if (this.pageVarUpdatedCallback) { - this.aiscript.execFn(this.pageVarUpdatedCallback, [values.STR(name), utils.jsToVal(value)]); + if (this.aiscript) this.aiscript.execFn(this.pageVarUpdatedCallback, [values.STR(name), utils.jsToVal(value)]); } } else { throw new AoiScriptError(`No such page var '${name}'`); @@ -142,7 +146,11 @@ export class ASEvaluator { } if (block.type === 'aiScriptVar') { - return utils.valToJs(this.aiscript.scope.get(block.value)); + if (this.aiscript) { + return utils.valToJs(this.aiscript.scope.get(block.value)); + } else { + return null; + } } if (isFnBlock(block)) { // ユーザー関数定義 diff --git a/src/client/store.ts b/src/client/store.ts index 14c854860..0b596ba41 100644 --- a/src/client/store.ts +++ b/src/client/store.ts @@ -42,6 +42,7 @@ const defaultDeviceSettings = { animatedMfm: true, imageNewTab: false, showFixedPostForm: false, + disablePagesScript: true, sfxVolume: 0.3, sfxNote: 'syuilo/down', sfxNoteMy: 'syuilo/up', diff --git a/yarn.lock b/yarn.lock index 598ea8ee5..724488a69 100644 --- a/yarn.lock +++ b/yarn.lock @@ -144,13 +144,15 @@ dependencies: type-detect "4.0.8" -"@syuilo/aiscript@0.1.4": - version "0.1.4" - resolved "https://registry.yarnpkg.com/@syuilo/aiscript/-/aiscript-0.1.4.tgz#ff027552f32990ae3e29145ce6efe0a7a516b442" - integrity sha512-SMDlBInsGTL3DOe0U394X7na0N6ryYg0RGQPPtCVhXkJpVDZiaqUe5vDO+DkRyuRlkmBbN82LWToou19j/Uv8g== +"@syuilo/aiscript@0.2.0": + version "0.2.0" + resolved "https://registry.yarnpkg.com/@syuilo/aiscript/-/aiscript-0.2.0.tgz#dcb489bca13f6d965ac86034a45fd46514b1487a" + integrity sha512-N9fYchn3zjtniG9fNmZ81PwYZFdulk+RSBcjDZWBgHsFJQc1wxOCr9hZux/vSXrZ/ZWEzK0loNz1dorl2jJLeA== dependencies: + "@types/seedrandom" "2.4.28" autobind-decorator "2.4.0" chalk "4.0.0" + seedrandom "3.0.5" uuid "7.0.3" "@tokenizer/token@^0.1.0", "@tokenizer/token@^0.1.1":