UNPKG

@wordpress/edit-post

Version:
135 lines (133 loc) 4.96 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/edit-post/src/components/browser-url/index.js var browser_url_exports = {}; __export(browser_url_exports, { default: () => BrowserURL, getPostEditURL: () => getPostEditURL }); module.exports = __toCommonJS(browser_url_exports); var import_element = require("@wordpress/element"); var import_data = require("@wordpress/data"); var import_url = require("@wordpress/url"); var import_editor = require("@wordpress/editor"); var import_lock_unlock = require("../../lock-unlock.cjs"); var import_use_classic_revision_redirect = __toESM(require("./use-classic-revision-redirect.cjs")); var URL_WRITE_DEBOUNCE_MS = 300; function getPostEditURL(postId, revisionId) { const args = { post: postId, action: "edit" }; if (revisionId) { args.revision = revisionId; } return (0, import_url.addQueryArgs)("post.php", args); } function BrowserURL() { (0, import_use_classic_revision_redirect.default)(); const [initialRevisionId] = (0, import_element.useState)(() => { const revision = Number( (0, import_url.getQueryArg)(window.location.href, "revision") ); return Number.isInteger(revision) && revision > 0 ? revision : null; }); const hasHandledInitialRevisionRef = (0, import_element.useRef)(false); const { postId, postStatus, currentRevisionId } = (0, import_data.useSelect)((select) => { const { getCurrentPost } = select(import_editor.store); const { getCurrentRevisionId } = (0, import_lock_unlock.unlock)(select(import_editor.store)); 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 } = (0, import_lock_unlock.unlock)((0, import_data.useDispatch)(import_editor.store)); const lastURLWriteTimeRef = (0, import_element.useRef)(null); const lastPostIdRef = (0, import_element.useRef)(null); (0, import_element.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; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getPostEditURL }); //# sourceMappingURL=index.cjs.map