aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/RawHtmlMount.vue
blob: ef9840598801720d8cf98fb7cd90a6ebfb867e1b (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
<template>
    <div ref="mount"></div>
</template>
<script setup>
import {nextTick, ref, watch} from "vue";
import {ready} from "@/assets/javascripts/lib/ready";

const props = defineProps({
    html: {
        type: String,
        required: true,
    },
});

const mount = ref(null);

let initedForVersion = -1;
let version = 0;

async function renderAndInit(currentVersion) {
    if (
        currentVersion !== version
        || !mount.value
    ) {
        return;
    }

    mount.value.innerHTML = props.html;

    if (initedForVersion === currentVersion) {
        return;
    }

    ready.trigger(mount.value);

    initedForVersion = currentVersion;
}

async function update() {
    version++;
    const current = version;

    await nextTick();

    mount.value.innerHTML = '';

    renderAndInit(current);
}

watch(() => props.html, update, {immediate: true});

</script>