@wordpress/edit-post
Version:
Edit Post module for WordPress.
62 lines (61 loc) • 2.17 kB
JavaScript
// packages/edit-post/src/components/browser-url/use-classic-revision-redirect.js
import { useRegistry, useSelect } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { store as editorStore } from "@wordpress/editor";
import { useEffect } from "@wordpress/element";
import { addQueryArgs } from "@wordpress/url";
import { unlock } from "../../lock-unlock.mjs";
function useClassicRevisionRedirect() {
const registry = useRegistry();
const { disableVisualRevisions, currentRevisionId, postType, postId } = useSelect((select) => {
const editor = select(editorStore);
return {
disableVisualRevisions: !!editor.getEditorSettings().disableVisualRevisions,
currentRevisionId: unlock(editor).getCurrentRevisionId(),
postType: editor.getCurrentPostType(),
postId: editor.getCurrentPostId()
};
}, []);
const hasClassicRevisionScreen = ![
"wp_template",
"wp_template_part"
].includes(postType);
useEffect(() => {
if (!disableVisualRevisions || !hasClassicRevisionScreen || !currentRevisionId || !postType || !postId) {
return;
}
let cancelled = false;
registry.resolveSelect(coreStore).getRevision("postType", postType, postId, currentRevisionId, {
context: "edit",
_fields: "id"
}).then((revision) => {
if (cancelled || !revision) {
return;
}
const editor = registry.select(editorStore);
const isStillSelected = !!editor.getEditorSettings().disableVisualRevisions && editor.getCurrentPostType() === postType && String(editor.getCurrentPostId()) === String(postId) && unlock(editor).getCurrentRevisionId() === currentRevisionId;
if (!isStillSelected) {
return;
}
window.location.replace(
addQueryArgs("revision.php", {
revision: currentRevisionId
})
);
});
return () => {
cancelled = true;
};
}, [
disableVisualRevisions,
hasClassicRevisionScreen,
currentRevisionId,
postType,
postId,
registry
]);
}
export {
useClassicRevisionRedirect as default
};
//# sourceMappingURL=use-classic-revision-redirect.mjs.map