-
Notifications
You must be signed in to change notification settings - Fork 4.7k
RichText: Remove focusPosition state #7651
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
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
67 changes: 67 additions & 0 deletions
67
editor/components/rich-text/format-toolbar/positioned-at-selection.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,67 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component } from '@wordpress/element'; | ||
| import { getOffsetParent, getRectangleFromRange } from '@wordpress/dom'; | ||
|
|
||
| /** | ||
| * Returns a style object for applying as `position: absolute` for an element | ||
| * relative to the bottom-center of the current selection. Includes `top` and | ||
| * `left` style properties. | ||
| * | ||
| * @return {Object} Style object. | ||
| */ | ||
| function getCurrentCaretPositionStyle() { | ||
| const selection = window.getSelection(); | ||
|
|
||
| // Unlikely, but in the case there is no selection, return empty styles so | ||
| // as to avoid a thrown error by `Selection#getRangeAt` on invalid index. | ||
| if ( selection.rangeCount === 0 ) { | ||
| return {}; | ||
| } | ||
|
|
||
| // Get position relative viewport. | ||
| const rect = getRectangleFromRange( selection.getRangeAt( 0 ) ); | ||
| let top = rect.top + rect.height; | ||
| let left = rect.left + ( rect.width / 2 ); | ||
|
|
||
| // Offset by positioned parent, if one exists. | ||
| const offsetParent = getOffsetParent( selection.anchorNode ); | ||
| if ( offsetParent ) { | ||
| const parentRect = offsetParent.getBoundingClientRect(); | ||
| top -= parentRect.top; | ||
| left -= parentRect.left; | ||
| } | ||
|
|
||
| return { top, left }; | ||
| } | ||
|
|
||
| /** | ||
| * Component which renders itself positioned under the current caret selection. | ||
| * The position is calculated at the time of the component being mounted, so it | ||
| * should only be mounted after the desired selection has been made. | ||
| * | ||
| * @type {WPComponent} | ||
| */ | ||
| class PositionedAtSelection extends Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
|
|
||
| this.state = { | ||
| style: getCurrentCaretPositionStyle(), | ||
| }; | ||
| } | ||
|
|
||
| render() { | ||
| const { className, children } = this.props; | ||
| const { style } = this.state; | ||
|
|
||
| return ( | ||
| <div className={ className } style={ style }> | ||
| { children } | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default PositionedAtSelection; |
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
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.
Why is this needing to be calculated at every node change event? Do we need to scroll every time on that event? Cc @mcsf.
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.
It almost certainly doesn't need to be. There's definitely room for improvement here, but I considered it outside the scope of the current task.
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.
Sounds good, I like the smaller PRs :)
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.
I'm sure there's room for improvement. When we looked in #5769, there was no proper way to detect when the insertion of a character fills the whole width of a text container and forces the caret into a "new line" (a different height in the same container).
'nodechange'is definitely too generic, but per the comment, onnodechangewe can work with the finalized TinyMCE instance and scroll to proper position.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.
Perhaps the same kind of reasoning we're seeing in this PR (let's manage this ourselves with our vdom abstraction so as to batch positioning writes and reads) can be applied to the caret-tracking scrolling feature.