Show More
codemirror_placeholder.js
63 lines
| 2.2 KiB
| application/javascript
|
JavascriptLexer
r1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others | |||
r4105 | // Distributed under an MIT license: https://codemirror.net/LICENSE | |||
r1 | ||||
(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) { | ||||
CodeMirror.defineOption("placeholder", "", function(cm, val, old) { | ||||
var prev = old && old != CodeMirror.Init; | ||||
if (val && !prev) { | ||||
cm.on("blur", onBlur); | ||||
cm.on("change", onChange); | ||||
r4105 | cm.on("swapDoc", onChange); | |||
r1 | onChange(cm); | |||
} else if (!val && prev) { | ||||
cm.off("blur", onBlur); | ||||
cm.off("change", onChange); | ||||
r4105 | cm.off("swapDoc", onChange); | |||
r1 | clearPlaceholder(cm); | |||
var wrapper = cm.getWrapperElement(); | ||||
wrapper.className = wrapper.className.replace(" CodeMirror-empty", ""); | ||||
} | ||||
if (val && !cm.hasFocus()) onBlur(cm); | ||||
}); | ||||
function clearPlaceholder(cm) { | ||||
if (cm.state.placeholder) { | ||||
cm.state.placeholder.parentNode.removeChild(cm.state.placeholder); | ||||
cm.state.placeholder = null; | ||||
} | ||||
} | ||||
function setPlaceholder(cm) { | ||||
clearPlaceholder(cm); | ||||
var elt = cm.state.placeholder = document.createElement("pre"); | ||||
elt.style.cssText = "height: 0; overflow: visible"; | ||||
r4105 | elt.style.direction = cm.getOption("direction"); | |||
elt.className = "CodeMirror-placeholder CodeMirror-line-like"; | ||||
r346 | var placeHolder = cm.getOption("placeholder") | |||
if (typeof placeHolder == "string") placeHolder = document.createTextNode(placeHolder) | ||||
elt.appendChild(placeHolder) | ||||
r1 | cm.display.lineSpace.insertBefore(elt, cm.display.lineSpace.firstChild); | |||
} | ||||
function onBlur(cm) { | ||||
if (isEmpty(cm)) setPlaceholder(cm); | ||||
} | ||||
function onChange(cm) { | ||||
var wrapper = cm.getWrapperElement(), empty = isEmpty(cm); | ||||
wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : ""); | ||||
if (empty) setPlaceholder(cm); | ||||
else clearPlaceholder(cm); | ||||
} | ||||
function isEmpty(cm) { | ||||
return (cm.lineCount() === 1) && (cm.getLine(0) === ""); | ||||
} | ||||
}); | ||||