2019-05-02 10:55:59 +02:00
|
|
|
<template>
|
2023-05-19 13:52:15 +02:00
|
|
|
<Sortable :modelValue="modelValue" tag="div" itemKey="id" handle=".drag-handle" :group="{ name: 'blocks' }" :animation="150" :swapThreshold="0.5" @update:modelValue="v => $emit('update:modelValue', v)">
|
2020-12-05 04:50:09 +01:00
|
|
|
<template #item="{element}">
|
2022-12-24 03:57:06 +01:00
|
|
|
<div :class="$style.item">
|
|
|
|
<!-- divが無いとエラーになる https://github.com/SortableJS/vue.draggable.next/issues/189 -->
|
2023-05-30 10:37:38 +02:00
|
|
|
<component :is="getComponent(element.type)" :modelValue="element" @update:modelValue="updateItem" @remove="() => removeItem(element)"/>
|
2022-12-24 03:57:06 +01:00
|
|
|
</div>
|
2020-12-05 04:50:09 +01:00
|
|
|
</template>
|
2022-12-21 03:04:49 +01:00
|
|
|
</Sortable>
|
2019-05-02 10:55:59 +02:00
|
|
|
</template>
|
|
|
|
|
2023-05-14 04:43:56 +02:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { defineAsyncComponent } from 'vue';
|
2019-05-02 10:55:59 +02:00
|
|
|
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';
|
2020-11-15 05:42:04 +01:00
|
|
|
import XNote from './els/page-editor.el.note.vue';
|
2019-05-02 10:55:59 +02:00
|
|
|
|
2023-05-30 10:37:38 +02:00
|
|
|
function getComponent(type: string) {
|
|
|
|
switch (type) {
|
|
|
|
case 'section': return XSection;
|
|
|
|
case 'text': return XText;
|
|
|
|
case 'image': return XImage;
|
|
|
|
case 'note': return XNote;
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 04:43:56 +02:00
|
|
|
const Sortable = defineAsyncComponent(() => import('vuedraggable').then(x => x.default));
|
2019-05-02 10:55:59 +02:00
|
|
|
|
2023-05-14 04:43:56 +02:00
|
|
|
const props = defineProps<{
|
|
|
|
modelValue: any[];
|
|
|
|
}>();
|
2019-05-02 10:55:59 +02:00
|
|
|
|
2023-05-14 04:43:56 +02:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: 'update:modelValue', value: any[]): void;
|
|
|
|
}>();
|
2020-12-05 04:50:09 +01:00
|
|
|
|
2023-05-14 04:43:56 +02:00
|
|
|
function updateItem(v) {
|
|
|
|
const i = props.modelValue.findIndex(x => x.id === v.id);
|
|
|
|
const newValue = [
|
|
|
|
...props.modelValue.slice(0, i),
|
|
|
|
v,
|
|
|
|
...props.modelValue.slice(i + 1),
|
|
|
|
];
|
|
|
|
emit('update:modelValue', newValue);
|
|
|
|
}
|
2019-05-02 10:55:59 +02:00
|
|
|
|
2023-05-14 04:43:56 +02:00
|
|
|
function removeItem(el) {
|
|
|
|
const i = props.modelValue.findIndex(x => x.id === el.id);
|
|
|
|
const newValue = [
|
|
|
|
...props.modelValue.slice(0, i),
|
|
|
|
...props.modelValue.slice(i + 1),
|
|
|
|
];
|
|
|
|
emit('update:modelValue', newValue);
|
|
|
|
}
|
2019-05-02 10:55:59 +02:00
|
|
|
</script>
|
2022-12-24 03:57:06 +01:00
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
.item {
|
|
|
|
& + .item {
|
|
|
|
margin-top: 16px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|