@atkonoffice/sdk 1.0.0 → 1.1.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/README.md +45 -0
- package/dist/atkonoffice.d.ts +3 -1
- package/dist/atkonoffice.js +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,7 +1,52 @@
|
|
|
1
1
|
# @atkonoffice/sdk
|
|
2
2
|
|
|
3
|
+
浏览器侧 SDK:在你的业务页里拉起 atkonoffice 编辑器编辑文档,编辑完成后收到保存回传。整个包只暴露一个 `atkonoffice` 对象。
|
|
4
|
+
|
|
3
5
|
## 安装
|
|
4
6
|
|
|
5
7
|
```bash
|
|
6
8
|
npm install @atkonoffice/sdk
|
|
7
9
|
```
|
|
10
|
+
|
|
11
|
+
## 用法
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { atkonoffice } from '@atkonoffice/sdk'
|
|
15
|
+
|
|
16
|
+
// 指向你的后端(拉起的签名工作在后端完成,密钥不进浏览器)
|
|
17
|
+
atkonoffice.config({ backendBase: 'https://your-backend.example.com' })
|
|
18
|
+
|
|
19
|
+
// 收保存回传:编辑器保存完成后回推到这里
|
|
20
|
+
atkonoffice.on('documentSaved', (e) => {
|
|
21
|
+
// e.doc_id 已保存到你的后端存储
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// 打开一份已存在的文档
|
|
25
|
+
await atkonoffice.open({ params: { docId: 'report.docx' } })
|
|
26
|
+
|
|
27
|
+
// 新建一份空白文档('word' | 'excel' | 'ppt'):把 hostPagePath 指向你后端对应的新建宿主页路由,
|
|
28
|
+
// 客户端会打开一份 SDK 自带空白模板。新文档的落盘命名由你的后端保存端点决定;若要以你为新文档预分配的 id
|
|
29
|
+
// 落盘,由你的编辑承载页在保存时透传(完整示例见参考集成 atkonoffice-integration)。
|
|
30
|
+
await atkonoffice.open({
|
|
31
|
+
create: 'word',
|
|
32
|
+
params: { docId: 'new-report.docx' },
|
|
33
|
+
hostPagePath: '/atkonoffice/host-page/create/word',
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// 你的业务编辑页有首屏登录守卫(进页先读 localStorage 判登录态,否则重定向登录页)时:把登录令牌作为
|
|
37
|
+
// storage 交给客户端。客户端内是独立的浏览器环境、localStorage 本是空的,直接打开会被守卫判未登录;传了
|
|
38
|
+
// storage,客户端会在你的编辑页首屏脚本执行**之前**把它逐键写进壳内 localStorage,守卫随即放行、直接进
|
|
39
|
+
// 编辑器(**编辑页无需为客户端做任何改造**)。
|
|
40
|
+
await atkonoffice.open({
|
|
41
|
+
page: 'https://your-app.example.com/host/edit?docId=report.docx',
|
|
42
|
+
params: { docId: 'report.docx' },
|
|
43
|
+
storage: { token: '<你的用户登录令牌>' },
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`storage` 是一份 `{ 键: 值 }`(值为字符串)快照,有**两个互补的消费面**,缺一不可:
|
|
48
|
+
|
|
49
|
+
- **前端 localStorage**:在编辑页首屏脚本执行前逐键写入壳内 `localStorage`,让你原有的登录守卫放行。
|
|
50
|
+
- **数据面鉴权**:同一份 `storage` 还会序列化进 `X-Atkonoffice-Storage` 头,随编辑器数据面请求发回你的后端做鉴权。
|
|
51
|
+
|
|
52
|
+
两处消费同一份 `storage`、不可互替——只保留其一会破坏另一侧能力。
|
package/dist/atkonoffice.d.ts
CHANGED
|
@@ -70,7 +70,7 @@ export interface AtkonofficeOpenRequest {
|
|
|
70
70
|
|
|
71
71
|
cookie?: Record<string, string> | string
|
|
72
72
|
|
|
73
|
-
storage?: Record<string,
|
|
73
|
+
storage?: Record<string, string>
|
|
74
74
|
|
|
75
75
|
options?: AtkonofficeWindowOptions
|
|
76
76
|
|
|
@@ -81,6 +81,8 @@ export interface AtkonofficeOpenRequest {
|
|
|
81
81
|
hostPagePath?: string
|
|
82
82
|
|
|
83
83
|
compareHost?: 'pageB' | string
|
|
84
|
+
|
|
85
|
+
create?: 'word' | 'excel' | 'ppt' | string
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
export interface AtkonofficeOpenResult {
|
package/dist/atkonoffice.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var h={};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},j="atkonoffice.ws.session",K=12,V=["documentSaved","documentOpened","documentBeforeSave","documentEmbedFailed","error"],g={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave"},R="atkonoffice-launch=",se="\u2007",O="pair.ready",ce="launch.open",fe="doc.embedFailed",C="__atkonofficeTitle",J="atkonoffice-install-guide",Y="atkonoffice-embed-failure-guide";function le(e){return e&&e.endsWith("/")?e.slice(0,-1):e}function A(e,t,o){let r=e.indexOf("?")===-1?"?":"&";return e+r+t+"="+o}function U(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 de(e){return e?typeof e=="string"?e:typeof e=="object"?Object.keys(e).map(t=>t+"="+String(e[t])).join("; "):"":""}function ue(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 G(){if(typeof window>"u")return!1;try{return window.top!==window.self}catch{return!0}}function N(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 B(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 z(){return typeof n.fetchImpl=="function"?n.fetchImpl:typeof globalThis<"u"&&typeof globalThis.fetch=="function"?globalThis.fetch.bind(globalThis):null}function Xe(){let e=z();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 Xe()(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 pe(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,q(),Ze()}function me(){!n.pairing||typeof document>"u"||(n.pairingTitleSaved||(n.pairingPrevTitle=k(),n.pairingTitleSaved=!0),ye(),Qe())}function ye(){v(L(n.wsUrl,n.pairId,n.launchTicket))}function L(e,t,o){let r=ue(JSON.stringify({wsUrl:e,pairId:t,launchTicket:o}));return se+R+r}function v(e){try{typeof document<"u"&&(document.title=e)}catch{}if(G())try{let t={};t[C]=e,window.top.postMessage(t,"*")}catch{}}function q(){n.titleRelayInstalled||typeof window>"u"||typeof window.addEventListener!="function"||G()||(n.titleRelayInstalled=!0,window.addEventListener("message",e=>{if(!$e(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 $e(e){let t=n.trustedFrameOrigins;return!t||t.length===0?!0:typeof e=="string"&&t.indexOf(e)!==-1}function Qe(){n.pairingRecoverTimer||typeof setInterval>"u"||(n.pairingRecoverTimer=setInterval(()=>{n.pairing&&k().indexOf(R)<0&&ye()},n.pairRecoverMs))}function Ze(){n.pairingTimeoutTimer||typeof setTimeout>"u"||(n.pairingTimeoutTimer=setTimeout(()=>{n.pairingTimeoutTimer=null,ge(!0)},n.pairTimeoutMs))}function he(){if(!n.pairing)return;let e=n.ws;ge(!1),e&&et(e)}function et(e){let t={v:1,type:"event",op:ce,ts:Math.floor(Date.now()/1e3),payload:{pair_id:n.pairId,openParams:n.openParams||{}}};try{e.send(JSON.stringify(t))}catch(o){l("error",new Error("atkonoffice: failed to send launch.open frame \u2014 "+x(o)))}}function ge(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&&(v(n.pairingPrevTitle),n.pairingTitleSaved=!1),e&&t&&(N(n.fallbackUrl),typeof n.onPairDowngrade=="function"&&n.onPairDowngrade())}var we="documentEmbedFailed";function Te(e){let t=e||{};l(we,t),$(we)||tt(typeof t.reason=="string"?t.reason:"")}function tt(e){if(typeof document>"u"||!document.body||typeof document.createElement!="function")return null;X();let t=document.createElement("div");t.setAttribute("data-atkonoffice-guide",Y),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,i;e==="wps_not_installed"?(r="\u672A\u68C0\u6D4B\u5230 WPS",i="\u672A\u68C0\u6D4B\u5230\u672C\u673A\u5DF2\u5B89\u88C5 WPS Office,\u65E0\u6CD5\u5728\u7F16\u8F91\u5668\u4E2D\u6253\u5F00\u6587\u6863\u3002\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u5B89\u88C5 WPS \u540E\u91CD\u8BD5;\u82E5\u786E\u5DF2\u5B89\u88C5\u4ECD\u63D0\u793A,\u8BF7\u5C1D\u8BD5\u4EE5\u666E\u901A\u6743\u9650(\u975E\u7BA1\u7406\u5458)\u91CD\u65B0\u6253\u5F00\u3002"):e==="doc_fetch_failed"?(r="\u65E0\u6CD5\u83B7\u53D6\u6587\u6863",i="\u6587\u6863\u4E0B\u8F7D\u6216\u53C2\u6570\u6821\u9A8C\u5931\u8D25(\u53EF\u80FD\u662F\u7F51\u7EDC\u5F02\u5E38\u3001\u767B\u5F55\u6001\u8FC7\u671F\u6216\u65E0\u8BBF\u95EE\u6743\u9650),\u7F16\u8F91\u5668\u672A\u5F00\u59CB\u52A0\u8F7D\u3002\u8BF7\u68C0\u67E5\u7F51\u7EDC\u4E0E\u767B\u5F55\u72B6\u6001\u540E\u70B9\u300C\u91CD\u8BD5\u300D,\u6216\u8054\u7CFB\u7BA1\u7406\u5458\u6392\u67E5\u3002"):(r="\u7F16\u8F91\u5668\u5D4C\u5165\u5931\u8D25",i="\u7F16\u8F91\u5668\u672A\u80FD\u52A0\u8F7D\u6587\u6863(\u5D4C\u5165\u5931\u8D25)\u3002\u8BF7\u70B9\u300C\u91CD\u8BD5\u300D,\u6216\u8054\u7CFB\u7BA1\u7406\u5458\u6392\u67E5\u3002");let a=document.createElement("h2");a.textContent=r,a.style.cssText="margin:0 0 12px;font-size:18px;";let p=document.createElement("p");p.style.cssText="margin:0 0 20px;color:#444;",p.textContent=i;let s=document.createElement("div");s.style.cssText="display:flex;gap:10px;justify-content:flex-end;flex-wrap:wrap;";let m="font:inherit;padding:8px 16px;border-radius:6px;cursor:pointer;border:1px solid #ccc;background:#f6f6f6;color:#222;",u=document.createElement("button");u.type="button",u.textContent="\u91CD\u8BD5",u.style.cssText=m,u.addEventListener("click",()=>{X();try{typeof n.replayOpen=="function"&&n.replayOpen()}catch{}});let c=document.createElement("button");return c.type="button",c.textContent="\u5173\u95ED",c.style.cssText=m,c.addEventListener("click",()=>X()),s.appendChild(u),s.appendChild(c),o.appendChild(a),o.appendChild(p),o.appendChild(s),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="'+Y+'"]');e&&typeof e.remove=="function"?e.remove():e&&e.parentNode&&typeof e.parentNode.removeChild=="function"&&e.parentNode.removeChild(e)}catch{}}function be(e,t){if(!(!e||typeof sessionStorage>"u"))try{sessionStorage.setItem(j,JSON.stringify({pairId:e,wsUrl:t}))}catch{}}function xe(){if(n.pairId)return{pairId:n.pairId,wsUrl:n.wsUrl};if(typeof sessionStorage>"u")return null;try{let e=sessionStorage.getItem(j);return e?JSON.parse(e):null}catch{return null}}function H(){if(n.ws||n.wsReconnectTimer||!n.backendBase)return;let e=xe();!e||!e.pairId||(n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,n.wsReconnectAttempt=0,Ee(e.pairId,t=>M(t)))}function M(e){if(typeof WebSocket>"u"){l("error",new Error("atkonoffice: WebSocket unavailable in this host \u2014 document events disabled"));return}if(!n.wsUrl){l("error",new Error("atkonoffice: no wsUrl \u2014 cannot open the document-event channel"));return}if(!e){l("error",new Error("atkonoffice: missing ws_ticket \u2014 cannot subscribe to document events"));return}nt();let t=A(n.wsUrl,"ticket",encodeURIComponent(e)),o;try{o=new WebSocket(t)}catch(r){l("error",new Error("atkonoffice: WS connect failed \u2014 "+x(r))),P();return}n.ws=o,o.onopen=()=>{n.wsReconnectAttempt=0,n.wsGaveUp=!1,ot(o),n.pairing&&me()},o.onmessage=r=>{rt(r&&r.data)},o.onerror=()=>{l("error",new Error("atkonoffice: WS error on the document-event channel"))},o.onclose=r=>{n.ws===o&&(n.ws=null),!(r&&r.code===1e3)&&P()}}function nt(){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 ot(e){if(!U(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){l("error",new Error("atkonoffice: failed to send headers frame \u2014 "+x(o)))}}function rt(e){if(typeof e!="string")return;let t;try{t=JSON.parse(e)}catch{return}if(!t||t.type!=="event")return;if(t.op===O){he();return}if(t.op===fe){Te(t.payload||{});return}let o=g[t.op];o&&l(o,t.payload||{})}function P(){if(n.wsReconnectTimer||n.wsGaveUp)return;let e=xe();if(!e||!e.pairId){l("error",new Error("atkonoffice: document-event channel dropped and no pair to resume"));return}if(n.wsReconnectAttempt>=K){n.wsGaveUp=!0;let r=new Error("atkonoffice: document-event channel lost \u2014 gave up after "+K+" consecutive reconnect attempts");r.terminal=!0,l("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,Ee(e.pairId,r=>{n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,M(r)})},o)}async function Ee(e,t){let o=z();if(!o||!n.backendBase){l("error",new Error("atkonoffice: cannot refresh ws_ticket \u2014 backend not configured")),P();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){l("error",new Error("atkonoffice: ws_ticket refresh returned HTTP "+r.status)),P();return}let i=await r.json();if(!i||typeof i.wsTicket!="string"||!i.wsTicket){l("error",new Error("atkonoffice: ws_ticket refresh response missing wsTicket")),P();return}typeof i.wsUrl=="string"&&i.wsUrl&&(n.wsUrl=i.wsUrl),t(i.wsTicket)}catch(r){l("error",new Error("atkonoffice: ws_ticket refresh failed \u2014 "+x(r))),P()}}function $(e){let t=n.eventHandlers[e];return!!t&&t.length>0}function it(e){for(let t in g)if(g.hasOwnProperty(t)&&g[t]===e)return!0;return!1}function ke(){for(let e in g)if(g.hasOwnProperty(e)&&$(g[e]))return!0;return!1}function l(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 ve(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),it(e)&&H(),h}function Pe(){at(n.pairTimeoutMs).then(e=>{e.installed||ct(e.clientDownloadUrl)})}function at(e){return new Promise(t=>{let o=!1,r=null,i=null,a=null,p=!1,s="",m="",u=()=>{if(i&&(clearTimeout(i),i=null),a&&(clearInterval(a),a=null),p&&(v(s),p=!1),r){r.onopen=r.onmessage=r.onerror=r.onclose=null;try{r.close(1e3,"probe_done")}catch{}r=null}},c=y=>{o||(o=!0,u(),t({installed:y,clientDownloadUrl:m}))};i=setTimeout(()=>c(!1),e),st().then(y=>{if(y&&typeof y.clientDownloadUrl=="string"&&(m=y.clientDownloadUrl),o||!B(y)){c(!1);return}let F=y.wsUrl,re=typeof y.pairId=="string"?y.pairId:"",ie=y.launchTicket;q();let b;try{b=new WebSocket(A(F,"ticket",encodeURIComponent(y.wsTicket)))}catch{c(!1);return}r=b,b.onopen=()=>{s=k(),p=!0,v(L(F,re,ie)),a=setInterval(()=>{!o&&k().indexOf(R)<0&&v(L(F,re,ie))},n.pairRecoverMs)},b.onmessage=W=>{let ae=W&&W.data;if(typeof ae!="string")return;let I;try{I=JSON.parse(ae)}catch{return}I&&I.type==="event"&&I.op===O&&c(!0)},b.onerror=()=>{},b.onclose=()=>{}}).catch(()=>{c(!1)})})}async function st(){return D({docId:"",page:"",openType:"",user:""},"atkonoffice: launch-probe")}function ct(e){if(typeof document>"u"||!document.body||typeof document.createElement!="function")return null;Q();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 p="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 c=document.createElement("a");c.textContent="\u4E0B\u8F7D\u5BA2\u6237\u7AEF",c.href=e,c.target="_blank",c.rel="noopener noreferrer",c.style.cssText=s,a.appendChild(c)}let m=document.createElement("button");m.type="button",m.textContent="\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5",m.style.cssText=p,m.addEventListener("click",()=>{Q();try{typeof n.replayOpen=="function"&&n.replayOpen()}catch{}});let u=document.createElement("button");return u.type="button",u.textContent="\u5173\u95ED",u.style.cssText=p,u.addEventListener("click",()=>Q()),a.appendChild(m),a.appendChild(u),o.appendChild(r),o.appendChild(i),o.appendChild(a),t.appendChild(o),document.body.appendChild(t),t}function Q(){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 Re(e){if(!e||typeof e!="object")throw new TypeError("atkonoffice.config requires an options object");if(typeof e.backendBase=="string"&&(n.backendBase=le(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,ke()&&H(),h}async function Z(e){let t=e||{};if(!n.backendBase)throw new Error("atkonoffice.open: backendBase not configured \u2014 call atkonoffice.config({ backendBase }) first");let o=t.page!=null?String(t.page):"",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={docId:i,page:o,openType:r.openType!=null?String(r.openType):"",user:r.user!=null?String(r.user):""},p=lt(t.options);p&&(a.options=p),typeof t.compareWith=="string"&&t.compareWith&&(a.compareWith=t.compareWith),typeof t.compareMode=="string"&&t.compareMode&&(a.compareMode=t.compareMode),typeof t.hostPagePath=="string"&&t.hostPagePath&&(a.hostPagePath=t.hostPagePath),typeof t.compareHost=="string"&&t.compareHost&&(a.compareHost=t.compareHost);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 m=ft(t);n.headers=U(m)?m:null;let u=B(s);return n.replayOpen=()=>Z(e),n.onPairDowngrade=u?Pe: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:"",be(n.pairId,n.wsUrl),n.wsReconnectAttempt=0,n.wsGaveUp=!1,u&&pe(s),M(s.wsTicket)),u||N(s.launchUrl),{launchUrl:s.launchUrl,downloadUrl:typeof s.downloadUrl=="string"?s.downloadUrl:"",pairId:n.pairId,wsUrl:n.wsUrl}}function ft(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=de(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 lt(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 Se="atkonoffice-doc-host",ee="__atkonofficeDocHostEvent",te=["documentSaved","documentOpened","documentBeforeSave","error","controlReady","compareDiff"],_e={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave","compare.diff":"compareDiff"},Ie="controlReady",Oe="toolbarClick",Ce="readbackResult",d={eventHandlers:{},hintShown:!1,toolbarButtons:{},toolbarSeq:0,readbackPending:{},readbackSeq:0};function Ae(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;T();let r=typeof t=="number"&&isFinite(t)?Math.trunc(t):0,i="btn-"+ ++d.toolbarSeq;return d.toolbarButtons[i]=o,w("toolbarAdd",JSON.stringify({buttonId:i,caption:e,iconIndex:r}))?{remove:function(){delete d.toolbarButtons[i],w("toolbarRemove",JSON.stringify({buttonId:i}))}}:(delete d.toolbarButtons[i],He(),null)}function Ue(e){if(typeof e!="boolean")throw new TypeError("atkonoffice embed.setCustomToolbarVisible: visible must be a boolean");return typeof window>"u"?f:(T(),w("toolbarSetVisible",JSON.stringify({visible:e})),f)}function Ne(e){let t=e&&e.buttonId;if(typeof t!="string"||!t)return;let o=d.toolbarButtons[t];if(typeof o=="function")try{o()}catch(r){Me("addCustomToolButton onClick threw: "+(r&&r.message?r.message:r))}}function S(){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):w("save",o)}),typeof e.exec!="function"&&(e.exec=function(t){return typeof t!="string"||!t?!1:w("executeJs",t)}),typeof e.setSavePath!="function"&&(e.setSavePath=function(t){return typeof t!="string"||!t?!1:w("setSavePath",t)}),typeof e.saveAsPdf!="function"&&(e.saveAsPdf=function(){return w("saveAsPdf")}),e}function Be(e){let t=S();return t&&typeof t.save=="function"?t.save(e):!1}function De(e){let t=S();return t&&typeof t.setSavePath=="function"?t.setSavePath(e):!1}function Le(){let e=S();return e&&typeof e.saveAsPdf=="function"?e.saveAsPdf():!1}function w(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 dt=15e3;function _(e,t){if(typeof window>"u")return Promise.resolve({ok:!1,error:"non-browser"});T();let o=++d.readbackSeq,r=JSON.stringify({rid:o,method:e,args:Array.isArray(t)?t:[]});return new Promise(i=>{let a=setTimeout(function(){ne(o,{ok:!1,error:"timeout"})},dt);a&&typeof a.unref=="function"&&a.unref(),d.readbackPending[o]={resolve:i,timer:a},w("readback",r)||ne(o,{ok:!1,error:"shell-absent"})})}function ne(e,t){let o=d.readbackPending[e];o&&(delete d.readbackPending[e],clearTimeout(o.timer),o.resolve(t))}function Fe(e){let t=e&&e.rid;typeof t=="number"&&ne(t,{ok:!!(e&&e.ok),value:e?e.value:void 0,error:e?e.error:void 0})}function T(){ut(),S()}function je(e,t){if(typeof e!="string"||te.indexOf(e)===-1)throw new TypeError('atkonoffice embed.on: unknown event "'+e+'" (expected one of '+te.join(", ")+")");if(typeof t!="function")throw new TypeError("atkonoffice embed.on: handler must be a function");return T(),d.eventHandlers[e]||(d.eventHandlers[e]=[]),d.eventHandlers[e].push(t),f}function ut(){typeof window>"u"||typeof window[ee]=="function"||(window[ee]=function(e,t){if(e===Ie){We("controlReady",t||{});return}if(e===Oe){Ne(t||{});return}if(e===Ce){Fe(t||{});return}let o=_e[e];o&&We(o,t||{})})}function We(e,t){let o=d.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 He(){if(!d.hintShown){d.hintShown=!0;try{console.info("[atkonoffice embed] <"+Se+"> \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 Me(e){try{console.warn("[atkonoffice embed] "+e)}catch{}}function Ke(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeSave: fn must be a function");return T(),Je("onBeforeSave",e),f}function Ve(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeClose: fn must be a function");return T(),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 pt="AO_";function oe(e){return!!(e&&e.ok&&Number(e.value)>0)}function Ye(e,t){if(typeof e!="string"||!e)return Promise.resolve(!1);let o=typeof t=="string"?t:t==null?"":String(t);return _("Word_AddDataRegion",[e,o]).then(oe)}function Ge(e){return typeof e!="string"||!e?Promise.resolve(!1):_("Word_DeleteDataRegion",[e]).then(oe)}function ze(e){return typeof e!="string"||!e?Promise.resolve(!1):_("Word_LocateDataRegion",[e]).then(oe)}function qe(e){let t=typeof e=="string"?e:pt;return _("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=je;f.addCustomToolButton=Ae;f.setCustomToolbarVisible=Ue;f.save=Be;f.setSavePath=De;f.saveAsPdf=Le;f.onBeforeSave=Ke;f.onBeforeClose=Ve;f.word={addDataRegion:Ye,deleteDataRegion:Ge,locateDataRegion:ze,listDataRegions:qe};h.config=Re;h.open=Z;h.on=ve;h.session=f;var mt=h,On=mt;export{mt as atkonoffice,On as default};
|
|
1
|
+
var h={};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},j="atkonoffice.ws.session",K=12,V=["documentSaved","documentOpened","documentBeforeSave","documentEmbedFailed","error"],g={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave"},R="atkonoffice-launch=",se="\u2007",I="pair.ready",ce="launch.open",fe="doc.embedFailed",C="__atkonofficeTitle",J="atkonoffice-install-guide",Y="atkonoffice-embed-failure-guide";function le(e){return e&&e.endsWith("/")?e.slice(0,-1):e}function A(e,t,o){let r=e.indexOf("?")===-1?"?":"&";return e+r+t+"="+o}function U(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 de(e){return e?typeof e=="string"?e:typeof e=="object"?Object.keys(e).map(t=>t+"="+String(e[t])).join("; "):"":""}function ue(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 G(){if(typeof window>"u")return!1;try{return window.top!==window.self}catch{return!0}}function N(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 B(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 z(){return typeof n.fetchImpl=="function"?n.fetchImpl:typeof globalThis<"u"&&typeof globalThis.fetch=="function"?globalThis.fetch.bind(globalThis):null}function Xe(){let e=z();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 Xe()(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 pe(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,q(),Ze()}function me(){!n.pairing||typeof document>"u"||(n.pairingTitleSaved||(n.pairingPrevTitle=k(),n.pairingTitleSaved=!0),ye(),Qe())}function ye(){v(L(n.wsUrl,n.pairId,n.launchTicket))}function L(e,t,o){let r=ue(JSON.stringify({wsUrl:e,pairId:t,launchTicket:o}));return se+R+r}function v(e){try{typeof document<"u"&&(document.title=e)}catch{}if(G())try{let t={};t[C]=e,window.top.postMessage(t,"*")}catch{}}function q(){n.titleRelayInstalled||typeof window>"u"||typeof window.addEventListener!="function"||G()||(n.titleRelayInstalled=!0,window.addEventListener("message",e=>{if(!$e(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 $e(e){let t=n.trustedFrameOrigins;return!t||t.length===0?!0:typeof e=="string"&&t.indexOf(e)!==-1}function Qe(){n.pairingRecoverTimer||typeof setInterval>"u"||(n.pairingRecoverTimer=setInterval(()=>{n.pairing&&k().indexOf(R)<0&&ye()},n.pairRecoverMs))}function Ze(){n.pairingTimeoutTimer||typeof setTimeout>"u"||(n.pairingTimeoutTimer=setTimeout(()=>{n.pairingTimeoutTimer=null,ge(!0)},n.pairTimeoutMs))}function he(){if(!n.pairing)return;let e=n.ws;ge(!1),e&&et(e)}function et(e){let t={v:1,type:"event",op:ce,ts:Math.floor(Date.now()/1e3),payload:{pair_id:n.pairId,openParams:n.openParams||{}}};try{e.send(JSON.stringify(t))}catch(o){l("error",new Error("atkonoffice: failed to send launch.open frame \u2014 "+x(o)))}}function ge(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&&(v(n.pairingPrevTitle),n.pairingTitleSaved=!1),e&&t&&(N(n.fallbackUrl),typeof n.onPairDowngrade=="function"&&n.onPairDowngrade())}var we="documentEmbedFailed";function Te(e){let t=e||{};l(we,t),$(we)||tt(typeof t.reason=="string"?t.reason:"")}function tt(e){if(typeof document>"u"||!document.body||typeof document.createElement!="function")return null;X();let t=document.createElement("div");t.setAttribute("data-atkonoffice-guide",Y),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,i;e==="wps_not_installed"?(r="\u672A\u68C0\u6D4B\u5230 WPS",i="\u672A\u68C0\u6D4B\u5230\u672C\u673A\u5DF2\u5B89\u88C5 WPS Office,\u65E0\u6CD5\u5728\u7F16\u8F91\u5668\u4E2D\u6253\u5F00\u6587\u6863\u3002\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u5B89\u88C5 WPS \u540E\u91CD\u8BD5;\u82E5\u786E\u5DF2\u5B89\u88C5\u4ECD\u63D0\u793A,\u8BF7\u5C1D\u8BD5\u4EE5\u666E\u901A\u6743\u9650(\u975E\u7BA1\u7406\u5458)\u91CD\u65B0\u6253\u5F00\u3002"):e==="doc_fetch_failed"?(r="\u65E0\u6CD5\u83B7\u53D6\u6587\u6863",i="\u6587\u6863\u4E0B\u8F7D\u6216\u53C2\u6570\u6821\u9A8C\u5931\u8D25(\u53EF\u80FD\u662F\u7F51\u7EDC\u5F02\u5E38\u3001\u767B\u5F55\u6001\u8FC7\u671F\u6216\u65E0\u8BBF\u95EE\u6743\u9650),\u7F16\u8F91\u5668\u672A\u5F00\u59CB\u52A0\u8F7D\u3002\u8BF7\u68C0\u67E5\u7F51\u7EDC\u4E0E\u767B\u5F55\u72B6\u6001\u540E\u70B9\u300C\u91CD\u8BD5\u300D,\u6216\u8054\u7CFB\u7BA1\u7406\u5458\u6392\u67E5\u3002"):(r="\u7F16\u8F91\u5668\u5D4C\u5165\u5931\u8D25",i="\u7F16\u8F91\u5668\u672A\u80FD\u52A0\u8F7D\u6587\u6863(\u5D4C\u5165\u5931\u8D25)\u3002\u8BF7\u70B9\u300C\u91CD\u8BD5\u300D,\u6216\u8054\u7CFB\u7BA1\u7406\u5458\u6392\u67E5\u3002");let a=document.createElement("h2");a.textContent=r,a.style.cssText="margin:0 0 12px;font-size:18px;";let p=document.createElement("p");p.style.cssText="margin:0 0 20px;color:#444;",p.textContent=i;let s=document.createElement("div");s.style.cssText="display:flex;gap:10px;justify-content:flex-end;flex-wrap:wrap;";let m="font:inherit;padding:8px 16px;border-radius:6px;cursor:pointer;border:1px solid #ccc;background:#f6f6f6;color:#222;",u=document.createElement("button");u.type="button",u.textContent="\u91CD\u8BD5",u.style.cssText=m,u.addEventListener("click",()=>{X();try{typeof n.replayOpen=="function"&&n.replayOpen()}catch{}});let c=document.createElement("button");return c.type="button",c.textContent="\u5173\u95ED",c.style.cssText=m,c.addEventListener("click",()=>X()),s.appendChild(u),s.appendChild(c),o.appendChild(a),o.appendChild(p),o.appendChild(s),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="'+Y+'"]');e&&typeof e.remove=="function"?e.remove():e&&e.parentNode&&typeof e.parentNode.removeChild=="function"&&e.parentNode.removeChild(e)}catch{}}function be(e,t){if(!(!e||typeof sessionStorage>"u"))try{sessionStorage.setItem(j,JSON.stringify({pairId:e,wsUrl:t}))}catch{}}function xe(){if(n.pairId)return{pairId:n.pairId,wsUrl:n.wsUrl};if(typeof sessionStorage>"u")return null;try{let e=sessionStorage.getItem(j);return e?JSON.parse(e):null}catch{return null}}function H(){if(n.ws||n.wsReconnectTimer||!n.backendBase)return;let e=xe();!e||!e.pairId||(n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,n.wsReconnectAttempt=0,Ee(e.pairId,t=>M(t)))}function M(e){if(typeof WebSocket>"u"){l("error",new Error("atkonoffice: WebSocket unavailable in this host \u2014 document events disabled"));return}if(!n.wsUrl){l("error",new Error("atkonoffice: no wsUrl \u2014 cannot open the document-event channel"));return}if(!e){l("error",new Error("atkonoffice: missing ws_ticket \u2014 cannot subscribe to document events"));return}nt();let t=A(n.wsUrl,"ticket",encodeURIComponent(e)),o;try{o=new WebSocket(t)}catch(r){l("error",new Error("atkonoffice: WS connect failed \u2014 "+x(r))),P();return}n.ws=o,o.onopen=()=>{n.wsReconnectAttempt=0,n.wsGaveUp=!1,ot(o),n.pairing&&me()},o.onmessage=r=>{rt(r&&r.data)},o.onerror=()=>{l("error",new Error("atkonoffice: WS error on the document-event channel"))},o.onclose=r=>{n.ws===o&&(n.ws=null),!(r&&r.code===1e3)&&P()}}function nt(){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 ot(e){if(!U(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){l("error",new Error("atkonoffice: failed to send headers frame \u2014 "+x(o)))}}function rt(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){he();return}if(t.op===fe){Te(t.payload||{});return}let o=g[t.op];o&&l(o,t.payload||{})}function P(){if(n.wsReconnectTimer||n.wsGaveUp)return;let e=xe();if(!e||!e.pairId){l("error",new Error("atkonoffice: document-event channel dropped and no pair to resume"));return}if(n.wsReconnectAttempt>=K){n.wsGaveUp=!0;let r=new Error("atkonoffice: document-event channel lost \u2014 gave up after "+K+" consecutive reconnect attempts");r.terminal=!0,l("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,Ee(e.pairId,r=>{n.pairId=e.pairId,n.wsUrl=e.wsUrl||n.wsUrl,M(r)})},o)}async function Ee(e,t){let o=z();if(!o||!n.backendBase){l("error",new Error("atkonoffice: cannot refresh ws_ticket \u2014 backend not configured")),P();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){l("error",new Error("atkonoffice: ws_ticket refresh returned HTTP "+r.status)),P();return}let i=await r.json();if(!i||typeof i.wsTicket!="string"||!i.wsTicket){l("error",new Error("atkonoffice: ws_ticket refresh response missing wsTicket")),P();return}typeof i.wsUrl=="string"&&i.wsUrl&&(n.wsUrl=i.wsUrl),t(i.wsTicket)}catch(r){l("error",new Error("atkonoffice: ws_ticket refresh failed \u2014 "+x(r))),P()}}function $(e){let t=n.eventHandlers[e];return!!t&&t.length>0}function it(e){for(let t in g)if(g.hasOwnProperty(t)&&g[t]===e)return!0;return!1}function ke(){for(let e in g)if(g.hasOwnProperty(e)&&$(g[e]))return!0;return!1}function l(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 ve(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),it(e)&&H(),h}function Pe(){at(n.pairTimeoutMs).then(e=>{e.installed||ct(e.clientDownloadUrl)})}function at(e){return new Promise(t=>{let o=!1,r=null,i=null,a=null,p=!1,s="",m="",u=()=>{if(i&&(clearTimeout(i),i=null),a&&(clearInterval(a),a=null),p&&(v(s),p=!1),r){r.onopen=r.onmessage=r.onerror=r.onclose=null;try{r.close(1e3,"probe_done")}catch{}r=null}},c=y=>{o||(o=!0,u(),t({installed:y,clientDownloadUrl:m}))};i=setTimeout(()=>c(!1),e),st().then(y=>{if(y&&typeof y.clientDownloadUrl=="string"&&(m=y.clientDownloadUrl),o||!B(y)){c(!1);return}let F=y.wsUrl,re=typeof y.pairId=="string"?y.pairId:"",ie=y.launchTicket;q();let b;try{b=new WebSocket(A(F,"ticket",encodeURIComponent(y.wsTicket)))}catch{c(!1);return}r=b,b.onopen=()=>{s=k(),p=!0,v(L(F,re,ie)),a=setInterval(()=>{!o&&k().indexOf(R)<0&&v(L(F,re,ie))},n.pairRecoverMs)},b.onmessage=W=>{let ae=W&&W.data;if(typeof ae!="string")return;let O;try{O=JSON.parse(ae)}catch{return}O&&O.type==="event"&&O.op===I&&c(!0)},b.onerror=()=>{},b.onclose=()=>{}}).catch(()=>{c(!1)})})}async function st(){return D({docId:"",page:"",openType:"",user:""},"atkonoffice: launch-probe")}function ct(e){if(typeof document>"u"||!document.body||typeof document.createElement!="function")return null;Q();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 p="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 c=document.createElement("a");c.textContent="\u4E0B\u8F7D\u5BA2\u6237\u7AEF",c.href=e,c.target="_blank",c.rel="noopener noreferrer",c.style.cssText=s,a.appendChild(c)}let m=document.createElement("button");m.type="button",m.textContent="\u6211\u5DF2\u5B89\u88C5,\u91CD\u8BD5",m.style.cssText=p,m.addEventListener("click",()=>{Q();try{typeof n.replayOpen=="function"&&n.replayOpen()}catch{}});let u=document.createElement("button");return u.type="button",u.textContent="\u5173\u95ED",u.style.cssText=p,u.addEventListener("click",()=>Q()),a.appendChild(m),a.appendChild(u),o.appendChild(r),o.appendChild(i),o.appendChild(a),t.appendChild(o),document.body.appendChild(t),t}function Q(){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 Re(e){if(!e||typeof e!="object")throw new TypeError("atkonoffice.config requires an options object");if(typeof e.backendBase=="string"&&(n.backendBase=le(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,ke()&&H(),h}async function Z(e){let t=e||{};if(!n.backendBase)throw new Error("atkonoffice.open: backendBase not configured \u2014 call atkonoffice.config({ backendBase }) first");let o=t.page!=null?String(t.page):"",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={docId:i,page:o,openType:r.openType!=null?String(r.openType):"",user:r.user!=null?String(r.user):""},p=lt(t.options);p&&(a.options=p),typeof t.compareWith=="string"&&t.compareWith&&(a.compareWith=t.compareWith),typeof t.compareMode=="string"&&t.compareMode&&(a.compareMode=t.compareMode),typeof t.hostPagePath=="string"&&t.hostPagePath&&(a.hostPagePath=t.hostPagePath),typeof t.compareHost=="string"&&t.compareHost&&(a.compareHost=t.compareHost),typeof t.create=="string"&&t.create&&(a.create=t.create),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 m=ft(t);n.headers=U(m)?m:null;let u=B(s);return n.replayOpen=()=>Z(e),n.onPairDowngrade=u?Pe: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:"",be(n.pairId,n.wsUrl),n.wsReconnectAttempt=0,n.wsGaveUp=!1,u&&pe(s),M(s.wsTicket)),u||N(s.launchUrl),{launchUrl:s.launchUrl,downloadUrl:typeof s.downloadUrl=="string"?s.downloadUrl:"",pairId:n.pairId,wsUrl:n.wsUrl}}function ft(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=de(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 lt(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 Se="atkonoffice-doc-host",ee="__atkonofficeDocHostEvent",te=["documentSaved","documentOpened","documentBeforeSave","error","controlReady","compareDiff"],_e={"doc.saved":"documentSaved","doc.opened":"documentOpened","doc.beforeSave":"documentBeforeSave","compare.diff":"compareDiff"},Oe="controlReady",Ie="toolbarClick",Ce="readbackResult",d={eventHandlers:{},hintShown:!1,toolbarButtons:{},toolbarSeq:0,readbackPending:{},readbackSeq:0};function Ae(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;T();let r=typeof t=="number"&&isFinite(t)?Math.trunc(t):0,i="btn-"+ ++d.toolbarSeq;return d.toolbarButtons[i]=o,w("toolbarAdd",JSON.stringify({buttonId:i,caption:e,iconIndex:r}))?{remove:function(){delete d.toolbarButtons[i],w("toolbarRemove",JSON.stringify({buttonId:i}))}}:(delete d.toolbarButtons[i],He(),null)}function Ue(e){if(typeof e!="boolean")throw new TypeError("atkonoffice embed.setCustomToolbarVisible: visible must be a boolean");return typeof window>"u"?f:(T(),w("toolbarSetVisible",JSON.stringify({visible:e})),f)}function Ne(e){let t=e&&e.buttonId;if(typeof t!="string"||!t)return;let o=d.toolbarButtons[t];if(typeof o=="function")try{o()}catch(r){Me("addCustomToolButton onClick threw: "+(r&&r.message?r.message:r))}}function S(){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):w("save",o)}),typeof e.exec!="function"&&(e.exec=function(t){return typeof t!="string"||!t?!1:w("executeJs",t)}),typeof e.setSavePath!="function"&&(e.setSavePath=function(t){return typeof t!="string"||!t?!1:w("setSavePath",t)}),typeof e.saveAsPdf!="function"&&(e.saveAsPdf=function(){return w("saveAsPdf")}),e}function Be(e){let t=S();return t&&typeof t.save=="function"?t.save(e):!1}function De(e){let t=S();return t&&typeof t.setSavePath=="function"?t.setSavePath(e):!1}function Le(){let e=S();return e&&typeof e.saveAsPdf=="function"?e.saveAsPdf():!1}function w(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 dt=15e3;function _(e,t){if(typeof window>"u")return Promise.resolve({ok:!1,error:"non-browser"});T();let o=++d.readbackSeq,r=JSON.stringify({rid:o,method:e,args:Array.isArray(t)?t:[]});return new Promise(i=>{let a=setTimeout(function(){ne(o,{ok:!1,error:"timeout"})},dt);a&&typeof a.unref=="function"&&a.unref(),d.readbackPending[o]={resolve:i,timer:a},w("readback",r)||ne(o,{ok:!1,error:"shell-absent"})})}function ne(e,t){let o=d.readbackPending[e];o&&(delete d.readbackPending[e],clearTimeout(o.timer),o.resolve(t))}function Fe(e){let t=e&&e.rid;typeof t=="number"&&ne(t,{ok:!!(e&&e.ok),value:e?e.value:void 0,error:e?e.error:void 0})}function T(){ut(),S()}function je(e,t){if(typeof e!="string"||te.indexOf(e)===-1)throw new TypeError('atkonoffice embed.on: unknown event "'+e+'" (expected one of '+te.join(", ")+")");if(typeof t!="function")throw new TypeError("atkonoffice embed.on: handler must be a function");return T(),d.eventHandlers[e]||(d.eventHandlers[e]=[]),d.eventHandlers[e].push(t),f}function ut(){typeof window>"u"||typeof window[ee]=="function"||(window[ee]=function(e,t){if(e===Oe){We("controlReady",t||{});return}if(e===Ie){Ne(t||{});return}if(e===Ce){Fe(t||{});return}let o=_e[e];o&&We(o,t||{})})}function We(e,t){let o=d.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 He(){if(!d.hintShown){d.hintShown=!0;try{console.info("[atkonoffice embed] <"+Se+"> \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 Me(e){try{console.warn("[atkonoffice embed] "+e)}catch{}}function Ke(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeSave: fn must be a function");return T(),Je("onBeforeSave",e),f}function Ve(e){if(typeof e!="function")throw new TypeError("atkonoffice embed.onBeforeClose: fn must be a function");return T(),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 pt="AO_";function oe(e){return!!(e&&e.ok&&Number(e.value)>0)}function Ye(e,t){if(typeof e!="string"||!e)return Promise.resolve(!1);let o=typeof t=="string"?t:t==null?"":String(t);return _("Word_AddDataRegion",[e,o]).then(oe)}function Ge(e){return typeof e!="string"||!e?Promise.resolve(!1):_("Word_DeleteDataRegion",[e]).then(oe)}function ze(e){return typeof e!="string"||!e?Promise.resolve(!1):_("Word_LocateDataRegion",[e]).then(oe)}function qe(e){let t=typeof e=="string"?e:pt;return _("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=je;f.addCustomToolButton=Ae;f.setCustomToolbarVisible=Ue;f.save=Be;f.setSavePath=De;f.saveAsPdf=Le;f.onBeforeSave=Ke;f.onBeforeClose=Ve;f.word={addDataRegion:Ye,deleteDataRegion:Ge,locateDataRegion:ze,listDataRegions:qe};h.config=Re;h.open=Z;h.on=ve;h.session=f;var mt=h,In=mt;export{mt as atkonoffice,In as default};
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atkonoffice/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Browser-side SDK for the atkonoffice editor shell",
|
|
5
|
+
"homepage": "https://atkon.yunhuichongming.cn",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"main": "./dist/atkonoffice.js",
|
|
7
8
|
"module": "./dist/atkonoffice.js",
|