You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.8 KiB

2 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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. if (cm.state.markedSelection)
  34. cm.operation(function() { update(cm); });
  35. }
  36. function onChange(cm) {
  37. if (cm.state.markedSelection && cm.state.markedSelection.length)
  38. cm.operation(function() { clear(cm); });
  39. }
  40. var CHUNK_SIZE = 8;
  41. var Pos = CodeMirror.Pos;
  42. var cmp = CodeMirror.cmpPos;
  43. function coverRange(cm, from, to, addAt) {
  44. if (cmp(from, to) == 0) return;
  45. var array = cm.state.markedSelection;
  46. var cls = cm.state.markedSelectionStyle;
  47. for (var line = from.line;;) {
  48. var start = line == from.line ? from : Pos(line, 0);
  49. var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line;
  50. var end = atEnd ? to : Pos(endLine, 0);
  51. var mark = cm.markText(start, end, {className: cls});
  52. if (addAt == null) array.push(mark);
  53. else array.splice(addAt++, 0, mark);
  54. if (atEnd) break;
  55. line = endLine;
  56. }
  57. }
  58. function clear(cm) {
  59. var array = cm.state.markedSelection;
  60. for (var i = 0; i < array.length; ++i) array[i].clear();
  61. array.length = 0;
  62. }
  63. function reset(cm) {
  64. clear(cm);
  65. var ranges = cm.listSelections();
  66. for (var i = 0; i < ranges.length; i++)
  67. coverRange(cm, ranges[i].from(), ranges[i].to());
  68. }
  69. function update(cm) {
  70. if (!cm.somethingSelected()) return clear(cm);
  71. if (cm.listSelections().length > 1) return reset(cm);
  72. var from = cm.getCursor("start"), to = cm.getCursor("end");
  73. var array = cm.state.markedSelection;
  74. if (!array.length) return coverRange(cm, from, to);
  75. var coverStart = array[0].find(), coverEnd = array[array.length - 1].find();
  76. if (!coverStart || !coverEnd || to.line - from.line <= CHUNK_SIZE ||
  77. cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0)
  78. return reset(cm);
  79. while (cmp(from, coverStart.from) > 0) {
  80. array.shift().clear();
  81. coverStart = array[0].find();
  82. }
  83. if (cmp(from, coverStart.from) < 0) {
  84. if (coverStart.to.line - from.line < CHUNK_SIZE) {
  85. array.shift().clear();
  86. coverRange(cm, from, coverStart.to, 0);
  87. } else {
  88. coverRange(cm, from, coverStart.from, 0);
  89. }
  90. }
  91. while (cmp(to, coverEnd.to) < 0) {
  92. array.pop().clear();
  93. coverEnd = array[array.length - 1].find();
  94. }
  95. if (cmp(to, coverEnd.to) > 0) {
  96. if (to.line - coverEnd.from.line < CHUNK_SIZE) {
  97. array.pop().clear();
  98. coverRange(cm, coverEnd.from, to);
  99. } else {
  100. coverRange(cm, coverEnd.to, to);
  101. }
  102. }
  103. }
  104. });