Display.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { gecko, ie, ie_version, mobile, webkit } from "../util/browser.js"
  2. import { elt, eltP } from "../util/dom.js"
  3. import { scrollerGap } from "../util/misc.js"
  4. // The display handles the DOM integration, both for input reading
  5. // and content drawing. It holds references to DOM nodes and
  6. // display-related state.
  7. export function Display(place, doc, input) {
  8. let d = this
  9. this.input = input
  10. // Covers bottom-right square when both scrollbars are present.
  11. d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler")
  12. d.scrollbarFiller.setAttribute("cm-not-content", "true")
  13. // Covers bottom of gutter when coverGutterNextToScrollbar is on
  14. // and h scrollbar is present.
  15. d.gutterFiller = elt("div", null, "CodeMirror-gutter-filler")
  16. d.gutterFiller.setAttribute("cm-not-content", "true")
  17. // Will contain the actual code, positioned to cover the viewport.
  18. d.lineDiv = eltP("div", null, "CodeMirror-code")
  19. // Elements are added to these to represent selection and cursors.
  20. d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1")
  21. d.cursorDiv = elt("div", null, "CodeMirror-cursors")
  22. // A visibility: hidden element used to find the size of things.
  23. d.measure = elt("div", null, "CodeMirror-measure")
  24. // When lines outside of the viewport are measured, they are drawn in this.
  25. d.lineMeasure = elt("div", null, "CodeMirror-measure")
  26. // Wraps everything that needs to exist inside the vertically-padded coordinate system
  27. d.lineSpace = eltP("div", [d.measure, d.lineMeasure, d.selectionDiv, d.cursorDiv, d.lineDiv],
  28. null, "position: relative; outline: none")
  29. let lines = eltP("div", [d.lineSpace], "CodeMirror-lines")
  30. // Moved around its parent to cover visible view.
  31. d.mover = elt("div", [lines], null, "position: relative")
  32. // Set to the height of the document, allowing scrolling.
  33. d.sizer = elt("div", [d.mover], "CodeMirror-sizer")
  34. d.sizerWidth = null
  35. // Behavior of elts with overflow: auto and padding is
  36. // inconsistent across browsers. This is used to ensure the
  37. // scrollable area is big enough.
  38. d.heightForcer = elt("div", null, null, "position: absolute; height: " + scrollerGap + "px; width: 1px;")
  39. // Will contain the gutters, if any.
  40. d.gutters = elt("div", null, "CodeMirror-gutters")
  41. d.lineGutter = null
  42. // Actual scrollable element.
  43. d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll")
  44. d.scroller.setAttribute("tabIndex", "-1")
  45. // The element in which the editor lives.
  46. d.wrapper = elt("div", [d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror")
  47. // Work around IE7 z-index bug (not perfect, hence IE7 not really being supported)
  48. if (ie && ie_version < 8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0 }
  49. if (!webkit && !(gecko && mobile)) d.scroller.draggable = true
  50. if (place) {
  51. if (place.appendChild) place.appendChild(d.wrapper)
  52. else place(d.wrapper)
  53. }
  54. // Current rendered range (may be bigger than the view window).
  55. d.viewFrom = d.viewTo = doc.first
  56. d.reportedViewFrom = d.reportedViewTo = doc.first
  57. // Information about the rendered lines.
  58. d.view = []
  59. d.renderedView = null
  60. // Holds info about a single rendered line when it was rendered
  61. // for measurement, while not in view.
  62. d.externalMeasured = null
  63. // Empty space (in pixels) above the view
  64. d.viewOffset = 0
  65. d.lastWrapHeight = d.lastWrapWidth = 0
  66. d.updateLineNumbers = null
  67. d.nativeBarWidth = d.barHeight = d.barWidth = 0
  68. d.scrollbarsClipped = false
  69. // Used to only resize the line number gutter when necessary (when
  70. // the amount of lines crosses a boundary that makes its width change)
  71. d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null
  72. // Set to true when a non-horizontal-scrolling line widget is
  73. // added. As an optimization, line widget aligning is skipped when
  74. // this is false.
  75. d.alignWidgets = false
  76. d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null
  77. // Tracks the maximum line length so that the horizontal scrollbar
  78. // can be kept static when scrolling.
  79. d.maxLine = null
  80. d.maxLineLength = 0
  81. d.maxLineChanged = false
  82. // Used for measuring wheel scrolling granularity
  83. d.wheelDX = d.wheelDY = d.wheelStartX = d.wheelStartY = null
  84. // True when shift is held down.
  85. d.shift = false
  86. // Used to track whether anything happened since the context menu
  87. // was opened.
  88. d.selForContextMenu = null
  89. d.activeTouch = null
  90. input.init(d)
  91. }