Sharkey/src/client/components/page/page.vue

146 lines
3 KiB
Vue
Raw Normal View History

<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"/>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
2020-04-12 21:23:23 +03:00
import { AiScript, parse, values } from '@syuilo/aiscript';
import { faHeart as faHeartS } from '@fortawesome/free-solid-svg-icons';
import { faHeart } from '@fortawesome/free-regular-svg-icons';
2020-04-12 21:23:23 +03:00
import i18n from '../../i18n';
import XBlock from './page.block.vue';
2020-04-12 13:38:19 +03:00
import { ASEvaluator } from '../../scripts/aoiscript/evaluator';
import { collectPageVars } from '../../scripts/collect-page-vars';
import { url } from '../../config';
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>;
public page: Record<string, any>;
2020-04-12 21:23:23 +03:00
constructor(page, aoiScript, onError, cb) {
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
2020-04-13 17:46:53 +03:00
if (this.page.script && this.aoiScript.aiscript) {
2020-04-12 21:23:23 +03:00
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 {
2020-04-13 17:46:53 +03:00
setTimeout(() => {
this.eval();
cb();
}, 1);
2020-04-12 21:23:23 +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);
}
}
public interpolate(str: string) {
if (str == null) return null;
return str.replace(/{(.+?)}/g, match => {
2019-05-01 08:54:34 +03:00
const v = this.vars[match.slice(1, -1).trim()];
return v == null ? 'NULL' : v.toString();
});
}
2020-04-12 21:23:23 +03:00
public callAiScript(fn: string) {
2020-04-13 17:46:53 +03:00
if (this.aoiScript.aiscript) this.aoiScript.aiscript.execFn(this.aoiScript.aiscript.scope.get(fn), []);
2020-04-12 21:23:23 +03:00
}
}
export default Vue.extend({
i18n,
components: {
XBlock
},
props: {
page: {
type: Object,
required: true
},
},
data() {
return {
script: null,
faHeartS, faHeart
};
},
created() {
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, {
randomSeed: Math.random(),
visitor: this.$store.state.i,
page: this.page,
2020-04-13 17:46:53 +03:00
url: url,
enableAiScript: !this.$store.state.device.disablePagesScript
}), e => {
console.dir(e);
2020-04-12 21:23:23 +03:00
}, () => {
this.script = s;
});
2020-04-12 21:23:23 +03:00
2020-04-13 17:46:53 +03:00
if (s.aoiScript.aiscript) s.aoiScript.aiscript.scope.opts.onUpdated = (name, value) => {
2020-04-12 21:23:23 +03:00
s.eval();
};
},
2020-04-13 17:46:53 +03:00
beforeDestroy() {
if (this.script.aoiScript.aiscript) this.script.aoiScript.aiscript.abort();
},
methods: {
getPageVars() {
return collectPageVars(this.page.content);
},
}
});
</script>
<style lang="scss" scoped>
.iroscrza {
&.serif {
> div {
font-family: serif;
}
}
&.center {
text-align: center;
}
}
</style>