import { __ } from "@wordpress/i18n"; import { registerPlugin } from "@wordpress/plugins"; import { useSelect } from "@wordpress/data"; import { createPortal, useEffect, useState } from "@wordpress/element"; import "./editor.scss"; const CopyIcon = () => ( ); const PORTAL_CLASSNAME = "spspc-shortcode-portal"; const copyText = async (text) => { // First try Clipboard API if (navigator.clipboard && navigator.clipboard.writeText) { try { await navigator.clipboard.writeText(text); return true; } catch (e) { console.log("Clipboard API failed, using fallback.", e); } } // Fallback method: hidden textarea + execCommand try { const textarea = document.createElement("textarea"); textarea.value = text; // Hide from screen textarea.style.position = "fixed"; textarea.style.opacity = "0"; textarea.style.pointerEvents = "none"; document.body.appendChild(textarea); textarea.select(); const success = document.execCommand("copy"); document.body.removeChild(textarea); return success; } catch (err) { console.log("Fallback copy failed:", err); return false; } }; const SavedTemplateSidebar = () => { const { postType, postId } = useSelect( (select) => ({ postType: select("core/editor")?.getCurrentPostType?.() || null, postId: select("core/editor")?.getCurrentPostId?.() || null, }), [] ); const [copied, setCopied] = useState(false); const [portalTarget, setPortalTarget] = useState(null); useEffect(() => { if (postType !== "sp_post_template") { setPortalTarget(null); return undefined; } let container = null; const ensureContainer = () => { const fill = document.querySelector(".interface-complementary-area__fill"); if (!fill) { return; } let existing = fill.querySelector(`:scope > .${PORTAL_CLASSNAME}`); if (!existing) { existing = document.createElement("div"); existing.className = PORTAL_CLASSNAME; fill.insertBefore(existing, fill.firstChild); } else if (fill.firstElementChild !== existing) { fill.insertBefore(existing, fill.firstChild); } if (existing !== container) { container = existing; setPortalTarget(existing); } }; ensureContainer(); const observer = new window.MutationObserver(ensureContainer); observer.observe(document.body, { childList: true, subtree: true }); return () => { observer.disconnect(); if (container && container.parentNode) { container.parentNode.removeChild(container); } setPortalTarget(null); }; }, [postType]); if (postType !== "sp_post_template" || !portalTarget) { return null; } const idForShortcode = postId || 0; const shortcode = `[smart_post id="${idForShortcode}"]`; const savedTemplatesUrl = (window.sp_smart_post_block_localize && window.sp_smart_post_block_localize.savedTemplatesUrl) || (window.sp_pcp_block_settings && window.sp_pcp_block_settings.savedTemplatesUrl) || ""; const handleCopy = async () => { const ok = await copyText(shortcode); if (!ok) { return; } setCopied(true); window.setTimeout(() => setCopied(false), 1500); }; return createPortal(
{__("You can use this shortcode anywhere and manage it from", "post-carousel")}{" "} {__("Saved Templates.", "post-carousel")}