mark-selection.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(mod) {
  7. if (typeof exports == "object" && typeof module == "object") // CommonJS
  8. mod(require("../../lib/codemirror"));
  9. else if (typeof define == "function" && define.amd) // AMD
  10. define(["../../lib/codemirror"], mod);
  11. else // Plain browser env
  12. mod(CodeMirror);
  13. })(function(CodeMirror) {
  14. "use strict";
  15. CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) {
  16. var prev = old && old != CodeMirror.Init;
  17. if (val && !prev) {
  18. cm.state.markedSelection = [];
  19. cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror-selectedtext";
  20. reset(cm);
  21. cm.on("cursorActivity", onCursorActivity);
  22. cm.on("change", onChange);
  23. } else if (!val && prev) {
  24. cm.off("cursorActivity", onCursorActivity);
  25. cm.off("change", onChange);
  26. clear(cm);
  27. cm.state.markedSelection = cm.state.markedSelectionStyle = null;
  28. }
  29. });
  30. function onCursorActivity(cm) {
  31. cm.operation(function() { update(cm); });
  32. }
  33. function onChange(cm) {
  34. if (cm.state.markedSelection.length)
  35. cm.operation(function() { clear(cm); });
  36. }
  37. var CHUNK_SIZE = 8;
  38. var Pos = CodeMirror.Pos;
  39. var cmp = CodeMirror.cmpPos;
  40. function coverRange(cm, from, to, addAt) {
  41. if (cmp(from, to) == 0) return;
  42. var array = cm.state.markedSelection;
  43. var cls = cm.state.markedSelectionStyle;
  44. for (var line = from.line;;) {
  45. var start = line == from.line ? from : Pos(line, 0);
  46. var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line;
  47. var end = atEnd ? to : Pos(endLine, 0);
  48. var mark = cm.markText(start, end, {className: cls});
  49. if (addAt == null) array.push(mark);
  50. else array.splice(addAt++, 0, mark);
  51. if (atEnd) break;
  52. line = endLine;
  53. }
  54. }
  55. function clear(cm) {
  56. var array = cm.state.markedSelection;
  57. for (var i = 0; i < array.length; ++i) array[i].clear();
  58. array.length = 0;
  59. }
  60. function reset(cm) {
  61. clear(cm);
  62. var ranges = cm.listSelections();
  63. for (var i = 0; i < ranges.length; i++)
  64. coverRange(cm, ranges[i].from(), ranges[i].to());
  65. }
  66. function update(cm) {
  67. if (!cm.somethingSelected()) return clear(cm);
  68. if (cm.listSelections().length > 1) return reset(cm);
  69. var from = cm.getCursor("start"), to = cm.getCursor("end");
  70. var array = cm.state.markedSelection;
  71. if (!array.length) return coverRange(cm, from, to);
  72. var coverStart = array[0].find(), coverEnd = array[array.length - 1].find();
  73. if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE ||
  74. cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0)
  75. return reset(cm);
  76. while (cmp(from, coverStart.from) > 0) {
  77. array.shift().clear();
  78. coverStart = array[0].find();
  79. }
  80. if (cmp(from, coverStart.from) < 0) {
  81. if (coverStart.to.line - from.line < CHUNK_SIZE) {
  82. array.shift().clear();
  83. coverRange(cm, from, coverStart.to, 0);
  84. } else {
  85. coverRange(cm, from, coverStart.from, 0);
  86. }
  87. }
  88. while (cmp(to, coverEnd.to) < 0) {
  89. array.pop().clear();
  90. coverEnd = array[array.length - 1].find();
  91. }
  92. if (cmp(to, coverEnd.to) > 0) {
  93. if (to.line - coverEnd.from.line < CHUNK_SIZE) {
  94. array.pop().clear();
  95. coverRange(cm, coverEnd.from, to);
  96. } else {
  97. coverRange(cm, coverEnd.to, to);
  98. }
  99. }
  100. }
  101. });