本站源代码
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.

73 lines
2.4KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var WRAP_CLASS = "CodeMirror-activeline";
  13. var BACK_CLASS = "CodeMirror-activeline-background";
  14. var GUTT_CLASS = "CodeMirror-activeline-gutter";
  15. CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
  16. var prev = old == CodeMirror.Init ? false : old;
  17. if (val == prev) return
  18. if (prev) {
  19. cm.off("beforeSelectionChange", selectionChange);
  20. clearActiveLines(cm);
  21. delete cm.state.activeLines;
  22. }
  23. if (val) {
  24. cm.state.activeLines = [];
  25. updateActiveLines(cm, cm.listSelections());
  26. cm.on("beforeSelectionChange", selectionChange);
  27. }
  28. });
  29. function clearActiveLines(cm) {
  30. for (var i = 0; i < cm.state.activeLines.length; i++) {
  31. cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
  32. cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
  33. cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS);
  34. }
  35. }
  36. function sameArray(a, b) {
  37. if (a.length != b.length) return false;
  38. for (var i = 0; i < a.length; i++)
  39. if (a[i] != b[i]) return false;
  40. return true;
  41. }
  42. function updateActiveLines(cm, ranges) {
  43. var active = [];
  44. for (var i = 0; i < ranges.length; i++) {
  45. var range = ranges[i];
  46. var option = cm.getOption("styleActiveLine");
  47. if (typeof option == "object" && option.nonEmpty ? range.anchor.line != range.head.line : !range.empty())
  48. continue
  49. var line = cm.getLineHandleVisualStart(range.head.line);
  50. if (active[active.length - 1] != line) active.push(line);
  51. }
  52. if (sameArray(cm.state.activeLines, active)) return;
  53. cm.operation(function() {
  54. clearActiveLines(cm);
  55. for (var i = 0; i < active.length; i++) {
  56. cm.addLineClass(active[i], "wrap", WRAP_CLASS);
  57. cm.addLineClass(active[i], "background", BACK_CLASS);
  58. cm.addLineClass(active[i], "gutter", GUTT_CLASS);
  59. }
  60. cm.state.activeLines = active;
  61. });
  62. }
  63. function selectionChange(cm, sel) {
  64. updateActiveLines(cm, sel.ranges);
  65. }
  66. });
上海开阖软件有限公司 沪ICP备12045867号-1