-
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.
- Loading branch information
1 parent
fe7bff9
commit 3ca4bb4
Showing
49 changed files
with
714 additions
and
716 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
286 changes: 286 additions & 0 deletions
286
packages/edit-site/src/components/header-edit-mode/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 |
---|---|---|
@@ -0,0 +1,286 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useRef } from '@wordpress/element'; | ||
import { useViewportMatch } from '@wordpress/compose'; | ||
import { | ||
ToolSelector, | ||
__experimentalPreviewOptions as PreviewOptions, | ||
NavigableToolbar, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { PinnedItems } from '@wordpress/interface'; | ||
import { _x, __ } from '@wordpress/i18n'; | ||
import { listView, plus, external, chevronUpDown } from '@wordpress/icons'; | ||
import { | ||
Button, | ||
ToolbarItem, | ||
MenuGroup, | ||
MenuItem, | ||
VisuallyHidden, | ||
} from '@wordpress/components'; | ||
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts'; | ||
import { store as preferencesStore } from '@wordpress/preferences'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import MoreMenu from './more-menu'; | ||
import SaveButton from '../save-button'; | ||
import UndoButton from './undo-redo/undo'; | ||
import RedoButton from './undo-redo/redo'; | ||
import DocumentActions from './document-actions'; | ||
import TemplateDetails from '../template-details'; | ||
import { store as editSiteStore } from '../../store'; | ||
|
||
const preventDefault = ( event ) => { | ||
event.preventDefault(); | ||
}; | ||
|
||
export default function Header() { | ||
const inserterButton = useRef(); | ||
const { | ||
deviceType, | ||
entityTitle, | ||
template, | ||
templateType, | ||
isInserterOpen, | ||
isListViewOpen, | ||
listViewShortcut, | ||
isLoaded, | ||
isVisualMode, | ||
settings, | ||
blockEditorMode, | ||
showIconLabels, | ||
} = useSelect( ( select ) => { | ||
const { | ||
__experimentalGetPreviewDeviceType, | ||
getEditedPostType, | ||
getEditedPostId, | ||
isInserterOpened, | ||
isListViewOpened, | ||
getEditorMode, | ||
getSettings, | ||
} = select( editSiteStore ); | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const { __experimentalGetTemplateInfo: getTemplateInfo } = | ||
select( editorStore ); | ||
const { getShortcutRepresentation } = select( keyboardShortcutsStore ); | ||
const { __unstableGetEditorMode } = select( blockEditorStore ); | ||
|
||
const postType = getEditedPostType(); | ||
const postId = getEditedPostId(); | ||
const record = getEditedEntityRecord( 'postType', postType, postId ); | ||
const _isLoaded = !! postId; | ||
|
||
return { | ||
deviceType: __experimentalGetPreviewDeviceType(), | ||
entityTitle: getTemplateInfo( record ).title, | ||
isLoaded: _isLoaded, | ||
template: record, | ||
templateType: postType, | ||
isInserterOpen: isInserterOpened(), | ||
isListViewOpen: isListViewOpened(), | ||
listViewShortcut: getShortcutRepresentation( | ||
'core/edit-site/toggle-list-view' | ||
), | ||
isVisualMode: getEditorMode() === 'visual', | ||
settings: getSettings(), | ||
blockEditorMode: __unstableGetEditorMode(), | ||
showIconLabels: select( preferencesStore ).get( | ||
'core/edit-site', | ||
'showIconLabels' | ||
), | ||
}; | ||
}, [] ); | ||
|
||
const { | ||
__experimentalSetPreviewDeviceType: setPreviewDeviceType, | ||
setIsInserterOpened, | ||
setIsListViewOpened, | ||
} = useDispatch( editSiteStore ); | ||
const { __unstableSetEditorMode } = useDispatch( blockEditorStore ); | ||
|
||
const isLargeViewport = useViewportMatch( 'medium' ); | ||
|
||
const openInserter = useCallback( () => { | ||
if ( isInserterOpen ) { | ||
// Focusing the inserter button closes the inserter popover. | ||
inserterButton.current.focus(); | ||
} else { | ||
setIsInserterOpened( true ); | ||
} | ||
}, [ isInserterOpen, setIsInserterOpened ] ); | ||
|
||
const toggleListView = useCallback( | ||
() => setIsListViewOpened( ! isListViewOpen ), | ||
[ setIsListViewOpened, isListViewOpen ] | ||
); | ||
|
||
const isFocusMode = templateType === 'wp_template_part'; | ||
|
||
/* 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 ( | ||
<div | ||
className={ classnames( 'edit-site-header-edit-mode', { | ||
'show-icon-labels': showIconLabels, | ||
} ) } | ||
> | ||
<NavigableToolbar | ||
className="edit-site-header-edit-mode__start" | ||
aria-label={ __( 'Document tools' ) } | ||
> | ||
<div className="edit-site-header-edit-mode__toolbar"> | ||
<ToolbarItem | ||
ref={ inserterButton } | ||
as={ Button } | ||
className="edit-site-header-edit-mode-toolbar__inserter-toggle" | ||
variant="primary" | ||
isPressed={ isInserterOpen } | ||
onMouseDown={ preventDefault } | ||
onClick={ openInserter } | ||
disabled={ ! isVisualMode } | ||
icon={ plus } | ||
label={ showIconLabels ? shortLabel : longLabel } | ||
showTooltip={ ! showIconLabels } | ||
/> | ||
{ isLargeViewport && ( | ||
<> | ||
<ToolbarItem | ||
as={ ToolSelector } | ||
showTooltip={ ! showIconLabels } | ||
variant={ | ||
showIconLabels ? 'tertiary' : undefined | ||
} | ||
disabled={ ! isVisualMode } | ||
/> | ||
<ToolbarItem | ||
as={ UndoButton } | ||
showTooltip={ ! showIconLabels } | ||
variant={ | ||
showIconLabels ? 'tertiary' : undefined | ||
} | ||
/> | ||
<ToolbarItem | ||
as={ RedoButton } | ||
showTooltip={ ! showIconLabels } | ||
variant={ | ||
showIconLabels ? 'tertiary' : undefined | ||
} | ||
/> | ||
<ToolbarItem | ||
as={ Button } | ||
className="edit-site-header-edit-mode-toolbar__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 } | ||
shortcut={ listViewShortcut } | ||
showTooltip={ ! showIconLabels } | ||
variant={ | ||
showIconLabels ? 'tertiary' : undefined | ||
} | ||
/> | ||
{ isZoomedOutViewExperimentEnabled && ( | ||
<ToolbarItem | ||
as={ Button } | ||
className="edit-site-header-edit-mode-toolbar__zoom-out-view-toggle" | ||
icon={ chevronUpDown } | ||
isPressed={ isZoomedOutView } | ||
/* translators: button label text should, if possible, be under 16 characters. */ | ||
label={ __( 'Zoom-out View' ) } | ||
onClick={ () => { | ||
setPreviewDeviceType( 'desktop' ); | ||
setIsListViewOpened( false ); | ||
__unstableSetEditorMode( | ||
isZoomedOutView | ||
? 'edit' | ||
: 'zoom-out' | ||
); | ||
} } | ||
/> | ||
) } | ||
</> | ||
) } | ||
</div> | ||
</NavigableToolbar> | ||
|
||
<div className="edit-site-header-edit-mode__center"> | ||
<DocumentActions | ||
entityTitle={ entityTitle } | ||
entityLabel={ | ||
templateType === 'wp_template_part' | ||
? 'template part' | ||
: 'template' | ||
} | ||
isLoaded={ isLoaded } | ||
showIconLabels={ showIconLabels } | ||
> | ||
{ ( { onClose } ) => ( | ||
<TemplateDetails | ||
template={ template } | ||
onClose={ onClose } | ||
/> | ||
) } | ||
</DocumentActions> | ||
</div> | ||
|
||
<div className="edit-site-header-edit-mode__end"> | ||
<div className="edit-site-header-edit-mode__actions"> | ||
{ ! isFocusMode && ( | ||
<div | ||
className={ classnames( | ||
'edit-site-header-edit-mode__actions__preview-options', | ||
{ 'is-zoomed-out': isZoomedOutView } | ||
) } | ||
> | ||
<PreviewOptions | ||
deviceType={ deviceType } | ||
setDeviceType={ setPreviewDeviceType } | ||
> | ||
<MenuGroup> | ||
<MenuItem | ||
href={ settings?.siteUrl } | ||
target="_blank" | ||
icon={ external } | ||
> | ||
{ __( 'View site' ) } | ||
<VisuallyHidden as="span"> | ||
{ | ||
/* translators: accessibility text */ | ||
__( '(opens in a new tab)' ) | ||
} | ||
</VisuallyHidden> | ||
</MenuItem> | ||
</MenuGroup> | ||
</PreviewOptions> | ||
</div> | ||
) } | ||
<SaveButton /> | ||
<PinnedItems.Slot scope="core/edit-site" /> | ||
<MoreMenu showIconLabels={ showIconLabels } /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.