UNPKG

sandai-react

Version:

React components and utilities for the Sandai 3D AI Characters.

3 lines (2 loc) 3.62 kB
"use strict";var e=require("react");function t(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 r(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return r(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 r(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 n{_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 a{_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 n(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 s{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()}}exports.AI3DCharacter=function(r){var n=e.useRef(),i=t(e.useState(""),2),o=i[0],c=i[1];return e.useEffect((function(){var e=new s(r.url).setParam("vrmUrl",r.vrmUrl).setParam("voiceName",r.voiceName).setParam("showControls",r.showControls).setParam("environment",r.environment).build();c(e)}),[r.vrmUrl,r.voiceName,r.showControls,r.url,r.environment]),e.useEffect((function(){return n.current||(n.current=new a("sandai-frame",r.userId,r.apiKey)),n.current.init().then((function(){r.onLoad(n.current)})),function(){var e;null===(e=n.current)||void 0===e||e.destroy(),n.current=void 0}}),[r.url,r.onLoad]),React.createElement("iframe",{id:"sandai-frame",src:o,width:"100%",height:"100%",allowFullScreen:!0})}; //# sourceMappingURL=index.cjs.js.map