@atkonoffice/sdk 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/atkonoffice.d.ts +8 -0
- package/dist/atkonoffice.js +1 -1
- package/package.json +1 -1
package/dist/atkonoffice.d.ts
CHANGED
|
@@ -51,6 +51,12 @@ export interface AtkonofficeDocBeforeSave {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
|
|
54
|
+
export interface AtkonofficeShellClosed {
|
|
55
|
+
|
|
56
|
+
reason: string
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
54
60
|
export interface AtkonofficeWindowOptions {
|
|
55
61
|
|
|
56
62
|
width?: number
|
|
@@ -202,6 +208,8 @@ export interface Atkonoffice {
|
|
|
202
208
|
|
|
203
209
|
on(event: 'documentBeforeSave', handler: (payload: AtkonofficeDocBeforeSave) => void): Atkonoffice
|
|
204
210
|
|
|
211
|
+
on(event: 'shellClosed', handler: (payload: AtkonofficeShellClosed) => void): Atkonoffice
|
|
212
|
+
|
|
205
213
|
on(event: 'error', handler: (err: Error) => void): Atkonoffice
|
|
206
214
|
|
|
207
215
|
session: AtkonofficeSession
|
package/dist/atkonoffice.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var y={};var n={backendBase:"",launchPath:"/atkonoffice-rpc/launch",wsTicketPath:"/atkonoffice-rpc/ws-ticket",fetchImpl:null,initialized:!1,ws:null,wsUrl:"",pairId:"",headers:null,eventHandlers:{},wsReconnectAttempt:0,wsReconnectTimer:null,wsGaveUp:!1,pairing:!1,launchTicket:"",openParams:null,fallbackUrl:"",pairingPrevTitle:"",pairingTitleSaved:!1,pairingRecoverTimer:null,pairingTimeoutTimer:null,pairTimeoutMs:5e3,pairRecoverMs:1e3,titleRelayInstalled:!1,trustedFrameOrigins:null,onPairDowngrade:null,replayOpen:null},O="atkonoffice.ws.session",K=12,V=["documentSaved","documentOpened","documentBeforeSave","documentEmbedFailed","error"],g={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave"},P="atkonoffice-launch=",re="\u2007",I="pair.ready",ie="launch.open",ae="doc.embedFailed",C="__atkonofficeTitle",J="atkonoffice-install-guide";function se(e){return e&&e.endsWith("/")?e.slice(0,-1):e}function U(e,t,o){let r=e.indexOf("?")===-1?"?":"&";return e+r+t+"="+o}function A(e){return!!e&&Object.keys(e).length>0}function x(e){return e&&e.message?e.message:String(e)}function E(e,t,o){if(t==null)return;let r=String(t).trim();r&&(e[r]=o==null?"":String(o))}function ce(e){return e?typeof e=="string"?e:typeof e=="object"?Object.keys(e).map(t=>t+"="+String(e[t])).join("; "):"":""}function fe(e){let t;if(typeof TextEncoder<"u"){let o=new TextEncoder().encode(e),r="";for(let i=0;i<o.length;i++)r+=String.fromCharCode(o[i]);t=r}else t=unescape(encodeURIComponent(e));return btoa(t).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function k(){try{return typeof document<"u"&&typeof document.title=="string"?document.title:""}catch{return""}}function Y(){if(typeof window>"u")return!1;try{return window.top!==window.self}catch{return!0}}function B(e){try{let t=document.createElement("iframe");t.style.display="none",t.src=e,document.body.appendChild(t),setTimeout(()=>{try{document.body.removeChild(t)}catch{}},1e3)}catch{window.location.href=e}}function N(e){return!!(typeof e.launchTicket=="string"&&e.launchTicket&&typeof e.wsUrl=="string"&&e.wsUrl&&typeof e.wsTicket=="string"&&e.wsTicket&&typeof document<"u"&&typeof WebSocket<"u")}function G(){return typeof n.fetchImpl=="function"?n.fetchImpl:typeof globalThis<"u"&&typeof globalThis.fetch=="function"?globalThis.fetch.bind(globalThis):null}function Ye(){let e=G();if(!e)throw new Error("atkonoffice: no fetch available \u2014 pass fetchImpl via config in non-browser hosts");return e}async function D(e,t){let r=await Ye()(n.backendBase+n.launchPath,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!r.ok)throw new Error(t+" endpoint returned HTTP "+r.status);return r.json()}function le(e){n.launchTicket=e.launchTicket,n.openParams=e.openParams&&typeof e.openParams=="object"?e.openParams:{},n.fallbackUrl=e.launchUrl,n.pairing=!0,n.pairingTitleSaved=!1,z(),Xe()}function ue(){!n.pairing||typeof document>"u"||(n.pairingTitleSaved||(n.pairingPrevTitle=k(),n.pairingTitleSaved=!0),de(),ze())}function de(){R(L(n.wsUrl,n.pairId,n.launchTicket))}function L(e,t,o){let r=fe(JSON.stringify({wsUrl:e,pairId:t,launchTicket:o}));return re+P+r}function R(e){try{typeof document<"u"&&(document.title=e)}catch{}if(Y())try{let t={};t[C]=e,window.top.postMessage(t,"*")}catch{}}function z(){n.titleRelayInstalled||typeof window>"u"||typeof window.addEventListener!="function"||Y()||(n.titleRelayInstalled=!0,window.addEventListener("message",e=>{if(!Ge(e&&e.origin))return;let t=e&&e.data;if(t&&typeof t=="object"&&typeof t[C]=="string")try{document.title=t[C]}catch{}}))}function Ge(e){let t=n.trustedFrameOrigins;return!t||t.length===0?!0:typeof e=="string"&&t.indexOf(e)!==-1}function ze(){n.pairingRecoverTimer||typeof setInterval>"u"||(n.pairingRecoverTimer=setInterval(()=>{n.pairing&&k().indexOf(P)<0&&de()},n.pairRecoverMs))}function Xe(){n.pairingTimeoutTimer||typeof setTimeout>"u"||(n.pairingTimeoutTimer=setTimeout(()=>{n.pairingTimeoutTimer=null,me(!0)},n.pairTimeoutMs))}function pe(){if(!n.pairing)return;let e=n.ws;me(!1),e&&qe(e)}function qe(e){let t={v:1,type:"event",op:ie,ts:Math.floor(Date.now()/1e3),payload:{pair_id:n.pairId,openParams:n.openParams||{}}};try{e.send(JSON.stringify(t))}catch(o){c("error",new Error("atkonoffice: failed to send launch.open frame \u2014 "+x(o)))}}function me(e){n.pairingRecoverTimer&&(clearInterval(n.pairingRecoverTimer),n.pairingRecoverTimer=null),n.pairingTimeoutTimer&&(clearTimeout(n.pairingTimeoutTimer),n.pairingTimeoutTimer=null);let t=n.pairing;n.pairing=!1,n.pairingTitleSaved&&(R(n.pairingPrevTitle),n.pairingTitleSaved=!1),e&&t&&(B(n.fallbackUrl),typeof n.onPairDowngrade=="function"&&n.onPairDowngrade())}var $e="documentEmbedFailed";function he(e){c($e,e||{})}function ye(e,t){if(!(!e||typeof sessionStorage>"u"))try{sessionStorage.setItem(O,JSON.stringify({pairId:e,wsUrl:t}))}catch{}}function ge(){if(n.pairId)return{pairId:n.pairId,wsUrl:n.wsUrl};if(typeof sessionStorage>"u")return null;try{let e=sessionStorage.getItem(O);return e?JSON.parse(e):null}catch{return null}}function we(){if(n.pairId="",n.wsUrl="",!(typeof sessionStorage>"u"))try{sessionStorage.removeItem(O)}catch{}}function H(){if(n.ws||n.wsReconnectTimer||!n.backendBase)return;let e=ge();!e||!e.pairId||(n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,n.wsReconnectAttempt=0,Te(e.pairId,t=>M(t)))}function M(e){if(typeof WebSocket>"u"){c("error",new Error("atkonoffice: WebSocket unavailable in this host \u2014 document events disabled"));return}if(!n.wsUrl){c("error",new Error("atkonoffice: no wsUrl \u2014 cannot open the document-event channel"));return}if(!e){c("error",new Error("atkonoffice: missing ws_ticket \u2014 cannot subscribe to document events"));return}Qe();let t=U(n.wsUrl,"ticket",encodeURIComponent(e)),o;try{o=new WebSocket(t)}catch(r){c("error",new Error("atkonoffice: WS connect failed \u2014 "+x(r))),v();return}n.ws=o,o.onopen=()=>{n.wsReconnectAttempt=0,n.wsGaveUp=!1,Ze(o),n.pairing&&ue()},o.onmessage=r=>{et(r&&r.data)},o.onerror=()=>{c("error",new Error("atkonoffice: WS error on the document-event channel"))},o.onclose=r=>{if(n.ws===o&&(n.ws=null),r&&r.code===1e3){we();return}v()}}function Qe(){n.wsReconnectTimer&&(clearTimeout(n.wsReconnectTimer),n.wsReconnectTimer=null);let e=n.ws;if(n.ws=null,e){e.onopen=null,e.onmessage=null,e.onerror=null,e.onclose=null;try{e.close(1e3,"client_close")}catch{}}}function Ze(e){if(!A(n.headers))return;let t={v:1,type:"event",op:"client.headers",ts:Math.floor(Date.now()/1e3),payload:{headers:n.headers}};try{e.send(JSON.stringify(t))}catch(o){c("error",new Error("atkonoffice: failed to send headers frame \u2014 "+x(o)))}}function et(e){if(typeof e!="string")return;let t;try{t=JSON.parse(e)}catch{return}if(!t||t.type!=="event")return;if(t.op===I){pe();return}if(t.op===ae){he(t.payload||{});return}let o=g[t.op];o&&c(o,t.payload||{})}function v(){if(n.wsReconnectTimer||n.wsGaveUp)return;let e=ge();if(!e||!e.pairId){c("error",new Error("atkonoffice: document-event channel dropped and no pair to resume"));return}if(n.wsReconnectAttempt>=K){n.wsGaveUp=!0,we();let r=new Error("atkonoffice: document-event channel lost \u2014 gave up after "+K+" consecutive reconnect attempts");r.terminal=!0,c("error",r);return}let t=n.wsReconnectAttempt+1;n.wsReconnectAttempt=t;let o=Math.min(3e4,500*Math.pow(2,t-1));n.wsReconnectTimer=setTimeout(()=>{n.wsReconnectTimer=null,Te(e.pairId,r=>{n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,M(r)})},o)}async function Te(e,t){let o=G();if(!o||!n.backendBase){c("error",new Error("atkonoffice: cannot refresh ws_ticket \u2014 backend not configured")),v();return}try{let r=await o(n.backendBase+n.wsTicketPath,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({pairId:e})});if(!r.ok){c("error",new Error("atkonoffice: ws_ticket refresh returned HTTP "+r.status)),v();return}let i=await r.json();if(!i||typeof i.wsTicket!="string"||!i.wsTicket){c("error",new Error("atkonoffice: ws_ticket refresh response missing wsTicket")),v();return}typeof i.wsUrl=="string"&&i.wsUrl&&(n.wsUrl=i.wsUrl),t(i.wsTicket)}catch(r){c("error",new Error("atkonoffice: ws_ticket refresh failed \u2014 "+x(r))),v()}}function tt(e){let t=n.eventHandlers[e];return!!t&&t.length>0}function nt(e){for(let t in g)if(g.hasOwnProperty(t)&&g[t]===e)return!0;return!1}function be(){for(let e in g)if(g.hasOwnProperty(e)&&tt(g[e]))return!0;return!1}function c(e,t){let o=n.eventHandlers[e];if(!o||o.length===0){if(e==="error")try{console.error("[atkonoffice] "+(t&&t.message?t.message:t))}catch{}return}for(let r of o.slice())try{r(t)}catch{}}function xe(e,t){if(typeof e!="string"||V.indexOf(e)===-1)throw new TypeError('atkonoffice.on: unknown event "'+e+'" (expected one of '+V.join(", ")+")");if(typeof t!="function")throw new TypeError("atkonoffice.on: handler must be a function");return n.eventHandlers[e]||(n.eventHandlers[e]=[]),n.eventHandlers[e].push(t),nt(e)&&H(),y}function Ee(){ot(n.pairTimeoutMs).then(e=>{e.installed||it(e.clientDownloadUrl)})}function ot(e){return new Promise(t=>{let o=!1,r=null,i=null,a=null,h=!1,s="",p="",m=()=>{if(i&&(clearTimeout(i),i=null),a&&(clearInterval(a),a=null),h&&(R(s),h=!1),r){r.onopen=r.onmessage=r.onerror=r.onclose=null;try{r.close(1e3,"probe_done")}catch{}r=null}},u=d=>{o||(o=!0,m(),t({installed:d,clientDownloadUrl:p}))};i=setTimeout(()=>u(!1),e),rt().then(d=>{if(d&&typeof d.clientDownloadUrl=="string"&&(p=d.clientDownloadUrl),o||!N(d)){u(!1);return}let j=d.wsUrl,te=typeof d.pairId=="string"?d.pairId:"",ne=d.launchTicket;z();let b;try{b=new WebSocket(U(j,"ticket",encodeURIComponent(d.wsTicket)))}catch{u(!1);return}r=b,b.onopen=()=>{s=k(),h=!0,R(L(j,te,ne)),a=setInterval(()=>{!o&&k().indexOf(P)<0&&R(L(j,te,ne))},n.pairRecoverMs)},b.onmessage=W=>{let oe=W&&W.data;if(typeof oe!="string")return;let _;try{_=JSON.parse(oe)}catch{return}_&&_.type==="event"&&_.op===I&&u(!0)},b.onerror=()=>{},b.onclose=()=>{}}).catch(()=>{u(!1)})})}async function rt(){return D({docId:"",pageUrl:"",user:""},"atkonoffice: launch-probe")}function it(e){if(typeof document>"u"||!document.body||typeof document.createElement!="function")return null;X();let t=document.createElement("div");t.setAttribute("data-atkonoffice-guide",J),t.style.cssText="position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.45);font:14px/1.6 system-ui,-apple-system,sans-serif;";let o=document.createElement("div");o.style.cssText="background:#fff;color:#222;max-width:420px;width:86%;border-radius:8px;padding:24px;box-shadow:0 8px 32px rgba(0,0,0,0.25);box-sizing:border-box;";let r=document.createElement("h2");r.textContent="\u9700\u8981\u5B89\u88C5\u7F16\u8F91\u5668\u5BA2\u6237\u7AEF",r.style.cssText="margin:0 0 12px;font-size:18px;";let i=document.createElement("p");i.style.cssText="margin:0 0 20px;color:#444;",i.textContent=e?"\u672A\u68C0\u6D4B\u5230\u672C\u673A\u5DF2\u5B89\u88C5\u7F16\u8F91\u5668\u5BA2\u6237\u7AEF\u3002\u8BF7\u5148\u4E0B\u8F7D\u5E76\u5B89\u88C5,\u5B89\u88C5\u5B8C\u6210\u540E\u70B9\u300C\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5\u300D\u3002":"\u672A\u68C0\u6D4B\u5230\u672C\u673A\u5DF2\u5B89\u88C5\u7F16\u8F91\u5668\u5BA2\u6237\u7AEF,\u4E14\u672A\u914D\u7F6E\u4E0B\u8F7D\u5730\u5740\u3002\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u83B7\u53D6\u5B89\u88C5\u5305,\u5B89\u88C5\u5B8C\u6210\u540E\u70B9\u300C\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5\u300D\u3002";let a=document.createElement("div");a.style.cssText="display:flex;gap:10px;justify-content:flex-end;flex-wrap:wrap;";let h="font:inherit;padding:8px 16px;border-radius:6px;cursor:pointer;border:1px solid #ccc;background:#f6f6f6;color:#222;",s="font:inherit;padding:8px 16px;border-radius:6px;cursor:pointer;border:1px solid #1a6dd6;background:#1a6dd6;color:#fff;text-decoration:none;display:inline-block;";if(e){let u=document.createElement("a");u.textContent="\u4E0B\u8F7D\u5BA2\u6237\u7AEF",u.href=e,u.target="_blank",u.rel="noopener noreferrer",u.style.cssText=s,a.appendChild(u)}let p=document.createElement("button");p.type="button",p.textContent="\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5",p.style.cssText=h,p.addEventListener("click",()=>{X();try{typeof n.replayOpen=="function"&&n.replayOpen()}catch{}});let m=document.createElement("button");return m.type="button",m.textContent="\u5173\u95ED",m.style.cssText=h,m.addEventListener("click",()=>X()),a.appendChild(p),a.appendChild(m),o.appendChild(r),o.appendChild(i),o.appendChild(a),t.appendChild(o),document.body.appendChild(t),t}function X(){if(!(typeof document>"u"||typeof document.querySelector!="function"))try{let e=document.querySelector('[data-atkonoffice-guide="'+J+'"]');e&&typeof e.remove=="function"?e.remove():e&&e.parentNode&&typeof e.parentNode.removeChild=="function"&&e.parentNode.removeChild(e)}catch{}}function ke(e){if(!e||typeof e!="object")throw new TypeError("atkonoffice.config requires an options object");if(typeof e.backendBase=="string"&&(n.backendBase=se(e.backendBase)),typeof e.launchPath=="string"&&e.launchPath&&(n.launchPath=e.launchPath),typeof e.wsTicketPath=="string"&&e.wsTicketPath&&(n.wsTicketPath=e.wsTicketPath),typeof e.fetchImpl=="function"&&(n.fetchImpl=e.fetchImpl),Array.isArray(e.trustedFrameOrigins)){let t=e.trustedFrameOrigins.filter(o=>typeof o=="string"&&o);n.trustedFrameOrigins=t.length>0?t:null}return n.initialized=!0,be()&&H(),y}async function q(e){let t=e||{};if(!n.backendBase)throw new Error("atkonoffice.open: backendBase not configured \u2014 call atkonoffice.config({ backendBase }) first");let o=t.pageUrl!=null?String(t.pageUrl):"",r=t.params||{},i=r.docId!=null?String(r.docId):"";if(!o&&!i)throw new Error("atkonoffice.open: docId is required \u2014 call open({ params: { docId, \u2026 } }) (page-less \u6A21\u5F0F\u4E0B\u65E0\u4E1A\u52A1\u9875\u53EF\u951A docId\uFF0C\u4E0B\u8F7D/\u4FDD\u5B58\u5747 docId \u7ED1\u5B9A)");let a={pageUrl:o,params:r},h=st(t.options);h&&(a.options=h),typeof t.pageApi=="string"&&t.pageApi&&(a.pageApi=t.pageApi),t.storage&&typeof t.storage=="object"&&Object.keys(t.storage).length>0&&(a.storage=t.storage);let s=await D(a,"atkonoffice.open: launch");if(!s||typeof s.launchUrl!="string"||!s.launchUrl)throw new Error("atkonoffice.open: launch endpoint response missing launchUrl");let p=at(t);n.headers=A(p)?p:null;let m=N(s);return n.replayOpen=()=>q(e),n.onPairDowngrade=m?Ee:null,typeof s.wsTicket=="string"&&s.wsTicket&&typeof s.wsUrl=="string"&&s.wsUrl&&(n.wsUrl=s.wsUrl,n.pairId=typeof s.pairId=="string"?s.pairId:"",ye(n.pairId,n.wsUrl),n.wsReconnectAttempt=0,n.wsGaveUp=!1,m&&le(s),M(s.wsTicket)),m||B(s.launchUrl),{launchUrl:s.launchUrl,downloadUrl:typeof s.downloadUrl=="string"?s.downloadUrl:"",pairId:n.pairId,wsUrl:n.wsUrl}}function at(e){let t={},o=e.headers;if(Array.isArray(o)){for(let i of o)if(Array.isArray(i)&&i.length>=2)E(t,i[0],i[1]);else if(typeof i=="string"&&i.indexOf(":")!==-1){let a=i.indexOf(":");E(t,i.slice(0,a),i.slice(a+1).trim())}}else if(o&&typeof o=="object")for(let i of Object.keys(o))E(t,i,o[i]);let r=ce(e.cookie);return r&&E(t,"Cookie",r),e.storage&&typeof e.storage=="object"&&E(t,"X-Atkonoffice-Storage",JSON.stringify(e.storage)),t}function st(e){if(!e||typeof e!="object")return null;let t={};return typeof e.width=="number"&&(t.width=e.width),typeof e.height=="number"&&(t.height=e.height),typeof e.modal=="boolean"&&(t.modal=e.modal),Object.keys(t).length>0?t:null}var f={};var Re="atkonoffice-doc-host",$="__atkonofficeDocHostEvent",Q=["documentSaved","documentOpened","documentBeforeSave","error","controlReady","compareDiff"],ve={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave","compare.diff":"compareDiff"},Pe="controlReady",Se="toolbarClick",_e="readbackResult",l={eventHandlers:{},hintShown:!1,toolbarButtons:{},toolbarSeq:0,readbackPending:{},readbackSeq:0};function Oe(e,t,o){if(typeof e!="string")throw new TypeError("atkonoffice embed.addCustomToolButton: caption must be a string");if(typeof o!="function")throw new TypeError("atkonoffice embed.addCustomToolButton: onClick must be a function");if(typeof window>"u")return null;w();let r=typeof t=="number"&&isFinite(t)?Math.trunc(t):0,i="btn-"+ ++l.toolbarSeq;return l.toolbarButtons[i]=o,T("toolbarAdd",JSON.stringify({buttonId:i,caption:e,iconIndex:r}))?{remove:function(){delete l.toolbarButtons[i],T("toolbarRemove",JSON.stringify({buttonId:i}))}}:(delete l.toolbarButtons[i],Be(),null)}function Ie(e){if(typeof e!="boolean")throw new TypeError("atkonoffice embed.setCustomToolbarVisible: visible must be a boolean");return typeof window>"u"?f:(w(),T("toolbarSetVisible",JSON.stringify({visible:e})),f)}function Ce(e){let t=e&&e.buttonId;if(typeof t!="string"||!t)return;let o=l.toolbarButtons[t];if(typeof o=="function")try{o()}catch(r){Ne("addCustomToolButton onClick threw: "+(r&&r.message?r.message:r))}}function F(){if(typeof window>"u")return null;let e=window.atkonofficeCtrl=window.atkonofficeCtrl||{};return typeof e.save!="function"&&(e.save=function(t){let o=t&&typeof t=="object"?JSON.stringify(t):void 0;return typeof e._WebSave=="function"?e._WebSave(o):T("save",o)}),typeof e.exec!="function"&&(e.exec=function(t){return typeof t!="string"||!t?!1:T("executeJs",t)}),typeof e.setSavePath!="function"&&(e.setSavePath=function(t){return typeof t!="string"||!t?!1:T("setSavePath",t)}),e}function Ue(e){let t=F();return t&&typeof t.save=="function"?t.save(e):!1}function Ae(e){let t=F();return t&&typeof t.setSavePath=="function"?t.setSavePath(e):!1}function T(e,t){if(typeof window>"u")return!1;let o=window.__atkonofficeDocHost;return o&&typeof o.sendCommand=="function"?(t!=null?o.sendCommand(e,t):o.sendCommand(e),!0):!1}var ct=15e3;function S(e,t){if(typeof window>"u")return Promise.resolve({ok:!1,error:"non-browser"});w();let o=++l.readbackSeq,r=JSON.stringify({rid:o,method:e,args:Array.isArray(t)?t:[]});return new Promise(i=>{let a=setTimeout(function(){Z(o,{ok:!1,error:"timeout"})},ct);a&&typeof a.unref=="function"&&a.unref(),l.readbackPending[o]={resolve:i,timer:a},T("readback",r)||Z(o,{ok:!1,error:"shell-absent"})})}function Z(e,t){let o=l.readbackPending[e];o&&(delete l.readbackPending[e],clearTimeout(o.timer),o.resolve(t))}function De(e){let t=e&&e.rid;typeof t=="number"&&Z(t,{ok:!!(e&&e.ok),value:e?e.value:void 0,error:e?e.error:void 0})}function w(){ft(),F()}function He(e,t){if(typeof e!="string"||Q.indexOf(e)===-1)throw new TypeError('atkonoffice embed.on: unknown event "'+e+'" (expected one of '+Q.join(", ")+")");if(typeof t!="function")throw new TypeError("atkonoffice embed.on: handler must be a function");return w(),l.eventHandlers[e]||(l.eventHandlers[e]=[]),l.eventHandlers[e].push(t),f}function ft(){typeof window>"u"||typeof window[$]=="function"||(window[$]=function(e,t){if(e===Pe){Le("controlReady",t||{});return}if(e===Se){Ce(t||{});return}if(e===_e){De(t||{});return}let o=ve[e];o&&Le(o,t||{})})}function Le(e,t){let o=l.eventHandlers[e];if(!o||o.length===0){if(e==="error")try{console.error("[atkonoffice embed] "+(t&&t.message?t.message:t))}catch{}return}for(let r of o.slice())try{r(t)}catch{}}function Be(){if(!l.hintShown){l.hintShown=!0;try{console.info("[atkonoffice embed] <"+Re+"> \u672A\u88AB\u5B9A\u4E49 \u2014\u2014 \u8BF7\u5728\u5DF2\u5B89\u88C5 atkonoffice \u5BA2\u6237\u7AEF\u7684\u58F3\u5185\u6253\u5F00\u672C\u7F16\u8F91\u9875;\u666E\u901A\u6D4F\u89C8\u5668\u91CC\u5B83\u662F\u7A7A\u76D2(\u4E0D\u5F71\u54CD\u9875\u9762\u5176\u4F59\u90E8\u5206)\u3002")}catch{}}}function Ne(e){try{console.warn("[atkonoffice embed] "+e)}catch{}}function Me(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeSave: fn must be a function");return w(),je("onBeforeSave",e),f}function Fe(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeClose: fn must be a function");return w(),je("onBeforeClose",e),f}function je(e,t){if(typeof window>"u")return!1;let o=window.__atkonofficeDocHost;return o&&typeof o[e]=="function"?(o[e](t),!0):!1}var lt="AO_";function ee(e){return!!(e&&e.ok&&Number(e.value)>0)}function We(e,t){if(typeof e!="string"||!e)return Promise.resolve(!1);let o=typeof t=="string"?t:t==null?"":String(t);return S("Word_AddDataRegion",[e,o]).then(ee)}function Ke(e){return typeof e!="string"||!e?Promise.resolve(!1):S("Word_DeleteDataRegion",[e]).then(ee)}function Ve(e){return typeof e!="string"||!e?Promise.resolve(!1):S("Word_LocateDataRegion",[e]).then(ee)}function Je(e){let t=typeof e=="string"?e:lt;return S("BookmarksAsJson",[]).then(function(o){if(!o||!o.ok)return[];let r;try{r=JSON.parse(o.value)}catch{return[]}return Array.isArray(r)?r.filter(function(i){return typeof i=="string"&&(t===""||i.indexOf(t)===0)}):[]})}f.on=He;f.addCustomToolButton=Oe;f.setCustomToolbarVisible=Ie;f.save=Ue;f.setSavePath=Ae;f.onBeforeSave=Me;f.onBeforeClose=Fe;f.word={addDataRegion:We,deleteDataRegion:Ke,locateDataRegion:Ve,listDataRegions:Je};y.config=ke;y.open=q;y.on=xe;y.session=f;var ut=y,Pn=ut;export{ut as atkonoffice,Pn as default};
|
|
1
|
+
var y={};var n={backendBase:"",launchPath:"/atkonoffice-rpc/launch",wsTicketPath:"/atkonoffice-rpc/ws-ticket",fetchImpl:null,initialized:!1,ws:null,wsUrl:"",pairId:"",headers:null,eventHandlers:{},wsReconnectAttempt:0,wsReconnectTimer:null,wsGaveUp:!1,pairing:!1,launchTicket:"",openParams:null,fallbackUrl:"",pairingPrevTitle:"",pairingTitleSaved:!1,pairingRecoverTimer:null,pairingTimeoutTimer:null,pairTimeoutMs:5e3,pairRecoverMs:1e3,titleRelayInstalled:!1,trustedFrameOrigins:null,onPairDowngrade:null,replayOpen:null},O="atkonoffice.ws.session",K=12,V=["documentSaved","documentOpened","documentBeforeSave","documentEmbedFailed","shellClosed","error"],g={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave","shell.closed":"shellClosed"},P="atkonoffice-launch=",re="\u2007",I="pair.ready",ie="launch.open",ae="doc.embedFailed",C="__atkonofficeTitle",J="atkonoffice-install-guide";function se(e){return e&&e.endsWith("/")?e.slice(0,-1):e}function U(e,t,o){let r=e.indexOf("?")===-1?"?":"&";return e+r+t+"="+o}function A(e){return!!e&&Object.keys(e).length>0}function x(e){return e&&e.message?e.message:String(e)}function E(e,t,o){if(t==null)return;let r=String(t).trim();r&&(e[r]=o==null?"":String(o))}function ce(e){return e?typeof e=="string"?e:typeof e=="object"?Object.keys(e).map(t=>t+"="+String(e[t])).join("; "):"":""}function fe(e){let t;if(typeof TextEncoder<"u"){let o=new TextEncoder().encode(e),r="";for(let i=0;i<o.length;i++)r+=String.fromCharCode(o[i]);t=r}else t=unescape(encodeURIComponent(e));return btoa(t).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function k(){try{return typeof document<"u"&&typeof document.title=="string"?document.title:""}catch{return""}}function Y(){if(typeof window>"u")return!1;try{return window.top!==window.self}catch{return!0}}function B(e){try{let t=document.createElement("iframe");t.style.display="none",t.src=e,document.body.appendChild(t),setTimeout(()=>{try{document.body.removeChild(t)}catch{}},1e3)}catch{window.location.href=e}}function N(e){return!!(typeof e.launchTicket=="string"&&e.launchTicket&&typeof e.wsUrl=="string"&&e.wsUrl&&typeof e.wsTicket=="string"&&e.wsTicket&&typeof document<"u"&&typeof WebSocket<"u")}function G(){return typeof n.fetchImpl=="function"?n.fetchImpl:typeof globalThis<"u"&&typeof globalThis.fetch=="function"?globalThis.fetch.bind(globalThis):null}function Ye(){let e=G();if(!e)throw new Error("atkonoffice: no fetch available \u2014 pass fetchImpl via config in non-browser hosts");return e}async function D(e,t){let r=await Ye()(n.backendBase+n.launchPath,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!r.ok)throw new Error(t+" endpoint returned HTTP "+r.status);return r.json()}function le(e){n.launchTicket=e.launchTicket,n.openParams=e.openParams&&typeof e.openParams=="object"?e.openParams:{},n.fallbackUrl=e.launchUrl,n.pairing=!0,n.pairingTitleSaved=!1,z(),Xe()}function ue(){!n.pairing||typeof document>"u"||(n.pairingTitleSaved||(n.pairingPrevTitle=k(),n.pairingTitleSaved=!0),de(),ze())}function de(){R(L(n.wsUrl,n.pairId,n.launchTicket))}function L(e,t,o){let r=fe(JSON.stringify({wsUrl:e,pairId:t,launchTicket:o}));return re+P+r}function R(e){try{typeof document<"u"&&(document.title=e)}catch{}if(Y())try{let t={};t[C]=e,window.top.postMessage(t,"*")}catch{}}function z(){n.titleRelayInstalled||typeof window>"u"||typeof window.addEventListener!="function"||Y()||(n.titleRelayInstalled=!0,window.addEventListener("message",e=>{if(!Ge(e&&e.origin))return;let t=e&&e.data;if(t&&typeof t=="object"&&typeof t[C]=="string")try{document.title=t[C]}catch{}}))}function Ge(e){let t=n.trustedFrameOrigins;return!t||t.length===0?!0:typeof e=="string"&&t.indexOf(e)!==-1}function ze(){n.pairingRecoverTimer||typeof setInterval>"u"||(n.pairingRecoverTimer=setInterval(()=>{n.pairing&&k().indexOf(P)<0&&de()},n.pairRecoverMs))}function Xe(){n.pairingTimeoutTimer||typeof setTimeout>"u"||(n.pairingTimeoutTimer=setTimeout(()=>{n.pairingTimeoutTimer=null,me(!0)},n.pairTimeoutMs))}function pe(){if(!n.pairing)return;let e=n.ws;me(!1),e&&qe(e)}function qe(e){let t={v:1,type:"event",op:ie,ts:Math.floor(Date.now()/1e3),payload:{pair_id:n.pairId,openParams:n.openParams||{}}};try{e.send(JSON.stringify(t))}catch(o){c("error",new Error("atkonoffice: failed to send launch.open frame \u2014 "+x(o)))}}function me(e){n.pairingRecoverTimer&&(clearInterval(n.pairingRecoverTimer),n.pairingRecoverTimer=null),n.pairingTimeoutTimer&&(clearTimeout(n.pairingTimeoutTimer),n.pairingTimeoutTimer=null);let t=n.pairing;n.pairing=!1,n.pairingTitleSaved&&(R(n.pairingPrevTitle),n.pairingTitleSaved=!1),e&&t&&(B(n.fallbackUrl),typeof n.onPairDowngrade=="function"&&n.onPairDowngrade())}var $e="documentEmbedFailed";function he(e){c($e,e||{})}function ye(e,t){if(!(!e||typeof sessionStorage>"u"))try{sessionStorage.setItem(O,JSON.stringify({pairId:e,wsUrl:t}))}catch{}}function ge(){if(n.pairId)return{pairId:n.pairId,wsUrl:n.wsUrl};if(typeof sessionStorage>"u")return null;try{let e=sessionStorage.getItem(O);return e?JSON.parse(e):null}catch{return null}}function we(){if(n.pairId="",n.wsUrl="",!(typeof sessionStorage>"u"))try{sessionStorage.removeItem(O)}catch{}}function H(){if(n.ws||n.wsReconnectTimer||!n.backendBase)return;let e=ge();!e||!e.pairId||(n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,n.wsReconnectAttempt=0,Te(e.pairId,t=>M(t)))}function M(e){if(typeof WebSocket>"u"){c("error",new Error("atkonoffice: WebSocket unavailable in this host \u2014 document events disabled"));return}if(!n.wsUrl){c("error",new Error("atkonoffice: no wsUrl \u2014 cannot open the document-event channel"));return}if(!e){c("error",new Error("atkonoffice: missing ws_ticket \u2014 cannot subscribe to document events"));return}Qe();let t=U(n.wsUrl,"ticket",encodeURIComponent(e)),o;try{o=new WebSocket(t)}catch(r){c("error",new Error("atkonoffice: WS connect failed \u2014 "+x(r))),v();return}n.ws=o,o.onopen=()=>{n.wsReconnectAttempt=0,n.wsGaveUp=!1,Ze(o),n.pairing&&ue()},o.onmessage=r=>{et(r&&r.data)},o.onerror=()=>{c("error",new Error("atkonoffice: WS error on the document-event channel"))},o.onclose=r=>{if(n.ws===o&&(n.ws=null),r&&r.code===1e3){we();return}v()}}function Qe(){n.wsReconnectTimer&&(clearTimeout(n.wsReconnectTimer),n.wsReconnectTimer=null);let e=n.ws;if(n.ws=null,e){e.onopen=null,e.onmessage=null,e.onerror=null,e.onclose=null;try{e.close(1e3,"client_close")}catch{}}}function Ze(e){if(!A(n.headers))return;let t={v:1,type:"event",op:"client.headers",ts:Math.floor(Date.now()/1e3),payload:{headers:n.headers}};try{e.send(JSON.stringify(t))}catch(o){c("error",new Error("atkonoffice: failed to send headers frame \u2014 "+x(o)))}}function et(e){if(typeof e!="string")return;let t;try{t=JSON.parse(e)}catch{return}if(!t||t.type!=="event")return;if(t.op===I){pe();return}if(t.op===ae){he(t.payload||{});return}let o=g[t.op];o&&c(o,t.payload||{})}function v(){if(n.wsReconnectTimer||n.wsGaveUp)return;let e=ge();if(!e||!e.pairId){c("error",new Error("atkonoffice: document-event channel dropped and no pair to resume"));return}if(n.wsReconnectAttempt>=K){n.wsGaveUp=!0,we();let r=new Error("atkonoffice: document-event channel lost \u2014 gave up after "+K+" consecutive reconnect attempts");r.terminal=!0,c("error",r);return}let t=n.wsReconnectAttempt+1;n.wsReconnectAttempt=t;let o=Math.min(3e4,500*Math.pow(2,t-1));n.wsReconnectTimer=setTimeout(()=>{n.wsReconnectTimer=null,Te(e.pairId,r=>{n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,M(r)})},o)}async function Te(e,t){let o=G();if(!o||!n.backendBase){c("error",new Error("atkonoffice: cannot refresh ws_ticket \u2014 backend not configured")),v();return}try{let r=await o(n.backendBase+n.wsTicketPath,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({pairId:e})});if(!r.ok){c("error",new Error("atkonoffice: ws_ticket refresh returned HTTP "+r.status)),v();return}let i=await r.json();if(!i||typeof i.wsTicket!="string"||!i.wsTicket){c("error",new Error("atkonoffice: ws_ticket refresh response missing wsTicket")),v();return}typeof i.wsUrl=="string"&&i.wsUrl&&(n.wsUrl=i.wsUrl),t(i.wsTicket)}catch(r){c("error",new Error("atkonoffice: ws_ticket refresh failed \u2014 "+x(r))),v()}}function tt(e){let t=n.eventHandlers[e];return!!t&&t.length>0}function nt(e){for(let t in g)if(g.hasOwnProperty(t)&&g[t]===e)return!0;return!1}function be(){for(let e in g)if(g.hasOwnProperty(e)&&tt(g[e]))return!0;return!1}function c(e,t){let o=n.eventHandlers[e];if(!o||o.length===0){if(e==="error")try{console.error("[atkonoffice] "+(t&&t.message?t.message:t))}catch{}return}for(let r of o.slice())try{r(t)}catch{}}function xe(e,t){if(typeof e!="string"||V.indexOf(e)===-1)throw new TypeError('atkonoffice.on: unknown event "'+e+'" (expected one of '+V.join(", ")+")");if(typeof t!="function")throw new TypeError("atkonoffice.on: handler must be a function");return n.eventHandlers[e]||(n.eventHandlers[e]=[]),n.eventHandlers[e].push(t),nt(e)&&H(),y}function Ee(){ot(n.pairTimeoutMs).then(e=>{e.installed||it(e.clientDownloadUrl)})}function ot(e){return new Promise(t=>{let o=!1,r=null,i=null,a=null,h=!1,s="",p="",m=()=>{if(i&&(clearTimeout(i),i=null),a&&(clearInterval(a),a=null),h&&(R(s),h=!1),r){r.onopen=r.onmessage=r.onerror=r.onclose=null;try{r.close(1e3,"probe_done")}catch{}r=null}},u=d=>{o||(o=!0,m(),t({installed:d,clientDownloadUrl:p}))};i=setTimeout(()=>u(!1),e),rt().then(d=>{if(d&&typeof d.clientDownloadUrl=="string"&&(p=d.clientDownloadUrl),o||!N(d)){u(!1);return}let j=d.wsUrl,te=typeof d.pairId=="string"?d.pairId:"",ne=d.launchTicket;z();let b;try{b=new WebSocket(U(j,"ticket",encodeURIComponent(d.wsTicket)))}catch{u(!1);return}r=b,b.onopen=()=>{s=k(),h=!0,R(L(j,te,ne)),a=setInterval(()=>{!o&&k().indexOf(P)<0&&R(L(j,te,ne))},n.pairRecoverMs)},b.onmessage=W=>{let oe=W&&W.data;if(typeof oe!="string")return;let _;try{_=JSON.parse(oe)}catch{return}_&&_.type==="event"&&_.op===I&&u(!0)},b.onerror=()=>{},b.onclose=()=>{}}).catch(()=>{u(!1)})})}async function rt(){return D({docId:"",pageUrl:"",user:""},"atkonoffice: launch-probe")}function it(e){if(typeof document>"u"||!document.body||typeof document.createElement!="function")return null;X();let t=document.createElement("div");t.setAttribute("data-atkonoffice-guide",J),t.style.cssText="position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.45);font:14px/1.6 system-ui,-apple-system,sans-serif;";let o=document.createElement("div");o.style.cssText="background:#fff;color:#222;max-width:420px;width:86%;border-radius:8px;padding:24px;box-shadow:0 8px 32px rgba(0,0,0,0.25);box-sizing:border-box;";let r=document.createElement("h2");r.textContent="\u9700\u8981\u5B89\u88C5\u7F16\u8F91\u5668\u5BA2\u6237\u7AEF",r.style.cssText="margin:0 0 12px;font-size:18px;";let i=document.createElement("p");i.style.cssText="margin:0 0 20px;color:#444;",i.textContent=e?"\u672A\u68C0\u6D4B\u5230\u672C\u673A\u5DF2\u5B89\u88C5\u7F16\u8F91\u5668\u5BA2\u6237\u7AEF\u3002\u8BF7\u5148\u4E0B\u8F7D\u5E76\u5B89\u88C5,\u5B89\u88C5\u5B8C\u6210\u540E\u70B9\u300C\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5\u300D\u3002":"\u672A\u68C0\u6D4B\u5230\u672C\u673A\u5DF2\u5B89\u88C5\u7F16\u8F91\u5668\u5BA2\u6237\u7AEF,\u4E14\u672A\u914D\u7F6E\u4E0B\u8F7D\u5730\u5740\u3002\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u83B7\u53D6\u5B89\u88C5\u5305,\u5B89\u88C5\u5B8C\u6210\u540E\u70B9\u300C\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5\u300D\u3002";let a=document.createElement("div");a.style.cssText="display:flex;gap:10px;justify-content:flex-end;flex-wrap:wrap;";let h="font:inherit;padding:8px 16px;border-radius:6px;cursor:pointer;border:1px solid #ccc;background:#f6f6f6;color:#222;",s="font:inherit;padding:8px 16px;border-radius:6px;cursor:pointer;border:1px solid #1a6dd6;background:#1a6dd6;color:#fff;text-decoration:none;display:inline-block;";if(e){let u=document.createElement("a");u.textContent="\u4E0B\u8F7D\u5BA2\u6237\u7AEF",u.href=e,u.target="_blank",u.rel="noopener noreferrer",u.style.cssText=s,a.appendChild(u)}let p=document.createElement("button");p.type="button",p.textContent="\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5",p.style.cssText=h,p.addEventListener("click",()=>{X();try{typeof n.replayOpen=="function"&&n.replayOpen()}catch{}});let m=document.createElement("button");return m.type="button",m.textContent="\u5173\u95ED",m.style.cssText=h,m.addEventListener("click",()=>X()),a.appendChild(p),a.appendChild(m),o.appendChild(r),o.appendChild(i),o.appendChild(a),t.appendChild(o),document.body.appendChild(t),t}function X(){if(!(typeof document>"u"||typeof document.querySelector!="function"))try{let e=document.querySelector('[data-atkonoffice-guide="'+J+'"]');e&&typeof e.remove=="function"?e.remove():e&&e.parentNode&&typeof e.parentNode.removeChild=="function"&&e.parentNode.removeChild(e)}catch{}}function ke(e){if(!e||typeof e!="object")throw new TypeError("atkonoffice.config requires an options object");if(typeof e.backendBase=="string"&&(n.backendBase=se(e.backendBase)),typeof e.launchPath=="string"&&e.launchPath&&(n.launchPath=e.launchPath),typeof e.wsTicketPath=="string"&&e.wsTicketPath&&(n.wsTicketPath=e.wsTicketPath),typeof e.fetchImpl=="function"&&(n.fetchImpl=e.fetchImpl),Array.isArray(e.trustedFrameOrigins)){let t=e.trustedFrameOrigins.filter(o=>typeof o=="string"&&o);n.trustedFrameOrigins=t.length>0?t:null}return n.initialized=!0,be()&&H(),y}async function q(e){let t=e||{};if(!n.backendBase)throw new Error("atkonoffice.open: backendBase not configured \u2014 call atkonoffice.config({ backendBase }) first");let o=t.pageUrl!=null?String(t.pageUrl):"",r=t.params||{},i=r.docId!=null?String(r.docId):"";if(!o&&!i)throw new Error("atkonoffice.open: docId is required \u2014 call open({ params: { docId, \u2026 } }) (page-less \u6A21\u5F0F\u4E0B\u65E0\u4E1A\u52A1\u9875\u53EF\u951A docId\uFF0C\u4E0B\u8F7D/\u4FDD\u5B58\u5747 docId \u7ED1\u5B9A)");let a={pageUrl:o,params:r,backendBase:n.backendBase},h=st(t.options);h&&(a.options=h),typeof t.pageApi=="string"&&t.pageApi&&(a.pageApi=t.pageApi),t.storage&&typeof t.storage=="object"&&Object.keys(t.storage).length>0&&(a.storage=t.storage);let s=await D(a,"atkonoffice.open: launch");if(!s||typeof s.launchUrl!="string"||!s.launchUrl)throw new Error("atkonoffice.open: launch endpoint response missing launchUrl");let p=at(t);n.headers=A(p)?p:null;let m=N(s);return n.replayOpen=()=>q(e),n.onPairDowngrade=m?Ee:null,typeof s.wsTicket=="string"&&s.wsTicket&&typeof s.wsUrl=="string"&&s.wsUrl&&(n.wsUrl=s.wsUrl,n.pairId=typeof s.pairId=="string"?s.pairId:"",ye(n.pairId,n.wsUrl),n.wsReconnectAttempt=0,n.wsGaveUp=!1,m&&le(s),M(s.wsTicket)),m||B(s.launchUrl),{launchUrl:s.launchUrl,downloadUrl:typeof s.downloadUrl=="string"?s.downloadUrl:"",pairId:n.pairId,wsUrl:n.wsUrl}}function at(e){let t={},o=e.headers;if(Array.isArray(o)){for(let i of o)if(Array.isArray(i)&&i.length>=2)E(t,i[0],i[1]);else if(typeof i=="string"&&i.indexOf(":")!==-1){let a=i.indexOf(":");E(t,i.slice(0,a),i.slice(a+1).trim())}}else if(o&&typeof o=="object")for(let i of Object.keys(o))E(t,i,o[i]);let r=ce(e.cookie);return r&&E(t,"Cookie",r),e.storage&&typeof e.storage=="object"&&E(t,"X-Atkonoffice-Storage",JSON.stringify(e.storage)),t}function st(e){if(!e||typeof e!="object")return null;let t={};return typeof e.width=="number"&&(t.width=e.width),typeof e.height=="number"&&(t.height=e.height),typeof e.modal=="boolean"&&(t.modal=e.modal),Object.keys(t).length>0?t:null}var f={};var Re="atkonoffice-doc-host",$="__atkonofficeDocHostEvent",Q=["documentSaved","documentOpened","documentBeforeSave","error","controlReady","compareDiff"],ve={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave","compare.diff":"compareDiff"},Pe="controlReady",Se="toolbarClick",_e="readbackResult",l={eventHandlers:{},hintShown:!1,toolbarButtons:{},toolbarSeq:0,readbackPending:{},readbackSeq:0};function Oe(e,t,o){if(typeof e!="string")throw new TypeError("atkonoffice embed.addCustomToolButton: caption must be a string");if(typeof o!="function")throw new TypeError("atkonoffice embed.addCustomToolButton: onClick must be a function");if(typeof window>"u")return null;w();let r=typeof t=="number"&&isFinite(t)?Math.trunc(t):0,i="btn-"+ ++l.toolbarSeq;return l.toolbarButtons[i]=o,T("toolbarAdd",JSON.stringify({buttonId:i,caption:e,iconIndex:r}))?{remove:function(){delete l.toolbarButtons[i],T("toolbarRemove",JSON.stringify({buttonId:i}))}}:(delete l.toolbarButtons[i],Be(),null)}function Ie(e){if(typeof e!="boolean")throw new TypeError("atkonoffice embed.setCustomToolbarVisible: visible must be a boolean");return typeof window>"u"?f:(w(),T("toolbarSetVisible",JSON.stringify({visible:e})),f)}function Ce(e){let t=e&&e.buttonId;if(typeof t!="string"||!t)return;let o=l.toolbarButtons[t];if(typeof o=="function")try{o()}catch(r){Ne("addCustomToolButton onClick threw: "+(r&&r.message?r.message:r))}}function F(){if(typeof window>"u")return null;let e=window.atkonofficeCtrl=window.atkonofficeCtrl||{};return typeof e.save!="function"&&(e.save=function(t){let o=t&&typeof t=="object"?JSON.stringify(t):void 0;return typeof e._WebSave=="function"?e._WebSave(o):T("save",o)}),typeof e.exec!="function"&&(e.exec=function(t){return typeof t!="string"||!t?!1:T("executeJs",t)}),typeof e.setSavePath!="function"&&(e.setSavePath=function(t){return typeof t!="string"||!t?!1:T("setSavePath",t)}),e}function Ue(e){let t=F();return t&&typeof t.save=="function"?t.save(e):!1}function Ae(e){let t=F();return t&&typeof t.setSavePath=="function"?t.setSavePath(e):!1}function T(e,t){if(typeof window>"u")return!1;let o=window.__atkonofficeDocHost;return o&&typeof o.sendCommand=="function"?(t!=null?o.sendCommand(e,t):o.sendCommand(e),!0):!1}var ct=15e3;function S(e,t){if(typeof window>"u")return Promise.resolve({ok:!1,error:"non-browser"});w();let o=++l.readbackSeq,r=JSON.stringify({rid:o,method:e,args:Array.isArray(t)?t:[]});return new Promise(i=>{let a=setTimeout(function(){Z(o,{ok:!1,error:"timeout"})},ct);a&&typeof a.unref=="function"&&a.unref(),l.readbackPending[o]={resolve:i,timer:a},T("readback",r)||Z(o,{ok:!1,error:"shell-absent"})})}function Z(e,t){let o=l.readbackPending[e];o&&(delete l.readbackPending[e],clearTimeout(o.timer),o.resolve(t))}function De(e){let t=e&&e.rid;typeof t=="number"&&Z(t,{ok:!!(e&&e.ok),value:e?e.value:void 0,error:e?e.error:void 0})}function w(){ft(),F()}function He(e,t){if(typeof e!="string"||Q.indexOf(e)===-1)throw new TypeError('atkonoffice embed.on: unknown event "'+e+'" (expected one of '+Q.join(", ")+")");if(typeof t!="function")throw new TypeError("atkonoffice embed.on: handler must be a function");return w(),l.eventHandlers[e]||(l.eventHandlers[e]=[]),l.eventHandlers[e].push(t),f}function ft(){typeof window>"u"||typeof window[$]=="function"||(window[$]=function(e,t){if(e===Pe){Le("controlReady",t||{});return}if(e===Se){Ce(t||{});return}if(e===_e){De(t||{});return}let o=ve[e];o&&Le(o,t||{})})}function Le(e,t){let o=l.eventHandlers[e];if(!o||o.length===0){if(e==="error")try{console.error("[atkonoffice embed] "+(t&&t.message?t.message:t))}catch{}return}for(let r of o.slice())try{r(t)}catch{}}function Be(){if(!l.hintShown){l.hintShown=!0;try{console.info("[atkonoffice embed] <"+Re+"> \u672A\u88AB\u5B9A\u4E49 \u2014\u2014 \u8BF7\u5728\u5DF2\u5B89\u88C5 atkonoffice \u5BA2\u6237\u7AEF\u7684\u58F3\u5185\u6253\u5F00\u672C\u7F16\u8F91\u9875;\u666E\u901A\u6D4F\u89C8\u5668\u91CC\u5B83\u662F\u7A7A\u76D2(\u4E0D\u5F71\u54CD\u9875\u9762\u5176\u4F59\u90E8\u5206)\u3002")}catch{}}}function Ne(e){try{console.warn("[atkonoffice embed] "+e)}catch{}}function Me(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeSave: fn must be a function");return w(),je("onBeforeSave",e),f}function Fe(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeClose: fn must be a function");return w(),je("onBeforeClose",e),f}function je(e,t){if(typeof window>"u")return!1;let o=window.__atkonofficeDocHost;return o&&typeof o[e]=="function"?(o[e](t),!0):!1}var lt="AO_";function ee(e){return!!(e&&e.ok&&Number(e.value)>0)}function We(e,t){if(typeof e!="string"||!e)return Promise.resolve(!1);let o=typeof t=="string"?t:t==null?"":String(t);return S("Word_AddDataRegion",[e,o]).then(ee)}function Ke(e){return typeof e!="string"||!e?Promise.resolve(!1):S("Word_DeleteDataRegion",[e]).then(ee)}function Ve(e){return typeof e!="string"||!e?Promise.resolve(!1):S("Word_LocateDataRegion",[e]).then(ee)}function Je(e){let t=typeof e=="string"?e:lt;return S("BookmarksAsJson",[]).then(function(o){if(!o||!o.ok)return[];let r;try{r=JSON.parse(o.value)}catch{return[]}return Array.isArray(r)?r.filter(function(i){return typeof i=="string"&&(t===""||i.indexOf(t)===0)}):[]})}f.on=He;f.addCustomToolButton=Oe;f.setCustomToolbarVisible=Ie;f.save=Ue;f.setSavePath=Ae;f.onBeforeSave=Me;f.onBeforeClose=Fe;f.word={addDataRegion:We,deleteDataRegion:Ke,locateDataRegion:Ve,listDataRegions:Je};y.config=ke;y.open=q;y.on=xe;y.session=f;var ut=y,Pn=ut;export{ut as atkonoffice,Pn as default};
|