| 1 |
import React, { Component, Fragment } from 'react'; |
| 2 |
import Parser from 'html-react-parser'; |
| 3 |
import domToReact from 'html-react-parser/lib/dom-to-react'; |
| 4 |
|
| 5 |
import Api from 'Shared/Api'; |
| 6 |
import Loader from 'Shared/Loader'; |
| 7 |
import Helpers from '../../general/Helpers'; |
| 8 |
import BlockProperties from '../../menu/BlockProperties'; |
| 9 |
import Property from '../../menu/Property'; |
| 10 |
|
| 11 |
export default class Block extends Component { |
| 12 |
constructor(props) { |
| 13 |
super(props); |
| 14 |
|
| 15 |
this.state = { |
| 16 |
fullShortcode: '', |
| 17 |
html: '', |
| 18 |
loading: false, |
| 19 |
blockMode: 'edit', |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
componentDidMount() { |
| 24 |
this.checkShortcodeChange(); |
| 25 |
} |
| 26 |
|
| 27 |
componentDidUpdate(prevProps) { |
| 28 |
this.checkShortcodeChange(); |
| 29 |
|
| 30 |
// Check for item change. |
| 31 |
if ( prevProps.item !== this.props.item ) { |
| 32 |
this.updatePreview(); |
| 33 |
} |
| 34 |
|
| 35 |
// Make sure we start out in edit mode. |
| 36 |
if ( prevProps.editingBlock !== this.props.editingBlock ) { |
| 37 |
this.onChangeBlockMode('edit'); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
checkShortcodeChange() { |
| 42 |
const fullShortcode = Helpers.getFullShortcode(this.props.shortcode); |
| 43 |
|
| 44 |
if ( fullShortcode !== this.state.fullShortcode ) { |
| 45 |
this.setState({ |
| 46 |
fullShortcode |
| 47 |
}, this.updatePreview); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
updatePreview() { |
| 52 |
this.setState({ |
| 53 |
loading: true, |
| 54 |
}); |
| 55 |
|
| 56 |
Api.template.previewShortcode( this.props.shortcode.uid, this.state.fullShortcode, this.props.item ) |
| 57 |
.then((data) => { |
| 58 |
this.setState({ |
| 59 |
html: data.hasOwnProperty( this.props.shortcode.uid ) ? data[ this.props.shortcode.uid ] : '', |
| 60 |
loading: false, |
| 61 |
}); |
| 62 |
}); |
| 63 |
} |
| 64 |
|
| 65 |
getBlockProperties(shortcode = this.props.shortcode) { |
| 66 |
let properties = {}; |
| 67 |
const structure = wpupg_admin_template.shortcodes.hasOwnProperty(shortcode.id) ? wpupg_admin_template.shortcodes[shortcode.id] : false; |
| 68 |
|
| 69 |
if (structure) { |
| 70 |
Object.entries(structure).forEach(([id, options]) => { |
| 71 |
if(options.type) { |
| 72 |
let name = options.name ? options.name : id.replace(/_/g, ' ').toLowerCase().replace(/\b[a-z]/g, function(letter) { |
| 73 |
return letter.toUpperCase(); |
| 74 |
}); |
| 75 |
|
| 76 |
let value = shortcode.attributes.hasOwnProperty(id) ? shortcode.attributes[id] : options.default; |
| 77 |
|
| 78 |
// Revert HTML entity change. |
| 79 |
value = value.replace(/"/gm, '"'); |
| 80 |
value = value.replace(/]/gm, ']'); |
| 81 |
|
| 82 |
properties[id] = { |
| 83 |
...options, |
| 84 |
id, |
| 85 |
name, |
| 86 |
value, |
| 87 |
}; |
| 88 |
} |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
return properties; |
| 93 |
} |
| 94 |
|
| 95 |
onChangeBlockMode(blockMode) { |
| 96 |
if ( blockMode !== this.state.blockMode ) { |
| 97 |
this.setState({ |
| 98 |
blockMode |
| 99 |
}); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
onCopyPasteStyle(from, to) { |
| 104 |
const fromProperties = this.getBlockProperties(this.props.shortcodes[from]); |
| 105 |
const toProperties = this.getBlockProperties(this.props.shortcodes[to]); |
| 106 |
|
| 107 |
let changedProperties = {}; |
| 108 |
|
| 109 |
Object.entries(toProperties).forEach(([property, options]) => { |
| 110 |
if ( |
| 111 |
fromProperties.hasOwnProperty(property) |
| 112 |
&& fromProperties[property].value !== options.value |
| 113 |
// Exclude some properties. |
| 114 |
&& 'icon' !== property |
| 115 |
&& 'text' !== property |
| 116 |
&& 'label' !== property |
| 117 |
&& 'header' !== property |
| 118 |
// Make sure type matches and dropdown actual has this option. |
| 119 |
&& fromProperties[property].type === options.type |
| 120 |
&& ( 'dropdown' !== options.type || options.options.hasOwnProperty( fromProperties[property].value ) ) // Make sure dropdown option exists. |
| 121 |
) { |
| 122 |
changedProperties[property] = fromProperties[property].value; |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
if ( Object.keys(changedProperties).length ) { |
| 127 |
this.props.onBlockPropertiesChange(to, changedProperties); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
render() { |
| 132 |
const properties = this.getBlockProperties(); |
| 133 |
|
| 134 |
return ( |
| 135 |
<Fragment> |
| 136 |
{ |
| 137 |
this.state.loading |
| 138 |
? |
| 139 |
<Loader/> |
| 140 |
: |
| 141 |
<Fragment> |
| 142 |
{ Parser(this.state.html.trim(), { |
| 143 |
replace: function(domNode) { |
| 144 |
if ( ! domNode.parent && this.props.shortcode.uid === this.props.hoveringBlock ) { |
| 145 |
if ( ! domNode.attribs ) { |
| 146 |
domNode.attribs = {}; |
| 147 |
} |
| 148 |
domNode.attribs.class = domNode.attribs.class ? domNode.attribs.class + ' wpupg-template-block-hovering' : 'wpupg-template-block-hovering'; |
| 149 |
return domToReact(domNode); |
| 150 |
} |
| 151 |
}.bind(this) |
| 152 |
}) } |
| 153 |
</Fragment> |
| 154 |
} |
| 155 |
{ |
| 156 |
this.props.shortcode.uid === this.props.editingBlock |
| 157 |
? |
| 158 |
<BlockProperties> |
| 159 |
{ |
| 160 |
'edit' === this.state.blockMode |
| 161 |
&& |
| 162 |
<Fragment> |
| 163 |
<div className="wpupg-template-menu-block-details"><a href="#" onClick={ (e) => { e.preventDefault(); return this.props.onChangeEditingBlock(false); }}>Blocks</a> > { this.props.shortcode.name }</div> |
| 164 |
<div className="wpupg-template-menu-block-quick-edit"> |
| 165 |
<a href="#" onClick={(e) => { |
| 166 |
e.preventDefault(); |
| 167 |
this.onChangeBlockMode('copy'); |
| 168 |
}}>Copy styles to...</a> | <a href="#" onClick={(e) => { |
| 169 |
e.preventDefault(); |
| 170 |
this.onChangeBlockMode('paste'); |
| 171 |
}}>Paste styles from...</a> |
| 172 |
</div> |
| 173 |
{ |
| 174 |
Object.values(properties).map((property, i) => { |
| 175 |
return <Property |
| 176 |
properties={properties} |
| 177 |
property={property} |
| 178 |
onPropertyChange={(propertyId, value) => this.props.onBlockPropertyChange( this.props.shortcode.uid, propertyId, value )} |
| 179 |
key={i} |
| 180 |
/>; |
| 181 |
}) |
| 182 |
} |
| 183 |
{ |
| 184 |
! Object.keys(properties).length && <p>There are no adjustable properties for this block.</p> |
| 185 |
} |
| 186 |
</Fragment> |
| 187 |
} |
| 188 |
{ |
| 189 |
( 'copy' === this.state.blockMode || 'paste' === this.state.blockMode ) |
| 190 |
&& |
| 191 |
<Fragment> |
| 192 |
<a href="#" onClick={(e) => { |
| 193 |
e.preventDefault(); |
| 194 |
this.onChangeBlockMode('edit'); |
| 195 |
}}>Stop</a> |
| 196 |
<p> |
| 197 |
{ |
| 198 |
'copy' === this.state.blockMode |
| 199 |
? |
| 200 |
'Copy styles to:' |
| 201 |
: |
| 202 |
'Paste styles from:' |
| 203 |
} |
| 204 |
</p> |
| 205 |
{ |
| 206 |
this.props.shortcodes.map((shortcode, i) => { |
| 207 |
if ( shortcode.uid === this.props.shortcode.uid ) { |
| 208 |
return ( |
| 209 |
<div |
| 210 |
key={i} |
| 211 |
className="wpupg-template-menu-block wpupg-template-menu-block-self" |
| 212 |
>{ 'copy' === this.state.blockMode ? 'Copying from' : 'Pasting to' } { shortcode.name }</div> |
| 213 |
); |
| 214 |
} else { |
| 215 |
return ( |
| 216 |
<div |
| 217 |
key={i} |
| 218 |
className={ shortcode.uid === this.props.hoveringBlock ? 'wpupg-template-menu-block wpupg-template-menu-block-hover' : 'wpupg-template-menu-block' } |
| 219 |
onClick={ () => { |
| 220 |
const from = 'copy' === this.state.blockMode ? this.props.shortcode.uid : shortcode.uid; |
| 221 |
const to = 'copy' === this.state.blockMode ? shortcode.uid : this.props.shortcode.uid; |
| 222 |
this.onCopyPasteStyle(from, to); |
| 223 |
}} |
| 224 |
onMouseEnter={ () => this.props.onChangeHoveringBlock(shortcode.uid) } |
| 225 |
onMouseLeave={ () => this.props.onChangeHoveringBlock(false) } |
| 226 |
>{ shortcode.name }</div> |
| 227 |
); |
| 228 |
} |
| 229 |
}) |
| 230 |
} |
| 231 |
</Fragment> |
| 232 |
} |
| 233 |
</BlockProperties> |
| 234 |
: |
| 235 |
null |
| 236 |
} |
| 237 |
</Fragment> |
| 238 |
); |
| 239 |
} |
| 240 |
} |