5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
12
CodeMirror.defineOption("placeholder", "", function(cm, val, old) {
13
var prev = old && old != CodeMirror.Init;
15
cm.on("blur", onBlur);
16
cm.on("change", onChange);
17
cm.on("swapDoc", onChange);
18
CodeMirror.on(cm.getInputField(), "compositionupdate", cm.state.placeholderCompose = function() { onComposition(cm) })
20
} else if (!val && prev) {
21
cm.off("blur", onBlur);
22
cm.off("change", onChange);
23
cm.off("swapDoc", onChange);
24
CodeMirror.off(cm.getInputField(), "compositionupdate", cm.state.placeholderCompose)
26
var wrapper = cm.getWrapperElement();
27
wrapper.className = wrapper.className.replace(" CodeMirror-empty", "");
30
if (val && !cm.hasFocus()) onBlur(cm);
33
function clearPlaceholder(cm) {
34
if (cm.state.placeholder) {
35
cm.state.placeholder.parentNode.removeChild(cm.state.placeholder);
36
cm.state.placeholder = null;
39
function setPlaceholder(cm) {
41
var elt = cm.state.placeholder = document.createElement("pre");
42
elt.style.cssText = "height: 0; overflow: visible";
43
elt.style.direction = cm.getOption("direction");
44
elt.className = "CodeMirror-placeholder CodeMirror-line-like";
45
var placeHolder = cm.getOption("placeholder")
46
if (typeof placeHolder == "string") placeHolder = document.createTextNode(placeHolder)
47
elt.appendChild(placeHolder)
48
cm.display.lineSpace.insertBefore(elt, cm.display.lineSpace.firstChild);
51
function onComposition(cm) {
52
setTimeout(function() {
54
if (cm.lineCount() == 1) {
55
var input = cm.getInputField()
56
empty = input.nodeName == "TEXTAREA" ? !cm.getLine(0).length
57
: !/[^\u200b]/.test(input.querySelector(".CodeMirror-line").textContent)
59
if (empty) setPlaceholder(cm)
60
else clearPlaceholder(cm)
65
if (isEmpty(cm)) setPlaceholder(cm);
67
function onChange(cm) {
68
var wrapper = cm.getWrapperElement(), empty = isEmpty(cm);
69
wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : "");
71
if (empty) setPlaceholder(cm);
72
else clearPlaceholder(cm);
75
function isEmpty(cm) {
76
return (cm.lineCount() === 1) && (cm.getLine(0) === "");