-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
140 lines (133 loc) · 3.89 KB
/
index.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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useCallback, useRef } from '@wordpress/element';
import { useEntityBlockEditor } from '@wordpress/core-data';
import {
BlockList,
BlockEditorProvider,
__experimentalLinkControl,
BlockInspector,
BlockTools,
__unstableBlockSettingsMenuFirstItem,
__unstableUseTypingObserver as useTypingObserver,
BlockEditorKeyboardShortcuts,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useMergeRefs, useViewportMatch } from '@wordpress/compose';
import { ReusableBlocksMenuItems } from '@wordpress/reusable-blocks';
/**
* Internal dependencies
*/
import TemplatePartConverter from '../template-part-converter';
import NavigateToLink from '../navigate-to-link';
import { SidebarInspectorFill } from '../sidebar';
import { store as editSiteStore } from '../../store';
import BlockInspectorButton from './block-inspector-button';
import EditTemplatePartMenuButton from '../edit-template-part-menu-button';
import BackButton from './back-button';
import ResizableEditor from './resizable-editor';
const LAYOUT = {
type: 'default',
// At the root level of the site editor, no alignments should be allowed.
alignments: [],
};
export default function BlockEditor( { setIsInserterOpen } ) {
const { settings, templateType, templateId, page } = useSelect(
( select ) => {
const {
getSettings,
getEditedPostType,
getEditedPostId,
getPage,
} = select( editSiteStore );
return {
settings: getSettings( setIsInserterOpen ),
templateType: getEditedPostType(),
templateId: getEditedPostId(),
page: getPage(),
};
},
[ setIsInserterOpen ]
);
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
templateType
);
const { setPage } = useDispatch( editSiteStore );
const contentRef = useRef();
const mergedRefs = useMergeRefs( [ contentRef, useTypingObserver() ] );
const isMobileViewport = useViewportMatch( 'small', '<' );
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const isTemplatePart = templateType === 'wp_template_part';
return (
<BlockEditorProvider
settings={ settings }
value={ blocks }
onInput={ onInput }
onChange={ onChange }
useSubRegistry={ false }
>
<EditTemplatePartMenuButton />
<TemplatePartConverter />
<__experimentalLinkControl.ViewerFill>
{ useCallback(
( fillProps ) => (
<NavigateToLink
{ ...fillProps }
activePage={ page }
onActivePageChange={ setPage }
/>
),
[ page ]
) }
</__experimentalLinkControl.ViewerFill>
<SidebarInspectorFill>
<BlockInspector />
</SidebarInspectorFill>
<BlockTools
className={ classnames( 'edit-site-visual-editor', {
'is-focus-mode': isTemplatePart,
} ) }
__unstableContentRef={ contentRef }
onClick={ ( event ) => {
// Clear selected block when clicking on the gray background.
if ( event.target === event.currentTarget ) {
clearSelectedBlock();
}
} }
>
<BlockEditorKeyboardShortcuts.Register />
<BackButton />
<ResizableEditor
// Reinitialize the editor and reset the states when the template changes.
key={ templateId }
enableResizing={
isTemplatePart &&
// Disable resizing in mobile viewport.
! isMobileViewport
}
settings={ settings }
contentRef={ mergedRefs }
>
<BlockList
className="edit-site-block-editor__block-list wp-site-blocks"
__experimentalLayout={ LAYOUT }
renderAppender={ isTemplatePart ? false : undefined }
/>
</ResizableEditor>
<__unstableBlockSettingsMenuFirstItem>
{ ( { onClose } ) => (
<BlockInspectorButton onClick={ onClose } />
) }
</__unstableBlockSettingsMenuFirstItem>
</BlockTools>
<ReusableBlocksMenuItems />
</BlockEditorProvider>
);
}