-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathedit.js
More file actions
159 lines (141 loc) · 5.36 KB
/
edit.js
File metadata and controls
159 lines (141 loc) · 5.36 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
import { justifyCenter, justifyLeft, justifyRight } from '@wordpress/icons'
import { faBrush } from '@fortawesome/free-solid-svg-icons'
import { __ } from '@wordpress/i18n'
import { BlockControls, useBlockProps } from '@wordpress/block-editor'
import { Fragment, useState } from '@wordpress/element'
import { Button, ToolbarDropdownMenu, Modal, Placeholder, ToolbarButton, ToolbarGroup } from '@wordpress/components'
import { get } from 'lodash'
import { GLOBAL_KEY } from '../../admin/src/constants'
import { iconDefinitionFromIconChooserSelectionEvent } from './iconDefinitions'
import { wpIconFromFaIconDefinition } from './icons'
import { computeIconLayerCount, prepareParamsForUseBlock, renderIcon } from './rendering'
import IconModifier from './iconModifier'
import createCustomEvent from './createCustomEvent'
const openIconChooserForAddLayerEvent = createCustomEvent()
const { IconChooserModal } = get(window, [GLOBAL_KEY, 'iconChooser'], {})
const modifyToolbarIcon = wpIconFromFaIconDefinition(faBrush)
const defaultStylingParams = {
spin: false,
transform: null
}
export function Edit(props) {
const { attributes, setAttributes } = props
const [justificationDropdownMenuIcon, setJustificationDropdownMenuIcon] = useState(justifyCenter)
const setJustification = (justification) => {
let menuIcon
if ('left' === justification) {
menuIcon = justifyLeft
} else if ('right' === justification) {
menuIcon = justifyRight
} else {
menuIcon = justifyCenter
}
setJustificationDropdownMenuIcon(menuIcon)
setAttributes({ ...attributes, justification })
}
const prepareHandleSelect = (layerParams) => (event) => {
const iconLayers = attributes?.iconLayers || []
const iconDefinition = iconDefinitionFromIconChooserSelectionEvent(event)
if (!iconDefinition) return
const { replace, append } = layerParams
const newIconLayers = [...iconLayers]
if (append) {
const layer = {
iconDefinition,
...defaultStylingParams
}
newIconLayers.push(layer)
} else if (Number.isInteger(replace) && replace < iconLayers.length) {
const oldLayer = iconLayers[replace] || {}
const newLayer = {...oldLayer, iconDefinition }
newIconLayers[replace] = newLayer
}
setAttributes({ iconLayers: newIconLayers })
}
const iconLayerCount = computeIconLayerCount(attributes)
const extraProps = {
wrapperProps: useBlockProps(prepareParamsForUseBlock(attributes))
}
const [isEditModalOpen, setIsEditModalOpen] = useState(false)
return iconLayerCount > 0 ? (
<Fragment>
<BlockControls>
<ToolbarGroup>
<ToolbarDropdownMenu
controls={[
{
icon: justifyLeft,
onClick: () => setJustification('left'),
title: __('Justify Icon Left', 'font-awesome')
},
{
icon: justifyCenter,
onClick: () => setJustification('center'),
title: __('Justify Icon Center', 'font-awesome')
},
{
icon: justifyRight,
onClick: () => setJustification('right'),
title: __('Justify Icon Right', 'font-awesome')
}
]}
icon={justificationDropdownMenuIcon}
label={__('Change Icon Justification', 'font-awesome')}
/>
</ToolbarGroup>
<ToolbarGroup>
<ToolbarButton
showTooltip
onClick={() => setIsEditModalOpen(!isEditModalOpen)}
aria-haspopup="true"
aria-expanded={isEditModalOpen}
label={__('Add Icon Styling')}
icon={modifyToolbarIcon}
/>
</ToolbarGroup>
{isEditModalOpen && (
<Modal
title={__('Add Icon Styling', 'font-awesome')}
onRequestClose={() => setIsEditModalOpen(false)}
className="fawp-icon-styling-modal"
>
<IconModifier
attributes={attributes}
setAttributes={setAttributes}
IconChooserModal={IconChooserModal}
prepareHandleSelect={prepareHandleSelect}
iconChooserOpenEvent={openIconChooserForAddLayerEvent}
/>
</Modal>
)}
</BlockControls>
{renderIcon(attributes, { extraProps })}
</Fragment>
) : (
<Fragment>
<Placeholder
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"
>
<path d="M91.7 96C106.3 86.8 116 70.5 116 52C116 23.3 92.7 0 64 0S12 23.3 12 52c0 16.7 7.8 31.5 20 41l0 3 0 352 0 64 64 0 0-64 373.6 0c14.6 0 26.4-11.8 26.4-26.4c0-3.7-.8-7.3-2.3-10.7L432 272l61.7-138.9c1.5-3.4 2.3-7 2.3-10.7c0-14.6-11.8-26.4-26.4-26.4L91.7 96z" />
</svg>
}
label={__('Add a Font Awesome Icon', 'font-awesome')}
instructions={__('Add an icon as a block element, and add styling to make it extra awesome!', 'font-awesome')}
>
<IconChooserModal
onSubmit={prepareHandleSelect({ append: true })}
openEvent={openIconChooserForAddLayerEvent}
/>
<Button
variant="secondary"
onClick={() => document.dispatchEvent(openIconChooserForAddLayerEvent)}
>
{__('Choose Icon', 'font-awesome')}
</Button>
</Placeholder>
</Fragment>
)
}