-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathedit.js
More file actions
524 lines (453 loc) · 15.3 KB
/
edit.js
File metadata and controls
524 lines (453 loc) · 15.3 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* Internal dependencies
*/
import previewImage from './images/preview.jpg'
import {
i18n,
srcUrl,
settings,
cdnUrl,
devMode,
} from 'stackable'
import {
Button,
ModalDesignLibrary,
} from '~stackable/components'
import { SVGStackableIcon } from '~stackable/icons'
import { substituteCoreIfDisabled, BLOCK_STATE } from '~stackable/util'
import { usePresetControls } from '~stackable/hooks'
import { substitutionRules } from '../../blocks'
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n'
import { dispatch, select } from '@wordpress/data'
import {
createBlock, createBlocksFromInnerBlocksTemplate, getBlockVariations, getBlockType,
} from '@wordpress/blocks'
import {
useRef, useState, useCallback,
} from '@wordpress/element'
import { applyFilters } from '@wordpress/hooks'
import {
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
Placeholder, Modal, __experimentalVStack as VStack, Flex,
Spinner,
} from '@wordpress/components'
import { useBlockProps } from '@wordpress/block-editor'
import apiFetch from '@wordpress/api-fetch'
const convertBlocksToArray = block => {
const innerBlocks = block.innerBlocks.map( innerBlock => convertBlocksToArray( innerBlock ) )
return [ block.name, block.attributes, innerBlocks ]
}
const checkIfImageUrl = async value => {
if ( typeof value !== 'string' ) {
return value
}
let attributeUrl, libraryUrl
try {
attributeUrl = new URL( value )
libraryUrl = new URL( cdnUrl )
} catch ( error ) {
return value
}
if ( attributeUrl.origin !== libraryUrl.origin || ! attributeUrl.pathname.startsWith( libraryUrl.pathname ) ) {
return value
}
const matches = attributeUrl.pathname.match( /\/([^/]+\.(jpe?g|gif|png|mp4|webp))$/i )
if ( matches ) {
try {
const result = await apiFetch( {
path: '/stackable/v3/design_library_image',
method: 'POST',
// eslint-disable-next-line camelcase
data: { image_url: attributeUrl.href },
} )
if ( result.success ) {
return result.new_url
}
console.error( 'Stackable Design Library:', result.message ) // eslint-disable-line no-console
return value
} catch ( error ) {
console.error( 'Stackable Design Library:', error.message ) // eslint-disable-line no-console
return value
}
}
return value
}
const DIALOG_OPTIONS = {
CLOSE: 0,
REMOVE_BLOCKS: 1,
DISABLED_BLOCKS: 2,
}
const Edit = props => {
const {
clientId,
attributes,
} = props
const [ isLibraryOpen, setIsLibraryOpen ] = useState( false )
const [ isDialogOpen, setIsDialogOpen ] = useState( DIALOG_OPTIONS.CLOSE )
const [ isInserting, setIsInserting ] = useState( false )
const designsRef = useRef( [] )
const disabledBlocksRef = useRef( [] )
const callbackRef = useRef( null )
const blocksToRemoveRef = useRef( [] )
const insertIndexRef = useRef( -1 )
const blockProps = useBlockProps( {
className: 'ugb-design-library-block',
} )
const presetMarks = usePresetControls( 'spacingSizes' )
?.getPresetMarks() || null
const spacingSize = ! presetMarks || ! Array.isArray( presetMarks ) ? 120 : presetMarks[ presetMarks.length - 2 ].value
// Replaces the current block with a block made out of attributes.
const createBlockWithAttributes = async ( category, blockName, attributes, innerBlocks, substituteBlocks, parentClientId, type ) => {
const disabledBlocks = settings.stackable_block_states || {} // eslint-disable-line camelcase
// Recursively substitute core blocks to disabled Stackable blocks
const traverseBlocksAndSubstitute = blocks => {
return blocks.map( block => {
let isDisabled = true
// Maximum attempt to error if no substitution rule for the block
let attempts = 10
// Check if the new substituted block is still disabled
while ( isDisabled && attempts > 0 ) {
const _blockName = block[ 1 ].originalName || block[ 0 ]
block = substituteCoreIfDisabled( _blockName, block[ 1 ], block[ 2 ], substitutionRules )
isDisabled = block[ 0 ] in disabledBlocks && disabledBlocks[ block[ 0 ] ] === BLOCK_STATE.DISABLED
attempts--
}
// Do a preorder traversal by subsituting first before traversing
if ( block[ 2 ] && block[ 2 ].length > 0 ) {
block[ 2 ] = traverseBlocksAndSubstitute( block[ 2 ] )
}
if ( ! Array.isArray( block[ 2 ] ) ) {
block[ 2 ] = []
}
const _block = {
name: block[ 0 ],
attributes: block[ 1 ],
innerBlocks: block[ 2 ],
isValid: true,
}
return _block
} )
}
if ( ! Array.isArray( disabledBlocks ) && substituteBlocks ) {
let block = convertBlocksToArray( {
name: blockName, attributes, innerBlocks,
} )
block = traverseBlocksAndSubstitute( [ block ] )[ 0 ]
blockName = block.name
attributes = block.attributes
innerBlocks = block.innerBlocks
}
const cleanBlockAttributes = async blocks => {
for ( const block of blocks ) {
const blockName = block.name
// For blocks with variations, do not remove the uniqueId
// since that will prompt the layout picker to show.
const hasVariations = !! getBlockType( blockName ) && getBlockVariations( blockName ).length > 0
if ( ! hasVariations && block.attributes?.uniqueId ) {
delete block.attributes.uniqueId
}
const customAttributes = block.attributes?.customAttributes
const isDesignLibraryDevMode = devMode && localStorage.getItem( 'stk__design_library__dev_mode' ) === '1'
if ( ! isDesignLibraryDevMode ) {
const indexToDelete = customAttributes?.findIndex( attribute => attribute[ 0 ] === 'stk-design-library__bg-target' )
if ( customAttributes && indexToDelete !== -1 ) {
block.attributes?.customAttributes.splice( indexToDelete, 1 )
}
}
for ( const attributeName in block.attributes ) {
if ( typeof block.attributes[ attributeName ] === 'string' ) {
const value = String( block.attributes[ attributeName ] )
block.attributes[ attributeName ] = await checkIfImageUrl( value )
}
}
block.innerBlocks = await cleanBlockAttributes( block.innerBlocks )
}
return blocks
}
const block = await cleanBlockAttributes( [ {
name: blockName, attributes, innerBlocks,
} ] )
blockName = block[ 0 ].name
attributes = block[ 0 ].attributes
innerBlocks = block[ 0 ].innerBlocks
const isDesignLibraryDevMode = devMode && localStorage.getItem( 'stk__design_library__dev_mode' ) === '1'
if ( ! isDesignLibraryDevMode && type !== 'saved' ) {
if ( category !== 'Header' ) {
if ( ! parentClientId && attributes.hasBackground ) {
attributes.blockMargin = {
top: '',
right: '',
bottom: '0',
left: '',
}
} else if ( ! parentClientId ) {
attributes.blockMargin = {
top: spacingSize,
right: '',
bottom: spacingSize,
left: '',
}
}
const blockLayouts = select( 'stackable/global-spacing-and-borders' ).getBlockLayouts()
if ( attributes.hasBackground && typeof blockLayouts === 'object' && ! blockLayouts[ 'block-background-padding' ] ) {
attributes.blockPadding = {
top: spacingSize,
right: spacingSize,
bottom: spacingSize,
left: spacingSize,
}
}
}
}
return createBlock( blockName, attributes, createBlocksFromInnerBlocksTemplate( innerBlocks ) )
}
const addDesigns = async substituteBlocks => {
setIsInserting( true )
const { getBlockRootClientId } = select( 'core/block-editor' )
const parentClientId = getBlockRootClientId( clientId )
if ( ! designsRef.current?.length ) {
console.error( 'Design library selection failed: No designs found' ) // eslint-disable-line no-console
setIsInserting( false )
return
}
const designs = designsRef.current
const blocks = []
for ( const blockDesign of designs ) {
const {
designData, category, type,
} = blockDesign
for ( const patterns of designData ) {
const {
name, attributes, innerBlocks,
} = patterns
if ( name && attributes ) {
const block = await createBlockWithAttributes( category, name, applyFilters( 'stackable.design-library.attributes', attributes ), innerBlocks || [], substituteBlocks, parentClientId, type )
blocks.push( block )
} else {
console.error( 'Design library selection failed: No block data found' ) // eslint-disable-line no-console
}
}
}
if ( ! blocks.length ) {
setIsInserting( false )
return
}
if ( insertIndexRef.current !== -1 ) {
dispatch( 'core/block-editor' ).insertBlocks( blocks, insertIndexRef.current )
} else {
dispatch( 'core/block-editor' ).replaceBlocks( clientId, blocks )
}
// Set focus on the top-most added block
dispatch( 'core/block-editor' ).selectBlock( blocks[ 0 ].clientId )
if ( blocksToRemoveRef.current.length ) {
dispatch( 'core/block-editor' ).removeBlocks( blocksToRemoveRef.current )
blocksToRemoveRef.current = []
}
if ( callbackRef.current ) {
callbackRef.current()
}
setIsInserting( false )
}
const onClickTertiary = () => {
const disabledBlocks = disabledBlocksRef.current
const settingsPageUrl = `/wp-admin/options-general.php?page=stackable`
const newWindow = window.open( settingsPageUrl, '_blank' )
if ( newWindow ) {
newWindow.onload = () => { // Wait for the new page to fully load
setTimeout( () => {
try {
const blocksTab = newWindow.document.getElementById( 'stk-tab__blocks' )
if ( blocksTab ) {
blocksTab.click()
newWindow.postMessage( {
type: 'STK_ENABLE_BLOCKS', blocks: disabledBlocks, source: 'STK_DESIGN_LIBRARY',
}, window.location.origin )
}
} catch ( error ) {}
}, 5 )
}
newWindow.focus()
}
}
const onClickSecondary = async () => {
await addDesigns( false )
}
const onClickPrimary = async () => {
await addDesigns( true )
}
const onClose = useCallback( () => setIsLibraryOpen( false ), [] )
const onSelect = useCallback( async ( _designs, callback, type ) => {
const designs = []
let disabledBlocks = new Set()
_designs.forEach( design => {
const {
designData, blocksForSubstitution, category, type: designType,
} = design
if ( blocksForSubstitution.size ) {
disabledBlocks = disabledBlocks.union( blocksForSubstitution )
}
designs.push( {
designData, category, type: designType,
} )
} )
designsRef.current = designs
disabledBlocksRef.current = disabledBlocks
callbackRef.current = callback
if ( type === 'pages' ) {
const allBlocks = select( 'core/block-editor' ).getBlockOrder()
const blocksToRemove = allBlocks.filter( id => id !== clientId )
if ( blocksToRemove.length ) {
blocksToRemoveRef.current = allBlocks
setIsDialogOpen( DIALOG_OPTIONS.REMOVE_BLOCKS )
return
}
}
if ( disabledBlocks.size ) {
setIsDialogOpen( DIALOG_OPTIONS.DISABLED_BLOCKS )
return
}
await addDesigns( false )
}, [] )
if ( attributes.previewMode ) {
const src = previewImage.match( /https?:/i ) ? previewImage
: srcUrl ? `${ srcUrl }/${ previewImage }`
: previewImage
return (
<div className="ugb-design-library-block">
<img src={ src } alt="design-library" />
</div>
)
}
return (
<div { ...blockProps }>
<Placeholder
icon={ <SVGStackableIcon /> }
label={ __( 'Stackable Design Library', i18n ) }
instructions={ __( 'Open the Design Library and select a pre-designed block or layout.', i18n ) }
>
<Button
isSecondary
className="ugb-design-library-block__button"
onClick={ () => {
setIsLibraryOpen( true )
} }
>{ __( 'Open Design Library', i18n ) }</Button>
</Placeholder>
{ isLibraryOpen &&
<ModalDesignLibrary
onClose={ onClose }
onSelect={ onSelect }
/>
}
{ isDialogOpen !== DIALOG_OPTIONS.CLOSE &&
<Modal
className="ugb-design-library__confirm-dialog"
title={ __( 'Stackable Design Library', i18n ) }
onRequestClose={ () => {
setIsDialogOpen( DIALOG_OPTIONS.CLOSE )
setIsInserting( false )
} }
>
<VStack spacing={ 8 }>
{ isDialogOpen === DIALOG_OPTIONS.REMOVE_BLOCKS && <>
<div>
<p>
{ __( 'Adding this page design will replace all existing blocks in the editor. Are you sure you want to continue?', i18n ) }
</p>
</div>
<Flex direction="column" align="stretch">
{ isInserting ? <Spinner style={ { margin: '0 auto' } } /> : <>
<Button
__next40pxDefaultSize
variant="primary"
onClick={ () => {
insertIndexRef.current = 0
if ( disabledBlocksRef.current.size ) {
// Close this dialog and reopen after a while to show the notice for disabled blocks
// The existing blocks will be removed later
setIsDialogOpen( DIALOG_OPTIONS.CLOSE )
setTimeout( () => setIsDialogOpen( DIALOG_OPTIONS.DISABLED_BLOCKS ), 500 )
return
}
addDesigns( false )
} }
>
{ __( 'Replace existing content with page design', i18n ) }
</Button>
<Button
__next40pxDefaultSize
variant="secondary"
onClick={ () => {
insertIndexRef.current = blocksToRemoveRef.current.length
// When appending the page design, only remove the design library block
blocksToRemoveRef.current = [ clientId ]
if ( disabledBlocksRef.current.size ) {
setIsDialogOpen( DIALOG_OPTIONS.CLOSE )
setTimeout( () => setIsDialogOpen( DIALOG_OPTIONS.DISABLED_BLOCKS ), 500 )
return
}
addDesigns( false )
} }
>
{ __( 'Append page design only', i18n ) }
</Button>
<Button
__next40pxDefaultSize
variant="tertiary"
onClick={ () => {
blocksToRemoveRef.current = []
setIsDialogOpen( DIALOG_OPTIONS.CLOSE )
} }
>
{ __( 'Cancel', i18n ) }
</Button>
</> }
</Flex>
</> }
{ isDialogOpen === DIALOG_OPTIONS.DISABLED_BLOCKS && <>
<div>
<p>{ __( 'The designs you have selected contain the following disabled blocks:', i18n ) }</p>
<ul>
{ disabledBlocksRef.current && [ ...disabledBlocksRef.current ].map( ( block, i ) => <li key={ i }>{ block }</li> ) }
</ul>
<p> { __( 'These blocks can be enabled in the Stackable settings page. Do you want to keep the disabled blocks or substitute them with other Stackable or core blocks?', i18n ) }</p>
</div>
<Flex direction="column" align="stretch">
{ isInserting ? <Spinner style={ { margin: '0 auto' } } /> : <>
<Button
__next40pxDefaultSize
style={ { textAlign: 'center' } }
variant="primary"
onClick={ () => onClickPrimary() }
>
{ __( 'Add patterns and substitute blocks', i18n ) }
</Button>
<Button
__next40pxDefaultSize
style={ { textAlign: 'center', marginBottom: '16px' } }
variant="secondary"
onClick={ () => onClickSecondary() }
>
{ __( 'Add patterns only (no substitutes)', i18n ) }
</Button>
<Button
__next40pxDefaultSize
style={ { textAlign: 'center', marginBottom: '16px' } }
variant="tertiary"
onClick={ () => onClickTertiary() }
>
{ __( 'Enable blocks in settings', i18n ) }
</Button>
</> }
</Flex>
</> }
</VStack>
</Modal>
}
</div>
)
}
export default Edit