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