@matterandmotion/emdash-engagement 0.1.0 → 0.1.1
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 +3 -2
- package/astro/Engagement.astro +45 -4
- package/dist/index.mjs +1 -1
- package/dist/manifest.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,9 +29,10 @@ import Engagement from "@matterandmotion/emdash-engagement/astro/Engagement.astr
|
|
|
29
29
|
It shows a like button and the view count. After the page loads it:
|
|
30
30
|
- fetches the counts (cached for 30 seconds);
|
|
31
31
|
- sends one view per entry per day from each browser;
|
|
32
|
-
- sends a like on click, and remembers it in the browser's local storage so the button stays pressed
|
|
32
|
+
- sends a like on click, and remembers it in the browser's local storage so the button stays pressed;
|
|
33
|
+
- remembers the highest counts it has shown, so a reload within the 30-second cache never shows fewer views than the visitor just saw.
|
|
33
34
|
|
|
34
|
-
Props: `collection`, `id` (the entry's ID, not its slug), `countView` (default `true`), `showViews` (default `true`), `likeLabel`, `viewsLabel
|
|
35
|
+
Props: `collection`, `id` (the entry's ID, not its slug), `countView` (default `true`), `showViews` (default `true`), `likeLabel`, `viewLabel` and `viewsLabel` (the words after a count of one and any other count, chosen by the page language's plural rules), and `pluginId` for registry installs. Style it through the `mm-engagement` classes, `--mm-engagement-font-size` and `--mm-engagement-liked`. Without JavaScript it stays hidden.
|
|
35
36
|
|
|
36
37
|
## Popular lists
|
|
37
38
|
|
package/astro/Engagement.astro
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* after the page loads, so the page itself stays cacheable.
|
|
5
5
|
*
|
|
6
6
|
* - Sends one view per entry per day from each browser (unless `countView` is false).
|
|
7
|
-
* - Remembers a like in localStorage, so the button stays pressed
|
|
7
|
+
* - Remembers a like in localStorage, so the button stays pressed, and the
|
|
8
|
+
* highest counts it has shown, so a cached response never shows fewer.
|
|
8
9
|
* - Without JavaScript the counts and button stay hidden.
|
|
9
10
|
*
|
|
10
11
|
* Style it through the `mm-engagement` classes or the CSS custom properties below.
|
|
@@ -22,6 +23,9 @@ interface Props {
|
|
|
22
23
|
/** Show the view count. Default true. */
|
|
23
24
|
showViews?: boolean;
|
|
24
25
|
likeLabel?: string;
|
|
26
|
+
/** Shown after a count of one ("1 view"), as the page language's plural rules decide. */
|
|
27
|
+
viewLabel?: string;
|
|
28
|
+
/** Shown after any other count ("0 views", "2 views"). */
|
|
25
29
|
viewsLabel?: string;
|
|
26
30
|
}
|
|
27
31
|
|
|
@@ -32,6 +36,7 @@ const {
|
|
|
32
36
|
countView = true,
|
|
33
37
|
showViews = true,
|
|
34
38
|
likeLabel = "Like",
|
|
39
|
+
viewLabel = "view",
|
|
35
40
|
viewsLabel = "views",
|
|
36
41
|
} = Astro.props;
|
|
37
42
|
const endpoint = engagementEndpoint(pluginId ? { pluginId } : {});
|
|
@@ -54,7 +59,7 @@ const endpoint = engagementEndpoint(pluginId ? { pluginId } : {});
|
|
|
54
59
|
{
|
|
55
60
|
showViews && (
|
|
56
61
|
<span class="mm-engagement__views">
|
|
57
|
-
<span data-views>0</span> {viewsLabel}
|
|
62
|
+
<span data-views>0</span> <span data-views-label data-one={viewLabel} data-other={viewsLabel}>{viewsLabel}</span>
|
|
58
63
|
</span>
|
|
59
64
|
)
|
|
60
65
|
}
|
|
@@ -108,6 +113,15 @@ const endpoint = engagementEndpoint(pluginId ? { pluginId } : {});
|
|
|
108
113
|
return (json.data ?? json) as Record<string, unknown>;
|
|
109
114
|
}
|
|
110
115
|
|
|
116
|
+
/** "one" or another plural category for `count`, in the page's language. */
|
|
117
|
+
function pluralCategory(count: number) {
|
|
118
|
+
try {
|
|
119
|
+
return new Intl.PluralRules(document.documentElement.lang || undefined).select(count);
|
|
120
|
+
} catch {
|
|
121
|
+
return count === 1 ? "one" : "other";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
111
125
|
function post(url: string, body: unknown) {
|
|
112
126
|
return call(url, {
|
|
113
127
|
method: "POST",
|
|
@@ -127,12 +141,39 @@ const endpoint = engagementEndpoint(pluginId ? { pluginId } : {});
|
|
|
127
141
|
const button = root.querySelector<HTMLButtonElement>("[data-like]");
|
|
128
142
|
const likes = root.querySelector<HTMLElement>("[data-likes]");
|
|
129
143
|
const views = root.querySelector<HTMLElement>("[data-views]");
|
|
144
|
+
const viewsLabel = root.querySelector<HTMLElement>("[data-views-label]");
|
|
130
145
|
const target = { collection, id };
|
|
131
146
|
|
|
147
|
+
// The highest counts this browser has seen for the entry. All-time counts
|
|
148
|
+
// only grow, so a cached counts response (30 s), or one that arrives after
|
|
149
|
+
// this browser's own view or like, never takes them back down.
|
|
150
|
+
const seenKey = `${key}:seen`;
|
|
151
|
+
const seen: Partial<Counts> = {};
|
|
132
152
|
const show = (counts: Partial<Counts>) => {
|
|
133
|
-
if (
|
|
134
|
-
|
|
153
|
+
if (typeof counts.likes === "number") {
|
|
154
|
+
seen.likes = Math.max(seen.likes ?? 0, counts.likes);
|
|
155
|
+
if (likes) likes.textContent = String(seen.likes);
|
|
156
|
+
}
|
|
157
|
+
if (typeof counts.views === "number") {
|
|
158
|
+
seen.views = Math.max(seen.views ?? 0, counts.views);
|
|
159
|
+
if (views) views.textContent = String(seen.views);
|
|
160
|
+
if (viewsLabel) {
|
|
161
|
+
const { one, other } = viewsLabel.dataset;
|
|
162
|
+
viewsLabel.textContent = (pluralCategory(seen.views) === "one" ? one : other) ?? "";
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
store?.setItem(seenKey, JSON.stringify(seen));
|
|
167
|
+
} catch {
|
|
168
|
+
// Storage full or blocked: the counts still show.
|
|
169
|
+
}
|
|
135
170
|
};
|
|
171
|
+
try {
|
|
172
|
+
const saved = JSON.parse(store?.getItem(seenKey) ?? "null") as Partial<Counts> | null;
|
|
173
|
+
if (saved && typeof saved === "object") show(saved);
|
|
174
|
+
} catch {
|
|
175
|
+
// Unreadable: wait for the server's counts.
|
|
176
|
+
}
|
|
136
177
|
const pressed = () => {
|
|
137
178
|
button?.setAttribute("aria-pressed", "true");
|
|
138
179
|
if (button) button.disabled = true;
|
package/dist/index.mjs
CHANGED
|
@@ -9,7 +9,7 @@ const descriptor = Object.freeze({
|
|
|
9
9
|
"id": "mm-engagement",
|
|
10
10
|
"displayName": "Engagement",
|
|
11
11
|
"description": "Likes, view counts and top lists, counted from the browser so pages stay cacheable, with no cookies.",
|
|
12
|
-
"version": "0.1.
|
|
12
|
+
"version": "0.1.1",
|
|
13
13
|
"format": "standard",
|
|
14
14
|
"entrypoint": "@matterandmotion/emdash-engagement/sandbox",
|
|
15
15
|
"capabilities": [
|
package/dist/manifest.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matterandmotion/emdash-engagement",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Likes, view counts and top lists for EmDash CMS entries, counted from the browser so pages stay cacheable, with no cookies.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|