Skip to content

Commit f7398bb

Browse files
dgrahammuan
andcommitted
Base list indexes on original list order
Because nested lists are now included in the index, the original list order must be captured before list items are moved in the tree. Co-authored-by: Mu-An Chiou <[email protected]>
1 parent 58bf067 commit f7398bb

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/sortable.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @flow */
22

3-
type SortHandler = ({
3+
type SortStartHandler = (srcList: Element) => mixed
4+
type SortEndHandler = ({
45
src: {list: Element, index: number},
56
dst: {list: Element, index: number}
67
}) => mixed
@@ -21,8 +22,8 @@ export function isDragging(): boolean {
2122
return !!state
2223
}
2324

24-
export function sortable(el: Element, fn: SortHandler) {
25-
sortHandlers.set(el, fn)
25+
export function sortable(el: Element, sortStarted: SortStartHandler, sortFinished: SortEndHandler) {
26+
sortHandlers.set(el, {sortStarted, sortFinished})
2627
el.addEventListener('dragstart', onDragStart)
2728
el.addEventListener('dragenter', onDragEnter)
2829
el.addEventListener('dragend', onDragEnd)
@@ -67,6 +68,11 @@ function onDragStart(event: DragEvent) {
6768
const siblings = Array.from(target.parentElement.children)
6869
const sourceIndex = siblings.indexOf(target)
6970

71+
const handlers = sortHandlers.get(target)
72+
if (handlers) {
73+
handlers.sortStarted(sourceList)
74+
}
75+
7076
state = {
7177
didDrop: false,
7278
dragging: target,
@@ -136,8 +142,10 @@ function onDrop(event: DragEvent) {
136142

137143
const src = {list: state.sourceList, index: state.sourceIndex}
138144
const dst = {list: currentList, index: newIndex}
139-
const fn = sortHandlers.get(state.dragging)
140-
if (fn) fn({src, dst})
145+
const handlers = sortHandlers.get(state.dragging)
146+
if (handlers) {
147+
handlers.sortFinished({src, dst})
148+
}
141149
}
142150

143151
function onDragEnd() {

src/task-lists-element.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function initItem(el: HTMLElement) {
110110
handle.addEventListener('mouseenter', onHandleMouseOver)
111111
handle.addEventListener('mouseleave', onHandleMouseOut)
112112

113-
sortable(el, onSorted)
113+
sortable(el, onSortStart, onSorted)
114114

115115
// Drag operations don't remove :hover styles, so manage drag handle hover state.
116116
el.addEventListener('mouseenter', onListItemMouseOver)
@@ -190,16 +190,27 @@ function listIndex(list: Element): number {
190190
return Array.from(container.querySelectorAll('ol, ul')).indexOf(list)
191191
}
192192

193+
const originalLists = new WeakMap()
194+
195+
function onSortStart(srcList: Element) {
196+
const container = srcList.closest('task-lists')
197+
if (!container) throw new Error('parent not found')
198+
originalLists.set(container, Array.from(container.querySelectorAll('ol, ul')))
199+
}
200+
193201
function onSorted({src, dst}) {
194202
const container = src.list.closest('task-lists')
195203
if (!container) return
196204

205+
const lists = originalLists.get(container)
206+
if (!lists) return
207+
197208
container.dispatchEvent(
198209
new CustomEvent('task-lists:move', {
199210
bubbles: true,
200211
detail: {
201-
src: [listIndex(src.list), src.index],
202-
dst: [listIndex(dst.list), dst.index]
212+
src: [lists.indexOf(src.list), src.index],
213+
dst: [lists.indexOf(dst.list), dst.index]
203214
}
204215
})
205216
)

0 commit comments

Comments
 (0)