-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathadmin.js
More file actions
1660 lines (1571 loc) · 58.6 KB
/
admin.js
File metadata and controls
1660 lines (1571 loc) · 58.6 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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Internal dependencies
*/
import './news'
import SVGEssentialIcon from './images/settings-icon-essential.svg'
import SVGSpecialIcon from './images/settings-icon-special.svg'
import SVGSectionIcon from './images/settings-icon-section.svg'
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n'
import {
useEffect, useState, useCallback, useMemo, Suspense, Fragment,
} from '@wordpress/element'
import domReady from '@wordpress/dom-ready'
import {
Button, Flex, FlexItem, Spinner, CheckboxControl, Modal,
} from '@wordpress/components'
import { models } from '@wordpress/api'
import { applyFilters } from '@wordpress/hooks'
/**
* External dependencies
*/
import {
i18n,
showProNoticesOption,
isPro,
v2disabledBlocks,
defaultBreakpoints,
editorRoles,
} from 'stackable'
import classnames from 'classnames'
import { fetchSettings, importBlocks } from '~stackable/util/admin'
import { createRoot } from '~stackable/util/element'
import AdminSelectSetting from '~stackable/components/admin-select-setting'
import AdminToggleSetting from '~stackable/components/admin-toggle-setting'
import AdminTextSetting from '~stackable/components/admin-text-setting'
import AdminToolbarSetting from '~stackable/components/admin-toolbar-setting'
import { GettingStarted } from './getting-started'
import { BLOCK_STATE } from '~stackable/util/blocks'
import { BlockToggler, OptimizationSettings } from '~stackable/deprecated/v2/welcome/admin'
import blockData from '~stackable/deprecated/v2/welcome/blocks'
import { UsefulPlugins } from './useful-plugins'
const [ FREE_BLOCKS, BLOCK_DEPENDENCIES ] = importBlocks( require.context( '../block', true, /block\.json$/ ) )
export const getAllBlocks = () => applyFilters( 'stackable.settings.blocks', FREE_BLOCKS )
export const BLOCK_CATEROGIES = [
{
id: 'essential',
label: __( 'Essential Blocks', i18n ),
Icon: SVGEssentialIcon,
description: __( 'All the necessary building blocks you need to design anything.', i18n ),
},
{
id: 'special',
label: __( 'Special Blocks', i18n ),
Icon: SVGSpecialIcon,
description: __( 'Blocks with special functionality that will allow you to create distinctive designs.', i18n ),
},
{
id: 'section',
label: __( 'Section Blocks', i18n ),
Icon: SVGSectionIcon,
description: __( 'Use these blocks act as templates to help you build sections effortlessly.', i18n ),
},
]
const SEARCH_TREE = [
{
id: 'editor-settings',
label: __( 'Editor Settings', i18n ),
groups: [
{
id: 'blocks',
children: [
__( 'Nested Block Width', i18n ),
__( 'Nested Wide Block Width', i18n ),
],
},
{
id: 'editor',
children: [
__( 'Stackable Text as Default Block', i18n ),
__( 'Design Library', i18n ),
__( 'Stackable Settings', i18n ),
__( 'Use Size Presets by Default', i18n ),
__( 'Block Linking (Beta)', i18n ),
],
},
{
id: 'toolbar',
children: [
__( 'Toolbar Text Highlight', i18n ),
__( 'Toolbar Dynamic Content', i18n ),
__( 'Copy & Paste Styles', i18n ),
__( 'Reset Layout', i18n ),
__( 'Save as Default Block', i18n ),
],
},
{
id: 'inspector',
children: [
__( 'Don\'t show help video tooltips', i18n ),
__( 'Auto-Collapse Panels', i18n ),
],
},
],
},
{
id: 'responsiveness',
label: __( 'Responsiveness', i18n ),
groups: [
{
id: 'dynamic-breakpoints',
children: [
__( 'Tablet Breakpoint', i18n ),
__( 'Mobile Breakpoint', i18n ),
],
},
],
},
{
id: 'blocks',
label: __( 'Blocks', i18n ),
// Categories are essential, special, and section
groups: BLOCK_CATEROGIES.map( ( { id } ) => {
const DERIVED_BLOCKS = getAllBlocks()
return {
id,
children: DERIVED_BLOCKS[ id ].map( block => {
return block.title
} ),
}
} ),
},
{
id: 'optimizations',
label: __( 'Optimization', i18n ),
groups: [
{
id: 'optimizations',
children: [
__( 'Optimize Inline CSS', i18n ),
],
},
],
},
{
id: 'global-settings',
label: __( 'Global Settings', i18n ),
groups: [
{
id: 'global-settings',
children: [
__( 'Force Typography Styles', i18n ),
],
},
],
},
{
id: 'role-manager',
label: __( 'Role Manager', i18n ),
groups: [
{
id: 'role-manager',
children: Object.values( editorRoles || {} ),
},
],
},
{
id: 'custom-fields-settings',
label: __( 'Custom Fields', i18n ),
groups: [
{
id: 'custom-fields-settings',
children: [
__( 'Custom Fields', i18n ),
...Object.values( editorRoles || {} ),
],
},
],
},
{
id: 'integrations',
label: __( 'Integration', i18n ),
groups: [
{
id: 'integrations',
children: [
__( 'Google Maps API Key', i18n ),
__( 'FontAwesome Pro Kit', i18n ),
__( 'FontAwesome Icon Library Version', i18n ),
],
},
],
},
{
id: 'other-settings',
label: __( 'Miscellaneous ', i18n ),
groups: [
{
id: 'miscellaneous',
children: [
__( 'Show Go premium notices', i18n ),
__( 'Generate Global Colors for native blocks', i18n ),
__( 'Inherit Block Styles from theme.json', i18n ),
__( 'Use v3.16.0 Color Scheme Inheritance', i18n ),
__( 'Block Defaults (Sunsetted)', i18n ),
],
},
{
id: 'migration-settings',
children: [
__( 'Load version 2 blocks in the editor', i18n ),
__( 'Load version 2 blocks in the editor only when the page was using version 2 blocks', i18n ),
__( 'Load version 2 frontend block stylesheet and scripts for backward compatibility', i18n ),
],
},
],
},
{
id: 'v2-settings',
label: __( 'V2 Settings', i18n ),
groups: [
{
id: 'optimizations',
children: [
__( 'Frontend JS & CSS Files', i18n ),
],
},
{
id: 'blocks',
children: Object.values( blockData ).map( block => block.title ),
},
],
},
]
const DERIVED_BLOCKS = getAllBlocks()
// An object containing all the blocks and their required children
const REQUIRED_BLOCKS = BLOCK_CATEROGIES.reduce( ( acc, { id } ) => {
DERIVED_BLOCKS[ id ].forEach( block => {
acc[ block.name ] = block[ 'stk-required-blocks' ] ?? []
} )
return acc
}, {} )
const getChildrenBlocks = blockname => {
return REQUIRED_BLOCKS[ blockname ] || []
}
const getParentBlocks = blockName => {
const parents = []
for ( const parent in REQUIRED_BLOCKS ) {
if ( REQUIRED_BLOCKS[ parent ].includes( blockName ) ) {
parents.push( parent )
}
}
return parents
}
export const getBlockTitle = name => {
for ( const category in DERIVED_BLOCKS ) {
for ( const block of DERIVED_BLOCKS[ category ] ) {
if ( block.name === name ) {
return block.title
}
}
}
return name
}
const BlockList = () => {
const DERIVED_BLOCKS = getAllBlocks()
return (
<>
{ BLOCK_CATEROGIES.map( ( { id, label } ) => {
return (
<div className="s-getting-started-blocks-wrapper" key={ id }>
<h3>{ label }</h3>
<div className="s-getting-started-blocks">
{ DERIVED_BLOCKS[ id ].map( ( block, i ) => {
return (
<div
key={ i }
className="s-box"
>
<h4>{
// eslint-disable-next-line @wordpress/i18n-no-variables
__( block.title, i18n )
}</h4>
<p>{
// eslint-disable-next-line @wordpress/i18n-no-variables
__( block.description, i18n )
}</p>
{ block[ 'stk-demo' ] && <a href={ block[ 'stk-demo' ] } target="_example">{ __( 'See example', i18n ) }</a> }
</div>
)
} ) }
</div>
</div>
)
} ) }
</>
)
}
// Create an admin notice if there's an error fetching the settings.
const RestSettingsNotice = () => {
const [ error, setError ] = useState( null )
useEffect( () => {
fetchSettings().catch( error => {
setError( error )
} )
}, [] )
if ( ! error ) {
return null
}
return (
<div className="notice notice-error">
<p>{ __( 'Error getting Stackable settings. We got the following error. Please contact your administrator.', i18n ) }</p>
{ error.responseJSON &&
<p><strong>{ error.responseJSON.data.status } ({ error.responseJSON.code }).</strong> { error.responseJSON.message } </p>
}
</div>
)
}
// Confirmation dialog when disabling a block that is dependent on another block.
const ToggleBlockDialog = ( {
blockName = '',
blockList,
isDisabled,
onConfirm,
onCancel,
hideHeader = false,
customText = '',
} ) => {
const blockTitle = getBlockTitle( blockName )
return (
<Modal
className="s-confirm-modal"
size="medium"
title={ isDisabled
? sprintf( __( 'Disable %s block?', i18n ), blockTitle )
: sprintf( __( 'Enable %s block?', i18n ), blockTitle ) }
onRequestClose={ onCancel }
__experimentalHideHeader={ hideHeader }
>
{ hideHeader ? customText
: ( isDisabled
? <p>{ __( 'Disabling this block will also disable these blocks that require this block to function:', i18n ) }</p> // eslint-disable-line @wordpress/i18n-no-variables
: <p>{ __( 'Enabling this block will also enable these blocks that are needed for this block to function:', i18n ) }</p> // eslint-disable-line @wordpress/i18n-no-variables
)
}
<ul>
{ blockList.map( ( block, i ) => (
<li key={ i }>{ getBlockTitle( block ) }</li>
) ) }
</ul>
<Flex
justify="flex-end"
expanded={ false }
>
<FlexItem>
<Button
variant="secondary"
onClick={ onCancel }
>
{ __( 'Cancel', i18n ) }
</Button>
</FlexItem>
<FlexItem>
<Button
variant="primary"
onClick={ onConfirm }
>
{ isDisabled
? __( 'Disable', i18n )
: __( 'Enable', i18n )
}
</Button>
</FlexItem>
</Flex>
</Modal>
)
}
// Side navigation with the save changes button and search on tabs
const Sidenav = ( {
currentTab,
handleTabChange,
hasUnsavedChanges,
handleSettingsSave,
currentSearch,
filteredSearchTree,
isSaving,
isRecentlySaved,
hasV2Tab,
} ) => {
const saveButtonClasses = classnames( [
's-save-changes',
{ 's-button-has-unsaved-changes': hasUnsavedChanges && ! isRecentlySaved },
] )
return (
<>
<nav className="s-sidenav">
<div>
{ filteredSearchTree.map( ( {
id,
label,
groups,
} ) => {
const isSearched = currentSearch && groups.some( group => group.children
.length > 0 )
const tabClasses = classnames( [
's-sidenav-item',
{ 's-sidenav-item-highlight': isSearched },
{ 's-active': currentTab === id },
] )
if ( id === 'v2-settings' && ! hasV2Tab ) {
return null
}
return ( <button
key={ id }
className={ tabClasses }
onClick={ () => handleTabChange( id ) }
onKeyDown={ () => handleTabChange( id ) }
role="tab"
tabIndex={ 0 }
id={ `stk-tab__${ id }` }
>
{ label }
</button>
)
} ) }
<div className="s-save-changes-wrapper">
{ currentTab !== 'v2-settings' &&
<>
<div className="s-save-changes-inner-wrapper">
{ hasUnsavedChanges && <span className="s-save-changes-note">{ __( 'There are unsaved changes', i18n ) }</span> }
<button
className={ saveButtonClasses }
onClick={ handleSettingsSave }
disabled={ isRecentlySaved }
>
{ isSaving ? (
<Spinner />
) : isRecentlySaved ? (
__( 'Saved Succesfully!', i18n )
) : (
__( 'Save Changes', i18n )
) }
</button>
</div>
</>
}
</div>
</div>
</nav>
</>
)
}
const Searchbar = ( { currentSearch, handleSearchChange } ) => {
const handleSearch = e => {
handleSearchChange( e.target.value.toLowerCase() )
}
return (
<div className="s-search-setting">
<input
className="s-search-setting__input"
type="search"
placeholder={ __( 'Search settings', i18n ) }
value={ currentSearch }
onChange={ handleSearch }
/>
</div>
)
}
// Main settings component
const Settings = () => {
const [ settings, setSettings ] = useState( {} )
const [ unsavedChanges, setUnsavedChanges ] = useState( {} )
const [ currentTab, setCurrentTab ] = useState( 'editor-settings' )
const [ currentSearch, setCurrentSearch ] = useState( '' )
const [ isSaving, setIsSaving ] = useState( false )
const [ isFetching, setIsFetching ] = useState( false )
const [ isRecentlySaved, setIsRecentlySaved ] = useState( false )
const [ hasV2Tab, setHasV2Tab ] = useState( false )
const hasV2Compatibility = currentSettings => {
return currentSettings.stackable_v2_frontend_compatibility === '1' ||
currentSettings.stackable_v2_editor_compatibility === '1' ||
currentSettings.stackable_v2_editor_compatibility_usage === '1'
}
const handleSettingsChange = useCallback( newSettings => {
setSettings( prev => ( { ...prev, ...newSettings } ) )
setUnsavedChanges( prev => ( { ...prev, ...newSettings } ) )
}, [] )
const handleSettingsSave = useCallback( () => {
if ( Object.keys( unsavedChanges ).length === 0 ) {
return
}
setIsSaving( true )
setIsRecentlySaved( true )
const model = new models.Settings( unsavedChanges )
model.save().then( () => {
// Add a little more time for the spinner for better feedback
setTimeout( () => {
setIsSaving( false )
}, 500 )
setTimeout( () => {
setIsRecentlySaved( false )
}, 1500 )
} )
setUnsavedChanges( {} )
}, [ unsavedChanges, settings ] )
useEffect( () => {
setIsFetching( true )
fetchSettings().then( response => {
setSettings( response )
// Should only be set initially since we have to reload after setting for it to work with the backend
setHasV2Tab( hasV2Compatibility( response ) )
setIsFetching( false )
} )
}, [] )
// However, when disabling V2 blocks, update the settings page to disallow further configuration
useEffect( () => {
if ( ! hasV2Compatibility( settings ) ) {
setHasV2Tab( false )
}
}, [ settings ] )
const hasUnsavedChanges = useMemo( () => Object.keys( unsavedChanges ).length > 0, [ unsavedChanges ] )
useEffect( () => {
const handleBeforeUnload = event => {
if ( hasUnsavedChanges ) {
event.preventDefault()
// Most browsers ignore the custom message, but returning a value triggers the dialog
// https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent/returnValue
event.returnValue = true
}
}
window.addEventListener( 'beforeunload', handleBeforeUnload )
return () => {
window.removeEventListener( 'beforeunload', handleBeforeUnload )
}
}, [ hasUnsavedChanges ] )
const filteredSearchTree = useMemo( () => {
const loweredSearch = currentSearch.toLowerCase()
return SEARCH_TREE.map( tabs => {
const filtedGroups = tabs.groups.map( group => {
if ( ! currentSearch ) {
return { ...group, children: null }
}
const filteredChildren = group.children.filter( child => {
return child.toLowerCase().includes( loweredSearch.toLowerCase() )
} )
return { ...group, children: filteredChildren }
} )
return { ...tabs, groups: filtedGroups }
} )
}, [ currentSearch ] )
const props = {
settings,
handleSettingsChange,
currentSearch,
filteredSearchTree,
currentTab,
isFetching,
}
return <>
<Sidenav
currentTab={ currentTab }
handleTabChange={ setCurrentTab }
hasUnsavedChanges={ hasUnsavedChanges }
handleSettingsSave={ handleSettingsSave }
currentSearch={ currentSearch }
filteredSearchTree={ filteredSearchTree }
isSaving={ isSaving }
isRecentlySaved={ isRecentlySaved }
hasV2Tab={ hasV2Tab }
/>
<article className="s-box" id={ currentTab }>
<Searchbar currentSearch={ currentSearch } handleSearchChange={ setCurrentSearch } />
{ currentTab === 'editor-settings' && <EditorSettings { ...props } /> }
{ currentTab === 'responsiveness' && <Responsiveness { ...props } /> }
{ currentTab === 'blocks' && <Blocks { ...props } /> }
{ currentTab === 'optimizations' && <Optimizations { ...props } /> }
{ currentTab === 'global-settings' && <GlobalSettings { ...props } /> }
{ currentTab === 'role-manager' && <RoleManager { ...props } /> }
{ currentTab === 'custom-fields-settings' && <CustomFields { ...props } /> }
{ currentTab === 'integrations' && <Integrations { ...props } /> }
{ currentTab === 'other-settings' && <AdditionalOptions { ...props } /> }
{ /* Render the V2 settings and show/hide via CSS */ }
<V2Settings { ...props } />
</article>
</>
}
const EditorSettings = props => {
const {
settings,
handleSettingsChange,
filteredSearchTree,
} = props
const groups = filteredSearchTree.find( tab => tab.id === 'editor-settings' ).groups
const blocks = groups.find( group => group.id === 'blocks' )
const editor = groups.find( group => group.id === 'editor' )
const toolbar = groups.find( group => group.id === 'toolbar' )
const inspector = groups.find( group => group.id === 'inspector' )
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
return (
<div className="s-editor-settings">
{ ! hasGroupMatch ? (
<h3>{ __( 'No matching settings', i18n ) }</h3>
) : (
<>
{ ( blocks.children === null || blocks.children.length > 0 ) &&
<div className="s-setting-group">
<h2>{ __( 'Block Widths', i18n ) }</h2>
<p className="s-settings-subtitle">{ __( 'Adjust the width of Stackable blocks here.', i18n ) }</p>
<AdminTextSetting
label={ __( 'Nested Block Width', i18n ) }
searchedSettings={ blocks.children }
value={ settings.stackable_block_default_width }
type="text"
onChange={ value => {
handleSettingsChange( { stackable_block_default_width: value } ) // eslint-disable-line camelcase
} }
help={ __( 'The width used when a Columns block has its Content Width set to center. This is automatically detected from your theme. You can adjust it if your blocks are not aligned correctly. In px, you can also use other units or use a calc() formula.', i18n ) }
/>
<AdminTextSetting
label={ __( 'Nested Wide Block Width', i18n ) }
searchedSettings={ blocks.children }
value={ settings.stackable_block_wide_width }
type="text"
onChange={ value => {
handleSettingsChange( { stackable_block_wide_width: value } ) // eslint-disable-line camelcase
} }
help={ __( 'The width used when a Columns block has its Content Width set to wide. This is automatically detected from your theme. You can adjust it if your blocks are not aligned correctly. In px, you can also use other units or use a calc() formula.', i18n ) }
/>
</div>
}
{ ( editor.children === null || editor.children.length > 0 ) &&
<div className="s-setting-group">
<h2>{ __( 'Editor', i18n ) }</h2>
<p className="s-settings-subtitle">{ __( 'You can customize some of the features and behavior of Stackable in the editor here.', i18n ) } </p>
<AdminToggleSetting
label={ __( 'Stackable Text as Default Block', i18n ) }
searchedSettings={ editor.children }
value={ settings.stackable_enable_text_default_block }
onChange={ value => {
handleSettingsChange( { stackable_enable_text_default_block: value } ) // eslint-disable-line camelcase
} }
help={ __( 'If enabled, Stackable Text blocks will be added by default instead of the native Paragraph Block.', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Design Library', i18n ) }
searchedSettings={ editor.children }
value={ settings.stackable_enable_design_library }
onChange={ value => {
handleSettingsChange( { stackable_enable_design_library: value } ) // eslint-disable-line camelcase
} }
help={ __( 'Adds a button on the top of the editor which gives access to a collection of pre-made block designs. Note: You can still access the Design Library by adding the Design Library block.', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Stackable Settings', i18n ) }
searchedSettings={ editor.children }
value={ settings.stackable_enable_global_settings }
onChange={ value => {
handleSettingsChange( { stackable_enable_global_settings: value } ) // eslint-disable-line camelcase
} }
help={ __( 'Adds a button on the top of the editor which gives access to Stackable settings. Note: You won\'t be able to access Stackable settings when this is disabled.', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Use Size Presets by Default', i18n ) }
searchedSettings={ editor.children }
value={ props.settings.stackable_use_size_presets_by_default }
onChange={ value => {
props.handleSettingsChange( { stackable_use_size_presets_by_default: value } ) // eslint-disable-line camelcase
} }
help={ __( 'If enabled, range controls will be on preset mode by default.', i18n ) }
disabled={ __( 'Use custom values', i18n ) }
enabled={ __( 'Use presets', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Block Linking (Beta)', i18n ) }
searchedSettings={ editor.children }
value={ settings.stackable_enable_block_linking }
onChange={ value => {
handleSettingsChange( { stackable_enable_block_linking: value } ) // eslint-disable-line camelcase
} }
help={
<>
{ __( 'Gives you the ability to link columns. Any changes you make on one column will automatically get applied on the other columns.', i18n ) }
<a target="_docs" href="https://docs.wpstackable.com/article/452-how-to-use-block-linking">{ __( 'Learn more', i18n ) }</a>
</>
}
/>
</div>
}
{ ( toolbar.children === null || toolbar.children.length > 0 ) &&
<div className="s-setting-group">
<h2>{ __( 'Toolbar', i18n ) }</h2>
<p className="s-settings-subtitle">{ __( 'You can disable some toolbar features here.', i18n ) } </p>
<AdminToggleSetting
label={ __( 'Toolbar Text Highlight', i18n ) }
searchedSettings={ toolbar.children }
value={ settings.stackable_enable_text_highlight }
onChange={ value => {
handleSettingsChange( { stackable_enable_text_highlight: value } ) // eslint-disable-line camelcase
} }
help={ __( 'Adds a toolbar button for highlighting text', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Toolbar Dynamic Content', i18n ) }
searchedSettings={ toolbar.children }
value={ settings.stackable_enable_dynamic_content }
onChange={ value => {
handleSettingsChange( { stackable_enable_dynamic_content: value } ) // eslint-disable-line camelcase
} }
help={ __( 'Adds a toolbar button for inserting and modifying dynamic content', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Copy & Paste Styles', i18n ) }
searchedSettings={ toolbar.children }
value={ settings.stackable_enable_copy_paste_styles }
onChange={ value => {
handleSettingsChange( { stackable_enable_copy_paste_styles: value } ) // eslint-disable-line camelcase
} }
help={ __( 'Adds a toolbar button for advanced copying and pasting block styles', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Reset Layout', i18n ) }
searchedSettings={ toolbar.children }
value={ settings.stackable_enable_reset_layout }
onChange={ value => {
handleSettingsChange( { stackable_enable_reset_layout: value } ) // eslint-disable-line camelcase
} }
help={ __( 'Adds a toolbar button for resetting the layout of a stackble block back to the original', i18n ) }
/>
{ settings.stackable_enable_block_defaults && <AdminToggleSetting
label={ __( 'Save as Default Block', i18n ) }
searchedSettings={ toolbar.children }
value={ settings.stackable_enable_save_as_default_block }
onChange={ value => {
handleSettingsChange( { stackable_enable_save_as_default_block: value } ) // eslint-disable-line
} }
help={ __( 'Adds a toolbar button for saving a block as the default block', i18n ) }
/> }
</div>
}
{ ( inspector.children === null || inspector.children.length > 0 ) &&
<div className="s-setting-group">
<h2>{ __( 'Inspector', i18n ) }</h2>
<p className="s-settings-subtitle">{ __( 'You can customize some of the features and behavior of Stackable in the inspector here.', i18n ) }</p>
<AdminToggleSetting
label={ __( 'Don\'t show help video tooltips', i18n ) }
searchedSettings={ inspector.children }
value={ settings.stackable_help_tooltip_disabled === '1' }
onChange={ value => {
handleSettingsChange( { stackable_help_tooltip_disabled: value ? '1' : '' } ) // eslint-disable-line camelcase
} }
help={ __( 'Disables the help video tooltips that appear in the inspector.', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Auto-Collapse Panels', i18n ) }
searchedSettings={ inspector.children }
value={ settings.stackable_auto_collapse_panels }
onChange={ value => {
handleSettingsChange( { stackable_auto_collapse_panels: value } ) // eslint-disable-line camelcase
} }
help={ __( 'Collapse other inspector panels when opening another, keeping only one open at a time.', i18n ) }
/>
</div>
}
</>
) }
</div>
)
}
const Responsiveness = props => {
const {
settings,
handleSettingsChange,
filteredSearchTree,
} = props
const groups = filteredSearchTree.find( tab => tab.id === 'responsiveness' ).groups
const dynamicBreakpoints = groups.find( group => group.id === 'dynamic-breakpoints' )
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
return (
<div className="s-responsiveness">
{ ! hasGroupMatch ? (
<h3>{ __( 'No matching settings', i18n ) }</h3>
) : (
<>
{ ( dynamicBreakpoints.children === null || dynamicBreakpoints.children.length > 0 ) &&
<div className="s-setting-group">
<h2>{ __( 'Dynamic Breakpoints', i18n ) }</h2>
<p className="s-settings-subtitle">
{ __( 'Blocks can be styles differently for tablet and mobile screens, and some styles adjust to make them fit better in smaller screens. You can change the widths when tablet and mobile views are triggered. ', i18n ) }
<a href="https://docs.wpstackable.com/article/464-how-to-use-dynamic-breakpoints?utm_source=wp-settings-global-settings&utm_campaign=learnmore&utm_medium=wp-dashboard" target="_docs">
{ __( 'Learn more', i18n ) }
</a>
</p>
<AdminTextSetting
label={ __( 'Tablet Breakpoint', i18n ) }
searchedSettings={ dynamicBreakpoints.children }
type="number"
value={ settings.stackable_dynamic_breakpoints?.tablet || '' } // eslint-disable-line camelcase
onChange={ value => {
handleSettingsChange( {
stackable_dynamic_breakpoints: { // eslint-disable-line camelcase
tablet: value,
mobile: settings.stackable_dynamic_breakpoints?.mobile || '', // eslint-disable-line camelcase
},
} )
} }
placeholder={ defaultBreakpoints.tablet || '1024' }
> px</AdminTextSetting>
<AdminTextSetting
label={ __( 'Mobile Breakpoint', i18n ) }
searchedSettings={ dynamicBreakpoints.children }
type="number"
value={ settings.stackable_dynamic_breakpoints?.mobile || '' } // eslint-disable-line camelcase
onChange={ value => {
handleSettingsChange( {
stackable_dynamic_breakpoints: { // eslint-disable-line camelcase
tablet: settings.stackable_dynamic_breakpoints?.tablet || '', // eslint-disable-line camelcase
mobile: value,
},
} )
} }
placeholder={ defaultBreakpoints.mobile || '768' }
> px</AdminTextSetting>
</div>
}
</>
) }
</div>
)
}
// Toggle the block states between enabled, disabled and hidden.
// Enabled blocks are not stored in the settings object.
const Blocks = props => {
const {
settings,
handleSettingsChange,
filteredSearchTree,
isFetching,
} = props
const BLOCK_STATE_MAP = Object.freeze( {
enabled: BLOCK_STATE.ENABLED,
hidden: BLOCK_STATE.HIDDEN,
disabled: BLOCK_STATE.DISABLED,
} )
const DERIVED_BLOCKS = getAllBlocks()
const groups = filteredSearchTree.find( tab => tab.id === 'blocks' ).groups
const hasGroupMatch = groups.some( group => group.children === null || group.children.length > 0 )
const disabledBlocks = settings.stackable_block_states ?? {} // eslint-disable-line camelcase
const [ isDisabledDialogOpen, setIsDisabledDialogOpen ] = useState( false )
const [ isEnabledDialogOpen, setIsEnabledDialogOpen ] = useState( false )
const [ currentToggleBlock, setCurrentToggleBlock ] = useState( '' )
const [ currentToggleBlockList, setCurrentToggleBlockList ] = useState( [] )
const [ blocksToEnable, setBlocksToEnable ] = useState( [] )
useEffect( () => {
// eslint-disable-next-line @wordpress/no-global-event-listener
window.addEventListener( 'message', ev => {
if ( typeof ev.data !== 'object' || ev.data.source !== 'STK_DESIGN_LIBRARY' || ev.origin !== window.location.origin ) {
return
}
if ( ev.data.type === 'STK_ENABLE_BLOCKS' && ev.data.blocks instanceof Set && ev.data.blocks.size ) {
const blocks = [ ...ev.data.blocks ].map( block => {
if ( block in BLOCK_DEPENDENCIES ) {
return BLOCK_DEPENDENCIES[ block ]
}
return block
} )
setBlocksToEnable( blocks )
}
} )
}, [] )
useEffect( () => {
if ( ! isFetching && blocksToEnable.length ) {
setIsEnabledDialogOpen( true )
}
}, [ isFetching, blocksToEnable ] )
// Map string states to integer block states
const mapStringStates = states => {
return states?.map(
state => BLOCK_STATE_MAP[ state.toLowerCase() ]
)
}
const enableAllBlocks = () => {
const newDisabledBlocks = {}
BLOCK_CATEROGIES.forEach( ( { id } ) => {
DERIVED_BLOCKS[ id ].forEach( block => {
const availableStates = mapStringStates( block[ 'stk-available-states' ] )
// Retain previous state if cannot be enabled
if ( availableStates && ! availableStates.includes( BLOCK_STATE.ENABLED ) && disabledBlocks[ block.name ] ) {
newDisabledBlocks[ block.name ] = disabledBlocks[ block.name ]
}
} )
} )
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}
const disableAllBlocks = () => {
const newDisabledBlocks = {}
BLOCK_CATEROGIES.forEach( ( { id } ) => {
DERIVED_BLOCKS[ id ].forEach( block => {
const availableStates = mapStringStates( block[ 'stk-available-states' ] )
if ( ! availableStates || availableStates.includes( BLOCK_STATE.DISABLED ) ) {
newDisabledBlocks[ block.name ] = BLOCK_STATE.DISABLED
} else if ( availableStates.includes( BLOCK_STATE.HIDDEN ) ) { // If the block cannot be disabled, default to hidden.
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
} else if ( disabledBlocks[ block.name ] ) { // Retain previous state if cannot be disabled or hidden
newDisabledBlocks[ block.name ] = disabledBlocks[ block.name ]
}
} )
} )
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}
const hideAllBlocks = () => {
const newDisabledBlocks = {}
BLOCK_CATEROGIES.forEach( ( { id } ) => {
DERIVED_BLOCKS[ id ].forEach( block => {
const availableStates = mapStringStates( block[ 'stk-available-states' ] )
if ( ! availableStates || availableStates.includes( BLOCK_STATE.HIDDEN ) ) {
newDisabledBlocks[ block.name ] = BLOCK_STATE.HIDDEN
} else if ( disabledBlocks[ block.name ] ) { // Retain previous state if cannot be hidden
newDisabledBlocks[ block.name ] = disabledBlocks[ block.name ]
}
} )
} )
handleSettingsChange( { stackable_block_states: newDisabledBlocks } ) // eslint-disable-line camelcase
}
const toggleBlock = ( name, value ) => {
const valueInt = Number( value )
let newDisabledBlocks = { ...disabledBlocks }
setCurrentToggleBlock( name )
// Check if a parent is being enabled
if ( valueInt === BLOCK_STATE.ENABLED ) {
// Get the parent's disabled/hidden children and confirm if they will also be enabled
const childrenBlocks = getChildrenBlocks( name ).filter( block => block in disabledBlocks )
if ( childrenBlocks.length > 0 ) {
setCurrentToggleBlockList( childrenBlocks )
setIsEnabledDialogOpen( true )
} else {
delete newDisabledBlocks[ name ]
}
} else if ( valueInt === BLOCK_STATE.DISABLED ) { // Check if a child is being disabled
// Get the child's enabled parents and confirm if they will also be disabled
const parentBlocks = getParentBlocks( name ).filter( block => ! ( block in disabledBlocks ) )
if ( parentBlocks.length > 0 ) {
setCurrentToggleBlockList( parentBlocks )