-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
410 lines (382 loc) · 11.2 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
VisualEditorGlobalKeyboardShortcuts,
PostTitle,
store as editorStore,
} from '@wordpress/editor';
import {
WritingFlow,
BlockList,
BlockTools,
store as blockEditorStore,
__unstableUseBlockSelectionClearer as useBlockSelectionClearer,
__unstableUseTypewriter as useTypewriter,
__unstableUseClipboardHandler as useClipboardHandler,
__unstableUseTypingObserver as useTypingObserver,
__unstableBlockSettingsMenuFirstItem,
__experimentalUseResizeCanvas as useResizeCanvas,
__unstableEditorStyles as EditorStyles,
useSetting,
__experimentalLayoutStyle as LayoutStyle,
__unstableUseMouseMoveTypingReset as useMouseMoveTypingReset,
__unstableIframe as Iframe,
__experimentalRecursionProvider as RecursionProvider,
__experimentaluseLayoutClasses as useLayoutClasses,
__experimentaluseLayoutStyles as useLayoutStyles,
} from '@wordpress/block-editor';
import { useEffect, useRef, useMemo } from '@wordpress/element';
import { Button, __unstableMotion as motion } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMergeRefs } from '@wordpress/compose';
import { arrowLeft } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import { parse } from '@wordpress/blocks';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import BlockInspectorButton from './block-inspector-button';
import { store as editPostStore } from '../../store';
function MaybeIframe( {
children,
contentRef,
shouldIframe,
styles,
assets,
style,
} ) {
const ref = useMouseMoveTypingReset();
if ( ! shouldIframe ) {
return (
<>
<EditorStyles styles={ styles } />
<WritingFlow
ref={ contentRef }
className="editor-styles-wrapper"
style={ { flex: '1', ...style } }
tabIndex={ -1 }
>
{ children }
</WritingFlow>
</>
);
}
return (
<Iframe
head={ <EditorStyles styles={ styles } /> }
assets={ assets }
ref={ ref }
contentRef={ contentRef }
style={ { width: '100%', height: '100%', display: 'block' } }
name="editor-canvas"
>
{ children }
</Iframe>
);
}
/**
* Given an array of nested blocks, find the first Post Content
* block inside it, recursing through any nesting levels.
*
* @param {Array} blocks A list of blocks.
*
* @return {Object} The Post Content block.
*/
function findPostContent( blocks ) {
for ( let i = 0; i < blocks.length; i++ ) {
if ( blocks[ i ].name === 'core/post-content' ) {
return blocks[ i ];
}
if ( blocks[ i ].innerBlocks.length ) {
const nestedPostContent = findPostContent(
blocks[ i ].innerBlocks
);
if ( nestedPostContent ) {
return nestedPostContent;
}
}
}
}
export default function VisualEditor( { styles } ) {
const {
deviceType,
isWelcomeGuideVisible,
isTemplateMode,
editedPostTemplate = {},
wrapperBlockName,
wrapperUniqueId,
isBlockBasedTheme,
} = useSelect( ( select ) => {
const {
isFeatureActive,
isEditingTemplate,
__experimentalGetPreviewDeviceType,
getEditedPostTemplate,
} = select( editPostStore );
const { getCurrentPostId, getCurrentPostType, getEditorSettings } =
select( editorStore );
const _isTemplateMode = isEditingTemplate();
let _wrapperBlockName;
if ( getCurrentPostType() === 'wp_block' ) {
_wrapperBlockName = 'core/block';
} else if ( ! _isTemplateMode ) {
_wrapperBlockName = 'core/post-content';
}
const editorSettings = getEditorSettings();
const supportsTemplateMode = editorSettings.supportsTemplateMode;
const canEditTemplate = select( coreStore ).canUser(
'create',
'templates'
);
return {
deviceType: __experimentalGetPreviewDeviceType(),
isWelcomeGuideVisible: isFeatureActive( 'welcomeGuide' ),
isTemplateMode: _isTemplateMode,
// Post template fetch returns a 404 on classic themes, which
// messes with e2e tests, so we check it's a block theme first.
editedPostTemplate:
supportsTemplateMode && canEditTemplate
? getEditedPostTemplate()
: undefined,
wrapperBlockName: _wrapperBlockName,
wrapperUniqueId: getCurrentPostId(),
isBlockBasedTheme: editorSettings.__unstableIsBlockBasedTheme,
};
}, [] );
const { isCleanNewPost } = useSelect( editorStore );
const hasMetaBoxes = useSelect(
( select ) => select( editPostStore ).hasMetaBoxes(),
[]
);
const {
themeHasDisabledLayoutStyles,
themeSupportsLayout,
assets,
isFocusMode,
} = useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeHasDisabledLayoutStyles: _settings.disableLayoutStyles,
themeSupportsLayout: _settings.supportsLayout,
assets: _settings.__unstableResolvedAssets,
isFocusMode: _settings.focusMode,
};
}, [] );
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const { setIsEditingTemplate } = useDispatch( editPostStore );
const desktopCanvasStyles = {
height: '100%',
width: '100%',
margin: 0,
display: 'flex',
flexFlow: 'column',
// Default background color so that grey
// .edit-post-editor-regions__content color doesn't show through.
background: 'white',
};
const templateModeStyles = {
...desktopCanvasStyles,
borderRadius: '2px 2px 0 0',
border: '1px solid #ddd',
borderBottom: 0,
};
const resizedCanvasStyles = useResizeCanvas( deviceType, isTemplateMode );
const globalLayoutSettings = useSetting( 'layout' );
const previewMode = 'is-' + deviceType.toLowerCase() + '-preview';
let animatedStyles = isTemplateMode
? templateModeStyles
: desktopCanvasStyles;
if ( resizedCanvasStyles ) {
animatedStyles = resizedCanvasStyles;
}
let paddingBottom;
// Add a constant padding for the typewritter effect. When typing at the
// bottom, there needs to be room to scroll up.
if ( ! hasMetaBoxes && ! resizedCanvasStyles && ! isTemplateMode ) {
paddingBottom = '40vh';
}
const ref = useRef();
const contentRef = useMergeRefs( [
ref,
useClipboardHandler(),
useTypewriter(),
useTypingObserver(),
useBlockSelectionClearer(),
] );
const blockSelectionClearerRef = useBlockSelectionClearer();
// fallbackLayout is used if there is no Post Content,
// and for Post Title.
const fallbackLayout = useMemo( () => {
if ( isTemplateMode ) {
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' };
}, [ isTemplateMode, themeSupportsLayout, globalLayoutSettings ] );
const postContentBlock = useMemo( () => {
// When in template editing mode, we can access the blocks directly.
if ( editedPostTemplate?.blocks ) {
return findPostContent( 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 findPostContent( parse( parseableContent ) ) || {};
}, [ editedPostTemplate?.content, editedPostTemplate?.blocks ] );
const postContentLayoutClasses = useLayoutClasses( postContentBlock );
const blockListLayoutClass = classnames(
{
'is-layout-flow': ! themeSupportsLayout,
},
themeSupportsLayout && postContentLayoutClasses
);
const postContentLayoutStyles = useLayoutStyles(
postContentBlock,
'.block-editor-block-list__layout.is-root-container'
);
const layout = postContentBlock?.attributes?.layout || {};
// 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 = postContentBlock
? postContentLayout
: fallbackLayout;
const titleRef = useRef();
useEffect( () => {
if ( isWelcomeGuideVisible || ! isCleanNewPost() ) {
return;
}
titleRef?.current?.focus();
}, [ isWelcomeGuideVisible, isCleanNewPost ] );
return (
<BlockTools
__unstableContentRef={ ref }
className={ classnames( 'edit-post-visual-editor', {
'is-template-mode': isTemplateMode,
} ) }
>
<VisualEditorGlobalKeyboardShortcuts />
<motion.div
className="edit-post-visual-editor__content-area"
animate={ {
padding: isTemplateMode ? '48px 48px 0' : '0',
} }
ref={ blockSelectionClearerRef }
>
{ isTemplateMode && (
<Button
className="edit-post-visual-editor__exit-template-mode"
icon={ arrowLeft }
onClick={ () => {
clearSelectedBlock();
setIsEditingTemplate( false );
} }
>
{ __( 'Back' ) }
</Button>
) }
<motion.div
animate={ animatedStyles }
initial={ desktopCanvasStyles }
className={ previewMode }
>
<MaybeIframe
shouldIframe={
( isBlockBasedTheme && ! hasMetaBoxes ) ||
isTemplateMode ||
deviceType === 'Tablet' ||
deviceType === 'Mobile'
}
contentRef={ contentRef }
styles={ styles }
assets={ assets }
style={ { paddingBottom } }
>
{ themeSupportsLayout &&
! themeHasDisabledLayoutStyles &&
! isTemplateMode && (
<>
<LayoutStyle
selector=".edit-post-visual-editor__post-title-wrapper, .block-editor-block-list__layout.is-root-container"
layout={ fallbackLayout }
layoutDefinitions={
globalLayoutSettings?.definitions
}
/>
{ postContentLayoutStyles && (
<LayoutStyle
layout={ postContentLayout }
css={ postContentLayoutStyles }
layoutDefinitions={
globalLayoutSettings?.definitions
}
/>
) }
</>
) }
{ ! isTemplateMode && (
<div
className={ classnames(
'edit-post-visual-editor__post-title-wrapper',
{
'is-focus-mode': isFocusMode,
}
) }
contentEditable={ false }
>
<PostTitle ref={ titleRef } />
</div>
) }
<RecursionProvider
blockName={ wrapperBlockName }
uniqueId={ wrapperUniqueId }
>
<BlockList
className={
isTemplateMode
? 'wp-site-blocks'
: `${ blockListLayoutClass } wp-block-post-content` // Ensure root level blocks receive default/flow blockGap styling rules.
}
__experimentalLayout={ blockListLayout }
/>
</RecursionProvider>
</MaybeIframe>
</motion.div>
</motion.div>
<__unstableBlockSettingsMenuFirstItem>
{ ( { onClose } ) => (
<BlockInspectorButton onClick={ onClose } />
) }
</__unstableBlockSettingsMenuFirstItem>
</BlockTools>
);
}