163 lines
3.9 KiB
JavaScript
163 lines
3.9 KiB
JavaScript
(function (wp) {
|
|
var registerBlockType = wp.blocks.registerBlockType;
|
|
var el = wp.element.createElement;
|
|
var Fragment = wp.element.Fragment;
|
|
var blockEditor = wp.blockEditor || wp.editor;
|
|
var useBlockProps = blockEditor.useBlockProps;
|
|
var InnerBlocks = blockEditor.InnerBlocks;
|
|
var RichText = blockEditor.RichText;
|
|
var MediaUpload = blockEditor.MediaUpload;
|
|
var MediaUploadCheck = blockEditor.MediaUploadCheck;
|
|
var InspectorControls = blockEditor.InspectorControls;
|
|
var Button = wp.components.Button;
|
|
var PanelBody = wp.components.PanelBody;
|
|
var ALLOWED_BLOCKS = [
|
|
'core/paragraph',
|
|
'core/heading',
|
|
'core/list',
|
|
'core/table',
|
|
'core/html',
|
|
'core/quote',
|
|
'core/details',
|
|
'core/image',
|
|
'core/group',
|
|
];
|
|
|
|
registerBlockType('turquesa/article-section', {
|
|
apiVersion: 2,
|
|
title: 'Bloque de artículo',
|
|
description: 'Sección con título, contenido e imagen destacada.',
|
|
icon: 'align-wide',
|
|
category: 'text',
|
|
attributes: {
|
|
title: { type: 'string', default: '' },
|
|
imageId: { type: 'number', default: 0 },
|
|
imageUrl: { type: 'string', default: '' },
|
|
imageAlt: { type: 'string', default: '' },
|
|
},
|
|
supports: {
|
|
html: false,
|
|
anchor: true,
|
|
},
|
|
edit: function (props) {
|
|
var attributes = props.attributes;
|
|
var setAttributes = props.setAttributes;
|
|
var blockProps = useBlockProps({ className: 'blog-section turquesa-article-section-editor' });
|
|
|
|
function onSelectImage(media) {
|
|
if (!media || !media.id) {
|
|
return;
|
|
}
|
|
setAttributes({
|
|
imageId: media.id,
|
|
imageUrl: media.url,
|
|
imageAlt: media.alt || '',
|
|
});
|
|
}
|
|
|
|
function onRemoveImage() {
|
|
setAttributes({
|
|
imageId: 0,
|
|
imageUrl: '',
|
|
imageAlt: '',
|
|
});
|
|
}
|
|
|
|
return el(
|
|
Fragment,
|
|
null,
|
|
el(
|
|
InspectorControls,
|
|
null,
|
|
el(
|
|
PanelBody,
|
|
{ title: 'Imagen destacada del bloque', initialOpen: true },
|
|
el(
|
|
MediaUploadCheck,
|
|
null,
|
|
el(MediaUpload, {
|
|
onSelect: onSelectImage,
|
|
allowedTypes: ['image'],
|
|
value: attributes.imageId,
|
|
render: function (obj) {
|
|
return el(
|
|
Button,
|
|
{
|
|
onClick: obj.open,
|
|
variant: 'secondary',
|
|
},
|
|
attributes.imageId ? 'Cambiar imagen' : 'Elegir imagen destacada'
|
|
);
|
|
},
|
|
})
|
|
),
|
|
attributes.imageId
|
|
? el(
|
|
Button,
|
|
{
|
|
onClick: onRemoveImage,
|
|
isDestructive: true,
|
|
variant: 'link',
|
|
style: { marginTop: '8px' },
|
|
},
|
|
'Quitar imagen'
|
|
)
|
|
: null
|
|
)
|
|
),
|
|
el(
|
|
'section',
|
|
blockProps,
|
|
el(RichText, {
|
|
tagName: 'h2',
|
|
placeholder: 'Título del bloque',
|
|
value: attributes.title,
|
|
onChange: function (title) {
|
|
setAttributes({ title: title });
|
|
},
|
|
allowedFormats: [],
|
|
}),
|
|
el(
|
|
'div',
|
|
{ className: 'turquesa-article-section-editor__image' },
|
|
attributes.imageUrl
|
|
? el('img', {
|
|
src: attributes.imageUrl,
|
|
alt: attributes.imageAlt || '',
|
|
className: 'blog-featured',
|
|
})
|
|
: el(
|
|
MediaUploadCheck,
|
|
null,
|
|
el(MediaUpload, {
|
|
onSelect: onSelectImage,
|
|
allowedTypes: ['image'],
|
|
value: attributes.imageId,
|
|
render: function (obj) {
|
|
return el(
|
|
Button,
|
|
{
|
|
onClick: obj.open,
|
|
variant: 'secondary',
|
|
className: 'turquesa-article-section-editor__upload',
|
|
},
|
|
'Añadir imagen destacada'
|
|
);
|
|
},
|
|
})
|
|
)
|
|
),
|
|
el(InnerBlocks, {
|
|
allowedBlocks: ALLOWED_BLOCKS,
|
|
template: [['core/paragraph', { placeholder: 'Escribe el contenido de este bloque…' }]],
|
|
templateLock: false,
|
|
})
|
|
)
|
|
);
|
|
},
|
|
save: function () {
|
|
return el(InnerBlocks.Content);
|
|
},
|
|
});
|
|
})(window.wp);
|