@joryio/web-sdk 1.4.0-beta.2 → 1.4.0-beta.5

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +1 -1
  3. package/dist/cards-ui/card-feed-view.d.ts +4 -0
  4. package/dist/cards-ui/card-feed-view.d.ts.map +1 -0
  5. package/dist/cards-ui/index.d.ts +4 -0
  6. package/dist/cards-ui/index.d.ts.map +1 -0
  7. package/dist/core/cards-ui-contract.d.ts +30 -0
  8. package/dist/core/cards-ui-contract.d.ts.map +1 -0
  9. package/dist/core/cards.d.ts +131 -0
  10. package/dist/core/cards.d.ts.map +1 -0
  11. package/dist/core/config.d.ts +21 -1
  12. package/dist/core/config.d.ts.map +1 -1
  13. package/dist/core/editor-gate.d.ts +47 -0
  14. package/dist/core/editor-gate.d.ts.map +1 -0
  15. package/dist/core/flags.d.ts +74 -0
  16. package/dist/core/flags.d.ts.map +1 -0
  17. package/dist/core/inapp.d.ts +13 -1
  18. package/dist/core/inapp.d.ts.map +1 -1
  19. package/dist/core/links.d.ts +27 -0
  20. package/dist/core/links.d.ts.map +1 -0
  21. package/dist/core/network.d.ts.map +1 -1
  22. package/dist/core/page-context.d.ts +101 -0
  23. package/dist/core/page-context.d.ts.map +1 -0
  24. package/dist/core/page-edits.d.ts +129 -0
  25. package/dist/core/page-edits.d.ts.map +1 -0
  26. package/dist/core/placements.d.ts +219 -0
  27. package/dist/core/placements.d.ts.map +1 -0
  28. package/dist/core/sdk-host-files.d.ts +5 -0
  29. package/dist/core/sdk-host-files.d.ts.map +1 -0
  30. package/dist/core/sdk.d.ts +37 -0
  31. package/dist/core/sdk.d.ts.map +1 -1
  32. package/dist/core/subscription.d.ts +6 -0
  33. package/dist/core/subscription.d.ts.map +1 -0
  34. package/dist/editor/index.d.ts +4 -0
  35. package/dist/editor/index.d.ts.map +1 -0
  36. package/dist/editor/visual-editor.d.ts +39 -0
  37. package/dist/editor/visual-editor.d.ts.map +1 -0
  38. package/dist/index.d.mts +206 -3
  39. package/dist/index.d.ts +9 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/joryio-cards-ui.min.js +7 -0
  42. package/dist/joryio-cards-ui.min.js.map +1 -0
  43. package/dist/joryio-editor.min.js +7 -0
  44. package/dist/joryio-editor.min.js.map +1 -0
  45. package/dist/joryio-sw.js +1 -1
  46. package/dist/joryio.cjs.js +7215 -3508
  47. package/dist/joryio.min.js +2 -2
  48. package/dist/joryio.min.js.map +1 -1
  49. package/dist/joryio.mjs +7207 -3509
  50. package/dist/joryio.umd.js +7214 -3507
  51. package/package.json +4 -1
  52. package/src/cards-ui/card-feed-view.ts +249 -0
  53. package/src/cards-ui/index.ts +26 -0
  54. package/src/core/cards-ui-contract.ts +58 -0
  55. package/src/core/cards.ts +846 -0
  56. package/src/core/config.ts +86 -2
  57. package/src/core/editor-gate.ts +158 -0
  58. package/src/core/flags.ts +378 -0
  59. package/src/core/inapp.ts +283 -152
  60. package/src/core/links.ts +104 -0
  61. package/src/core/network.ts +3 -0
  62. package/src/core/page-context.ts +460 -0
  63. package/src/core/page-edits.ts +1036 -0
  64. package/src/core/placements.ts +1434 -0
  65. package/src/core/sdk-host-files.ts +93 -0
  66. package/src/core/sdk.ts +383 -1
  67. package/src/core/subscription.ts +27 -0
  68. package/src/editor/index.ts +27 -0
  69. package/src/editor/visual-editor.ts +301 -0
  70. package/src/index.ts +35 -0
  71. package/src/snippet/loader.js +25 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joryio/web-sdk",
