-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathedit.js
More file actions
164 lines (143 loc) · 4.15 KB
/
edit.js
File metadata and controls
164 lines (143 loc) · 4.15 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
/**
* Internal dependencies
*/
import blockStyles from './style'
/**
* External dependencies
*/
import { version as VERSION } from 'stackable'
import classnames from 'classnames'
import { InspectorTabs, useBlockCssGenerator } from '~stackable/components'
import {
BlockDiv,
MarginBottom,
getRowClasses,
Alignment,
getAlignmentClasses,
Advanced,
CustomCSS,
Responsive,
CustomAttributes,
EffectsAnimations,
ConditionalDisplay,
Transform,
} from '~stackable/block-components'
import {
withBlockAttributeContext, withBlockStyleContext,
withBlockWrapperIsHovered, withQueryLoopContext,
} from '~stackable/higher-order'
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose'
import { InnerBlocks } from '@wordpress/block-editor'
import { __ } from '@wordpress/i18n'
import { addFilter } from '@wordpress/hooks'
import { memo } from '@wordpress/element'
import { useSelect } from '@wordpress/data'
export const defaultIcon = '<svg data-prefix="fas" data-icon="play" class="svg-inline--fa fa-play fa-w-14" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z"></path></svg>'
const TEMPLATE = [
[ 'stackable/text', {
text: '$', htmlTag: 'span', innerTextTag: 'span',
} ],
[ 'stackable/text', {
text: '99', htmlTag: 'span', innerTextTag: 'span', className: 'stk-block-price__price',
} ],
[ 'stackable/text', {
text: '.00', htmlTag: 'span', innerTextTag: 'span',
} ],
]
const Edit = props => {
const {
className,
attributes,
} = props
const rowClass = getRowClasses( attributes )
const blockAlignmentClass = getAlignmentClasses( attributes )
const { hasInnerBlocks } = useSelect( select => {
const { getBlockOrder } = select( 'core/block-editor' )
return {
hasInnerBlocks: getBlockOrder( props.clientId ).length > 0,
}
}, [ props.clientId ] )
const blockClassNames = classnames( [
className,
'stk-block-price',
rowClass,
blockAlignmentClass,
] )
// Generate the CSS styles for the block.
const blockCss = useBlockCssGenerator( {
attributes: props.attributes,
blockStyles,
clientId: props.clientId,
context: props.context,
setAttributes: props.setAttributes,
blockState: props.blockState,
version: VERSION,
} )
return (
<>
<InspectorControls />
{ blockCss && <style key="block-css">{ blockCss }</style> }
<CustomCSS mainBlockClass="stk-block-price" />
<BlockDiv
blockHoverClass={ props.blockHoverClass }
clientId={ props.clientId }
attributes={ props.attributes }
className={ blockClassNames }
>
<InnerBlocks
template={ hasInnerBlocks ? undefined : TEMPLATE }
templateLock="all"
/>
</BlockDiv>
{ props.isHovered && <MarginBottom /> }
</>
)
}
const InspectorControls = memo( () => {
return (
<>
<InspectorTabs />
<Alignment.InspectorControls />
<BlockDiv.InspectorControls />
<Advanced.InspectorControls />
<Transform.InspectorControls />
<EffectsAnimations.InspectorControls />
<CustomAttributes.InspectorControls />
<CustomCSS.InspectorControls mainBlockClass="stk-block-price" />
<Responsive.InspectorControls />
<ConditionalDisplay.InspectorControls />
</>
)
} )
export default compose(
withBlockWrapperIsHovered,
withQueryLoopContext,
withBlockAttributeContext,
withBlockStyleContext( blockStyles ),
)( Edit )
// Disable bottom margins for child blocks.
addFilter( 'stackable.edit.margin-bottom.enable-handlers', 'stackable/price', ( enabled, parentBlock ) => {
return parentBlock?.name === 'stackable/price' ? false : enabled
} )
// Disable columns for child text blocks.
addFilter( 'stackable.text.edit.enable-column', 'stackable/price', ( enabled, parentBlock ) => {
return parentBlock?.name === 'stackable/price' ? false : enabled
} )
// Add custom text placeholders
addFilter( 'stackable.text.edit.placeholder', 'stackable/price', ( placeholder, {
parentBlock, isFirstBlock, isLastBlock,
} ) => {
if ( parentBlock?.name !== 'stackable/price' ) {
return placeholder
}
if ( isFirstBlock ) {
return '$'
}
if ( isLastBlock ) {
return '.00'
}
return '100'
} )