Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor to use core/block-editor as preference scope
  • Loading branch information
getdave authored and scruffian committed Aug 1, 2023
commit cca7eb79b938af0cadc8607a81cc28654c03ed90
48 changes: 29 additions & 19 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +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 } from '@wordpress/data';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -107,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
Expand Down Expand Up @@ -141,27 +140,38 @@ function LinkControl( {

const [ settingsOpen, setSettingsOpen ] = useState( false );
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
linkControlAdvancedSettingsPreference,
setLinkControlAdvancedSettingsPreference,
} = useSelect( ( select ) => {
const prefSettings = select( blockEditorStore ).getSettings();
const { advancedSettingsPreference } = useSelect( ( select ) => {
const prefsStore = select( preferencesStore );

return {
linkControlAdvancedSettingsPreference:
prefSettings.linkControlAdvancedSettingsPreference,
setLinkControlAdvancedSettingsPreference:
prefSettings.setLinkControlAdvancedSettingsPreference,
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 =
linkControlAdvancedSettingsPreference || settingsOpen;
const setSettingsOpenWithPreference =
setLinkControlAdvancedSettingsPreference || setSettingsOpen;
const isSettingsOpen = advancedSettingsPreference || settingsOpen;

const isMounting = useRef( true );
const wrapperNode = useRef();
Expand Down