summaryrefslogtreecommitdiff
path: root/packages/client/src/components/page/page.vue
blob: a06776237201e0bfb12c00255fa513f6403f4876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<template>
<div v-if="hpml" class="iroscrza" :class="{ center: page.alignCenter, serif: page.font === 'serif' }">
	<XBlock v-for="child in page.content" :key="child.id" :block="child" :hpml="hpml" :h="2"/>
</div>
</template>

<script lang="ts">
import { defineComponent, onMounted, nextTick, onUnmounted, PropType } from 'vue';
import { parse } from '@syuilo/aiscript';
import XBlock from './page.block.vue';
import { Hpml } from '@/scripts/hpml/evaluator';
import { url } from '@/config';
import { $i } from '@/account';
import { defaultStore } from '@/store';

export default defineComponent({
	components: {
		XBlock
	},
	props: {
		page: {
			type: Object as PropType<Record<string, any>>,
			required: true
		},
	},
	setup(props, ctx) {

		const hpml = new Hpml(props.page, {
			randomSeed: Math.random(),
			visitor: $i,
			url: url,
			enableAiScript: !defaultStore.state.disablePagesScript
		});

		onMounted(() => {
			nextTick(() => {
				if (props.page.script && hpml.aiscript) {
					let ast;
					try {
						ast = parse(props.page.script);
					} catch (err) {
						console.error(err);
						/*os.alert({
							type: 'error',
							text: 'Syntax error :('
						});*/
						return;
					}
					hpml.aiscript.exec(ast).then(() => {
						hpml.eval();
					}).catch(err => {
						console.error(err);
						/*os.alert({
							type: 'error',
							text: err
						});*/
					});
				} else {
					hpml.eval();
				}
			});
			onUnmounted(() => {
				if (hpml.aiscript) hpml.aiscript.abort();
			});
		});

		return {
			hpml,
		};
	},
});
</script>

<style lang="scss" scoped>
.iroscrza {
	&.serif {
		> div {
			font-family: serif;
		}
	}

	&.center {
		text-align: center;
	}
}
</style>