@vizbl/loader 0.0.0-canary-20261115065952
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/dist/embed.d.ts +1 -0
- package/dist/endpoint-resolver.d.ts +19 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +457 -0
- package/dist/keys.d.ts +16 -0
- package/dist/local-resolver.d.ts +10 -0
- package/dist/resolve-contract.d.ts +84 -0
- package/dist/vizbl-button.d.ts +26 -0
- package/dist/vizbl.js +20 -0
- package/dist/widget-scripts.d.ts +40 -0
- package/package.json +36 -0
package/dist/embed.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ButtonResolver } from './resolve-contract';
|
|
2
|
+
export interface EndpointResolverOptions {
|
|
3
|
+
fetch?: typeof fetch;
|
|
4
|
+
/**
|
|
5
|
+
* Answers when the API does not know the route yet (404): the endpoint
|
|
6
|
+
* reaches each environment on its own schedule, and a store that already
|
|
7
|
+
* installed the loader must not lose its buttons in between.
|
|
8
|
+
*/
|
|
9
|
+
fallback?: ButtonResolver;
|
|
10
|
+
}
|
|
11
|
+
type ResolveRequest = Parameters<ButtonResolver>[0];
|
|
12
|
+
export declare function buildEndpointUrl({ identifier, keys, product }: ResolveRequest): string;
|
|
13
|
+
/**
|
|
14
|
+
* Asks the backend which button to show. The answer is the same for every
|
|
15
|
+
* button of one product on a page, so each question is asked once per page
|
|
16
|
+
* load; the browser and the CDN cache it across page loads.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createEndpointResolver(options?: EndpointResolverOptions): ButtonResolver;
|
|
19
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type { EndpointResolverOptions } from './endpoint-resolver';
|
|
2
|
+
export { buildEndpointUrl, createEndpointResolver } from './endpoint-resolver';
|
|
3
|
+
export type { LoaderKeys } from './keys';
|
|
4
|
+
export { parseLoaderKeys, readLoaderKeys } from './keys';
|
|
5
|
+
export { resolveLocally } from './local-resolver';
|
|
6
|
+
export type { ButtonDecision, ButtonHiddenReason, ButtonResolver, ButtonWidgetData, CoreArMode, HiddenButton, LoaderResolveRequest, LoaderResolveResponse, ShownButton, StoreProductIdentifier, TryOnZone, VizblProduct, } from './resolve-contract';
|
|
7
|
+
export { BUTTON_HIDDEN_REASONS, isVizblProduct, VIZBL_PRODUCTS } from './resolve-contract';
|
|
8
|
+
export type { RegisterVizblButtonOptions } from './vizbl-button';
|
|
9
|
+
export { registerVizblButton, VIZBL_BUTTON_TAG_NAME, VizblButtonElement } from './vizbl-button';
|
|
10
|
+
export { WIDGET_SCRIPT_URLS } from './widget-scripts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
const PUBLISHABLE_KEY = /^pk_(?:live|test)_[\w-]{20,}$/;
|
|
2
|
+
const SECRET_KEY = /^sk_/;
|
|
3
|
+
const LOADER_SCRIPT_SELECTOR = 'script[data-key][src*="vizbl.js"]';
|
|
4
|
+
function parseLoaderKeys(value) {
|
|
5
|
+
const tokens = (value ?? '').split(/\s+/).filter(Boolean);
|
|
6
|
+
return {
|
|
7
|
+
keys: [
|
|
8
|
+
...new Set(tokens.filter((token)=>PUBLISHABLE_KEY.test(token)))
|
|
9
|
+
],
|
|
10
|
+
hasSecretKey: tokens.some((token)=>SECRET_KEY.test(token))
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function readLoaderKeys(doc, currentScript) {
|
|
14
|
+
const script = currentScript?.hasAttribute('data-key') ? currentScript : doc.querySelector(LOADER_SCRIPT_SELECTOR);
|
|
15
|
+
return parseLoaderKeys(script?.getAttribute('data-key'));
|
|
16
|
+
}
|
|
17
|
+
function isTestKey(key) {
|
|
18
|
+
return key.startsWith('pk_test_');
|
|
19
|
+
}
|
|
20
|
+
const OPENS_FROM_OBJECT_ID = new Set([
|
|
21
|
+
'core-ar',
|
|
22
|
+
'rugs',
|
|
23
|
+
'flooring'
|
|
24
|
+
]);
|
|
25
|
+
const resolveLocally = async ({ identifier, keys, product })=>{
|
|
26
|
+
const [key] = keys;
|
|
27
|
+
if (!('objectId' in identifier) || !OPENS_FROM_OBJECT_ID.has(product) || 1 !== keys.length || !key) return {
|
|
28
|
+
show: false,
|
|
29
|
+
product,
|
|
30
|
+
reason: 'resolver_required'
|
|
31
|
+
};
|
|
32
|
+
const { objectId } = identifier;
|
|
33
|
+
if ('core-ar' === product) return {
|
|
34
|
+
show: true,
|
|
35
|
+
key,
|
|
36
|
+
widget: {
|
|
37
|
+
product,
|
|
38
|
+
objectId,
|
|
39
|
+
mode: 'ar-viewer'
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
if ('rugs' === product || 'flooring' === product) return {
|
|
43
|
+
show: true,
|
|
44
|
+
key,
|
|
45
|
+
widget: {
|
|
46
|
+
product,
|
|
47
|
+
objectId
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
show: false,
|
|
52
|
+
product,
|
|
53
|
+
reason: 'resolver_required'
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
const VIZBL_PRODUCTS = [
|
|
57
|
+
'core-ar',
|
|
58
|
+
'rugs',
|
|
59
|
+
'flooring',
|
|
60
|
+
'wallart',
|
|
61
|
+
'ai-try-on'
|
|
62
|
+
];
|
|
63
|
+
const BUTTON_HIDDEN_REASONS = [
|
|
64
|
+
'invalid_key',
|
|
65
|
+
'domain_not_allowed',
|
|
66
|
+
'product_not_enabled',
|
|
67
|
+
'product_not_linked',
|
|
68
|
+
'object_not_found',
|
|
69
|
+
'object_not_ready',
|
|
70
|
+
'product_data_incomplete',
|
|
71
|
+
'out_of_tokens',
|
|
72
|
+
'missing_key',
|
|
73
|
+
'secret_key',
|
|
74
|
+
'unsupported_product',
|
|
75
|
+
'missing_identifier',
|
|
76
|
+
'resolver_required',
|
|
77
|
+
'unavailable'
|
|
78
|
+
];
|
|
79
|
+
function isVizblProduct(value) {
|
|
80
|
+
return 'string' == typeof value && VIZBL_PRODUCTS.includes(value);
|
|
81
|
+
}
|
|
82
|
+
const API_ORIGIN = {
|
|
83
|
+
live: 'https://api.vizbl.com',
|
|
84
|
+
test: 'https://api-dev.vizbl.com'
|
|
85
|
+
};
|
|
86
|
+
const ENDPOINT_PATH = '/public/loader/buttons';
|
|
87
|
+
function buildEndpointUrl({ identifier, keys, product }) {
|
|
88
|
+
const params = new URLSearchParams();
|
|
89
|
+
for (const key of keys)params.append('key', key);
|
|
90
|
+
params.set('product', product);
|
|
91
|
+
if ('sku' in identifier) params.set('sku', identifier.sku);
|
|
92
|
+
else if ('productUrl' in identifier) params.set('product-url', identifier.productUrl);
|
|
93
|
+
else if ('productId' in identifier) params.set('product-id', identifier.productId);
|
|
94
|
+
else params.set('object-id', identifier.objectId);
|
|
95
|
+
const origin = keys.some(isTestKey) ? API_ORIGIN.test : API_ORIGIN.live;
|
|
96
|
+
return `${origin}${ENDPOINT_PATH}?${params.toString()}`;
|
|
97
|
+
}
|
|
98
|
+
function readDecision(body, product) {
|
|
99
|
+
const unavailable = {
|
|
100
|
+
show: false,
|
|
101
|
+
product,
|
|
102
|
+
reason: 'unavailable'
|
|
103
|
+
};
|
|
104
|
+
const buttons = body?.buttons;
|
|
105
|
+
const decision = Array.isArray(buttons) ? buttons[0] : void 0;
|
|
106
|
+
if (!decision || 'object' != typeof decision) return unavailable;
|
|
107
|
+
if (false === decision.show) return BUTTON_HIDDEN_REASONS.includes(decision.reason) ? {
|
|
108
|
+
show: false,
|
|
109
|
+
product,
|
|
110
|
+
reason: decision.reason
|
|
111
|
+
} : unavailable;
|
|
112
|
+
if (true === decision.show && 'string' == typeof decision.key && 'object' == typeof decision.widget && null !== decision.widget && isVizblProduct(decision.widget.product) && decision.widget.product === product) return decision;
|
|
113
|
+
return unavailable;
|
|
114
|
+
}
|
|
115
|
+
function createEndpointResolver(options = {}) {
|
|
116
|
+
const fallback = options.fallback ?? resolveLocally;
|
|
117
|
+
const asked = new Map();
|
|
118
|
+
const ask = async (request, url)=>{
|
|
119
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
120
|
+
const response = await doFetch(url, {
|
|
121
|
+
credentials: 'omit'
|
|
122
|
+
});
|
|
123
|
+
if (404 === response.status) return fallback(request);
|
|
124
|
+
if (!response.ok) return {
|
|
125
|
+
show: false,
|
|
126
|
+
product: request.product,
|
|
127
|
+
reason: 'unavailable'
|
|
128
|
+
};
|
|
129
|
+
return readDecision(await response.json(), request.product);
|
|
130
|
+
};
|
|
131
|
+
return (request)=>{
|
|
132
|
+
const url = buildEndpointUrl(request);
|
|
133
|
+
let answer = asked.get(url);
|
|
134
|
+
if (!answer) {
|
|
135
|
+
answer = ask(request, url);
|
|
136
|
+
asked.set(url, answer);
|
|
137
|
+
answer.catch(()=>asked.delete(url));
|
|
138
|
+
}
|
|
139
|
+
return answer;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
const WIDGET_CDN_ORIGIN = 'https://cdn.vizbl.com';
|
|
143
|
+
const WIDGET_SCRIPT_URLS = {
|
|
144
|
+
'core-ar': `${WIDGET_CDN_ORIGIN}/@neurodyn/core-ar/dist/core-ar.embed.min.js`,
|
|
145
|
+
'room-viewer': `${WIDGET_CDN_ORIGIN}/@neurodyn/room-viewer/dist/room-viewer.embed.min.js`,
|
|
146
|
+
'ai-try-on': `${WIDGET_CDN_ORIGIN}/@neurodyn/ai-try-on/dist/ai-try-on.embed.min.js`
|
|
147
|
+
};
|
|
148
|
+
const PRODUCT_WIDGET_SCRIPT = {
|
|
149
|
+
'core-ar': 'core-ar',
|
|
150
|
+
rugs: 'room-viewer',
|
|
151
|
+
flooring: 'room-viewer',
|
|
152
|
+
wallart: 'room-viewer',
|
|
153
|
+
'ai-try-on': 'ai-try-on'
|
|
154
|
+
};
|
|
155
|
+
const CORE_AR_TAG_NAME = 'vizbl-core';
|
|
156
|
+
const ROOM_VIEWER_TRIGGER = {
|
|
157
|
+
marker: 'data-neurodyn-room-viewer-dialog',
|
|
158
|
+
type: 'data-neurodyn-room-viewer-type',
|
|
159
|
+
tinuuid: 'data-neurodyn-room-viewer-tinuuid',
|
|
160
|
+
name: 'data-neurodyn-room-viewer-name',
|
|
161
|
+
roomId: 'data-neurodyn-room-viewer-room-id',
|
|
162
|
+
apiKey: 'data-neurodyn-room-viewer-api-key',
|
|
163
|
+
src: 'data-neurodyn-room-viewer-src',
|
|
164
|
+
width: 'data-neurodyn-room-viewer-width',
|
|
165
|
+
height: 'data-neurodyn-room-viewer-height',
|
|
166
|
+
dimensionUnit: 'data-neurodyn-room-viewer-dimension-unit'
|
|
167
|
+
};
|
|
168
|
+
const AI_TRY_ON_TRIGGER = {
|
|
169
|
+
marker: 'data-neurodyn-ai-try-on-dialog',
|
|
170
|
+
productType: 'data-neurodyn-ai-try-on-product-type',
|
|
171
|
+
productName: 'data-neurodyn-ai-try-on-product-name',
|
|
172
|
+
productImageSrc: 'data-neurodyn-ai-try-on-product-image-src',
|
|
173
|
+
productPrice: 'data-neurodyn-ai-try-on-product-price',
|
|
174
|
+
productUrl: 'data-neurodyn-ai-try-on-product-url',
|
|
175
|
+
apiKey: 'data-neurodyn-ai-try-on-api-key'
|
|
176
|
+
};
|
|
177
|
+
const loading = new Map();
|
|
178
|
+
function isWidgetPresent(widget, doc) {
|
|
179
|
+
const win = doc.defaultView;
|
|
180
|
+
if (!win) return false;
|
|
181
|
+
if ('core-ar' === widget) return Boolean(win.customElements?.get(CORE_AR_TAG_NAME));
|
|
182
|
+
if ('room-viewer' === widget) return Boolean(win.__neurodynRoomViewerDialog);
|
|
183
|
+
return Boolean(win.__neurodynAiTryOnDialog);
|
|
184
|
+
}
|
|
185
|
+
function loadWidgetScript(widget, doc = document) {
|
|
186
|
+
const src = WIDGET_SCRIPT_URLS[widget];
|
|
187
|
+
if (isWidgetPresent(widget, doc)) return Promise.resolve();
|
|
188
|
+
const pending = loading.get(src);
|
|
189
|
+
if (pending) return pending;
|
|
190
|
+
const promise = new Promise((resolve, reject)=>{
|
|
191
|
+
const script = doc.createElement("script");
|
|
192
|
+
script.async = true;
|
|
193
|
+
script.src = src;
|
|
194
|
+
script.addEventListener('load', ()=>resolve(), {
|
|
195
|
+
once: true
|
|
196
|
+
});
|
|
197
|
+
script.addEventListener('error', ()=>{
|
|
198
|
+
loading.delete(src);
|
|
199
|
+
script.remove();
|
|
200
|
+
reject(new Error(`Could not load ${src}`));
|
|
201
|
+
}, {
|
|
202
|
+
once: true
|
|
203
|
+
});
|
|
204
|
+
doc.head.appendChild(script);
|
|
205
|
+
});
|
|
206
|
+
loading.set(src, promise);
|
|
207
|
+
return promise;
|
|
208
|
+
}
|
|
209
|
+
const VIZBL_BUTTON_TAG_NAME = 'vizbl-button';
|
|
210
|
+
const OBSERVED_ATTRIBUTES = [
|
|
211
|
+
'product',
|
|
212
|
+
'sku',
|
|
213
|
+
'product-url',
|
|
214
|
+
'product-id',
|
|
215
|
+
'object-id',
|
|
216
|
+
'variant-id',
|
|
217
|
+
'mode',
|
|
218
|
+
'label'
|
|
219
|
+
];
|
|
220
|
+
const CORE_AR_MODES = [
|
|
221
|
+
'ar-viewer',
|
|
222
|
+
'ar-one-click',
|
|
223
|
+
'advanced-ar'
|
|
224
|
+
];
|
|
225
|
+
const TRIGGER_ATTRIBUTE = 'data-vizbl-button-trigger';
|
|
226
|
+
const STYLE_ELEMENT_ID = 'vizbl-button-styles';
|
|
227
|
+
const DEFAULT_LABELS = {
|
|
228
|
+
rugs: 'View in your room',
|
|
229
|
+
flooring: 'View in your room',
|
|
230
|
+
wallart: 'View on your wall',
|
|
231
|
+
'ai-try-on': 'Try it on'
|
|
232
|
+
};
|
|
233
|
+
const REASON_MESSAGES = {
|
|
234
|
+
invalid_key: 'the key in data-key is not a valid Vizbl publishable key',
|
|
235
|
+
domain_not_allowed: 'this domain is not in the key\'s list of allowed domains',
|
|
236
|
+
product_not_enabled: 'the key does not give access to this product',
|
|
237
|
+
product_not_linked: 'this store product is not linked to a Vizbl object',
|
|
238
|
+
object_not_found: 'the object was not found',
|
|
239
|
+
object_not_ready: 'the object is not ready yet',
|
|
240
|
+
product_data_incomplete: 'the product is missing data the widget needs',
|
|
241
|
+
out_of_tokens: 'the account is out of tokens',
|
|
242
|
+
missing_key: "the vizbl.js script tag has no data-key",
|
|
243
|
+
secret_key: 'data-key holds a secret key (sk_…); use the publishable key (pk_…) and revoke the secret one',
|
|
244
|
+
unsupported_product: 'the product attribute is missing or not one of: core-ar, rugs, flooring, wallart, ai-try-on',
|
|
245
|
+
missing_identifier: 'exactly one of sku, product-url, product-id or object-id is required',
|
|
246
|
+
resolver_required: 'this combination is not available yet: for now use object-id with product core-ar, rugs or flooring and a single key',
|
|
247
|
+
unavailable: 'the widget could not be loaded'
|
|
248
|
+
};
|
|
249
|
+
const CORE_AR_ERROR_REASONS = {
|
|
250
|
+
DOMAIN_NOT_ALLOWED: 'domain_not_allowed',
|
|
251
|
+
INVALID_KEY: 'invalid_key',
|
|
252
|
+
KEY_EXPIRED: 'invalid_key',
|
|
253
|
+
KEY_REVOKED: 'invalid_key',
|
|
254
|
+
MISSING_API_KEY: 'missing_key',
|
|
255
|
+
MODEL_UNAVAILABLE: 'object_not_ready',
|
|
256
|
+
OBJECT_NOT_FOUND: 'object_not_found',
|
|
257
|
+
PAYMENT_REQUIRED: 'out_of_tokens',
|
|
258
|
+
POLICY_DENIED: 'product_not_enabled',
|
|
259
|
+
VARIANT_NOT_FOUND: 'object_not_found'
|
|
260
|
+
};
|
|
261
|
+
let runtime;
|
|
262
|
+
function installStyles(doc) {
|
|
263
|
+
if (doc.getElementById(STYLE_ELEMENT_ID)) return;
|
|
264
|
+
const style = doc.createElement('style');
|
|
265
|
+
style.id = STYLE_ELEMENT_ID;
|
|
266
|
+
style.textContent = `
|
|
267
|
+
:where(${VIZBL_BUTTON_TAG_NAME}) { display: inline-block; }
|
|
268
|
+
:where(${VIZBL_BUTTON_TAG_NAME}:not([data-state="ready"])) { display: none; }
|
|
269
|
+
:where(${VIZBL_BUTTON_TAG_NAME}:not([unstyled]) [${TRIGGER_ATTRIBUTE}]) {
|
|
270
|
+
align-items: center;
|
|
271
|
+
background: var(--vizbl-button-background, #3556f0);
|
|
272
|
+
border: 1px solid var(--vizbl-button-border-color, #3556f0);
|
|
273
|
+
border-radius: var(--vizbl-button-border-radius, 0.5rem);
|
|
274
|
+
box-sizing: border-box;
|
|
275
|
+
color: var(--vizbl-button-color, #ffffff);
|
|
276
|
+
cursor: pointer;
|
|
277
|
+
display: inline-flex;
|
|
278
|
+
font-family: var(--vizbl-button-font-family, inherit);
|
|
279
|
+
font-size: var(--vizbl-button-font-size, 0.875rem);
|
|
280
|
+
font-weight: var(--vizbl-button-font-weight, 500);
|
|
281
|
+
justify-content: center;
|
|
282
|
+
line-height: 1.25;
|
|
283
|
+
padding: var(--vizbl-button-padding, 0.375rem 0.75rem);
|
|
284
|
+
}
|
|
285
|
+
`;
|
|
286
|
+
doc.head.appendChild(style);
|
|
287
|
+
}
|
|
288
|
+
function readIdentifier(element) {
|
|
289
|
+
const sku = element.getAttribute('sku')?.trim();
|
|
290
|
+
const productUrl = element.getAttribute('product-url')?.trim();
|
|
291
|
+
const productId = element.getAttribute('product-id')?.trim();
|
|
292
|
+
const objectId = element.getAttribute('object-id')?.trim();
|
|
293
|
+
if (1 !== [
|
|
294
|
+
sku,
|
|
295
|
+
productUrl,
|
|
296
|
+
productId,
|
|
297
|
+
objectId
|
|
298
|
+
].filter(Boolean).length) return;
|
|
299
|
+
if (sku) return {
|
|
300
|
+
sku
|
|
301
|
+
};
|
|
302
|
+
if (productUrl) return {
|
|
303
|
+
productUrl
|
|
304
|
+
};
|
|
305
|
+
if (productId) return {
|
|
306
|
+
productId
|
|
307
|
+
};
|
|
308
|
+
return {
|
|
309
|
+
objectId: objectId
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
class VizblButtonElement extends HTMLElement {
|
|
313
|
+
static observedAttributes = OBSERVED_ATTRIBUTES;
|
|
314
|
+
customLabel = null;
|
|
315
|
+
queued = false;
|
|
316
|
+
generation = 0;
|
|
317
|
+
connectedCallback() {
|
|
318
|
+
this.queueRender();
|
|
319
|
+
}
|
|
320
|
+
disconnectedCallback() {
|
|
321
|
+
this.generation++;
|
|
322
|
+
}
|
|
323
|
+
attributeChangedCallback(_name, previous, next) {
|
|
324
|
+
if (previous !== next && this.isConnected) this.queueRender();
|
|
325
|
+
}
|
|
326
|
+
queueRender() {
|
|
327
|
+
if (this.queued) return;
|
|
328
|
+
this.queued = true;
|
|
329
|
+
const run = ()=>{
|
|
330
|
+
this.queued = false;
|
|
331
|
+
if (this.isConnected) this.render();
|
|
332
|
+
};
|
|
333
|
+
if ('loading' === this.ownerDocument.readyState) this.ownerDocument.addEventListener('DOMContentLoaded', run, {
|
|
334
|
+
once: true
|
|
335
|
+
});
|
|
336
|
+
else queueMicrotask(run);
|
|
337
|
+
}
|
|
338
|
+
async render() {
|
|
339
|
+
const generation = ++this.generation;
|
|
340
|
+
const doc = this.ownerDocument;
|
|
341
|
+
if (null === this.customLabel) {
|
|
342
|
+
const nodes = Array.from(this.childNodes);
|
|
343
|
+
this.customLabel = nodes.some((node)=>node.nodeType !== Node.TEXT_NODE || Boolean(node.textContent?.trim())) ? nodes : [];
|
|
344
|
+
}
|
|
345
|
+
this.replaceChildren();
|
|
346
|
+
this.removeAttribute('data-state');
|
|
347
|
+
this.removeAttribute('data-reason');
|
|
348
|
+
const product = this.getAttribute('product')?.trim();
|
|
349
|
+
if (!isVizblProduct(product)) return this.hide('unsupported_product');
|
|
350
|
+
const identifier = readIdentifier(this);
|
|
351
|
+
if (!identifier) return this.hide('missing_identifier');
|
|
352
|
+
const { keys, resolve } = runtime ?? {
|
|
353
|
+
keys: readLoaderKeys(doc),
|
|
354
|
+
resolve: resolveLocally
|
|
355
|
+
};
|
|
356
|
+
if (keys.hasSecretKey) return this.hide('secret_key');
|
|
357
|
+
if (0 === keys.keys.length) return this.hide(doc.querySelector("script[data-key]")?.getAttribute('data-key')?.trim() ? 'invalid_key' : 'missing_key');
|
|
358
|
+
let decision;
|
|
359
|
+
try {
|
|
360
|
+
decision = await resolve({
|
|
361
|
+
identifier,
|
|
362
|
+
keys: keys.keys,
|
|
363
|
+
product
|
|
364
|
+
});
|
|
365
|
+
} catch {
|
|
366
|
+
decision = {
|
|
367
|
+
show: false,
|
|
368
|
+
reason: 'unavailable'
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
if (generation !== this.generation) return;
|
|
372
|
+
if (!decision.show) return this.hide(decision.reason);
|
|
373
|
+
installStyles(doc);
|
|
374
|
+
this.draw(decision);
|
|
375
|
+
this.setAttribute('data-state', 'ready');
|
|
376
|
+
try {
|
|
377
|
+
await loadWidgetScript(PRODUCT_WIDGET_SCRIPT[decision.widget.product], doc);
|
|
378
|
+
} catch {
|
|
379
|
+
if (generation === this.generation) this.hide('unavailable');
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
draw({ key, label, widget }) {
|
|
383
|
+
const doc = this.ownerDocument;
|
|
384
|
+
const text = this.getAttribute('label')?.trim() || label;
|
|
385
|
+
if ('core-ar' === widget.product) {
|
|
386
|
+
const requestedMode = this.getAttribute('mode')?.trim();
|
|
387
|
+
const core = doc.createElement(CORE_AR_TAG_NAME);
|
|
388
|
+
core.setAttribute('api-key', key);
|
|
389
|
+
core.setAttribute('object-id', widget.objectId);
|
|
390
|
+
core.setAttribute('type', requestedMode && CORE_AR_MODES.includes(requestedMode) ? requestedMode : widget.mode);
|
|
391
|
+
const variantId = this.getAttribute('variant-id')?.trim() || widget.variantId;
|
|
392
|
+
if (variantId) core.setAttribute('variant-id', variantId);
|
|
393
|
+
if (isTestKey(key)) core.setAttribute('dev', '');
|
|
394
|
+
if (this.hasAttribute('unstyled')) core.setAttribute('unstyled', '');
|
|
395
|
+
if (this.customLabel?.length) core.append(...this.customLabel);
|
|
396
|
+
else if (text) core.textContent = text;
|
|
397
|
+
core.addEventListener('core-ar-error', (event)=>{
|
|
398
|
+
const code = event.detail?.code ?? '';
|
|
399
|
+
this.hide(CORE_AR_ERROR_REASONS[code] ?? 'unavailable');
|
|
400
|
+
});
|
|
401
|
+
this.append(core);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
const trigger = doc.createElement('button');
|
|
405
|
+
trigger.type = 'button';
|
|
406
|
+
trigger.setAttribute(TRIGGER_ATTRIBUTE, '');
|
|
407
|
+
if (this.customLabel?.length) trigger.append(...this.customLabel);
|
|
408
|
+
else trigger.textContent = text || DEFAULT_LABELS[widget.product];
|
|
409
|
+
if ('rugs' === widget.product || 'flooring' === widget.product) {
|
|
410
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.marker, '');
|
|
411
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.type, widget.product);
|
|
412
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.tinuuid, widget.objectId);
|
|
413
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.apiKey, key);
|
|
414
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.name, widget.name ?? '');
|
|
415
|
+
if (widget.roomId) trigger.setAttribute(ROOM_VIEWER_TRIGGER.roomId, widget.roomId);
|
|
416
|
+
this.append(trigger);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if ('wallart' === widget.product) {
|
|
420
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.marker, '');
|
|
421
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.type, 'wallart');
|
|
422
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.src, widget.imageUrl);
|
|
423
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.name, widget.name);
|
|
424
|
+
trigger.setAttribute(ROOM_VIEWER_TRIGGER.apiKey, key);
|
|
425
|
+
if (widget.width) trigger.setAttribute(ROOM_VIEWER_TRIGGER.width, String(widget.width));
|
|
426
|
+
if (widget.height) trigger.setAttribute(ROOM_VIEWER_TRIGGER.height, String(widget.height));
|
|
427
|
+
if (widget.dimensionUnit) trigger.setAttribute(ROOM_VIEWER_TRIGGER.dimensionUnit, widget.dimensionUnit);
|
|
428
|
+
this.append(trigger);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if ('ai-try-on' !== widget.product) return this.hide('unavailable');
|
|
432
|
+
trigger.setAttribute(AI_TRY_ON_TRIGGER.marker, '');
|
|
433
|
+
trigger.setAttribute(AI_TRY_ON_TRIGGER.productType, widget.zone);
|
|
434
|
+
trigger.setAttribute(AI_TRY_ON_TRIGGER.productName, widget.name);
|
|
435
|
+
trigger.setAttribute(AI_TRY_ON_TRIGGER.productImageSrc, widget.imageUrl);
|
|
436
|
+
trigger.setAttribute(AI_TRY_ON_TRIGGER.apiKey, key);
|
|
437
|
+
if (widget.price) trigger.setAttribute(AI_TRY_ON_TRIGGER.productPrice, widget.price);
|
|
438
|
+
if (widget.productUrl) trigger.setAttribute(AI_TRY_ON_TRIGGER.productUrl, widget.productUrl);
|
|
439
|
+
this.append(trigger);
|
|
440
|
+
}
|
|
441
|
+
hide(reason) {
|
|
442
|
+
this.replaceChildren();
|
|
443
|
+
this.setAttribute('data-state', 'hidden');
|
|
444
|
+
this.setAttribute('data-reason', reason);
|
|
445
|
+
console.warn(`[vizbl] <${VIZBL_BUTTON_TAG_NAME}> is not shown (${reason}): ${REASON_MESSAGES[reason]}.`, this);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function registerVizblButton(options = {}, doc = document) {
|
|
449
|
+
const registry = doc.defaultView?.customElements;
|
|
450
|
+
if (!registry) return;
|
|
451
|
+
runtime = {
|
|
452
|
+
keys: readLoaderKeys(doc, options.currentScript),
|
|
453
|
+
resolve: options.resolve ?? resolveLocally
|
|
454
|
+
};
|
|
455
|
+
if (!registry.get(VIZBL_BUTTON_TAG_NAME)) registry.define(VIZBL_BUTTON_TAG_NAME, VizblButtonElement);
|
|
456
|
+
}
|
|
457
|
+
export { BUTTON_HIDDEN_REASONS, VIZBL_BUTTON_TAG_NAME, VIZBL_PRODUCTS, VizblButtonElement, WIDGET_SCRIPT_URLS, buildEndpointUrl, createEndpointResolver, isVizblProduct, parseLoaderKeys, readLoaderKeys, registerVizblButton, resolveLocally };
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const LOADER_SCRIPT_SELECTOR = "script[data-key][src*=\"vizbl.js\"]";
|
|
2
|
+
export interface LoaderKeys {
|
|
3
|
+
/** Well-formed publishable keys, in the order written. */
|
|
4
|
+
keys: string[];
|
|
5
|
+
/** A secret key was found on the page; it is never used or sent anywhere. */
|
|
6
|
+
hasSecretKey: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function parseLoaderKeys(value: string | null | undefined): LoaderKeys;
|
|
9
|
+
/**
|
|
10
|
+
* Reads the keys from the loader's script tag. `currentScript` is only known
|
|
11
|
+
* while the script runs, so the caller captures it then; the selector covers a
|
|
12
|
+
* tag found later.
|
|
13
|
+
*/
|
|
14
|
+
export declare function readLoaderKeys(doc: Document, currentScript?: Element | null): LoaderKeys;
|
|
15
|
+
/** Keys for the dev and stage API are `pk_test_…`. */
|
|
16
|
+
export declare function isTestKey(key: string): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ButtonResolver } from './resolve-contract';
|
|
2
|
+
/**
|
|
3
|
+
* Answers without the endpoint, for what can be answered in the browser: the
|
|
4
|
+
* manual tag with `object-id`, one key on the page. Everything else needs the
|
|
5
|
+
* endpoint (`resolve-contract.ts`): `sku` and `product-url` need the product
|
|
6
|
+
* links, and with several keys only the backend knows which one belongs to
|
|
7
|
+
* which product. Nothing is checked here, so a wrong key, a foreign domain or
|
|
8
|
+
* an unfinished object surface later, from the widget itself.
|
|
9
|
+
*/
|
|
10
|
+
export declare const resolveLocally: ButtonResolver;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/** The Vizbl products a button can open, named as the cabinets and keys name them. */
|
|
2
|
+
export declare const VIZBL_PRODUCTS: readonly ["core-ar", "rugs", "flooring", "wallart", "ai-try-on"];
|
|
3
|
+
export type VizblProduct = (typeof VIZBL_PRODUCTS)[number];
|
|
4
|
+
/**
|
|
5
|
+
* The store product the button stands for: exactly one of the four. `productId`
|
|
6
|
+
* is the platform's own id of the variant or the product — on Shopify a sku is
|
|
7
|
+
* often empty, the variant id never is.
|
|
8
|
+
*/
|
|
9
|
+
export type StoreProductIdentifier = {
|
|
10
|
+
sku: string;
|
|
11
|
+
} | {
|
|
12
|
+
productUrl: string;
|
|
13
|
+
} | {
|
|
14
|
+
productId: string;
|
|
15
|
+
} | {
|
|
16
|
+
objectId: string;
|
|
17
|
+
};
|
|
18
|
+
export interface LoaderResolveRequest {
|
|
19
|
+
/** Every publishable key from the loader's `data-key`, in page order. */
|
|
20
|
+
keys: string[];
|
|
21
|
+
/** Left out in automatic mode: the answer then lists every linked product. */
|
|
22
|
+
product?: VizblProduct;
|
|
23
|
+
identifier: StoreProductIdentifier;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Why a button is not shown. The first group comes from the endpoint; the
|
|
27
|
+
* second is decided in the browser before any request is made.
|
|
28
|
+
*/
|
|
29
|
+
export declare const BUTTON_HIDDEN_REASONS: readonly ["invalid_key", "domain_not_allowed", "product_not_enabled", "product_not_linked", "object_not_found", "object_not_ready", "product_data_incomplete", "out_of_tokens", "missing_key", "secret_key", "unsupported_product", "missing_identifier", "resolver_required", "unavailable"];
|
|
30
|
+
export type ButtonHiddenReason = (typeof BUTTON_HIDDEN_REASONS)[number];
|
|
31
|
+
export type CoreArMode = 'ar-viewer' | 'ar-one-click' | 'advanced-ar';
|
|
32
|
+
export type TryOnZone = 'full-body' | 'upper-body' | 'lower-body' | 'footwear' | 'headwear' | 'eyewear' | 'neck' | 'wrist' | 'bag' | 'waist';
|
|
33
|
+
/** What each widget needs to open; everything comes from the cabinet. */
|
|
34
|
+
export type ButtonWidgetData = {
|
|
35
|
+
product: 'core-ar';
|
|
36
|
+
objectId: string;
|
|
37
|
+
variantId?: string;
|
|
38
|
+
mode: CoreArMode;
|
|
39
|
+
} | {
|
|
40
|
+
product: 'rugs' | 'flooring';
|
|
41
|
+
objectId: string;
|
|
42
|
+
name?: string;
|
|
43
|
+
roomId?: string;
|
|
44
|
+
} | {
|
|
45
|
+
product: 'wallart';
|
|
46
|
+
imageUrl: string;
|
|
47
|
+
name: string;
|
|
48
|
+
width?: number;
|
|
49
|
+
height?: number;
|
|
50
|
+
dimensionUnit?: 'm' | 'cm' | 'in';
|
|
51
|
+
displayUnit?: 'cm' | 'in';
|
|
52
|
+
} | {
|
|
53
|
+
product: 'ai-try-on';
|
|
54
|
+
zone: TryOnZone;
|
|
55
|
+
name: string;
|
|
56
|
+
imageUrl: string;
|
|
57
|
+
/** Already formatted for display. */
|
|
58
|
+
price?: string;
|
|
59
|
+
productUrl?: string;
|
|
60
|
+
};
|
|
61
|
+
export interface ShownButton {
|
|
62
|
+
show: true;
|
|
63
|
+
/** The key, out of the ones sent, that the widget must use for this product. */
|
|
64
|
+
key: string;
|
|
65
|
+
widget: ButtonWidgetData;
|
|
66
|
+
/** Button text set in the cabinet; the loader has a default per product. */
|
|
67
|
+
label?: string;
|
|
68
|
+
}
|
|
69
|
+
export interface HiddenButton {
|
|
70
|
+
show: false;
|
|
71
|
+
/** Absent only in automatic mode, when nothing is linked at all. */
|
|
72
|
+
product?: VizblProduct;
|
|
73
|
+
reason: ButtonHiddenReason;
|
|
74
|
+
}
|
|
75
|
+
export type ButtonDecision = ShownButton | HiddenButton;
|
|
76
|
+
/** One entry per product: a single one when `product` was sent. */
|
|
77
|
+
export interface LoaderResolveResponse {
|
|
78
|
+
buttons: ButtonDecision[];
|
|
79
|
+
}
|
|
80
|
+
/** Answers "which button, with what data" for one tag. */
|
|
81
|
+
export type ButtonResolver = (request: LoaderResolveRequest & {
|
|
82
|
+
product: VizblProduct;
|
|
83
|
+
}) => Promise<ButtonDecision>;
|
|
84
|
+
export declare function isVizblProduct(value: unknown): value is VizblProduct;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ButtonResolver } from './resolve-contract';
|
|
2
|
+
export declare const VIZBL_BUTTON_TAG_NAME = "vizbl-button";
|
|
3
|
+
export declare class VizblButtonElement extends HTMLElement {
|
|
4
|
+
static readonly observedAttributes: readonly ["product", "sku", "product-url", "product-id", "object-id", "variant-id", "mode", "label"];
|
|
5
|
+
/** What the merchant wrote inside the tag: the button's own text. */
|
|
6
|
+
private customLabel;
|
|
7
|
+
private queued;
|
|
8
|
+
/** Grows with every render, so an answer to an earlier one is dropped. */
|
|
9
|
+
private generation;
|
|
10
|
+
connectedCallback(): void;
|
|
11
|
+
disconnectedCallback(): void;
|
|
12
|
+
attributeChangedCallback(_name: string, previous: string | null, next: string | null): void;
|
|
13
|
+
private queueRender;
|
|
14
|
+
private render;
|
|
15
|
+
private draw;
|
|
16
|
+
/** Draws nothing and says why, for whoever is installing the button. */
|
|
17
|
+
private hide;
|
|
18
|
+
}
|
|
19
|
+
export interface RegisterVizblButtonOptions {
|
|
20
|
+
/** The loader's own script tag, captured while it runs. */
|
|
21
|
+
currentScript?: Element | null;
|
|
22
|
+
/** Replaces the built-in answer; the endpoint client goes here. */
|
|
23
|
+
resolve?: ButtonResolver;
|
|
24
|
+
}
|
|
25
|
+
/** Registers `<vizbl-button>`. Safe to call twice: the first call wins. */
|
|
26
|
+
export declare function registerVizblButton(options?: RegisterVizblButtonOptions, doc?: Document): void;
|
package/dist/vizbl.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
(()=>{"use strict";let t,e=/^pk_(?:live|test)_[\w-]{20,}$/,r=/^sk_/;function i(t,i){let o,n=i?.hasAttribute("data-key")?i:t.querySelector('script[data-key][src*="vizbl.js"]');return{keys:[...new Set((o=(n?.getAttribute("data-key")??"").split(/\s+/).filter(Boolean)).filter(t=>e.test(t)))],hasSecretKey:o.some(t=>r.test(t))}}function o(t){return t.startsWith("pk_test_")}let n=new Set(["core-ar","rugs","flooring"]),a=async({identifier:t,keys:e,product:r})=>{let[i]=e;if(!("objectId"in t)||!n.has(r)||1!==e.length||!i)return{show:!1,product:r,reason:"resolver_required"};let{objectId:o}=t;return"core-ar"===r?{show:!0,key:i,widget:{product:r,objectId:o,mode:"ar-viewer"}}:"rugs"===r||"flooring"===r?{show:!0,key:i,widget:{product:r,objectId:o}}:{show:!1,product:r,reason:"resolver_required"}},d=["core-ar","rugs","flooring","wallart","ai-try-on"],s=["invalid_key","domain_not_allowed","product_not_enabled","product_not_linked","object_not_found","object_not_ready","product_data_incomplete","out_of_tokens","missing_key","secret_key","unsupported_product","missing_identifier","resolver_required","unavailable"];function u(t){return"string"==typeof t&&d.includes(t)}let l="https://cdn.vizbl.com",c={"core-ar":`${l}/@neurodyn/core-ar/dist/core-ar.embed.min.js`,"room-viewer":`${l}/@neurodyn/room-viewer/dist/room-viewer.embed.min.js`,"ai-try-on":`${l}/@neurodyn/ai-try-on/dist/ai-try-on.embed.min.js`},b={"core-ar":"core-ar",rugs:"room-viewer",flooring:"room-viewer",wallart:"room-viewer","ai-try-on":"ai-try-on"},h="vizbl-core",y="data-neurodyn-room-viewer-dialog",p="data-neurodyn-room-viewer-type",m="data-neurodyn-room-viewer-name",g="data-neurodyn-room-viewer-api-key",v=new Map,_="vizbl-button",w=["product","sku","product-url","product-id","object-id","variant-id","mode","label"],f=["ar-viewer","ar-one-click","advanced-ar"],k="data-vizbl-button-trigger",A="vizbl-button-styles",E={rugs:"View in your room",flooring:"View in your room",wallart:"View on your wall","ai-try-on":"Try it on"},j={invalid_key:"the key in data-key is not a valid Vizbl publishable key",domain_not_allowed:"this domain is not in the key's list of allowed domains",product_not_enabled:"the key does not give access to this product",product_not_linked:"this store product is not linked to a Vizbl object",object_not_found:"the object was not found",object_not_ready:"the object is not ready yet",product_data_incomplete:"the product is missing data the widget needs",out_of_tokens:"the account is out of tokens",missing_key:"the vizbl.js script tag has no data-key",secret_key:"data-key holds a secret key (sk_…); use the publishable key (pk_…) and revoke the secret one",unsupported_product:"the product attribute is missing or not one of: core-ar, rugs, flooring, wallart, ai-try-on",missing_identifier:"exactly one of sku, product-url, product-id or object-id is required",resolver_required:"this combination is not available yet: for now use object-id with product core-ar, rugs or flooring and a single key",unavailable:"the widget could not be loaded"},z={DOMAIN_NOT_ALLOWED:"domain_not_allowed",INVALID_KEY:"invalid_key",KEY_EXPIRED:"invalid_key",KEY_REVOKED:"invalid_key",MISSING_API_KEY:"missing_key",MODEL_UNAVAILABLE:"object_not_ready",OBJECT_NOT_FOUND:"object_not_found",PAYMENT_REQUIRED:"out_of_tokens",POLICY_DENIED:"product_not_enabled",VARIANT_NOT_FOUND:"object_not_found"};class I extends HTMLElement{static observedAttributes=w;customLabel=null;queued=!1;generation=0;connectedCallback(){this.queueRender()}disconnectedCallback(){this.generation++}attributeChangedCallback(t,e,r){e!==r&&this.isConnected&&this.queueRender()}queueRender(){if(this.queued)return;this.queued=!0;let t=()=>{this.queued=!1,this.isConnected&&this.render()};"loading"===this.ownerDocument.readyState?this.ownerDocument.addEventListener("DOMContentLoaded",t,{once:!0}):queueMicrotask(t)}async render(){let e,r=++this.generation,o=this.ownerDocument;if(null===this.customLabel){let t=Array.from(this.childNodes);this.customLabel=t.some(t=>t.nodeType!==Node.TEXT_NODE||!!t.textContent?.trim())?t:[]}this.replaceChildren(),this.removeAttribute("data-state"),this.removeAttribute("data-reason");let n=this.getAttribute("product")?.trim();if(!u(n))return this.hide("unsupported_product");let d=function(t){let e=t.getAttribute("sku")?.trim(),r=t.getAttribute("product-url")?.trim(),i=t.getAttribute("product-id")?.trim(),o=t.getAttribute("object-id")?.trim();if(1===[e,r,i,o].filter(Boolean).length)return e?{sku:e}:r?{productUrl:r}:i?{productId:i}:{objectId:o}}(this);if(!d)return this.hide("missing_identifier");let{keys:s,resolve:l}=t??{keys:i(o),resolve:a};if(s.hasSecretKey)return this.hide("secret_key");if(0===s.keys.length)return this.hide(o.querySelector("script[data-key]")?.getAttribute("data-key")?.trim()?"invalid_key":"missing_key");try{e=await l({identifier:d,keys:s.keys,product:n})}catch{e={show:!1,reason:"unavailable"}}if(r===this.generation){if(!e.show)return this.hide(e.reason);!function(t){if(t.getElementById(A))return;let e=t.createElement("style");e.id=A,e.textContent=`
|
|
2
|
+
:where(${_}) { display: inline-block; }
|
|
3
|
+
:where(${_}:not([data-state="ready"])) { display: none; }
|
|
4
|
+
:where(${_}:not([unstyled]) [${k}]) {
|
|
5
|
+
align-items: center;
|
|
6
|
+
background: var(--vizbl-button-background, #3556f0);
|
|
7
|
+
border: 1px solid var(--vizbl-button-border-color, #3556f0);
|
|
8
|
+
border-radius: var(--vizbl-button-border-radius, 0.5rem);
|
|
9
|
+
box-sizing: border-box;
|
|
10
|
+
color: var(--vizbl-button-color, #ffffff);
|
|
11
|
+
cursor: pointer;
|
|
12
|
+
display: inline-flex;
|
|
13
|
+
font-family: var(--vizbl-button-font-family, inherit);
|
|
14
|
+
font-size: var(--vizbl-button-font-size, 0.875rem);
|
|
15
|
+
font-weight: var(--vizbl-button-font-weight, 500);
|
|
16
|
+
justify-content: center;
|
|
17
|
+
line-height: 1.25;
|
|
18
|
+
padding: var(--vizbl-button-padding, 0.375rem 0.75rem);
|
|
19
|
+
}
|
|
20
|
+
`,t.head.appendChild(e)}(o),this.draw(e),this.setAttribute("data-state","ready");try{await function(t,e=document){let r,i=c[t];if((r=e.defaultView)&&("core-ar"===t?!!r.customElements?.get(h):"room-viewer"===t?!!r.__neurodynRoomViewerDialog:!!r.__neurodynAiTryOnDialog))return Promise.resolve();let o=v.get(i);if(o)return o;let n=new Promise((t,r)=>{let o=e.createElement("script");o.async=!0,o.src=i,o.addEventListener("load",()=>t(),{once:!0}),o.addEventListener("error",()=>{v.delete(i),o.remove(),r(Error(`Could not load ${i}`))},{once:!0}),e.head.appendChild(o)});return v.set(i,n),n}(b[e.widget.product],o)}catch{r===this.generation&&this.hide("unavailable")}}}draw({key:t,label:e,widget:r}){let i=this.ownerDocument,n=this.getAttribute("label")?.trim()||e;if("core-ar"===r.product){let e=this.getAttribute("mode")?.trim(),a=i.createElement(h);a.setAttribute("api-key",t),a.setAttribute("object-id",r.objectId),a.setAttribute("type",e&&f.includes(e)?e:r.mode);let d=this.getAttribute("variant-id")?.trim()||r.variantId;d&&a.setAttribute("variant-id",d),o(t)&&a.setAttribute("dev",""),this.hasAttribute("unstyled")&&a.setAttribute("unstyled",""),this.customLabel?.length?a.append(...this.customLabel):n&&(a.textContent=n),a.addEventListener("core-ar-error",t=>{let e=t.detail?.code??"";this.hide(z[e]??"unavailable")}),this.append(a);return}let a=i.createElement("button");if(a.type="button",a.setAttribute(k,""),this.customLabel?.length?a.append(...this.customLabel):a.textContent=n||E[r.product],"rugs"===r.product||"flooring"===r.product){a.setAttribute(y,""),a.setAttribute(p,r.product),a.setAttribute("data-neurodyn-room-viewer-tinuuid",r.objectId),a.setAttribute(g,t),a.setAttribute(m,r.name??""),r.roomId&&a.setAttribute("data-neurodyn-room-viewer-room-id",r.roomId),this.append(a);return}if("wallart"===r.product){a.setAttribute(y,""),a.setAttribute(p,"wallart"),a.setAttribute("data-neurodyn-room-viewer-src",r.imageUrl),a.setAttribute(m,r.name),a.setAttribute(g,t),r.width&&a.setAttribute("data-neurodyn-room-viewer-width",String(r.width)),r.height&&a.setAttribute("data-neurodyn-room-viewer-height",String(r.height)),r.dimensionUnit&&a.setAttribute("data-neurodyn-room-viewer-dimension-unit",r.dimensionUnit),this.append(a);return}if("ai-try-on"!==r.product)return this.hide("unavailable");a.setAttribute("data-neurodyn-ai-try-on-dialog",""),a.setAttribute("data-neurodyn-ai-try-on-product-type",r.zone),a.setAttribute("data-neurodyn-ai-try-on-product-name",r.name),a.setAttribute("data-neurodyn-ai-try-on-product-image-src",r.imageUrl),a.setAttribute("data-neurodyn-ai-try-on-api-key",t),r.price&&a.setAttribute("data-neurodyn-ai-try-on-product-price",r.price),r.productUrl&&a.setAttribute("data-neurodyn-ai-try-on-product-url",r.productUrl),this.append(a)}hide(t){this.replaceChildren(),this.setAttribute("data-state","hidden"),this.setAttribute("data-reason",t),console.warn(`[vizbl] <${_}> is not shown (${t}): ${j[t]}.`,this)}}!function(e={},r=document){let o=r.defaultView?.customElements;o&&(t={keys:i(r,e.currentScript),resolve:e.resolve??a},o.get(_)||o.define(_,I))}({currentScript:document.currentScript,resolve:function(t={}){let e=t.fallback??a,r=new Map,i=async(r,i)=>{var o,n;let a,d,l,c=t.fetch??globalThis.fetch,b=await c(i,{credentials:"omit"});return 404===b.status?e(r):b.ok?(o=await b.json(),a={show:!1,product:n=r.product,reason:"unavailable"},(l=Array.isArray(d=o?.buttons)?d[0]:void 0)&&"object"==typeof l?!1===l.show?s.includes(l.reason)?{show:!1,product:n,reason:l.reason}:a:!0===l.show&&"string"==typeof l.key&&"object"==typeof l.widget&&null!==l.widget&&u(l.widget.product)&&l.widget.product===n?l:a:a):{show:!1,product:r.product,reason:"unavailable"}};return t=>{let e=function({identifier:t,keys:e,product:r}){let i=new URLSearchParams;for(let t of e)i.append("key",t);i.set("product",r),"sku"in t?i.set("sku",t.sku):"productUrl"in t?i.set("product-url",t.productUrl):"productId"in t?i.set("product-id",t.productId):i.set("object-id",t.objectId);let n=e.some(o)?"https://api-dev.vizbl.com":"https://api.vizbl.com";return`${n}/public/loader/buttons?${i.toString()}`}(t),n=r.get(e);return n||(n=i(t,e),r.set(e,n),n.catch(()=>r.delete(e))),n}}()})})();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { VizblProduct } from './resolve-contract';
|
|
2
|
+
export type WidgetScript = 'core-ar' | 'room-viewer' | 'ai-try-on';
|
|
3
|
+
export declare const WIDGET_SCRIPT_URLS: Record<WidgetScript, string>;
|
|
4
|
+
export declare const PRODUCT_WIDGET_SCRIPT: Record<VizblProduct, WidgetScript>;
|
|
5
|
+
/** The Core AR widget's own element, which the button wraps. */
|
|
6
|
+
export declare const CORE_AR_TAG_NAME = "vizbl-core";
|
|
7
|
+
/**
|
|
8
|
+
* The trigger attributes of the Room Viewer embed build, as published in
|
|
9
|
+
* `@neurodyn/room-viewer` (`src/contract.ts`). A frozen public contract: sites
|
|
10
|
+
* already carry it, so the names cannot drift.
|
|
11
|
+
*/
|
|
12
|
+
export declare const ROOM_VIEWER_TRIGGER: {
|
|
13
|
+
readonly marker: "data-neurodyn-room-viewer-dialog";
|
|
14
|
+
readonly type: "data-neurodyn-room-viewer-type";
|
|
15
|
+
readonly tinuuid: "data-neurodyn-room-viewer-tinuuid";
|
|
16
|
+
readonly name: "data-neurodyn-room-viewer-name";
|
|
17
|
+
readonly roomId: "data-neurodyn-room-viewer-room-id";
|
|
18
|
+
readonly apiKey: "data-neurodyn-room-viewer-api-key";
|
|
19
|
+
readonly src: "data-neurodyn-room-viewer-src";
|
|
20
|
+
readonly width: "data-neurodyn-room-viewer-width";
|
|
21
|
+
readonly height: "data-neurodyn-room-viewer-height";
|
|
22
|
+
readonly dimensionUnit: "data-neurodyn-room-viewer-dimension-unit";
|
|
23
|
+
};
|
|
24
|
+
/** The trigger attributes of the AI Try-On embed build (`@neurodyn/ai-try-on`, `src/contract.ts`). Frozen likewise. */
|
|
25
|
+
export declare const AI_TRY_ON_TRIGGER: {
|
|
26
|
+
readonly marker: "data-neurodyn-ai-try-on-dialog";
|
|
27
|
+
readonly productType: "data-neurodyn-ai-try-on-product-type";
|
|
28
|
+
readonly productName: "data-neurodyn-ai-try-on-product-name";
|
|
29
|
+
readonly productImageSrc: "data-neurodyn-ai-try-on-product-image-src";
|
|
30
|
+
readonly productPrice: "data-neurodyn-ai-try-on-product-price";
|
|
31
|
+
readonly productUrl: "data-neurodyn-ai-try-on-product-url";
|
|
32
|
+
readonly apiKey: "data-neurodyn-ai-try-on-api-key";
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Adds a widget's script to the page, once however many buttons ask. Resolves
|
|
36
|
+
* when it has run, rejects when it could not be loaded.
|
|
37
|
+
*/
|
|
38
|
+
export declare function loadWidgetScript(widget: WidgetScript, doc?: Document): Promise<void>;
|
|
39
|
+
/** Forgets what was loaded; tests start from a clean page. */
|
|
40
|
+
export declare function resetWidgetScripts(): void;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vizbl/loader",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0-canary-20261115065952",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@rslib/core": "^0.18.6",
|
|
23
|
+
"eslint": "9.39.1",
|
|
24
|
+
"happy-dom": "^17.6.3",
|
|
25
|
+
"typescript": "^5.9.3",
|
|
26
|
+
"vitest": "^3.0.5"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"dev": "rslib build --watch",
|
|
30
|
+
"build": "rslib build",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"lint": "eslint .",
|
|
34
|
+
"lint:fix": "eslint . --fix"
|
|
35
|
+
}
|
|
36
|
+
}
|