88 </div >
99 <div
1010 v-if =" stampIdsModel.length !== 0"
11- ref =" stampListRef "
11+ ref =" containerRef "
1212 :class =" $style.stampList"
1313 >
1414 <!-- FIXME: スタンプの総数が多い時に重くなる -->
4343</template >
4444
4545<script lang="ts" setup>
46- import Sortable , { type SortableEvent } from ' sortablejs'
47- import { nextTick , onMounted , onUnmounted , ref , watch } from ' vue'
46+ import { nextTick , ref , watch } from ' vue'
4847import StampPaletteEditorLimitIndicator from ' ./StampPaletteEditorLimitIndicator.vue'
4948import { STAMP_PALETTE_STAMPS_LIMIT } from ' ./utils'
5049import AStamp from ' /@/components/UI/AStamp.vue'
5150import IconButton from ' /@/components/UI/IconButton.vue'
5251import type { StampId } from ' /@/types/entity-ids'
52+ import { useSortable } from ' /@/composables/dom/useSortable'
5353
5454const stampIdsModel = defineModel <StampId []>(' stamp-ids' , { required: true })
5555
@@ -70,41 +70,14 @@ const removeSelectedStamps = () => {
7070 selectedStampIds .value = []
7171}
7272
73- const stampListRef = ref <HTMLElement | null >(null )
74- let sortableInstance: Sortable | null = null
75-
76- const setupSortable = () => {
77- if (sortableInstance ) return
78- if (! stampListRef .value ) return
79- if (stampIdsModel .value .length === 0 ) return
80-
81- sortableInstance = Sortable .create (stampListRef .value , {
82- animation: 150 ,
83- draggable: ' .js-sortable-item' ,
84- onUpdate : (event : SortableEvent ) => {
85- if (
86- event .newDraggableIndex === undefined ||
87- event .oldDraggableIndex === undefined
88- )
89- return
90- const newStampIds = [... stampIdsModel .value ]
91- const movedStampId = newStampIds .splice (event .oldDraggableIndex , 1 )[0 ]
92- if (movedStampId === undefined ) return
93- newStampIds .splice (event .newDraggableIndex , 0 , movedStampId )
94- stampIdsModel .value = newStampIds
73+ const { containerRef, setupSortable, destroySortableInstance } = useSortable ({
74+ store: {
75+ set : sortable => {
76+ stampIdsModel .value = sortable .toArray ()
9577 }
96- })
97- }
98-
99- const destroySortableInstance = () => {
100- if (sortableInstance ) {
101- sortableInstance .destroy ()
102- sortableInstance = null
10378 }
104- }
79+ })
10580
106- onMounted (setupSortable )
107- onUnmounted (destroySortableInstance )
10881watch (
10982 () => stampIdsModel .value .length ,
11083 (newLength , oldLength ) => {
0 commit comments