foldgutter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'), require('./foldcode'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', './foldcode'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineOption('foldGutter', false, function (cm, val, old) {
  15. if (old && old != CodeMirror.Init) {
  16. cm.clearGutter(cm.state.foldGutter.options.gutter)
  17. cm.state.foldGutter = null
  18. cm.off('gutterClick', onGutterClick)
  19. cm.off('changes', onChange)
  20. cm.off('viewportChange', onViewportChange)
  21. cm.off('fold', onFold)
  22. cm.off('unfold', onFold)
  23. cm.off('swapDoc', onChange)
  24. }
  25. if (val) {
  26. cm.state.foldGutter = new State(parseOptions(val))
  27. updateInViewport(cm)
  28. cm.on('gutterClick', onGutterClick)
  29. cm.on('changes', onChange)
  30. cm.on('viewportChange', onViewportChange)
  31. cm.on('fold', onFold)
  32. cm.on('unfold', onFold)
  33. cm.on('swapDoc', onChange)
  34. }
  35. })
  36. var Pos = CodeMirror.Pos
  37. function State(options) {
  38. this.options = options
  39. this.from = this.to = 0
  40. }
  41. function parseOptions(opts) {
  42. if (opts === true) opts = {}
  43. if (opts.gutter == null) opts.gutter = 'CodeMirror-foldgutter'
  44. if (opts.indicatorOpen == null) opts.indicatorOpen = 'CodeMirror-foldgutter-open'
  45. if (opts.indicatorFolded == null) opts.indicatorFolded = 'CodeMirror-foldgutter-folded'
  46. return opts
  47. }
  48. function isFolded(cm, line) {
  49. var marks = cm.findMarks(Pos(line, 0), Pos(line + 1, 0))
  50. for (var i = 0; i < marks.length; ++i) {
  51. if (marks[i].__isFold) {
  52. var fromPos = marks[i].find(-1)
  53. if (fromPos && fromPos.line === line) return marks[i]
  54. }
  55. }
  56. }
  57. function marker(spec) {
  58. if (typeof spec == 'string') {
  59. var elt = document.createElement('div')
  60. elt.className = spec + ' CodeMirror-guttermarker-subtle'
  61. return elt
  62. } else {
  63. return spec.cloneNode(true)
  64. }
  65. }
  66. function updateFoldInfo(cm, from, to) {
  67. var opts = cm.state.foldGutter.options,
  68. cur = from - 1
  69. var minSize = cm.foldOption(opts, 'minFoldSize')
  70. var func = cm.foldOption(opts, 'rangeFinder')
  71. // we can reuse the built-in indicator element if its className matches the new state
  72. var clsFolded = typeof opts.indicatorFolded == 'string' && classTest(opts.indicatorFolded)
  73. var clsOpen = typeof opts.indicatorOpen == 'string' && classTest(opts.indicatorOpen)
  74. cm.eachLine(from, to, function (line) {
  75. ++cur
  76. var mark = null
  77. var old = line.gutterMarkers
  78. if (old) old = old[opts.gutter]
  79. if (isFolded(cm, cur)) {
  80. if (clsFolded && old && clsFolded.test(old.className)) return
  81. mark = marker(opts.indicatorFolded)
  82. } else {
  83. var pos = Pos(cur, 0)
  84. var range = func && func(cm, pos)
  85. if (range && range.to.line - range.from.line >= minSize) {
  86. if (clsOpen && old && clsOpen.test(old.className)) return
  87. mark = marker(opts.indicatorOpen)
  88. }
  89. }
  90. if (!mark && !old) return
  91. cm.setGutterMarker(line, opts.gutter, mark)
  92. })
  93. }
  94. // copied from CodeMirror/src/util/dom.js
  95. function classTest(cls) {
  96. return new RegExp('(^|\\s)' + cls + '(?:$|\\s)\\s*')
  97. }
  98. function updateInViewport(cm) {
  99. var vp = cm.getViewport(),
  100. state = cm.state.foldGutter
  101. if (!state) return
  102. cm.operation(function () {
  103. updateFoldInfo(cm, vp.from, vp.to)
  104. })
  105. state.from = vp.from
  106. state.to = vp.to
  107. }
  108. function onGutterClick(cm, line, gutter) {
  109. var state = cm.state.foldGutter
  110. if (!state) return
  111. var opts = state.options
  112. if (gutter != opts.gutter) return
  113. var folded = isFolded(cm, line)
  114. if (folded) folded.clear()
  115. else cm.foldCode(Pos(line, 0), opts)
  116. }
  117. function onChange(cm) {
  118. var state = cm.state.foldGutter
  119. if (!state) return
  120. var opts = state.options
  121. state.from = state.to = 0
  122. clearTimeout(state.changeUpdate)
  123. state.changeUpdate = setTimeout(function () {
  124. updateInViewport(cm)
  125. }, opts.foldOnChangeTimeSpan || 600)
  126. }
  127. function onViewportChange(cm) {
  128. var state = cm.state.foldGutter
  129. if (!state) return
  130. var opts = state.options
  131. clearTimeout(state.changeUpdate)
  132. state.changeUpdate = setTimeout(function () {
  133. var vp = cm.getViewport()
  134. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  135. updateInViewport(cm)
  136. } else {
  137. cm.operation(function () {
  138. if (vp.from < state.from) {
  139. updateFoldInfo(cm, vp.from, state.from)
  140. state.from = vp.from
  141. }
  142. if (vp.to > state.to) {
  143. updateFoldInfo(cm, state.to, vp.to)
  144. state.to = vp.to
  145. }
  146. })
  147. }
  148. }, opts.updateViewportTimeSpan || 400)
  149. }
  150. function onFold(cm, from) {
  151. var state = cm.state.foldGutter
  152. if (!state) return
  153. var line = from.line
  154. if (line >= state.from && line < state.to) updateFoldInfo(cm, line, line + 1)
  155. }
  156. })