/
editor-canvas.js
158 lines (147 loc) · 4.1 KB
/
editor-canvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import { ENTER, SPACE } from '@wordpress/keycodes';
import { useState, useEffect, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import {
FOCUSABLE_ENTITIES,
NAVIGATION_POST_TYPE,
} from '../../utils/constants';
const { EditorCanvas: EditorCanvasRoot } = unlock( editorPrivateApis );
function EditorCanvas( {
enableResizing,
settings,
children,
onClick,
...props
} ) {
const {
hasBlocks,
isFocusMode,
templateType,
canvasMode,
currentPostIsTrashed,
} = useSelect( ( select ) => {
const { getBlockCount } = select( blockEditorStore );
const { getEditedPostType, getCanvasMode } = unlock(
select( editSiteStore )
);
const _templateType = getEditedPostType();
return {
templateType: _templateType,
isFocusMode: FOCUSABLE_ENTITIES.includes( _templateType ),
canvasMode: getCanvasMode(),
hasBlocks: !! getBlockCount(),
currentPostIsTrashed:
select( editorStore ).getCurrentPostAttribute( 'status' ) ===
'trash',
};
}, [] );
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const [ isFocused, setIsFocused ] = useState( false );
useEffect( () => {
if ( canvasMode === 'edit' ) {
setIsFocused( false );
}
}, [ canvasMode ] );
// In view mode, make the canvas iframe be perceived and behave as a button
// to switch to edit mode, with a meaningful label and no title attribute.
const viewModeIframeProps = {
'aria-label': __( 'Edit' ),
'aria-disabled': currentPostIsTrashed,
title: null,
role: 'button',
tabIndex: 0,
onFocus: () => setIsFocused( true ),
onBlur: () => setIsFocused( false ),
onKeyDown: ( event ) => {
const { keyCode } = event;
if (
( keyCode === ENTER || keyCode === SPACE ) &&
! currentPostIsTrashed
) {
event.preventDefault();
setCanvasMode( 'edit' );
}
},
onClick: () => {
if ( !! onClick ) {
onClick();
} else {
setCanvasMode( 'edit' );
}
},
onClickCapture: ( event ) => {
if ( currentPostIsTrashed ) {
event.preventDefault();
event.stopPropagation();
}
},
readonly: true,
};
const isTemplateTypeNavigation = templateType === NAVIGATION_POST_TYPE;
const isNavigationFocusMode = isTemplateTypeNavigation && isFocusMode;
// Hide the appender when:
// - In navigation focus mode (should only allow the root Nav block).
// - In view mode (i.e. not editing).
const showBlockAppender =
( isNavigationFocusMode && hasBlocks ) || canvasMode === 'view'
? false
: undefined;
const styles = useMemo(
() => [
...settings.styles,
{
// Forming a "block formatting context" to prevent margin collapsing.
// @see https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
css: `.is-root-container{display:flow-root;${
// Some themes will have `min-height: 100vh` for the root container,
// which isn't a requirement in auto resize mode.
enableResizing ? 'min-height:0!important;' : ''
}}body{position:relative; ${
canvasMode === 'view'
? `min-height: 100vh; ${
currentPostIsTrashed ? '' : 'cursor: pointer;'
}`
: ''
}}}`,
},
],
[ settings.styles, enableResizing, canvasMode, currentPostIsTrashed ]
);
return (
<EditorCanvasRoot
className={ clsx( 'edit-site-editor-canvas__block-list', {
'is-navigation-block': isTemplateTypeNavigation,
} ) }
renderAppender={ showBlockAppender }
styles={ styles }
iframeProps={ {
className: clsx( 'edit-site-visual-editor__editor-canvas', {
'is-focused': isFocused && canvasMode === 'view',
} ),
...props,
...( canvasMode === 'view' ? viewModeIframeProps : {} ),
} }
>
{ children }
</EditorCanvasRoot>
);
}
export default EditorCanvas;