LaravelTest
78 строк · 2.8 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12CodeMirror.defineOption("placeholder", "", function(cm, val, old) {13var prev = old && old != CodeMirror.Init;14if (val && !prev) {15cm.on("blur", onBlur);16cm.on("change", onChange);17cm.on("swapDoc", onChange);18CodeMirror.on(cm.getInputField(), "compositionupdate", cm.state.placeholderCompose = function() { onComposition(cm) })19onChange(cm);20} else if (!val && prev) {21cm.off("blur", onBlur);22cm.off("change", onChange);23cm.off("swapDoc", onChange);24CodeMirror.off(cm.getInputField(), "compositionupdate", cm.state.placeholderCompose)25clearPlaceholder(cm);26var wrapper = cm.getWrapperElement();27wrapper.className = wrapper.className.replace(" CodeMirror-empty", "");28}29
30if (val && !cm.hasFocus()) onBlur(cm);31});32
33function clearPlaceholder(cm) {34if (cm.state.placeholder) {35cm.state.placeholder.parentNode.removeChild(cm.state.placeholder);36cm.state.placeholder = null;37}38}39function setPlaceholder(cm) {40clearPlaceholder(cm);41var elt = cm.state.placeholder = document.createElement("pre");42elt.style.cssText = "height: 0; overflow: visible";43elt.style.direction = cm.getOption("direction");44elt.className = "CodeMirror-placeholder CodeMirror-line-like";45var placeHolder = cm.getOption("placeholder")46if (typeof placeHolder == "string") placeHolder = document.createTextNode(placeHolder)47elt.appendChild(placeHolder)48cm.display.lineSpace.insertBefore(elt, cm.display.lineSpace.firstChild);49}50
51function onComposition(cm) {52setTimeout(function() {53var empty = false54if (cm.lineCount() == 1) {55var input = cm.getInputField()56empty = input.nodeName == "TEXTAREA" ? !cm.getLine(0).length57: !/[^\u200b]/.test(input.querySelector(".CodeMirror-line").textContent)58}59if (empty) setPlaceholder(cm)60else clearPlaceholder(cm)61}, 20)62}63
64function onBlur(cm) {65if (isEmpty(cm)) setPlaceholder(cm);66}67function onChange(cm) {68var wrapper = cm.getWrapperElement(), empty = isEmpty(cm);69wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : "");70
71if (empty) setPlaceholder(cm);72else clearPlaceholder(cm);73}74
75function isEmpty(cm) {76return (cm.lineCount() === 1) && (cm.getLine(0) === "");77}78});79