/* eslint-disable quotes */ /* eslint-disable space-in-parens */ /* eslint-disable valid-jsdoc */ /** * BLOCK: epyt/youtube * * Registering a basic block with Gutenberg. * Simple block, renders and saves the same content without any interactivity. */ // Import CSS. import "./style.scss"; import "./editor.scss"; const { __ } = wp.i18n; // Import __() from wp.i18n const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks const { Component, Fragment } = wp.element; const { ServerSideRender, Button, Modal } = wp.components; //const { withState } = wp.compose; import debounce from "lodash/debounce"; /** * Register: aa Gutenberg Block. * * Registers a new block provided a unique name and an object defining its * behavior. Once registered, the block is made editor as an option to any * editor interface where blocks are implemented. * * @link https://wordpress.org/gutenberg/handbook/block-api/ * @param {string} name Block name. * @param {Object} settings Block settings. * @return {?WPBlock} The block, if it has been successfully * registered; otherwise `undefined`. */ registerBlockType("epyt/youtube", { // Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block. title: __("YouTube Wizard"), // Block title. // icon: ( YouTube Wizard Icon ), category: "embed", // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed. keywords: [__("gallery"), __("live"), __("video")], // playlist description: __('Embed a video, playlist, channel, gallery, or live stream.'), attributes: { shortcode: { type: "string", default: "" } }, /** * 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. * * The "edit" property must be a valid function. * * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ */ edit: class extends Component { state = { show: false }; constructor(props) { super(...arguments); this.props = props; //this.onMessage = this.onMessage.bind(this); } componentDidMount() { window.addEventListener("message", this.onMessage); this.gbPreviewSetup("componentDidMount"); } componentDidUpdate() { this.gbPreviewSetup("componentDidUpdate"); } componentWillUnmount() { window.removeEventListener("message", this.onMessage); } showModal = () => { this.setState({ show: true }); }; hideModal = () => { this.setState({ show: false }); }; onMessage = e => { try { if ( e.data.indexOf("youtubeembedplus") === 0 && e.data.indexOf(this.props.clientId) > 0 ) { let embedcode = ""; embedcode = e.data.split("|")[1]; // if (embedcode.indexOf("[") !== 0) // { // embedcode = "

" + embedcode + "

"; // } this.props.setAttributes({ shortcode: embedcode.toString() }); this.hideModal(); // close modal this.gbPreviewSetup("onMessage"); } } catch (err) { } }; gbPreviewSetup = debounce(myContext => { setTimeout(() => { window._EPYTA_.gbPreviewSetup(); }, 1500); }, 50); render() { if (this.props.attributes.shortcode) { const ssr = ( ); return ssr; } return (
YouTube Wizard Icon YouTube Wizard
Click the button below to easily embed a video, playlist, channel, gallery, or live stream.
{this.state.show ? (