-
Notifications
You must be signed in to change notification settings - Fork 64
fix: cursor drift during vertical arrow navigation (SD-1689) #1918
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
Open
luccas-harbour
wants to merge
21
commits into
main
Choose a base branch
from
luccas/sd-1689-fix-cursor-drift-during-vertical-arrow-navigation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4482642
feat: add plugin for handling vertical arrow navigation
4a19ac6
feat: keep track of X position during vertical navigation
58239dc
feat: compute next Y position after vertical navigation
7e92d8e
feat: add helper to convert back to client-space coordinates
158291c
fix: compute caret position based on closest entry
754fa3c
feat: expose caret layout computation method
266cf82
feat: use caret computation to determine current coordinates
4eb5562
feat: change selection after navigation
4b4b523
feat: add function for computing page Y offset
53f9287
fix: adjust coordinates denormalization to account for Y offset
240c2b1
docs: add documentation to vertical navigation plugin
529c0cd
test: add tests for the new plugin
d84a95a
fix: account for zomm during height computation
9cecef3
refactor: retrieve page element using existing helper
luccas-harbour f656d53
refactor: simplify getPageOffsetY to call existing helper
luccas-harbour 1b2d158
refactor: use lodash for binary search
luccas-harbour 5bc1189
chore: add lodash as a dependency
luccas-harbour e8cf464
refactor: reuse variable in adjacent line computation
luccas-harbour 16f2ee2
fix: selection computation
luccas-harbour d4fd979
fix: always use findEntryClosestToPosition when computing caret position
luccas-harbour 2c62bd0
fix: account for page offset when normalizing point
luccas-harbour 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
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
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
172 changes: 172 additions & 0 deletions
172
packages/super-editor/src/core/presentation-editor/dom/PointerNormalization.test.ts
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,172 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { denormalizeClientPoint, normalizeClientPoint } from './PointerNormalization.js'; | ||
|
|
||
| describe('PointerNormalization', () => { | ||
| const makeHosts = () => { | ||
| const viewportHost = document.createElement('div'); | ||
| const visibleHost = document.createElement('div'); | ||
| viewportHost.appendChild(visibleHost); | ||
|
|
||
| vi.spyOn(viewportHost, 'getBoundingClientRect').mockReturnValue({ | ||
| left: 20, | ||
| top: 10, | ||
| right: 0, | ||
| bottom: 0, | ||
| width: 0, | ||
| height: 0, | ||
| x: 0, | ||
| y: 0, | ||
| toJSON: () => ({}), | ||
| } as DOMRect); | ||
|
|
||
| (visibleHost as HTMLElement & { scrollLeft: number; scrollTop: number }).scrollLeft = 30; | ||
| (visibleHost as HTMLElement & { scrollLeft: number; scrollTop: number }).scrollTop = 40; | ||
|
|
||
| return { viewportHost, visibleHost }; | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| delete (document as Document & { elementsFromPoint?: Document['elementsFromPoint'] }).elementsFromPoint; | ||
| }); | ||
|
|
||
| describe('normalizeClientPoint', () => { | ||
| it('returns null for non-finite client coordinates', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 1, | ||
| getPageOffsetX: () => 0, | ||
| getPageOffsetY: () => 0, | ||
| }; | ||
|
|
||
| expect(normalizeClientPoint(options, NaN, 0)).toBe(null); | ||
| expect(normalizeClientPoint(options, 0, Infinity)).toBe(null); | ||
| expect(normalizeClientPoint(options, -Infinity, 0)).toBe(null); | ||
| }); | ||
|
|
||
| it('normalizes client coordinates to layout coordinates with zoom and scroll', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 2, | ||
| getPageOffsetX: () => 0, | ||
| getPageOffsetY: () => 0, | ||
| }; | ||
|
|
||
| const result = normalizeClientPoint(options, 200, 150); | ||
| expect(result).toEqual({ x: 105, y: 90 }); | ||
| }); | ||
|
|
||
| it('adjusts X when the pointer is over a page with a known offset', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
| const pageEl = document.createElement('div'); | ||
| pageEl.className = 'superdoc-page'; | ||
| pageEl.dataset.pageIndex = '2'; | ||
|
|
||
| (document as Document & { elementsFromPoint: Document['elementsFromPoint'] }).elementsFromPoint = vi | ||
| .fn() | ||
| .mockReturnValue([pageEl]); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 2, | ||
| getPageOffsetX: (pageIndex: number) => (pageIndex === 2 ? 12 : null), | ||
| getPageOffsetY: (pageIndex: number) => (pageIndex === 2 ? 8 : null), | ||
| }; | ||
|
|
||
| const result = normalizeClientPoint(options, 200, 150); | ||
| expect(result).toEqual({ x: 93, y: 82 }); | ||
| }); | ||
|
|
||
| it('does not adjust X when page offset is unavailable', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
| const pageEl = document.createElement('div'); | ||
| pageEl.className = 'superdoc-page'; | ||
| pageEl.dataset.pageIndex = '3'; | ||
|
|
||
| (document as Document & { elementsFromPoint: Document['elementsFromPoint'] }).elementsFromPoint = vi | ||
| .fn() | ||
| .mockReturnValue([pageEl]); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 2, | ||
| getPageOffsetX: () => null, | ||
| getPageOffsetY: () => null, | ||
| }; | ||
|
|
||
| const result = normalizeClientPoint(options, 200, 150); | ||
| expect(result).toEqual({ x: 105, y: 90 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('denormalizeClientPoint', () => { | ||
| it('returns null for non-finite layout coordinates', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 1, | ||
| getPageOffsetX: () => 0, | ||
| getPageOffsetY: () => 0, | ||
| }; | ||
|
|
||
| expect(denormalizeClientPoint(options, NaN, 0)).toBe(null); | ||
| expect(denormalizeClientPoint(options, 0, Infinity)).toBe(null); | ||
| }); | ||
|
|
||
| it('denormalizes layout coordinates to client coordinates', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 2, | ||
| getPageOffsetX: () => 0, | ||
| getPageOffsetY: () => 0, | ||
| }; | ||
|
|
||
| const result = denormalizeClientPoint(options, 50, 60); | ||
| expect(result).toEqual({ x: 90, y: 90 }); | ||
| }); | ||
|
|
||
| it('applies page offsets when a page index is provided', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 2, | ||
| getPageOffsetX: () => 15, | ||
| getPageOffsetY: () => 25, | ||
| }; | ||
|
|
||
| const result = denormalizeClientPoint(options, 50, 60, 3); | ||
| expect(result).toEqual({ x: 120, y: 140 }); | ||
| }); | ||
|
|
||
| it('scales height based on the zoom level when provided', () => { | ||
| const { viewportHost, visibleHost } = makeHosts(); | ||
|
|
||
| const options = { | ||
| viewportHost, | ||
| visibleHost, | ||
| zoom: 1.5, | ||
| getPageOffsetX: () => 0, | ||
| getPageOffsetY: () => 0, | ||
| }; | ||
|
|
||
| const result = denormalizeClientPoint(options, 10, 12, undefined, 8); | ||
| expect(result).toEqual({ x: 5, y: -12, height: 12 }); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to duplicate the contents of
getPageOffsets(). Why not use that method (or wrap it likegetPageOffsetX) instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! I should have double checked this one