@celljs/ta-web 4.40.0 → 4.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/browser/components/cloud-sign-in-view.d.ts +1 -1
- package/esm/browser/components/host-view.d.ts +1 -1
- package/esm/browser/extension/web-api-play.d.ts +9 -1
- package/esm/browser/extension/web-api-spec.js +1 -0
- package/esm/browser/extension/web-extension-context.js +2 -0
- package/esm/browser/i18n/en/chat.d.ts +1 -0
- package/esm/browser/i18n/en/chat.js +1 -0
- package/esm/browser/i18n/ja/chat.js +1 -0
- package/esm/browser/i18n/locales.d.ts +2 -1
- package/esm/browser/i18n/messages.d.ts +1 -0
- package/esm/browser/i18n/zh/chat.js +1 -0
- package/esm/browser/i18n/zh-tw/chat.js +1 -0
- package/esm/browser/sandbox/sandbox-runtime-code.js +1 -1
- package/esm/browser/sandbox/work/work-api.d.ts +9 -0
- package/esm/browser/sandbox/work/work-api.js +4 -8
- package/esm/browser/sandbox/work/work-docs.js +26 -2
- package/esm/browser/sandbox/work/work-frame.d.ts +22 -2
- package/esm/browser/sandbox/work/work-frame.js +60 -9
- package/esm/browser/sandbox/work/work-senses.d.ts +100 -0
- package/esm/browser/sandbox/work/work-senses.js +352 -0
- package/esm/browser/share/shared-extensions.js +13 -1
- package/esm/browser/share/shared-link.d.ts +3 -1
- package/esm/browser/share/shared-link.js +17 -1
- package/esm/browser/share/shared-page.js +27 -3
- package/esm/browser/style/shared.d.ts +1 -1
- package/esm/browser/style/shared.js +3 -0
- package/lib/browser/components/cloud-sign-in-view.d.ts +1 -1
- package/lib/browser/components/host-view.d.ts +1 -1
- package/lib/browser/extension/web-api-play.d.ts +9 -1
- package/lib/browser/extension/web-api-spec.js +1 -0
- package/lib/browser/extension/web-extension-context.js +2 -0
- package/lib/browser/i18n/en/chat.d.ts +1 -0
- package/lib/browser/i18n/en/chat.js +1 -0
- package/lib/browser/i18n/ja/chat.js +1 -0
- package/lib/browser/i18n/locales.d.ts +2 -1
- package/lib/browser/i18n/messages.d.ts +1 -0
- package/lib/browser/i18n/zh/chat.js +1 -0
- package/lib/browser/i18n/zh-tw/chat.js +1 -0
- package/lib/browser/sandbox/sandbox-runtime-code.js +1 -1
- package/lib/browser/sandbox/work/work-api.d.ts +9 -0
- package/lib/browser/sandbox/work/work-api.js +4 -8
- package/lib/browser/sandbox/work/work-docs.js +26 -2
- package/lib/browser/sandbox/work/work-frame.d.ts +22 -2
- package/lib/browser/sandbox/work/work-frame.js +60 -9
- package/lib/browser/sandbox/work/work-senses.d.ts +101 -0
- package/lib/browser/sandbox/work/work-senses.js +356 -0
- package/lib/browser/share/shared-extensions.js +13 -1
- package/lib/browser/share/shared-link.d.ts +3 -1
- package/lib/browser/share/shared-link.js +16 -0
- package/lib/browser/share/shared-page.js +27 -3
- package/lib/browser/style/shared.d.ts +1 -1
- package/lib/browser/style/shared.js +3 -0
- package/package.json +7 -7
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type TaWorkSenseName, type TaWorkSenseSpec } from '@celljs/ta-protocol';
|
|
1
2
|
/**
|
|
2
3
|
* The `ta` of an HTML work (docs/design/ta-works.md 7.2): what a document a model wrote reaches of Ta, from its frame of
|
|
3
4
|
* no origin and no network (WorkDocs). One table, WORK_API: the bridge in the frame is made from it, and the page checks
|
|
@@ -15,6 +16,8 @@ export interface WorkValues {
|
|
|
15
16
|
data: Record<string, unknown>;
|
|
16
17
|
/** The asks (WorkAsks) the page answers here: the bridge takes the others away, for `ta.ai?.(…)` to tell. */
|
|
17
18
|
can: string[];
|
|
19
|
+
/** The senses (TA_WORK_SENSES) the page reads here; for the others `ta.sense.on` draws a stand-in to touch. */
|
|
20
|
+
senses: string[];
|
|
18
21
|
}
|
|
19
22
|
/** What a work does: `ta.set(key, value)`… Each returns nothing to it. */
|
|
20
23
|
export interface WorkCalls {
|
|
@@ -38,6 +41,8 @@ export interface WorkEvents {
|
|
|
38
41
|
state(key: string, value: unknown, by?: string): void;
|
|
39
42
|
message(data: unknown, from?: string): void;
|
|
40
43
|
players(players: unknown[]): void;
|
|
44
|
+
/** A signal of a sense the page reads (WorkSenses): the bridge tells it to `ta.sense.on(name, …)`. */
|
|
45
|
+
sense(name: string, value: unknown): void;
|
|
41
46
|
}
|
|
42
47
|
/**
|
|
43
48
|
* An argument as the page takes it: `key` text of WorkLimits.KEY characters at most; `value` JSON of WorkLimits.VALUE
|
|
@@ -68,6 +73,10 @@ export interface WorkSpec {
|
|
|
68
73
|
readonly events: {
|
|
69
74
|
readonly [K in keyof WorkEvents]: WorkEffect;
|
|
70
75
|
};
|
|
76
|
+
/** What a work may sense, and the stand-in of each. */
|
|
77
|
+
readonly senses: {
|
|
78
|
+
readonly [K in TaWorkSenseName]: TaWorkSenseSpec;
|
|
79
|
+
};
|
|
71
80
|
}
|
|
72
81
|
export declare const WORK_API: WorkSpec;
|
|
73
82
|
/** What the page tells a work: an event, by its name and arguments. */
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
* The `ta` of an HTML work (docs/design/ta-works.md 7.2): what a document a model wrote reaches of Ta, from its frame of
|
|
3
|
-
* no origin and no network (WorkDocs). One table, WORK_API: the bridge in the frame is made from it, and the page checks
|
|
4
|
-
* by it what crosses (WorkFrame). The games of ta-play are the first works; what a work may do more (its model, its
|
|
5
|
-
* data) is added here, once.
|
|
6
|
-
*/
|
|
1
|
+
import { TA_WORK_SENSES } from '@celljs/ta-protocol';
|
|
7
2
|
export const WORK_API = {
|
|
8
|
-
values: { me: null, players: [], state: {}, solo: true, saved: null, data: {}, can: [] },
|
|
3
|
+
values: { me: null, players: [], state: {}, solo: true, saved: null, data: {}, can: [], senses: [] },
|
|
9
4
|
calls: {
|
|
10
5
|
set: { args: ['key', 'value'], rate: 'writes', patch: 'state' },
|
|
11
6
|
send: { args: ['value', 'text'], rate: 'sends' },
|
|
@@ -14,7 +9,8 @@ export const WORK_API = {
|
|
|
14
9
|
finish: { args: ['any'] },
|
|
15
10
|
},
|
|
16
11
|
asks: { ai: { args: ['any'], rate: 'asks' } },
|
|
17
|
-
events: { state: { patch: 'state' }, message: {}, players: { assign: 'players' } },
|
|
12
|
+
events: { state: { patch: 'state' }, message: {}, players: { assign: 'players' }, sense: {} },
|
|
13
|
+
senses: TA_WORK_SENSES,
|
|
18
14
|
};
|
|
19
15
|
export var WorkLimits;
|
|
20
16
|
(function (WorkLimits) {
|
|
@@ -24,12 +24,36 @@ function put(s,a){if(s.patch){var o=ta[s.patch]||(ta[s.patch]={});if(a[1]===null
|
|
|
24
24
|
function post(m){m.ta='work';try{parent.postMessage(m,'*')}catch(e){}}
|
|
25
25
|
function emit(t,a){(on[t]||[]).forEach(function(f){try{f.apply(null,a)}catch(e){console.error(e)}})}
|
|
26
26
|
Object.keys(S.calls).forEach(function(n){var s=S.calls[n];ta[n]=function(){var a=[].slice.call(arguments);put(s,a);post({type:'call',name:n,args:a})}});
|
|
27
|
+
var sw=[],sd={},inited=false,box,W={tilt:'Tilt',shake:'Shake',blow:'Hold to blow',loud:'Hold for a sound',move:'Move'};
|
|
28
|
+
ta.sense={on:function(n,f){(on['sense:'+n]=on['sense:'+n]||[]).push(f);if(sw.indexOf(n)<0){sw.push(n);if(inited)stand()}}};
|
|
29
|
+
ta.on('sense',function(n,v){emit('sense:'+n,[v])});
|
|
30
|
+
function fire(n,v){emit('sense:'+n,[v])}
|
|
31
|
+
var BTN='border:1px solid rgba(127,127,127,.45);background:rgba(127,127,127,.22);color:inherit;border-radius:999px;padding:10px 14px;font:inherit;-webkit-backdrop-filter:blur(6px);backdrop-filter:blur(6px)';
|
|
32
|
+
function mk(side){var d=document.createElement('div');d.setAttribute('style','position:fixed;bottom:12px;'+side+':12px;z-index:2147483646;display:flex;gap:8px;align-items:flex-end;font:600 13px system-ui,sans-serif;touch-action:none;-webkit-user-select:none;user-select:none');document.documentElement.appendChild(d);return d}
|
|
33
|
+
function boxes(){return box||(box={l:mk('left'),r:mk('right')})}
|
|
34
|
+
function btn(t,p){var b=document.createElement('button');b.type='button';b.textContent=t;b.setAttribute('style',BTN);p.appendChild(b);return b}
|
|
35
|
+
function hold(b,down,up){b.addEventListener('pointerdown',function(e){e.preventDefault();e.stopPropagation();try{b.setPointerCapture(e.pointerId)}catch(x){}down()});['pointerup','pointercancel'].forEach(function(n){b.addEventListener(n,function(e){e.stopPropagation();up()})});['click','mousedown','mouseup','touchstart','touchend'].forEach(function(n){b.addEventListener(n,function(e){e.stopPropagation()})})}
|
|
36
|
+
function pad(n,p){var o=document.createElement('div'),k=document.createElement('div'),go=false;o.setAttribute('style','width:88px;height:88px;border-radius:50%;position:relative;touch-action:none;'+BTN.replace('padding:10px 14px;',''));o.setAttribute('aria-label',W[n]||n);k.setAttribute('style','position:absolute;left:26px;top:26px;width:34px;height:34px;border-radius:50%;background:rgba(127,127,127,.6);pointer-events:none');o.appendChild(k);p.appendChild(o);
|
|
37
|
+
function at(e){var r=o.getBoundingClientRect(),x=(e.clientX-r.left-r.width/2)/(r.width/2),y=(e.clientY-r.top-r.height/2)/(r.height/2),d=Math.sqrt(x*x+y*y);if(d>1){x/=d;y/=d}k.style.transform='translate('+x*27+'px,'+y*27+'px)';fire(n,{x:Math.round(x*100)/100,y:Math.round(y*100)/100})}
|
|
38
|
+
o.addEventListener('pointerdown',function(e){e.preventDefault();e.stopPropagation();go=true;try{o.setPointerCapture(e.pointerId)}catch(x){}at(e)});o.addEventListener('pointermove',function(e){if(go){e.stopPropagation();at(e)}});['pointerup','pointercancel'].forEach(function(m){o.addEventListener(m,function(e){e.stopPropagation();go=false;k.style.transform='';fire(n,{x:0,y:0})})})}
|
|
39
|
+
function stand(){sw.forEach(function(n){var s=S.senses[n];if(!s||sd[n]||(ta.senses||[]).indexOf(n)>=0)return;sd[n]=1;var B=boxes(),v=0,t=null;
|
|
40
|
+
if(s.standIn==='pad')pad(n,B.l);
|
|
41
|
+
else if(s.standIn==='tap')hold(btn(W[n]||n,B.r),function(){fire(n,{power:1})},function(){});
|
|
42
|
+
else if(s.standIn==='hold')hold(btn(W[n]||n,B.r),function(){clearInterval(t);t=setInterval(function(){v=Math.min(1,v+.12);fire(n,Math.round(v*100)/100)},50)},function(){clearInterval(t);t=setInterval(function(){v=Math.max(0,v-.25);fire(n,Math.round(v*100)/100);if(!v)clearInterval(t)},50)});
|
|
43
|
+
else if(s.standIn==='sides')[-1,1].forEach(function(x){var b=btn(x<0?'\\u25C0':'\\u25B6',B.r),u=null;b.setAttribute('aria-label',W[n]||n);hold(b,function(){clearInterval(u);u=setInterval(function(){fire(n,{x:x,y:0,amount:1})},66)},function(){clearInterval(u);fire(n,{x:0,y:0,amount:0})})})})}
|
|
44
|
+
function tap(x,y){var t=document.elementFromPoint(x,y)||document.body;if(!t)return;['pointerdown','mousedown','pointerup','mouseup','click'].forEach(function(n){var e;try{e=/^pointer/.test(n)&&window.PointerEvent?new PointerEvent(n,{bubbles:true,cancelable:true,clientX:x,clientY:y,pointerId:1,isPrimary:true,pointerType:'touch'}):new MouseEvent(n,{bubbles:true,cancelable:true,clientX:x,clientY:y})}catch(x2){return}t.dispatchEvent(e)})}
|
|
45
|
+
function blank(){var b=document.body;if(!b)return true;if(document.querySelector('canvas,svg,img,video'))return false;if((b.innerText||'').replace(/\\s/g,'').length)return false;var a=b.getElementsByTagName('*');for(var i=0;i<a.length;i++){var r=a[i].getBoundingClientRect(),s=getComputedStyle(a[i]);if(r.width*r.height>400&&s.visibility!=='hidden'&&(s.backgroundColor!=='rgba(0, 0, 0, 0)'||s.backgroundImage!=='none'))return false}return true}
|
|
46
|
+
function probe(){var w=innerWidth,h=innerHeight;[[.5,.5],[.33,.5],[.66,.5],[.5,.75]].forEach(function(p,i){setTimeout(function(){tap(w*p[0],h*p[1])},150+i*200)});
|
|
47
|
+
setTimeout(function(){sw.forEach(function(n){var s=S.senses[n];if(s)fire(n,s.standIn==='pad'?{x:.5,y:-.5}:s.standIn==='sides'?{x:1,y:0,amount:1}:s.standIn==='tap'?{power:1}:.8)})},500);
|
|
48
|
+
setTimeout(function(){post({type:'probed',blank:blank()})},1600)}
|
|
27
49
|
var asked={},nid=0;function can(){Object.keys(S.asks).forEach(function(n){if((ta.can||[]).indexOf(n)<0)delete ta[n];else ta[n]=function(){var a=[].slice.call(arguments),id=++nid;return new Promise(function(ok,no){asked[id]=[ok,no];post({type:'ask',id:id,name:n,args:a})})}})}can();
|
|
28
50
|
addEventListener('message',function(e){var m=e.data;if(e.source!==parent||!m||m.ta!=='work')return;
|
|
29
|
-
if(m.type==='init'){Object.keys(S.values).forEach(function(k){if(m.values&&k in m.values)ta[k]=m.values[k]});can();ready(ta);emit('ready',[ta])}
|
|
51
|
+
if(m.type==='init'){Object.keys(S.values).forEach(function(k){if(m.values&&k in m.values)ta[k]=m.values[k]});can();if(m.words)Object.keys(m.words).forEach(function(k){W[k]=String(m.words[k])});var first=!inited;inited=true;stand();if(first){ready(ta);emit('ready',[ta])}else{emit('players',[ta.players])}}
|
|
30
52
|
else if(m.type==='answer'&&asked[m.id]){var w=asked[m.id];delete asked[m.id];if(m.error!==undefined)w[1](new Error(String(m.error)));else w[0](m.value)}
|
|
31
53
|
else if(m.type==='event'&&S.events.hasOwnProperty(m.name)){put(S.events[m.name],m.args||[]);emit(m.name,m.args||[])}
|
|
32
|
-
else if(m.type==='point')point(!!m.on)
|
|
54
|
+
else if(m.type==='point')point(!!m.on);
|
|
55
|
+
else if(m.type==='probe')probe();
|
|
56
|
+
else if(m.type==='senses'){ta.senses=m.names||[];stand()}});
|
|
33
57
|
var pk=null,EAT=['pointerdown','mousedown','mouseup','touchstart','touchend','click','dblclick','contextmenu','keydown','wheel'];
|
|
34
58
|
function path(el){var p=[];while(el&&el.nodeType===1&&el!==document.documentElement){if(el.id&&/^[A-Za-z][A-Za-z0-9_-]*$/.test(el.id)){p.unshift('#'+el.id);break}var i=1,s=el.previousElementSibling;for(;s;s=s.previousElementSibling)if(s.tagName===el.tagName)i++;p.unshift(el.tagName.toLowerCase()+(i>1?':nth-of-type('+i+')':''));el=el.parentElement}return p.join('>')}
|
|
35
59
|
function show(t){var r=t.getBoundingClientRect(),b=pk.box.style;b.display='block';b.left=r.left+'px';b.top=r.top+'px';b.width=r.width+'px';b.height=r.height+'px'}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import { TaWorkPoint } from '@celljs/ta-protocol';
|
|
1
|
+
import { TaWorkPoint, type TaWorkSenseName } from '@celljs/ta-protocol';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import { type WorkHost } from './work-api.js';
|
|
4
4
|
import { WorkDocs } from './work-docs.js';
|
|
5
|
+
import type { WorkSenses } from './work-senses.js';
|
|
6
|
+
/** What a work did as it was played alone for a moment, tapped here and there (WorkFrameProps.probe). */
|
|
7
|
+
export interface WorkProbe {
|
|
8
|
+
/** The errors of its scripts, each a line. */
|
|
9
|
+
errors: string[];
|
|
10
|
+
/** Nothing shows: no text, no picture, no canvas, no box with a colour. */
|
|
11
|
+
blank: boolean;
|
|
12
|
+
}
|
|
5
13
|
export interface WorkFrameProps {
|
|
6
14
|
/** The work's document, as written. */
|
|
7
15
|
html: string;
|
|
@@ -18,10 +26,22 @@ export interface WorkFrameProps {
|
|
|
18
26
|
* not to the work.
|
|
19
27
|
*/
|
|
20
28
|
onPoint?(point: TaWorkPoint): void;
|
|
29
|
+
/**
|
|
30
|
+
* What it senses here (docs/design/ta-works.md 7.9), asked in the user's tap before it started; without, or for a
|
|
31
|
+
* sense not here, the frame draws a stand-in to touch. The same one while it is played; stopped by who made it.
|
|
32
|
+
*/
|
|
33
|
+
senses?: WorkSenses;
|
|
34
|
+
/** The words of the stand-ins of the senses, in the user's language. */
|
|
35
|
+
senseWords?: Partial<Record<TaWorkSenseName, string>>;
|
|
36
|
+
/**
|
|
37
|
+
* Tries it before anyone plays it: taps it here and there, gives its senses a signal, and tells once, about two
|
|
38
|
+
* seconds in, what went wrong (docs/design/ta-play.md 7). For a frame no one sees.
|
|
39
|
+
*/
|
|
40
|
+
probe?(report: WorkProbe): void;
|
|
21
41
|
}
|
|
22
42
|
/**
|
|
23
43
|
* An HTML work in a frame the user sees (docs/design/ta-works.md 7.1, L3): a document apart (WorkDocs) that talks to the
|
|
24
44
|
* page by messages alone, only those of its own window heard; what it does checked by WORK_API before it reaches the
|
|
25
45
|
* host. Give it another React `key` to start it again.
|
|
26
46
|
*/
|
|
27
|
-
export declare function WorkFrame({ html, title, host, kind, className, style, onError, onPoint }: WorkFrameProps): React.JSX.Element;
|
|
47
|
+
export declare function WorkFrame({ html, title, host, kind, className, style, onError, onPoint, senses, senseWords, probe }: WorkFrameProps): React.JSX.Element;
|
|
@@ -2,12 +2,14 @@ import { TaWorkPoint } from '@celljs/ta-protocol';
|
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import { WORK_API, WorkCheck, WorkLimits, WorkRate } from './work-api.js';
|
|
4
4
|
import { WorkDocs } from './work-docs.js';
|
|
5
|
+
/** Errors of a page that are no fault of the work's. */
|
|
6
|
+
const HARMLESS = /ResizeObserver loop/i;
|
|
5
7
|
/**
|
|
6
8
|
* An HTML work in a frame the user sees (docs/design/ta-works.md 7.1, L3): a document apart (WorkDocs) that talks to the
|
|
7
9
|
* page by messages alone, only those of its own window heard; what it does checked by WORK_API before it reaches the
|
|
8
10
|
* host. Give it another React `key` to start it again.
|
|
9
11
|
*/
|
|
10
|
-
export function WorkFrame({ html, title, host, kind = 'game', className, style, onError, onPoint }) {
|
|
12
|
+
export function WorkFrame({ html, title, host, kind = 'game', className, style, onError, onPoint, senses, senseWords, probe }) {
|
|
11
13
|
const frame = React.useRef(null);
|
|
12
14
|
const document = React.useMemo(() => WorkDocs.document(html, kind), [html, kind]);
|
|
13
15
|
const errors = React.useRef(onError);
|
|
@@ -15,6 +17,12 @@ export function WorkFrame({ html, title, host, kind = 'game', className, style,
|
|
|
15
17
|
const points = React.useRef(onPoint);
|
|
16
18
|
points.current = onPoint;
|
|
17
19
|
const pointing = !!onPoint;
|
|
20
|
+
const words = React.useRef(senseWords);
|
|
21
|
+
words.current = senseWords;
|
|
22
|
+
const probed = React.useRef(probe);
|
|
23
|
+
probed.current = probe;
|
|
24
|
+
// The work said hello once: loaded.
|
|
25
|
+
const said = React.useRef(false);
|
|
18
26
|
React.useEffect(() => {
|
|
19
27
|
var _a, _b;
|
|
20
28
|
(_b = (_a = frame.current) === null || _a === void 0 ? void 0 : _a.contentWindow) === null || _b === void 0 ? void 0 : _b.postMessage({ type: 'point', on: pointing, ta: 'work' }, '*');
|
|
@@ -29,6 +37,24 @@ export function WorkFrame({ html, title, host, kind = 'game', className, style,
|
|
|
29
37
|
const toWork = (message) => { var _a; return (_a = element.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage({ ...message, ta: 'work' }, '*'); };
|
|
30
38
|
let opened;
|
|
31
39
|
let load = 0;
|
|
40
|
+
// Tried (probe): its errors until it answers, then told once.
|
|
41
|
+
const trying = probed.current ? { errors: [], timer: undefined, told: false } : undefined;
|
|
42
|
+
const tell = (blank) => {
|
|
43
|
+
var _a;
|
|
44
|
+
if (trying && !trying.told) {
|
|
45
|
+
trying.told = true;
|
|
46
|
+
clearTimeout(trying.timer);
|
|
47
|
+
(_a = probed.current) === null || _a === void 0 ? void 0 : _a.call(probed, { errors: [...new Set(trying.errors)].slice(0, 5), blank });
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const failed = (message) => {
|
|
51
|
+
var _a;
|
|
52
|
+
if (HARMLESS.test(message)) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
trying === null || trying === void 0 ? void 0 : trying.errors.push(message);
|
|
56
|
+
(_a = errors.current) === null || _a === void 0 ? void 0 : _a.call(errors, message);
|
|
57
|
+
};
|
|
32
58
|
const start = () => {
|
|
33
59
|
opened === null || opened === void 0 ? void 0 : opened.stop();
|
|
34
60
|
opened = undefined;
|
|
@@ -40,23 +66,37 @@ export function WorkFrame({ html, title, host, kind = 'game', className, style,
|
|
|
40
66
|
queue ? queue.push(event) : toWork(event);
|
|
41
67
|
};
|
|
42
68
|
host.open(emit).then(({ values, stop }) => {
|
|
69
|
+
var _a;
|
|
43
70
|
if (now !== load) {
|
|
44
71
|
stop();
|
|
45
72
|
return;
|
|
46
73
|
}
|
|
47
|
-
|
|
74
|
+
const unsense = senses === null || senses === void 0 ? void 0 : senses.listen((name, value) => toWork({ type: 'event', name: 'sense', args: [name, value] }),
|
|
75
|
+
// A sense said to be here gave nothing: a stand-in for it now.
|
|
76
|
+
() => toWork({ type: 'senses', names: senses.names }));
|
|
77
|
+
opened = {
|
|
78
|
+
stop: () => {
|
|
79
|
+
unsense === null || unsense === void 0 ? void 0 : unsense();
|
|
80
|
+
stop();
|
|
81
|
+
},
|
|
82
|
+
};
|
|
48
83
|
// What it may ask is what the host answers, no more: of those, what the host says it answers here.
|
|
49
84
|
const can = Object.keys(WORK_API.asks).filter(name => typeof host[name] === 'function' && (!values.can || values.can.includes(name)));
|
|
50
|
-
toWork({ type: 'init', values: { ...values, can } });
|
|
85
|
+
toWork({ type: 'init', values: { ...values, can, senses: (_a = senses === null || senses === void 0 ? void 0 : senses.names) !== null && _a !== void 0 ? _a : [] }, ...(words.current ? { words: words.current } : {}) });
|
|
51
86
|
for (const event of queue !== null && queue !== void 0 ? queue : []) {
|
|
52
87
|
toWork(event);
|
|
53
88
|
}
|
|
54
89
|
queue = undefined;
|
|
55
|
-
|
|
90
|
+
if (trying && !trying.timer) {
|
|
91
|
+
toWork({ type: 'probe' });
|
|
92
|
+
// One that never answers (its scripts stuck): told what it said until then.
|
|
93
|
+
trying.timer = setTimeout(() => tell(false), 5000);
|
|
94
|
+
}
|
|
95
|
+
}, error => failed(String(error instanceof Error ? error.message : error).slice(0, WorkLimits.ERROR)));
|
|
56
96
|
};
|
|
57
97
|
const heard = (event) => {
|
|
58
|
-
var _a, _b
|
|
59
|
-
var
|
|
98
|
+
var _a, _b;
|
|
99
|
+
var _c;
|
|
60
100
|
if (!element.contentWindow || event.source !== element.contentWindow) {
|
|
61
101
|
return;
|
|
62
102
|
}
|
|
@@ -66,6 +106,7 @@ export function WorkFrame({ html, title, host, kind = 'game', className, style,
|
|
|
66
106
|
}
|
|
67
107
|
if (message.type === 'hello') {
|
|
68
108
|
// Again when the work loaded itself again: heard from now on, as new.
|
|
109
|
+
said.current = true;
|
|
69
110
|
start();
|
|
70
111
|
if (points.current) {
|
|
71
112
|
toWork({ type: 'point', on: true });
|
|
@@ -79,7 +120,10 @@ export function WorkFrame({ html, title, host, kind = 'game', className, style,
|
|
|
79
120
|
}
|
|
80
121
|
}
|
|
81
122
|
else if (message.type === 'error') {
|
|
82
|
-
(
|
|
123
|
+
failed(String((_c = message.message) !== null && _c !== void 0 ? _c : '').slice(0, WorkLimits.ERROR));
|
|
124
|
+
}
|
|
125
|
+
else if (message.type === 'probed') {
|
|
126
|
+
tell(message.blank === true);
|
|
83
127
|
}
|
|
84
128
|
else if (message.type === 'ask') {
|
|
85
129
|
const id = typeof message.id === 'number' ? message.id : undefined;
|
|
@@ -108,17 +152,24 @@ export function WorkFrame({ html, title, host, kind = 'game', className, style,
|
|
|
108
152
|
const spec = WORK_API.calls[name];
|
|
109
153
|
const args = WorkCheck.args(spec, Array.isArray(message.args) ? message.args : []);
|
|
110
154
|
if (args && (!spec.rate || rates[spec.rate].allow())) {
|
|
111
|
-
(
|
|
155
|
+
(_b = host[name]) === null || _b === void 0 ? void 0 : _b.call(host, ...args);
|
|
112
156
|
}
|
|
113
157
|
}
|
|
114
158
|
};
|
|
115
159
|
window.addEventListener('message', heard);
|
|
160
|
+
// A new host (or senses) for a work already loaded: it hears this one from now on, as it would after hello.
|
|
161
|
+
if (said.current) {
|
|
162
|
+
start();
|
|
163
|
+
}
|
|
116
164
|
return () => {
|
|
117
165
|
window.removeEventListener('message', heard);
|
|
118
166
|
load++;
|
|
119
167
|
opened === null || opened === void 0 ? void 0 : opened.stop();
|
|
168
|
+
if (trying) {
|
|
169
|
+
clearTimeout(trying.timer);
|
|
170
|
+
}
|
|
120
171
|
};
|
|
121
|
-
}, [host]);
|
|
172
|
+
}, [host, senses]);
|
|
122
173
|
return (React.createElement("iframe", { ref: frame, className: className, title: title, sandbox: WorkDocs.SANDBOX, srcDoc: document, referrerPolicy: "no-referrer", style: style, "data-own-gestures": "" }));
|
|
123
174
|
}
|
|
124
175
|
//# sourceMappingURL=work-frame.js.map
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { type TaWorkSenseDevice, type TaWorkSenseName } from '@celljs/ta-protocol';
|
|
2
|
+
/** A signal of a sense, as the work hears it (TA_WORK_SENSES). */
|
|
3
|
+
export type WorkSenseValue = number | {
|
|
4
|
+
readonly [key: string]: number;
|
|
5
|
+
};
|
|
6
|
+
/** What a sense tells: its name and what it reads now. */
|
|
7
|
+
export type WorkSenseEmit = (name: TaWorkSenseName, value: WorkSenseValue) => void;
|
|
8
|
+
/**
|
|
9
|
+
* The signals of the devices (docs/design/ta-works.md 7.9), worked out from what they read: an angle, an acceleration,
|
|
10
|
+
* the spectrum of a sound, two small grey frames. What the microphone heard and the camera saw stays here.
|
|
11
|
+
*/
|
|
12
|
+
export declare namespace SenseMath {
|
|
13
|
+
/** Degrees of tilt that read as all the way. */
|
|
14
|
+
const TILT_RANGE = 35;
|
|
15
|
+
/**
|
|
16
|
+
* Tilt of `deviceorientation`, {x, y} each -1 to 1: x to the right, y toward the user, from `neutral` (the beta it
|
|
17
|
+
* was held at as it started); the axes turned with the screen (`angle`, 0, 90, 180, 270).
|
|
18
|
+
*/
|
|
19
|
+
function tilt(beta: number, gamma: number, neutral: number, angle?: number): {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
};
|
|
23
|
+
/** The beta a phone is held at as it starts, taken as level: 0 (flat) to 70 (upright). */
|
|
24
|
+
function neutral(beta: number): number;
|
|
25
|
+
/** m/s² of acceleration (gravity left out) that is a shake. */
|
|
26
|
+
const SHAKE = 14;
|
|
27
|
+
/** How hard a shake was, 0 to 1 (0: none), of `devicemotion`'s acceleration. */
|
|
28
|
+
function shake(x: number, y: number, z: number): number;
|
|
29
|
+
/**
|
|
30
|
+
* How loud a sound is and how hard it is blown, each 0 to 1, of the bytes of an AnalyserNode's
|
|
31
|
+
* `getByteFrequencyData` (`binHz` apart). A blow is wind on the microphone: loud below 400 Hz, much more than above.
|
|
32
|
+
*/
|
|
33
|
+
function sound(bins: ArrayLike<number>, binHz: number): {
|
|
34
|
+
loud: number;
|
|
35
|
+
blow: number;
|
|
36
|
+
};
|
|
37
|
+
/** How far apart two grey pixels must be to be something that moved. */
|
|
38
|
+
const MOVED = 32;
|
|
39
|
+
/**
|
|
40
|
+
* What moved between two grey frames of the front camera (`width` by `height`, a byte a pixel): where, mirrored as a
|
|
41
|
+
* selfie is, {x, y} -1 to 1, and how much of the frame, 0 to 1.
|
|
42
|
+
*/
|
|
43
|
+
function move(before: ArrayLike<number>, now: ArrayLike<number>, width: number, height: number): {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
amount: number;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** How many a second of a sense, the rest dropped; a value much the same as the last one waits for a while. */
|
|
50
|
+
declare class SenseGate {
|
|
51
|
+
protected readonly perSecond: number;
|
|
52
|
+
protected last: number;
|
|
53
|
+
protected sent: string | undefined;
|
|
54
|
+
constructor(perSecond: number);
|
|
55
|
+
allow(value: WorkSenseValue, now: number): boolean;
|
|
56
|
+
}
|
|
57
|
+
/** What the senses reach of the browser: asked of the page (WorkSenses), a fake one in tests. */
|
|
58
|
+
export interface WorkSenseDevices {
|
|
59
|
+
/** Motion sensors here, allowed (iOS asks, in a tap of the user); false on a computer. */
|
|
60
|
+
motion(): Promise<boolean>;
|
|
61
|
+
/** A stream of the microphone or the front camera; it throws when there is none, or the user said no. */
|
|
62
|
+
media(device: 'mic' | 'camera'): Promise<MediaStream>;
|
|
63
|
+
}
|
|
64
|
+
/** The devices of the browser the page runs in. */
|
|
65
|
+
export declare const BROWSER_SENSE_DEVICES: WorkSenseDevices;
|
|
66
|
+
/**
|
|
67
|
+
* What a work senses where the page shows it (docs/design/ta-works.md 7.9): the page reads the devices and gives the
|
|
68
|
+
* work's frame signals alone (SenseMath), each as often as TA_WORK_SENSES says, none while the page is hidden.
|
|
69
|
+
* `request` asks the devices in a tap of the user (iOS lets motion be asked nowhere else), at once, before anything
|
|
70
|
+
* it waits for; asked again it adds. `stop` lets the microphone and the camera go. A sense whose device is not here,
|
|
71
|
+
* or not allowed, is not one of `names`: the frame draws a stand-in to touch in its place.
|
|
72
|
+
*/
|
|
73
|
+
export declare class WorkSenses {
|
|
74
|
+
protected readonly devices: WorkSenseDevices;
|
|
75
|
+
protected readonly wanted: Set<TaWorkSenseName>;
|
|
76
|
+
protected readonly asked: Map<TaWorkSenseDevice, Promise<void>>;
|
|
77
|
+
protected readonly ready: Set<TaWorkSenseDevice>;
|
|
78
|
+
protected readonly streams: Map<"camera" | "mic", MediaStream>;
|
|
79
|
+
protected stopped: boolean;
|
|
80
|
+
constructor(devices?: WorkSenseDevices);
|
|
81
|
+
/** Asks the devices of these senses; resolves with the senses that work here. Call it in a tap of the user. */
|
|
82
|
+
request(names: readonly unknown[]): Promise<TaWorkSenseName[]>;
|
|
83
|
+
/** The senses asked for whose device works here. */
|
|
84
|
+
get names(): TaWorkSenseName[];
|
|
85
|
+
protected ask(device: TaWorkSenseDevice): Promise<void>;
|
|
86
|
+
/** How long the motion sensors may stay silent before they are taken as not here (a computer that says it is touched). */
|
|
87
|
+
static readonly SILENT_MS: number;
|
|
88
|
+
/**
|
|
89
|
+
* Reads the devices of the senses that work here, telling each signal; `missing` once a device said it was here and
|
|
90
|
+
* then gave nothing (its senses are no longer of `names`: stand-ins instead). Returns how to stop reading.
|
|
91
|
+
*/
|
|
92
|
+
listen(emit: WorkSenseEmit, missing?: () => void, now?: () => number): () => void;
|
|
93
|
+
stop(): void;
|
|
94
|
+
protected static release(stream: MediaStream): void;
|
|
95
|
+
protected static hidden(): boolean;
|
|
96
|
+
protected readMotion(tell: WorkSenseEmit, gates: Map<TaWorkSenseName, SenseGate>, silent: () => void): () => void;
|
|
97
|
+
protected readSound(stream: MediaStream, tell: WorkSenseEmit, gates: Map<TaWorkSenseName, SenseGate>): () => void;
|
|
98
|
+
protected readCamera(stream: MediaStream, tell: WorkSenseEmit): () => void;
|
|
99
|
+
}
|
|
100
|
+
export {};
|