rulers.min.js (1915B)
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: http://codemirror.net/LICENSE 3 4 (function(mod) { 5 if (typeof exports == "object" && typeof module == "object") // CommonJS 6 mod(require("../../lib/codemirror")); 7 else if (typeof define == "function" && define.amd) // AMD 8 define(["../../lib/codemirror"], mod); 9 else // Plain browser env 10 mod(CodeMirror); 11 })(function(CodeMirror) { 12 "use strict"; 13 14 CodeMirror.defineOption("rulers", false, function(cm, val) { 15 if (cm.state.rulerDiv) { 16 cm.state.rulerDiv.parentElement.removeChild(cm.state.rulerDiv) 17 cm.state.rulerDiv = null 18 cm.off("refresh", drawRulers) 19 } 20 if (val && val.length) { 21 cm.state.rulerDiv = cm.display.lineSpace.parentElement.insertBefore(document.createElement("div"), cm.display.lineSpace) 22 cm.state.rulerDiv.className = "CodeMirror-rulers" 23 drawRulers(cm) 24 cm.on("refresh", drawRulers) 25 } 26 }); 27 28 function drawRulers(cm) { 29 cm.state.rulerDiv.textContent = "" 30 var val = cm.getOption("rulers"); 31 var cw = cm.defaultCharWidth(); 32 var left = cm.charCoords(CodeMirror.Pos(cm.firstLine(), 0), "div").left; 33 cm.state.rulerDiv.style.minHeight = (cm.display.scroller.offsetHeight + 30) + "px"; 34 for (var i = 0; i < val.length; i++) { 35 var elt = document.createElement("div"); 36 elt.className = "CodeMirror-ruler"; 37 var col, conf = val[i]; 38 if (typeof conf == "number") { 39 col = conf; 40 } else { 41 col = conf.column; 42 if (conf.className) elt.className += " " + conf.className; 43 if (conf.color) elt.style.borderColor = conf.color; 44 if (conf.lineStyle) elt.style.borderLeftStyle = conf.lineStyle; 45 if (conf.width) elt.style.borderLeftWidth = conf.width; 46 } 47 elt.style.left = (left + col * cw) + "px"; 48 cm.state.rulerDiv.appendChild(elt) 49 } 50 } 51 });