/
timeline-item.js
143 lines (131 loc) · 3.78 KB
/
timeline-item.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
/**
* External dependencies
*/
import {
InspectorControls,
InnerBlocks,
PanelColorSettings,
BlockControls,
} from '@wordpress/block-editor';
import { registerBlockType } from '@wordpress/blocks';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { positionLeft, positionRight } from '@wordpress/icons';
import classnames from 'classnames';
/**
* Internal dependencies
*/
import { TimelineIcon } from './icon';
function Controls( { alignment, clientId, toggleAlignment } ) {
const parentIsAlternating = useSelect( ( select ) => {
const parentIds = select( 'core/block-editor' ).getBlockParents( clientId );
const parent = select( 'core/block-editor' ).getBlock( parentIds[ 0 ] );
return parent?.attributes?.isAlternating;
} );
if ( parentIsAlternating === false ) {
return null;
}
return (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
onClick={ () => toggleAlignment( 'left' ) }
isActive={ alignment === 'left' }
icon={ positionLeft }
title={ __( 'Left', 'full-site-editing' ) }
/>
<ToolbarButton
onClick={ () => toggleAlignment( 'right' ) }
isActive={ alignment === 'right' }
icon={ positionRight }
title={ __( 'Right', 'full-site-editing' ) }
/>
</ToolbarGroup>
</BlockControls>
);
}
export function registerTimelineItemBlock() {
registerBlockType( 'jetpack/timeline-item', {
title: __( 'Timeline Entry', 'full-site-editing' ),
description: __( 'An entry on the timeline', 'full-site-editing' ),
icon: TimelineIcon,
category: 'widgets',
parent: [ 'jetpack/timeline' ],
edit: ( { attributes, clientId, setAttributes } ) => {
const style = {
backgroundColor: attributes.background,
};
const bubbleStyle = {
borderColor: attributes.background,
};
const toggleAlignment = ( alignment ) => {
const newAlignment = alignment === attributes.alignment ? 'auto' : alignment;
setAttributes( { alignment: newAlignment } );
};
const classes = classnames( 'wp-block-jetpack-timeline-item', {
'is-left': attributes.alignment === 'left',
'is-right': attributes.alignment === 'right',
} );
return (
<>
<Controls
alignment={ attributes.alignment }
clientId={ clientId }
toggleAlignment={ toggleAlignment }
/>
<li style={ style } className={ classes }>
<InspectorControls>
<PanelColorSettings
title={ __( 'Color Settings', 'full-site-editing' ) }
colorSettings={ [
{
value: attributes.background,
onChange: ( background ) => setAttributes( { background } ),
label: __( 'Background Color', 'full-site-editing' ),
},
] }
/>
</InspectorControls>
<div className="timeline-item">
<div className="timeline-item__bubble" style={ bubbleStyle } />
<div className="timeline-item__dot" style={ style } />
<InnerBlocks template={ [ [ 'core/paragraph' ] ] } />
</div>
</li>
</>
);
},
save: ( { attributes } ) => {
const classes = classnames( {
'is-left': attributes.alignment === 'left',
'is-right': attributes.alignment === 'right',
} );
const style = {
backgroundColor: attributes.background,
};
const bubbleStyle = {
borderColor: attributes.background,
};
return (
<li style={ style } className={ classes }>
<div className="timeline-item">
<div className="timeline-item__bubble" style={ bubbleStyle } />
<div className="timeline-item__dot" style={ style } />
<InnerBlocks.Content />
</div>
</li>
);
},
attributes: {
alignment: {
type: 'string',
default: 'auto',
},
background: {
type: 'string',
default: '#eeeeee',
},
},
} );
}