-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
381 lines (352 loc) · 11.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
BlockList,
store as blockEditorStore,
__unstableUseTypewriter as useTypewriter,
__unstableUseTypingObserver as useTypingObserver,
useSettings,
__experimentalRecursionProvider as RecursionProvider,
privateApis as blockEditorPrivateApis,
__experimentalUseResizeCanvas as useResizeCanvas,
} from '@wordpress/block-editor';
import { useEffect, useRef, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { parse } from '@wordpress/blocks';
import { store as coreStore } from '@wordpress/core-data';
import { useMergeRefs } from '@wordpress/compose';
/**
* Internal dependencies
*/
import PostTitle from '../post-title';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import EditTemplateBlocksNotification from './edit-template-blocks-notification';
const {
LayoutStyle,
useLayoutClasses,
useLayoutStyles,
ExperimentalBlockCanvas: BlockCanvas,
} = unlock( blockEditorPrivateApis );
/**
* Given an array of nested blocks, find the first Post Content
* block inside it, recursing through any nesting levels,
* and return its attributes.
*
* @param {Array} blocks A list of blocks.
*
* @return {Object | undefined} The Post Content block.
*/
function getPostContentAttributes( blocks ) {
for ( let i = 0; i < blocks.length; i++ ) {
if ( blocks[ i ].name === 'core/post-content' ) {
return blocks[ i ].attributes;
}
if ( blocks[ i ].innerBlocks.length ) {
const nestedPostContent = getPostContentAttributes(
blocks[ i ].innerBlocks
);
if ( nestedPostContent ) {
return nestedPostContent;
}
}
}
}
function checkForPostContentAtRootLevel( blocks ) {
for ( let i = 0; i < blocks.length; i++ ) {
if ( blocks[ i ].name === 'core/post-content' ) {
return true;
}
}
return false;
}
function EditorCanvas( {
// Ideally as we unify post and site editors, we won't need these props.
autoFocus,
className,
renderAppender,
styles,
disableIframe = false,
iframeProps,
children,
} ) {
const {
renderingMode,
postContentAttributes,
editedPostTemplate = {},
wrapperBlockName,
wrapperUniqueId,
deviceType,
} = useSelect( ( select ) => {
const {
getCurrentPostId,
getCurrentPostType,
getCurrentTemplateId,
getEditorSettings,
getRenderingMode,
getDeviceType,
} = select( editorStore );
const { getPostType, canUser, getEditedEntityRecord } =
select( coreStore );
const postTypeSlug = getCurrentPostType();
const _renderingMode = getRenderingMode();
let _wrapperBlockName;
if ( postTypeSlug === 'wp_block' ) {
_wrapperBlockName = 'core/block';
} else if ( ! _renderingMode === 'post-only' ) {
_wrapperBlockName = 'core/post-content';
}
const editorSettings = getEditorSettings();
const supportsTemplateMode = editorSettings.supportsTemplateMode;
const postType = getPostType( postTypeSlug );
const canEditTemplate = canUser( 'create', 'templates' );
const currentTemplateId = getCurrentTemplateId();
const template = currentTemplateId
? getEditedEntityRecord(
'postType',
'wp_template',
currentTemplateId
)
: undefined;
return {
renderingMode: _renderingMode,
postContentAttributes: getEditorSettings().postContentAttributes,
// Post template fetch returns a 404 on classic themes, which
// messes with e2e tests, so check it's a block theme first.
editedPostTemplate:
postType?.viewable && supportsTemplateMode && canEditTemplate
? template
: undefined,
wrapperBlockName: _wrapperBlockName,
wrapperUniqueId: getCurrentPostId(),
deviceType: getDeviceType(),
};
}, [] );
const { isCleanNewPost } = useSelect( editorStore );
const {
hasRootPaddingAwareAlignments,
themeHasDisabledLayoutStyles,
themeSupportsLayout,
} = useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeHasDisabledLayoutStyles: _settings.disableLayoutStyles,
themeSupportsLayout: _settings.supportsLayout,
hasRootPaddingAwareAlignments:
_settings.__experimentalFeatures?.useRootPaddingAwareAlignments,
};
}, [] );
const deviceStyles = useResizeCanvas( deviceType );
const [ globalLayoutSettings ] = useSettings( 'layout' );
// fallbackLayout is used if there is no Post Content,
// and for Post Title.
const fallbackLayout = useMemo( () => {
if ( renderingMode !== 'post-only' ) {
return { type: 'default' };
}
if ( themeSupportsLayout ) {
// We need to ensure support for wide and full alignments,
// so we add the constrained type.
return { ...globalLayoutSettings, type: 'constrained' };
}
// Set default layout for classic themes so all alignments are supported.
return { type: 'default' };
}, [ renderingMode, themeSupportsLayout, globalLayoutSettings ] );
const newestPostContentAttributes = useMemo( () => {
if (
! editedPostTemplate?.content &&
! editedPostTemplate?.blocks &&
postContentAttributes
) {
return postContentAttributes;
}
// When in template editing mode, we can access the blocks directly.
if ( editedPostTemplate?.blocks ) {
return getPostContentAttributes( editedPostTemplate?.blocks );
}
// If there are no blocks, we have to parse the content string.
// Best double-check it's a string otherwise the parse function gets unhappy.
const parseableContent =
typeof editedPostTemplate?.content === 'string'
? editedPostTemplate?.content
: '';
return getPostContentAttributes( parse( parseableContent ) ) || {};
}, [
editedPostTemplate?.content,
editedPostTemplate?.blocks,
postContentAttributes,
] );
const hasPostContentAtRootLevel = useMemo( () => {
if ( ! editedPostTemplate?.content && ! editedPostTemplate?.blocks ) {
return false;
}
// When in template editing mode, we can access the blocks directly.
if ( editedPostTemplate?.blocks ) {
return checkForPostContentAtRootLevel( editedPostTemplate?.blocks );
}
// If there are no blocks, we have to parse the content string.
// Best double-check it's a string otherwise the parse function gets unhappy.
const parseableContent =
typeof editedPostTemplate?.content === 'string'
? editedPostTemplate?.content
: '';
return (
checkForPostContentAtRootLevel( parse( parseableContent ) ) || false
);
}, [ editedPostTemplate?.content, editedPostTemplate?.blocks ] );
const { layout = {}, align = '' } = newestPostContentAttributes || {};
const postContentLayoutClasses = useLayoutClasses(
newestPostContentAttributes,
'core/post-content'
);
const blockListLayoutClass = classnames(
{
'is-layout-flow': ! themeSupportsLayout,
},
themeSupportsLayout && postContentLayoutClasses,
align && `align${ align }`
);
const postContentLayoutStyles = useLayoutStyles(
newestPostContentAttributes,
'core/post-content',
'.block-editor-block-list__layout.is-root-container'
);
// Update type for blocks using legacy layouts.
const postContentLayout = useMemo( () => {
return layout &&
( layout?.type === 'constrained' ||
layout?.inherit ||
layout?.contentSize ||
layout?.wideSize )
? { ...globalLayoutSettings, ...layout, type: 'constrained' }
: { ...globalLayoutSettings, ...layout, type: 'default' };
}, [
layout?.type,
layout?.inherit,
layout?.contentSize,
layout?.wideSize,
globalLayoutSettings,
] );
// If there is a Post Content block we use its layout for the block list;
// if not, this must be a classic theme, in which case we use the fallback layout.
const blockListLayout = postContentAttributes
? postContentLayout
: fallbackLayout;
const postEditorLayout =
blockListLayout?.type === 'default' && ! hasPostContentAtRootLevel
? fallbackLayout
: blockListLayout;
const observeTypingRef = useTypingObserver();
const titleRef = useRef();
useEffect( () => {
if ( ! autoFocus || ! isCleanNewPost() ) {
return;
}
titleRef?.current?.focus();
}, [ autoFocus, isCleanNewPost ] );
// Add some styles for alignwide/alignfull Post Content and its children.
const alignCSS = `.is-root-container.alignwide { max-width: var(--wp--style--global--wide-size); margin-left: auto; margin-right: auto;}
.is-root-container.alignwide:where(.is-layout-flow) > :not(.alignleft):not(.alignright) { max-width: var(--wp--style--global--wide-size);}
.is-root-container.alignfull { max-width: none; margin-left: auto; margin-right: auto;}
.is-root-container.alignfull:where(.is-layout-flow) > :not(.alignleft):not(.alignright) { max-width: none;}`;
const localRef = useRef();
const typewriterRef = useTypewriter();
const contentRef = useMergeRefs(
[
localRef,
renderingMode === 'post-only' ? typewriterRef : undefined,
].filter( ( r ) => !! r )
);
return (
<BlockCanvas
shouldIframe={
! disableIframe || [ 'Tablet', 'Mobile' ].includes( deviceType )
}
contentRef={ contentRef }
styles={ styles }
height="100%"
iframeProps={ {
...iframeProps,
style: {
...iframeProps?.style,
...deviceStyles,
},
} }
>
{ themeSupportsLayout &&
! themeHasDisabledLayoutStyles &&
renderingMode === 'post-only' && (
<>
<LayoutStyle
selector=".editor-editor-canvas__post-title-wrapper"
layout={ fallbackLayout }
/>
<LayoutStyle
selector=".block-editor-block-list__layout.is-root-container"
layout={ postEditorLayout }
/>
{ align && <LayoutStyle css={ alignCSS } /> }
{ postContentLayoutStyles && (
<LayoutStyle
layout={ postContentLayout }
css={ postContentLayoutStyles }
/>
) }
</>
) }
{ renderingMode === 'post-only' && (
<div
className={ classnames(
'editor-editor-canvas__post-title-wrapper',
// The following class is only here for backward comapatibility
// some themes might be using it to style the post title.
'edit-post-visual-editor__post-title-wrapper',
{
'has-global-padding': hasRootPaddingAwareAlignments,
}
) }
contentEditable={ false }
ref={ observeTypingRef }
style={ {
// This is using inline styles
// so it's applied for both iframed and non iframed editors.
marginTop: '4rem',
} }
>
<PostTitle ref={ titleRef } />
</div>
) }
<RecursionProvider
blockName={ wrapperBlockName }
uniqueId={ wrapperUniqueId }
>
<BlockList
className={ classnames(
className,
'is-' + deviceType.toLowerCase() + '-preview',
renderingMode !== 'post-only'
? 'wp-site-blocks'
: `${ blockListLayoutClass } wp-block-post-content` // Ensure root level blocks receive default/flow blockGap styling rules.
) }
layout={ blockListLayout }
dropZoneElement={
// When iframed, pass in the html element of the iframe to
// ensure the drop zone extends to the edges of the iframe.
disableIframe
? localRef.current
: localRef.current?.parentNode
}
renderAppender={ renderAppender }
/>
<EditTemplateBlocksNotification contentRef={ localRef } />
</RecursionProvider>
{ children }
</BlockCanvas>
);
}
export default EditorCanvas;