Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merl 1256] Bug fix. Generate Autosave is missing from Post/Page Previews #1644

Merged
merged 21 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-cooks-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/wordpress-plugin': patch
---

Bug Fix: Fixed missing call to autosave when using Post/Page previews.
50 changes: 37 additions & 13 deletions plugins/faustwp/includes/replacement/previewlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
*/

document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', function () {
// Get the preview data via wp_localize_script
const faustPreviewData = window._faustwp_preview_data;

Expand All @@ -19,12 +19,10 @@ document.addEventListener('DOMContentLoaded', function() {

function debounce(func, wait) {
let timeout;
return function() {
const context = this;
const args = arguments;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(function() {
func.apply(context, args);
timeout = setTimeout(function () {
func(args);
}, wait);
};
}
Expand All @@ -34,25 +32,51 @@ document.addEventListener('DOMContentLoaded', function() {
switch (version) {
default:
return {
headerLink: document.querySelector('.edit-post-header-preview__grouping-external a'),
snackbarLink: document.querySelector('.components-snackbar__content a'),
headerLink: document.querySelector(
'.edit-post-header-preview__grouping-external a',
),
snackbarLink: document.querySelector(
'.components-snackbar__content a',
),
};
}
}

function autoSave() {
wp.data.dispatch('core/editor').autosave();
}

function maybeSetAutoUpdate() {
const { headerLink, snackbarLink } = getPreviewLinksByVersion(wpVersion);
if (headerLink) {
if (!headerLink.onClick) {
headerLink.onClick = autoSave;
headerLink.onTouch = autoSave;
}
}
if (snackbarLink) {
if (!snackbarLink.onClick) {
snackbarLink.onClick = autoSave;
snackbarLink.onTouch = autoSave;
}
}
}

function updateUIElements() {
const { headerLink, snackbarLink } = getPreviewLinksByVersion(wpVersion);

// Clone & replace the original link in order to clear pre-existing events.
if (headerLink && headerLink.getAttribute('href') !== faustPreviewLink) {
const clonedHeaderLink = headerLink.cloneNode(true);
Copy link
Member Author

@theodesp theodesp Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOM modifications are not allowed under a React managed DOM

Screenshot 2023-11-13 at 13 32 12

headerLink.parentNode.replaceChild(clonedHeaderLink, headerLink);
if (clonedHeaderLink) clonedHeaderLink.setAttribute('href', faustPreviewLink);
headerLink.setAttribute('href', faustPreviewLink);
}

if (snackbarLink && snackbarLink.getAttribute('href') !== faustPreviewLink) {
if (
snackbarLink &&
snackbarLink.getAttribute('href') !== faustPreviewLink
) {
snackbarLink.setAttribute('href', faustPreviewLink);
}

maybeSetAutoUpdate();
}

// Run the update function on initial page load.
Expand Down
Loading