Skip to content

Commit 4b3fa34

Browse files
author
Antonio Scandurra
authored
Merge pull request atom#15580 from atom/as-update-scrollbars-on-detach-reattach
Flush scroll position to dummy scrollbar components on re-attach
2 parents e903237 + 806b652 commit 4b3fa34

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

spec/text-editor-component-spec.js

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -378,35 +378,50 @@ describe('TextEditorComponent', () => {
378378
expect(horizontalScrollbar.style.visibility).toBe('')
379379
})
380380

381-
it('updates the bottom/right of dummy scrollbars and client height/width measurements without forgetting the previous scroll top/left when scrollbar styles change', async () => {
382-
const {component, element, editor} = buildComponent({height: 100, width: 100})
383-
expect(getHorizontalScrollbarHeight(component)).toBeGreaterThan(10)
384-
expect(getVerticalScrollbarWidth(component)).toBeGreaterThan(10)
385-
setScrollTop(component, 20)
386-
setScrollLeft(component, 10)
387-
await component.getNextUpdatePromise()
388-
389-
const style = document.createElement('style')
390-
style.textContent = '::-webkit-scrollbar { height: 10px; width: 10px; }'
391-
jasmine.attachToDOM(style)
381+
describe('when scrollbar styles change or the editor element is detached and then reattached', () => {
382+
it('updates the bottom/right of dummy scrollbars and client height/width measurements', async () => {
383+
const {component, element, editor} = buildComponent({height: 100, width: 100})
384+
expect(getHorizontalScrollbarHeight(component)).toBeGreaterThan(10)
385+
expect(getVerticalScrollbarWidth(component)).toBeGreaterThan(10)
386+
setScrollTop(component, 20)
387+
setScrollLeft(component, 10)
388+
await component.getNextUpdatePromise()
392389

393-
TextEditor.didUpdateScrollbarStyles()
394-
await component.getNextUpdatePromise()
390+
// Updating scrollbar styles.
391+
const style = document.createElement('style')
392+
style.textContent = '::-webkit-scrollbar { height: 10px; width: 10px; }'
393+
jasmine.attachToDOM(style)
394+
TextEditor.didUpdateScrollbarStyles()
395+
await component.getNextUpdatePromise()
395396

396-
expect(getHorizontalScrollbarHeight(component)).toBe(10)
397-
expect(getVerticalScrollbarWidth(component)).toBe(10)
398-
expect(component.refs.horizontalScrollbar.element.style.right).toBe('10px')
399-
expect(component.refs.verticalScrollbar.element.style.bottom).toBe('10px')
400-
expect(component.refs.horizontalScrollbar.element.scrollLeft).toBe(10)
401-
expect(component.refs.verticalScrollbar.element.scrollTop).toBe(20)
402-
expect(component.getScrollContainerClientHeight()).toBe(100 - 10)
403-
expect(component.getScrollContainerClientWidth()).toBe(100 - component.getGutterContainerWidth() - 10)
397+
expect(getHorizontalScrollbarHeight(component)).toBe(10)
398+
expect(getVerticalScrollbarWidth(component)).toBe(10)
399+
expect(component.refs.horizontalScrollbar.element.style.right).toBe('10px')
400+
expect(component.refs.verticalScrollbar.element.style.bottom).toBe('10px')
401+
expect(component.refs.horizontalScrollbar.element.scrollLeft).toBe(10)
402+
expect(component.refs.verticalScrollbar.element.scrollTop).toBe(20)
403+
expect(component.getScrollContainerClientHeight()).toBe(100 - 10)
404+
expect(component.getScrollContainerClientWidth()).toBe(100 - component.getGutterContainerWidth() - 10)
405+
406+
// Detaching and re-attaching the editor element.
407+
element.remove()
408+
jasmine.attachToDOM(element)
404409

405-
// Ensure we don't throw an error trying to remeasure non-existent scrollbars for mini editors.
406-
await editor.update({mini: true})
407-
TextEditor.didUpdateScrollbarStyles()
408-
component.scheduleUpdate()
409-
await component.getNextUpdatePromise()
410+
expect(getHorizontalScrollbarHeight(component)).toBe(10)
411+
expect(getVerticalScrollbarWidth(component)).toBe(10)
412+
expect(component.refs.horizontalScrollbar.element.style.right).toBe('10px')
413+
expect(component.refs.verticalScrollbar.element.style.bottom).toBe('10px')
414+
expect(component.refs.horizontalScrollbar.element.scrollLeft).toBe(10)
415+
expect(component.refs.verticalScrollbar.element.scrollTop).toBe(20)
416+
expect(component.getScrollContainerClientHeight()).toBe(100 - 10)
417+
expect(component.getScrollContainerClientWidth()).toBe(100 - component.getGutterContainerWidth() - 10)
418+
419+
// Ensure we don't throw an error trying to remeasure non-existent scrollbars for mini editors.
420+
await editor.update({mini: true})
421+
TextEditor.didUpdateScrollbarStyles()
422+
component.scheduleUpdate()
423+
await component.getNextUpdatePromise()
424+
})
410425
})
411426

412427
it('renders cursors within the visible row range', async () => {
@@ -1142,7 +1157,7 @@ describe('TextEditorComponent', () => {
11421157
expect(component.getScrollTopRow()).toBe(4)
11431158
expect(component.getScrollTop()).toBe(Math.round(4 * component.getLineHeight()))
11441159

1145-
// Preserves the scrollTopRow when sdetached
1160+
// Preserves the scrollTopRow when detached
11461161
element.remove()
11471162
expect(component.getScrollTopRow()).toBe(4)
11481163
expect(component.getScrollTop()).toBe(Math.round(4 * component.getLineHeight()))

src/text-editor-component.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,9 @@ class TextEditorComponent {
14031403

14041404
if (this.isVisible()) {
14051405
this.didShow()
1406+
1407+
if (this.refs.verticalScrollbar) this.refs.verticalScrollbar.flushScrollPosition()
1408+
if (this.refs.horizontalScrollbar) this.refs.horizontalScrollbar.flushScrollPosition()
14061409
} else {
14071410
this.didHide()
14081411
}

0 commit comments

Comments
 (0)