mark-selection.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Because sometimes you need to mark the selected *text*.
  4. //
  5. // Adds an option 'styleSelectedText' which, when enabled, gives
  6. // selected text the CSS class given as option value, or
  7. // "CodeMirror-selectedtext" when the value is not a string.
  8. ;(function (mod) {
  9. if (typeof exports == 'object' && typeof module == 'object')
  10. // CommonJS
  11. mod(require('../../lib/codemirror'))
  12. else if (typeof define == 'function' && define.amd)
  13. // AMD
  14. define(['../../lib/codemirror'], mod)
  15. // Plain browser env
  16. else mod(CodeMirror)
  17. })(function (CodeMirror) {
  18. 'use strict'
  19. CodeMirror.defineOption('styleSelectedText', false, function (cm, val, old) {
  20. var prev = old && old != CodeMirror.Init
  21. if (val && !prev) {
  22. cm.state.markedSelection = []
  23. cm.state.markedSelectionStyle = typeof val == 'string' ? val : 'CodeMirror-selectedtext'
  24. reset(cm)
  25. cm.on('cursorActivity', onCursorActivity)
  26. cm.on('change', onChange)
  27. } else if (!val && prev) {
  28. cm.off('cursorActivity', onCursorActivity)
  29. cm.off('change', onChange)
  30. clear(cm)
  31. cm.state.markedSelection = cm.state.markedSelectionStyle = null
  32. }
  33. })
  34. function onCursorActivity(cm) {
  35. if (cm.state.markedSelection)
  36. cm.operation(function () {
  37. update(cm)
  38. })
  39. }
  40. function onChange(cm) {
  41. if (cm.state.markedSelection && cm.state.markedSelection.length)
  42. cm.operation(function () {
  43. clear(cm)
  44. })
  45. }
  46. var CHUNK_SIZE = 8
  47. var Pos = CodeMirror.Pos
  48. var cmp = CodeMirror.cmpPos
  49. function coverRange(cm, from, to, addAt) {
  50. if (cmp(from, to) == 0) return
  51. var array = cm.state.markedSelection
  52. var cls = cm.state.markedSelectionStyle
  53. for (var line = from.line; ; ) {
  54. var start = line == from.line ? from : Pos(line, 0)
  55. var endLine = line + CHUNK_SIZE,
  56. atEnd = endLine >= to.line
  57. var end = atEnd ? to : Pos(endLine, 0)
  58. var mark = cm.markText(start, end, { className: cls })
  59. if (addAt == null) array.push(mark)
  60. else array.splice(addAt++, 0, mark)
  61. if (atEnd) break
  62. line = endLine
  63. }
  64. }
  65. function clear(cm) {
  66. var array = cm.state.markedSelection
  67. for (var i = 0; i < array.length; ++i) array[i].clear()
  68. array.length = 0
  69. }
  70. function reset(cm) {
  71. clear(cm)
  72. var ranges = cm.listSelections()
  73. for (var i = 0; i < ranges.length; i++) coverRange(cm, ranges[i].from(), ranges[i].to())
  74. }
  75. function update(cm) {
  76. if (!cm.somethingSelected()) return clear(cm)
  77. if (cm.listSelections().length > 1) return reset(cm)
  78. var from = cm.getCursor('start'),
  79. to = cm.getCursor('end')
  80. var array = cm.state.markedSelection
  81. if (!array.length) return coverRange(cm, from, to)
  82. var coverStart = array[0].find(),
  83. coverEnd = array[array.length - 1].find()
  84. if (!coverStart || !coverEnd || to.line - from.line <= CHUNK_SIZE || cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0) return reset(cm)
  85. while (cmp(from, coverStart.from) > 0) {
  86. array.shift().clear()
  87. coverStart = array[0].find()
  88. }
  89. if (cmp(from, coverStart.from) < 0) {
  90. if (coverStart.to.line - from.line < CHUNK_SIZE) {
  91. array.shift().clear()
  92. coverRange(cm, from, coverStart.to, 0)
  93. } else {
  94. coverRange(cm, from, coverStart.from, 0)
  95. }
  96. }
  97. while (cmp(to, coverEnd.to) < 0) {
  98. array.pop().clear()
  99. coverEnd = array[array.length - 1].find()
  100. }
  101. if (cmp(to, coverEnd.to) > 0) {
  102. if (to.line - coverEnd.from.line < CHUNK_SIZE) {
  103. array.pop().clear()
  104. coverRange(cm, coverEnd.from, to)
  105. } else {
  106. coverRange(cm, coverEnd.to, to)
  107. }
  108. }
  109. }
  110. })