scrollpastend.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/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. "use strict";
  12. CodeMirror.defineOption("scrollPastEnd", false, function(cm, val, old) {
  13. if (old && old != CodeMirror.Init) {
  14. cm.off("change", onChange);
  15. cm.off("refresh", updateBottomMargin);
  16. cm.display.lineSpace.parentNode.style.paddingBottom = "";
  17. cm.state.scrollPastEndPadding = null;
  18. }
  19. if (val) {
  20. cm.on("change", onChange);
  21. cm.on("refresh", updateBottomMargin);
  22. updateBottomMargin(cm);
  23. }
  24. });
  25. function onChange(cm, change) {
  26. if (CodeMirror.changeEnd(change).line == cm.lastLine())
  27. updateBottomMargin(cm);
  28. }
  29. function updateBottomMargin(cm) {
  30. var padding = "";
  31. if (cm.lineCount() > 1) {
  32. var totalH = cm.display.scroller.clientHeight - 30,
  33. lastLineH = cm.getLineHandle(cm.lastLine()).height;
  34. padding = (totalH - lastLineH) + "px";
  35. }
  36. if (cm.state.scrollPastEndPadding != padding) {
  37. cm.state.scrollPastEndPadding = padding;
  38. cm.display.lineSpace.parentNode.style.paddingBottom = padding;
  39. cm.setSize();
  40. }
  41. }
  42. });