/* global flatsome_gutenberg */ (function (wp) { 'use strict' var FlatsomeGutenberg = { headerToolbar: null, editButton: null, init: function () { if (!flatsome_gutenberg.edit_button.enabled || this.editButton) { return } this.buttonText = flatsome_gutenberg.edit_button.text this.editUrl = flatsome_gutenberg.edit_button.url this.buttonIcon = ' ' this.addEditButton() this.bindEvents() }, addEditButton: function () { // Pre WP 6.6 this.headerToolbar = document.querySelector('.block-editor .edit-post-header__toolbar') // WP 6.6 if (!this.headerToolbar) { this.headerToolbar = document.querySelector( '.block-editor .editor-header__toolbar' ) } if (!this.headerToolbar) return this.headerToolbar.insertAdjacentHTML('beforeend', '') this.editButton = this.headerToolbar.querySelector('#uxbuilder-edit-button') }, bindEvents: function () { var self = this if (!this.editButton) return this.editButton.addEventListener('click', function (e) { e.preventDefault() this.classList.add('is-busy') this.blur() var title = wp.data.select('core/editor').getEditedPostAttribute('title') if (!title) wp.data.dispatch('core/editor').editPost({title: 'Auto Draft'}) wp.data.dispatch('core/editor').savePost() self.redirectToBuilder() }, false) }, redirectToBuilder: function () { var self = this setTimeout(function () { if (wp.data.select('core/editor').isSavingPost()) { return self.redirectToBuilder() } if (wp.data.select('core/editor').didPostSaveRequestSucceed()) { window.top.location.href = self.editUrl self.editButton.innerHTML += '...' } else { self.editButton.classList.remove('is-busy') } }, 500) } } wp.domReady(function () { wp.data.subscribe(function () { FlatsomeGutenberg.init() }) }) }(window.wp))