@dasl/tile-loader 2.6.0 → 3.0.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 +52 -7
- package/dist/data.js +2 -2
- package/dist/data.js.map +1 -1
- package/dist/index.d.ts +23 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +67 -43
- package/dist/index.js.map +1 -1
- package/dist/link.d.ts +61 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +142 -0
- package/dist/link.js.map +1 -0
- package/dist/size.d.ts.map +1 -1
- package/dist/size.js +24 -9
- package/dist/size.js.map +1 -1
- package/package.json +7 -2
- package/src/data.ts +2 -2
- package/src/index.ts +81 -48
- package/src/link.ts +179 -0
- package/src/size.ts +22 -8
package/src/link.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// The context side of the link protocol (https://dasl.ing/tp-link.html): a
|
|
2
|
+
// tile asks for a link to be opened, the context that frames it decides.
|
|
3
|
+
//
|
|
4
|
+
// A tile cannot open a window, and its frame only ever shows the tile's own
|
|
5
|
+
// documents. When the user follows a link out of a tile, or the tile asks
|
|
6
|
+
// from script, the request arrives here, and nothing happens unless the host
|
|
7
|
+
// says so:
|
|
8
|
+
//
|
|
9
|
+
// import { tileLinks, linkPrompt } from '@dasl/tile-loader/link';
|
|
10
|
+
// tileLinks(tile); // links out of the tile do nothing
|
|
11
|
+
// tileLinks(tile, { open: linkPrompt(container) }); // the user is shown where, and opens it
|
|
12
|
+
// tileLinks(tile, { open: (url) => myApp.confirmAndOpen(url) });
|
|
13
|
+
//
|
|
14
|
+
// The URL is the tile's to choose, and whatever it wants to say can be in it:
|
|
15
|
+
// a link opened without the user seeing where it goes is the tile sending
|
|
16
|
+
// data out. So the default opens nothing, `linkPrompt` has the user follow a
|
|
17
|
+
// link of the host's own making with the destination in plain sight, and a
|
|
18
|
+
// host with a handler of its own should do no less. Whatever the host does,
|
|
19
|
+
// it does somewhere of its own: the tile's frame is never navigated.
|
|
20
|
+
|
|
21
|
+
import type { ProtocolEvent, Tile } from './index.js';
|
|
22
|
+
|
|
23
|
+
/** The tiles protocol this module speaks. */
|
|
24
|
+
export const LINK_PROTOCOL = 'link';
|
|
25
|
+
|
|
26
|
+
// Longer than any link a person follows; a tile has no call to send more.
|
|
27
|
+
const MAX_URL_LENGTH = 8192;
|
|
28
|
+
|
|
29
|
+
export interface LinkRequest {
|
|
30
|
+
/**
|
|
31
|
+
* Whether the user has just interacted with the page (a click in the tile
|
|
32
|
+
* counts for the page framing it). False means the tile is asking by
|
|
33
|
+
* itself; a host has every reason to refuse then.
|
|
34
|
+
*/
|
|
35
|
+
activated: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Set when the tile asked from script and waits for an answer. A handler
|
|
38
|
+
* that returns nothing answers later itself, with
|
|
39
|
+
* `tile.sendProtocol('link', 'result', { id, opened })`.
|
|
40
|
+
*/
|
|
41
|
+
id?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Decides about one link: `true` if the host opened it, `false` if it did
|
|
46
|
+
* not. A host that answers the tile itself, later, returns nothing.
|
|
47
|
+
*/
|
|
48
|
+
export type LinkHandler = (url: URL, request: LinkRequest) => boolean | void | Promise<boolean | void>;
|
|
49
|
+
|
|
50
|
+
export interface TileLinksOptions {
|
|
51
|
+
/** What to do with a link the tile asks for. Without one, nothing opens. */
|
|
52
|
+
open?: LinkHandler;
|
|
53
|
+
/** The URL schemes that reach `open`; any other is refused. Default: `http:` and `https:`. */
|
|
54
|
+
schemes?: string[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface TileLinksControl {
|
|
58
|
+
/** Stops listening. Links out of the tile then do nothing, and go unanswered. */
|
|
59
|
+
stop (): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Hears a rendered tile's requests to open links, and answers them. */
|
|
63
|
+
export function tileLinks (tile: Tile, options: TileLinksOptions = {}): TileLinksControl {
|
|
64
|
+
const schemes = options.schemes ?? ['http:', 'https:'];
|
|
65
|
+
// One at a time: while the host (or its user) makes up its mind about a
|
|
66
|
+
// link, the tile does not get to queue up others.
|
|
67
|
+
let deciding = false;
|
|
68
|
+
|
|
69
|
+
const answer = (id: unknown, opened: boolean) => {
|
|
70
|
+
if (typeof id !== 'string' || !id) return; // a followed link carries no id, and gets no answer
|
|
71
|
+
try { tile.sendProtocol(LINK_PROTOCOL, 'result', { id, opened }); }
|
|
72
|
+
catch { /* gone */ }
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const onProtocol = async (ev: Event) => {
|
|
76
|
+
const { protocol, kind, payload } = ev as ProtocolEvent;
|
|
77
|
+
if (protocol !== LINK_PROTOCOL || kind !== 'open') return;
|
|
78
|
+
const { id, url: asked } = (payload && typeof payload === 'object' ? payload : {}) as { id?: unknown; url?: unknown };
|
|
79
|
+
let url: URL | undefined;
|
|
80
|
+
if (typeof asked === 'string' && asked.length <= MAX_URL_LENGTH) {
|
|
81
|
+
try { url = new URL(asked); }
|
|
82
|
+
catch { /* not a URL */ }
|
|
83
|
+
}
|
|
84
|
+
if (!url || !schemes.includes(url.protocol) || !options.open || deciding) return answer(id, false);
|
|
85
|
+
deciding = true;
|
|
86
|
+
let opened: boolean | void;
|
|
87
|
+
try {
|
|
88
|
+
opened = await options.open(url, {
|
|
89
|
+
activated: globalThis.navigator?.userActivation?.isActive ?? false,
|
|
90
|
+
...(typeof id === 'string' && id ? { id } : {}),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
console.error('Could not open a link for a tile:', err);
|
|
95
|
+
opened = false;
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
deciding = false;
|
|
99
|
+
}
|
|
100
|
+
if (typeof opened === 'boolean') answer(id, opened);
|
|
101
|
+
};
|
|
102
|
+
tile.addEventListener('protocol', onProtocol);
|
|
103
|
+
return {
|
|
104
|
+
stop () {
|
|
105
|
+
tile.removeEventListener('protocol', onProtocol);
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Opens `url` in a new tab or window, cut off from the page that opens it (no
|
|
112
|
+
* opener, no referrer). Only `http:` and `https:`. Browsers only allow it
|
|
113
|
+
* right after the user did something, so call it from a click of theirs.
|
|
114
|
+
*/
|
|
115
|
+
export function openInNewTab (url: URL): boolean {
|
|
116
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
|
|
117
|
+
window.open(url.href, '_blank', 'noopener,noreferrer');
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface LinkPromptOptions {
|
|
122
|
+
/** The words before the destination. Default: "This tile wants to open". */
|
|
123
|
+
label?: string;
|
|
124
|
+
/** The word on the button that refuses. Default: "Dismiss". */
|
|
125
|
+
dismiss?: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* A link handler that leaves it to the user: it shows, in `container`, where
|
|
130
|
+
* the tile wants to go, as a link of the host's own that the user can follow,
|
|
131
|
+
* with a button to dismiss it. The whole address is shown, since the address
|
|
132
|
+
* is where a tile would hide what it wants to send out. Nothing opens unless
|
|
133
|
+
* the user follows the link, and it opens in a new tab, never in the tile.
|
|
134
|
+
*
|
|
135
|
+
* A request that arrives when the user has not touched the page is refused
|
|
136
|
+
* outright, and a new request replaces one still showing.
|
|
137
|
+
*
|
|
138
|
+
* The prompt is plain elements with a few class names (`tile-link-prompt`,
|
|
139
|
+
* `tile-link-url`, `tile-link-dismiss`) for a host to style.
|
|
140
|
+
*/
|
|
141
|
+
export function linkPrompt (container: HTMLElement, options: LinkPromptOptions = {}): LinkHandler {
|
|
142
|
+
let settle: ((opened: boolean) => void) | undefined;
|
|
143
|
+
return (url, { activated }) => {
|
|
144
|
+
if (!activated) return false;
|
|
145
|
+
settle?.(false);
|
|
146
|
+
const doc = container.ownerDocument;
|
|
147
|
+
const box = doc.createElement('div');
|
|
148
|
+
box.className = 'tile-link-prompt';
|
|
149
|
+
box.setAttribute('role', 'group');
|
|
150
|
+
box.style.cssText = 'display: flex; gap: 0.5em; align-items: baseline; padding: 0.5em 0.75em; font: inherit; overflow-wrap: anywhere;';
|
|
151
|
+
const label = doc.createElement('span');
|
|
152
|
+
label.textContent = options.label ?? 'This tile wants to open';
|
|
153
|
+
const link = doc.createElement('a');
|
|
154
|
+
link.className = 'tile-link-url';
|
|
155
|
+
link.href = url.href;
|
|
156
|
+
link.target = '_blank';
|
|
157
|
+
link.rel = 'noopener noreferrer';
|
|
158
|
+
link.textContent = url.href;
|
|
159
|
+
link.style.cssText = 'flex: 1; min-width: 0;';
|
|
160
|
+
const dismiss = doc.createElement('button');
|
|
161
|
+
dismiss.type = 'button';
|
|
162
|
+
dismiss.className = 'tile-link-dismiss';
|
|
163
|
+
dismiss.textContent = options.dismiss ?? 'Dismiss';
|
|
164
|
+
box.append(label, link, dismiss);
|
|
165
|
+
return new Promise<boolean>((resolve) => {
|
|
166
|
+
const done = (opened: boolean) => {
|
|
167
|
+
if (settle !== done) return;
|
|
168
|
+
settle = undefined;
|
|
169
|
+
box.remove();
|
|
170
|
+
resolve(opened);
|
|
171
|
+
};
|
|
172
|
+
settle = done;
|
|
173
|
+
// The link is a real one: following it is the user's own navigation.
|
|
174
|
+
link.addEventListener('click', () => setTimeout(() => done(true)));
|
|
175
|
+
dismiss.addEventListener('click', () => done(false));
|
|
176
|
+
container.append(box);
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
}
|
package/src/size.ts
CHANGED
|
@@ -77,7 +77,9 @@ export function tileSize (tile: Tile, frame: HTMLElement, options: TileSizeOptio
|
|
|
77
77
|
let reported = ''; // the last size the host was told of
|
|
78
78
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
79
79
|
let lastApplied = 0;
|
|
80
|
-
|
|
80
|
+
let stopped = false;
|
|
81
|
+
const waiting: string[] = []; // request ids whose move is still to come
|
|
82
|
+
const moved: string[] = []; // request ids whose move the host is still to apply
|
|
81
83
|
|
|
82
84
|
// The frame's real box, when it has one.
|
|
83
85
|
const box = (): TileSize | undefined => {
|
|
@@ -92,6 +94,7 @@ export function tileSize (tile: Tile, frame: HTMLElement, options: TileSizeOptio
|
|
|
92
94
|
});
|
|
93
95
|
|
|
94
96
|
const grant = (id?: string) => {
|
|
97
|
+
if (stopped) return; // a move's timer can still fire after stop()
|
|
95
98
|
const size = box() ?? current;
|
|
96
99
|
current = size;
|
|
97
100
|
const dims = `${size.width}x${size.height}`;
|
|
@@ -106,11 +109,16 @@ export function tileSize (tile: Tile, frame: HTMLElement, options: TileSizeOptio
|
|
|
106
109
|
}
|
|
107
110
|
catch { /* not rendered yet: it asks when it is */ }
|
|
108
111
|
};
|
|
109
|
-
const answer = () => {
|
|
110
|
-
const ids = waiting.splice(0);
|
|
112
|
+
const answer = (ids: string[]) => {
|
|
111
113
|
if (ids.length) ids.forEach(id => grant(id));
|
|
112
114
|
else grant();
|
|
113
115
|
};
|
|
116
|
+
// The ids of `ids` that are still in `moved`, taken out of it.
|
|
117
|
+
const take = (ids: string[]) => ids.filter(id => {
|
|
118
|
+
const i = moved.indexOf(id);
|
|
119
|
+
if (i >= 0) moved.splice(i, 1);
|
|
120
|
+
return i >= 0;
|
|
121
|
+
});
|
|
114
122
|
|
|
115
123
|
const put = (size: TileSize) => {
|
|
116
124
|
current = size;
|
|
@@ -128,10 +136,14 @@ export function tileSize (tile: Tile, frame: HTMLElement, options: TileSizeOptio
|
|
|
128
136
|
timer = undefined;
|
|
129
137
|
lastApplied = Date.now();
|
|
130
138
|
put(settle());
|
|
131
|
-
|
|
132
|
-
|
|
139
|
+
const ids = waiting.splice(0);
|
|
140
|
+
if (apply) return answer(ids);
|
|
141
|
+
// The host resizes its box. Each move's requests get their own time
|
|
142
|
+
// for that; the observer may answer them sooner.
|
|
143
|
+
moved.push(...ids);
|
|
144
|
+
setTimeout(() => answer(take(ids)), HOST_APPLIES_MS);
|
|
133
145
|
};
|
|
134
|
-
if (timer) return;
|
|
146
|
+
if (timer || stopped) return;
|
|
135
147
|
const since = Date.now() - lastApplied;
|
|
136
148
|
if (since >= SETTLE_MS) go();
|
|
137
149
|
else timer = setTimeout(go, SETTLE_MS - since);
|
|
@@ -157,8 +169,9 @@ export function tileSize (tile: Tile, frame: HTMLElement, options: TileSizeOptio
|
|
|
157
169
|
tile.addEventListener('protocol', onProtocol);
|
|
158
170
|
|
|
159
171
|
// The context's layout changes the box too (a narrower column, a rotated
|
|
160
|
-
// phone): the tile is told its new size.
|
|
161
|
-
|
|
172
|
+
// phone): the tile is told its new size. Requests still waiting for their
|
|
173
|
+
// move are left to it.
|
|
174
|
+
const observer = typeof ResizeObserver === 'function' ? new ResizeObserver(() => answer(moved.splice(0))) : undefined;
|
|
162
175
|
observer?.observe(frame);
|
|
163
176
|
|
|
164
177
|
put(settle());
|
|
@@ -179,6 +192,7 @@ export function tileSize (tile: Tile, frame: HTMLElement, options: TileSizeOptio
|
|
|
179
192
|
return resolved;
|
|
180
193
|
},
|
|
181
194
|
stop () {
|
|
195
|
+
stopped = true;
|
|
182
196
|
tile.removeEventListener('protocol', onProtocol);
|
|
183
197
|
observer?.disconnect();
|
|
184
198
|
if (timer) clearTimeout(timer);
|