Skip to content

Commit e0d2198

Browse files
authored
fix: take the dragged node depth into account when calculating the nesting levels (#91)
1 parent cca3187 commit e0d2198

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/main.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,20 @@ class NestedSort {
338338
let depth = 0
339339
const list = this.getSortableList()
340340

341+
let selfDepth = 0
342+
if (this.draggedNode) {
343+
// the dragged node might be a nested list contributing to the final nesting levels
344+
const depthUL = this.draggedNode.querySelectorAll('ul').length || 0
345+
const depthOL = this.draggedNode.querySelectorAll('ol').length || 0
346+
selfDepth = depthUL > depthOL ? depthUL : depthOL
347+
}
348+
341349
while (list !== el?.parentElement) {
342350
if (el?.parentElement instanceof this.listInterface) depth++
343351
el = el?.parentElement as HTMLElement
344352
}
345353

346-
return depth
354+
return depth + selfDepth
347355
}
348356

349357
nestingThresholdReached(el: HTMLElement, isPlaceHolderCheck = false): boolean {

test/main.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,26 @@ describe('NestedSort', () => {
11931193
expect(depth).toBe(id.toString().split('').length - 1)
11941194
})
11951195
})
1196+
1197+
it('should take the depth of the dragged node into account', () => {
1198+
const ns = initDataDrivenList({
1199+
data: [
1200+
{id: 1, text: '1'},
1201+
{id: 11, text: '1-1', parent: 1},
1202+
{id: 111, text: '1-1-1', parent: 11},
1203+
{id: 1111, text: '1-1-1-1', parent: 111},
1204+
{id: 2, text: '2'},
1205+
{id: 22, text: '2-2', parent: 2},
1206+
],
1207+
})
1208+
1209+
const node = document.querySelector('[data-id="1111"]') // has 3 levels of nesting
1210+
ns.draggedNode = document.querySelector('[data-id="2"]') // has 1 level of nesting since it includes the item with ID of 22
1211+
1212+
const depth = ns.getNodeDepth(node)
1213+
1214+
expect(depth).toBe(4)
1215+
})
11961216
})
11971217

11981218
describe('nestingThresholdReached method', () => {

0 commit comments

Comments
 (0)