| 5 |
lars |
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 |
CodeMirror.defineOption("placeholder", "", function(cm, val, old) {
|
|
|
13 |
var prev = old && old != CodeMirror.Init;
|
|
|
14 |
if (val && !prev) {
|
|
|
15 |
cm.on("blur", onBlur);
|
|
|
16 |
cm.on("change", onChange);
|
|
|
17 |
onChange(cm);
|
|
|
18 |
} else if (!val && prev) {
|
|
|
19 |
cm.off("blur", onBlur);
|
|
|
20 |
cm.off("change", onChange);
|
|
|
21 |
clearPlaceholder(cm);
|
|
|
22 |
var wrapper = cm.getWrapperElement();
|
|
|
23 |
wrapper.className = wrapper.className.replace(" CodeMirror-empty", "");
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
if (val && !cm.hasFocus()) onBlur(cm);
|
|
|
27 |
});
|
|
|
28 |
|
|
|
29 |
function clearPlaceholder(cm) {
|
|
|
30 |
if (cm.state.placeholder) {
|
|
|
31 |
cm.state.placeholder.parentNode.removeChild(cm.state.placeholder);
|
|
|
32 |
cm.state.placeholder = null;
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
function setPlaceholder(cm) {
|
|
|
36 |
clearPlaceholder(cm);
|
|
|
37 |
var elt = cm.state.placeholder = document.createElement("pre");
|
|
|
38 |
elt.style.cssText = "height: 0; overflow: visible";
|
|
|
39 |
elt.className = "CodeMirror-placeholder";
|
|
|
40 |
elt.appendChild(document.createTextNode(cm.getOption("placeholder")));
|
|
|
41 |
cm.display.lineSpace.insertBefore(elt, cm.display.lineSpace.firstChild);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
function onBlur(cm) {
|
|
|
45 |
if (isEmpty(cm)) setPlaceholder(cm);
|
|
|
46 |
}
|
|
|
47 |
function onChange(cm) {
|
|
|
48 |
var wrapper = cm.getWrapperElement(), empty = isEmpty(cm);
|
|
|
49 |
wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : "");
|
|
|
50 |
|
|
|
51 |
if (empty) setPlaceholder(cm);
|
|
|
52 |
else clearPlaceholder(cm);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
function isEmpty(cm) {
|
|
|
56 |
return (cm.lineCount() === 1) && (cm.getLine(0) === "");
|
|
|
57 |
}
|
|
|
58 |
});
|