@nomac/phone 0.1.1 → 0.2.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/README.md +2 -2
- package/index.d.ts +4 -0
- package/index.js +12 -1
- package/package.json +1 -1
- package/react.js +17 -5
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Get a link for the phone from NoMac's API: every phone's answer carries `links.v
|
|
|
8
8
|
import { embedPhone } from "@nomac/phone";
|
|
9
9
|
|
|
10
10
|
const phone = embedPhone(document.getElementById("phone"), link, {
|
|
11
|
-
onEvent: (event) => console.log(event.type), // ready, app_launched, ended, error
|
|
11
|
+
onEvent: (event) => console.log(event.type), // ready, screen, app_launched, ended, error
|
|
12
12
|
});
|
|
13
13
|
phone.home();
|
|
14
14
|
phone.tap(0.5, 0.5);
|
|
@@ -23,6 +23,6 @@ import { NomacPhone } from "@nomac/phone/react";
|
|
|
23
23
|
<NomacPhone url={link} onEvent={(e) => e.type === "ended" && reload()} style={{ height: 720 }} />
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
The box
|
|
26
|
+
The box takes the shape of the phone's screen: an iPhone's (402 by 874) until the view connects, then its own (an iPad's, an iPhone Duo's), and again whenever it changes, as when an iPhone Duo folds. A `style` with an `aspectRatio` of your own keeps that shape instead. Each change is a `screen` event with the screen's `width` and `height` in pixels and its `scale` (and on an iPhone Duo its `role`, outer or inner, and `posture`, closed, book or open); with `embedPhone`, size your container from it: `screenAspect(event)` gives the CSS aspect ratio. `ended` comes once, when a phone that was shown ends; a link that does not work from the start is an `error`. While the phone starts, the view waits for it, and after a dropped connection it comes back by itself. Only view links of https://nomac.app are framed.
|
|
27
27
|
|
|
28
28
|
The screen comes as H.264 over a WebSocket, decoded in the page (WebCodecs), with no TURN servers. With a control link, one viewer at a time holds input; another can take over.
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export type PhoneEvent =
|
|
2
2
|
| { type: "ready" }
|
|
3
|
+
/** The screen in use, in pixels: when the phone is first shown, and again when it changes (an iPhone Duo's fold). */
|
|
4
|
+
| { type: "screen"; width: number; height: number; scale: number; role?: "main" | "outer" | "inner"; posture?: "closed" | "book" | "open" }
|
|
3
5
|
| { type: "app_launched" }
|
|
4
6
|
| { type: "ended" }
|
|
5
7
|
| { type: "error"; error: string }
|
|
@@ -19,5 +21,7 @@ export type Phone = {
|
|
|
19
21
|
export const ORIGIN: string;
|
|
20
22
|
/** The view link with embed=1. Throws for anything that is not a NoMac Simulator view link of origin (https://nomac.app). */
|
|
21
23
|
export function embedUrl(viewUrl: string, options?: { origin?: string }): string;
|
|
24
|
+
/** A screen event's shape as a CSS aspect-ratio ("1206 / 2622"); undefined for any other event. */
|
|
25
|
+
export function screenAspect(event: PhoneEvent): string | undefined;
|
|
22
26
|
/** Puts the phone in container; onEvent receives its events. */
|
|
23
27
|
export function embedPhone(container: HTMLElement, viewUrl: string, options?: { title?: string; origin?: string; onEvent?: (event: PhoneEvent) => void }): Phone;
|
package/index.js
CHANGED
|
@@ -19,7 +19,18 @@ export function embedUrl(viewUrl, { origin = ORIGIN } = {}) {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* The CSS aspect-ratio of a screen event ("1206 / 2622"), or undefined for any other event: the shape of the phone's
|
|
23
|
+
* screen in use, which an iPhone Duo changes when it folds.
|
|
24
|
+
*/
|
|
25
|
+
export function screenAspect(event) {
|
|
26
|
+
// The view's bounds: whole-number sides of 32 to 8,192 pixels, a scale of 1 to 4.
|
|
27
|
+
const side = (n) => Number.isInteger(n) && n >= 32 && n <= 8192;
|
|
28
|
+
const scaled = typeof event?.scale === "number" && event.scale >= 1 && event.scale <= 4;
|
|
29
|
+
return event?.type === "screen" && side(event.width) && side(event.height) && scaled ? `${event.width} / ${event.height}` : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Puts the phone in container. options.onEvent receives { type, ...detail } for ready, screen, app_launched, ended,
|
|
23
34
|
* error and screenshot. Answers controls for it, and destroy() to take it away.
|
|
24
35
|
*/
|
|
25
36
|
export function embedPhone(container, viewUrl, options = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomac/phone",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A NoMac Simulator phone in your page: its view link in an iframe, its events, and home, tap and screenshot from your code.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://nomac.app",
|
package/react.js
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
|
-
import { createElement, forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
2
|
-
import { embedPhone } from "./index.js";
|
|
1
|
+
import { createElement, forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
|
|
2
|
+
import { embedPhone, screenAspect } from "./index.js";
|
|
3
|
+
|
|
4
|
+
/** The box's shape until the phone says its own: an iPhone's. */
|
|
5
|
+
const IPHONE = "402 / 874";
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
8
|
* <NomacPhone url={viewLink} onEvent={...} /> shows the phone and fills its box; a ref gives home, tap and
|
|
6
|
-
* screenshot.
|
|
9
|
+
* screenshot. The box takes the shape of the phone's screen in use (an iPad's, an iPhone Duo's as it folds), unless
|
|
10
|
+
* style gives an aspectRatio of its own.
|
|
7
11
|
*/
|
|
8
12
|
export const NomacPhone = forwardRef(function NomacPhone({ url, onEvent, title, className, style }, ref) {
|
|
9
13
|
const box = useRef(null);
|
|
10
14
|
const phone = useRef(null);
|
|
11
15
|
const handler = useRef(onEvent);
|
|
12
16
|
handler.current = onEvent;
|
|
17
|
+
const [aspect, setAspect] = useState(IPHONE);
|
|
13
18
|
useEffect(() => {
|
|
14
19
|
if (!box.current) return undefined;
|
|
15
|
-
phone.current = embedPhone(box.current, url, {
|
|
20
|
+
phone.current = embedPhone(box.current, url, {
|
|
21
|
+
title,
|
|
22
|
+
onEvent: (event) => {
|
|
23
|
+
const shape = screenAspect(event);
|
|
24
|
+
if (shape) setAspect(shape);
|
|
25
|
+
handler.current?.(event);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
16
28
|
return () => { phone.current?.destroy(); phone.current = null; };
|
|
17
29
|
}, [url, title]);
|
|
18
30
|
useImperativeHandle(ref, () => ({
|
|
@@ -20,5 +32,5 @@ export const NomacPhone = forwardRef(function NomacPhone({ url, onEvent, title,
|
|
|
20
32
|
tap: (x, y) => phone.current?.tap(x, y),
|
|
21
33
|
screenshot: (options) => phone.current?.screenshot(options) ?? Promise.reject(new Error("The phone is not shown")),
|
|
22
34
|
}), []);
|
|
23
|
-
return createElement("div", { ref: box, className, style: { aspectRatio:
|
|
35
|
+
return createElement("div", { ref: box, className, style: { aspectRatio: aspect, ...style } });
|
|
24
36
|
});
|