import * as autoScroll from 'dom-autoscroller' ; import * as dragula from '../../node_modules/dragula/dragula' ; import BookmarkDestinationArg = chrome . bookmarks . BookmarkDestinationArg ; import { Utils } from "../common/Utils" ; export function elementIndex ( element ) { if ( ! ( element . parentNode instanceof Element ) ) { return - 1 ; } return < number > Array . from ( element . parentNode . childNodes ) . filter ( ( elem ) => elem . nodeType !== Node . TEXT_NODE ) . indexOf ( element ) ; } export function initDragDrop ( bookmaksElement , wrapper ) { let initialIndexOfDraggable = null ; const drake = dragula ( [ bookmaksElement ] , { isContainer : ( element ) => element . classList . contains ( 'sub' ) , moves : ( element ) => ! element . classList . contains ( 'nosort' ) , accepts : ( element , target ) => { if ( ( element . parentNode as Element ) . getAttribute ( 'id' ) === 'bookmarks' && elementIndex ( element ) === 0 ) { return false ; } return element . classList . contains ( 'folder' ) || target . classList . contains ( 'sub' ) ; } , revertOnSpill : true } ) . on ( 'drag' , ( element ) => { initialIndexOfDraggable = elementIndex ( element ) ; } ) . on ( 'drop' , ( element ) => { const index = < number > elementIndex ( element ) ; if ( - 1 === index ) { return ; } if ( null === initialIndexOfDraggable ) { return ; } const options = { index : index } ; if ( options . index > initialIndexOfDraggable ) { options . index ++ ; } if ( ( element . parentNode as Element ) . getAttribute ( 'id' ) === 'bookmarks' ) { options . index -- ; } else { ( options as BookmarkDestinationArg ) . parentId = Utils . getElementData ( ( element . parentNode as HTMLElement ) . parentNode as HTMLElement , 'itemId' ) ; } chrome . bookmarks . move ( Utils . getElementData ( element , 'itemId' ) , options ) ; } ) ; autoScroll ( [ wrapper ] , { margin : 20 , maxSpeed : 5 , scrollWhenOutside : true , autoScroll : function autoScrollCheck ( ) { return this . down && drake . dragging ; } } ) ; }