-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Link Control: persist advanced settings toggle state to preferences if available #52799
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c6d0c70
Link Control: Create a preference for whether the advanced section is…
scruffian 958a6ad
move the useSelect to the component that uses it
scruffian 88b58cf
Supply preference setter via settings
getdave 69cd85b
Pass setter to Post Editor
getdave 512b14a
Provide local state fallbacks in absence of preference store settings
getdave 039a324
Conditionalise display of settings drawer to “edit” mode only
getdave 0ece1d6
Extract to constant to improve comprehension
getdave 9c233bd
Fix bug in preferences resolution
getdave ad70cd9
Improve comments
getdave 437920b
Add e2e scaffold
getdave be6babf
Fix e2e test to correctly assert on feature
getdave e1571a4
Remove focused test
getdave 933ddb7
Reinstate original logic to hide settings when not required
getdave 631ca0e
Scaffold documentation
getdave d3dee01
Revert providing prefs via settings
getdave cca7eb7
Refactor to use `core/block-editor` as preference scope
getdave 81bb3f2
Update docs
getdave d36cc9b
Reinstate remaining original conditional
getdave 38741d5
tentative fix for the e2e test
MaggieCabrera 9d94903
Try a different syntax for shiftAlt
scruffian c7f7cf7
another attempt to fix the e2e test
scruffian 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ import { useRef, useState, useEffect } from '@wordpress/element'; | |
| import { focus } from '@wordpress/dom'; | ||
| import { ENTER } from '@wordpress/keycodes'; | ||
| import { isShallowEqualObjects } from '@wordpress/is-shallow-equal'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { store as preferencesStore } from '@wordpress/preferences'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -101,6 +103,9 @@ import { DEFAULT_LINK_SETTINGS } from './constants'; | |
|
|
||
| const noop = () => {}; | ||
|
|
||
| const PREFERENCE_SCOPE = 'core/block-editor'; | ||
| const PREFERENCE_KEY = 'linkControlSettingsDrawer'; | ||
|
|
||
| /** | ||
| * Renders a link control. A link control is a controlled input which maintains | ||
| * a value associated with a link (HTML anchor element) and relevant settings | ||
|
|
@@ -133,15 +138,48 @@ function LinkControl( { | |
| withCreateSuggestion = true; | ||
| } | ||
|
|
||
| const [ settingsOpen, setSettingsOpen ] = useState( false ); | ||
|
Contributor
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. We need the local state as a fallback in case that the preference isn't supplied. I considered using a local state as the value passed as the setting but I realised that is also no good because the editor might not pass the settings at all so we cannot rely on their presence. |
||
|
|
||
| const { advancedSettingsPreference } = useSelect( ( select ) => { | ||
| const prefsStore = select( preferencesStore ); | ||
|
|
||
| return { | ||
| advancedSettingsPreference: | ||
| prefsStore.get( PREFERENCE_SCOPE, PREFERENCE_KEY ) ?? false, | ||
| }; | ||
| }, [] ); | ||
|
|
||
| const { set: setPreference } = useDispatch( preferencesStore ); | ||
|
|
||
| /** | ||
| * Sets the open/closed state of the Advanced Settings Drawer, | ||
| * optionlly persisting the state to the user's preferences. | ||
| * | ||
| * Note that Block Editor components can be consumed by non-WordPress | ||
| * environments which may not have preferences setup. | ||
| * Therefore a local state is also used as a fallback. | ||
| * | ||
| * @param {boolean} prefVal the open/closed state of the Advanced Settings Drawer. | ||
| */ | ||
| const setSettingsOpenWithPreference = ( prefVal ) => { | ||
| if ( setPreference ) { | ||
| setPreference( PREFERENCE_SCOPE, PREFERENCE_KEY, prefVal ); | ||
| } | ||
| setSettingsOpen( prefVal ); | ||
| }; | ||
|
|
||
| // Block Editor components can be consumed by non-WordPress environments | ||
| // which may not have these preferences setup. | ||
| // Therefore a local state is used as a fallback. | ||
| const isSettingsOpen = advancedSettingsPreference || settingsOpen; | ||
|
|
||
| const isMounting = useRef( true ); | ||
| const wrapperNode = useRef(); | ||
| const textInputRef = useRef(); | ||
| const isEndingEditWithFocus = useRef( false ); | ||
|
|
||
| const settingsKeys = settings.map( ( { id } ) => id ); | ||
|
|
||
| const [ settingsOpen, setSettingsOpen ] = useState( false ); | ||
|
|
||
| const [ | ||
| internalControlValue, | ||
| setInternalControlValue, | ||
|
|
@@ -207,7 +245,6 @@ function LinkControl( { | |
| wrapperNode.current.ownerDocument.activeElement | ||
| ); | ||
|
|
||
| setSettingsOpen( false ); | ||
| setIsEditingLink( false ); | ||
| }; | ||
|
|
||
|
|
@@ -292,7 +329,6 @@ function LinkControl( { | |
| const shownUnlinkControl = | ||
| onRemove && value && ! isEditingLink && ! isCreatingPage; | ||
|
|
||
| const showSettings = !! settings?.length && isEditingLink && hasLinkValue; | ||
| const showActions = isEditingLink && hasLinkValue; | ||
|
|
||
| // Only show text control once a URL value has been committed | ||
|
|
@@ -302,6 +338,7 @@ function LinkControl( { | |
|
|
||
| const isEditing = ( isEditingLink || ! value ) && ! isCreatingPage; | ||
| const isDisabled = ! valueHasChanges || currentInputIsEmpty; | ||
| const showSettings = !! settings?.length && isEditingLink && hasLinkValue; | ||
|
|
||
| return ( | ||
| <div | ||
|
|
@@ -385,8 +422,8 @@ function LinkControl( { | |
| <div className="block-editor-link-control__tools"> | ||
| { ! currentInputIsEmpty && ( | ||
| <LinkControlSettingsDrawer | ||
| settingsOpen={ settingsOpen } | ||
| setSettingsOpen={ setSettingsOpen } | ||
| settingsOpen={ isSettingsOpen } | ||
| setSettingsOpen={ setSettingsOpenWithPreference } | ||
| > | ||
| <LinkSettings | ||
| value={ internalControlValue } | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.