active-line.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Because sometimes you need to style the cursor's line.
  2. //
  3. // Adds an option 'styleActiveLine' which, when enabled, gives the
  4. // active line's wrapping <div> the CSS class "CodeMirror-activeline",
  5. // and gives its background <div> the class "CodeMirror-activeline-background".
  6. (function() {
  7. "use strict";
  8. var WRAP_CLASS = "CodeMirror-activeline";
  9. var BACK_CLASS = "CodeMirror-activeline-background";
  10. CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
  11. var prev = old && old != CodeMirror.Init;
  12. if (val && !prev) {
  13. updateActiveLine(cm);
  14. cm.on("cursorActivity", updateActiveLine);
  15. } else if (!val && prev) {
  16. cm.off("cursorActivity", updateActiveLine);
  17. clearActiveLine(cm);
  18. delete cm._activeLine;
  19. }
  20. });
  21. function clearActiveLine(cm) {
  22. if ("_activeLine" in cm) {
  23. cm.removeLineClass(cm._activeLine, "wrap", WRAP_CLASS);
  24. cm.removeLineClass(cm._activeLine, "background", BACK_CLASS);
  25. }
  26. }
  27. function updateActiveLine(cm) {
  28. var line = cm.getLineHandle(cm.getCursor().line);
  29. if (cm._activeLine == line) return;
  30. clearActiveLine(cm);
  31. cm.addLineClass(line, "wrap", WRAP_CLASS);
  32. cm.addLineClass(line, "background", BACK_CLASS);
  33. cm._activeLine = line;
  34. }
  35. })();