| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import classnames from 'classnames'; |
| 5 |
import ExtendAlignmentToolbar from './aligntoolbar'; |
| 6 |
import ExtendTagToolbar from './tagtoolbar'; |
| 7 |
|
| 8 |
/** |
| 9 |
* WordPress dependencies |
| 10 |
*/ |
| 11 |
|
| 12 |
const { |
| 13 |
createBlock |
| 14 |
} = wp.blocks; |
| 15 |
|
| 16 |
const { |
| 17 |
__, |
| 18 |
} = wp.i18n; |
| 19 |
|
| 20 |
const { |
| 21 |
Component, |
| 22 |
} = wp.element; |
| 23 |
|
| 24 |
const { |
| 25 |
BlockControls, |
| 26 |
} = wp.blockEditor; |
| 27 |
|
| 28 |
const { RichText } = wp.blockEditor; |
| 29 |
|
| 30 |
/** |
| 31 |
* Component |
| 32 |
*/ |
| 33 |
class SectionHeadingBlockEdit extends Component { |
| 34 |
constructor() { |
| 35 |
super(...arguments); |
| 36 |
|
| 37 |
this.state = { |
| 38 |
tagName: canvasBSHLocalize.sectionHeadingTag |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
render() { |
| 43 |
const { |
| 44 |
setAttributes |
| 45 |
} = this.props; |
| 46 |
|
| 47 |
let { |
| 48 |
className, |
| 49 |
attributes, |
| 50 |
} = this.props; |
| 51 |
|
| 52 |
const { |
| 53 |
halign, |
| 54 |
tag, |
| 55 |
content, |
| 56 |
canvasClassName, |
| 57 |
} = attributes; |
| 58 |
|
| 59 |
this.state.tagName = tag ? tag : ''; |
| 60 |
|
| 61 |
// If tag default. |
| 62 |
if ( 'default' === tag || 'none' === tag || ! tag ) { |
| 63 |
this.state.tagName = canvasBSHLocalize.sectionHeadingTag; |
| 64 |
} |
| 65 |
|
| 66 |
let classAlign = halign ? `halign${halign}` : ''; |
| 67 |
|
| 68 |
// If align default. |
| 69 |
if ( 'default' === halign || 'none' === halign || ! halign ) { |
| 70 |
classAlign = canvasBSHLocalize.sectionHeadingAlign; |
| 71 |
} |
| 72 |
|
| 73 |
className = classnames( |
| 74 |
'cnvs-block-section-heading', |
| 75 |
classAlign, |
| 76 |
canvasClassName, |
| 77 |
className |
| 78 |
); |
| 79 |
return ( |
| 80 |
<div> |
| 81 |
<BlockControls> |
| 82 |
<ExtendAlignmentToolbar |
| 83 |
value={halign} |
| 84 |
onChange={(val) => { |
| 85 |
let align= (val === undefined) ? 'default' : val; |
| 86 |
|
| 87 |
setAttributes({ halign: align }) |
| 88 |
}} |
| 89 |
/> |
| 90 |
<ExtendTagToolbar |
| 91 |
value={tag} |
| 92 |
onChange={(val) => { |
| 93 |
let type= (val === undefined) ? 'default' : val; |
| 94 |
|
| 95 |
setAttributes({ tag: type }) |
| 96 |
}} |
| 97 |
/> |
| 98 |
</BlockControls> |
| 99 |
|
| 100 |
<this.state.tagName className={className}> |
| 101 |
<div className="cnvs-section-title"> |
| 102 |
<RichText |
| 103 |
tagName="span" |
| 104 |
className="cnvs-section-plain" |
| 105 |
onChange={(newContent) => { |
| 106 |
setAttributes({ content: newContent }); |
| 107 |
}} |
| 108 |
value={content} |
| 109 |
placeholder={__('Write heading…')} |
| 110 |
/> |
| 111 |
</div> |
| 112 |
</this.state.tagName> |
| 113 |
</div> |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
jQuery(() => { |
| 119 |
function sectionHeadingTransforms(settings) { |
| 120 |
if (settings.name !== 'canvas/section-heading') { |
| 121 |
return settings; |
| 122 |
} |
| 123 |
|
| 124 |
settings.transforms = { |
| 125 |
from: [ |
| 126 |
{ |
| 127 |
type: 'block', |
| 128 |
blocks: ['core/heading', 'core/paragraph'], |
| 129 |
transform: function (attributes) { |
| 130 |
return createBlock('canvas/section-heading', { |
| 131 |
content: attributes.content, |
| 132 |
}); |
| 133 |
}, |
| 134 |
}, |
| 135 |
], |
| 136 |
}; |
| 137 |
|
| 138 |
return settings; |
| 139 |
} |
| 140 |
|
| 141 |
wp.hooks.addFilter('canvas.customBlock.registerData', 'canvas/section-heading/transforms', sectionHeadingTransforms); |
| 142 |
}); |
| 143 |
|
| 144 |
export default SectionHeadingBlockEdit; |
| 145 |
|