aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/blocks/CoursewareCodeBlock.vue
blob: 465ad41fe719b1297cddfdd72a7a710570ea10a0 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
<template>
    <div class="cw-block cw-block-code">
        <courseware-default-block
            :block="block"
            :canEdit="canEdit"
            :isTeacher="isTeacher"
            :preview="true"
            @showEdit="initCurrentData"
            @storeEdit="storeBlock"
            @closeEdit="initCurrentData"
        >
            <template #content>
                <pre v-show="currentContent !== ''"><code v-html="highlightContent" class="hljs"></code></pre>
                <div v-show="currentLang !== ''" class="code-lang">
                    <span>{{ currentLang }}</span>
                </div>
            </template>
            <template v-if="canEdit" #edit>
                <form class="default" @submit.prevent="">
                    <label>
                        {{ $gettext('Sprache') }}
                        <input type="text" v-model="currentLang" />
                    </label>
                    <label>
                        {{ $gettext('Quelltext') }}
                        <textarea v-model="currentContent"></textarea>
                    </label>
                </form>
            </template>
            <template #info>
                <p>{{ $gettext('Informationen zum Quelltext-Block') }}</p>
            </template>
        </courseware-default-block>
    </div>
</template>

<script>
import BlockComponents from './block-components.js';
import blockMixin from '@/vue/mixins/courseware/block.js';
import hljs from 'highlight.js';

import { mapActions } from 'vuex';

export default {
    name: 'courseware-code-block',
    mixins: [blockMixin],
    components: Object.assign(BlockComponents, {}),
    props: {
        block: Object,
        canEdit: Boolean,
        isTeacher: Boolean,
    },
    data() {
        return {
            currentLang: '',
            currentContent: '',
        };
    },
    computed: {
        content() {
            return this.block?.attributes?.payload?.content;
        },
        lang() {
            return this.block?.attributes?.payload?.lang;
        },
        highlightContent() {
            let language = this.currentLang !== '' ? [this.currentLang] : null;
            return hljs.highlightAuto(this.currentContent, language).value;
        },
    },
    mounted() {
        this.initCurrentData();
    },
    methods: {
        ...mapActions({
            updateBlock: 'updateBlockInContainer',
        }),
        initCurrentData() {
            this.currentLang = this.lang;
            this.currentContent = this.content;
        },
        storeBlock() {
            let attributes = {};
            attributes.payload = {};
            attributes.payload.lang = this.currentLang;
            attributes.payload.content = this.currentContent;

            this.updateBlock({
                attributes: attributes,
                blockId: this.block.id,
                containerId: this.block.relationships.container.data.id,
            });
        },
    },
};
</script>
<style lang="scss">
@import '../../../../assets/stylesheets/scss/courseware/blocks/code';
</style>