update_lines.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { heightAtLine } from "../line/spans.js"
  2. import { getLine, lineAtHeight, updateLineHeight } from "../line/utils_line.js"
  3. import { paddingTop, textHeight } from "../measurement/position_measurement.js"
  4. import { ie, ie_version } from "../util/browser.js"
  5. // Read the actual heights of the rendered lines, and update their
  6. // stored heights to match.
  7. export function updateHeightsInViewport(cm) {
  8. let display = cm.display
  9. let prevBottom = display.lineDiv.offsetTop
  10. for (let i = 0; i < display.view.length; i++) {
  11. let cur = display.view[i], height
  12. if (cur.hidden) continue
  13. if (ie && ie_version < 8) {
  14. let bot = cur.node.offsetTop + cur.node.offsetHeight
  15. height = bot - prevBottom
  16. prevBottom = bot
  17. } else {
  18. let box = cur.node.getBoundingClientRect()
  19. height = box.bottom - box.top
  20. }
  21. let diff = cur.line.height - height
  22. if (height < 2) height = textHeight(display)
  23. if (diff > .005 || diff < -.005) {
  24. updateLineHeight(cur.line, height)
  25. updateWidgetHeight(cur.line)
  26. if (cur.rest) for (let j = 0; j < cur.rest.length; j++)
  27. updateWidgetHeight(cur.rest[j])
  28. }
  29. }
  30. }
  31. // Read and store the height of line widgets associated with the
  32. // given line.
  33. function updateWidgetHeight(line) {
  34. if (line.widgets) for (let i = 0; i < line.widgets.length; ++i) {
  35. let w = line.widgets[i], parent = w.node.parentNode
  36. if (parent) w.height = parent.offsetHeight
  37. }
  38. }
  39. // Compute the lines that are visible in a given viewport (defaults
  40. // the the current scroll position). viewport may contain top,
  41. // height, and ensure (see op.scrollToPos) properties.
  42. export function visibleLines(display, doc, viewport) {
  43. let top = viewport && viewport.top != null ? Math.max(0, viewport.top) : display.scroller.scrollTop
  44. top = Math.floor(top - paddingTop(display))
  45. let bottom = viewport && viewport.bottom != null ? viewport.bottom : top + display.wrapper.clientHeight
  46. let from = lineAtHeight(doc, top), to = lineAtHeight(doc, bottom)
  47. // Ensure is a {from: {line, ch}, to: {line, ch}} object, and
  48. // forces those lines into the viewport (if possible).
  49. if (viewport && viewport.ensure) {
  50. let ensureFrom = viewport.ensure.from.line, ensureTo = viewport.ensure.to.line
  51. if (ensureFrom < from) {
  52. from = ensureFrom
  53. to = lineAtHeight(doc, heightAtLine(getLine(doc, ensureFrom)) + display.wrapper.clientHeight)
  54. } else if (Math.min(ensureTo, doc.lastLine()) >= to) {
  55. from = lineAtHeight(doc, heightAtLine(getLine(doc, ensureTo)) - display.wrapper.clientHeight)
  56. to = ensureTo
  57. }
  58. }
  59. return {from: from, to: Math.max(to, from + 1)}
  60. }