-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathsite-editor-canvas.js
146 lines (132 loc) · 3.91 KB
/
site-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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useRef } from '@wordpress/element';
import {
BlockList,
BlockTools,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useViewportMatch, useResizeObserver } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BackButton from './back-button';
import ResizableEditor from './resizable-editor';
import EditorCanvas from './editor-canvas';
import EditorCanvasContainer from '../editor-canvas-container';
import useSiteEditorSettings from './use-site-editor-settings';
import { store as editSiteStore } from '../../store';
import {
FOCUSABLE_ENTITIES,
NAVIGATION_POST_TYPE,
} from '../../utils/constants';
import { unlock } from '../../lock-unlock';
import PageContentFocusManager from '../page-content-focus-manager';
const LAYOUT = {
type: 'default',
// At the root level of the site editor, no alignments should be allowed.
alignments: [],
};
export default function SiteEditorCanvas( { postType, postId, context } ) {
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const isFocusMode = FOCUSABLE_ENTITIES.includes( postType );
const { isViewMode } = useSelect( ( select ) => {
const { getCanvasMode } = unlock( select( editSiteStore ) );
return {
isViewMode: getCanvasMode() === 'view',
};
}, [] );
const [ resizeObserver, sizes ] = useResizeObserver();
const settings = useSiteEditorSettings( postType, postId );
const { hasBlocks } = useSelect( ( select ) => {
const { getBlockCount } = select( blockEditorStore );
const blocks = getBlockCount();
return {
hasBlocks: !! blocks,
};
}, [] );
const isMobileViewport = useViewportMatch( 'small', '<' );
const enableResizing =
isFocusMode &&
! isViewMode &&
// Disable resizing in mobile viewport.
! isMobileViewport;
const contentRef = useRef();
const isTemplateTypeNavigation = postType === 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 ) || isViewMode
? false
: undefined;
const forceFullHeight = isNavigationFocusMode;
return (
<>
<EditorCanvasContainer.Slot>
{ ( [ editorCanvasView ] ) =>
editorCanvasView ? (
<div className="edit-site-visual-editor is-focus-mode">
{ editorCanvasView }
</div>
) : (
<BlockTools
className={ classnames( 'edit-site-visual-editor', {
'is-focus-mode':
isFocusMode || !! editorCanvasView,
'is-view-mode': isViewMode,
} ) }
__unstableContentRef={ contentRef }
onClick={ ( event ) => {
// Clear selected block when clicking on the gray background.
if ( event.target === event.currentTarget ) {
clearSelectedBlock();
}
} }
>
<BackButton />
<ResizableEditor
enableResizing={ enableResizing }
height={
sizes.height && ! forceFullHeight
? sizes.height
: '100%'
}
>
<EditorCanvas
enableResizing={ enableResizing }
settings={ settings }
contentRef={ contentRef }
>
{ resizeObserver }
<BlockList
className={ classnames(
'edit-site-block-editor__block-list wp-site-blocks',
{
'is-navigation-block':
isTemplateTypeNavigation,
}
) }
layout={ LAYOUT }
renderAppender={ showBlockAppender }
/>
</EditorCanvas>
</ResizableEditor>
</BlockTools>
)
}
</EditorCanvasContainer.Slot>
<PageContentFocusManager
contentRef={ contentRef }
context={ context }
/>
</>
);
}