@yomo/react-cursor-chat
Advanced tools
Comparing version 0.0.10 to 0.0.11
import React from 'react'; | ||
import './styles/cursor-chat.less'; | ||
declare const _default: React.MemoExoticComponent<({ presenceURL, presenceAuth, name, avatar, theme }: { | ||
declare const _default: React.MemoExoticComponent<({ presenceURL, presenceAuth, room, name, avatar, theme, }: { | ||
presenceURL: string; | ||
@@ -10,2 +10,3 @@ presenceAuth: { | ||
}; | ||
room?: string | undefined; | ||
name?: string | undefined; | ||
@@ -12,0 +13,0 @@ avatar?: string | undefined; |
import Me from '../cursor/me'; | ||
import Others from '../cursor/others'; | ||
declare const useOnlineCursor: ({ presenceURL, presenceAuth, name, avatar, }: { | ||
declare const useOnlineCursor: ({ presenceURL, presenceAuth, room, name, avatar, }: { | ||
presenceURL: string; | ||
@@ -10,2 +10,3 @@ presenceAuth: { | ||
}; | ||
room?: string | undefined; | ||
name?: string | undefined; | ||
@@ -12,0 +13,0 @@ avatar?: string | undefined; |
@@ -1073,3 +1073,5 @@ 'use strict'; | ||
})); | ||
return movement$.subscribe(yomo.ofRoom('001', 'movement')); | ||
return movement$.subscribe(function (data) { | ||
yomo.send('movement', data); | ||
}); | ||
}; | ||
@@ -1157,13 +1159,18 @@ | ||
presenceAuth = _ref.presenceAuth, | ||
room = _ref.room, | ||
name = _ref.name, | ||
avatar = _ref.avatar; | ||
var _useState = React.useState(null), | ||
me = _useState[0], | ||
setMe = _useState[1]; | ||
var _useState = React.useState(undefined), | ||
yomo = _useState[0], | ||
setYoMo = _useState[1]; | ||
var _useState2 = React.useState(new Map()), | ||
othersMap = _useState2[0], | ||
setOthersMap = _useState2[1]; | ||
var _useState2 = React.useState(null), | ||
me = _useState2[0], | ||
setMe = _useState2[1]; | ||
var _useState3 = React.useState(new Map()), | ||
othersMap = _useState3[0], | ||
setOthersMap = _useState3[1]; | ||
React.useEffect(function () { | ||
@@ -1182,4 +1189,4 @@ var ID = uuidv4(); | ||
}); | ||
setYoMo(yomo); | ||
yomo.on('connected', function () { | ||
yomo.toRoom('001'); | ||
yomo.on$('online').pipe(operators.filter(function (data) { | ||
@@ -1264,2 +1271,7 @@ return data.id !== ID; | ||
}, []); | ||
React.useEffect(function () { | ||
if (yomo && room) { | ||
yomo.toRoom(room); | ||
} | ||
}, [yomo, room]); | ||
var others = []; | ||
@@ -1377,4 +1389,4 @@ othersMap.forEach(function (value) { | ||
}), latencyData.latency > 0 && React__default.createElement("div", { | ||
className: 'online-cursor-wrapper__latency' | ||
}, "\uD83D\uDCCD ", latencyData.meshId, " ", React__default.createElement("span", { | ||
className: "online-cursor-wrapper__latency" | ||
}, "\uD83D\uDCCD ", latencyData.meshId, ' ', React__default.createElement("span", { | ||
style: { | ||
@@ -1420,4 +1432,4 @@ backgroundColor: latencyData.backgroundColor | ||
}), latencyData.latency > 0 && React__default.createElement("div", { | ||
className: 'online-cursor-wrapper__latency' | ||
}, "\uD83D\uDCCD ", latencyData.meshId, " ", React__default.createElement("span", { | ||
className: "online-cursor-wrapper__latency" | ||
}, "\uD83D\uDCCD ", latencyData.meshId, ' ', React__default.createElement("span", { | ||
style: { | ||
@@ -1439,2 +1451,3 @@ backgroundColor: latencyData.backgroundColor | ||
presenceAuth = _ref3.presenceAuth, | ||
room = _ref3.room, | ||
name = _ref3.name, | ||
@@ -1448,2 +1461,3 @@ avatar = _ref3.avatar, | ||
presenceAuth: presenceAuth, | ||
room: room, | ||
name: name, | ||
@@ -1450,0 +1464,0 @@ avatar: avatar |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,r=require("rxjs"),o=require("rxjs/operators"),i=require("@yomo/presencejs");function a(e,t,n,r,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,o)}function s(e){return function(){var t=this,n=arguments;return new Promise((function(r,o){var i=e.apply(t,n);function s(e){a(i,r,o,s,c,"next",e)}function c(e){a(i,r,o,s,c,"throw",e)}s(void 0)}))}}function c(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var u,l=(function(e){var t=function(e){var t=Object.prototype,n=t.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},o=r.iterator||"@@iterator",i=r.asyncIterator||"@@asyncIterator",a=r.toStringTag||"@@toStringTag";function s(e,t,n){return Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{s({},"")}catch(e){s=function(e,t,n){return e[t]=n}}function c(e,t,n,r){var o=Object.create((t&&t.prototype instanceof f?t:f).prototype),i=new S(r||[]);return o._invoke=function(e,t,n){var r="suspendedStart";return function(o,i){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===o)throw i;return{value:void 0,done:!0}}for(n.method=o,n.arg=i;;){var a=n.delegate;if(a){var s=x(a,n);if(s){if(s===l)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var c=u(e,t,n);if("normal"===c.type){if(r=n.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(r="completed",n.method="throw",n.arg=c.arg)}}}(e,n,i),o}function u(e,t,n){try{return{type:"normal",arg:e.call(t,n)}}catch(e){return{type:"throw",arg:e}}}e.wrap=c;var l={};function f(){}function h(){}function p(){}var d={};s(d,o,(function(){return this}));var v=Object.getPrototypeOf,m=v&&v(v(L([])));m&&m!==t&&n.call(m,o)&&(d=m);var y=p.prototype=f.prototype=Object.create(d);function b(e){["next","throw","return"].forEach((function(t){s(e,t,(function(e){return this._invoke(t,e)}))}))}function g(e,t){var r;this._invoke=function(o,i){function a(){return new t((function(r,a){!function r(o,i,a,s){var c=u(e[o],e,i);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==typeof f&&n.call(f,"__await")?t.resolve(f.__await).then((function(e){r("next",e,a,s)}),(function(e){r("throw",e,a,s)})):t.resolve(f).then((function(e){l.value=e,a(l)}),(function(e){return r("throw",e,a,s)}))}s(c.arg)}(o,i,r,a)}))}return r=r?r.then(a,a):a()}}function x(e,t){var n=e.iterator[t.method];if(void 0===n){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,x(e,t),"throw"===t.method))return l;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=u(n,e.iterator,t.arg);if("throw"===r.type)return t.method="throw",t.arg=r.arg,t.delegate=null,l;var o=r.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,l):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,l)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function S(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,i=function t(){for(;++r<e.length;)if(n.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:M}}function M(){return{value:void 0,done:!0}}return h.prototype=p,s(y,"constructor",p),s(p,"constructor",h),h.displayName=s(p,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,p):(e.__proto__=p,s(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},b(g.prototype),s(g.prototype,i,(function(){return this})),e.AsyncIterator=g,e.async=function(t,n,r,o,i){void 0===i&&(i=Promise);var a=new g(c(t,n,r,o),i);return e.isGeneratorFunction(n)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},b(y),s(y,a,"Generator"),s(y,o,(function(){return this})),s(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var n in e)t.push(n);return t.reverse(),function n(){for(;t.length;){var r=t.pop();if(r in e)return n.value=r,n.done=!1,n}return n.done=!0,n}},e.values=L,S.prototype={constructor:S,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&n.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function r(n,r){return a.type="throw",a.arg=e,t.next=n,r&&(t.method="next",t.arg=void 0),!!r}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return r("end");if(i.tryLoc<=this.prev){var s=n.call(i,"catchLoc"),c=n.call(i,"finallyLoc");if(s&&c){if(this.prev<i.catchLoc)return r(i.catchLoc,!0);if(this.prev<i.finallyLoc)return r(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return r(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return r(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),l},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),E(n),l}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var o=r.arg;E(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:L(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},e}(e.exports);try{regeneratorRuntime=t}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=t:Function("r","regeneratorRuntime = r")(t)}}(u={exports:{}}),u.exports),f=function(){return window.getViewportSize||(window.onresize=function(){window.getViewportSize={width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}},window.onresize()),window.getViewportSize},h=function(){function e(e,t,n,r,o){this.id=e,this.x=t,this.y=n,this.name=r,this.avatar=o,this.color=function(e){for(var t=0,n=0;n<e.length;n++)t=e.charCodeAt(n)+((t<<5)-t);for(var r="#",o=0;o<3;o++)r+=("00"+(t>>8*o&255).toString(16)).slice(-2);return r}(e)}var t=e.prototype;return t.move=function(e,t){this.x=e,this.y=t},t.onMove=function(e){},t.onGetLatency=function(e){},t.subscribeLatency=function(e){var t=this;return e.on$("latency").subscribe((function(e){e.id===t.id&&t.onGetLatency(e)}))},e}(),p=function(e){function t(t){var n,r=t.name,o=t.avatar;return(n=e.call(this,t.id,t.x,t.y,void 0===r?"":r,void 0===o?"":o)||this).mousemoveSubscription=n.subscribeMousemove(),n}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.yomo=t,this.online(t),this.onlineSubscription=this.subscribeOnline(t),this.mousePositionSubscription&&this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=this.subscribeMousePosition(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.goOffline=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return this.yomo&&this.yomo.send("offline",{id:this.id}),this.mousemoveSubscription&&(this.mousemoveSubscription.unsubscribe(),this.mousemoveSubscription=void 0),this.mousePositionSubscription&&(this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=void 0),this.onlineSubscription&&(this.onlineSubscription.unsubscribe(),this.onlineSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0),e.next=7,new Promise((function(e){setTimeout(e,500)}));case 7:return e.abrupt("return",e.sent);case 8:case"end":return e.stop()}}),e,this)})));return function(){return e.apply(this,arguments)}}(),n.sendMessage=function(e){this.yomo&&this.yomo.send("text",{id:this.id,message:e})},n.online=function(e){e.send("online",{id:this.id,x:0,y:0,name:this.name,avatar:this.avatar})},n.subscribeOnline=function(e){var t=this;return e.on$("online").subscribe((function(){e.send("sync",{id:t.id,x:t.x,y:t.y,name:t.name,avatar:t.avatar})}))},n.subscribeMousemove=function(){var t=this;return r.fromEvent(document,"mousemove").subscribe((function(n){var r=n.clientX,o=n.clientY;e.prototype.move.call(t,r,o),t.onMove({mouseX:r,mouseY:o})}))},n.subscribeMousePosition=function(e){var t=this;return r.fromEvent(document,"mousemove").pipe(o.map((function(e){var n,r,o=(n=e.clientY,{scaleX:e.clientX/(r=f()).width,scaleY:n/r.height});return{id:t.id,x:o.scaleX,y:o.scaleY}}))).subscribe(e.ofRoom("001","movement"))},t}(h),d=function(e){function t(t){var n=t.name,r=t.avatar;return e.call(this,t.id,t.x,t.y,void 0===n?"":n,void 0===r?"":r)||this}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.movementMessageSubscription=this.subscribeMovement(t),this.textMessageSubscription=this.subscribeTextMessage(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.unsubscribe=function(){this.textMessageSubscription&&(this.textMessageSubscription.unsubscribe(),this.textMessageSubscription=void 0),this.movementMessageSubscription&&(this.movementMessageSubscription.unsubscribe(),this.movementMessageSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0)},n.onTextMessage=function(e){},n.subscribeTextMessage=function(e){var t=this;return e.on$("text").pipe(o.filter((function(e){return e.id===t.id}))).subscribe((function(e){t.onTextMessage(e.message)}))},n.subscribeMovement=function(t){var n=this;return t.on$("movement").pipe(o.filter((function(e){return e.id===n.id})),o.map((function(e){return t=e.y,{mouseX:e.x*(n=f()).width,mouseY:t*n.height};var t,n}))).subscribe((function(t){var r=t.mouseX,o=t.mouseY;e.prototype.move.call(n,r,o),n.onMove({mouseX:r,mouseY:o})}))},t}(h),v=function(e){var n=e.presenceURL,r=e.presenceAuth,a=e.name,c=e.avatar,u=t.useState(null),f=u[0],h=u[1],v=t.useState(new Map),m=v[0],y=v[1];t.useEffect((function(){var e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})),t=new p({id:e,x:0,y:0,name:a||"",avatar:c||""});h(t);var u=new i.Presence(n,{auth:r});u.on("connected",(function(){u.toRoom("001"),u.on$("online").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){y((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(u),n.set(r.id,r),n}))})),u.on("offline",(function(e){y((function(t){var n=new Map(t),r=n.get(e.id);return r&&r.unsubscribe(),n.delete(e.id),n}))})),u.on$("sync").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){y((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(u),n.set(r.id,r),n}))})),t.goOnline(u)}));var f=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.goOffline();case 2:u.close();case 3:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}();return window.addEventListener("unload",f),function(){f(),window.removeEventListener("unload",f)}}),[]);var b=[];return m.forEach((function(e){b.push(e)})),{me:f,others:b}},m=function(e){var n=t.useRef(null);return t.useEffect((function(){var t=function(e){n.current&&n.current.setAttribute("style","transform: translate3d("+e.mouseX+"px, "+e.mouseY+"px, 0);")};t({mouseX:e.x,mouseY:e.y}),e.onMove=function(e){t(e)}}),[e]),n},y=function(e){var n=t.useState({meshId:"",latency:0,backgroundColor:"green"}),r=n[0],o=n[1];return t.useEffect((function(){e.onGetLatency=function(e){if(e.latency){var t="green";e.latency>=200&&e.latency<300&&(t="#FFB02A"),e.latency>=300&&(t="red"),o({backgroundColor:t,meshId:e.meshId,latency:e.latency})}}}),[]),r},b=function(e){var r=e.cursor,o=e.theme,i=m(r),a=t.useState(!1),s=a[0],c=a[1],u=t.useState(""),l=u[0],f=u[1],h=y(r),p=t.useCallback((function(e){"Slash"===e.code&&c(!0),"Escape"===e.code&&(c(!1),f(""),r.sendMessage(""))}),[]),d=t.useCallback((function(e){var t=e.target.value;"/"!==t&&(f(t),r.sendMessage(t))}),[]);return t.useEffect((function(){return document.addEventListener("keydown",p),function(){document.removeEventListener("keydown",p)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:i},n.createElement(x,{color:r.color}),h.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"📍 ",h.meshId," ",n.createElement("span",{style:{backgroundColor:h.backgroundColor}},h.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),s&&n.createElement("div",{className:"online-cursor-wrapper__input-box "+("light"===o?"light":"dark")},n.createElement("span",null,l),n.createElement("input",{autoFocus:!0,placeholder:"Say something",value:l,onChange:d})))}),[s,l,h])},g=function(e){var r=e.cursor,o=e.theme,i=m(r),a=t.useState(r.name),s=a[0],c=a[1],u=y(r);return t.useEffect((function(){r.onTextMessage=function(e){c(e)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:i},n.createElement(x,{color:r.color}),u.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"📍 ",u.meshId," ",n.createElement("span",{style:{backgroundColor:u.backgroundColor}},u.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),s&&n.createElement("div",{className:"online-cursor-wrapper__text "+("light"===o?"light":"dark")},s))}),[s,u])};function x(e){var r=e.color;return t.useMemo((function(){return n.createElement("svg",{shapeRendering:"geometricPrecision",xmlns:"http://www.w3.org/2000/svg",fill:r},n.createElement("path",{fill:"#666",d:"M9.63 6.9a1 1 0 011.27-1.27l11.25 3.75a1 1 0 010 1.9l-4.68 1.56a1 1 0 00-.63.63l-1.56 4.68a1 1 0 01-1.9 0L9.63 6.9z"}),n.createElement("path",{stroke:"#fff",strokeWidth:"1.5",d:"M11.13 4.92a1.75 1.75 0 00-2.2 2.21l3.74 11.26a1.75 1.75 0 003.32 0l1.56-4.68a.25.25 0 01.16-.16L22.4 12a1.75 1.75 0 000-3.32L11.13 4.92z"}))}),[r])}exports.default=t.memo((function(e){var t=e.theme,r=void 0===t?"dark":t,o=v({presenceURL:e.presenceURL,presenceAuth:e.presenceAuth,name:e.name,avatar:e.avatar}),i=o.me;return i?n.createElement("div",{className:"online-cursor-wrapper"},o.others.map((function(e){return n.createElement(g,{key:e.id,cursor:e,theme:r})})),n.createElement(b,{cursor:i,theme:r})):null})),exports.useOnlineCursor=v,exports.useRenderPosition=m; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,r=require("rxjs"),o=require("rxjs/operators"),i=require("@yomo/presencejs");function a(e,t,n,r,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,o)}function s(e){return function(){var t=this,n=arguments;return new Promise((function(r,o){var i=e.apply(t,n);function s(e){a(i,r,o,s,c,"next",e)}function c(e){a(i,r,o,s,c,"throw",e)}s(void 0)}))}}function c(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var u,l=(function(e){var t=function(e){var t=Object.prototype,n=t.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},o=r.iterator||"@@iterator",i=r.asyncIterator||"@@asyncIterator",a=r.toStringTag||"@@toStringTag";function s(e,t,n){return Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{s({},"")}catch(e){s=function(e,t,n){return e[t]=n}}function c(e,t,n,r){var o=Object.create((t&&t.prototype instanceof f?t:f).prototype),i=new S(r||[]);return o._invoke=function(e,t,n){var r="suspendedStart";return function(o,i){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===o)throw i;return{value:void 0,done:!0}}for(n.method=o,n.arg=i;;){var a=n.delegate;if(a){var s=x(a,n);if(s){if(s===l)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var c=u(e,t,n);if("normal"===c.type){if(r=n.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(r="completed",n.method="throw",n.arg=c.arg)}}}(e,n,i),o}function u(e,t,n){try{return{type:"normal",arg:e.call(t,n)}}catch(e){return{type:"throw",arg:e}}}e.wrap=c;var l={};function f(){}function h(){}function p(){}var d={};s(d,o,(function(){return this}));var m=Object.getPrototypeOf,v=m&&m(m(L([])));v&&v!==t&&n.call(v,o)&&(d=v);var y=p.prototype=f.prototype=Object.create(d);function b(e){["next","throw","return"].forEach((function(t){s(e,t,(function(e){return this._invoke(t,e)}))}))}function g(e,t){var r;this._invoke=function(o,i){function a(){return new t((function(r,a){!function r(o,i,a,s){var c=u(e[o],e,i);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==typeof f&&n.call(f,"__await")?t.resolve(f.__await).then((function(e){r("next",e,a,s)}),(function(e){r("throw",e,a,s)})):t.resolve(f).then((function(e){l.value=e,a(l)}),(function(e){return r("throw",e,a,s)}))}s(c.arg)}(o,i,r,a)}))}return r=r?r.then(a,a):a()}}function x(e,t){var n=e.iterator[t.method];if(void 0===n){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,x(e,t),"throw"===t.method))return l;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=u(n,e.iterator,t.arg);if("throw"===r.type)return t.method="throw",t.arg=r.arg,t.delegate=null,l;var o=r.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,l):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,l)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function S(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,i=function t(){for(;++r<e.length;)if(n.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:M}}function M(){return{value:void 0,done:!0}}return h.prototype=p,s(y,"constructor",p),s(p,"constructor",h),h.displayName=s(p,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,p):(e.__proto__=p,s(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},b(g.prototype),s(g.prototype,i,(function(){return this})),e.AsyncIterator=g,e.async=function(t,n,r,o,i){void 0===i&&(i=Promise);var a=new g(c(t,n,r,o),i);return e.isGeneratorFunction(n)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},b(y),s(y,a,"Generator"),s(y,o,(function(){return this})),s(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var n in e)t.push(n);return t.reverse(),function n(){for(;t.length;){var r=t.pop();if(r in e)return n.value=r,n.done=!1,n}return n.done=!0,n}},e.values=L,S.prototype={constructor:S,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&n.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function r(n,r){return a.type="throw",a.arg=e,t.next=n,r&&(t.method="next",t.arg=void 0),!!r}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return r("end");if(i.tryLoc<=this.prev){var s=n.call(i,"catchLoc"),c=n.call(i,"finallyLoc");if(s&&c){if(this.prev<i.catchLoc)return r(i.catchLoc,!0);if(this.prev<i.finallyLoc)return r(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return r(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return r(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),l},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),E(n),l}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var o=r.arg;E(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:L(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},e}(e.exports);try{regeneratorRuntime=t}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=t:Function("r","regeneratorRuntime = r")(t)}}(u={exports:{}}),u.exports),f=function(){return window.getViewportSize||(window.onresize=function(){window.getViewportSize={width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}},window.onresize()),window.getViewportSize},h=function(){function e(e,t,n,r,o){this.id=e,this.x=t,this.y=n,this.name=r,this.avatar=o,this.color=function(e){for(var t=0,n=0;n<e.length;n++)t=e.charCodeAt(n)+((t<<5)-t);for(var r="#",o=0;o<3;o++)r+=("00"+(t>>8*o&255).toString(16)).slice(-2);return r}(e)}var t=e.prototype;return t.move=function(e,t){this.x=e,this.y=t},t.onMove=function(e){},t.onGetLatency=function(e){},t.subscribeLatency=function(e){var t=this;return e.on$("latency").subscribe((function(e){e.id===t.id&&t.onGetLatency(e)}))},e}(),p=function(e){function t(t){var n,r=t.name,o=t.avatar;return(n=e.call(this,t.id,t.x,t.y,void 0===r?"":r,void 0===o?"":o)||this).mousemoveSubscription=n.subscribeMousemove(),n}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.yomo=t,this.online(t),this.onlineSubscription=this.subscribeOnline(t),this.mousePositionSubscription&&this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=this.subscribeMousePosition(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.goOffline=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return this.yomo&&this.yomo.send("offline",{id:this.id}),this.mousemoveSubscription&&(this.mousemoveSubscription.unsubscribe(),this.mousemoveSubscription=void 0),this.mousePositionSubscription&&(this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=void 0),this.onlineSubscription&&(this.onlineSubscription.unsubscribe(),this.onlineSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0),e.next=7,new Promise((function(e){setTimeout(e,500)}));case 7:return e.abrupt("return",e.sent);case 8:case"end":return e.stop()}}),e,this)})));return function(){return e.apply(this,arguments)}}(),n.sendMessage=function(e){this.yomo&&this.yomo.send("text",{id:this.id,message:e})},n.online=function(e){e.send("online",{id:this.id,x:0,y:0,name:this.name,avatar:this.avatar})},n.subscribeOnline=function(e){var t=this;return e.on$("online").subscribe((function(){e.send("sync",{id:t.id,x:t.x,y:t.y,name:t.name,avatar:t.avatar})}))},n.subscribeMousemove=function(){var t=this;return r.fromEvent(document,"mousemove").subscribe((function(n){var r=n.clientX,o=n.clientY;e.prototype.move.call(t,r,o),t.onMove({mouseX:r,mouseY:o})}))},n.subscribeMousePosition=function(e){var t=this;return r.fromEvent(document,"mousemove").pipe(o.map((function(e){var n,r,o=(n=e.clientY,{scaleX:e.clientX/(r=f()).width,scaleY:n/r.height});return{id:t.id,x:o.scaleX,y:o.scaleY}}))).subscribe((function(t){e.send("movement",t)}))},t}(h),d=function(e){function t(t){var n=t.name,r=t.avatar;return e.call(this,t.id,t.x,t.y,void 0===n?"":n,void 0===r?"":r)||this}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.movementMessageSubscription=this.subscribeMovement(t),this.textMessageSubscription=this.subscribeTextMessage(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.unsubscribe=function(){this.textMessageSubscription&&(this.textMessageSubscription.unsubscribe(),this.textMessageSubscription=void 0),this.movementMessageSubscription&&(this.movementMessageSubscription.unsubscribe(),this.movementMessageSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0)},n.onTextMessage=function(e){},n.subscribeTextMessage=function(e){var t=this;return e.on$("text").pipe(o.filter((function(e){return e.id===t.id}))).subscribe((function(e){t.onTextMessage(e.message)}))},n.subscribeMovement=function(t){var n=this;return t.on$("movement").pipe(o.filter((function(e){return e.id===n.id})),o.map((function(e){return t=e.y,{mouseX:e.x*(n=f()).width,mouseY:t*n.height};var t,n}))).subscribe((function(t){var r=t.mouseX,o=t.mouseY;e.prototype.move.call(n,r,o),n.onMove({mouseX:r,mouseY:o})}))},t}(h),m=function(e){var n=e.presenceURL,r=e.presenceAuth,a=e.room,c=e.name,u=e.avatar,f=t.useState(void 0),h=f[0],m=f[1],v=t.useState(null),y=v[0],b=v[1],g=t.useState(new Map),x=g[0],w=g[1];t.useEffect((function(){var e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})),t=new p({id:e,x:0,y:0,name:c||"",avatar:u||""});b(t);var a=new i.Presence(n,{auth:r});m(a),a.on("connected",(function(){a.on$("online").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){w((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(a),n.set(r.id,r),n}))})),a.on("offline",(function(e){w((function(t){var n=new Map(t),r=n.get(e.id);return r&&r.unsubscribe(),n.delete(e.id),n}))})),a.on$("sync").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){w((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(a),n.set(r.id,r),n}))})),t.goOnline(a)}));var f=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.goOffline();case 2:a.close();case 3:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}();return window.addEventListener("unload",f),function(){f(),window.removeEventListener("unload",f)}}),[]),t.useEffect((function(){h&&a&&h.toRoom(a)}),[h,a]);var E=[];return x.forEach((function(e){E.push(e)})),{me:y,others:E}},v=function(e){var n=t.useRef(null);return t.useEffect((function(){var t=function(e){n.current&&n.current.setAttribute("style","transform: translate3d("+e.mouseX+"px, "+e.mouseY+"px, 0);")};t({mouseX:e.x,mouseY:e.y}),e.onMove=function(e){t(e)}}),[e]),n},y=function(e){var n=t.useState({meshId:"",latency:0,backgroundColor:"green"}),r=n[0],o=n[1];return t.useEffect((function(){e.onGetLatency=function(e){if(e.latency){var t="green";e.latency>=200&&e.latency<300&&(t="#FFB02A"),e.latency>=300&&(t="red"),o({backgroundColor:t,meshId:e.meshId,latency:e.latency})}}}),[]),r},b=function(e){var r=e.cursor,o=e.theme,i=v(r),a=t.useState(!1),s=a[0],c=a[1],u=t.useState(""),l=u[0],f=u[1],h=y(r),p=t.useCallback((function(e){"Slash"===e.code&&c(!0),"Escape"===e.code&&(c(!1),f(""),r.sendMessage(""))}),[]),d=t.useCallback((function(e){var t=e.target.value;"/"!==t&&(f(t),r.sendMessage(t))}),[]);return t.useEffect((function(){return document.addEventListener("keydown",p),function(){document.removeEventListener("keydown",p)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:i},n.createElement(x,{color:r.color}),h.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"📍 ",h.meshId," ",n.createElement("span",{style:{backgroundColor:h.backgroundColor}},h.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),s&&n.createElement("div",{className:"online-cursor-wrapper__input-box "+("light"===o?"light":"dark")},n.createElement("span",null,l),n.createElement("input",{autoFocus:!0,placeholder:"Say something",value:l,onChange:d})))}),[s,l,h])},g=function(e){var r=e.cursor,o=e.theme,i=v(r),a=t.useState(r.name),s=a[0],c=a[1],u=y(r);return t.useEffect((function(){r.onTextMessage=function(e){c(e)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:i},n.createElement(x,{color:r.color}),u.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"📍 ",u.meshId," ",n.createElement("span",{style:{backgroundColor:u.backgroundColor}},u.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),s&&n.createElement("div",{className:"online-cursor-wrapper__text "+("light"===o?"light":"dark")},s))}),[s,u])};function x(e){var r=e.color;return t.useMemo((function(){return n.createElement("svg",{shapeRendering:"geometricPrecision",xmlns:"http://www.w3.org/2000/svg",fill:r},n.createElement("path",{fill:"#666",d:"M9.63 6.9a1 1 0 011.27-1.27l11.25 3.75a1 1 0 010 1.9l-4.68 1.56a1 1 0 00-.63.63l-1.56 4.68a1 1 0 01-1.9 0L9.63 6.9z"}),n.createElement("path",{stroke:"#fff",strokeWidth:"1.5",d:"M11.13 4.92a1.75 1.75 0 00-2.2 2.21l3.74 11.26a1.75 1.75 0 003.32 0l1.56-4.68a.25.25 0 01.16-.16L22.4 12a1.75 1.75 0 000-3.32L11.13 4.92z"}))}),[r])}exports.default=t.memo((function(e){var t=e.theme,r=void 0===t?"dark":t,o=m({presenceURL:e.presenceURL,presenceAuth:e.presenceAuth,room:e.room,name:e.name,avatar:e.avatar}),i=o.me;return i?n.createElement("div",{className:"online-cursor-wrapper"},o.others.map((function(e){return n.createElement(g,{key:e.id,cursor:e,theme:r})})),n.createElement(b,{cursor:i,theme:r})):null})),exports.useOnlineCursor=m,exports.useRenderPosition=v; | ||
//# sourceMappingURL=react-cursor-chat.cjs.production.min.js.map |
@@ -1066,3 +1066,5 @@ import React, { useState, useEffect, useRef, memo, useCallback, useMemo } from 'react'; | ||
})); | ||
return movement$.subscribe(yomo.ofRoom('001', 'movement')); | ||
return movement$.subscribe(function (data) { | ||
yomo.send('movement', data); | ||
}); | ||
}; | ||
@@ -1150,13 +1152,18 @@ | ||
presenceAuth = _ref.presenceAuth, | ||
room = _ref.room, | ||
name = _ref.name, | ||
avatar = _ref.avatar; | ||
var _useState = useState(null), | ||
me = _useState[0], | ||
setMe = _useState[1]; | ||
var _useState = useState(undefined), | ||
yomo = _useState[0], | ||
setYoMo = _useState[1]; | ||
var _useState2 = useState(new Map()), | ||
othersMap = _useState2[0], | ||
setOthersMap = _useState2[1]; | ||
var _useState2 = useState(null), | ||
me = _useState2[0], | ||
setMe = _useState2[1]; | ||
var _useState3 = useState(new Map()), | ||
othersMap = _useState3[0], | ||
setOthersMap = _useState3[1]; | ||
useEffect(function () { | ||
@@ -1175,4 +1182,4 @@ var ID = uuidv4(); | ||
}); | ||
setYoMo(yomo); | ||
yomo.on('connected', function () { | ||
yomo.toRoom('001'); | ||
yomo.on$('online').pipe(filter(function (data) { | ||
@@ -1257,2 +1264,7 @@ return data.id !== ID; | ||
}, []); | ||
useEffect(function () { | ||
if (yomo && room) { | ||
yomo.toRoom(room); | ||
} | ||
}, [yomo, room]); | ||
var others = []; | ||
@@ -1370,4 +1382,4 @@ othersMap.forEach(function (value) { | ||
}), latencyData.latency > 0 && React.createElement("div", { | ||
className: 'online-cursor-wrapper__latency' | ||
}, "\uD83D\uDCCD ", latencyData.meshId, " ", React.createElement("span", { | ||
className: "online-cursor-wrapper__latency" | ||
}, "\uD83D\uDCCD ", latencyData.meshId, ' ', React.createElement("span", { | ||
style: { | ||
@@ -1413,4 +1425,4 @@ backgroundColor: latencyData.backgroundColor | ||
}), latencyData.latency > 0 && React.createElement("div", { | ||
className: 'online-cursor-wrapper__latency' | ||
}, "\uD83D\uDCCD ", latencyData.meshId, " ", React.createElement("span", { | ||
className: "online-cursor-wrapper__latency" | ||
}, "\uD83D\uDCCD ", latencyData.meshId, ' ', React.createElement("span", { | ||
style: { | ||
@@ -1432,2 +1444,3 @@ backgroundColor: latencyData.backgroundColor | ||
presenceAuth = _ref3.presenceAuth, | ||
room = _ref3.room, | ||
name = _ref3.name, | ||
@@ -1441,2 +1454,3 @@ avatar = _ref3.avatar, | ||
presenceAuth: presenceAuth, | ||
room: room, | ||
name: name, | ||
@@ -1443,0 +1457,0 @@ avatar: avatar |
{ | ||
"name": "@yomo/react-cursor-chat", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "A react component for cursor chat", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -133,4 +133,6 @@ import { fromEvent, Subscription } from 'rxjs'; | ||
return movement$.subscribe(yomo.ofRoom('001', 'movement')); | ||
return movement$.subscribe(data => { | ||
yomo.send('movement', data); | ||
}); | ||
} | ||
} |
@@ -14,2 +14,3 @@ import { useEffect, useState } from 'react'; | ||
presenceAuth, | ||
room, | ||
name, | ||
@@ -26,5 +27,7 @@ avatar, | ||
}; | ||
room?: string; | ||
name?: string; | ||
avatar?: string; | ||
}) => { | ||
const [yomo, setYoMo] = useState<Presence | undefined>(undefined); | ||
const [me, setMe] = useState<Me | null>(null); | ||
@@ -52,5 +55,5 @@ const [othersMap, setOthersMap] = useState<Map<string, Others>>( | ||
setYoMo(yomo); | ||
yomo.on('connected', () => { | ||
yomo.toRoom('001'); | ||
yomo.on$<CursorMessage>('online') | ||
@@ -118,2 +121,8 @@ .pipe(filter(data => data.id !== ID)) | ||
useEffect(() => { | ||
if (yomo && room) { | ||
yomo.toRoom(room); | ||
} | ||
}, [yomo, room]); | ||
const others: Others[] = []; | ||
@@ -120,0 +129,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
379140
3430