import React from 'react';
import { i18n } from 'gutenberg/utils/helpers';
import isEmpty from 'lodash/isEmpty';
import { getRawContent } from 'gutenberg/utils/data';
// We should use this... but no time for now
// import { __ } from '@wordpress/i18n';
import { RawHTML } from '@wordpress/element';
import edit from './edit';
const name = 'divi/placeholder';
const tag = `wp:${name}`;
const unwrap = content => content.replace(RegExp(``, 'g'), '');
const encode = content => unwrap(content).replace(//g, '');
const wrap = content => `${encode(content)}`;
const shortcode = (content) => {
if (isEmpty(content) || content.indexOf('[et_pb_section') >= 0) {
// If content is empty or already has shortcode, do nothing
return content;
}
let modified = content;
// Add Text Module.
modified = `[et_pb_text]${modified}[/et_pb_text]`;
// Add Column.
modified = `[et_pb_column type="4_4"]${modified}[/et_pb_column]`;
// Add Row.
modified = `[et_pb_row]${modified}[/et_pb_row]`;
// Add Section.
modified = `[et_pb_section]${modified}[/et_pb_section]`;
return modified;
};
let processed = false;
const { placeholder: { block: { title, description } } } = i18n();
const icon = (
);
export default {
name,
tag,
settings: {
title,
description,
icon,
category: 'embed',
useOnce: true,
attributes: {
content: {
type: 'string',
source: 'html',
},
builder: {
type: 'string',
source: 'meta',
meta: '_et_pb_use_builder',
},
old: {
type: 'string',
source: 'meta',
meta: '_et_pb_old_content',
},
},
supports: {
// This is needed or else GB will try to wrap the shortcode with an extra div.className
// causing a validation error
className: false,
customClassName: false,
html: false,
},
save: props => { props.attributes.content },
edit,
},
hooks: {
// Aight, all the following is needed because GB performs block validation
// which will fail if the shortcode includes invalid HTML.
// In this case, GB would show an error message instead of our custom block
// and we can't allow that to happen.
'divi.addPlaceholder': (content) => {
processed = shortcode(content);
return wrap(processed);
},
'blocks.getSaveElement': (element, blockType) => {
if (blockType.name !== name) {
return element;
}
return { encode(processed === false ? getRawContent() : processed) };
},
},
};