summaryrefslogtreecommitdiff
path: root/public/js/lib/syncscroll.js
blob: d492fbc9266e497b59556bc457ee80a9cae3c757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* eslint-env browser, jquery */
/* global _ */
// Inject line numbers for sync scroll.

import markdownitContainer from 'markdown-it-container'

import { md } from '../extra'
import modeType from './modeType'
import appState from './appState'

function addPart (tokens, idx) {
  if (tokens[idx].map && tokens[idx].level === 0) {
    const startline = tokens[idx].map[0] + 1
    const endline = tokens[idx].map[1]
    tokens[idx].attrJoin('class', 'part')
    tokens[idx].attrJoin('data-startline', startline)
    tokens[idx].attrJoin('data-endline', endline)
  }
}

md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
  tokens[idx].attrJoin('class', 'raw')
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}
md.renderer.rules.table_open = function (tokens, idx, options, env, self) {
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}
md.renderer.rules.bullet_list_open = function (tokens, idx, options, env, self) {
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
  tokens[idx].attrJoin('class', 'raw')
  if (tokens[idx].map) {
    const startline = tokens[idx].map[0] + 1
    const endline = tokens[idx].map[1]
    tokens[idx].attrJoin('data-startline', startline)
    tokens[idx].attrJoin('data-endline', endline)
  }
  return self.renderToken(...arguments)
}
md.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) {
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}
md.renderer.rules.paragraph_open = function (tokens, idx, options, env, self) {
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
  tokens[idx].attrJoin('class', 'raw')
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
  const token = tokens[idx]
  const info = token.info ? md.utils.unescapeAll(token.info).trim() : ''
  let langName = ''
  let highlighted

  if (info) {
    langName = info.split(/\s+/g)[0]
    if (/!$/.test(info)) token.attrJoin('class', 'wrap')
    token.attrJoin('class', options.langPrefix + langName.replace(/=$|=\d+$|=\+$|!$|=!/, ''))
    token.attrJoin('class', 'hljs')
    token.attrJoin('class', 'raw')
  }

  if (options.highlight) {
    highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content)
  } else {
    highlighted = md.utils.escapeHtml(token.content)
  }

  if (highlighted.indexOf('<pre') === 0) {
    return `${highlighted}\n`
  }

  if (tokens[idx].map && tokens[idx].level === 0) {
    const startline = tokens[idx].map[0] + 1
    const endline = tokens[idx].map[1]
    return `<pre class="part" data-startline="${startline}" data-endline="${endline}"><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`
  }

  return `<pre><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`
}
md.renderer.rules.code_block = (tokens, idx, options, env, self) => {
  if (tokens[idx].map && tokens[idx].level === 0) {
    const startline = tokens[idx].map[0] + 1
    const endline = tokens[idx].map[1]
    return `<pre class="part" data-startline="${startline}" data-endline="${endline}"><code>${md.utils.escapeHtml(tokens[idx].content)}</code></pre>\n`
  }
  return `<pre><code>${md.utils.escapeHtml(tokens[idx].content)}</code></pre>\n`
}
function renderContainer (tokens, idx, options, env, self) {
  tokens[idx].attrJoin('role', 'alert')
  tokens[idx].attrJoin('class', 'alert')
  tokens[idx].attrJoin('class', `alert-${tokens[idx].info.trim()}`)
  addPart(tokens, idx)
  return self.renderToken(...arguments)
}

md.use(markdownitContainer, 'success', { render: renderContainer })
md.use(markdownitContainer, 'info', { render: renderContainer })
md.use(markdownitContainer, 'warning', { render: renderContainer })
md.use(markdownitContainer, 'danger', { render: renderContainer })

window.preventSyncScrollToEdit = false
window.preventSyncScrollToView = false

const editScrollThrottle = 5
const viewScrollThrottle = 5
const buildMapThrottle = 100

let viewScrolling = false
let editScrolling = false

let editArea = null
let viewArea = null
let markdownArea = null

let editor

