openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

update_lines.min.js (2653B)


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