|
| 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(typeofexports=="object"&&typeofmodule=="object")// CommonJS |
| 6 | +mod(require("../../lib/codemirror")); |
| 7 | +elseif(typeofdefine=="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 | +varprev=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 | +}elseif(!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 | +varwrapper=cm.getWrapperElement(); |
| 27 | +wrapper.className=wrapper.className.replace(" CodeMirror-empty",""); |
| 28 | +} |
| 29 | + |
| 30 | +if(val&&!cm.hasFocus())onBlur(cm); |
| 31 | +}); |
| 32 | + |
| 33 | +functionclearPlaceholder(cm){ |
| 34 | +if(cm.state.placeholder){ |
| 35 | +cm.state.placeholder.parentNode.removeChild(cm.state.placeholder); |
| 36 | +cm.state.placeholder=null; |
| 37 | +} |
| 38 | +} |
| 39 | +functionsetPlaceholder(cm){ |
| 40 | +clearPlaceholder(cm); |
| 41 | +varelt=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 | +varplaceHolder=cm.getOption("placeholder") |
| 46 | +if(typeofplaceHolder=="string")placeHolder=document.createTextNode(placeHolder) |
| 47 | +elt.appendChild(placeHolder) |
| 48 | +cm.display.lineSpace.insertBefore(elt,cm.display.lineSpace.firstChild); |
| 49 | +} |
| 50 | + |
| 51 | +functiononComposition(cm){ |
| 52 | +setTimeout(function(){ |
| 53 | +varempty=false |
| 54 | +if(cm.lineCount()==1){ |
| 55 | +varinput=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 | +elseclearPlaceholder(cm) |
| 61 | +},20) |
| 62 | +} |
| 63 | + |
| 64 | +functiononBlur(cm){ |
| 65 | +if(isEmpty(cm))setPlaceholder(cm); |
| 66 | +} |
| 67 | +functiononChange(cm){ |
| 68 | +varwrapper=cm.getWrapperElement(),empty=isEmpty(cm); |
| 69 | +wrapper.className=wrapper.className.replace(" CodeMirror-empty","")+(empty ?" CodeMirror-empty" :""); |
| 70 | + |
| 71 | +if(empty)setPlaceholder(cm); |
| 72 | +elseclearPlaceholder(cm); |
| 73 | +} |
| 74 | + |
| 75 | +functionisEmpty(cm){ |
| 76 | +return(cm.lineCount()===1)&&(cm.getLine(0)===""); |
| 77 | +} |
| 78 | +}); |