scrollpastend.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineOption('scrollPastEnd', false, function (cm, val, old) {
  15. if (old && old != CodeMirror.Init) {
  16. cm.off('change', onChange)
  17. cm.off('refresh', updateBottomMargin)
  18. cm.display.lineSpace.parentNode.style.paddingBottom = ''
  19. cm.state.scrollPastEndPadding = null
  20. }
  21. if (val) {
  22. cm.on('change', onChange)
  23. cm.on('refresh', updateBottomMargin)
  24. updateBottomMargin(cm)
  25. }
  26. })
  27. function onChange(cm, change) {
  28. if (CodeMirror.changeEnd(change).line == cm.lastLine()) updateBottomMargin(cm)
  29. }
  30. function updateBottomMargin(cm) {
  31. var padding = ''
  32. if (cm.lineCount() > 1) {
  33. var totalH = cm.display.scroller.clientHeight - 30,
  34. lastLineH = cm.getLineHandle(cm.lastLine()).height
  35. padding = totalH - lastLineH + 'px'
  36. }
  37. if (cm.state.scrollPastEndPadding != padding) {
  38. cm.state.scrollPastEndPadding = padding
  39. cm.display.lineSpace.parentNode.style.paddingBottom = padding
  40. cm.off('refresh', updateBottomMargin)
  41. cm.setSize()
  42. cm.on('refresh', updateBottomMargin)
  43. }
  44. }
  45. })