sharkey/packages/frontend/src/pages/page-editor/page-editor.blocks.vue

56 lines
1.6 KiB
Vue
Raw Normal View History

2019-05-02 17:55:59 +09:00
<template>
2022-12-24 11:57:06 +09:00
<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}">
2022-12-24 11:57:06 +09:00
<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>
2019-05-02 17:55:59 +09:00
</template>
<script lang="ts" setup>
import { defineAsyncComponent } from 'vue';
2019-05-02 17:55:59 +09: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 13:42:04 +09:00
import XNote from './els/page-editor.el.note.vue';
2019-05-02 17:55:59 +09:00
const Sortable = defineAsyncComponent(() => import('vuedraggable').then(x => x.default));
2019-05-02 17:55:59 +09:00
const props = defineProps<{
modelValue: any[];
}>();
2019-05-02 17:55:59 +09:00
const emit = defineEmits<{
(ev: 'update:modelValue', value: any[]): void;
}>();
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 17:55:59 +09: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 17:55:59 +09:00
</script>
2022-12-24 11:57:06 +09:00
<style lang="scss" module>
.item {
& + .item {
margin-top: 16px;
}
}
</style>