rulers.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. CodeMirror.defineOption("rulers", false, function(cm, val) {
  13. if (cm.state.rulerDiv) {
  14. cm.state.rulerDiv.parentElement.removeChild(cm.state.rulerDiv)
  15. cm.state.rulerDiv = null
  16. cm.off("refresh", drawRulers)
  17. }
  18. if (val && val.length) {
  19. cm.state.rulerDiv = cm.display.lineSpace.parentElement.insertBefore(document.createElement("div"), cm.display.lineSpace)
  20. cm.state.rulerDiv.className = "CodeMirror-rulers"
  21. drawRulers(cm)
  22. cm.on("refresh", drawRulers)
  23. }
  24. });
  25. function drawRulers(cm) {
  26. cm.state.rulerDiv.textContent = ""
  27. var val = cm.getOption("rulers");
  28. var cw = cm.defaultCharWidth();
  29. var left = cm.charCoords(CodeMirror.Pos(cm.firstLine(), 0), "div").left;
  30. cm.state.rulerDiv.style.minHeight = (cm.display.scroller.offsetHeight + 30) + "px";
  31. for (var i = 0; i < val.length; i++) {
  32. var elt = document.createElement("div");
  33. elt.className = "CodeMirror-ruler";
  34. var col, conf = val[i];
  35. if (typeof conf == "number") {
  36. col = conf;
  37. } else {
  38. col = conf.column;
  39. if (conf.className) elt.className += " " + conf.className;
  40. if (conf.color) elt.style.borderColor = conf.color;
  41. if (conf.lineStyle) elt.style.borderLeftStyle = conf.lineStyle;
  42. if (conf.width) elt.style.borderLeftWidth = conf.width;
  43. }
  44. elt.style.left = (left + col * cw) + "px";
  45. cm.state.rulerDiv.appendChild(elt)
  46. }
  47. }
  48. });