MCPcopy Create free account
hub / github.com/hackmdio/codimd / syncScrollToEdit

Function syncScrollToEdit

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

Source from the content-addressed store, hash-verified

276let viewScrollingTimer = null
277
278export function syncScrollToEdit (event, preventAnimate) {
279 if (appState.currentMode !== modeType.both || !appState.syncscroll || !editArea) return
280 if (window.preventSyncScrollToEdit) {
281 if (typeof window.preventSyncScrollToEdit === 'number') {
282 window.preventSyncScrollToEdit--
283 } else {
284 window.preventSyncScrollToEdit = false
285 }
286 return
287 }
288 if (!scrollMap || !lineHeightMap) {
289 buildMap(() => {
290 syncScrollToEdit(event, preventAnimate)
291 })
292 return
293 }
294 if (editScrolling) return
295
296 const scrollTop = viewArea[0].scrollTop
297 let lineIndex = 0
298 for (let i = 0, l = scrollMap.length; i < l; i++) {
299 if (scrollMap[i] > scrollTop) {
300 break
301 } else {
302 lineIndex = i
303 }
304 }
305 let lineNo = 0
306 let lineDiff = 0
307 for (let i = 0, l = lineHeightMap.length; i < l; i++) {
308 if (lineHeightMap[i] > lineIndex) {
309 break
310 } else {
311 lineNo = lineHeightMap[i]
312 lineDiff = lineHeightMap[i + 1] - lineNo
313 }
314 }
315
316 let posTo = 0
317 let topDiffPercent = 0
318 let posToNextDiff = 0
319 const scrollInfo = editor.getScrollInfo()
320 const textHeight = editor.defaultTextHeight()
321 const preLastLineHeight = scrollInfo.height - scrollInfo.clientHeight - textHeight
322 const preLastLineNo = Math.round(preLastLineHeight / textHeight)
323 const preLastLinePos = scrollMap[preLastLineNo]
324
325 if (scrollInfo.height > scrollInfo.clientHeight && scrollTop >= preLastLinePos) {
326 posTo = preLastLineHeight
327 topDiffPercent = (scrollTop - preLastLinePos) / (viewBottom - preLastLinePos)
328 posToNextDiff = textHeight * topDiffPercent
329 posTo += Math.ceil(posToNextDiff)
330 } else {
331 posTo = lineNo * textHeight
332 topDiffPercent = (scrollTop - scrollMap[lineNo]) / (scrollMap[lineNo + lineDiff] - scrollMap[lineNo])
333 posToNextDiff = textHeight * lineDiff * topDiffPercent
334 posTo += Math.ceil(posToNextDiff)
335 }

Callers 3

autoSyncscrollFunction · 0.90
checkSyncToggleFunction · 0.90
changeModeFunction · 0.90

Calls 1

stopMethod · 0.45

Tested by

no test coverage detected