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.

scrollpastend.js 1.5KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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.off("refresh", updateBottomMargin);
  40. cm.setSize();
  41. cm.on("refresh", updateBottomMargin);
  42. }
  43. }
  44. });