UNPKG

@wordpress/edit-post

Version:
101 lines (100 loc) 3.09 kB
// packages/edit-post/src/components/browser-url/index.js import { useEffect, useRef, useState } from "@wordpress/element"; import { useSelect, useDispatch } from "@wordpress/data"; import { addQueryArgs, getQueryArg } from "@wordpress/url"; import { store as editorStore } from "@wordpress/editor"; import { unlock } from "../../lock-unlock.mjs"; import useClassicRevisionRedirect from "./use-classic-revision-redirect.mjs"; var URL_WRITE_DEBOUNCE_MS = 300; function getPostEditURL(postId, revisionId) { const args = { post: postId, action: "edit" }; if (revisionId) { args.revision = revisionId; } return addQueryArgs("post.php", args); } function BrowserURL() { useClassicRevisionRedirect(); const [initialRevisionId] = useState(() => { const revision = Number( getQueryArg(window.location.href, "revision") ); return Number.isInteger(revision) && revision > 0 ? revision : null; }); const hasHandledInitialRevisionRef = useRef(false); const { postId, postStatus, currentRevisionId } = useSelect((select) => { const { getCurrentPost } = select(editorStore); const { getCurrentRevisionId } = unlock(select(editorStore)); const post = getCurrentPost(); let { id, status, type } = post; const isTemplate = ["wp_template", "wp_template_part"].includes( type ); if (isTemplate) { id = post.wp_id; } return { postId: id, postStatus: status, // `post.php` rejects templates because `show_ui` is false. Adding // their revision ID to this URL would create a dead link. currentRevisionId: isTemplate ? null : getCurrentRevisionId() }; }, []); const { openRevision } = unlock(useDispatch(editorStore)); const lastURLWriteTimeRef = useRef(null); const lastPostIdRef = useRef(null); useEffect(() => { if (!postId) { return; } if (initialRevisionId && !hasHandledInitialRevisionRef.current) { hasHandledInitialRevisionRef.current = true; openRevision(initialRevisionId); return; } if (postStatus === "auto-draft") { return; } const previousPostId = lastPostIdRef.current; lastPostIdRef.current = postId; const url = getPostEditURL(postId, currentRevisionId); const write = () => { lastURLWriteTimeRef.current = Date.now(); try { window.history.replaceState( { id: postId }, "Post " + postId, url ); } catch { } }; if (previousPostId !== null && previousPostId !== postId) { write(); return; } if (lastURLWriteTimeRef.current === null) { write(); return; } if (Date.now() - lastURLWriteTimeRef.current >= URL_WRITE_DEBOUNCE_MS) { write(); return; } const timeoutId = setTimeout(write, URL_WRITE_DEBOUNCE_MS); return () => clearTimeout(timeoutId); }, [ postId, postStatus, currentRevisionId, initialRevisionId, openRevision ]); return null; } export { BrowserURL as default, getPostEditURL }; //# sourceMappingURL=index.mjs.map