-
Notifications
You must be signed in to change notification settings - Fork 24
Prevent unwanted sync in preview mode #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // @vitest-environment happy-dom | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
| import { ChangeSet, EditorSelection, Text } from '@codemirror/state'; | ||
| import { startObserving } from '../src/scroll'; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| doc: undefined as unknown as Text, | ||
| selection: undefined as unknown as EditorSelection, | ||
| syncScroll: true, | ||
| scrollToPosition: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('markedit-api', () => ({ | ||
| MarkEdit: { | ||
| editorView: { | ||
| lineBlockAtHeight: () => ({ from: 0 }), | ||
| state: { | ||
| get selection() { return mocks.selection; }, | ||
| get doc() { return mocks.doc; }, | ||
| }, | ||
| domAtPos: () => ({ node: document.createElement('div') }), | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../src/support/settings', () => ({ get syncScroll() { return mocks.syncScroll; } })); | ||
| vi.mock('../src/shared/utils', () => ({ | ||
| getClosestLine: () => null, | ||
| getBlockRange: vi.fn(), | ||
| getElementTop: vi.fn(), | ||
| scrollToElement: vi.fn(), | ||
| scrollToPosition: mocks.scrollToPosition, | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| mocks.doc = Text.of(['Example document']); | ||
| mocks.selection = EditorSelection.single(0); | ||
| mocks.syncScroll = true; | ||
| mocks.scrollToPosition.mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllTimers(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| function scroll(source: HTMLElement, top: number) { | ||
| source.scrollTop = top; | ||
| source.dispatchEvent(new Event('onscrollend' in window ? 'scrollend' : 'scroll')); | ||
| } | ||
|
|
||
| describe('Editor scroll synchronization', () => { | ||
| test('ignores hidden-editor scrolling in preview mode', () => { | ||
| const source = document.createElement('div'); | ||
| const target = document.createElement('div'); | ||
| target.classList.add('overlay'); | ||
|
|
||
| startObserving(source, target); | ||
| scroll(source, 400); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
|
|
||
| target.classList.remove('overlay'); | ||
| scroll(source, 500); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).toHaveBeenCalledWith(target, 0, true); | ||
| }); | ||
|
|
||
| test('continues synchronizing side-by-side scrolling', () => { | ||
| const source = document.createElement('div'); | ||
| const target = document.createElement('div'); | ||
| startObserving(source, target); | ||
| scroll(source, 400); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).toHaveBeenCalledWith(target, 0, true); | ||
| }); | ||
|
|
||
| test('syncs selection navigation in preview mode but ignores later corrections', () => { | ||
| const source = document.createElement('div'); | ||
| const target = document.createElement('div'); | ||
| target.classList.add('overlay'); | ||
| startObserving(source, target); | ||
|
|
||
| mocks.selection = EditorSelection.single(40); | ||
| scroll(source, 400); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).toHaveBeenCalledWith(target, 0, true); | ||
|
|
||
| mocks.scrollToPosition.mockClear(); | ||
| scroll(source, 450); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('ignores document-mapped selections but allows subsequent navigation', () => { | ||
| const source = document.createElement('div'); | ||
| const target = document.createElement('div'); | ||
| target.classList.add('overlay'); | ||
| mocks.selection = EditorSelection.single(8); | ||
| startObserving(source, target); | ||
|
|
||
| const changes = ChangeSet.of({ from: 0, insert: 'External ' }, mocks.doc.length); | ||
| mocks.selection = mocks.selection.map(changes); | ||
| mocks.doc = changes.apply(mocks.doc); | ||
| scroll(source, 400); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
|
|
||
| scroll(source, 450); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
|
|
||
| mocks.selection = EditorSelection.single(0); | ||
| scroll(source, 0); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).toHaveBeenCalledWith(target, 0, true); | ||
| }); | ||
|
|
||
| test('uses the reset selection as the baseline for a new observer', () => { | ||
| const source = document.createElement('div'); | ||
| const target = document.createElement('div'); | ||
| target.classList.add('overlay'); | ||
| mocks.selection = EditorSelection.single(40); | ||
| startObserving(source, target); | ||
|
|
||
| scroll(source, 400); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('ignores phantom scroll events', () => { | ||
| const source = document.createElement('div'); | ||
| startObserving(source, document.createElement('div')); | ||
| scroll(source, 0); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('cancels pending scroll synchronization when observation restarts', () => { | ||
| const source = document.createElement('div'); | ||
| const target = document.createElement('div'); | ||
| startObserving(source, target); | ||
| scroll(source, 400); | ||
|
|
||
| startObserving(document.createElement('div'), target); | ||
| mocks.scrollToPosition.mockClear(); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('respects disabled synchronization', () => { | ||
| mocks.syncScroll = false; | ||
| const source = document.createElement('div'); | ||
| startObserving(source, document.createElement('div')); | ||
|
|
||
| vi.runAllTimers(); | ||
| scroll(source, 400); | ||
| vi.runAllTimers(); | ||
| expect(mocks.scrollToPosition).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.