mark-selection.js 3.7 KB

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