@onlyoffice/docspace-sdk-js
Version:
Simple integration JavaScript SDK for ONLYOFFICE DocSpace
19 lines (18 loc) • 16 kB
JavaScript
var ce=Object.defineProperty;var z=o=>{throw TypeError(o)};var me=(o,e,t)=>e in o?ce(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var g=(o,e,t)=>me(o,typeof e!="symbol"?e+"":e,t),O=(o,e,t)=>e.has(o)||z("Cannot "+t);var i=(o,e,t)=>(O(o,e,"read from private field"),t?t.call(o):e.get(o)),p=(o,e,t)=>e.has(o)?z("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(o):e.set(o,t),v=(o,e,t,n)=>(O(o,e,"write to private field"),n?n.call(o,t):e.set(o,t),t),d=(o,e,t)=>(O(o,e,"access private method"),t);var q="/api/2.0/security/csp",A="frameDocSpace",j={src:"",rootPath:"/rooms/shared/",requestToken:null,width:"100%",height:"100%",name:A,type:"desktop",frameId:"ds-frame",mode:"manager",id:null,locale:null,theme:"System",editorType:"desktop",editorGoBack:!0,selectorType:"all",showSelectorCancel:!1,showSelectorHeader:!1,showHeader:!1,showHeaderBanner:"none",showTitle:!0,showMenu:!1,showFilter:!1,showSignOut:!0,destroyText:"",viewAs:"row",viewTableColumns:"Index,Name,Size,Type,Tags",checkCSP:!0,disableActionButton:!1,showSettings:!1,waiting:!1,noLoader:!0,withSearch:!0,withBreadCrumbs:!0,withSubtitle:!0,filterParam:"ALL",buttonColor:"#5299E0",infoPanelVisible:!0,downloadToEvent:!1,filter:{count:"100",page:"1",sortOrder:"descending",sortBy:"DateAndTime",search:"",withSubfolders:!1},editorCustomization:{},events:{onSelectCallback:null,onCloseCallback:null,onAppReady:null,onAppError:null,onEditorCloseCallback:null,onAuthSuccess:null,onSignOut:null,onDownload:null,onNoAccess:null,onNotFound:null,onContentReady:null,onEditorOpen:null,onFileManagerClick:null}},N="The current domain is not set in the Content Security Policy (CSP) settings.",K="Message bus is not connected with frame";var w=o=>o?(Object.keys(o).forEach(e=>(o[e]===void 0||o[e]===null)&&delete o[e]),new URLSearchParams(o).toString()):"",V=async o=>{let{origin:e,host:t}=window.location;if(e.includes(o))return;let n=await fetch(`${o}${q}`),r;try{r=await n.json()}catch(f){throw new Error(`CSP validation failed: ${f}`)}let{response:{domains:s}}=r,m=t||new URL(e).host;if(!s.map(f=>{try{let u=new URL(f.toLowerCase());return u.host+(u.pathname!=="/"?u.pathname:"")}catch{return f}}).includes(m.toLowerCase()))throw new Error(N)},W=o=>`<body style=background:#f3f4f4><link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,300"rel=stylesheet><div style="display:flex;flex-direction:column;gap:80px;align-items:center;justify-content:flex-start;margin-top:60px;padding:0 30px"><div style=flex-shrink:0;position:relative><img src=${o}/static/images/logo/lightsmall.svg></div><div style=display:flex;flex-direction:column;gap:16px;align-items:center;justify-content:flex-start;flex-shrink:0;position:relative><div style=flex-shrink:0;width:120px;height:100px;position:relative><img src=${o}/static/images/frame-error.svg></div><span style="color:#a3a9ae;text-align:center;font-family:Open Sans;font-size:14px;font-style:normal;font-weight:700;line-height:16px">${N} Please add it via <a href=${o}/developer-tools/javascript-sdk style="color:#4781d1;text-align:center;font-family:Open Sans;font-size:14px;font-style:normal;font-weight:700;line-height:16px;text-decoration-line:underline"target=_blank>the Developer Tools section</a>.</span></div></div></body>`,J=o=>` rotate { 0%{ transform: rotate(-45deg); will-change: transform; } 15%{ transform: rotate(45deg); } 30%{ transform: rotate(135deg); } 45%{ transform: rotate(225deg); } 60%, 100%{ transform: rotate(315deg); } } .${o} { width: 74px; height: 74px; border: 4px solid rgba(51,51,51, 0.1); border-top-color: #333333; border-radius: 50%; transform: rotate(-45deg); position: relative; box-sizing: border-box; animation: 1s linear infinite rotate; will-change: transform; } (prefers-color-scheme: dark) { .${o} { border-color: rgba(204, 204, 204, 0.1); border-top-color: #CCCCCC; } } (prefers-reduced-motion: reduce) { .${o} { animation-duration: 1.5s; } }`;var Y=o=>{let e={theme:o.theme,locale:o.locale},t={acceptLabel:o.acceptButtonLabel,cancel:o.showSelectorCancel,cancelLabel:o.cancelButtonLabel,header:o.showSelectorHeader,roomType:o.roomType,search:o.withSearch},n={isSDK:!0,fileId:!o.id||o.id==="undefined"||o.id==="null"?-1:o.id,editorType:o.editorType,share:o.requestToken?o.requestToken:void 0,is_file:o.requestToken?!0:void 0,editorGoBack:o.events?.onEditorCloseCallback&&typeof o.events.onEditorCloseCallback=="function"?"event":o.editorGoBack?o.editorGoBack:void 0};switch(o.mode){case"manager":{o.id&&(o.filter.folder=o.id);let r=o.requestToken?{key:o.requestToken,...o.filter}:o.filter;r?.withSubfolders||delete r?.withSubfolders;let s=w(r);return`${o.rootPath}${o.requestToken?`?${s}`:`${o.id?o.id+"/":""}filter?${s}`}`}case"room-selector":{let r={...e,...t},s=w(r);return`/sdk/room-selector${s?`?${s}`:""}`}case"file-selector":{let r={...e,...t,breadCrumbs:o.withBreadCrumbs,filter:o.filterParam,id:o.id,selectorType:o.selectorType,subtitle:o.withSubtitle},s=w(r);return`/sdk/file-selector${s?`?${s}`:""}`}case"public-room":{let r={...e,folder:o.id,key:o.requestToken,showFilter:o.showFilter,showHeader:o.showHeader,showTitle:o.showTitle},s=w(r);return`/sdk/public-room${s?`?${s}`:""}`}case"system":{let r=w(e);return`/old-sdk/system${r?`?${r}`:""}`}case"editor":{let r={...e,...n},s=w(r);return`/doceditor${s?`?${s}`:""}`}case"viewer":{let r={...e,...n,action:"view"},s=w(r);return`/doceditor${s?`?${s}`:""}`}default:return o.rootPath||"/"}};var F,b,T,x,H,M,a,Z,L,P,Q,X,ee,te,S,oe,re,ne,G,se,ie,ae,le,l,h=class h{constructor(e){p(this,a);p(this,F,!1);p(this,b,[]);p(this,T,[]);p(this,x,"");g(this,"config");p(this,H,e=>{let{frameId:t,width:n,height:r}=e,s=`${t}-loader__element`,m=`${n}_${r}`,c=h._loaderCache.style,f=h._loaderCache.templates;if(!c.has(s)){let C=document.createElement("style");C.textContent=J(s);let E=document.createDocumentFragment();E.appendChild(C),document.head.appendChild(E),c.set(s,C)}let u;if(f.has(m))u=f.get(m).cloneNode(!0),u.id=`${t}-loader`;else{u=h._loaderCache.container.cloneNode(),Object.assign(u.style,{width:n,height:r,display:"flex",justifyContent:"center",alignItems:"center",position:"relative",zIndex:"1",backgroundColor:"transparent",transition:"opacity 0.15s ease-out"});let C=document.createElement("div");C.className=s,u.appendChild(C),f.set(m,u.cloneNode(!0)),u.id=`${t}-loader`}return u});p(this,M,e=>{if(!h._iframeCache){let I=document.createElement("iframe");I.allowFullscreen=!0,I.setAttribute("allow","storage-access *"),h._iframeCache={template:I,pathCache:new Map,styleCache:new Map}}let{mode:t,id:n,frameId:r,type:s,width:m,height:c,src:f,events:u,checkCSP:C}=e,E=s==="mobile",y=h._iframeCache.template.cloneNode(),_=`${t}_${n||""}_${r}`,U=`${m}_${c}_${E?"mobile":"desktop"}`,R=h._iframeCache.pathCache.get(_);R||(R=Y(e),h._iframeCache.pathCache.set(_,R)),y.id=r,y.name=`${A}__#${r}`,y.src=f+R;let $=h._iframeCache.styleCache.get(U);return $||($={width:m,height:c,border:"0px",opacity:"0",...E&&{position:"fixed",overflow:"hidden",webkitOverflowScrolling:"touch"}},h._iframeCache.styleCache.set(U,$)),Object.assign(y.style,$),E&&(document.body.style.overscrollBehaviorY!=="contain"&&(document.body.style.overscrollBehaviorY="contain"),"loading"in HTMLIFrameElement.prototype&&(y.loading="eager")),C&&d(this,a,Z).call(this,y,f,u),y});p(this,L,e=>{try{let{frameId:t,src:n}=this.config,r=document.getElementById(t);if(!r?.contentWindow)return;let s={frameId:t,type:"",data:e};r.contentWindow.postMessage(JSON.stringify(s,(m,c)=>typeof c=="function"?c.toString():c),n)}catch(t){d(this,a,S).call(this,t)}});p(this,P,e=>{try{if(typeof e.data!="string")return;let t=d(this,a,Q).call(this,e.data);if(t.frameId!==this.config.frameId)return;switch(t.type){case"onMethodReturn":d(this,a,X).call(this,t);break;case"onEventReturn":t.eventReturnData&&d(this,a,ee).call(this,t.eventReturnData);break;case"onCallCommand":d(this,a,te).call(this,t);break;case"error":t.error&&d(this,a,S).call(this,t.error);break;default:console.warn("Unrecognized message type:",t.type)}}catch(t){d(this,a,S).call(this,t)}});p(this,l,(e,t=null)=>new Promise(n=>{d(this,a,oe).call(this,e,t,r=>n(r))}));this.config=e}setIsLoaded(){let{frameId:e,width:t,height:n,events:r}=this.config,s=document.getElementById(e),m=s?.parentElement;!s||!m||requestAnimationFrame(()=>{try{m.style.width=t,m.style.height=n;let c=document.getElementById(`${e}-loader`);c?(c.style.opacity="0",requestAnimationFrame(()=>{try{c.parentNode&&c.parentNode.removeChild(c),r?.onContentReady?.()}catch(f){console.error("Error removing loader:",f),r?.onContentReady?.()}})):r?.onContentReady?.(),requestAnimationFrame(()=>{Object.assign(s.style,{opacity:"1",position:"relative",width:t,height:n})})}catch(c){console.error("Error in setIsLoaded:",c),r?.onContentReady?.()}})}initFrame(e){this.config=d(this,a,re).call(this,e);let t=d(this,a,ne).call(this,this.config.frameId);if(!t)return null;let{container:n,target:r}=t,s=d(this,a,se).call(this);return d(this,a,ie).call(this,s),d(this,a,ae).call(this,n,r,s),d(this,a,le).call(this),s}destroyFrame(){let e=this.config.frameId,t=document.getElementById(`${e}-container`),n=document.createElement("div");if(n.id=e,n.className=i(this,x),n.innerHTML=this.config.destroyText||"",t&&(t.parentNode?t.parentNode.replaceChild(n,t):document.body.appendChild(n),h._iframeCache)){let s=`${this.config.mode}_${this.config.id||""}_${this.config.frameId}`;h._iframeCache.pathCache.delete(s)}window.removeEventListener("message",i(this,P)),v(this,F,!1),v(this,b,[]),v(this,T,[]);let r=window.DocSpace?.SDK?.frames;r&&e in r&&delete r[e]}setConfig(e=j){return this.config={...this.config,...e},i(this,l).call(this,"setConfig",this.config)}getConfig(){return this.config}getFolderInfo(){return i(this,l).call(this,"getFolderInfo")}getSelection(){return i(this,l).call(this,"getSelection")}getFiles(){return i(this,l).call(this,"getFiles")}getFolders(){return i(this,l).call(this,"getFolders")}getList(){return i(this,l).call(this,"getList")}getRooms(e){return i(this,l).call(this,"getRooms",e)}getUserInfo(){return i(this,l).call(this,"getUserInfo")}getHashSettings(){return i(this,l).call(this,"getHashSettings")}openModal(e,t){return i(this,l).call(this,"openModal",{type:e,options:t})}createFile(e,t,n,r){return i(this,l).call(this,"createFile",{folderId:e,title:t,templateId:n,formId:r})}createFolder(e,t){return i(this,l).call(this,"createFolder",{parentFolderId:e,title:t})}createRoom(e,t,n,r,s,m,c,f){return i(this,l).call(this,"createRoom",{title:e,roomType:t,...n!==void 0&&{quota:n},...f!==void 0&&{denyDownload:f},...r!==void 0&&{tags:r},...s!==void 0&&{color:s},...m!==void 0&&{cover:m},...c!==void 0&&{indexing:c}})}setListView(e){return i(this,l).call(this,"setListView",{viewType:e})}createHash(e,t){return i(this,l).call(this,"createHash",{password:e,hashSettings:t})}login(e,t,n,r){return i(this,l).call(this,"login",{email:e,passwordHash:t,...n!==void 0&&{password:n},...r!==void 0&&{session:r}})}logout(){return i(this,l).call(this,"logout")}createTag(e){return i(this,l).call(this,"createTag",{name:e})}addTagsToRoom(e,t){return i(this,l).call(this,"addTagsToRoom",{roomId:e,tags:t})}removeTagsFromRoom(e,t){return i(this,l).call(this,"removeTagsFromRoom",{roomId:e,tags:t})}executeInEditor(e,t){i(this,l).call(this,"executeInEditor",{callback:e,data:t})}};F=new WeakMap,b=new WeakMap,T=new WeakMap,x=new WeakMap,H=new WeakMap,M=new WeakMap,a=new WeakSet,Z=function(e,t,n){requestAnimationFrame(()=>{V(t).catch(r=>{n?.onAppError?.(r.message),e.srcdoc=W(t),this.setIsLoaded()})})},L=new WeakMap,P=new WeakMap,Q=function(e){try{let t=JSON.parse(e);if(!t||typeof t!="object")throw new Error("Invalid message structure");return t.frameId||(t.frameId=""),t}catch(t){let n=t instanceof Error?t.message:"Unknown parsing error";return console.warn("Failed to parse message:",n),{frameId:"error",type:"error",commandName:"parseMessageData",error:{message:"Invalid message format: "+n}}}},X=function(e){let t=i(this,b).shift();if(t)try{t(e.methodReturnData||{})}catch(n){console.error("Error in callback execution:",n)}i(this,T).length>0&&i(this,L).call(this,i(this,T).shift())},ee=function(e){if(!e?.event)return;let t=e.event,n=this.config.events?.[t];if(typeof n=="function")try{n(e.data||{})}catch(r){console.error("Event handler failed:",t,r)}},te=function(e){if(!e.commandName)return;let t=this[e.commandName];typeof t=="function"&&t.call(this,e.commandData)},S=function(e){console.error("SDK Error:",e),this.config.events?.onAppError?.(e instanceof Error?e.message:"Unknown error occurred")},oe=function(e,t,n){if(!i(this,F)&&e!=="setConfig"){d(this,a,S).call(this,{message:K});return}i(this,b).push(n);let r={type:"method",methodName:e,data:t};i(this,b).length>1?i(this,T).push(r):i(this,L).call(this,r)},re=function(e){let t={...this.config,...j,...e};return(t.mode==="manager"||t.mode==="system")&&(t.noLoader=!1),t},ne=function(e){let t=document.getElementById(e);if(!t)return null;let n=document.getElementById(`${e}-container`);if(n){let r=n.parentNode;if(r){let s=document.createElement("div");s.id=e,r.replaceChild(s,n);let m=`${this.config.mode}_${this.config.id||""}_${this.config.frameId}`;return h._iframeCache.pathCache.delete(m),d(this,a,G).call(this,s)}}return v(this,x,t.className),d(this,a,G).call(this,t)},G=function(e){let t=document.createElement("div");return t.id=e.id+"-container",t.className="frame-container",Object.assign(t.style,{position:"relative",width:this.config.width,height:this.config.height}),{container:t,target:e}},se=function(){let e=i(this,M).call(this,this.config);return Object.assign(e.style,{opacity:this.config.noLoader?"1":"0",zIndex:"2",position:this.config.noLoader?"relative":"absolute",width:this.config.noLoader?this.config.width:"100%",height:this.config.noLoader?this.config.height:"100%",top:"0",left:"0"}),e},ie=function(e){let t=()=>{window.addEventListener("message",i(this,P),!1),v(this,F,!0),this.config.noLoader&&this.config.events?.onContentReady?.(),e.removeEventListener("load",t)};e.addEventListener("load",t)},ae=function(e,t,n){let r=document.createDocumentFragment();if((!this.config.waiting||this.config.mode==="system")&&r.appendChild(n),this.config.noLoader)e.style.height=this.config.height,e.style.width=this.config.width;else{let s=i(this,H).call(this,this.config);r.appendChild(s)}return e.appendChild(r),t?.parentNode?(t.parentNode.insertBefore(e,t),t.remove()):t?.replaceWith(e),n},le=function(){window.DocSpace.SDK.frames=window.DocSpace.SDK.frames||{},window.DocSpace.SDK.frames[this.config.frameId]=this},l=new WeakMap,g(h,"_loaderCache",{style:new Map,container:document.createElement("div"),templates:new Map}),g(h,"_iframeCache");var D=h;var k=class{constructor(){g(this,"frames",{});g(this,"init",e=>{let{frameId:t}=e,n=this.frames[t];if(n)return n.initFrame(e),n;let r=new D(e);return r.initFrame(e),this.frames[t]=r,r});g(this,"initFrame",e=>this.init(e));g(this,"initManager",e=>this.init({...e,mode:"manager"}));g(this,"initViewer",e=>this.init({...e,mode:"viewer"}));g(this,"initEditor",e=>this.init({...e,mode:"editor"}));g(this,"initRoomSelector",e=>this.init({...e,mode:"room-selector"}));g(this,"initFileSelector",e=>this.init({...e,mode:"file-selector"}));g(this,"initSystem",e=>this.init({...e,mode:"system"}))}};window.DocSpace=window.DocSpace||{};window.DocSpace.SDK=window.DocSpace.SDK||new k;var Oe=k;export{Oe as default};
/**
* (c) Copyright Ascensio System SIA 2025
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @license
*/