3
- "version": "1.4.0-beta.2",
3
+ "version": "1.4.0-beta.5",
4
4
  "description": "Joryio Web Tracking SDK - Lightweight analytics and event tracking",
5
5
  "main": "dist/joryio.cjs.js",
6
6
  "module": "dist/joryio.mjs",
@@ -34,6 +34,7 @@
34
34
  ],
35
35
  "scripts": {
36
36
  "build": "rollup -c",
37
+ "size": "node scripts/check-size.js",
37
38
  "build:watch": "rollup -c -w",
38
39
  "dev": "rollup -c -w",
39
40
  "type-check": "tsc --noEmit",
@@ -67,6 +68,8 @@
67
68
  "babel-jest": "^29.7.0",
68
69
  "eslint": "^8.56.0",
69
70
  "jest": "^29.7.0",
71
+ "jest-environment-jsdom": "29.7.0",
72
+ "jsdom": "24.1.3",
70
73
  "rollup": "^4.9.2",
71
74
  "rollup-plugin-dts": "^6.1.0",
72
75
  "rollup-plugin-visualizer": "^5.12.0",
@@ -0,0 +1,249 @@
1
+ /**
2
+ * The default card feed (`mountCardFeed`), drawn from what the core's
3
+ * CardFeedHost gives it (src/core/cards-ui-contract.ts). Lives in
4
+ * `joryio-cards-ui.min.js`, loaded only by a page that mounts a feed.
5
+ *
6
+ * Text is set as text, never as markup; colors and fonts are the page's.
7
+ */
8
+ import type { Card, MountCardFeedOptions } from '../core/cards';
9
+ import type { CardFeedHost, CardFeedView } from '../core/cards-ui-contract';
10
+ import type { ViewTracker } from '../core/placements';
11
+ import { isAppDeepLink, safeHttpUrl } from '../core/placements';
12
+
13
+ type DrawnCard = { node: HTMLElement; sig: string; tracker: ViewTracker | null };
14
+
15
+ const DEFAULT_LABELS = { emptyText: 'No messages yet', pinnedLabel: 'Pinned', dismissLabel: 'Dismiss', unreadLabel: 'Unread' };
16
+
17
+ export function mountFeedView(element: HTMLElement, options: MountCardFeedOptions, host: CardFeedHost): CardFeedView {
18
+ const opts = options || {};
19
+ let removed = false;
20
+ let loadingMore = false;
21
+ /** Drawn card nodes by card key, reused while the card looks the same. */
22
+ let nodes = new Map<string, DrawnCard>();
23
+ let empty: HTMLElement | null = null;
24
+
25
+ const root = document.createElement('div');
26
+ root.className = 'joryio-card-feed';
27
+ root.setAttribute('role', 'list');
28
+ const sentinel = document.createElement('div');
29
+ sentinel.className = 'joryio-card-feed-end';
30
+ sentinel.setAttribute('aria-hidden', 'true');
31
+ element.appendChild(root);
32
+
33
+ /** A tap: report clicked, then open the url by the placement link rule (the site-wide link handler sees it first). */
34
+ const click = (card: Card): void => {
35
+ host.markCardClicked(card);
36
+ const url = card.url?.trim();
37
+ if (!url) return;
38
+ const context = { campaignId: card.campaignId, cardId: card.id };
39
+ const http = safeHttpUrl(url);
40
+ if (http) {
41
+ host.routeLink(http, context, () => {
42
+ try {
43
+ window.open(http, '_blank', 'noopener');
44
+ } catch (error) {
45
+ host.logger.debug('Could not open the card link', error);
46
+ }
47
+ });
48
+ return;
49
+ }
50
+ if (isAppDeepLink(url)) {
51
+ // The site routes its own links; the SDK never navigates to a custom scheme.
52
+ host.routeLink(url, context, () => {
53
+ if (!opts.onDeepLink) return;
54
+ try {
55
+ opts.onDeepLink(url, card);
56
+ } catch (error) {
57
+ host.logger.error('The mountCardFeed onDeepLink callback threw; ignored:', error);
58
+ }
59
+ });
60
+ return;
61
+ }
62
+ host.logger.warn('Card link refused: not http(s) or an app deep link');
63
+ };
64
+
65
+ const label = (k: keyof typeof DEFAULT_LABELS): string => {
66
+ const v = opts[k];
67
+ return typeof v === 'string' ? v : DEFAULT_LABELS[k];
68
+ };
69
+
70
+ /** Image on top, a header with the unread dot, the title, the pinned marker and the dismiss control, then the body and the button. */
71
+ const drawCard = (card: Card, generation: number): HTMLElement => {
72
+ const el = document.createElement('div');
73
+ el.className = 'joryio-card';
74
+ el.setAttribute('role', 'listitem');
75
+ el.setAttribute('data-joryio-card', card.id);
76
+ el.setAttribute('data-joryio-card-read', card.read ? 'true' : 'false');
77
+ if (card.pinned) el.setAttribute('data-joryio-card-pinned', 'true');
78
+ el.style.cssText = `position:relative;box-sizing:border-box;padding:12px 16px;${card.url ? 'cursor:pointer;' : ''}`;
79
+ const open = () => !removed && generation === host.generation() && click(card);
80
+ if (card.url) {
81
+ el.setAttribute('tabindex', '0');
82
+ el.addEventListener('click', open);
83
+ el.addEventListener('keydown', (e: any) => {
84
+ if (e?.key === 'Enter' && e?.target === el) open();
85
+ });
86
+ }
87
+ const image = safeHttpUrl(card.imageUrl);
88
+ if (image) {
89
+ const img = document.createElement('img');
90
+ img.className = 'joryio-card-image';
91
+ img.src = image;
92
+ img.alt = '';
93
+ img.style.cssText = 'display:block;max-width:100%;height:auto;margin-bottom:8px;';
94
+ el.appendChild(img);
95
+ }
96
+ const header = document.createElement('div');
97
+ header.className = 'joryio-card-header';
98
+ header.style.cssText = 'display:flex;align-items:center;gap:6px;';
99
+ if (!card.read) {
100
+ const dot = document.createElement('span');
101
+ dot.className = 'joryio-card-unread';
102
+ dot.setAttribute('role', 'img');
103
+ dot.setAttribute('aria-label', label('unreadLabel'));
104
+ dot.textContent = '●';
105
+ dot.style.cssText = 'font-size:10px;line-height:1;';
106
+ header.appendChild(dot);
107
+ }
108
+ const title = document.createElement('div');
109
+ title.className = 'joryio-card-title';
110
+ title.textContent = card.title || '';
111
+ title.style.cssText = 'flex:1;font-weight:bold;';
112
+ header.appendChild(title);
113
+ if (card.pinned) {
114
+ const pinned = document.createElement('span');
115
+ pinned.className = 'joryio-card-pinned';
116
+ pinned.textContent = label('pinnedLabel');
117
+ pinned.style.cssText = 'font-size:0.8em;';
118
+ header.appendChild(pinned);
119
+ }
120
+ const dismiss = document.createElement('button');
121
+ dismiss.type = 'button';
122
+ dismiss.className = 'joryio-card-dismiss';
123
+ dismiss.setAttribute('aria-label', label('dismissLabel'));
124
+ dismiss.textContent = '×';
125
+ dismiss.style.cssText = 'background:none;border:0;padding:4px 8px;color:inherit;font:inherit;font-size:1.25em;line-height:1;cursor:pointer;';
126
+ dismiss.addEventListener('click', (e: any) => {
127
+ e?.stopPropagation?.();
128
+ if (generation === host.generation()) host.dismissCard(card);
129
+ });
130
+ header.appendChild(dismiss);
131
+ el.appendChild(header);
132
+ if (card.body) {
133
+ const body = document.createElement('div');
134
+ body.className = 'joryio-card-body';
135
+ body.textContent = card.body;
136
+ el.appendChild(body);
137
+ }
138
+ if (card.buttonText && card.url) {
139
+ const btn = document.createElement('button');
140
+ btn.type = 'button';
141
+ btn.className = 'joryio-card-button';
142
+ btn.textContent = card.buttonText;
143
+ btn.style.cssText = 'margin-top:8px;font:inherit;';
144
+ btn.addEventListener('click', (e: any) => {
145
+ e?.stopPropagation?.();
146
+ open();
147
+ });
148
+ el.appendChild(btn);
149
+ }
150
+ return el;
151
+ };
152
+
153
+ const render = (): void => {
154
+ if (removed) return;
155
+ const feed = host.view();
156
+ const generation = host.generation();
157
+ const keep = new Map<string, DrawnCard>();
158
+ for (const card of feed.cards) {
159
+ const key = `${card.id}\u0000${card.createdAt}`;
160
+ const sig = JSON.stringify(card);
161
+ const existing = nodes.get(key);
162
+ if (existing && existing.sig === sig) {
163
+ keep.set(key, existing);
164
+ nodes.delete(key);
165
+ continue;
166
+ }
167
+ const node = drawCard(card, generation);
168
+ // Viewed, then read, once at least half the card was on screen for a second.
169
+ const tracker = card.read
170
+ ? null
171
+ : host.trackViewable(node, () => {
172
+ if (generation !== host.generation()) return;
173
+ host.markCardViewed(card);
174
+ host.markCardRead(card);
175
+ });
176
+ keep.set(key, { node, sig, tracker });
177
+ }
178
+ // Cards no longer in the feed (dismissed, expired, another person's) go.
179
+ nodes.forEach((n) => {
180
+ n.tracker?.cancel();
181
+ n.node.remove();
182
+ });
183
+ nodes = keep;
184
+ if (empty) {
185
+ empty.remove();
186
+ empty = null;
187
+ }
188
+ sentinel.remove();
189
+ if (!feed.cards.length) {
190
+ empty = document.createElement('div');
191
+ empty.className = 'joryio-card-feed-empty';
192
+ empty.textContent = label('emptyText');
193
+ empty.style.cssText = 'padding:32px 16px;text-align:center;';
194
+ root.appendChild(empty);
195
+ } else {
196
+ // appendChild moves a node that is already drawn: the order follows the feed.
197
+ keep.forEach((n) => root.appendChild(n.node));
198
+ }
199
+ root.appendChild(sentinel);
200
+ element.setAttribute('data-joryio-card-feed-state', feed.cards.length ? 'filled' : 'empty');
201
+ if (opts.onRender) {
202
+ try {
203
+ opts.onRender(feed, element);
204
+ } catch (error) {
205
+ host.logger.error('The mountCardFeed onRender callback threw; ignored:', error);
206
+ }
207
+ }
208
+ };
209
+
210
+ // Load the next page when the end of the feed comes into view.
211
+ let observer: IntersectionObserver | null = null;
212
+ if (typeof IntersectionObserver !== 'undefined') {
213
+ observer = new IntersectionObserver((entries) => {
214
+ if (removed || loadingMore || !entries.some((e) => e.isIntersecting)) return;
215
+ loadingMore = true;
216
+ host
217
+ .loadMore()
218
+ .catch(() => undefined)
219
+ .finally(() => {
220
+ loadingMore = false;
221
+ });
222
+ });
223
+ observer.observe(sentinel);
224
+ }
225
+
226
+ // The viewability dwell stops while the page is hidden.
227
+ const onVisibility = () => {
228
+ const visible = document.visibilityState !== 'hidden';
229
+ nodes.forEach((n) => {
230
+ if (!n.tracker) return;
231
+ if (visible) n.tracker.resume();
232
+ else n.tracker.pause();
233
+ });
234
+ };
235
+ if (typeof document.addEventListener === 'function') document.addEventListener('visibilitychange', onVisibility);
236
+
237
+ return {
238
+ render,
239
+ remove() {
240
+ if (removed) return;
241
+ removed = true;
242
+ observer?.disconnect();
243
+ if (typeof document.removeEventListener === 'function') document.removeEventListener('visibilitychange', onVisibility);
244
+ nodes.forEach((n) => n.tracker?.cancel());
245
+ nodes.clear();
246
+ root.remove();
247
+ },
248
+ };
249
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The default card feed bundle - `joryio-cards-ui.min.js` on the SDK host,
3
+ * next to `joryio.min.js` (BACKLOG D444).
4
+ *
5
+ * Loaded by the core (src/core/cards.ts) the first time the page calls
6
+ * `mountCardFeed`, so a visitor of a site without a feed never downloads it.
7
+ * It registers one global and does nothing else on its own: no request, no
8
+ * listener, until the core calls `mount` with a CardFeedHost.
9
+ *
10
+ * A bundler user who does not want the SDK host fetch imports this file once
11
+ * (`import '@joryio/web-sdk/dist/joryio-cards-ui.min.js'`): the core finds the
12
+ * global and draws at once.
13
+ */
14
+ import { mountFeedView } from './card-feed-view';
15
+ import { CARDS_UI_API_VERSION, CARDS_UI_GLOBAL, type CardsUiBundleApi } from '../core/cards-ui-contract';
16
+
17
+ const api: CardsUiBundleApi = {
18
+ version: CARDS_UI_API_VERSION,
19
+ mount: mountFeedView,
20
+ };
21
+
22
+ if (typeof window !== 'undefined' && !(window as any)[CARDS_UI_GLOBAL]) {
23
+ (window as any)[CARDS_UI_GLOBAL] = api;
24
+ }
25
+
26
+ export { api as cardsUiApi };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * The contract between the core (src/core/cards.ts) and the default card feed
3
+ * renderer, `joryio-cards-ui.min.js` (src/cards-ui/), a separate file next to
4
+ * `joryio.min.js` on the SDK host (BACKLOG D444). Only a page that calls
5
+ * `mountCardFeed` downloads it; the data API (getCards, markCard*, ...) stays
6
+ * in the core.
7
+ *
8
+ * The renderer registers one global, `__joryioCardsUi`, and nothing else. The
9
+ * core reads it once, checks `version`, keeps the reference, and hands each
10
+ * mount a `CardFeedHost`: what the renderer may do is exactly what the public
11
+ * API already lets a page do (read the feed, report viewed / read / clicked /
12
+ * dismissed, pass a link to its own link handler, load the next page) - never
13
+ * the delivery tokens, the request code or the storage. A page that replaced the global
14
+ * would get nothing it could not already call on `window.Joryio`.
15
+ *
16
+ * Type-only imports: nothing of the core is bundled into the renderer through here.
17
+ */
18
+ import type { Card, CardFeed, MountCardFeedOptions } from './cards';
19
+ import type { ViewTracker } from './placements';
20
+ import type { Logger } from '../utils/logger';
21
+
22
+ export const CARDS_UI_GLOBAL = '__joryioCardsUi';
23
+ /** Bumped on a breaking change to CardFeedHost / CardsUiBundleApi; a renderer of another version is refused. */
24
+ export const CARDS_UI_API_VERSION = 1;
25
+ /** The file next to joryio.min.js on the SDK host (and in the npm package's dist/). */
26
+ export const CARDS_UI_BUNDLE_FILE = 'joryio-cards-ui.min.js';
27
+
28
+ /** What one mounted feed may ask of the core. */
29
+ export interface CardFeedHost {
30
+ /** The feed now (cached, with this browser's read / dismissed state applied). */
31
+ view(): CardFeed;
32
+ /** Bumped when the person or the consent changes: a node drawn under an older one does nothing. */
33
+ generation(): number;
34
+ markCardViewed(card: Card): void;
35
+ markCardRead(card: Card): void;
36
+ markCardClicked(card: Card): void;
37
+ dismissCard(card: Card): void;
38
+ /** Hand a card link to the site-wide link handler (`setLinkHandler`); `fallback` runs when there is none or it declined. */
39
+ routeLink(url: string, context: { campaignId?: string; cardId?: string }, fallback: () => void): void;
40
+ /** Load the page after the feed's nextCursor, if any (a no-op on the last page). */
41
+ loadMore(): Promise<void>;
42
+ /** The viewability rule (half the card on screen for the dwell), the same as placements'. */
43
+ trackViewable(node: Element, onViewable: () => void): ViewTracker;
44
+ logger: Logger;
45
+ }
46
+
47
+ /** One drawn feed. */
48
+ export interface CardFeedView {
49
+ /** Redraw from host.view() (the core calls it on every feed change). */
50
+ render(): void;
51
+ /** Take out everything drawn and stop observing. */
52
+ remove(): void;
53
+ }
54
+
55
+ export interface CardsUiBundleApi {
56
+ version: number;
57
+ mount(element: HTMLElement, options: MountCardFeedOptions, host: CardFeedHost): CardFeedView;
58
+ }