Skip to content

Commit f2eccc3

Browse files
authored
Merge pull request #3676 from gambitph/feat/3673/3674-block-defaults
feat: add block defaults for heading theme margin and icon for icon l…
2 parents 5aa0554 + cfb7275 commit f2eccc3

13 files changed

Lines changed: 227 additions & 4 deletions

File tree

src/block/heading/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import metadata from './block.json'
1717
import example from './example'
1818
import deprecated from './deprecated'
1919
import substitute from './substitute'
20+
import variations from './variations'
2021

2122
export const settings = {
2223
...metadata,
@@ -33,6 +34,7 @@ export const settings = {
3334
save,
3435
example,
3536
transforms,
37+
variations,
3638
merge( attributes, attributesToMerge ) {
3739
// Make sure that the selection is always at the end of the text.
3840
// @see https://github.com/WordPress/gutenberg/blob/3da717b8d0ac7d7821fc6d0475695ccf3ae2829f/packages/block-library/src/paragraph/index.js

src/block/heading/transforms.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
*/
44
import { createBlock, createBlocksFromInnerBlocksTemplate } from '@wordpress/blocks'
55

6+
/**
7+
* External dependencies
8+
*/
9+
import { settings } from 'stackable'
10+
611
/**
712
* Internal dependencies
813
*/
914
import { TEMPLATE as ICON_LABEL_TEMPLATE } from '../icon-label/edit'
15+
import { getHeadingDefaultAttributes } from './util'
1016

1117
const transforms = {
1218
from: [
@@ -15,15 +21,21 @@ const transforms = {
1521
isMultiBlock: true,
1622
blocks: [ 'stackable/text' ],
1723
transform: attributes => {
18-
return attributes.map( ( { ...attrs } ) => createBlock( 'stackable/heading', { ...attrs } ) )
24+
return attributes.map( ( { ...attrs } ) => createBlock( 'stackable/heading', {
25+
...getHeadingDefaultAttributes( settings ),
26+
...attrs,
27+
} ) )
1928
},
2029
},
2130
{
2231
type: 'block',
2332
isMultiBlock: true,
2433
blocks: [ 'stackable/subtitle' ],
2534
transform: attributes => {
26-
return attributes.map( ( { ...attrs } ) => createBlock( 'stackable/heading', { ...attrs } ) )
35+
return attributes.map( ( { ...attrs } ) => createBlock( 'stackable/heading', {
36+
...getHeadingDefaultAttributes( settings ),
37+
...attrs,
38+
} ) )
2739
},
2840
},
2941
{
@@ -32,6 +44,7 @@ const transforms = {
3244
blocks: [ 'core/paragraph' ],
3345
transform: attributes => {
3446
return attributes.map( ( { content } ) => createBlock( 'stackable/heading', {
47+
...getHeadingDefaultAttributes( settings ),
3548
text: content,
3649
} ) )
3750
},
@@ -42,6 +55,7 @@ const transforms = {
4255
blocks: [ 'core/heading' ],
4356
transform: attributes => {
4457
return attributes.map( ( { content } ) => createBlock( 'stackable/heading', {
58+
...getHeadingDefaultAttributes( settings ),
4559
text: content,
4660
} ) )
4761
},

src/block/heading/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This helper is called only by the inserter variation and explicit block
2+
// transforms. Parsed existing blocks and Design Library blocks do not call it.
3+
export const getHeadingDefaultAttributes = ( editorSettings = {} ) => {
4+
const isPost = editorSettings.stackable_current_post_type === 'post'
5+
6+
return {
7+
// Posts and non-post editor screens have separate admin defaults.
8+
useThemeTextMargins: isPost
9+
? !! editorSettings.stackable_enable_heading_default_theme_margins_posts
10+
: !! editorSettings.stackable_enable_heading_default_theme_margins_non_posts,
11+
}
12+
}

src/block/heading/variations.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { settings } from 'stackable'
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import { getHeadingDefaultAttributes } from './util'
10+
11+
// Scope this variation to direct inserter creation.
12+
export const getHeadingVariations = ( editorSettings = {} ) => [
13+
{
14+
name: 'default',
15+
isDefault: true,
16+
scope: [ 'inserter' ],
17+
attributes: getHeadingDefaultAttributes( editorSettings ),
18+
},
19+
]
20+
21+
export default getHeadingVariations( settings )

src/block/icon-list/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import schema from './schema'
1313
import example from './example'
1414
import deprecated from './deprecated'
1515
import transforms from './transforms'
16+
import variations from './variations'
1617

1718
/**
1819
* WordPress dependencies
@@ -35,4 +36,5 @@ export const settings = {
3536
edit,
3637
save,
3738
transforms,
39+
variations,
3840
}

src/block/icon-list/transforms.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import {
66
create, split, toHTMLString,
77
} from '@wordpress/rich-text'
88

9+
/**
10+
* External dependencies
11+
*/
12+
import { settings } from 'stackable'
13+
914
/**
1015
* Internal dependencies
1116
*/
17+
import { getIconListDefaultAttributes } from './util'
1218

1319
// import { createListBlockFromDOMElement } from './util'
1420

@@ -69,6 +75,7 @@ const transforms = {
6975
return createBlock(
7076
'stackable/icon-list',
7177
{
78+
...getIconListDefaultAttributes( settings ),
7279
anchor: blockAttributes.anchor,
7380
},
7481
childBlocks
@@ -85,6 +92,7 @@ const transforms = {
8592
return createBlock(
8693
'stackable/icon-list',
8794
{
95+
...getIconListDefaultAttributes( settings ),
8896
anchor: blockAttributes.anchor,
8997
},
9098
childBlocks

src/block/icon-list/util.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import { i18n } from 'stackable'
1212
// The default icon list SVG.
1313
export const DEFAULT_SVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 190 190"><polygon points="173.8,28.4 60.4,141.8 15.7,97.2 5.1,107.8 60.4,163 184.4,39 173.8,28.4"/></svg>'
1414

15+
// This helper is called only by the inserter variation and explicit block
16+
// transforms. Parsed existing blocks and Design Library blocks do not call it.
17+
export const getIconListDefaultAttributes = ( editorSettings = {} ) => ( {
18+
// Preserve the original schema icon when no custom admin default is set.
19+
icon: editorSettings.stackable_icon_list_block_default_icon || DEFAULT_SVG,
20+
} )
21+
1522
export const IconSvgDef = props => {
1623
const { icon, uniqueId } = props
1724

@@ -122,4 +129,3 @@ export function migrateTypeToInlineStyle( attributes ) {
122129

123130
return attributes
124131
}
125-

src/block/icon-list/variations.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { settings } from 'stackable'
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import { getIconListDefaultAttributes } from './util'
10+
11+
// Scope this variation to direct inserter creation.
12+
export const getIconListVariations = ( editorSettings = {} ) => [
13+
{
14+
name: 'default',
15+
isDefault: true,
16+
scope: [ 'inserter' ],
17+
attributes: getIconListDefaultAttributes( editorSettings ),
18+
},
19+
]
20+
21+
export default getIconListVariations( settings )
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.ugb-admin-icon-setting {
2+
--wp-components-color-accent: var(--stk-skin-dark);
3+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import AdminBaseSetting from '../admin-base-setting'
2+
import IconControl from '../icon-control'
3+
import classnames from 'classnames'
4+
5+
const defaultIcon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 190 190"><polygon points="173.8,28.4 60.4,141.8 15.7,97.2 5.1,107.8 60.4,163 184.4,39 173.8,28.4"/></svg>'
6+
7+
const AdminIconSetting = props => {
8+
return (
9+
<AdminBaseSetting
10+
{ ...props }
11+
className={ classnames( props.className, 'ugb-admin-icon-setting' ) }>
12+
<IconControl
13+
label=""
14+
value={ props.value }
15+
defaultValue={ defaultIcon }
16+
onChange={ icon => {
17+
props.onChange( icon )
18+
} }
19+
allowReset={ false }
20+
hasPanelModifiedIndicator={ true }
21+
/>
22+
{ props.children }
23+
</AdminBaseSetting>
24+
)
25+
}
26+
27+
AdminIconSetting.defaultProps = {
28+
value: '',
29+
onChange: () => {},
30+
}
31+
32+
export default AdminIconSetting

0 commit comments

Comments
 (0)