-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
The menu no longer extends beyond the window #15401
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e828496
:lipstick: The menu no longer extends beyond the window
TCOTC b37f65b
:art: The menu no longer extends beyond the window
TCOTC ce37930
:art: The menu no longer extends beyond the window
TCOTC 1a0599d
:art: The menu no longer extends beyond the window
TCOTC ab24fbe
Merge branch 'dev' into fix/15400
TCOTC 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,39 @@ | ||
| import {Constants} from "../constants"; | ||
|
|
||
| export const setPosition = (element: HTMLElement, x: number, y: number, targetHeight = 0, targetLeft = 0) => { | ||
| element.style.top = y + "px"; | ||
| element.style.left = x + "px"; | ||
| export const setPosition = (element: HTMLElement, left: number, top: number, targetHeight = 0, targetLeft = 0) => { | ||
| const isTopValid = !isNaN(top); // 存在 top 时调整垂直方向位置 | ||
| const isLeftValid = !isNaN(left); // 存在 left 时调整水平方向位置 | ||
| if (isTopValid) { | ||
| element.style.top = top + "px"; | ||
| } | ||
| if (isLeftValid) { | ||
| element.style.left = left + "px"; | ||
| } | ||
| const rect = element.getBoundingClientRect(); | ||
| // 上下超出屏幕 | ||
| if (rect.bottom > window.innerHeight || rect.top < Constants.SIZE_TOOLBAR_HEIGHT) { | ||
| const top = y - rect.height - targetHeight; | ||
| if (top > Constants.SIZE_TOOLBAR_HEIGHT && (top + rect.height) < window.innerHeight) { | ||
| // 上部 | ||
| element.style.top = top + "px"; | ||
| } else if (top <= Constants.SIZE_TOOLBAR_HEIGHT) { | ||
| // 位置超越到屏幕上方外时,需移动到屏幕顶部。eg:光标在第一个块,然后滚动到上方看不见的位置,按 ctrl+a | ||
|
|
||
| if (isTopValid) { | ||
| if (rect.top < Constants.SIZE_TOOLBAR_HEIGHT) { | ||
| // 如果元素接触顶栏,向下移 | ||
| element.style.top = Constants.SIZE_TOOLBAR_HEIGHT + "px"; | ||
| } else { | ||
| // 依旧展现在下部,只是位置上移 | ||
| element.style.top = Math.max(Constants.SIZE_TOOLBAR_HEIGHT, window.innerHeight - rect.height) + "px"; | ||
| } else if (rect.bottom > window.innerHeight) { | ||
| // 如果元素底部超出窗口(下方空间不够),向上移 | ||
| if (top - Constants.SIZE_TOOLBAR_HEIGHT >= rect.height) { | ||
| // 如果上方空间足够,向上移 | ||
| element.style.top = (top - rect.height - targetHeight) + "px"; | ||
| } else { | ||
| // 如果上下空间都不够,向上移,但尽量靠底部 | ||
| element.style.top = Math.max(Constants.SIZE_TOOLBAR_HEIGHT, window.innerHeight - rect.height) + "px"; | ||
| } | ||
| } | ||
| } | ||
| if (rect.right > window.innerWidth) { | ||
| // 展现在左侧 | ||
| element.style.left = `${window.innerWidth - rect.width - targetLeft}px`; | ||
| } else if (rect.left < 0) { | ||
| // 依旧展现在左侧,只是位置右移 | ||
| element.style.left = "0"; | ||
|
|
||
| if (isLeftValid) { | ||
| if (rect.right > window.innerWidth) { | ||
| // 展现在左侧 | ||
| element.style.left = window.innerWidth - rect.width - targetLeft + "px"; | ||
| } else if (rect.left < 0) { | ||
| // 依旧展现在左侧,只是位置右移 | ||
| element.style.left = "0"; | ||
| } | ||
| } | ||
| }; |
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
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.
In
showSubMenu, the horizontal positioning logic readssubMenuRect(line 72) before setting anyleft/topstyles. The condition at line 81 (subMenuRect.right <= window.innerWidth) checks whether the submenu fits within the window at its current (stale) position, not at the intended placement ofitemRect.right + 8. This can cause incorrect positioning:leftyet (e.g., first show), its default fixed position might havesubMenuRect.rightwithin the window width, so the code takes theifbranch and setsleft = itemRect.right + 8. But the submenu atitemRect.right + 8 + subMenuRect.widthmight still overflow the right edge of the window.The correct check should compare the intended position against the viewport width:
itemRect.right + 8 + subMenuRect.width > window.innerWidth. This is what the old code did (set the position first, then read the rect).