hedgedoc/public/vendor/codemirror/addon/display/rulers.js

52 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-05-04 03:53:29 -04:00
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
2016-07-30 00:25:24 -04:00
CodeMirror.defineOption("rulers", false, function(cm, val) {
if (cm.state.rulerDiv) {
cm.display.lineSpace.removeChild(cm.state.rulerDiv)
cm.state.rulerDiv = null
cm.off("refresh", drawRulers)
2015-05-04 03:53:29 -04:00
}
if (val && val.length) {
2016-07-30 00:25:24 -04:00
cm.state.rulerDiv = cm.display.lineSpace.insertBefore(document.createElement("div"), cm.display.cursorDiv)
cm.state.rulerDiv.className = "CodeMirror-rulers"
drawRulers(cm)
cm.on("refresh", drawRulers)
2015-05-04 03:53:29 -04:00
}
});
2016-07-30 00:25:24 -04:00
function drawRulers(cm) {
cm.state.rulerDiv.textContent = ""
2015-05-04 03:53:29 -04:00
var val = cm.getOption("rulers");
var cw = cm.defaultCharWidth();
var left = cm.charCoords(CodeMirror.Pos(cm.firstLine(), 0), "div").left;
2016-07-30 00:25:24 -04:00
cm.state.rulerDiv.style.minHeight = (cm.display.scroller.offsetHeight + 30) + "px";
2015-05-04 03:53:29 -04:00
for (var i = 0; i < val.length; i++) {
var elt = document.createElement("div");
elt.className = "CodeMirror-ruler";
2015-07-03 23:31:01 -04:00
var col, conf = val[i];
2015-05-04 03:53:29 -04:00
if (typeof conf == "number") {
col = conf;
} else {
col = conf.column;
if (conf.className) elt.className += " " + conf.className;
if (conf.color) elt.style.borderColor = conf.color;
if (conf.lineStyle) elt.style.borderLeftStyle = conf.lineStyle;
if (conf.width) elt.style.borderLeftWidth = conf.width;
}
elt.style.left = (left + col * cw) + "px";
2016-07-30 00:25:24 -04:00
cm.state.rulerDiv.appendChild(elt)
2015-05-04 03:53:29 -04:00
}
}
});