export function setupSyncAreas (edit, view, markdown, _editor) {
  editArea = edit
  viewArea = view
  markdownArea = markdown

  editor = _editor

  editArea.on('scroll', _.throttle(syncScrollToView, editScrollThrottle))
  viewArea.on('scroll', _.throttle(syncScrollToEdit, viewScrollThrottle))
}

let scrollMap, lineHeightMap, viewTop, viewBottom

export function clearMap () {
  scrollMap = null
  lineHeightMap = null
  viewTop = null
  viewBottom = null
}
window.viewAjaxCallback = clearMap

const buildMap = _.throttle(buildMapInner, buildMapThrottle)

// Build offsets for each line (lines can be wrapped)
// That's a bit dirty to process each line everytime, but ok for demo.
// Optimizations are required only for big texts.
function buildMapInner (callback) {
  if (!viewArea || !markdownArea) return
  let i, offset, nonEmptyList, pos, a, b, _lineHeightMap, linesCount, acc, _scrollMap

  offset = viewArea.scrollTop() - viewArea.offset().top
  _scrollMap = []
  nonEmptyList = []
  _lineHeightMap = []
  viewTop = 0
  viewBottom = viewArea[0].scrollHeight - viewArea.height()

  acc = 0
  const lines = editor.getValue().split('\n')
  const lineHeight = editor.defaultTextHeight()
  for (i = 0; i < lines.length; i++) {
    const str = lines[i]

    _lineHeightMap.push(acc)

    if (str.length === 0) {
      acc++
      continue
    }

    const h = editor.heightAtLine(i + 1) - editor.heightAtLine(i)
    acc += Math.round(h / lineHeight)
  }
  _lineHeightMap.push(acc)
  linesCount = acc

  for (i = 0; i < linesCount; i++) {
    _scrollMap.push(-1)
  }

  nonEmptyList.push(0)
  // make the first line go top
  _scrollMap[0] = viewTop

  const parts = markdownArea.find('.part').toArray()
  for (i = 0; i < parts.length; i++) {
    const $el = $(parts[i])
    let t = $el.attr('data-startline') - 1
    if (t === '') {
      return
    }
    t = _lineHeightMap[t]
    if (t !== 0 && t !== nonEmptyList[nonEmptyList.length - 1]) {
      nonEmptyList.push(t)
    }
    _scrollMap[t] = Math.round($el.offset().top + offset - 10)
  }

  nonEmptyList.push(linesCount)
  _scrollMap[linesCount] = viewArea[0].scrollHeight

  pos = 0
  for (i = 1; i < linesCount; i++) {
    if (_scrollMap[i] !== -1) {
      pos++
      continue
    }

    a = nonEmptyList[pos]
    b = nonEmptyList[pos + 1]
    _scrollMap[i] = Math.round((_scrollMap[b] * (i - a) + _scrollMap[a] * (b - i)) / (b - a))
  }

  _scrollMap[0] = 0

  scrollMap = _scrollMap
  lineHeightMap = _lineHeightMap

  if (window.loaded && callback) callback()
}

// sync view scroll progress to edit
let viewScrollingTimer = null

