-
Notifications
You must be signed in to change notification settings - Fork 202
fix(super-editor): keep the context menu within the editor viewport and scroll area #3839
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
hitpopdimestop
wants to merge
5
commits into
superdoc:v1
Choose a base branch
from
hitpopdimestop:fix/context-menu-viewport-clamp
base: v1
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
5 commits
Select commit
Hold shift + click to select a range
2f3c04f
fix(super-editor): keep context menu within the editor scroll area
hitpopdimestop a5a4016
fix(super-editor): handle nested context menu clipping
caio-pizzol b50b45f
test(super-editor): cover context menu clamping
caio-pizzol 9feb20b
fix(super-editor): handle context menu edge cases
caio-pizzol b782a35
test(super-editor): cover search menu clamping
caio-pizzol 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
76 changes: 76 additions & 0 deletions
76
packages/super-editor/src/editors/v1/components/context-menu/menu-position.js
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,76 @@ | ||
| const CLIPPING_OVERFLOW = new Set(['auto', 'scroll', 'hidden', 'clip']); | ||
|
|
||
| /** | ||
| * Visible bounds (viewport coordinates) a fixed-position menu should stay within: the viewport | ||
| * minus any window scrollbar, intersected with every clipping ancestor's client box. | ||
| * | ||
| * @param {Element|null} anchorEl - Element inside the scroll area (e.g. the editor surface). | ||
| * @param {Window} view - Window used for measurements (injectable for tests). | ||
| * @returns {{ left: number, top: number, right: number, bottom: number }} | ||
| */ | ||
| export const resolveMenuBounds = (anchorEl, view) => { | ||
| const docEl = view.document.documentElement; | ||
| const bounds = { left: 0, top: 0, right: docEl.clientWidth, bottom: docEl.clientHeight }; | ||
|
|
||
| let current = anchorEl; | ||
| while (current) { | ||
| const { overflowX, overflowY } = view.getComputedStyle(current); | ||
| const clipsX = CLIPPING_OVERFLOW.has(overflowX); | ||
| const clipsY = CLIPPING_OVERFLOW.has(overflowY); | ||
|
|
||
| if ((clipsX || clipsY) && current.getBoundingClientRect) { | ||
| const rect = current.getBoundingClientRect(); | ||
| const clientLeft = rect.left + current.clientLeft; | ||
| const clientTop = rect.top + current.clientTop; | ||
|
|
||
| if (clipsX) { | ||
| bounds.left = Math.max(bounds.left, clientLeft); | ||
| bounds.right = Math.min(bounds.right, clientLeft + current.clientWidth); | ||
| } | ||
| if (clipsY) { | ||
| bounds.top = Math.max(bounds.top, clientTop); | ||
| bounds.bottom = Math.min(bounds.bottom, clientTop + current.clientHeight); | ||
| } | ||
| } | ||
|
|
||
| current = current.parentElement; | ||
| } | ||
|
|
||
| return bounds; | ||
| }; | ||
|
|
||
| /** | ||
| * Clamp a fixed-position menu back inside `bounds` using its rendered rect. Shifts by how far the | ||
| * rect overflows each edge, so the result is correct regardless of the menu's containing block. | ||
| * | ||
| * @param {{ left: string, top: string }} position - Current CSS position (px strings). | ||
| * @param {{ left: number, top: number, right: number, bottom: number }} rect - Rendered menu rect. | ||
| * @param {{ left: number, top: number, right: number, bottom: number }} bounds - Allowed area. | ||
| * @param {number} [gutter=8] - Minimum gap from each edge. | ||
| * @returns {{ left: string, top: string }} | ||
| */ | ||
| export const clampMenuPositionToBounds = (position, rect, bounds, gutter = 8) => { | ||
| let left = parseFloat(position.left) || 0; | ||
| let top = parseFloat(position.top) || 0; | ||
|
|
||
| const menuWidth = rect.right - rect.left; | ||
| const menuHeight = rect.bottom - rect.top; | ||
| const boundsWidth = bounds.right - bounds.left; | ||
| const boundsHeight = bounds.bottom - bounds.top; | ||
| const fitsX = menuWidth <= boundsWidth; | ||
| const fitsY = menuHeight <= boundsHeight; | ||
| const gutterX = Math.min(gutter, Math.max(0, (boundsWidth - menuWidth) / 2)); | ||
| const gutterY = Math.min(gutter, Math.max(0, (boundsHeight - menuHeight) / 2)); | ||
|
|
||
| if (fitsX) { | ||
| if (rect.right > bounds.right - gutterX) left -= rect.right - (bounds.right - gutterX); | ||
| else if (rect.left < bounds.left + gutterX) left += bounds.left + gutterX - rect.left; | ||
| } | ||
|
|
||
| if (fitsY) { | ||
| if (rect.bottom > bounds.bottom - gutterY) top -= rect.bottom - (bounds.bottom - gutterY); | ||
| else if (rect.top < bounds.top + gutterY) top += bounds.top + gutterY - rect.top; | ||
| } | ||
|
|
||
| return { left: `${left}px`, top: `${top}px` }; | ||
| }; |
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
Oops, something went wrong.
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.