@celljs/ta-web 4.39.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/chat/chat-shown.js +2 -2
- package/esm/browser/components/busy.d.ts +5 -0
- package/esm/browser/components/busy.js +17 -0
- 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/icons.js +15 -1
- package/esm/browser/extension/web-api-hosts.d.ts +22 -0
- package/esm/browser/extension/web-api-hosts.js +2 -0
- package/esm/browser/extension/web-api-play.d.ts +9 -1
- package/esm/browser/extension/web-api-spec.js +3 -0
- package/esm/browser/extension/web-extension-api.d.ts +7 -0
- package/esm/browser/extension/web-extension-context.js +6 -3
- package/esm/browser/extension/web-file-drag.d.ts +3 -0
- package/esm/browser/extension/web-file-drag.js +4 -0
- package/esm/browser/extension/web-hosts.d.ts +7 -0
- package/esm/browser/extension/web-hosts.js +29 -0
- package/esm/browser/hosts.d.ts +2 -0
- package/esm/browser/hosts.js +9 -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/conversation.d.ts +1 -1
- package/esm/browser/style/conversation.js +2 -2
- package/esm/browser/style/shared.d.ts +1 -1
- package/esm/browser/style/shared.js +3 -0
- package/esm/browser/workbench-hosts.d.ts +1 -0
- package/esm/browser/workbench-hosts.js +9 -0
- package/esm/browser/workbench-protocol.d.ts +5 -0
- package/lib/browser/chat/chat-shown.js +2 -2
- package/lib/browser/components/busy.d.ts +5 -0
- package/lib/browser/components/busy.js +17 -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/icons.js +14 -0
- package/lib/browser/extension/web-api-hosts.d.ts +23 -0
- package/lib/browser/extension/web-api-hosts.js +3 -0
- package/lib/browser/extension/web-api-play.d.ts +9 -1
- package/lib/browser/extension/web-api-spec.js +3 -0
- package/lib/browser/extension/web-extension-api.d.ts +7 -0
- package/lib/browser/extension/web-extension-context.js +6 -3
- package/lib/browser/extension/web-file-drag.d.ts +4 -0
- package/lib/browser/extension/web-file-drag.js +7 -0
- package/lib/browser/extension/web-hosts.d.ts +8 -0
- package/lib/browser/extension/web-hosts.js +32 -0
- package/lib/browser/hosts.d.ts +2 -0
- package/lib/browser/hosts.js +9 -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/conversation.d.ts +1 -1
- package/lib/browser/style/conversation.js +2 -2
- package/lib/browser/style/shared.d.ts +1 -1
- package/lib/browser/style/shared.js +3 -0
- package/lib/browser/workbench-hosts.d.ts +1 -0
- package/lib/browser/workbench-hosts.js +9 -0
- package/lib/browser/workbench-protocol.d.ts +5 -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,14 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
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
|
|
4
|
-
* no origin and no network (WorkDocs). One table, WORK_API: the bridge in the frame is made from it, and the page checks
|
|
5
|
-
* by it what crosses (WorkFrame). The games of ta-play are the first works; what a work may do more (its model, its
|
|
6
|
-
* data) is added here, once.
|
|
7
|
-
*/
|
|
8
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
3
|
exports.WorkCheck = exports.WorkRate = exports.WorkLimits = exports.WORK_API = void 0;
|
|
4
|
+
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
10
5
|
exports.WORK_API = {
|
|
11
|
-
values: { me: null, players: [], state: {}, solo: true, saved: null, data: {}, can: [] },
|
|
6
|
+
values: { me: null, players: [], state: {}, solo: true, saved: null, data: {}, can: [], senses: [] },
|
|
12
7
|
calls: {
|
|
13
8
|
set: { args: ['key', 'value'], rate: 'writes', patch: 'state' },
|
|
14
9
|
send: { args: ['value', 'text'], rate: 'sends' },
|
|
@@ -17,7 +12,8 @@ exports.WORK_API = {
|
|
|
17
12
|
finish: { args: ['any'] },
|
|
18
13
|
},
|
|
19
14
|
asks: { ai: { args: ['any'], rate: 'asks' } },
|
|
20
|
-
events: { state: { patch: 'state' }, message: {}, players: { assign: 'players' } },
|
|
15
|
+
events: { state: { patch: 'state' }, message: {}, players: { assign: 'players' }, sense: {} },
|
|
16
|
+
senses: ta_protocol_1.TA_WORK_SENSES,
|
|
21
17
|
};
|
|
22
18
|
var WorkLimits;
|
|
23
19
|
(function (WorkLimits) {
|
|
@@ -27,12 +27,36 @@ function put(s,a){if(s.patch){var o=ta[s.patch]||(ta[s.patch]={});if(a[1]===null
|
|
|
27
27
|
function post(m){m.ta='work';try{parent.postMessage(m,'*')}catch(e){}}
|
|
28
28
|
function emit(t,a){(on[t]||[]).forEach(function(f){try{f.apply(null,a)}catch(e){console.error(e)}})}
|
|
29
29
|
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})}});
|
|
30
|
+
var sw=[],sd={},inited=false,box,W={tilt:'Tilt',shake:'Shake',blow:'Hold to blow',loud:'Hold for a sound',move:'Move'};
|
|
31
|
+
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()}}};
|
|
32
|
+
ta.on('sense',function(n,v){emit('sense:'+n,[v])});
|
|
33
|
+
function fire(n,v){emit('sense:'+n,[v])}
|
|
34
|
+
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)';
|
|
35
|
+
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}
|
|
36
|
+
function boxes(){return box||(box={l:mk('left'),r:mk('right')})}
|
|
37
|
+
function btn(t,p){var b=document.createElement('button');b.type='button';b.textContent=t;b.setAttribute('style',BTN);p.appendChild(b);return b}
|
|
38
|
+
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()})})}
|
|
39
|
+
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);
|
|
40
|
+
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})}
|
|
41
|
+
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})})})}
|
|
42
|
+
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;
|
|
43
|
+
if(s.standIn==='pad')pad(n,B.l);
|
|
44
|
+
else if(s.standIn==='tap')hold(btn(W[n]||n,B.r),function(){fire(n,{power:1})},function(){});
|
|
45
|
+
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)});
|
|
46
|
+
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})})})})}
|
|
47
|
+
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)})}
|
|
48
|
+
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}
|
|
49
|
+
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)});
|
|
50
|
+
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);
|
|
51
|
+
setTimeout(function(){post({type:'probed',blank:blank()})},1600)}
|
|
30
52
|
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();
|
|
31
53
|
addEventListener('message',function(e){var m=e.data;if(e.source!==parent||!m||m.ta!=='work')return;
|
|
32
|
-
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])}
|
|
54
|
+
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])}}
|
|
33
55
|
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)}
|
|
34
56
|
else if(m.type==='event'&&S.events.hasOwnProperty(m.name)){put(S.events[m.name],m.args||[]);emit(m.name,m.args||[])}
|
|
35
|
-
else if(m.type==='point')point(!!m.on)
|
|
57
|
+
else if(m.type==='point')point(!!m.on);
|
|
58
|
+
else if(m.type==='probe')probe();
|
|
59
|
+
else if(m.type==='senses'){ta.senses=m.names||[];stand()}});
|
|
36
60
|
var pk=null,EAT=['pointerdown','mousedown','mouseup','touchstart','touchend','click','dblclick','contextmenu','keydown','wheel'];
|
|
37
61
|
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('>')}
|
|
38
62
|
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';
|
|
4
4
|
import { WorkDocs } from './work-docs';
|
|
5
|
+
import type { WorkSenses } from './work-senses';
|
|
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,11 +26,23 @@ 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;
|
|
28
48
|
//# sourceMappingURL=work-frame.d.ts.map
|
|
@@ -38,12 +38,14 @@ const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
|
38
38
|
const React = __importStar(require("react"));
|
|
39
39
|
const work_api_1 = require("./work-api");
|
|
40
40
|
const work_docs_1 = require("./work-docs");
|
|
41
|
+
/** Errors of a page that are no fault of the work's. */
|
|
42
|
+
const HARMLESS = /ResizeObserver loop/i;
|
|
41
43
|
/**
|
|
42
44
|
* 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
|
|
43
45
|
* page by messages alone, only those of its own window heard; what it does checked by WORK_API before it reaches the
|
|
44
46
|
* host. Give it another React `key` to start it again.
|
|
45
47
|
*/
|
|
46
|
-
function WorkFrame({ html, title, host, kind = 'game', className, style, onError, onPoint }) {
|
|
48
|
+
function WorkFrame({ html, title, host, kind = 'game', className, style, onError, onPoint, senses, senseWords, probe }) {
|
|
47
49
|
const frame = React.useRef(null);
|
|
48
50
|
const document = React.useMemo(() => work_docs_1.WorkDocs.document(html, kind), [html, kind]);
|
|
49
51
|
const errors = React.useRef(onError);
|
|
@@ -51,6 +53,12 @@ function WorkFrame({ html, title, host, kind = 'game', className, style, onError
|
|
|
51
53
|
const points = React.useRef(onPoint);
|
|
52
54
|
points.current = onPoint;
|
|
53
55
|
const pointing = !!onPoint;
|
|
56
|
+
const words = React.useRef(senseWords);
|
|
57
|
+
words.current = senseWords;
|
|
58
|
+
const probed = React.useRef(probe);
|
|
59
|
+
probed.current = probe;
|
|
60
|
+
// The work said hello once: loaded.
|
|
61
|
+
const said = React.useRef(false);
|
|
54
62
|
React.useEffect(() => {
|
|
55
63
|
var _a, _b;
|
|
56
64
|
(_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' }, '*');
|
|
@@ -65,6 +73,24 @@ function WorkFrame({ html, title, host, kind = 'game', className, style, onError
|
|
|
65
73
|
const toWork = (message) => { var _a; return (_a = element.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage({ ...message, ta: 'work' }, '*'); };
|
|
66
74
|
let opened;
|
|
67
75
|
let load = 0;
|
|
76
|
+
// Tried (probe): its errors until it answers, then told once.
|
|
77
|
+
const trying = probed.current ? { errors: [], timer: undefined, told: false } : undefined;
|
|
78
|
+
const tell = (blank) => {
|
|
79
|
+
var _a;
|
|
80
|
+
if (trying && !trying.told) {
|
|
81
|
+
trying.told = true;
|
|
82
|
+
clearTimeout(trying.timer);
|
|
83
|
+
(_a = probed.current) === null || _a === void 0 ? void 0 : _a.call(probed, { errors: [...new Set(trying.errors)].slice(0, 5), blank });
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const failed = (message) => {
|
|
87
|
+
var _a;
|
|
88
|
+
if (HARMLESS.test(message)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
trying === null || trying === void 0 ? void 0 : trying.errors.push(message);
|
|
92
|
+
(_a = errors.current) === null || _a === void 0 ? void 0 : _a.call(errors, message);
|
|
93
|
+
};
|
|
68
94
|
const start = () => {
|
|
69
95
|
opened === null || opened === void 0 ? void 0 : opened.stop();
|
|
70
96
|
opened = undefined;
|
|
@@ -76,23 +102,37 @@ function WorkFrame({ html, title, host, kind = 'game', className, style, onError
|
|
|
76
102
|
queue ? queue.push(event) : toWork(event);
|
|
77
103
|
};
|
|
78
104
|
host.open(emit).then(({ values, stop }) => {
|
|
105
|
+
var _a;
|
|
79
106
|
if (now !== load) {
|
|
80
107
|
stop();
|
|
81
108
|
return;
|
|
82
109
|
}
|
|
83
|
-
|
|
110
|
+
const unsense = senses === null || senses === void 0 ? void 0 : senses.listen((name, value) => toWork({ type: 'event', name: 'sense', args: [name, value] }),
|
|
111
|
+
// A sense said to be here gave nothing: a stand-in for it now.
|
|
112
|
+
() => toWork({ type: 'senses', names: senses.names }));
|
|
113
|
+
opened = {
|
|
114
|
+
stop: () => {
|
|
115
|
+
unsense === null || unsense === void 0 ? void 0 : unsense();
|
|
116
|
+
stop();
|
|
117
|
+
},
|
|
118
|
+
};
|
|
84
119
|
// What it may ask is what the host answers, no more: of those, what the host says it answers here.
|
|
85
120
|
const can = Object.keys(work_api_1.WORK_API.asks).filter(name => typeof host[name] === 'function' && (!values.can || values.can.includes(name)));
|
|
86
|
-
toWork({ type: 'init', values: { ...values, can } });
|
|
121
|
+
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 } : {}) });
|
|
87
122
|
for (const event of queue !== null && queue !== void 0 ? queue : []) {
|
|
88
123
|
toWork(event);
|
|
89
124
|
}
|
|
90
125
|
queue = undefined;
|
|
91
|
-
|
|
126
|
+
if (trying && !trying.timer) {
|
|
127
|
+
toWork({ type: 'probe' });
|
|
128
|
+
// One that never answers (its scripts stuck): told what it said until then.
|
|
129
|
+
trying.timer = setTimeout(() => tell(false), 5000);
|
|
130
|
+
}
|
|
131
|
+
}, error => failed(String(error instanceof Error ? error.message : error).slice(0, work_api_1.WorkLimits.ERROR)));
|
|
92
132
|
};
|
|
93
133
|
const heard = (event) => {
|
|
94
|
-
var _a, _b
|
|
95
|
-
var
|
|
134
|
+
var _a, _b;
|
|
135
|
+
var _c;
|
|
96
136
|
if (!element.contentWindow || event.source !== element.contentWindow) {
|
|
97
137
|
return;
|
|
98
138
|
}
|
|
@@ -102,6 +142,7 @@ function WorkFrame({ html, title, host, kind = 'game', className, style, onError
|
|
|
102
142
|
}
|
|
103
143
|
if (message.type === 'hello') {
|
|
104
144
|
// Again when the work loaded itself again: heard from now on, as new.
|
|
145
|
+
said.current = true;
|
|
105
146
|
start();
|
|
106
147
|
if (points.current) {
|
|
107
148
|
toWork({ type: 'point', on: true });
|
|
@@ -115,7 +156,10 @@ function WorkFrame({ html, title, host, kind = 'game', className, style, onError
|
|
|
115
156
|
}
|
|
116
157
|
}
|
|
117
158
|
else if (message.type === 'error') {
|
|
118
|
-
(
|
|
159
|
+
failed(String((_c = message.message) !== null && _c !== void 0 ? _c : '').slice(0, work_api_1.WorkLimits.ERROR));
|
|
160
|
+
}
|
|
161
|
+
else if (message.type === 'probed') {
|
|
162
|
+
tell(message.blank === true);
|
|
119
163
|
}
|
|
120
164
|
else if (message.type === 'ask') {
|
|
121
165
|
const id = typeof message.id === 'number' ? message.id : undefined;
|
|
@@ -144,17 +188,24 @@ function WorkFrame({ html, title, host, kind = 'game', className, style, onError
|
|
|
144
188
|
const spec = work_api_1.WORK_API.calls[name];
|
|
145
189
|
const args = work_api_1.WorkCheck.args(spec, Array.isArray(message.args) ? message.args : []);
|
|
146
190
|
if (args && (!spec.rate || rates[spec.rate].allow())) {
|
|
147
|
-
(
|
|
191
|
+
(_b = host[name]) === null || _b === void 0 ? void 0 : _b.call(host, ...args);
|
|
148
192
|
}
|
|
149
193
|
}
|
|
150
194
|
};
|
|
151
195
|
window.addEventListener('message', heard);
|
|
196
|
+
// A new host (or senses) for a work already loaded: it hears this one from now on, as it would after hello.
|
|
197
|
+
if (said.current) {
|
|
198
|
+
start();
|
|
199
|
+
}
|
|
152
200
|
return () => {
|
|
153
201
|
window.removeEventListener('message', heard);
|
|
154
202
|
load++;
|
|
155
203
|
opened === null || opened === void 0 ? void 0 : opened.stop();
|
|
204
|
+
if (trying) {
|
|
205
|
+
clearTimeout(trying.timer);
|
|
206
|
+
}
|
|
156
207
|
};
|
|
157
|
-
}, [host]);
|
|
208
|
+
}, [host, senses]);
|
|
158
209
|
return (React.createElement("iframe", { ref: frame, className: className, title: title, sandbox: work_docs_1.WorkDocs.SANDBOX, srcDoc: document, referrerPolicy: "no-referrer", style: style, "data-own-gestures": "" }));
|
|
159
210
|
}
|
|
160
211
|
//# sourceMappingURL=work-frame.js.map
|
|
@@ -0,0 +1,101 @@
|
|
|
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 {};
|
|
101
|
+
//# sourceMappingURL=work-senses.d.ts.map
|