-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Force remount LinkControl when moving between links within same richt…
…ext block (#34742) * Force remount LinkControl within Popovers * Use anchor ref as semi-unique value to force remount * Use the HTML string of the anchor onto which the linkcontrol is attached as the remount key * Use custom key based unique ID generator
- Loading branch information
1 parent
393c2b5
commit b5afbff
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,31 @@ | ||
// Weakly referenced map allows unused ids to be garbage collected. | ||
const weakMap = new WeakMap(); | ||
|
||
// Incrementing zero-based ID value | ||
let id = -1; | ||
|
||
const prefix = 'link-control-instance'; | ||
|
||
function getKey( _id ) { | ||
return `${ prefix }-${ _id }`; | ||
} | ||
|
||
/** | ||
* Builds a unique link control key for the given object reference. | ||
* | ||
* @param {Object} instance an unique object reference specific to this link control instance. | ||
* @return {string} the unique key to use for this link control. | ||
*/ | ||
function useLinkInstanceKey( instance ) { | ||
if ( weakMap.has( instance ) ) { | ||
return getKey( weakMap.get( instance ) ); | ||
} | ||
|
||
id += 1; | ||
|
||
weakMap.set( instance, id ); | ||
|
||
return getKey( id ); | ||
} | ||
|
||
export default useLinkInstanceKey; |