-
Notifications
You must be signed in to change notification settings - Fork 2
/
inspector.js
180 lines (161 loc) · 4.3 KB
/
inspector.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
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
/**
* External Dependencies
*/
import { filter } from 'lodash'
/**
* WordPress Dependencies
*/
import { createBlock } from '@wordpress/blocks'
import {
PanelBody,
Button,
IconButton,
BaseControl,
} from '@wordpress/components'
import {
dispatch,
select,
} from '@wordpress/data'
import {
ColorPalette,
InspectorControls,
PanelColorSettings,
} from '@wordpress/editor'
import { Fragment } from '@wordpress/element'
import { __ } from '@wordpress/i18n'
/**
* Internal Dependencies
*/
import canAddColumns from '../utils/can-add-columns'
import getWidthClass from '../utils/get-width-class'
import options from '../data/options'
/**
*
* @param {object} props component props
* @returns {object} Component
*/
const Inspector = (props) => {
const {
attributes: {
columns,
},
backgroundColor,
clientId,
setAttributes,
setBackgroundColor,
setTextColor,
textColor,
} = props
/**
* Add Column
*/
const onAddColumn = () => {
const newColumn = 0
// Create a new block
const block = createBlock('tomodomo/column', {
columnWidth: 0,
className: '',
})
// Insert the block
dispatch('core/editor').insertBlock(block, columns.length, clientId)
// Update the columns attribute
setAttributes({
columns: columns.concat(newColumn),
})
}
/**
* Update Column
*/
const onChangeWidth = (event, colIndex) => {
// Grab the current block
var col = select('core/editor').getBlocksByClientId(clientId)[0].innerBlocks[colIndex]
const updatedColumns = columns.map((column, index) => (
index === colIndex ? Number(event.target.value) : column
))
// Update the correct block's attributes
dispatch('core/editor').updateBlockAttributes(col.clientId, {
columnWidth: updatedColumns[colIndex],
className: getWidthClass(updatedColumns[colIndex]),
})
setAttributes({
columns: updatedColumns,
})
}
/**
* Delete Column
*/
const onDeleteColumn = (colIndex) => {
// Grab all the blocks
var col = select('core/editor').getBlocksByClientId(clientId)[0].innerBlocks[colIndex]
// Remove the nested block
dispatch('core/editor').removeBlock(col.clientId, false)
// Update the attributes in the parent
const updatedColumns = filter(columns, (column, index) => (
colIndex !== index
))
setAttributes({
columns: updatedColumns,
})
}
// Inspector Controls
return (
<InspectorControls>
<PanelBody title={__('Columns Settings')} initialOpen>
{columns.map((column, index) => (
<Fragment key={`column-${index}-width`}>
<div className='columns__column'>
<BaseControl label={`Column ${index + 1}`}>
<select
id={`columns-width-${index}`}
value={column}
className='components-select-control__input'
onChange={event => onChangeWidth(event, index)}
>
{options.columnWidths.map((option, index) => (
<option
key={`${option.label}-${option.label}-${index}`}
value={option.value}
>
{option.label}
</option>
))}
</select>
</BaseControl>
<Button
onClick={() => onDeleteColumn(index)}
className='columns__button--delete button-link-delete'
>
{__('Delete')}
</Button>
</div>
</Fragment>
))}
<IconButton
icon='plus'
className='button columns__button--add'
onClick={onAddColumn}
disabled={!canAddColumns(columns)}
>
{__('Add a Column')}
</IconButton>
</PanelBody>
<PanelColorSettings
title={__('Color Settings')}
initialOpen={false}
colorSettings={[
{
value: textColor.color,
onChange: setTextColor,
label: __('Text Color'),
},
{
value: backgroundColor.color,
onChange: setBackgroundColor,
label: __('Background Color'),
},
]}
/>
</InspectorControls>
)
}
export default Inspector