sandai-react
Version:
React components and utilities for the Sandai 3D AI Characters.
3 lines (2 loc) • 3.62 kB
JavaScript
import{useRef as e,useState as t,useEffect as r}from"react";function n(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,a,s,i,o=[],c=!0,m=!1;try{if(s=(r=r.call(e)).next,0===t);else for(;!(c=(n=s.call(r)).done)&&(o.push(n.value),o.length!==t);c=!0);}catch(e){m=!0,a=e}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(m)throw a}}return o}}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return a(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(e);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return a(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}class s{_iframe;_iframeUrl;constructor(e){this._iframe=e,this._iframeUrl=new URL(e.src)}_saySchema=[{name:"message",type:"string",label:"Message"}];say(e){this._sendMessage({type:"sendSpeech",message:e})}_stopSchema=[];stop(){this._sendMessage({type:"stopSpeech"})}_sendMessage(e){if(!this._iframe.contentWindow)throw new Error("iframe doesn't have a 'contentWindow' member");this._iframe.contentWindow.postMessage(e,"*")}_listenForMessage(e){window.addEventListener("message",(t=>{t.origin===this._iframeUrl.origin&&e(t.data)}))}_getDocs(){const e=new Map;e.set("Make the character say something",{schema:this._saySchema,func:this.say.bind(this)});const t=new Map;t.set("Make the character stop speaking",{schema:this._stopSchema,func:this.stop.bind(this)});const r=new Map;return r.set("say(message: string)",e),r.set("stop()",t),r}}class i{_iframe;_iframeUrl;interactionManager;constructor(e){const t=document.getElementById(e);if(!t)throw new Error(`Element with ID "${e}" not found.`);if(!(t instanceof HTMLIFrameElement))throw new Error(`Element with ID "${e}" is not an <iframe>. Tag was ${t.tagName}`);this._iframe=t,this._iframeUrl=new URL(t.src),this.interactionManager=new s(this._iframe)}_sendMessage(e){if(!this._iframe.contentWindow)throw new Error("iframe doesn't have a 'contentWindow' member");this._iframe.contentWindow.postMessage(e,"*")}_listenForMessage(e){window.addEventListener("message",(t=>{t.origin===this._iframeUrl.origin&&e(t.data)}))}_getDocs(){const e=this.interactionManager._getDocs(),t=new Map;return t.set("interactionManager",e),t}}class o{baseUrl;params;constructor(e){this.baseUrl=e,this.params=new URLSearchParams}setParam(e,t){return null!=t&&this.params.set(String(e),String(t)),this}build(){const e=new URL(this.baseUrl);return e.search=this.params.toString(),e.toString()}}var c=function(a){var s=e(),c=n(t(""),2),m=c[0],l=c[1];return r((function(){var e=new o(a.url).setParam("vrmUrl",a.vrmUrl).setParam("voiceName",a.voiceName).setParam("showControls",a.showControls).setParam("environment",a.environment).build();l(e)}),[a.vrmUrl,a.voiceName,a.showControls,a.url,a.environment]),r((function(){return s.current||(s.current=new i("sandai-frame",a.userId,a.apiKey)),s.current.init().then((function(){a.onLoad(s.current)})),function(){var e;null===(e=s.current)||void 0===e||e.destroy(),s.current=void 0}}),[a.url,a.onLoad]),React.createElement("iframe",{id:"sandai-frame",src:m,width:"100%",height:"100%",allowFullScreen:!0})};export{c as AI3DCharacter};
//# sourceMappingURL=index.esm.js.map