|
| 1 | +// CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 | +// Distributed under an MIT license: https://codemirror.net/5/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 | + cm.on("swapDoc", onChange); |
| 18 | + CodeMirror.on(cm.getInputField(), "compositionupdate", cm.state.placeholderCompose = function() { onComposition(cm) }) |
| 19 | + onChange(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) |
| 25 | + clearPlaceholder(cm); |
| 26 | + var wrapper = cm.getWrapperElement(); |
| 27 | + wrapper.className = wrapper.className.replace(" CodeMirror-empty", ""); |
| 28 | + } |
| 29 | + |
| 30 | + if (val && !cm.hasFocus()) onBlur(cm); |
| 31 | + }); |
| 32 | + |
| 33 | + function clearPlaceholder(cm) { |
| 34 | + if (cm.state.placeholder) { |
| 35 | + cm.state.placeholder.parentNode.removeChild(cm.state.placeholder); |
| 36 | + cm.state.placeholder = null; |
| 37 | + } |
| 38 | + } |
| 39 | + function setPlaceholder(cm) { |
| 40 | + clearPlaceholder(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); |
| 49 | + } |
| 50 | + |
| 51 | + function onComposition(cm) { |
| 52 | + setTimeout(function() { |
| 53 | + var empty = false |
| 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) |
| 58 | + } |
| 59 | + if (empty) setPlaceholder(cm) |
| 60 | + else clearPlaceholder(cm) |
| 61 | + }, 20) |
| 62 | + } |
| 63 | + |
| 64 | + function onBlur(cm) { |
| 65 | + if (isEmpty(cm)) setPlaceholder(cm); |
| 66 | + } |
| 67 | + function onChange(cm) { |
| 68 | + var wrapper = cm.getWrapperElement(), empty = isEmpty(cm); |
| 69 | + wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : ""); |
| 70 | + |
| 71 | + if (empty) setPlaceholder(cm); |
| 72 | + else clearPlaceholder(cm); |
| 73 | + } |
| 74 | + |
| 75 | + function isEmpty(cm) { |
| 76 | + return (cm.lineCount() === 1) && (cm.getLine(0) === ""); |
| 77 | + } |
| 78 | +}); |
0 commit comments