2019-04-29 03:11:57 +03:00
|
|
|
<template>
|
2020-01-29 23:19:18 +02:00
|
|
|
<div class="iroscrza" :class="{ center: page.alignCenter, serif: page.font === 'serif' }" v-if="script">
|
|
|
|
<x-block v-for="child in page.content" :value="child" @input="v => updateBlock(v)" :page="page" :script="script" :key="child.id" :h="2"/>
|
2019-04-29 03:11:57 +03:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import Vue from 'vue';
|
2020-04-12 21:23:23 +03:00
|
|
|
import { AiScript, parse, values } from '@syuilo/aiscript';
|
2019-05-17 13:56:47 +03:00
|
|
|
import { faHeart as faHeartS } from '@fortawesome/free-solid-svg-icons';
|
2019-07-07 00:56:13 +03:00
|
|
|
import { faHeart } from '@fortawesome/free-regular-svg-icons';
|
2020-04-12 21:23:23 +03:00
|
|
|
import i18n from '../../i18n';
|
2019-04-29 03:11:57 +03:00
|
|
|
import XBlock from './page.block.vue';
|
2020-04-12 13:38:19 +03:00
|
|
|
import { ASEvaluator } from '../../scripts/aoiscript/evaluator';
|
2020-01-29 21:37:25 +02:00
|
|
|
import { collectPageVars } from '../../scripts/collect-page-vars';
|
|
|
|
import { url } from '../../config';
|
2019-04-29 03:11:57 +03:00
|
|
|
|
|
|
|
class Script {
|
2020-04-12 21:23:23 +03:00
|
|
|
public aoiScript: ASEvaluator;
|
2019-05-01 09:17:24 +03:00
|
|
|
private onError: any;
|
2019-05-01 08:54:34 +03:00
|
|
|
public vars: Record<string, any>;
|
2019-07-06 12:14:50 +03:00
|
|
|
public page: Record<string, any>;
|
2019-04-29 03:11:57 +03:00
|
|
|
|
2020-04-12 21:23:23 +03:00
|
|
|
constructor(page, aoiScript, onError, cb) {
|
2019-07-06 12:14:50 +03:00
|
|
|
this.page = page;
|
2020-04-12 21:23:23 +03:00
|
|
|
this.aoiScript = aoiScript;
|
2019-05-01 09:17:24 +03:00
|
|
|
this.onError = onError;
|
2020-04-12 21:23:23 +03:00
|
|
|
|
|
|
|
if (this.page.script) {
|
|
|
|
let ast;
|
|
|
|
try {
|
|
|
|
ast = parse(this.page.script);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
/*this.$root.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: 'Syntax error :('
|
|
|
|
});*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.aoiScript.aiscript.exec(ast).then(() => {
|
|
|
|
this.eval();
|
|
|
|
cb();
|
|
|
|
}).catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
/*this.$root.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: e
|
|
|
|
});*/
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.eval();
|
|
|
|
cb();
|
|
|
|
}
|
2019-04-29 03:11:57 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 09:17:24 +03:00
|
|
|
public eval() {
|
|
|
|
try {
|
2020-04-12 21:23:23 +03:00
|
|
|
this.vars = this.aoiScript.evaluateVars();
|
2019-05-01 09:17:24 +03:00
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
}
|
2019-04-29 03:11:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public interpolate(str: string) {
|
2019-04-30 00:40:02 +03:00
|
|
|
if (str == null) return null;
|
2019-05-05 03:27:55 +03:00
|
|
|
return str.replace(/{(.+?)}/g, match => {
|
2019-05-01 08:54:34 +03:00
|
|
|
const v = this.vars[match.slice(1, -1).trim()];
|
2019-04-29 05:08:35 +03:00
|
|
|
return v == null ? 'NULL' : v.toString();
|
|
|
|
});
|
2019-04-29 03:11:57 +03:00
|
|
|
}
|
2020-04-12 21:23:23 +03:00
|
|
|
|
|
|
|
public callAiScript(fn: string) {
|
|
|
|
this.aoiScript.aiscript.execFn(this.aoiScript.aiscript.scope.get(fn), []);
|
|
|
|
}
|
2019-04-29 03:11:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Vue.extend({
|
2020-01-29 21:37:25 +02:00
|
|
|
i18n,
|
2019-04-29 03:11:57 +03:00
|
|
|
|
|
|
|
components: {
|
|
|
|
XBlock
|
|
|
|
},
|
|
|
|
|
|
|
|
props: {
|
2019-07-07 00:56:13 +03:00
|
|
|
page: {
|
|
|
|
type: Object,
|
2019-04-29 03:11:57 +03:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
script: null,
|
2019-05-17 13:56:47 +03:00
|
|
|
faHeartS, faHeart
|
2019-04-29 03:11:57 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
2019-07-07 00:56:13 +03:00
|
|
|
const pageVars = this.getPageVars();
|
2020-04-12 21:23:23 +03:00
|
|
|
|
|
|
|
const s = new Script(this.page, new ASEvaluator(this, this.page.variables, pageVars, {
|
2019-07-07 00:56:13 +03:00
|
|
|
randomSeed: Math.random(),
|
|
|
|
visitor: this.$store.state.i,
|
|
|
|
page: this.page,
|
|
|
|
url: url
|
|
|
|
}), e => {
|
|
|
|
console.dir(e);
|
2020-04-12 21:23:23 +03:00
|
|
|
}, () => {
|
|
|
|
this.script = s;
|
2019-07-07 00:56:13 +03:00
|
|
|
});
|
2020-04-12 21:23:23 +03:00
|
|
|
|
|
|
|
s.aoiScript.aiscript.scope.opts.onUpdated = (name, value) => {
|
|
|
|
s.eval();
|
|
|
|
};
|
2019-04-29 03:11:57 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
getPageVars() {
|
|
|
|
return collectPageVars(this.page.content);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
<style lang="scss" scoped>
|
|
|
|
.iroscrza {
|
|
|
|
&.serif {
|
|
|
|
> div {
|
|
|
|
font-family: serif;
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 03:11:57 +03:00
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
&.center {
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 03:11:57 +03:00
|
|
|
</style>
|