You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.8 KiB

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