| 1 |
var openStationIframeBridge=function(O){"use strict";function B(){const v=window;if(v.__openStationEditorAutosaveInstalled)return;v.__openStationEditorAutosaveInstalled=!0;const u=window.location.origin,M=()=>window.wp,A=y=>{if(!(typeof y!="string"||y===""))try{return new URL(y,u).origin===u?y:void 0}catch{return}},T=y=>{try{window.parent.postMessage(y,u)}catch{}},P=new Map,x=(y,d)=>{const _=M(),g=_?.data?.select?.("core/editor");if(g&&typeof _?.data?.subscribe=="function"){const l=_.data.select?.("core/block-editor"),f=_.data.dispatch?.("core/editor");let e=null,n=!1,o=l?.getBlocks?.(),t=g.getEditedPostAttribute?.("title"),r=!1;const s=()=>{if(e=null,!n){if((g.isSavingPost?.()??!1)||(g.isAutosavingPost?.()??!1)){e=window.setTimeout(s,1e3);return}if(!(typeof g.isEditedPostAutosaveable=="function"&&!g.isEditedPostAutosaveable())){if(typeof f?.__unstableSaveForPreview=="function"){Promise.resolve(f.__unstableSaveForPreview()).then(a=>{n||T({type:"os-editor-live-saved",watchId:y,...A(a)?{previewUrl:A(a)}:{}})}).catch(()=>{});return}typeof f?.autosave=="function"&&(f.autosave(),T({type:"os-editor-live-saved",watchId:y}))}}},i=_.data.subscribe(()=>{const a=l?.getBlocks?.(),w=g.getEditedPostAttribute?.("title");if((g.isSavingPost?.()??!1)||(g.isAutosavingPost?.()??!1)){o=a,t=w,r=!0;return}if(r){r=!1,o=a,t=w;return}a===o&&w===t||(o=a,t=w,!(typeof g.isEditedPostDirty=="function"&&!g.isEditedPostDirty())&&(e!==null&&window.clearTimeout(e),e=window.setTimeout(s,d)))});return()=>{n=!0,i(),e!==null&&(window.clearTimeout(e),e=null)}}if(_?.autosave?.server){const f=window.jQuery;if(!f)return null;let e=null,n=!1,o=!1;const t=window.tinymce,r=c=>{const E=document.getElementById(c);return!E||!("value"in E)?null:String(E.value)},s=()=>{const c=[];let E=!1;const W=r("title");W!==null&&(E=!0),c.push(W??"");for(const F of["content","excerpt"]){let L=null;try{const q=t?.get?.(F);q&&!q.isHidden?.()&&(L=q.getContent?.()??null)}catch{}L===null&&(L=r(F)),L!==null&&(E=!0),c.push(L??"")}return E?c.map(F=>`${F.length}:${F}`).join(""):null};let i=s(),a=null,w=!1;const I=()=>{if(e=null,n)return;if(o){e=window.setTimeout(I,1e3);return}const c=s();if(!(c!==null&&c===i))try{_.autosave?.server?.triggerSave?.()}catch{}},S=()=>{n||(w=!0,e!==null&&window.clearTimeout(e),e=window.setTimeout(I,d))},m=`.os-live-${y}`;f(document).on(`before-autosave${m}`,()=>{o=!0,a=s()}),f(document).on(`after-autosave${m}`,()=>{o=!1;const c=a;a=null;const E=c!==null&&c===i;c!==null&&(i=c),!(E||!w)&&(w=!1,T({type:"os-editor-live-saved",watchId:y}))});const p=[];for(const c of["title","content","excerpt"]){const E=document.getElementById(c);E&&(E.addEventListener("input",S),p.push(E))}const b="keyup input change undo redo SetContent ExecCommand",h=[],k=c=>{c?.on&&(c.on(b,S),h.push(c))};(t?.editors??[]).forEach(k);const j=c=>k(c?.editor);return t?.on?.("AddEditor",j),()=>{n=!0,e!==null&&(window.clearTimeout(e),e=null),f(document).off(`before-autosave${m}`),f(document).off(`after-autosave${m}`);for(const c of p)c.removeEventListener("input",S);for(const c of h)try{c.off?.(b,S)}catch{}t?.off?.("AddEditor",j)}}return null};window.addEventListener("message",y=>{if(y.origin!==u)return;const d=y?.data;if(!d||typeof d!="object")return;if(d.type==="os-editor-live-watch"&&typeof d.watchId=="string"){P.get(d.watchId)?.(),P.delete(d.watchId);const f=Math.min(3e4,Math.max(500,typeof d.debounceMs=="number"?d.debounceMs:1500));try{const e=x(d.watchId,f);e&&P.set(d.watchId,e)}catch{}return}if(d.type==="os-editor-live-unwatch"&&typeof d.watchId=="string"){try{P.get(d.watchId)?.()}catch{}P.delete(d.watchId);return}if(d.type!=="os-editor-autosave-request"||typeof d.requestId!="string")return;const _=d.requestId;let g=!1;const l=(f,e)=>{if(!g){g=!0;try{window.parent.postMessage({type:"os-editor-autosave-response",requestId:_,status:f,...e?{previewUrl:e}:{}},u)}catch{}}};try{const f=M(),e=f?.data?.select?.("core/editor");if(e){const o=f?.data?.dispatch?.("core/editor");if(typeof o?.__unstableSaveForPreview=="function"){Promise.resolve(o.__unstableSaveForPreview()).then(t=>{l("saved",A(t))}).catch(()=>l("error"));return}if(typeof e.isEditedPostAutosaveable=="function"&&!e.isEditedPostAutosaveable()){l("not-dirty");return}if(typeof o?.autosave=="function"&&typeof f?.data?.subscribe=="function"){let t=!1;const r=f.data.subscribe(()=>{if(e.isAutosavingPost?.()??!1){t=!0;return}t&&(r(),l("saved"))});window.setTimeout(()=>{r(),l("saved")},8e3),o.autosave();return}l("no-editor");return}const n=f?.autosave?.server?.triggerSave;if(typeof n=="function"){const t=window.jQuery;t?(t(document).one("after-autosave.os-editor-preview",()=>l("saved")),window.setTimeout(()=>l("not-dirty"),5e3)):window.setTimeout(()=>l("saved"),5e3),n.call(f?.autosave?.server);return}l("no-editor")}catch{l("error")}})}return function(){if(!window.parent||window.parent===window)return;B();const v=window;if(v.wp?.os?.iframe)return;const u=window.location.origin,M={},A=[],T={};let P=null;const x=[],y=e=>{if(!e||P===e)return;P=e;const n=x.splice(0);for(const o of n)try{o(e)}catch{}},d={},_=(e,n,o)=>{try{window.parent.postMessage({type:"os-bridge-publish",connectionId:e,topic:n,payload:o},u)}catch{}};window.addEventListener("message",e=>{if(e.origin!==u)return;const n=e?.data;if(!(!n||typeof n!="object"||typeof n.type!="string")){if(n.type==="os-bridge-handshake"&&typeof n.connectionId=="string"){const o=n.targetWindowId;if(typeof o=="string"&&o!==""&&y(o),M[n.connectionId]){try{window.parent.postMessage({type:"os-bridge-handshake-ack",connectionId:n.connectionId},u)}catch{}return}const t={id:n.connectionId,topics:Array.isArray(n.topics)?n.topics.slice():[]};M[t.id]=t;try{window.parent.postMessage({type:"os-bridge-handshake-ack",connectionId:t.id},u)}catch{}for(const r of A)try{r({id:t.id,topics:t.topics.slice()})}catch{}return}if(n.type==="os-bridge-beforeunload-query"){let o=!1,t="";const r=i=>{const a=i;Object.defineProperty(i,"returnValue",{get(){return a._returnValue||""},set(w){a._returnValue=w}})},s=(i,a)=>{const w=i.returnValue,I=typeof a=="string"&&a!=="",S=typeof w=="string"&&w!=="";(i.defaultPrevented||I||S)&&(o=!0,I?t=a:S&&(t=w))};if(typeof window.onbeforeunload=="function"){const i=new Event("beforeunload",{cancelable:!0});r(i);const a=window.onbeforeunload(i);s(i,a)}if(!o){const i=new Event("beforeunload",{cancelable:!0});r(i),window.dispatchEvent(i),s(i,null)}try{window.parent.postMessage({type:"os-bridge-beforeunload-response",prevent:o,message:t},u)}catch{}return}if(n.type==="os-bridge-publish"&&typeof n.topic=="string"){const o={topic:n.topic,connectionId:n.connectionId},t=T[n.topic];if(t)for(const s of t)try{s(n.payload,o)}catch{}const r=T["*"];if(r)for(const s of r)try{s(n.payload,o)}catch{}return}if(n.type==="os-bridge-disconnect"&&typeof n.connectionId=="string"&&delete M[n.connectionId],n.type==="os-window-send"&&typeof n.channel=="string"){const o=n,t={channel:o.channel},r=d[o.channel];if(r)for(const i of r.slice())try{i(o.payload,t)}catch{}const s=d["*"];if(s)for(const i of s.slice())try{i(o.payload,t)}catch{}}}});const g={publish(e,n){if(typeof e!="string"||e==="")return;const o=Object.keys(M);if(o.length===0){console.warn('[openstation] wp.os.iframe.publish dropped: no open connection for topic "%s". The parent shell must call `wp.os.connect(windowId)` first.',e);return}for(const t of o)_(t,e,n)},subscribe(e,n){if(typeof e!="string"||e===""||typeof n!="function")return()=>{};let o=T[e];return o||(o=[],T[e]=o),o.push(n),()=>{const t=o.indexOf(n);t>=0&&o.splice(t,1)}},onConnection(e){if(typeof e!="function")return()=>{};A.push(e);for(const n of Object.keys(M))try{e({id:M[n].id,topics:M[n].topics.slice()})}catch{}return()=>{const n=A.indexOf(e);n>=0&&A.splice(n,1)}},chrome:{setTheme(e){try{window.parent.postMessage({type:"os-chrome-theme",tokens:e??{}},u)}catch{}},setControls(e){try{window.parent.postMessage({type:"os-chrome-controls",config:e??null},u)}catch{}},setSlot(e,n){if(!(typeof e!="string"||e===""))try{window.parent.postMessage({type:"os-chrome-slot",slot:e,html:typeof n=="string"?n:""},u)}catch{}}},requestConnection(e){const n=e??{},o=Array.isArray(n.topics)?n.topics.slice():[],t="wpdir-"+Math.random().toString(36).slice(2,10);return new Promise((r,s)=>{let i=!1;const a=typeof n.timeoutMs=="number"?n.timeoutMs:5e3,w=(m,p)=>{i||(i=!0,window.removeEventListener("message",I),clearTimeout(S),m?r(p):s(p))},I=m=>{if(m.origin!==u)return;const p=m?.data;if(!(!p||typeof p!="object"||p.type!=="os-bridge-connection-ack"||p.requestId!==t))if(p.accepted){const b={id:typeof p.connectionId=="string"?p.connectionId:"",topics:o.slice()};if(typeof n.onOpen=="function")try{n.onOpen(b)}catch{}w(!0,b)}else w(!1,new Error(p.reason||"rejected"))};window.addEventListener("message",I);const S=setTimeout(()=>{w(!1,new Error("timeout"))},a);try{window.parent.postMessage({type:"os-bridge-connection-request",requestId:t,topics:o},u)}catch(m){w(!1,m)}})},get windowId(){return P},whenWindowId(){return P!==null?Promise.resolve(P):new Promise(e=>{x.push(e)})},isParentReachable(){if(!window.parent||window.parent===window)return!1;try{return window.parent.location.origin===u}catch{return!1}}};v.wp||(v.wp={}),v.wp.os||(v.wp.os={}),v.wp.os.iframe=g,typeof v.wp.os.send!="function"&&(v.wp.os.send=(e,n)=>{if(!(typeof e!="string"||e===""))try{window.parent.postMessage({type:"os-window-publish",channel:e,payload:n},u)}catch{}}),typeof v.wp.os.on!="function"&&(v.wp.os.on=(e,n)=>{if(typeof e!="string"||e===""||typeof n!="function")return()=>{};let o=d[e];return o||(o=[],d[e]=o),o.push(n),()=>{const t=o.indexOf(n);t>=0&&o.splice(t,1)}});const l=window;if(l.__openStationScreenMetaInstalled||(l.__openStationScreenMetaInstalled=!0,f(u)),!l.__openStationOsFileDropForwarderInstalled){l.__openStationOsFileDropForwarderInstalled=!0;const e=t=>{const r=t.dataTransfer?.types;if(!r)return!1;const s=r;if(typeof s.includes=="function")return s.includes("Files");if(typeof s.contains=="function")return s.contains("Files");for(let i=0;i<s.length;i++)if(s[i]==="Files")return!0;return!1},n=[".components-drop-zone","[data-drop-zone]",".uploader-window",".media-frame-content"],o=t=>{const r=t;if(!r||!r.closest)return!1;for(const s of n)if(r.closest(s))return!0;return!1};document.addEventListener("dragover",t=>{e(t)&&(o(t.target)||t.defaultPrevented||(t.preventDefault(),t.dataTransfer&&(t.dataTransfer.dropEffect="copy")))},!1),document.addEventListener("drop",t=>{if(!e(t)||o(t.target)||t.defaultPrevented)return;t.preventDefault(),t.stopPropagation();const r=[];if(t.dataTransfer?.files)for(let s=0;s<t.dataTransfer.files.length;s++)r.push(t.dataTransfer.files[s]);if(r.length!==0)try{window.parent.postMessage({type:"os-file-drop",files:r,x:t.clientX,y:t.clientY},u)}catch{}},!1)}if(!l.__openStationDragHoverForwarderInstalled){l.__openStationDragHoverForwarderInstalled=!0;const e=o=>{const t=o.dataTransfer?.types;if(!t)return!1;const r=t;return typeof r.includes=="function"?r.includes("Files"):typeof r.contains=="function"&&r.contains("Files")};let n=0;document.addEventListener("dragover",o=>{const t=Date.now();if(!(t-n<150)){n=t;try{window.parent.postMessage({type:"os-drag-hover",payloadType:e(o)?"os-file":"external"},u)}catch{}}},!0)}if(!l.__openStationPointerForwarderInstalled){l.__openStationPointerForwarderInstalled=!0;let e=!1,n=0;window.addEventListener("message",o=>{if(o.origin!==window.location.origin)return;const t=o.data;!t||t.type!=="os-pointer-track"||(e=t.enabled===!0)}),document.addEventListener("pointermove",o=>{if(!e)return;const t=Date.now();if(!(t-n<40)){n=t;try{window.parent.postMessage({type:"os-pointer-move",x:o.clientX,y:o.clientY},u)}catch{}}},{capture:!0,passive:!0})}try{window.parent&&window.parent!==window&&window.parent.postMessage({type:"os-ready"},u)}catch{}function f(e){const n=()=>{const r=document.getElementById("screen-options-wrap");return!!r&&!!r.querySelector('input:not([type="hidden"]):not([type="submit"]):not([type="button"]):not([type="reset"]), select, textarea')},o=()=>{const r=document.getElementById("contextual-help-wrap");if(!r)return!1;const s=r.querySelectorAll(".help-tab-content, .contextual-help-sidebar");for(let i=0;i<s.length;i++)if((s[i].textContent||"").trim()!=="")return!0;return!1},t=()=>{const r=document.getElementById("screen-meta-links"),s=r?document.getElementById("show-settings-link"):null,i=r?document.getElementById("contextual-help-link"):null,a=[];s&&n()&&a.push("screen-options"),i&&o()&&a.push("help");try{window.parent.postMessage({type:"os-screen-meta",panels:a},e)}catch{}if(a.length===0)return;const w=()=>s&&s.getAttribute("aria-expanded")==="true"?"screen-options":i&&i.getAttribute("aria-expanded")==="true"?"help":null,I=()=>{try{window.parent.postMessage({type:"os-screen-meta-state",open:w()},e)}catch{}};I();const S=new MutationObserver(I);s&&S.observe(s,{attributes:!0,attributeFilter:["aria-expanded"]}),i&&S.observe(i,{attributes:!0,attributeFilter:["aria-expanded"]});const m=p=>{if(!p||p.getAttribute("aria-expanded")!=="true")return;const b=p.getAttribute("aria-controls"),h=b?document.getElementById(b):null;if(!h)return;const k=window.jQuery;if(k)try{k(h).stop(!0,!1)}catch{}h.style.display="none",h.classList.add("hidden"),h.parentElement&&(h.parentElement.style.display="none"),p.classList.remove("screen-meta-active"),p.setAttribute("aria-expanded","false"),document.querySelectorAll(".screen-meta-toggle").forEach(c=>{c.style.visibility=""})};window.addEventListener("message",p=>{if(p.origin!==e)return;const b=p.data;if(!b||b.type!=="os-toggle-panel")return;let h=null;b.panel==="screen-options"&&s?h=s:b.panel==="help"&&i&&(h=i),h&&(h.getAttribute("aria-expanded")!=="true"&&m(h===s?i:s),h.click())})};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",t,{once:!0}):t()}}(),O.installEditorAutosaveHandler=B,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"}),O}({}); |