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.

active-line.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 style the cursor's line.
  4. //
  5. // Adds an option 'styleActiveLine' which, when enabled, gives the
  6. // active line's wrapping <div> the CSS class "CodeMirror-activeline",
  7. // and gives its background <div> the class "CodeMirror-activeline-background".
  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. var WRAP_CLASS = "CodeMirror-activeline";
  18. var BACK_CLASS = "CodeMirror-activeline-background";
  19. var GUTT_CLASS = "CodeMirror-activeline-gutter";
  20. CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
  21. var prev = old && old != CodeMirror.Init;
  22. if (val && !prev) {
  23. cm.state.activeLines = [];
  24. updateActiveLines(cm, cm.listSelections());
  25. cm.on("beforeSelectionChange", selectionChange);
  26. } else if (!val && prev) {
  27. cm.off("beforeSelectionChange", selectionChange);
  28. clearActiveLines(cm);
  29. delete cm.state.activeLines;
  30. }
  31. });
  32. function clearActiveLines(cm) {
  33. for (var i = 0; i < cm.state.activeLines.length; i++) {
  34. cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
  35. cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
  36. cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS);
  37. }
  38. }
  39. function sameArray(a, b) {
  40. if (a.length != b.length) return false;
  41. for (var i = 0; i < a.length; i++)
  42. if (a[i] != b[i]) return false;
  43. return true;
  44. }
  45. function updateActiveLines(cm, ranges) {
  46. var active = [];
  47. for (var i = 0; i < ranges.length; i++) {
  48. var range = ranges[i];
  49. if (!range.empty()) continue;
  50. var line = cm.getLineHandleVisualStart(range.head.line);
  51. if (active[active.length - 1] != line) active.push(line);
  52. }
  53. if (sameArray(cm.state.activeLines, active)) return;
  54. cm.operation(function() {
  55. clearActiveLines(cm);
  56. for (var i = 0; i < active.length; i++) {
  57. cm.addLineClass(active[i], "wrap", WRAP_CLASS);
  58. cm.addLineClass(active[i], "background", BACK_CLASS);
  59. cm.addLineClass(active[i], "gutter", GUTT_CLASS);
  60. }
  61. cm.state.activeLines = active;
  62. });
  63. }
  64. function selectionChange(cm, sel) {
  65. updateActiveLines(cm, sel.ranges);
  66. }
  67. });