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