-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
215 lines (207 loc) · 6.18 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { InterfaceSkeleton, ComplementaryArea } from '@wordpress/interface';
import { useSelect } from '@wordpress/data';
import { __, _x } from '@wordpress/i18n';
import { store as preferencesStore } from '@wordpress/preferences';
import { BlockBreadcrumb, BlockToolbar } from '@wordpress/block-editor';
import { useViewportMatch } from '@wordpress/compose';
import { useState, useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';
import EditorNotices from '../editor-notices';
import Header from '../header';
import InserterSidebar from '../inserter-sidebar';
import ListViewSidebar from '../list-view-sidebar';
import SavePublishPanels from '../save-publish-panels';
import TextEditor from '../text-editor';
import VisualEditor from '../visual-editor';
import EditorContentSlotFill from './content-slot-fill';
const interfaceLabels = {
/* translators: accessibility text for the editor top bar landmark region. */
header: __( 'Editor top bar' ),
/* translators: accessibility text for the editor content landmark region. */
body: __( 'Editor content' ),
/* translators: accessibility text for the editor settings landmark region. */
sidebar: __( 'Editor settings' ),
/* translators: accessibility text for the editor publish landmark region. */
actions: __( 'Editor publish' ),
/* translators: accessibility text for the editor footer landmark region. */
footer: __( 'Editor footer' ),
};
export default function EditorInterface( {
className,
styles,
children,
forceIsDirty,
contentRef,
disableIframe,
autoFocus,
customSaveButton,
customSavePanel,
forceDisableBlockTools,
title,
iframeProps,
} ) {
const {
mode,
isRichEditingEnabled,
isInserterOpened,
isListViewOpened,
isDistractionFree,
isPreviewMode,
showBlockBreadcrumbs,
documentLabel,
} = useSelect( ( select ) => {
const { get } = select( preferencesStore );
const { getEditorSettings, getPostTypeLabel } = select( editorStore );
const editorSettings = getEditorSettings();
const postTypeLabel = getPostTypeLabel();
return {
mode: select( editorStore ).getEditorMode(),
isRichEditingEnabled: editorSettings.richEditingEnabled,
isInserterOpened: select( editorStore ).isInserterOpened(),
isListViewOpened: select( editorStore ).isListViewOpened(),
isDistractionFree: get( 'core', 'distractionFree' ),
isPreviewMode: editorSettings.isPreviewMode,
showBlockBreadcrumbs: get( 'core', 'showBlockBreadcrumbs' ),
documentLabel:
// translators: Default label for the Document in the Block Breadcrumb.
postTypeLabel || _x( 'Document', 'noun, breadcrumb' ),
};
}, [] );
const isLargeViewport = useViewportMatch( 'medium' );
const secondarySidebarLabel = isListViewOpened
? __( 'Document Overview' )
: __( 'Block Library' );
// Local state for save panel.
// Note 'truthy' callback implies an open panel.
const [ entitiesSavedStatesCallback, setEntitiesSavedStatesCallback ] =
useState( false );
const closeEntitiesSavedStates = useCallback(
( arg ) => {
if ( typeof entitiesSavedStatesCallback === 'function' ) {
entitiesSavedStatesCallback( arg );
}
setEntitiesSavedStatesCallback( false );
},
[ entitiesSavedStatesCallback ]
);
return (
<InterfaceSkeleton
isDistractionFree={ isDistractionFree }
className={ clsx( 'editor-editor-interface', className, {
'is-entity-save-view-open': !! entitiesSavedStatesCallback,
'is-distraction-free': isDistractionFree && ! isPreviewMode,
} ) }
labels={ {
...interfaceLabels,
secondarySidebar: secondarySidebarLabel,
} }
header={
! isPreviewMode && (
<Header
forceIsDirty={ forceIsDirty }
setEntitiesSavedStatesCallback={
setEntitiesSavedStatesCallback
}
customSaveButton={ customSaveButton }
forceDisableBlockTools={ forceDisableBlockTools }
title={ title }
/>
)
}
editorNotices={ <EditorNotices /> }
secondarySidebar={
! isPreviewMode &&
mode === 'visual' &&
( ( isInserterOpened && <InserterSidebar /> ) ||
( isListViewOpened && <ListViewSidebar /> ) )
}
sidebar={
! isPreviewMode &&
! isDistractionFree && <ComplementaryArea.Slot scope="core" />
}
content={
<>
{ ! isDistractionFree && ! isPreviewMode && (
<EditorNotices />
) }
<EditorContentSlotFill.Slot>
{ ( [ editorCanvasView ] ) =>
editorCanvasView ? (
editorCanvasView
) : (
<>
{ ! isPreviewMode &&
( mode === 'text' ||
! isRichEditingEnabled ) && (
<TextEditor
// We should auto-focus the canvas (title) on load.
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={ autoFocus }
/>
) }
{ ! isPreviewMode &&
! isLargeViewport &&
mode === 'visual' && (
<BlockToolbar hideDragHandle />
) }
{ ( isPreviewMode ||
( isRichEditingEnabled &&
mode === 'visual' ) ) && (
<VisualEditor
styles={ styles }
contentRef={ contentRef }
disableIframe={ disableIframe }
// We should auto-focus the canvas (title) on load.
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={ autoFocus }
iframeProps={ iframeProps }
/>
) }
{ children }
</>
)
}
</EditorContentSlotFill.Slot>
</>
}
footer={
! isPreviewMode &&
! isDistractionFree &&
isLargeViewport &&
showBlockBreadcrumbs &&
isRichEditingEnabled &&
mode === 'visual' && (
<BlockBreadcrumb rootLabelText={ documentLabel } />
)
}
actions={
! isPreviewMode
? customSavePanel || (
<SavePublishPanels
closeEntitiesSavedStates={
closeEntitiesSavedStates
}
isEntitiesSavedStatesOpen={
entitiesSavedStatesCallback
}
setEntitiesSavedStatesCallback={
setEntitiesSavedStatesCallback
}
forceIsDirtyPublishPanel={ forceIsDirty }
/>
)
: undefined
}
/>
);
}