Sha256: 20d8eeb9b67e6d6a4962935020212b8b06f930da070f0e119b076b76b6fc7049

Contents?: true

Size: 1.63 KB

Versions: 7

Compression:

Stored size: 1.63 KB

Contents

(function() {
  CodeMirror.defineOption("placeholder", "", function(cm, val, old) {
    var prev = old && old != CodeMirror.Init;
    if (val && !prev) {
      cm.on("focus", onFocus);
      cm.on("blur", onBlur);
      cm.on("change", onChange);
      onChange(cm);
    } else if (!val && prev) {
      cm.off("focus", onFocus);
      cm.off("blur", onBlur);
      cm.off("change", onChange);
      clearPlaceholder(cm);
      var wrapper = cm.getWrapperElement();
      wrapper.className = wrapper.className.replace(" CodeMirror-empty", "");
    }

    if (val && !cm.hasFocus()) onBlur(cm);
  });

  function clearPlaceholder(cm) {
    if (cm._placeholder) {
      cm._placeholder.parentNode.removeChild(cm._placeholder);
      cm._placeholder = null;
    }
  }
  function setPlaceholder(cm) {
    clearPlaceholder(cm);
    var elt = cm._placeholder = document.createElement("pre");
    elt.style.cssText = "height: 0; overflow: visible";
    elt.className = "CodeMirror-placeholder";
    elt.appendChild(document.createTextNode(cm.getOption("placeholder")));
    cm.display.lineSpace.insertBefore(elt, cm.display.lineSpace.firstChild);
  }

  function onFocus(cm) {
    clearPlaceholder(cm);
  }
  function onBlur(cm) {
    if (isEmpty(cm)) setPlaceholder(cm);
  }
  function onChange(cm) {
    var wrapper = cm.getWrapperElement(), empty = isEmpty(cm);
    wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : "");

    if (cm.hasFocus()) return;
    if (empty) setPlaceholder(cm);
    else clearPlaceholder(cm);
  }

  function isEmpty(cm) {
    return (cm.lineCount() === 1) && (cm.getLine(0) === "");
  }
})();

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
mdbe-0.1.0 public/libs/codemirror/addon/display/placeholder.js
maglev-database-explorer-0.0.5 public/libs/codemirror/addon/display/placeholder.js
maglev-database-explorer-0.0.4 public/libs/codemirror/addon/display/placeholder.js
maglev-database-explorer-0.0.3 public/libs/codemirror/addon/display/placeholder.js
maglev-database-explorer-0.0.2 public/libs/codemirror/addon/display/placeholder.js
maglev-database-explorer-0.0.1 public/libs/codemirror/addon/display/placeholder.js
codemirror-rails-3.12 vendor/assets/javascripts/codemirror/addons/display/placeholder.js