MCPcopy Index your code
hub / github.com/hackmdio/codimd / syncScrollToView

Function syncScrollToView

public/js/lib/syncscroll.js:360–411  ·  view source on GitHub ↗
(event, preventAnimate)

Source from the content-addressed store, hash-verified

358let editScrollingTimer = null
359
360export function syncScrollToView (event, preventAnimate) {
361 if (appState.currentMode !== modeType.both || !appState.syncscroll || !viewArea) return
362 if (window.preventSyncScrollToView) {
363 if (typeof preventSyncScrollToView === 'number') {
364 window.preventSyncScrollToView--
365 } else {
366 window.preventSyncScrollToView = false
367 }
368 return
369 }
370 if (!scrollMap || !lineHeightMap) {
371 buildMap(() => {
372 syncScrollToView(event, preventAnimate)
373 })
374 return
375 }
376 if (viewScrolling) return
377
378 let posTo
379 let topDiffPercent, posToNextDiff
380 const scrollInfo = editor.getScrollInfo()
381 const textHeight = editor.defaultTextHeight()
382 const lineNo = Math.floor(scrollInfo.top / textHeight)
383 // if reach the last line, will start lerp to the bottom
384 const diffToBottom = (scrollInfo.top + scrollInfo.clientHeight) - (scrollInfo.height - textHeight)
385 if (scrollInfo.height > scrollInfo.clientHeight && diffToBottom > 0) {
386 topDiffPercent = diffToBottom / textHeight
387 posTo = scrollMap[lineNo + 1]
388 posToNextDiff = (viewBottom - posTo) * topDiffPercent
389 posTo += Math.floor(posToNextDiff)
390 } else {
391 topDiffPercent = (scrollInfo.top % textHeight) / textHeight
392 posTo = scrollMap[lineNo]
393 posToNextDiff = (scrollMap[lineNo + 1] - posTo) * topDiffPercent
394 posTo += Math.floor(posToNextDiff)
395 }
396
397 if (preventAnimate) {
398 viewArea.scrollTop(posTo)
399 } else {
400 const posDiff = Math.abs(viewArea.scrollTop() - posTo)
401 var duration = posDiff / 50
402 duration = duration >= 100 ? duration : 100
403 viewArea.stop(true, true).animate({
404 scrollTop: posTo
405 }, duration, 'linear')
406 }
407
408 editScrolling = true
409 clearTimeout(editScrollingTimer)
410 editScrollingTimer = setTimeout(editScrollingTimeoutInner, duration * 1.5)
411}
412
413function editScrollingTimeoutInner () {
414 editScrolling = false

Callers 4

autoSyncscrollFunction · 0.90
checkSyncToggleFunction · 0.90
changeModeFunction · 0.90
index.jsFile · 0.90

Calls 1

stopMethod · 0.45

Tested by

no test coverage detected