summaryrefslogtreecommitdiff
path: root/packages/frontend/src/pages/page-editor/page-editor.blocks.vue
blob: f99fcb202fd0e3e36a7fbb65655d6b2fc4dc0097 (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
<template>
<Sortable :model-value="modelValue" tag="div" item-key="id" handle=".drag-handle" :group="{ name: 'blocks' }" :animation="150" :swap-threshold="0.5" @update:model-value="v => $emit('update:modelValue', v)">
	<template #item="{element}">
		<div :class="$style.item">
			<!-- divが無いとエラーになる https://github.com/SortableJS/vue.draggable.next/issues/189 -->
			<component :is="'x-' + element.type" :model-value="element" @update:model-value="updateItem" @remove="() => removeItem(element)"/>
		</div>
	</template>
</Sortable>
</template>

<script lang="ts">
import { defineComponent, defineAsyncComponent } from 'vue';
import XSection from './els/page-editor.el.section.vue';
import XText from './els/page-editor.el.text.vue';
import XImage from './els/page-editor.el.image.vue';
import XNote from './els/page-editor.el.note.vue';
import * as os from '@/os';
import { deepClone } from '@/scripts/clone';

export default defineComponent({
	components: {
		Sortable: defineAsyncComponent(() => import('vuedraggable').then(x => x.default)),
		XSection, XText, XImage, XNote,
	},

	props: {
		modelValue: {
			type: Array,
			required: true,
		},
	},

	emits: ['update:modelValue'],

	methods: {
		updateItem(v) {
			const i = this.modelValue.findIndex(x => x.id === v.id);
			const newValue = [
				...this.modelValue.slice(0, i),
				v,
				...this.modelValue.slice(i + 1),
			];
			this.$emit('update:modelValue', newValue);
		},

		removeItem(el) {
			const i = this.modelValue.findIndex(x => x.id === el.id);
			const newValue = [
				...this.modelValue.slice(0, i),
				...this.modelValue.slice(i + 1),
			];
			this.$emit('update:modelValue', newValue);
		},
	},
});
</script>

<style lang="scss" module>
.item {
	& + .item {
		margin-top: 16px;
	}
}
</style>