export function syncScrollToEdit (event, preventAnimate) {
  if (appState.currentMode !== modeType.both || !appState.syncscroll || !editArea) return
  if (window.preventSyncScrollToEdit) {
    if (typeof window.preventSyncScrollToEdit === 'number') {
      window.preventSyncScrollToEdit--
    } else {
      window.preventSyncScrollToEdit = false
    }
    return
  }
  if (!scrollMap || !lineHeightMap) {
    buildMap(() => {
      syncScrollToEdit(event, preventAnimate)
    })
    return
  }
  if (editScrolling) return

  const scrollTop = viewArea[0].scrollTop
  let lineIndex = 0
  for (let i = 0, l = scrollMap.length; i < l; i++) {
    if (scrollMap[i] > scrollTop) {
      break
    } else {
      lineIndex = i
    }
  }
  let lineNo = 0
  let lineDiff = 0
  for (let i = 0, l = lineHeightMap.length; i < l; i++) {
    if (lineHeightMap[i] > lineIndex) {
      break
    } else {
      lineNo = lineHeightMap[i]
      lineDiff = lineHeightMap[i + 1] - lineNo
    }
  }

  let posTo = 0
  let topDiffPercent = 0
  let posToNextDiff = 0
  const scrollInfo = editor.getScrollInfo()
  const textHeight = editor.defaultTextHeight()
  const preLastLineHeight = scrollInfo.height - scrollInfo.clientHeight - textHeight
  const preLastLineNo = Math.round(preLastLineHeight / textHeight)
  const preLastLinePos = scrollMap[preLastLineNo]

  if (scrollInfo.height > scrollInfo.clientHeight && scrollTop >= preLastLinePos) {
    posTo = preLastLineHeight
    topDiffPercent = (scrollTop - preLastLinePos) / (viewBottom - preLastLinePos)
    posToNextDiff = textHeight * topDiffPercent
    posTo += Math.ceil(posToNextDiff)
  } else {
    posTo = lineNo * textHeight
    topDiffPercent = (scrollTop - scrollMap[lineNo]) / (scrollMap[lineNo + lineDiff] - scrollMap[lineNo])
    posToNextDiff = textHeight * lineDiff * topDiffPercent
    posTo += Math.ceil(posToNextDiff)
  }

  if (preventAnimate) {
    editArea.scrollTop(posTo)
  } else {
    const posDiff = Math.abs(scrollInfo.top - posTo)
    var duration = posDiff / 50
    duration = duration >= 100 ? duration : 100
    editArea.stop(true, true).animate({
      scrollTop: posTo
    }, duration, 'linear')
  }

  viewScrolling = true
  clearTimeout(viewScrollingTimer)
  viewScrollingTimer = setTimeout(viewScrollingTimeoutInner, duration * 1.5)
}

function viewScrollingTimeoutInner () {
  viewScrolling = false
}

// sync edit scroll progress to view
let editScrollingTimer = null

export function syncScrollToView (event, preventAnimate) {
  if (appState.currentMode !== modeType.both || !appState.syncscroll || !viewArea) return
  if (window.preventSyncScrollToView) {
    if (typeof preventSyncScrollToView === 'number') {
      window.preventSyncScrollToView--
    } else {
      window.preventSyncScrollToView = false
    }
    return
  }
  if (!scrollMap || !lineHeightMap) {
    buildMap(() => {
      syncScrollToView(event, preventAnimate)
    })
    return
  }
  if (viewScrolling) return

  let lineNo, posTo
  let topDiffPercent, posToNextDiff
  const scrollInfo = editor.getScrollInfo()
  const textHeight = editor.defaultTextHeight()
  lineNo = Math.floor(scrollInfo.top / textHeight)
  // if reach the last line, will start lerp to the bottom
  const diffToBottom = (scrollInfo.top + scrollInfo.clientHeight) - (scrollInfo.height - textHeight)
  if (scrollInfo.height > scrollInfo.clientHeight && diffToBottom > 0) {
    topDiffPercent = diffToBottom / textHeight
    posTo = scrollMap[lineNo + 1]
    posToNextDiff = (viewBottom - posTo) * topDiffPercent
    posTo += Math.floor(posToNextDiff)
  } else {
    topDiffPercent = (scrollInfo.top % textHeight) / textHeight
    posTo = scrollMap[lineNo]
    posToNextDiff = (scrollMap[lineNo + 1] - posTo) * topDiffPercent
    posTo += Math.floor(posToNextDiff)
  }

  if (preventAnimate) {
    viewArea.scrollTop(posTo)
  } else {
    const posDiff = Math.abs(viewArea.scrollTop() - posTo)
    var duration = posDiff / 50
    duration = duration >= 100 ? duration : 100
    viewArea.stop(true, true).animate({
      scrollTop: posTo
    }, duration, 'linear')
  }

  editScrolling = true
  clearTimeout(editScrollingTimer)
  editScrollingTimer = setTimeout(editScrollingTimeoutInner, duration * 1.5)
}

function editScrollingTimeoutInner () {
  editScrolling = false
}