Skip to content

Commit a7c5191

Browse files
authored
Merge pull request #1 from github/nested
Fix nested task list position determination
2 parents a6c7a9b + 47e4fda commit a7c5191

File tree

6 files changed

+171
-67
lines changed

6 files changed

+171
-67
lines changed

examples/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@
3434
WALL-E
3535
</label>
3636
</li>
37+
<li class="task-list-item">
38+
<label>
39+
<input type="checkbox" class="task-list-item-checkbox">
40+
R2-D2
41+
</label>
42+
43+
<ul class="contains-task-list">
44+
<li class="task-list-item">
45+
<label>
46+
<input type="checkbox" class="task-list-item-checkbox">
47+
Baymax
48+
</label>
49+
</li>
50+
</ul>
51+
</li>
3752
<li class="task-list-item">
3853
<label>
3954
<input type="checkbox" class="task-list-item-checkbox">

package-lock.json

Lines changed: 79 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
"babel-preset-es2015-rollup": "^3.0.0",
2929
"babel-preset-flow": "^6.23.0",
3030
"chai": "^4.1.2",
31-
"eslint": "^5.4.0",
31+
"eslint": "^5.5.0",
3232
"eslint-plugin-github": "^1.2.1",
33-
"flow-bin": "^0.79.1",
33+
"flow-bin": "^0.80.0",
3434
"karma": "^3.0.0",
3535
"karma-chai": "^0.1.0",
3636
"karma-chrome-launcher": "^2.2.0",
3737
"karma-mocha": "^1.3.0",
3838
"karma-mocha-reporter": "^2.2.5",
3939
"mocha": "^5.2.0",
40-
"rollup": "^0.64.1",
40+
"rollup": "^0.65.2",
4141
"rollup-plugin-babel": "^3.0.7"
4242
}
4343
}

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: 20 additions & 11 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)
@@ -136,12 +136,10 @@ function onListItemMouseOut(event: MouseEvent) {
136136
}
137137

138138
// Returns the list item position as a (list index, item index) tuple.
139-
// Listen on top-level task lists because item indexing includes nested task lists.
140-
function position(el: Element): [number, number] {
141-
const list = rootTaskList(el)
139+
function position(checkbox: HTMLInputElement): [number, number] {
140+
const list = taskList(checkbox)
142141
if (!list) throw new Error('.contains-task-list not found')
143-
const flattened = Array.from(list.querySelectorAll('li'))
144-
const index = flattened.indexOf(el.closest('.task-list-item'))
142+
const index = Array.from(list.children).indexOf(checkbox.closest('.task-list-item'))
145143
return [listIndex(list), index]
146144
}
147145

@@ -187,22 +185,33 @@ function syncDisabled(list: TaskListsElement) {
187185
// lists in the container, not just task lists, are indexed to match the
188186
// server-side Markdown parser's indexing.
189187
function listIndex(list: Element): number {
190-
const container = list.parentElement
188+
const container = list.closest('task-lists')
191189
if (!container) throw new Error('parent not found')
192-
const top = Array.from(container.children).filter(el => el.nodeName === 'OL' || el.nodeName === 'UL')
193-
return top.indexOf(list)
190+
return Array.from(container.querySelectorAll('ol, ul')).indexOf(list)
191+
}
192+
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')))
194199
}
195200

196201
function onSorted({src, dst}) {
197202
const container = src.list.closest('task-lists')
198203
if (!container) return
199204

205+
const lists = originalLists.get(container)
206+
if (!lists) return
207+
originalLists.delete(container)
208+
200209
container.dispatchEvent(
201210
new CustomEvent('task-lists:move', {
202211
bubbles: true,
203212
detail: {
204-
src: [listIndex(src.list), src.index],
205-
dst: [listIndex(dst.list), dst.index]
213+
src: [lists.indexOf(src.list), src.index],
214+
dst: [lists.indexOf(dst.list), dst.index]
206215
}
207216
})
208217
)

0 commit comments

Comments
 (0)