-
Notifications
You must be signed in to change notification settings - Fork 2
/
edit.js
152 lines (138 loc) · 4.18 KB
/
edit.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
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { __experimentalLinkControl as LinkControl, useBlockProps, RichText, MediaPlaceholder, BlockControls, InspectorControls } from '@wordpress/block-editor';
import { ToolbarGroup, PanelBody, SelectControl, ToolbarButton, Popover } from '@wordpress/components'
import { useState } from '@wordpress/element';
import { link } from '@wordpress/icons';
/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';
/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {WPElement} Element to render.
*/
export default function Edit({attributes, setAttributes}) {
const [ showLinkPopover, setShowLinkPopover ] = useState( false );
const toggleLinkPopover = () => {
setShowLinkPopover( ( state ) => ! state );
};
return (
<>
{/* Begin Toolbar Zone */}
<BlockControls>
<ToolbarGroup>
<ToolbarButton
icon={ link }
label="Link"
onClick={toggleLinkPopover}
isPressed={showLinkPopover}
/>
</ToolbarGroup>
{showLinkPopover && (
<Popover>
<LinkControl
searchInputPlaceholder="Search here..."
value={ attributes.link }
onChange={ ( newLink ) => {
console.log(newLink)
setAttributes( { link: {...newLink, title: attributes.link.title || ""} } ) }
}
>
</LinkControl>
</Popover>
)}
</BlockControls>
{/* End Toolbar Zone */}
{/* Begin Sidebar Inspector Zone */}
<InspectorControls>
<PanelBody title="Settings">
<SelectControl
label="Layout Variant:"
onChange={(val) => setAttributes({layout_variant: val})}
value={attributes.layout_variant}
options={
[
{
label: "Text Right",
value: "text-right"
},
{
label: "Text Left",
value: "text-left"
}
]
}
/>
</PanelBody>
</InspectorControls>
{/* End Sidebar Inspector Zone */}
{/* Begin Main Block Zone */}
<div { ...useBlockProps({className: `variant-${attributes.layout_variant}`}) }>
<div className='cta-image-container'>
{attributes.image_url && attributes.image_id ? (
<>
<img src={attributes.image_url} />
<button className="button-remove" onClick={() => setAttributes({image_url: "", image_id: null})}>Remove</button>
</>
) : (
<MediaPlaceholder
onSelect = {
( image ) => {
setAttributes( { image_url: image.url, image_id: image.id } );
}
}
allowedTypes = { [ 'image' ] }
multiple = { false }
labels = { { title: 'CTA Image' } }
>
</MediaPlaceholder>
)
}
</div>
<div className='cta-text-container'>
<RichText
tagName='h2'
allowedFormats={[]}
value={attributes.heading}
onChange={(heading) => setAttributes({heading})}
placeholder='This is the headline'
/>
<RichText
tagName='p'
allowedFormats={[]}
value={attributes.body}
onChange={(body) => setAttributes({body})}
placeholder='This is the body copy'
/>
<RichText
tagName='div'
className='wp-element-button'
allowedFormats={[]}
value={attributes.link.title}
onChange={(newTitle) => setAttributes({link: {...attributes.link, title: newTitle}})}
placeholder='Button text'
/>
</div>
</div>
{/* End Main Block Zone */}
</>
);
}