UNPKG

@onlyoffice/docspace-sdk-js

Version:

Simple integration JavaScript SDK for ONLYOFFICE DocSpace

19 lines (18 loc) 16.8 kB
"use strict";var DocSpace=(()=>{var de=Object.defineProperty;var B=o=>{throw TypeError(o)};var he=(o,e,t)=>e in o?de(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var g=(o,e,t)=>he(o,typeof e!="symbol"?e+"":e,t),A=(o,e,t)=>e.has(o)||B("Cannot "+t);var a=(o,e,t)=>(A(o,e,"read from private field"),t?t.call(o):e.get(o)),C=(o,e,t)=>e.has(o)?B("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(o):e.set(o,t),S=(o,e,t,n)=>(A(o,e,"write to private field"),n?n.call(o,t):e.set(o,t),t),h=(o,e,t)=>(A(o,e,"access private method"),t);var K="/api/2.0/security/csp",N="frameDocSpace",T={src:"",rootPath:"/rooms/shared/",requestToken:null,width:"100%",height:"100%",name:N,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}},G="The current domain is not set in the Content Security Policy (CSP) settings.",V="Message bus is not connected with frame";var b=o=>o?(Object.keys(o).forEach(e=>(o[e]===void 0||o[e]===null)&&delete o[e]),new URLSearchParams(o).toString()):"",W=async o=>{let{origin:e,host:t}=window.location;if(e.includes(o))return;let n=await fetch(`${o}${K}`),r;try{r=await n.json()}catch(f){throw new Error(`CSP validation failed: ${f}`)}let{response:{domains:i}}=r,s=t||new URL(e).host;if(!i.map(f=>{try{let m=new URL(f.toLowerCase());return m.host+(m.pathname!=="/"?m.pathname:"")}catch{return f}}).includes(s.toLowerCase()))throw new Error(G)},J=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">${G} 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>`,Y=o=>`@keyframes 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; } @media (prefers-color-scheme: dark) { .${o} { border-color: rgba(204, 204, 204, 0.1); border-top-color: #CCCCCC; } } @media (prefers-reduced-motion: reduce) { .${o} { animation-duration: 1.5s; } }`,Z=()=>{let o=document.currentScript,e=new URL(decodeURIComponent(o.src)).searchParams,t={...T};return e.forEach((n,r)=>{let i=n==="true"?!0:n==="false"?!1:n;T.filter&&r in T.filter?t.filter[r]=i:t[r]=i}),t.mode=e.get("mode")||"manager",t.src=e.get("src")||"",t},Q=o=>{var r;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:(r=o.events)!=null&&r.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 i=o.requestToken?{key:o.requestToken,...o.filter}:o.filter;i!=null&&i.withSubfolders||i==null||delete i.withSubfolders;let s=b(i);return`${o.rootPath}${o.requestToken?`?${s}`:`${o.id?o.id+"/":""}filter?${s}`}`}case"room-selector":{let i={...e,...t},s=b(i);return`/sdk/room-selector${s?`?${s}`:""}`}case"file-selector":{let i={...e,...t,breadCrumbs:o.withBreadCrumbs,filter:o.filterParam,id:o.id,selectorType:o.selectorType,subtitle:o.withSubtitle},s=b(i);return`/sdk/file-selector${s?`?${s}`:""}`}case"public-room":{let i={...e,folder:o.id,key:o.requestToken,showFilter:o.showFilter,showHeader:o.showHeader,showTitle:o.showTitle},s=b(i);return`/sdk/public-room${s?`?${s}`:""}`}case"system":{let i=b(e);return`/old-sdk/system${i?`?${i}`:""}`}case"editor":{let i={...e,...n},s=b(i);return`/doceditor${s?`?${s}`:""}`}case"viewer":{let i={...e,...n,action:"view"},s=b(i);return`/doceditor${s?`?${s}`:""}`}default:return o.rootPath||"/"}};var L,E,F,v,H,M,l,X,P,R,ee,te,oe,re,x,ne,ie,se,_,ae,le,ce,me,c,u=class u{constructor(e){C(this,l);C(this,L,!1);C(this,E,[]);C(this,F,[]);C(this,v,"");g(this,"config");C(this,H,e=>{let{frameId:t,width:n,height:r}=e,i=`${t}-loader__element`,s=`${n}_${r}`,d=u._loaderCache.style,f=u._loaderCache.templates;if(!d.has(i)){let p=document.createElement("style");p.textContent=Y(i);let y=document.createDocumentFragment();y.appendChild(p),document.head.appendChild(y),d.set(i,p)}let m;if(f.has(s))m=f.get(s).cloneNode(!0),m.id=`${t}-loader`;else{m=u._loaderCache.container.cloneNode(),Object.assign(m.style,{width:n,height:r,display:"flex",justifyContent:"center",alignItems:"center",position:"relative",zIndex:"1",backgroundColor:"transparent",transition:"opacity 0.15s ease-out"});let p=document.createElement("div");p.className=i,m.appendChild(p),f.set(s,m.cloneNode(!0)),m.id=`${t}-loader`}return m});C(this,M,e=>{if(!u._iframeCache){let O=document.createElement("iframe");O.allowFullscreen=!0,O.setAttribute("allow","storage-access *"),u._iframeCache={template:O,pathCache:new Map,styleCache:new Map}}let{mode:t,id:n,frameId:r,type:i,width:s,height:d,src:f,events:m,checkCSP:p}=e,y=i==="mobile",w=u._iframeCache.template.cloneNode(),U=`${t}_${n||""}_${r}`,z=`${s}_${d}_${y?"mobile":"desktop"}`,k=u._iframeCache.pathCache.get(U);k||(k=Q(e),u._iframeCache.pathCache.set(U,k)),w.id=r,w.name=`${N}__#${r}`,w.src=f+k;let $=u._iframeCache.styleCache.get(z);return $||($={width:s,height:d,border:"0px",opacity:"0",...y&&{position:"fixed",overflow:"hidden",webkitOverflowScrolling:"touch"}},u._iframeCache.styleCache.set(z,$)),Object.assign(w.style,$),y&&(document.body.style.overscrollBehaviorY!=="contain"&&(document.body.style.overscrollBehaviorY="contain"),"loading"in HTMLIFrameElement.prototype&&(w.loading="eager")),p&&h(this,l,X).call(this,w,f,m),w});C(this,P,e=>{try{let{frameId:t,src:n}=this.config,r=document.getElementById(t);if(!(r!=null&&r.contentWindow))return;let i={frameId:t,type:"",data:e};r.contentWindow.postMessage(JSON.stringify(i,(s,d)=>typeof d=="function"?d.toString():d),n)}catch(t){h(this,l,x).call(this,t)}});C(this,R,e=>{try{if(typeof e.data!="string")return;let t=h(this,l,ee).call(this,e.data);if(t.frameId!==this.config.frameId)return;switch(t.type){case"onMethodReturn":h(this,l,te).call(this,t);break;case"onEventReturn":t.eventReturnData&&h(this,l,oe).call(this,t.eventReturnData);break;case"onCallCommand":h(this,l,re).call(this,t);break;case"error":t.error&&h(this,l,x).call(this,t.error);break;default:console.warn("Unrecognized message type:",t.type)}}catch(t){h(this,l,x).call(this,t)}});C(this,c,(e,t=null)=>new Promise(n=>{h(this,l,ne).call(this,e,t,r=>n(r))}));this.config=e}setIsLoaded(){let{frameId:e,width:t,height:n,events:r}=this.config,i=document.getElementById(e),s=i==null?void 0:i.parentElement;!i||!s||requestAnimationFrame(()=>{var d,f;try{s.style.width=t,s.style.height=n;let m=document.getElementById(`${e}-loader`);m?(m.style.opacity="0",requestAnimationFrame(()=>{var p,y;try{m.parentNode&&m.parentNode.removeChild(m),(p=r==null?void 0:r.onContentReady)==null||p.call(r)}catch(w){console.error("Error removing loader:",w),(y=r==null?void 0:r.onContentReady)==null||y.call(r)}})):(d=r==null?void 0:r.onContentReady)==null||d.call(r),requestAnimationFrame(()=>{Object.assign(i.style,{opacity:"1",position:"relative",width:t,height:n})})}catch(m){console.error("Error in setIsLoaded:",m),(f=r==null?void 0:r.onContentReady)==null||f.call(r)}})}initFrame(e){this.config=h(this,l,ie).call(this,e);let t=h(this,l,se).call(this,this.config.frameId);if(!t)return null;let{container:n,target:r}=t,i=h(this,l,ae).call(this);return h(this,l,le).call(this,i),h(this,l,ce).call(this,n,r,i),h(this,l,me).call(this),i}destroyFrame(){var i,s;let e=this.config.frameId,t=document.getElementById(`${e}-container`),n=document.createElement("div");if(n.id=e,n.className=a(this,v),n.innerHTML=this.config.destroyText||"",t&&(t.parentNode?t.parentNode.replaceChild(n,t):document.body.appendChild(n),u._iframeCache)){let d=`${this.config.mode}_${this.config.id||""}_${this.config.frameId}`;u._iframeCache.pathCache.delete(d)}window.removeEventListener("message",a(this,R)),S(this,L,!1),S(this,E,[]),S(this,F,[]);let r=(s=(i=window.DocSpace)==null?void 0:i.SDK)==null?void 0:s.frames;r&&e in r&&delete r[e]}setConfig(e=T){return this.config={...this.config,...e},a(this,c).call(this,"setConfig",this.config)}getConfig(){return this.config}getFolderInfo(){return a(this,c).call(this,"getFolderInfo")}getSelection(){return a(this,c).call(this,"getSelection")}getFiles(){return a(this,c).call(this,"getFiles")}getFolders(){return a(this,c).call(this,"getFolders")}getList(){return a(this,c).call(this,"getList")}getRooms(e){return a(this,c).call(this,"getRooms",e)}getUserInfo(){return a(this,c).call(this,"getUserInfo")}getHashSettings(){return a(this,c).call(this,"getHashSettings")}openModal(e,t){return a(this,c).call(this,"openModal",{type:e,options:t})}createFile(e,t,n,r){return a(this,c).call(this,"createFile",{folderId:e,title:t,templateId:n,formId:r})}createFolder(e,t){return a(this,c).call(this,"createFolder",{parentFolderId:e,title:t})}createRoom(e,t,n,r,i,s,d,f){return a(this,c).call(this,"createRoom",{title:e,roomType:t,...n!==void 0&&{quota:n},...f!==void 0&&{denyDownload:f},...r!==void 0&&{tags:r},...i!==void 0&&{color:i},...s!==void 0&&{cover:s},...d!==void 0&&{indexing:d}})}setListView(e){return a(this,c).call(this,"setListView",{viewType:e})}createHash(e,t){return a(this,c).call(this,"createHash",{password:e,hashSettings:t})}login(e,t,n,r){return a(this,c).call(this,"login",{email:e,passwordHash:t,...n!==void 0&&{password:n},...r!==void 0&&{session:r}})}logout(){return a(this,c).call(this,"logout")}createTag(e){return a(this,c).call(this,"createTag",{name:e})}addTagsToRoom(e,t){return a(this,c).call(this,"addTagsToRoom",{roomId:e,tags:t})}removeTagsFromRoom(e,t){return a(this,c).call(this,"removeTagsFromRoom",{roomId:e,tags:t})}executeInEditor(e,t){a(this,c).call(this,"executeInEditor",{callback:e,data:t})}};L=new WeakMap,E=new WeakMap,F=new WeakMap,v=new WeakMap,H=new WeakMap,M=new WeakMap,l=new WeakSet,X=function(e,t,n){requestAnimationFrame(()=>{W(t).catch(r=>{var i;(i=n==null?void 0:n.onAppError)==null||i.call(n,r.message),e.srcdoc=J(t),this.setIsLoaded()})})},P=new WeakMap,R=new WeakMap,ee=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}}}},te=function(e){let t=a(this,E).shift();if(t)try{t(e.methodReturnData||{})}catch(n){console.error("Error in callback execution:",n)}a(this,F).length>0&&a(this,P).call(this,a(this,F).shift())},oe=function(e){var r;if(!(e!=null&&e.event))return;let t=e.event,n=(r=this.config.events)==null?void 0:r[t];if(typeof n=="function")try{n(e.data||{})}catch(i){console.error("Event handler failed:",t,i)}},re=function(e){if(!e.commandName)return;let t=this[e.commandName];typeof t=="function"&&t.call(this,e.commandData)},x=function(e){var t,n;console.error("SDK Error:",e),(n=(t=this.config.events)==null?void 0:t.onAppError)==null||n.call(t,e instanceof Error?e.message:"Unknown error occurred")},ne=function(e,t,n){if(!a(this,L)&&e!=="setConfig"){h(this,l,x).call(this,{message:V});return}a(this,E).push(n);let r={type:"method",methodName:e,data:t};a(this,E).length>1?a(this,F).push(r):a(this,P).call(this,r)},ie=function(e){let t={...this.config,...T,...e};return(t.mode==="manager"||t.mode==="system")&&(t.noLoader=!1),t},se=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 i=document.createElement("div");i.id=e,r.replaceChild(i,n);let s=`${this.config.mode}_${this.config.id||""}_${this.config.frameId}`;return u._iframeCache.pathCache.delete(s),h(this,l,_).call(this,i)}}return S(this,v,t.className),h(this,l,_).call(this,t)},_=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}},ae=function(){let e=a(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},le=function(e){let t=()=>{var n,r;window.addEventListener("message",a(this,R),!1),S(this,L,!0),this.config.noLoader&&((r=(n=this.config.events)==null?void 0:n.onContentReady)==null||r.call(n)),e.removeEventListener("load",t)};e.addEventListener("load",t)},ce=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 i=a(this,H).call(this,this.config);r.appendChild(i)}return e.appendChild(r),t!=null&&t.parentNode?(t.parentNode.insertBefore(e,t),t.remove()):t==null||t.replaceWith(e),n},me=function(){window.DocSpace.SDK.frames=window.DocSpace.SDK.frames||{},window.DocSpace.SDK.frames[this.config.frameId]=this},c=new WeakMap,g(u,"_loaderCache",{style:new Map,container:document.createElement("div"),templates:new Map}),g(u,"_iframeCache");var j=u;var D=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 j(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||{};var I=Z();window.DocSpace.SDK=window.DocSpace.SDK||new D;I!=null&&I.init&&window.DocSpace.SDK.init(I);})(); /** * (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 */