-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto scroll up/down when dragging to select multiple nodes
- Loading branch information
Showing
7 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import createDebug from 'debug' | ||
import { | ||
AUTO_SCROLL_INTERVAL, | ||
AUTO_SCROLL_SPEED_FAST, | ||
AUTO_SCROLL_SPEED_NORMAL, | ||
AUTO_SCROLL_SPEED_SLOW | ||
} from '../../constants.js' | ||
|
||
const debug = createDebug('jsoneditor:AutoScrollHandler') | ||
|
||
export function createAutoScrollHandler(scrollableElement) { | ||
debug('createAutoScrollHandler', scrollableElement) | ||
|
||
let autoScrollSpeed // pixels per second | ||
let autoScrollTimer | ||
|
||
function calculateSpeed(diff) { | ||
return diff < 20 | ||
? AUTO_SCROLL_SPEED_SLOW | ||
: diff < 50 | ||
? AUTO_SCROLL_SPEED_NORMAL | ||
: AUTO_SCROLL_SPEED_FAST | ||
} | ||
|
||
function autoScrollCallback() { | ||
// debug('auto scroll...') | ||
const diff = autoScrollSpeed * (AUTO_SCROLL_INTERVAL / 1000) | ||
|
||
scrollableElement.scrollTop += diff | ||
} | ||
|
||
function startAutoScroll(speed) { | ||
if (!autoScrollTimer || speed !== autoScrollSpeed) { | ||
stopAutoScroll() | ||
|
||
debug('startAutoScroll', speed) | ||
autoScrollSpeed = speed | ||
autoScrollTimer = setInterval(autoScrollCallback, AUTO_SCROLL_INTERVAL) | ||
} | ||
} | ||
|
||
function stopAutoScroll() { | ||
if (autoScrollTimer) { | ||
debug('stopAutoScroll') | ||
|
||
clearInterval(autoScrollTimer) | ||
autoScrollTimer = undefined | ||
autoScrollSpeed = undefined | ||
} | ||
} | ||
|
||
function onDrag(event) { | ||
if (scrollableElement) { | ||
const y = event.clientY | ||
const { top, bottom } = scrollableElement.getBoundingClientRect() | ||
|
||
if (y < top) { | ||
const speed = calculateSpeed(top - y) | ||
startAutoScroll(-speed) | ||
} else if (y > bottom) { | ||
const speed = calculateSpeed(y - bottom) | ||
startAutoScroll(speed) | ||
} else { | ||
stopAutoScroll() | ||
} | ||
} | ||
} | ||
|
||
function onDragEnd() { | ||
stopAutoScroll() | ||
} | ||
|
||
return { | ||
onDrag, | ||
onDragEnd | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/lib/components/modes/treemode/contextmenu/ContextMenu.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters