-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Unify the DocumentTools component (#57214)
- Loading branch information
1 parent
06b5fa5
commit 4eb3b74
Showing
18 changed files
with
107 additions
and
407 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
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
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
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
183 changes: 30 additions & 153 deletions
183
packages/edit-site/src/components/header-edit-mode/document-tools/index.js
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 |
---|---|---|
@@ -1,194 +1,71 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useRef } from '@wordpress/element'; | ||
import { useViewportMatch } from '@wordpress/compose'; | ||
import { | ||
ToolSelector, | ||
NavigableToolbar, | ||
store as blockEditorStore, | ||
privateApis as blockEditorPrivateApis, | ||
} from '@wordpress/block-editor'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { _x, __ } from '@wordpress/i18n'; | ||
import { listView, plus, chevronUpDown } from '@wordpress/icons'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { chevronUpDown } from '@wordpress/icons'; | ||
import { Button, ToolbarItem } from '@wordpress/components'; | ||
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { | ||
store as editorStore, | ||
privateApis as editorPrivateApis, | ||
} from '@wordpress/editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import UndoButton from '../undo-redo/undo'; | ||
import RedoButton from '../undo-redo/redo'; | ||
import { store as editSiteStore } from '../../../store'; | ||
import { unlock } from '../../../lock-unlock'; | ||
|
||
const { useCanBlockToolbarBeFocused } = unlock( blockEditorPrivateApis ); | ||
|
||
const preventDefault = ( event ) => { | ||
event.preventDefault(); | ||
}; | ||
const { DocumentTools: EditorDocumentTools } = unlock( editorPrivateApis ); | ||
|
||
export default function DocumentTools( { | ||
blockEditorMode, | ||
hasFixedToolbar, | ||
isDistractionFree, | ||
showIconLabels, | ||
} ) { | ||
const inserterButton = useRef(); | ||
const { | ||
isInserterOpen, | ||
isListViewOpen, | ||
listViewShortcut, | ||
isVisualMode, | ||
listViewToggleRef, | ||
} = useSelect( ( select ) => { | ||
const { isVisualMode } = useSelect( ( select ) => { | ||
const { getEditorMode } = select( editSiteStore ); | ||
const { getShortcutRepresentation } = select( keyboardShortcutsStore ); | ||
const { isInserterOpened, isListViewOpened, getListViewToggleRef } = | ||
unlock( select( editorStore ) ); | ||
|
||
return { | ||
isInserterOpen: isInserterOpened(), | ||
isListViewOpen: isListViewOpened(), | ||
listViewShortcut: getShortcutRepresentation( | ||
'core/editor/toggle-list-view' | ||
), | ||
isVisualMode: getEditorMode() === 'visual', | ||
listViewToggleRef: getListViewToggleRef(), | ||
}; | ||
}, [] ); | ||
const { __unstableSetEditorMode } = useDispatch( blockEditorStore ); | ||
const { setDeviceType, setIsInserterOpened, setIsListViewOpened } = | ||
useDispatch( editorStore ); | ||
|
||
const { setDeviceType } = useDispatch( editorStore ); | ||
const isLargeViewport = useViewportMatch( 'medium' ); | ||
|
||
const toggleInserter = useCallback( () => { | ||
if ( isInserterOpen ) { | ||
// Focusing the inserter button should close the inserter popover. | ||
// However, there are some cases it won't close when the focus is lost. | ||
// See https://github.com/WordPress/gutenberg/issues/43090 for more details. | ||
inserterButton.current.focus(); | ||
setIsInserterOpened( false ); | ||
} else { | ||
setIsInserterOpened( true ); | ||
} | ||
}, [ isInserterOpen, setIsInserterOpened ] ); | ||
|
||
const toggleListView = useCallback( | ||
() => setIsListViewOpened( ! isListViewOpen ), | ||
[ setIsListViewOpened, isListViewOpen ] | ||
); | ||
|
||
// If there's a block toolbar to be focused, disable the focus shortcut for the document toolbar. | ||
const blockToolbarCanBeFocused = useCanBlockToolbarBeFocused(); | ||
|
||
/* translators: button label text should, if possible, be under 16 characters. */ | ||
const longLabel = _x( | ||
'Toggle block inserter', | ||
'Generic label for block inserter button' | ||
); | ||
const shortLabel = ! isInserterOpen ? __( 'Add' ) : __( 'Close' ); | ||
|
||
const isZoomedOutViewExperimentEnabled = | ||
window?.__experimentalEnableZoomedOutView && isVisualMode; | ||
const isZoomedOutView = blockEditorMode === 'zoom-out'; | ||
|
||
return ( | ||
<NavigableToolbar | ||
className="edit-site-header-edit-mode__document-tools" | ||
aria-label={ __( 'Document tools' ) } | ||
shouldUseKeyboardFocusShortcut={ ! blockToolbarCanBeFocused } | ||
<EditorDocumentTools | ||
showIconLabels={ showIconLabels } | ||
disableBlockTools={ ! isVisualMode } | ||
listViewLabel={ __( 'List View' ) } | ||
> | ||
<div className="edit-site-header-edit-mode__toolbar"> | ||
{ ! isDistractionFree && ( | ||
{ isZoomedOutViewExperimentEnabled && | ||
isLargeViewport && | ||
! isDistractionFree && | ||
! hasFixedToolbar && ( | ||
<ToolbarItem | ||
ref={ inserterButton } | ||
as={ Button } | ||
className="edit-site-header-edit-mode__inserter-toggle" | ||
variant="primary" | ||
isPressed={ isInserterOpen } | ||
onMouseDown={ preventDefault } | ||
onClick={ toggleInserter } | ||
disabled={ ! isVisualMode } | ||
icon={ plus } | ||
label={ showIconLabels ? shortLabel : longLabel } | ||
showTooltip={ ! showIconLabels } | ||
aria-expanded={ isInserterOpen } | ||
className="edit-site-header-edit-mode__zoom-out-view-toggle" | ||
icon={ chevronUpDown } | ||
isPressed={ isZoomedOutView } | ||
/* translators: button label text should, if possible, be under 16 characters. */ | ||
label={ __( 'Zoom-out View' ) } | ||
onClick={ () => { | ||
setDeviceType( 'Desktop' ); | ||
__unstableSetEditorMode( | ||
isZoomedOutView ? 'edit' : 'zoom-out' | ||
); | ||
} } | ||
size="compact" | ||
/> | ||
) } | ||
{ isLargeViewport && ( | ||
<> | ||
{ ! hasFixedToolbar && ( | ||
<ToolbarItem | ||
as={ ToolSelector } | ||
showTooltip={ ! showIconLabels } | ||
variant={ | ||
showIconLabels ? 'tertiary' : undefined | ||
} | ||
disabled={ ! isVisualMode } | ||
size="compact" | ||
/> | ||
) } | ||
<ToolbarItem | ||
as={ UndoButton } | ||
showTooltip={ ! showIconLabels } | ||
variant={ showIconLabels ? 'tertiary' : undefined } | ||
size="compact" | ||
/> | ||
<ToolbarItem | ||
as={ RedoButton } | ||
showTooltip={ ! showIconLabels } | ||
variant={ showIconLabels ? 'tertiary' : undefined } | ||
size="compact" | ||
/> | ||
{ ! isDistractionFree && ( | ||
<ToolbarItem | ||
as={ Button } | ||
className="edit-site-header-edit-mode__list-view-toggle" | ||
disabled={ ! isVisualMode || isZoomedOutView } | ||
icon={ listView } | ||
isPressed={ isListViewOpen } | ||
/* translators: button label text should, if possible, be under 16 characters. */ | ||
label={ __( 'List View' ) } | ||
onClick={ toggleListView } | ||
ref={ listViewToggleRef } | ||
shortcut={ listViewShortcut } | ||
showTooltip={ ! showIconLabels } | ||
variant={ | ||
showIconLabels ? 'tertiary' : undefined | ||
} | ||
aria-expanded={ isListViewOpen } | ||
size="compact" | ||
/> | ||
) } | ||
{ isZoomedOutViewExperimentEnabled && | ||
! isDistractionFree && | ||
! hasFixedToolbar && ( | ||
<ToolbarItem | ||
as={ Button } | ||
className="edit-site-header-edit-mode__zoom-out-view-toggle" | ||
icon={ chevronUpDown } | ||
isPressed={ isZoomedOutView } | ||
/* translators: button label text should, if possible, be under 16 characters. */ | ||
label={ __( 'Zoom-out View' ) } | ||
onClick={ () => { | ||
setDeviceType( 'Desktop' ); | ||
__unstableSetEditorMode( | ||
isZoomedOutView | ||
? 'edit' | ||
: 'zoom-out' | ||
); | ||
} } | ||
size="compact" | ||
/> | ||
) } | ||
</> | ||
) } | ||
</div> | ||
</NavigableToolbar> | ||
</EditorDocumentTools> | ||
); | ||
} |
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
Oops, something went wrong.