@panomc/widget-host 0.0.0-stage → 1.0.0-dev.61
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/runtime/chunks/chunk-9EZydhXt.js +1 -0
- package/dist/runtime/chunks/chunk-BQPllGmY.js +1 -0
- package/dist/runtime/chunks/chunk-CTgT5UYL.js +1 -0
- package/dist/runtime/chunks/chunk-ChRkfjHR.js +1 -0
- package/dist/runtime/chunks/chunk-Cmbb89rp.js +1 -0
- package/dist/runtime/chunks/chunk-CqpY4RsZ.js +1 -0
- package/dist/runtime/chunks/chunk-D7Jd5Dpn.js +1 -0
- package/dist/runtime/chunks/chunk-DNUkGPCL.js +1 -0
- package/dist/runtime/chunks/chunk-DebScgYV.js +1 -0
- package/dist/runtime/chunks/chunk-DgPYxg7o.js +1 -0
- package/dist/runtime/chunks/chunk-DhgH0pN2.js +1 -0
- package/dist/runtime/chunks/chunk-FEL6LDrJ.js +1 -0
- package/dist/runtime/chunks/chunk-RPo0Hd8r.js +1 -0
- package/dist/runtime/chunks/chunk-Xd_2KAhC.js +1 -0
- package/dist/runtime/chunks/chunk-akOtSLvV.js +1 -0
- package/dist/runtime/chunks/chunk-cUxFvg6m.js +1 -0
- package/dist/runtime/css/pano-fallback-icons.css +9 -0
- package/dist/runtime/css/pano-tokens.css +225 -0
- package/dist/runtime/host/components-panel.js +1 -0
- package/dist/runtime/host/components-theme.js +1 -0
- package/dist/runtime/host/controllers.js +1 -0
- package/dist/runtime/host/index.js +1 -0
- package/dist/runtime/host/internal.js +1 -0
- package/dist/runtime/host/svelte.js +1 -0
- package/dist/runtime/host/toasts.js +1 -0
- package/dist/runtime/host/utils-api.js +1 -0
- package/dist/runtime/host/utils-auth.js +1 -0
- package/dist/runtime/host/utils-component.js +1 -0
- package/dist/runtime/host/utils-language.js +1 -0
- package/dist/runtime/host/utils-route.js +1 -0
- package/dist/runtime/host/utils-text.js +1 -0
- package/dist/runtime/host/utils-tooltip.js +1 -0
- package/dist/runtime/host/variables.js +1 -0
- package/dist/runtime/host/views.js +1 -0
- package/dist/runtime/loader.js +1 -0
- package/dist/runtime/runtime.json +104 -0
- package/dist/runtime/svelte/animate.js +1 -0
- package/dist/runtime/svelte/attachments.js +1 -0
- package/dist/runtime/svelte/easing.js +1 -0
- package/dist/runtime/svelte/events.js +1 -0
- package/dist/runtime/svelte/index.js +1 -0
- package/dist/runtime/svelte/internal-client.js +1 -0
- package/dist/runtime/svelte/internal-disclose-version.js +1 -0
- package/dist/runtime/svelte/internal-flags-async.js +1 -0
- package/dist/runtime/svelte/internal-flags-legacy.js +1 -0
- package/dist/runtime/svelte/internal-flags-tracing.js +1 -0
- package/dist/runtime/svelte/internal.js +1 -0
- package/dist/runtime/svelte/legacy.js +1 -0
- package/dist/runtime/svelte/motion.js +1 -0
- package/dist/runtime/svelte/reactivity-window.js +1 -0
- package/dist/runtime/svelte/reactivity.js +1 -0
- package/dist/runtime/svelte/store.js +1 -0
- package/dist/runtime/svelte/transition.js +1 -0
- package/dist/runtime/svelte-i18n.js +1 -0
- package/dist/runtime/webfonts/fa-brands-400.ttf +0 -0
- package/dist/runtime/webfonts/fa-brands-400.woff2 +0 -0
- package/dist/runtime/webfonts/fa-regular-400.ttf +0 -0
- package/dist/runtime/webfonts/fa-regular-400.woff2 +0 -0
- package/dist/runtime/webfonts/fa-solid-900.ttf +0 -0
- package/dist/runtime/webfonts/fa-solid-900.woff2 +0 -0
- package/dist/runtime/webfonts/fa-v4compatibility.ttf +0 -0
- package/dist/runtime/webfonts/fa-v4compatibility.woff2 +0 -0
- package/dist/widget-runtime.zip +0 -0
- package/package.json +59 -4
- package/scripts/build-zip.js +35 -0
- package/scripts/install-local.js +57 -0
- package/scripts/loader.js +169 -0
- package/scripts/runtime-map.js +52 -0
- package/scripts/zip.js +128 -0
- package/src/__tests__/components.test.js +77 -0
- package/src/__tests__/controllers.test.js +188 -0
- package/src/__tests__/define.test.js +276 -0
- package/src/__tests__/env.js +111 -0
- package/src/__tests__/fixtures/Goal.svelte +5 -0
- package/src/__tests__/modules.test.js +251 -0
- package/src/__tests__/parity.test.js +123 -0
- package/src/api.js +144 -0
- package/src/auth.js +2 -0
- package/src/component.js +4 -0
- package/src/components/Date.svelte +38 -0
- package/src/components/NoContent.svelte +13 -0
- package/src/components/PageActions.svelte +22 -0
- package/src/components/PageTitle.svelte +32 -0
- package/src/components/Pagination.svelte +49 -0
- package/src/components/PlayerHead.svelte +39 -0
- package/src/components/Toast.svelte +16 -0
- package/src/components/empty.js +3 -0
- package/src/components/index.js +14 -0
- package/src/components/panel.js +19 -0
- package/src/config.js +100 -0
- package/src/context.js +47 -0
- package/src/controllers.js +313 -0
- package/src/define.js +352 -0
- package/src/index.js +38 -0
- package/src/internal.js +2 -0
- package/src/language.js +181 -0
- package/src/plugin.js +20 -0
- package/src/route.js +4 -0
- package/src/runtime.js +49 -0
- package/src/session.js +72 -0
- package/src/svelte-runtime.js +3 -0
- package/src/svelte.js +72 -0
- package/src/text.js +2 -0
- package/src/toasts.js +75 -0
- package/src/tooltip.js +4 -0
- package/src/variables.js +29 -0
- package/src/views.js +17 -0
- package/README.md +0 -3
package/src/language.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// `@panomc/sdk/utils/language` outside a theme (doc 06 section 3.2): the real svelte-i18n of the shared runtime, with messages
|
|
2
|
+
// from the core's THEME translations and the plugin's own `_/translations/<locale>`, each fetched once per page.
|
|
3
|
+
import { addMessages, getLocaleFromNavigator, init as initI18n, locale, _ } from 'svelte-i18n';
|
|
4
|
+
import { get, writable } from 'svelte/store';
|
|
5
|
+
import { getClient, getConfig } from './config.js';
|
|
6
|
+
|
|
7
|
+
export { _ };
|
|
8
|
+
|
|
9
|
+
export const languageLoading = writable(false);
|
|
10
|
+
export const currentLanguage = writable(null);
|
|
11
|
+
export const Languages = writable({});
|
|
12
|
+
|
|
13
|
+
const FALLBACK = 'en-US';
|
|
14
|
+
|
|
15
|
+
/** @type {Map<string, Promise<object>>} */
|
|
16
|
+
const coreMessages = new Map();
|
|
17
|
+
/** @type {Map<string, Promise<object>>} */
|
|
18
|
+
const pluginMessages = new Map();
|
|
19
|
+
/** @type {Promise<any> | null} */
|
|
20
|
+
let initializing = null;
|
|
21
|
+
let initialized = false;
|
|
22
|
+
|
|
23
|
+
function flatten(obj, prefix = '', out = {}) {
|
|
24
|
+
for (const key of Object.keys(obj ?? {})) {
|
|
25
|
+
const value = obj[key];
|
|
26
|
+
const name = prefix ? `${prefix}.${key}` : key;
|
|
27
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) flatten(value, name, out);
|
|
28
|
+
else out[name] = value;
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function unflatten(flat) {
|
|
34
|
+
const out = {};
|
|
35
|
+
for (const [flatKey, value] of Object.entries(flat)) {
|
|
36
|
+
const keys = flatKey.split('.');
|
|
37
|
+
keys.reduce((acc, key, i) => {
|
|
38
|
+
if (i === keys.length - 1) acc[key] = value;
|
|
39
|
+
else acc[key] = acc[key] && typeof acc[key] === 'object' ? acc[key] : {};
|
|
40
|
+
return acc[key];
|
|
41
|
+
}, out);
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A list in any of the shapes the API has used: an array, `{ items }` or `{ data }`. */
|
|
47
|
+
function listOf(body) {
|
|
48
|
+
if (Array.isArray(body)) return body;
|
|
49
|
+
if (Array.isArray(body?.items)) return body.items;
|
|
50
|
+
if (Array.isArray(body?.data)) return body.data;
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function getJson(path) {
|
|
55
|
+
const result = await getClient().request({ method: 'GET', path: `/api/v1${path}` });
|
|
56
|
+
return result.ok ? result.data : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function fetchLanguages() {
|
|
60
|
+
const list = listOf(await getJson('/locales'));
|
|
61
|
+
Languages.set(Object.fromEntries(list.filter((l) => l?.code).map((l) => [l.code, l])));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function getLanguageByLocale(code) {
|
|
65
|
+
return Object.values(get(Languages)).find((l) => l.code === code) ?? null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function getAcceptedLanguage(headers) {
|
|
69
|
+
const value = headers?.get?.('accept-language');
|
|
70
|
+
return value ? value.split(',')[0] : '';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Core text of the active front-end: `GET /api/v1/locales/<code>/translations/types/THEME`, once per page. */
|
|
74
|
+
export function loadLanguage(language) {
|
|
75
|
+
const code = language.code;
|
|
76
|
+
if (!coreMessages.has(code)) {
|
|
77
|
+
coreMessages.set(
|
|
78
|
+
code,
|
|
79
|
+
(async () => {
|
|
80
|
+
const body = await getJson(`/locales/${encodeURIComponent(code)}/translations/types/THEME`);
|
|
81
|
+
const messages = body && typeof body === 'object' ? (body.data ?? body.translations ?? body) : {};
|
|
82
|
+
const { result: _r, ...rest } = messages;
|
|
83
|
+
const nested = unflatten(flatten(rest));
|
|
84
|
+
addMessages(code, nested);
|
|
85
|
+
for (const derivative of language.derivatives ?? []) addMessages(derivative, nested);
|
|
86
|
+
return nested;
|
|
87
|
+
})().catch(() => ({})),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return coreMessages.get(code);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The plugin's text for a locale: `GET /api/v1/plugins/<pluginId>/_/translations/<locale>`, once per page and locale.
|
|
95
|
+
* @param {string} pluginId
|
|
96
|
+
* @param {string} [code] default: the active locale
|
|
97
|
+
*/
|
|
98
|
+
export function loadPluginTranslations(pluginId, code = get(locale) || getConfig().locale || FALLBACK) {
|
|
99
|
+
const key = `${pluginId}:${code}`;
|
|
100
|
+
if (!pluginMessages.has(key)) {
|
|
101
|
+
pluginMessages.set(
|
|
102
|
+
key,
|
|
103
|
+
(async () => {
|
|
104
|
+
const body = await getJson(`/plugins/${encodeURIComponent(pluginId)}/_/translations/${encodeURIComponent(code)}`);
|
|
105
|
+
const translations = body?.translations;
|
|
106
|
+
if (!translations || typeof translations !== 'object') return {};
|
|
107
|
+
// the subtree under `plugins.<id>` of the full text, or already the full tree
|
|
108
|
+
const nested = translations.plugins ? translations : { plugins: { [pluginId]: translations } };
|
|
109
|
+
addMessages(code, nested);
|
|
110
|
+
return nested;
|
|
111
|
+
})().catch(() => ({})),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return pluginMessages.get(key);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Activates the locale for a language object: loads its text first. */
|
|
118
|
+
export async function init(initialLocale) {
|
|
119
|
+
await fetchLanguages();
|
|
120
|
+
|
|
121
|
+
const wanted = initialLocale || getConfig().locale || get(locale) || getLocaleFromNavigator() || FALLBACK;
|
|
122
|
+
const languages = get(Languages);
|
|
123
|
+
const language = getLanguageByLocale(wanted) ?? languages[FALLBACK] ?? { code: wanted };
|
|
124
|
+
const fallback = languages[FALLBACK] ?? { code: FALLBACK };
|
|
125
|
+
|
|
126
|
+
await Promise.all([loadLanguage(fallback), language.code === fallback.code ? null : loadLanguage(language)]);
|
|
127
|
+
|
|
128
|
+
currentLanguage.set(language);
|
|
129
|
+
initI18n({ fallbackLocale: FALLBACK, initialLocale: language.code });
|
|
130
|
+
initialized = true;
|
|
131
|
+
return language;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Runs `init` once per page; never rejects. */
|
|
135
|
+
export function ensureLanguage() {
|
|
136
|
+
if (!initializing) {
|
|
137
|
+
initializing = init().catch(() => {
|
|
138
|
+
if (!initialized) {
|
|
139
|
+
initI18n({ fallbackLocale: FALLBACK, initialLocale: getConfig().locale || FALLBACK });
|
|
140
|
+
initialized = true;
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return initializing;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function changeLanguage(language) {
|
|
149
|
+
if (get(currentLanguage) === language) return;
|
|
150
|
+
|
|
151
|
+
languageLoading.set(true);
|
|
152
|
+
try {
|
|
153
|
+
await loadLanguage(language);
|
|
154
|
+
locale.set(language.code);
|
|
155
|
+
currentLanguage.set(language);
|
|
156
|
+
} finally {
|
|
157
|
+
languageLoading.set(false);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Text for a key with a built-in default; safe before the locale is initialised.
|
|
163
|
+
* @param {string} key @param {string} fallback @param {Record<string, any>} [values]
|
|
164
|
+
*/
|
|
165
|
+
export function translate(key, fallback, values) {
|
|
166
|
+
try {
|
|
167
|
+
return get(_)(key, { default: fallback, values });
|
|
168
|
+
} catch {
|
|
169
|
+
return fallback;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Forgets what was fetched (tests). */
|
|
174
|
+
export function resetLanguage() {
|
|
175
|
+
coreMessages.clear();
|
|
176
|
+
pluginMessages.clear();
|
|
177
|
+
initializing = null;
|
|
178
|
+
initialized = false;
|
|
179
|
+
currentLanguage.set(null);
|
|
180
|
+
Languages.set({});
|
|
181
|
+
}
|
package/src/plugin.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// `PanoPlugin` base class of a widget page: never instantiated (a plugin's `main.js` / `onLoad` does not run in a widget).
|
|
2
|
+
export class PanoPlugin {
|
|
3
|
+
static isPanoPlugin = true;
|
|
4
|
+
|
|
5
|
+
pano;
|
|
6
|
+
contextApi;
|
|
7
|
+
context;
|
|
8
|
+
|
|
9
|
+
constructor({ pluginId } = {}) {
|
|
10
|
+
if (!pluginId) throw new Error('[PanoPlugin] pluginId is required');
|
|
11
|
+
this.contextApi = { context: {}, set() {}, subscribe: () => () => {}, destroy() {} };
|
|
12
|
+
this.context = this.contextApi.context;
|
|
13
|
+
this._unsubscribers = [];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
onLoad(_pano) {}
|
|
17
|
+
onUnload() {}
|
|
18
|
+
setContext(_partial) {}
|
|
19
|
+
__destroy() {}
|
|
20
|
+
}
|
package/src/route.js
ADDED
package/src/runtime.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Shared page state of the widget host that is not configuration: the mounted widgets and the element events go to.
|
|
2
|
+
|
|
3
|
+
/** @typedef {HTMLElement & { reload?: () => Promise<void> }} WidgetElement */
|
|
4
|
+
|
|
5
|
+
/** @type {Set<WidgetElement>} */
|
|
6
|
+
const active = new Set();
|
|
7
|
+
/** @type {WidgetElement | null} */
|
|
8
|
+
let lastTarget = null;
|
|
9
|
+
|
|
10
|
+
export function registerWidget(element) {
|
|
11
|
+
active.add(element);
|
|
12
|
+
lastTarget = element;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function unregisterWidget(element) {
|
|
16
|
+
active.delete(element);
|
|
17
|
+
if (lastTarget === element) lastTarget = [...active].pop() ?? null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const activeWidgets = () => [...active];
|
|
21
|
+
|
|
22
|
+
/** Where `pano:toast` / `pano:navigate` raised outside a widget go: the most recent widget, else the document. */
|
|
23
|
+
export function eventTarget() {
|
|
24
|
+
return lastTarget ?? (typeof document !== 'undefined' ? document : null);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {EventTarget | null} target
|
|
29
|
+
* @param {string} name
|
|
30
|
+
* @param {any} [detail]
|
|
31
|
+
* @param {boolean} [cancelable]
|
|
32
|
+
* @returns {boolean} false when a listener cancelled it
|
|
33
|
+
*/
|
|
34
|
+
export function emit(target, name, detail, cancelable = false) {
|
|
35
|
+
if (!target || typeof CustomEvent === 'undefined') return true;
|
|
36
|
+
return target.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true, cancelable }));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A thrown `error(status, message)` or a failed load; `code` ends up in `pano:error`. */
|
|
40
|
+
export class WidgetError extends Error {
|
|
41
|
+
/** @param {number} status @param {string} [message] @param {string} [code] */
|
|
42
|
+
constructor(status, message, code) {
|
|
43
|
+
super(message || `HTTP ${status}`);
|
|
44
|
+
this.name = 'WidgetError';
|
|
45
|
+
this.status = status;
|
|
46
|
+
this.body = { message: message || `HTTP ${status}` };
|
|
47
|
+
this.code = code || `HTTP_${status}`;
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/session.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// The session probe of doc 06 section 3.5: `GET <apiBase>/api/v1/auth/credentials`, run only when a widget needs a session.
|
|
2
|
+
// A failure is an anonymous visitor, never an error.
|
|
3
|
+
import { getClient } from './config.js';
|
|
4
|
+
|
|
5
|
+
const GUEST = Object.freeze({ user: null, csrfToken: null });
|
|
6
|
+
|
|
7
|
+
/** @type {{ user: object|null, csrfToken: string|null }} */
|
|
8
|
+
let current = GUEST;
|
|
9
|
+
/** @type {Promise<typeof current> | null} */
|
|
10
|
+
let probing = null;
|
|
11
|
+
let probed = false;
|
|
12
|
+
/** @type {Set<(s: typeof current) => void>} */
|
|
13
|
+
const listeners = new Set();
|
|
14
|
+
|
|
15
|
+
function publish(next) {
|
|
16
|
+
current = next;
|
|
17
|
+
for (const fn of [...listeners]) {
|
|
18
|
+
try {
|
|
19
|
+
fn(current);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.error('[pano-widgets] session listener failed', e);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getSession() {
|
|
27
|
+
return current;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @param {{ user?: object|null, csrfToken?: string|null }} session */
|
|
31
|
+
export function setSession(session) {
|
|
32
|
+
probed = true;
|
|
33
|
+
publish({ user: session?.user ?? null, csrfToken: session?.csrfToken ?? null });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** First bind, login, logout. Not called with the current value on subscribe. */
|
|
37
|
+
export function onSession(fn) {
|
|
38
|
+
listeners.add(fn);
|
|
39
|
+
return () => listeners.delete(fn);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Runs the probe once per page (or again after `force`). */
|
|
43
|
+
export function ensureSession(force = false) {
|
|
44
|
+
if (probed && !force) return Promise.resolve(current);
|
|
45
|
+
if (probing) return probing;
|
|
46
|
+
|
|
47
|
+
probing = (async () => {
|
|
48
|
+
try {
|
|
49
|
+
const result = await getClient().request({ method: 'GET', path: '/api/v1/auth/credentials', auth: 'none' });
|
|
50
|
+
if (result.ok && result.data && typeof result.data === 'object') {
|
|
51
|
+
const { csrfToken, result: _legacy, ...user } = result.data;
|
|
52
|
+
setSession({ user, csrfToken: typeof csrfToken === 'string' ? csrfToken : null });
|
|
53
|
+
} else {
|
|
54
|
+
setSession(GUEST);
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
setSession(GUEST);
|
|
58
|
+
} finally {
|
|
59
|
+
probing = null;
|
|
60
|
+
}
|
|
61
|
+
return current;
|
|
62
|
+
})();
|
|
63
|
+
|
|
64
|
+
return probing;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function resetSession() {
|
|
68
|
+
current = GUEST;
|
|
69
|
+
probed = false;
|
|
70
|
+
probing = null;
|
|
71
|
+
listeners.clear();
|
|
72
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// The three Svelte lifecycle functions this package calls, behind one file. In a widget bundle `svelte` is external (the shared
|
|
2
|
+
// runtime); tests replace this file with the browser build of Svelte, because under Bun the default `svelte` is the server build.
|
|
3
|
+
export { mount, unmount, onMount } from 'svelte';
|
package/src/svelte.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// `@panomc/sdk/svelte` outside a theme (doc 06 section 3.2).
|
|
2
|
+
import { readable } from 'svelte/store';
|
|
3
|
+
import { getConfig, onConfigure, resolveUrl } from './config.js';
|
|
4
|
+
import { activeWidgets, eventTarget, emit, WidgetError } from './runtime.js';
|
|
5
|
+
|
|
6
|
+
export const browser = true;
|
|
7
|
+
|
|
8
|
+
/** The site URL; a live binding, updated by `configure`. */
|
|
9
|
+
export let base = getConfig().siteUrl;
|
|
10
|
+
onConfigure((config) => {
|
|
11
|
+
base = config.siteUrl;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/** `{ url, params: {} }` of the page the widget sits on. */
|
|
15
|
+
export const page = readable({ url: currentUrl(), params: {} }, (set) => {
|
|
16
|
+
const update = () => set({ url: currentUrl(), params: {} });
|
|
17
|
+
update();
|
|
18
|
+
if (typeof window === 'undefined') return undefined;
|
|
19
|
+
window.addEventListener('popstate', update);
|
|
20
|
+
return () => window.removeEventListener('popstate', update);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const navigating = readable(null);
|
|
24
|
+
|
|
25
|
+
function currentUrl() {
|
|
26
|
+
try {
|
|
27
|
+
return new URL(globalThis.location.href);
|
|
28
|
+
} catch {
|
|
29
|
+
return new URL('about:blank');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Raises a cancelable `pano:navigate` { url }; when nobody cancels it, the configured `navigate` hook or `location.assign` runs.
|
|
35
|
+
* @param {string | URL} url a site path, an absolute URL or a key of the URL map
|
|
36
|
+
* @param {{ replaceState?: boolean }} [options]
|
|
37
|
+
*/
|
|
38
|
+
export async function goto(url, options) {
|
|
39
|
+
const target = resolveUrl(String(url));
|
|
40
|
+
const proceed = emit(eventTarget(), 'pano:navigate', { url: target }, true);
|
|
41
|
+
if (!proceed) return;
|
|
42
|
+
|
|
43
|
+
const navigate = getConfig().navigate;
|
|
44
|
+
if (typeof navigate === 'function') {
|
|
45
|
+
navigate(target, { replace: !!options?.replaceState });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (typeof location === 'undefined') return;
|
|
49
|
+
if (options?.replaceState) location.replace(target);
|
|
50
|
+
else location.assign(target);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** SvelteKit's `redirect(status, location)` throws; here it navigates. */
|
|
54
|
+
export function redirect(status, location) {
|
|
55
|
+
return goto(String(location));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Throws a `WidgetError`; the wrapper shows its error state and raises `pano:error`. */
|
|
59
|
+
export function error(status, message) {
|
|
60
|
+
throw new WidgetError(Number(status) || 500, typeof message === 'string' ? message : message?.message);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Re-runs the load of every mounted widget (a widget cannot tell which resource its view depends on). */
|
|
64
|
+
export async function invalidateAll() {
|
|
65
|
+
const { clearLoadMemo } = await import('./controllers.js');
|
|
66
|
+
clearLoadMemo();
|
|
67
|
+
await Promise.all(activeWidgets().map((el) => el.reload?.()));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function invalidate(_resource) {
|
|
71
|
+
return invalidateAll();
|
|
72
|
+
}
|
package/src/text.js
ADDED
package/src/toasts.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// `@panomc/sdk/toasts` outside a theme (doc 06 section 3.2). `showToast` raises a cancelable `pano:toast`; when nobody cancels it
|
|
2
|
+
// one shared `<pano-toasts>` element, appended to `body`, shows the text.
|
|
3
|
+
import { translate } from './language.js';
|
|
4
|
+
import { emit, eventTarget } from './runtime.js';
|
|
5
|
+
|
|
6
|
+
const TAG = 'pano-toasts';
|
|
7
|
+
const LIFETIME_MS = 4500;
|
|
8
|
+
const VARIANT_COLORS = { success: '#198754', danger: '#dc3545', warning: '#b58105' };
|
|
9
|
+
|
|
10
|
+
class PanoToasts extends (typeof HTMLElement === 'undefined' ? class {} : HTMLElement) {
|
|
11
|
+
connectedCallback() {
|
|
12
|
+
if (this.shadowRoot) return;
|
|
13
|
+
const root = this.attachShadow({ mode: 'open' });
|
|
14
|
+
root.innerHTML = `<style>
|
|
15
|
+
:host { position: fixed; z-index: 2147483000; left: 50%; bottom: 1rem; transform: translateX(-50%); display: flex; flex-direction: column; gap: .5rem; align-items: center; pointer-events: none; font: 14px/1.4 system-ui, sans-serif; }
|
|
16
|
+
.toast { pointer-events: auto; max-width: min(28rem, 92vw); padding: .6rem .9rem; border-radius: .5rem; background: #212529; color: #fff; box-shadow: 0 .25rem 1rem rgba(0,0,0,.3); }
|
|
17
|
+
</style><div part="list"></div>`;
|
|
18
|
+
this._list = root.querySelector('div');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @param {string} message @param {string|null} [variant] */
|
|
22
|
+
add(message, variant) {
|
|
23
|
+
this.connectedCallback();
|
|
24
|
+
const item = document.createElement('div');
|
|
25
|
+
item.className = 'toast';
|
|
26
|
+
item.setAttribute('role', 'status');
|
|
27
|
+
item.dataset.variant = variant || '';
|
|
28
|
+
item.textContent = message;
|
|
29
|
+
if (variant && VARIANT_COLORS[variant]) item.style.borderLeft = `4px solid ${VARIANT_COLORS[variant]}`;
|
|
30
|
+
this._list.append(item);
|
|
31
|
+
setTimeout(() => item.remove(), LIFETIME_MS);
|
|
32
|
+
return item;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function ensureHost() {
|
|
37
|
+
if (typeof document === 'undefined' || typeof customElements === 'undefined') return null;
|
|
38
|
+
if (!customElements.get(TAG)) customElements.define(TAG, PanoToasts);
|
|
39
|
+
let host = document.querySelector(TAG);
|
|
40
|
+
if (!host) {
|
|
41
|
+
host = document.createElement(TAG);
|
|
42
|
+
document.body.append(host);
|
|
43
|
+
}
|
|
44
|
+
return host;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {string} text a translation key (or a plain text when it has no translation)
|
|
49
|
+
* @param {Record<string, any>} [params] values for the text
|
|
50
|
+
* @param {unknown} [_toastComponent] kept for the theme signature; ignored
|
|
51
|
+
* @param {{ variant?: 'success'|'danger'|'warning'|null }} [options]
|
|
52
|
+
*/
|
|
53
|
+
export async function showToast(text, params = {}, _toastComponent = undefined, options = {}) {
|
|
54
|
+
if (!text) return;
|
|
55
|
+
const variant = options?.variant ?? null;
|
|
56
|
+
const message = translate(text, text, params);
|
|
57
|
+
|
|
58
|
+
const proceed = emit(eventTarget(), 'pano:toast', { key: text, message, variant, values: params }, true);
|
|
59
|
+
if (!proceed) return;
|
|
60
|
+
|
|
61
|
+
ensureHost()?.add(message, variant);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function limitTitle(text) {
|
|
65
|
+
const limit = 32;
|
|
66
|
+
return text.length > limit ? `${text.substring(0, limit)}...` : text;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function showSuccess(text, params = {}) {
|
|
70
|
+
return showToast(text, params, undefined, { variant: 'success' });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function showError(text, params = {}) {
|
|
74
|
+
return showToast(text, params, undefined, { variant: 'danger' });
|
|
75
|
+
}
|
package/src/tooltip.js
ADDED
package/src/variables.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// `@panomc/sdk/variables` outside a theme: live bindings filled from the page configuration; the rest are inert.
|
|
2
|
+
import { getConfig, onConfigure } from './config.js';
|
|
3
|
+
|
|
4
|
+
export let API_URL = `${getConfig().apiBase}/api`;
|
|
5
|
+
export let UI_URL = getConfig().siteUrl;
|
|
6
|
+
export let PANEL_URL = '';
|
|
7
|
+
export let SETUP_URL = '';
|
|
8
|
+
export let PANO_WEBSITE_URL = 'https://panomc.com';
|
|
9
|
+
export let PANO_WEBSITE_API_URL = 'https://api.panomc.com';
|
|
10
|
+
export const PRERELEASE = false;
|
|
11
|
+
export const COOKIE_PREFIX = '';
|
|
12
|
+
export const CSRF_TOKEN_COOKIE_NAME = '';
|
|
13
|
+
export const JWT_COOKIE_NAME = '';
|
|
14
|
+
export const CSRF_HEADER = 'X-CSRF-Token';
|
|
15
|
+
|
|
16
|
+
onConfigure((config) => {
|
|
17
|
+
API_URL = `${config.apiBase}/api`;
|
|
18
|
+
UI_URL = config.siteUrl;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const updateApiUrl = (url) => {
|
|
22
|
+
API_URL = url;
|
|
23
|
+
};
|
|
24
|
+
export const updatePanoWebsiteUrl = (url) => {
|
|
25
|
+
PANO_WEBSITE_URL = url;
|
|
26
|
+
};
|
|
27
|
+
export const updatePanoWebsiteApiUrl = (url) => {
|
|
28
|
+
PANO_WEBSITE_API_URL = url;
|
|
29
|
+
};
|
package/src/views.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// `@panomc/sdk/views` in a widget (doc 06 section 3.2): the real proxy. A widget page has no `context.views`,
|
|
2
|
+
// so the plugin's own default view always renders.
|
|
3
|
+
import { getPanoContext } from './context.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {string} name view id, `<ns>:<Name>`
|
|
7
|
+
* @param {Function} Default the plugin's own compiled Svelte component
|
|
8
|
+
* @returns {Function} a Svelte 5 component function
|
|
9
|
+
*/
|
|
10
|
+
export const createViewProxy = (name, Default) =>
|
|
11
|
+
function PanoView(anchorOrPayload, props) {
|
|
12
|
+
const views = getPanoContext().context?.views;
|
|
13
|
+
const Override = views?.getOverride?.(name);
|
|
14
|
+
const C = Override ?? Default;
|
|
15
|
+
|
|
16
|
+
return (views?.wrap?.(name, C, Override ? 'override' : 'default') ?? C)(anchorOrPayload, props);
|
|
17
|
+
};
|
package/README.md
DELETED