-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Make Link UI inactive if selection extends beyond format bounds or encounters new link #35946
Changes from all commits
0d428fd
b0bee95
121c39d
b3a9a38
9e442f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getActiveFormat, getActiveObject } from '@wordpress/rich-text'; | ||
import { | ||
getActiveFormat, | ||
getActiveObject, | ||
isCollapsed, | ||
} from '@wordpress/rich-text'; | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find } from 'lodash'; | ||
|
||
export default function FormatEdit( { | ||
formatTypes, | ||
|
@@ -18,11 +26,37 @@ export default function FormatEdit( { | |
} | ||
|
||
const activeFormat = getActiveFormat( value, name ); | ||
const isActive = activeFormat !== undefined; | ||
let isActive = activeFormat !== undefined; | ||
const activeObject = getActiveObject( value ); | ||
const isObjectActive = | ||
activeObject !== undefined && activeObject.type === name; | ||
|
||
// Edge case: un-collapsed link formats. | ||
// If there is a missing link format at either end of the selection | ||
// then we shouldn't show the Edit UI because the selection has exceeded | ||
// the bounds of the link format. | ||
// Also if the format objects don't match then we're dealing with two separate | ||
// links so we should not allow the link to be modified over the top. | ||
if ( name === 'core/link' && ! isCollapsed( value ) ) { | ||
const formats = value.formats; | ||
|
||
const linkFormatAtStart = find( formats[ value.start ], { | ||
type: 'core/link', | ||
} ); | ||
|
||
const linkFormatAtEnd = find( formats[ value.end - 1 ], { | ||
type: 'core/link', | ||
} ); | ||
|
||
if ( | ||
! linkFormatAtStart || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If no format here then we've exceeded the leftmost side of the link boundary. |
||
! linkFormatAtEnd || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If no format here then we've exceeded the rightmost side of the link boundary. |
||
linkFormatAtStart !== linkFormatAtEnd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If they do not display referential equality then we're dealing with two separate links. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that if the links are the same the UI shows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Screen.Capture.on.2021-10-26.at.14-09-46.movNope. Not in my testing. This is because the two link format objects are not referentially equal (i.e. they are not the same format object). Note in the video above the two items I'm |
||
) { | ||
isActive = false; | ||
} | ||
} | ||
|
||
return ( | ||
<Edit | ||
key={ name } | ||
|
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.
No need to handle collapsed selections because for these
start
andend
will be identical and thus there is no "selection".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.
Could we remove the hard coding of a format name here?
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.
Sure. Are you suggesting to replace with a reference instead? Or were you suggesting something else?
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 has this been merged while it's hardcoding link logic in the rich text component?
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.
Apologies. That must have been an oversight despite your comment here. Both Andrei and I must have missed it in the Github UI even though I responded 🤦
My question still stands though. How can we work around this?
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.
Not sure I understand the question 😅
In any case, I've made a PR here: #48789
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.
Neither do I nearly a year down the line 😄 I think it was "do you have any thoughts on how we could proceed"...and I'm not surprised by the fact that you did (do!).
Thank you for following up here.