mark-selection.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Because sometimes you need to mark the selected *text*.
  2. //
  3. // Adds an option 'styleSelectedText' which, when enabled, gives
  4. // selected text the CSS class given as option value, or
  5. // "CodeMirror-selectedtext" when the value is not a string.
  6. (function() {
  7. "use strict";
  8. CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) {
  9. var prev = old && old != CodeMirror.Init;
  10. if (val && !prev) {
  11. cm.state.markedSelection = [];
  12. cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror-selectedtext";
  13. reset(cm);
  14. cm.on("cursorActivity", onCursorActivity);
  15. cm.on("change", onChange);
  16. } else if (!val && prev) {
  17. cm.off("cursorActivity", onCursorActivity);
  18. cm.off("change", onChange);
  19. clear(cm);
  20. cm.state.markedSelection = cm.state.markedSelectionStyle = null;
  21. }
  22. });
  23. function onCursorActivity(cm) {
  24. cm.operation(function() { update(cm); });
  25. }
  26. function onChange(cm) {
  27. if (cm.state.markedSelection.length)
  28. cm.operation(function() { clear(cm); });
  29. }
  30. var CHUNK_SIZE = 8;
  31. var Pos = CodeMirror.Pos;
  32. function cmp(pos1, pos2) {
  33. return pos1.line - pos2.line || pos1.ch - pos2.ch;
  34. }
  35. function coverRange(cm, from, to, addAt) {
  36. if (cmp(from, to) == 0) return;
  37. var array = cm.state.markedSelection;
  38. var cls = cm.state.markedSelectionStyle;
  39. for (var line = from.line;;) {
  40. var start = line == from.line ? from : Pos(line, 0);
  41. var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line;
  42. var end = atEnd ? to : Pos(endLine, 0);
  43. var mark = cm.markText(start, end, {className: cls});
  44. if (addAt == null) array.push(mark);
  45. else array.splice(addAt++, 0, mark);
  46. if (atEnd) break;
  47. line = endLine;
  48. }
  49. }
  50. function clear(cm) {
  51. var array = cm.state.markedSelection;
  52. for (var i = 0; i < array.length; ++i) array[i].clear();
  53. array.length = 0;
  54. }
  55. function reset(cm) {
  56. clear(cm);
  57. var from = cm.getCursor("start"), to = cm.getCursor("end");
  58. coverRange(cm, from, to);
  59. }
  60. function update(cm) {
  61. var from = cm.getCursor("start"), to = cm.getCursor("end");
  62. if (cmp(from, to) == 0) return clear(cm);
  63. var array = cm.state.markedSelection;
  64. if (!array.length) return coverRange(cm, from, to);
  65. var coverStart = array[0].find(), coverEnd = array[array.length - 1].find();
  66. if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE ||
  67. cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0)
  68. return reset(cm);
  69. while (cmp(from, coverStart.from) > 0) {
  70. array.shift().clear();
  71. coverStart = array[0].find();
  72. }
  73. if (cmp(from, coverStart.from) < 0) {
  74. if (coverStart.to.line - from.line < CHUNK_SIZE) {
  75. array.shift().clear();
  76. coverRange(cm, from, coverStart.to, 0);
  77. } else {
  78. coverRange(cm, from, coverStart.from, 0);
  79. }
  80. }
  81. while (cmp(to, coverEnd.to) < 0) {
  82. array.pop().clear();
  83. coverEnd = array[array.length - 1].find();
  84. }
  85. if (cmp(to, coverEnd.to) > 0) {
  86. if (to.line - coverEnd.from.line < CHUNK_SIZE) {
  87. array.pop().clear();
  88. coverRange(cm, coverEnd.from, to);
  89. } else {
  90. coverRange(cm, coverEnd.to, to);
  91. }
  92. }
  93. }
  94. })();