comparison .cms/lib/codemirror/src/display/update_lines.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 import { heightAtLine } from "../line/spans.js"
2 import { getLine, lineAtHeight, updateLineHeight } from "../line/utils_line.js"
3 import { paddingTop, charWidth } 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 let viewTop = Math.max(0, display.scroller.getBoundingClientRect().top)
12 let oldHeight = display.lineDiv.getBoundingClientRect().top
13 let mustScroll = 0
14 for (let i = 0; i < display.view.length; i++) {
15 let cur = display.view[i], wrapping = cm.options.lineWrapping
16 let height, width = 0
17 if (cur.hidden) continue
18 oldHeight += cur.line.height
19 if (ie && ie_version < 8) {
20 let bot = cur.node.offsetTop + cur.node.offsetHeight
21 height = bot - prevBottom
22 prevBottom = bot
23 } else {
24 let box = cur.node.getBoundingClientRect()
25 height = box.bottom - box.top
26 // Check that lines don't extend past the right of the current
27 // editor width
28 if (!wrapping && cur.text.firstChild)
29 width = cur.text.firstChild.getBoundingClientRect().right - box.left - 1
30 }
31 let diff = cur.line.height - height
32 if (diff > .005 || diff < -.005) {
33 if (oldHeight < viewTop) mustScroll -= diff
34 updateLineHeight(cur.line, height)
35 updateWidgetHeight(cur.line)
36 if (cur.rest) for (let j = 0; j < cur.rest.length; j++)
37 updateWidgetHeight(cur.rest[j])
38 }
39 if (width > cm.display.sizerWidth) {
40 let chWidth = Math.ceil(width / charWidth(cm.display))
41 if (chWidth > cm.display.maxLineLength) {
42 cm.display.maxLineLength = chWidth
43 cm.display.maxLine = cur.line
44 cm.display.maxLineChanged = true
45 }
46 }
47 }
48 if (Math.abs(mustScroll) > 2) display.scroller.scrollTop += mustScroll
49 }
50
51 // Read and store the height of line widgets associated with the
52 // given line.
53 function updateWidgetHeight(line) {
54 if (line.widgets) for (let i = 0; i < line.widgets.length; ++i) {
55 let w = line.widgets[i], parent = w.node.parentNode
56 if (parent) w.height = parent.offsetHeight
57 }
58 }
59
60 // Compute the lines that are visible in a given viewport (defaults
61 // the current scroll position). viewport may contain top,
62 // height, and ensure (see op.scrollToPos) properties.
63 export function visibleLines(display, doc, viewport) {
64 let top = viewport && viewport.top != null ? Math.max(0, viewport.top) : display.scroller.scrollTop
65 top = Math.floor(top - paddingTop(display))
66 let bottom = viewport && viewport.bottom != null ? viewport.bottom : top + display.wrapper.clientHeight
67
68 let from = lineAtHeight(doc, top), to = lineAtHeight(doc, bottom)
69 // Ensure is a {from: {line, ch}, to: {line, ch}} object, and
70 // forces those lines into the viewport (if possible).
71 if (viewport && viewport.ensure) {
72 let ensureFrom = viewport.ensure.from.line, ensureTo = viewport.ensure.to.line
73 if (ensureFrom < from) {
74 from = ensureFrom
75 to = lineAtHeight(doc, heightAtLine(getLine(doc, ensureFrom)) + display.wrapper.clientHeight)
76 } else if (Math.min(ensureTo, doc.lastLine()) >= to) {
77 from = lineAtHeight(doc, heightAtLine(getLine(doc, ensureTo)) - display.wrapper.clientHeight)
78 to = ensureTo
79 }
80 }
81 return {from: from, to: Math.max(to, from + 1)}
82 }