Home Reference Source Repository

lib/adapters/stable-adapter.js

  1. 'use babel'
  2.  
  3. /**
  4. * @access private
  5. */
  6. export default class StableAdapter {
  7. constructor (textEditor) {
  8. this.textEditor = textEditor
  9. this.textEditorElement = atom.views.getView(this.textEditor)
  10. }
  11.  
  12. enableCache () { this.useCache = true }
  13.  
  14. clearCache () {
  15. this.useCache = false
  16. delete this.heightCache
  17. delete this.scrollTopCache
  18. delete this.scrollLeftCache
  19. delete this.maxScrollTopCache
  20. }
  21.  
  22. onDidChangeScrollTop (callback) {
  23. return this.textEditorElement.onDidChangeScrollTop(callback)
  24. }
  25.  
  26. onDidChangeScrollLeft (callback) {
  27. return this.textEditorElement.onDidChangeScrollLeft(callback)
  28. }
  29.  
  30. getHeight () {
  31. if (this.useCache) {
  32. if (!this.heightCache) {
  33. this.heightCache = this.textEditorElement.getHeight()
  34. }
  35. return this.heightCache
  36. }
  37. return this.textEditorElement.getHeight()
  38. }
  39.  
  40. getScrollTop () {
  41. if (this.useCache) {
  42. if (!this.scrollTopCache) {
  43. this.scrollTopCache = this.textEditorElement.getScrollTop()
  44. }
  45. return this.scrollTopCache
  46. }
  47. return this.textEditorElement.getScrollTop()
  48. }
  49.  
  50. setScrollTop (scrollTop) {
  51. this.textEditorElement.setScrollTop(scrollTop)
  52. }
  53.  
  54. getScrollLeft () {
  55. if (this.useCache) {
  56. if (!this.scrollLeftCache) {
  57. this.scrollLeftCache = this.textEditorElement.getScrollLeft()
  58. }
  59. return this.scrollLeftCache
  60. }
  61. return this.textEditorElement.getScrollLeft()
  62. }
  63.  
  64. getMaxScrollTop () {
  65. if (this.maxScrollTopCache != null && this.useCache) {
  66. return this.maxScrollTopCache
  67. }
  68.  
  69. let maxScrollTop = this.textEditorElement.getScrollHeight() - this.getHeight()
  70. let lineHeight = this.textEditor.getLineHeightInPixels()
  71.  
  72. if (this.scrollPastEnd) {
  73. maxScrollTop -= this.getHeight() - 3 * lineHeight
  74. }
  75.  
  76. if (this.useCache) {
  77. this.maxScrollTopCache = maxScrollTop
  78. }
  79.  
  80. return maxScrollTop
  81. }
  82. }