@plusguard/reels 1.9.1 → 1.10.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 +38 -6
- package/dist/index.js +35 -4
- package/early.js +69 -0
- package/index.ts +36 -3
- package/package.json +31 -7
package/README.md
CHANGED
|
@@ -23,9 +23,9 @@ From npm, through jsDelivr — the npm package is the code alone (about
|
|
|
23
23
|
jsDelivr's GitHub route.
|
|
24
24
|
|
|
25
25
|
```html
|
|
26
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.
|
|
26
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/index.css" />
|
|
27
27
|
<script type="module">
|
|
28
|
-
import { initReels } from "https://cdn.jsdelivr.net/npm/@plusguard/reels@1.
|
|
28
|
+
import { initReels } from "https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/dist/index.js";
|
|
29
29
|
initReels();
|
|
30
30
|
</script>
|
|
31
31
|
```
|
|
@@ -106,6 +106,21 @@ below).
|
|
|
106
106
|
`data-pg-reels-open`: the full view, on that group and clip. First the
|
|
107
107
|
event `pg-reels:open` on the section, with `detail.group` and
|
|
108
108
|
`detail.clip`; `preventDefault()` on it and the site opens its own.
|
|
109
|
+
- **Watched.** A clip that has played **3 seconds** — or half of it, if it
|
|
110
|
+
is shorter than 6 — on a card or in the full view: the event
|
|
111
|
+
`pg-reels:watched` on the section, once a clip a page visit, with
|
|
112
|
+
`detail.group`, `detail.clip`, `detail.id` (the group's
|
|
113
|
+
`data-pg-reels-id`) and `detail.where` (`"card"` or `"full"`). The
|
|
114
|
+
package counts nothing: the site sends it on — to its own server, which
|
|
115
|
+
counts and leaves out the same visitor twice a day — and writes the
|
|
116
|
+
number back in `data-pg-reels-views`.
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
section.addEventListener("pg-reels:watched", (e) => {
|
|
120
|
+
const { id, clip, where } = e.detail;
|
|
121
|
+
navigator.sendBeacon("/api/reels/watched", JSON.stringify({ id, clip, where }));
|
|
122
|
+
});
|
|
123
|
+
```
|
|
109
124
|
- **Reduced motion:** nothing plays by itself; a bar pressed shows its clip.
|
|
110
125
|
- **Seen.** A group is seen once **every one of its clips** has been shown
|
|
111
126
|
in the full view — one clip watched, and it is not; the rest watched
|
|
@@ -132,6 +147,22 @@ below).
|
|
|
132
147
|
[data-pg-reels-seen="true"] .avatar::before { background: rgb(255 255 255 / 0.5); }
|
|
133
148
|
```
|
|
134
149
|
|
|
150
|
+
**From the first paint.** `initReels()` marks what was seen once the module
|
|
151
|
+
has loaded — a moment after the page is drawn, so a ring seen before
|
|
152
|
+
would show bright for that moment, then go pale. `early.js`, a small
|
|
153
|
+
classic script in `<head>` after the stylesheets, marks each group as the
|
|
154
|
+
browser reads it, before anything is drawn:
|
|
155
|
+
|
|
156
|
+
```html
|
|
157
|
+
<script src="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/early.js"></script>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
No dependencies, nothing to call; `initReels()` takes over after. It reads
|
|
161
|
+
`--pg-reels-seen` and `--pg-reels-seen-for` from the stylesheets; set by a
|
|
162
|
+
script later, they are `initReels()`'s alone. In React no script is
|
|
163
|
+
needed: start the package in a layout effect (below), which runs before
|
|
164
|
+
the page is drawn.
|
|
165
|
+
|
|
135
166
|
## The full view
|
|
136
167
|
|
|
137
168
|
A `<dialog>` the package builds on `<body>` — its own buttons and icons;
|
|
@@ -233,7 +264,7 @@ and videos: the box has the shape, what is inside is 100% of it.
|
|
|
233
264
|
| `data-pg-reels-strip` | the row | Scrolls sideways |
|
|
234
265
|
| `data-pg-reels-group` | each group | A card and what is under it |
|
|
235
266
|
| `data-pg-reels-id` | a group | Its name, for what was seen to be remembered in this browser |
|
|
236
|
-
| `data-pg-reels-views` | in the card | A clip's view count, the site's — one a clip, in order, as the products: only the playing clip's shows, and it is copied into the full view, under the close. Only one in the card: the group's, always shown |
|
|
267
|
+
| `data-pg-reels-views` | in the card | A clip's view count, the site's (counted from `pg-reels:watched`) — one a clip, in order, as the products: only the playing clip's shows, and it is copied into the full view, under the close. Only one in the card: the group's, always shown |
|
|
237
268
|
| `data-pg-reels-seen` | a group, the full view's head | Written: `"true"` once seen in the full view, `"false"` before |
|
|
238
269
|
| `data-pg-reels-card` | the card | The shape; its clips fill it |
|
|
239
270
|
| `data-pg-reels-clip` | each clip | A video with its poster, or a picture |
|
|
@@ -262,11 +293,12 @@ and videos: the box has the shape, what is inside is 100% of it.
|
|
|
262
293
|
```tsx
|
|
263
294
|
"use client";
|
|
264
295
|
|
|
265
|
-
import {
|
|
296
|
+
import { useLayoutEffect } from "react";
|
|
266
297
|
import { initReels } from "@plusguard/reels";
|
|
267
298
|
|
|
268
299
|
export function ReelsInit() {
|
|
269
|
-
|
|
300
|
+
// Before the page is drawn: a ring seen before is pale from the start.
|
|
301
|
+
useLayoutEffect(() => initReels(), []);
|
|
270
302
|
return null;
|
|
271
303
|
}
|
|
272
304
|
```
|
|
@@ -296,7 +328,7 @@ So in React they go in the clip, as an element kept hidden:
|
|
|
296
328
|
Give its pictures `loading="lazy"`: hidden, they load when the full view
|
|
297
329
|
shows them. See [`examples/react/Example.tsx`](examples/react/Example.tsx)
|
|
298
330
|
— the section from data (variants and categories too), and
|
|
299
|
-
`pg-reels:open`
|
|
331
|
+
`pg-reels:open` and `pg-reels:watched` sent on to the site's server.
|
|
300
332
|
|
|
301
333
|
### The React example, running
|
|
302
334
|
|
package/dist/index.js
CHANGED
|
@@ -47,6 +47,13 @@
|
|
|
47
47
|
* the event pg-reels:open on the section (detail: group, clip); a site
|
|
48
48
|
* that calls preventDefault() on it opens its own.
|
|
49
49
|
*
|
|
50
|
+
* Watched: a clip that has played 3 seconds — or half of it, if it is
|
|
51
|
+
* shorter than 6 — on a card or in the full view, sends pg-reels:watched
|
|
52
|
+
* on the section (detail: group, clip, id — its group's data-pg-reels-id —
|
|
53
|
+
* and where: "card" or "full"), once a clip a page visit. The package only
|
|
54
|
+
* says so; counting, and the number shown in data-pg-reels-views, are the
|
|
55
|
+
* site's.
|
|
56
|
+
*
|
|
50
57
|
* The full view — a <dialog>, built here on <body>, its contents copies:
|
|
51
58
|
* the clip playing, the group's data-pg-reels-head, the clip's product.
|
|
52
59
|
* Within a group: the bars on top, a press on the video's right or left,
|
|
@@ -340,6 +347,21 @@ export function forgetReelsSeen() {
|
|
|
340
347
|
watched = new WeakMap();
|
|
341
348
|
paintSeen();
|
|
342
349
|
}
|
|
350
|
+
/* ---------- Watched, said to the site ----------
|
|
351
|
+
A clip that has played long enough: 3 seconds, or half of it if it is
|
|
352
|
+
shorter — once a clip a page visit. The site counts. */
|
|
353
|
+
const WATCHED_MS = 3000;
|
|
354
|
+
const told = new WeakMap();
|
|
355
|
+
function watchedIf(g, clip, playedMs, totalMs, where) {
|
|
356
|
+
if (!(totalMs > 0) || playedMs < Math.min(WATCHED_MS, totalMs / 2))
|
|
357
|
+
return;
|
|
358
|
+
const said = told.get(g.el) ?? new Set();
|
|
359
|
+
if (said.has(clip))
|
|
360
|
+
return;
|
|
361
|
+
said.add(clip);
|
|
362
|
+
told.set(g.el, said);
|
|
363
|
+
g.root.dispatchEvent(new CustomEvent("pg-reels:watched", { bubbles: true, detail: { group: g.el, clip, id: g.el.getAttribute(ID), where } }));
|
|
364
|
+
}
|
|
343
365
|
// The clips of each group shown in the full view so far, this visit.
|
|
344
366
|
let watched = new WeakMap();
|
|
345
367
|
/* ---------- Everything on the page ---------- */
|
|
@@ -452,10 +474,16 @@ export function initReels() {
|
|
|
452
474
|
const clip = g.clips[g.at];
|
|
453
475
|
const v = videoOf(clip);
|
|
454
476
|
let progress;
|
|
455
|
-
if (v)
|
|
477
|
+
if (v) {
|
|
456
478
|
progress = v.duration ? v.currentTime / v.duration : 0;
|
|
457
|
-
|
|
458
|
-
|
|
479
|
+
watchedIf(g, g.at, v.currentTime * 1000, v.duration * 1000, "card");
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
const time = pictureTime(g, clip);
|
|
483
|
+
const played = g.shown + now - g.since;
|
|
484
|
+
progress = played / Math.max(1, time);
|
|
485
|
+
watchedIf(g, g.at, played, time, "card");
|
|
486
|
+
}
|
|
459
487
|
g.bars[g.at]?.style.setProperty(PROGRESS, String(Math.min(1, progress)));
|
|
460
488
|
if (!v && progress >= 1)
|
|
461
489
|
due.push(g);
|
|
@@ -844,7 +872,10 @@ export function initReels() {
|
|
|
844
872
|
const g = current();
|
|
845
873
|
const clip = g.clips[view.ci];
|
|
846
874
|
const v = viewVideo();
|
|
847
|
-
const
|
|
875
|
+
const time = v ? v.duration * 1000 : pictureTime(g, clip);
|
|
876
|
+
const played = v ? v.currentTime * 1000 : view.shown + now - view.since;
|
|
877
|
+
const progress = v ? (v.duration ? v.currentTime / v.duration : 0) : played / Math.max(1, time);
|
|
878
|
+
watchedIf(g, view.ci, played, time, "full");
|
|
848
879
|
viewBars.children[view.ci]?.setAttribute("style", `${PROGRESS}: ${Math.min(1, progress)}`);
|
|
849
880
|
if (!v && progress >= 1) {
|
|
850
881
|
nextClip(true);
|
package/early.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* reels — early.js: before the page is first drawn, the seen marks.
|
|
3
|
+
*
|
|
4
|
+
* initReels() marks data-pg-reels-seen="true" once the module has loaded —
|
|
5
|
+
* a moment after the page is drawn, so a group seen before shows its ring
|
|
6
|
+
* bright first, then pale. This little script marks each group as the
|
|
7
|
+
* browser reads it, before anything is drawn; initReels() takes over after.
|
|
8
|
+
*
|
|
9
|
+
* A classic script, in <head>, after the page's stylesheets and before its
|
|
10
|
+
* markup — not deferred, not a module:
|
|
11
|
+
*
|
|
12
|
+
* <script src="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/early.js"></script>
|
|
13
|
+
*
|
|
14
|
+
* or its text written inline there. No dependencies; nothing to call. It
|
|
15
|
+
* reads --pg-reels-seen and --pg-reels-seen-for from the stylesheets, as
|
|
16
|
+
* initReels() does; set from a script later, they are initReels()'s alone.
|
|
17
|
+
*/
|
|
18
|
+
(function () {
|
|
19
|
+
var seen;
|
|
20
|
+
try {
|
|
21
|
+
seen = JSON.parse(localStorage.getItem("pg-reels-seen") || "{}");
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!seen || typeof seen !== "object" || Array.isArray(seen)) return;
|
|
26
|
+
var any = false;
|
|
27
|
+
for (var key in seen) any = true;
|
|
28
|
+
if (!any) return;
|
|
29
|
+
|
|
30
|
+
// "24h", "30m", "90s", "500ms", "7d" → ms; "forever" → Infinity.
|
|
31
|
+
var DAY = 24 * 60 * 60 * 1000;
|
|
32
|
+
var spanOf = function (raw) {
|
|
33
|
+
var text = String(raw || "").trim().toLowerCase();
|
|
34
|
+
if (!text) return DAY;
|
|
35
|
+
if (text === "forever" || text === "none") return Infinity;
|
|
36
|
+
var m = /^(-?[\d.]+)\s*(ms|s|m|h|d)?$/.exec(text);
|
|
37
|
+
if (!m) return DAY;
|
|
38
|
+
var unit = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 }[m[2] || "ms"];
|
|
39
|
+
var n = Number(m[1]) * unit;
|
|
40
|
+
return isFinite(n) ? Math.max(0, n) : DAY;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
var mark = function (el) {
|
|
44
|
+
var when = seen[el.getAttribute("data-pg-reels-id")];
|
|
45
|
+
if (typeof when !== "number") return;
|
|
46
|
+
var cs = getComputedStyle(el);
|
|
47
|
+
if (cs.getPropertyValue("--pg-reels-seen").trim() === "off") return;
|
|
48
|
+
if (Date.now() - when < spanOf(cs.getPropertyValue("--pg-reels-seen-for"))) el.setAttribute("data-pg-reels-seen", "true");
|
|
49
|
+
};
|
|
50
|
+
// Each group the browser adds while reading the page — or groups inside
|
|
51
|
+
// what it adds. The callback runs before the page is drawn.
|
|
52
|
+
var observer = new MutationObserver(function (records) {
|
|
53
|
+
for (var r = 0; r < records.length; r++) {
|
|
54
|
+
var added = records[r].addedNodes;
|
|
55
|
+
for (var n = 0; n < added.length; n++) {
|
|
56
|
+
var node = added[n];
|
|
57
|
+
if (node.nodeType !== 1) continue;
|
|
58
|
+
if (node.hasAttribute("data-pg-reels-group") && node.hasAttribute("data-pg-reels-id")) mark(node);
|
|
59
|
+
var inner = node.querySelectorAll("[data-pg-reels-group][data-pg-reels-id]");
|
|
60
|
+
for (var k = 0; k < inner.length; k++) mark(inner[k]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
observer.observe(document.documentElement, { childList: true, subtree: true });
|
|
65
|
+
// The page is read: initReels() keeps the marks from here on.
|
|
66
|
+
document.addEventListener("DOMContentLoaded", function () {
|
|
67
|
+
observer.disconnect();
|
|
68
|
+
});
|
|
69
|
+
})();
|
package/index.ts
CHANGED
|
@@ -47,6 +47,13 @@
|
|
|
47
47
|
* the event pg-reels:open on the section (detail: group, clip); a site
|
|
48
48
|
* that calls preventDefault() on it opens its own.
|
|
49
49
|
*
|
|
50
|
+
* Watched: a clip that has played 3 seconds — or half of it, if it is
|
|
51
|
+
* shorter than 6 — on a card or in the full view, sends pg-reels:watched
|
|
52
|
+
* on the section (detail: group, clip, id — its group's data-pg-reels-id —
|
|
53
|
+
* and where: "card" or "full"), once a clip a page visit. The package only
|
|
54
|
+
* says so; counting, and the number shown in data-pg-reels-views, are the
|
|
55
|
+
* site's.
|
|
56
|
+
*
|
|
50
57
|
* The full view — a <dialog>, built here on <body>, its contents copies:
|
|
51
58
|
* the clip playing, the group's data-pg-reels-head, the clip's product.
|
|
52
59
|
* Within a group: the bars on top, a press on the video's right or left,
|
|
@@ -366,6 +373,22 @@ export function forgetReelsSeen(): void {
|
|
|
366
373
|
paintSeen();
|
|
367
374
|
}
|
|
368
375
|
|
|
376
|
+
/* ---------- Watched, said to the site ----------
|
|
377
|
+
A clip that has played long enough: 3 seconds, or half of it if it is
|
|
378
|
+
shorter — once a clip a page visit. The site counts. */
|
|
379
|
+
|
|
380
|
+
const WATCHED_MS = 3000;
|
|
381
|
+
const told = new WeakMap<Element, Set<number>>();
|
|
382
|
+
|
|
383
|
+
function watchedIf(g: Group, clip: number, playedMs: number, totalMs: number, where: "card" | "full"): void {
|
|
384
|
+
if (!(totalMs > 0) || playedMs < Math.min(WATCHED_MS, totalMs / 2)) return;
|
|
385
|
+
const said = told.get(g.el) ?? new Set<number>();
|
|
386
|
+
if (said.has(clip)) return;
|
|
387
|
+
said.add(clip);
|
|
388
|
+
told.set(g.el, said);
|
|
389
|
+
g.root.dispatchEvent(new CustomEvent("pg-reels:watched", { bubbles: true, detail: { group: g.el, clip, id: g.el.getAttribute(ID), where } }));
|
|
390
|
+
}
|
|
391
|
+
|
|
369
392
|
// The clips of each group shown in the full view so far, this visit.
|
|
370
393
|
let watched = new WeakMap<Element, Set<number>>();
|
|
371
394
|
|
|
@@ -480,8 +503,15 @@ export function initReels(): () => void {
|
|
|
480
503
|
const clip = g.clips[g.at];
|
|
481
504
|
const v = videoOf(clip);
|
|
482
505
|
let progress: number;
|
|
483
|
-
if (v)
|
|
484
|
-
|
|
506
|
+
if (v) {
|
|
507
|
+
progress = v.duration ? v.currentTime / v.duration : 0;
|
|
508
|
+
watchedIf(g, g.at, v.currentTime * 1000, v.duration * 1000, "card");
|
|
509
|
+
} else {
|
|
510
|
+
const time = pictureTime(g, clip);
|
|
511
|
+
const played = g.shown + now - g.since;
|
|
512
|
+
progress = played / Math.max(1, time);
|
|
513
|
+
watchedIf(g, g.at, played, time, "card");
|
|
514
|
+
}
|
|
485
515
|
g.bars[g.at]?.style.setProperty(PROGRESS, String(Math.min(1, progress)));
|
|
486
516
|
if (!v && progress >= 1) due.push(g);
|
|
487
517
|
});
|
|
@@ -869,7 +899,10 @@ export function initReels(): () => void {
|
|
|
869
899
|
const g = current();
|
|
870
900
|
const clip = g.clips[view.ci];
|
|
871
901
|
const v = viewVideo();
|
|
872
|
-
const
|
|
902
|
+
const time = v ? v.duration * 1000 : pictureTime(g, clip);
|
|
903
|
+
const played = v ? v.currentTime * 1000 : view.shown + now - view.since;
|
|
904
|
+
const progress = v ? (v.duration ? v.currentTime / v.duration : 0) : played / Math.max(1, time);
|
|
905
|
+
watchedIf(g, view.ci, played, time, "full");
|
|
873
906
|
viewBars.children[view.ci]?.setAttribute("style", `${PROGRESS}: ${Math.min(1, progress)}`);
|
|
874
907
|
if (!v && progress >= 1) {
|
|
875
908
|
nextClip(true);
|
package/package.json
CHANGED
|
@@ -1,22 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plusguard/reels",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Shoppable reels for a store: a strip of video cards — each a group of clips, each clip with its own product — that opens into a full view. Up and down between groups, left and right within one, the product beside the video on a computer and under it on a phone. No dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
".": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
10
13
|
"./index.css": "./index.css"
|
|
11
14
|
},
|
|
12
|
-
"files": [
|
|
13
|
-
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"index.ts",
|
|
18
|
+
"index.css",
|
|
19
|
+
"early.js",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": [
|
|
24
|
+
"*.css"
|
|
25
|
+
],
|
|
14
26
|
"scripts": {
|
|
15
27
|
"build": "npx -p typescript tsc index.ts --outDir dist --declaration --target es2020 --module es2020 --lib dom,dom.iterable,es2020 --strict"
|
|
16
28
|
},
|
|
17
|
-
"publishConfig": {
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
18
32
|
"license": "MIT",
|
|
19
33
|
"author": "PlusGuard",
|
|
20
|
-
"repository": {
|
|
21
|
-
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/omisilli/reels.git"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"reels",
|
|
40
|
+
"shoppable-video",
|
|
41
|
+
"video-commerce",
|
|
42
|
+
"stories",
|
|
43
|
+
"ecommerce",
|
|
44
|
+
"vanilla"
|
|
45
|
+
]
|
|
22
46
|
}
|