@widgetic/creator 0.3.81 → 0.4.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 +157 -0
- package/dist/CreatorApp.svelte +1593 -812
- package/dist/CreatorApp.svelte.d.ts +22 -0
- package/dist/components/BrandKitPanel.svelte +48 -40
- package/dist/components/BrandKitPanel.svelte.d.ts +1 -1
- package/dist/components/DesignQuickEdit.svelte +13 -4
- package/dist/components/EditableName.svelte +13 -8
- package/dist/components/EditableName.svelte.d.ts +2 -2
- package/dist/components/FileUrlSourceControl.svelte +21 -8
- package/dist/components/HoverTooltip.svelte +1 -1
- package/dist/components/SelectorDropdown.svelte +19 -15
- package/dist/components/SelectorDropdown.svelte.d.ts +2 -2
- package/dist/components/VersionDropdownRow.svelte +14 -11
- package/dist/components/WidgetDetails.svelte +118 -92
- package/dist/components/WidgetShapeLivePreviews.svelte +105 -46
- package/dist/creator-types.d.ts +96 -0
- package/dist/host/creatorHost.d.ts +176 -0
- package/dist/host/creatorHost.js +242 -0
- package/dist/host/limitRefusal.d.ts +64 -0
- package/dist/host/limitRefusal.js +109 -0
- package/dist/i18n/index.d.ts +5 -0
- package/dist/i18n/index.js +5 -0
- package/dist/i18n/labels.d.ts +14 -0
- package/dist/i18n/labels.js +39 -0
- package/dist/i18n/language.d.ts +559 -0
- package/dist/i18n/language.js +47 -0
- package/dist/i18n/languageKit.d.ts +52 -0
- package/dist/i18n/languageKit.js +45 -0
- package/dist/i18n/messages.d.ts +526 -0
- package/dist/i18n/messages.js +548 -0
- package/dist/i18n/slots.d.ts +22 -0
- package/dist/i18n/slots.js +42 -0
- package/dist/i18n/statusText.d.ts +22 -0
- package/dist/i18n/statusText.js +223 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/stores/userSession.d.ts +8 -1
- package/dist/stores/userSession.js +18 -25
- package/dist/stores/websocketStore.d.ts +6 -0
- package/dist/stores/websocketStore.js +39 -4
- package/dist/utils/designIntentTelemetry.d.ts +8 -0
- package/dist/utils/designIntentTelemetry.js +52 -34
- package/dist/utils/directionPlacement.d.ts +51 -0
- package/dist/utils/directionPlacement.js +55 -0
- package/dist/utils/embedCode.d.ts +0 -1
- package/dist/utils/embedCode.js +0 -4
- package/dist/utils/generationSharing.d.ts +122 -0
- package/dist/utils/generationSharing.js +188 -0
- package/dist/utils/nativeScrollbarPadding.js +1 -1
- package/dist/utils/pickedFile.d.ts +15 -0
- package/dist/utils/pickedFile.js +21 -0
- package/dist/utils/widgeticChatUpload.d.ts +9 -0
- package/dist/utils/widgeticChatUpload.js +24 -2
- package/package.json +5 -3
- package/dist/retiredSurface.test.d.ts +0 -1
- package/dist/retiredSurface.test.js +0 -113
- package/dist/utils/designIntentResolver.test.d.ts +0 -1
- package/dist/utils/designIntentResolver.test.js +0 -246
- package/dist/utils/designIntentTelemetry.test.d.ts +0 -1
- package/dist/utils/designIntentTelemetry.test.js +0 -385
- package/dist/utils/designQuickEditValues.test.d.ts +0 -1
- package/dist/utils/designQuickEditValues.test.js +0 -100
- package/dist/utils/widgeticChatUpload.test.d.ts +0 -1
- package/dist/utils/widgeticChatUpload.test.js +0 -257
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the creator may touch of the page it runs in.
|
|
3
|
+
*
|
|
4
|
+
* On the Widgetic site and in the standalone app the creator owns its page: it finds the visitor's sign-in in the
|
|
5
|
+
* browser's storage, remembers the last canvas there, writes the canvas id into the address bar, sends the visitor to
|
|
6
|
+
* the billing page, reloads the page to recover and talks to the realtime socket with the visitor's account.
|
|
7
|
+
*
|
|
8
|
+
* In a partner's page none of that is the creator's to do. The partner's server signs the visitor in (the page hands
|
|
9
|
+
* the creator a session token), the partner decides which canvas opens, and the storage, the address bar and the
|
|
10
|
+
* navigation of the page belong to the partner. A browser can also be shared by several of the partner's visitors, so
|
|
11
|
+
* what one of them types or draws must not be left behind where the next one could read it.
|
|
12
|
+
*
|
|
13
|
+
* "Partner mode" is the creator keeping its hands off all of that. The page switches it on by giving the creator a
|
|
14
|
+
* `getAccessToken` function; everything below answers "may I?" for the places in the creator that used to just do it.
|
|
15
|
+
* Outside partner mode every answer is the old behaviour, so the site and the standalone app are not affected.
|
|
16
|
+
*
|
|
17
|
+
* One creator can be on a page at a time (its stores are shared by the whole page), so this is a plain module-level
|
|
18
|
+
* switch, not something passed through every component.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* The visitor, as far as the creator's own state goes. The creator never learns who the managed user is (the token is
|
|
22
|
+
* opaque and the partner keeps their own id for the person) and has no need to: it only wants *an* identity to hold its
|
|
23
|
+
* signed-in state, and to put in the `user_id` of the messages it saves.
|
|
24
|
+
*
|
|
25
|
+
* The gateway checks that field's shape (a UUID, strictly) and then replaces it with the account's, so the value only
|
|
26
|
+
* has to look right: a version-4 UUID of zeros passes the strictest check. (A plain word here made every message save
|
|
27
|
+
* fail the check.)
|
|
28
|
+
*/
|
|
29
|
+
export declare const HOST_VISITOR: {
|
|
30
|
+
readonly id: "00000000-0000-4000-8000-000000000000";
|
|
31
|
+
readonly name: "Visitor";
|
|
32
|
+
};
|
|
33
|
+
/** The three calls the creator makes on the browser's storage, so a stand-in only has to offer these. */
|
|
34
|
+
export type CreatorStorage = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
|
|
35
|
+
/** What the creator tells the page. Every one is optional: a page that does not care leaves them out. */
|
|
36
|
+
export interface CreatorHostHandlers {
|
|
37
|
+
/**
|
|
38
|
+
* The session cannot be used any more and the creator cannot get another from the page: the page has to start a new
|
|
39
|
+
* one (or take the creator away).
|
|
40
|
+
*/
|
|
41
|
+
onSessionEnded?: (detail: {
|
|
42
|
+
message: string;
|
|
43
|
+
code?: string;
|
|
44
|
+
}) => void;
|
|
45
|
+
/**
|
|
46
|
+
* The creator would reload the page to recover from something. It never does that in a partner's page; the page
|
|
47
|
+
* decides how to start over (usually by mounting the creator again).
|
|
48
|
+
*/
|
|
49
|
+
onReloadRequested?: () => void;
|
|
50
|
+
/** The visitor ran into a limit (credits used up, a cap reached). The creator shows a neutral message itself. */
|
|
51
|
+
onLimitReached?: (detail: {
|
|
52
|
+
message: string;
|
|
53
|
+
code?: string;
|
|
54
|
+
}) => void;
|
|
55
|
+
/**
|
|
56
|
+
* The canvas the page named cannot be opened by this visitor (the API answers as if it did not exist: it is not
|
|
57
|
+
* theirs, or it is gone). The creator has nothing to show, says so on its own screen and stops asking for it.
|
|
58
|
+
*/
|
|
59
|
+
onCanvasUnavailable?: (detail: {
|
|
60
|
+
canvasId: string | null;
|
|
61
|
+
message: string;
|
|
62
|
+
code?: string;
|
|
63
|
+
}) => void;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Where the creator keeps the little it remembers (the open widget, prompt drafts, cross-tab flags).
|
|
67
|
+
*
|
|
68
|
+
* The browser's local storage everywhere except in a partner's page, where it is memory: nothing the visitor does is
|
|
69
|
+
* written to the partner's origin, and nothing the partner (or another visitor of the partner) stored there is read.
|
|
70
|
+
* Each call looks at the current mode, so the switch can be flipped while code that holds this object is running.
|
|
71
|
+
*/
|
|
72
|
+
export declare const creatorStorage: CreatorStorage;
|
|
73
|
+
/**
|
|
74
|
+
* Where the creator keeps what it only needs while the page is open (a counter of the shapes it last saw, the commit a
|
|
75
|
+
* publish is waiting for). The browser's session storage everywhere except in a partner's page, where it is the same
|
|
76
|
+
* memory as {@link creatorStorage}: the keys differ, and nothing is written to the partner's own storage.
|
|
77
|
+
*/
|
|
78
|
+
export declare const creatorSessionStorage: CreatorStorage;
|
|
79
|
+
/** Whether the creator is in a partner's page (the page handed it a token, so it owns neither the sign-in nor the page). */
|
|
80
|
+
export declare function isPartnerMode(): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Puts the creator in partner mode. Whatever the memory held before is dropped, so a creator that starts for another
|
|
83
|
+
* visitor never sees what the previous one left.
|
|
84
|
+
*/
|
|
85
|
+
export declare function enterPartnerMode(handlers?: CreatorHostHandlers): void;
|
|
86
|
+
/** Ends partner mode and forgets everything kept in memory. */
|
|
87
|
+
export declare function leavePartnerMode(): void;
|
|
88
|
+
/** Replaces the page's handlers while the creator is running (the page may hand over new functions). */
|
|
89
|
+
export declare function setHostHandlers(handlers: CreatorHostHandlers): void;
|
|
90
|
+
/** Whether the creator may read or write the page's address bar (the canvas id in the path, `widget_id` in the query). */
|
|
91
|
+
export declare function mayUseAddressBar(): boolean;
|
|
92
|
+
/** Whether the creator may connect to the realtime socket. The socket only knows signed-in accounts, not sessions. */
|
|
93
|
+
export declare function mayUseRealtimeSocket(): boolean;
|
|
94
|
+
/** Whether the creator may ask for the account's trial and credit counter (an account matter, not a visitor's). */
|
|
95
|
+
export declare function mayShowAccountBilling(): boolean;
|
|
96
|
+
/** Whether the creator may take part in the page-wide `widgetic:*` events it exchanges with the Widgetic site's own page chrome. */
|
|
97
|
+
export declare function mayListenToSiteEvents(): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Lets the Widgetic site's own page chrome (its header, its save indicator, its canvas list) know something changed in
|
|
100
|
+
* the creator, with a `widgetic:*` event on `window`. A partner's page has no such chrome and is sent nothing: the
|
|
101
|
+
* creator tells it through the handlers instead.
|
|
102
|
+
*/
|
|
103
|
+
export declare function announceToSite(eventName: string, detail?: unknown): void;
|
|
104
|
+
/**
|
|
105
|
+
* Whether an event came from inside the area of the page the creator was mounted into.
|
|
106
|
+
*
|
|
107
|
+
* The creator listens on `window` for a click (to close a dropdown), Escape (to close the image viewer) and a paste (to
|
|
108
|
+
* turn a picture into a prompt attachment). On the Widgetic site every such event on the page is the creator's. In a
|
|
109
|
+
* partner's page it may only react to what happens in its own area, and must leave the partner's keys alone.
|
|
110
|
+
*
|
|
111
|
+
* The creator is mounted in a shadow root there, and the events of its content pass through that root, so the test is
|
|
112
|
+
* "is the creator's root in the event's path". Looking at `event.target` does not work for this: seen from `window`, the
|
|
113
|
+
* target of an event that started in a shadow root is the shadow's host element.
|
|
114
|
+
*/
|
|
115
|
+
export declare function eventIsFromCreator(event: Event, creatorRoot: Node | null): boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Whether any element in the event's path (shadow roots included) matches the selector. For the places that asked
|
|
118
|
+
* `event.target.closest(selector)`, which answers wrongly for an event that started inside a shadow root.
|
|
119
|
+
*/
|
|
120
|
+
export declare function eventPathMatches(event: Event, selector: string): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* The shadow root a node of the creator is in, or null where the creator is mounted straight in the page (the Widgetic
|
|
123
|
+
* site, the standalone app). `document.querySelector` and `document.body` do not reach into a shadow root and must not
|
|
124
|
+
* be used for the creator's own elements there: ask the root of the tree the creator is in instead.
|
|
125
|
+
*/
|
|
126
|
+
export declare function getCreatorShadowRoot(node: Node | null | undefined): ShadowRoot | null;
|
|
127
|
+
/**
|
|
128
|
+
* The element that gets the cursor and the "no text selection" while the visitor drags a panel or one of its edges.
|
|
129
|
+
*
|
|
130
|
+
* On the site that is the page's `<body>`, so the cursor holds even when the pointer leaves the creator. Where the creator
|
|
131
|
+
* is in a shadow root it is the element the creator was mounted into: the `<body>` of a partner's page is not the
|
|
132
|
+
* creator's to restyle, and clearing it afterwards would erase a cursor or a selection rule the page set itself.
|
|
133
|
+
*/
|
|
134
|
+
export declare function getDragFeedbackTarget(node: Node | null | undefined): HTMLElement;
|
|
135
|
+
/**
|
|
136
|
+
* The cursor and the "no text selection" while the visitor drags a panel or an edge of one: `start` puts them where
|
|
137
|
+
* `getDragFeedbackTarget` says, `stop` takes them off the very element they were put on (so a creator that is gone by
|
|
138
|
+
* then does not leave them behind, or clear a page's own style instead).
|
|
139
|
+
*/
|
|
140
|
+
export declare function createDragFeedback(): {
|
|
141
|
+
start: (node: Node | null | undefined, cursor: string) => void;
|
|
142
|
+
stop: () => void;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Whether a message came from one of the iframes the creator shows itself (a widget's preview), and not from the rest of
|
|
146
|
+
* the page.
|
|
147
|
+
*
|
|
148
|
+
* `window` hears every frame of the page it is in. On the Widgetic site that is only the creator's own previews. In a
|
|
149
|
+
* partner's page it is also an ad, a chat widget or the page's own scripts, so a handler that acts on a message (reload
|
|
150
|
+
* the preview, put a picture on the canvas) must ask first. The creator's iframes are in the same shadow root as the
|
|
151
|
+
* creator, whichever part of it they were put in (a modal included), so that is where to look for the sender.
|
|
152
|
+
*/
|
|
153
|
+
export declare function messageIsFromCreatorFrame(event: MessageEvent, creatorRoot: Node | null): boolean;
|
|
154
|
+
/** Tells the page the session is over. Returns whether the page was told (a page without the handler is not). */
|
|
155
|
+
export declare function reportSessionEnded(detail: {
|
|
156
|
+
message: string;
|
|
157
|
+
code?: string;
|
|
158
|
+
}): boolean;
|
|
159
|
+
/** Tells the page the creator wanted to reload it. Returns whether the page was told. */
|
|
160
|
+
export declare function reportReloadRequested(): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* What a "Reload" button in the creator does. The Widgetic site reloads its page; a partner's page is never reloaded
|
|
163
|
+
* by the creator, it is told the creator wants a fresh start and decides how (usually by mounting the creator again).
|
|
164
|
+
*/
|
|
165
|
+
export declare function reloadThePage(): void;
|
|
166
|
+
/** Tells the page the visitor reached a limit. Returns whether the page was told. */
|
|
167
|
+
export declare function reportLimitReached(detail: {
|
|
168
|
+
message: string;
|
|
169
|
+
code?: string;
|
|
170
|
+
}): boolean;
|
|
171
|
+
/** Tells the page the canvas it named cannot be opened by the visitor. Returns whether the page was told. */
|
|
172
|
+
export declare function reportCanvasUnavailable(detail: {
|
|
173
|
+
canvasId: string | null;
|
|
174
|
+
message: string;
|
|
175
|
+
code?: string;
|
|
176
|
+
}): boolean;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the creator may touch of the page it runs in.
|
|
3
|
+
*
|
|
4
|
+
* On the Widgetic site and in the standalone app the creator owns its page: it finds the visitor's sign-in in the
|
|
5
|
+
* browser's storage, remembers the last canvas there, writes the canvas id into the address bar, sends the visitor to
|
|
6
|
+
* the billing page, reloads the page to recover and talks to the realtime socket with the visitor's account.
|
|
7
|
+
*
|
|
8
|
+
* In a partner's page none of that is the creator's to do. The partner's server signs the visitor in (the page hands
|
|
9
|
+
* the creator a session token), the partner decides which canvas opens, and the storage, the address bar and the
|
|
10
|
+
* navigation of the page belong to the partner. A browser can also be shared by several of the partner's visitors, so
|
|
11
|
+
* what one of them types or draws must not be left behind where the next one could read it.
|
|
12
|
+
*
|
|
13
|
+
* "Partner mode" is the creator keeping its hands off all of that. The page switches it on by giving the creator a
|
|
14
|
+
* `getAccessToken` function; everything below answers "may I?" for the places in the creator that used to just do it.
|
|
15
|
+
* Outside partner mode every answer is the old behaviour, so the site and the standalone app are not affected.
|
|
16
|
+
*
|
|
17
|
+
* One creator can be on a page at a time (its stores are shared by the whole page), so this is a plain module-level
|
|
18
|
+
* switch, not something passed through every component.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* The visitor, as far as the creator's own state goes. The creator never learns who the managed user is (the token is
|
|
22
|
+
* opaque and the partner keeps their own id for the person) and has no need to: it only wants *an* identity to hold its
|
|
23
|
+
* signed-in state, and to put in the `user_id` of the messages it saves.
|
|
24
|
+
*
|
|
25
|
+
* The gateway checks that field's shape (a UUID, strictly) and then replaces it with the account's, so the value only
|
|
26
|
+
* has to look right: a version-4 UUID of zeros passes the strictest check. (A plain word here made every message save
|
|
27
|
+
* fail the check.)
|
|
28
|
+
*/
|
|
29
|
+
export const HOST_VISITOR = { id: '00000000-0000-4000-8000-000000000000', name: 'Visitor' };
|
|
30
|
+
const state = {
|
|
31
|
+
partnerMode: false,
|
|
32
|
+
handlers: {},
|
|
33
|
+
memory: new Map(),
|
|
34
|
+
};
|
|
35
|
+
/** Storage that lives only as long as the creator is on the page: nothing is written where the partner's page keeps its own. */
|
|
36
|
+
const memoryStorage = {
|
|
37
|
+
getItem: (key) => state.memory.get(String(key)) ?? null,
|
|
38
|
+
setItem: (key, value) => {
|
|
39
|
+
state.memory.set(String(key), String(value));
|
|
40
|
+
},
|
|
41
|
+
removeItem: (key) => {
|
|
42
|
+
state.memory.delete(String(key));
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Where the creator keeps the little it remembers (the open widget, prompt drafts, cross-tab flags).
|
|
47
|
+
*
|
|
48
|
+
* The browser's local storage everywhere except in a partner's page, where it is memory: nothing the visitor does is
|
|
49
|
+
* written to the partner's origin, and nothing the partner (or another visitor of the partner) stored there is read.
|
|
50
|
+
* Each call looks at the current mode, so the switch can be flipped while code that holds this object is running.
|
|
51
|
+
*/
|
|
52
|
+
export const creatorStorage = {
|
|
53
|
+
getItem: (key) => (state.partnerMode ? memoryStorage : window.localStorage).getItem(key),
|
|
54
|
+
setItem: (key, value) => (state.partnerMode ? memoryStorage : window.localStorage).setItem(key, value),
|
|
55
|
+
removeItem: (key) => (state.partnerMode ? memoryStorage : window.localStorage).removeItem(key),
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Where the creator keeps what it only needs while the page is open (a counter of the shapes it last saw, the commit a
|
|
59
|
+
* publish is waiting for). The browser's session storage everywhere except in a partner's page, where it is the same
|
|
60
|
+
* memory as {@link creatorStorage}: the keys differ, and nothing is written to the partner's own storage.
|
|
61
|
+
*/
|
|
62
|
+
export const creatorSessionStorage = {
|
|
63
|
+
getItem: (key) => (state.partnerMode ? memoryStorage : window.sessionStorage).getItem(key),
|
|
64
|
+
setItem: (key, value) => (state.partnerMode ? memoryStorage : window.sessionStorage).setItem(key, value),
|
|
65
|
+
removeItem: (key) => (state.partnerMode ? memoryStorage : window.sessionStorage).removeItem(key),
|
|
66
|
+
};
|
|
67
|
+
/** Whether the creator is in a partner's page (the page handed it a token, so it owns neither the sign-in nor the page). */
|
|
68
|
+
export function isPartnerMode() {
|
|
69
|
+
return state.partnerMode;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Puts the creator in partner mode. Whatever the memory held before is dropped, so a creator that starts for another
|
|
73
|
+
* visitor never sees what the previous one left.
|
|
74
|
+
*/
|
|
75
|
+
export function enterPartnerMode(handlers = {}) {
|
|
76
|
+
state.partnerMode = true;
|
|
77
|
+
state.handlers = handlers;
|
|
78
|
+
state.memory.clear();
|
|
79
|
+
}
|
|
80
|
+
/** Ends partner mode and forgets everything kept in memory. */
|
|
81
|
+
export function leavePartnerMode() {
|
|
82
|
+
state.partnerMode = false;
|
|
83
|
+
state.handlers = {};
|
|
84
|
+
state.memory.clear();
|
|
85
|
+
}
|
|
86
|
+
/** Replaces the page's handlers while the creator is running (the page may hand over new functions). */
|
|
87
|
+
export function setHostHandlers(handlers) {
|
|
88
|
+
state.handlers = handlers;
|
|
89
|
+
}
|
|
90
|
+
/** Whether the creator may read or write the page's address bar (the canvas id in the path, `widget_id` in the query). */
|
|
91
|
+
export function mayUseAddressBar() {
|
|
92
|
+
return !state.partnerMode;
|
|
93
|
+
}
|
|
94
|
+
/** Whether the creator may connect to the realtime socket. The socket only knows signed-in accounts, not sessions. */
|
|
95
|
+
export function mayUseRealtimeSocket() {
|
|
96
|
+
return !state.partnerMode;
|
|
97
|
+
}
|
|
98
|
+
/** Whether the creator may ask for the account's trial and credit counter (an account matter, not a visitor's). */
|
|
99
|
+
export function mayShowAccountBilling() {
|
|
100
|
+
return !state.partnerMode;
|
|
101
|
+
}
|
|
102
|
+
/** Whether the creator may take part in the page-wide `widgetic:*` events it exchanges with the Widgetic site's own page chrome. */
|
|
103
|
+
export function mayListenToSiteEvents() {
|
|
104
|
+
return !state.partnerMode;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Lets the Widgetic site's own page chrome (its header, its save indicator, its canvas list) know something changed in
|
|
108
|
+
* the creator, with a `widgetic:*` event on `window`. A partner's page has no such chrome and is sent nothing: the
|
|
109
|
+
* creator tells it through the handlers instead.
|
|
110
|
+
*/
|
|
111
|
+
export function announceToSite(eventName, detail) {
|
|
112
|
+
if (!mayListenToSiteEvents())
|
|
113
|
+
return;
|
|
114
|
+
if (typeof window === 'undefined')
|
|
115
|
+
return;
|
|
116
|
+
window.dispatchEvent(detail === undefined ? new CustomEvent(eventName) : new CustomEvent(eventName, { detail }));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Whether an event came from inside the area of the page the creator was mounted into.
|
|
120
|
+
*
|
|
121
|
+
* The creator listens on `window` for a click (to close a dropdown), Escape (to close the image viewer) and a paste (to
|
|
122
|
+
* turn a picture into a prompt attachment). On the Widgetic site every such event on the page is the creator's. In a
|
|
123
|
+
* partner's page it may only react to what happens in its own area, and must leave the partner's keys alone.
|
|
124
|
+
*
|
|
125
|
+
* The creator is mounted in a shadow root there, and the events of its content pass through that root, so the test is
|
|
126
|
+
* "is the creator's root in the event's path". Looking at `event.target` does not work for this: seen from `window`, the
|
|
127
|
+
* target of an event that started in a shadow root is the shadow's host element.
|
|
128
|
+
*/
|
|
129
|
+
export function eventIsFromCreator(event, creatorRoot) {
|
|
130
|
+
if (!state.partnerMode)
|
|
131
|
+
return true;
|
|
132
|
+
if (!creatorRoot)
|
|
133
|
+
return false;
|
|
134
|
+
return event.composedPath().includes(creatorRoot.getRootNode());
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Whether any element in the event's path (shadow roots included) matches the selector. For the places that asked
|
|
138
|
+
* `event.target.closest(selector)`, which answers wrongly for an event that started inside a shadow root.
|
|
139
|
+
*/
|
|
140
|
+
export function eventPathMatches(event, selector) {
|
|
141
|
+
return event.composedPath().some((node) => node instanceof Element && node.matches(selector));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The shadow root a node of the creator is in, or null where the creator is mounted straight in the page (the Widgetic
|
|
145
|
+
* site, the standalone app). `document.querySelector` and `document.body` do not reach into a shadow root and must not
|
|
146
|
+
* be used for the creator's own elements there: ask the root of the tree the creator is in instead.
|
|
147
|
+
*/
|
|
148
|
+
export function getCreatorShadowRoot(node) {
|
|
149
|
+
const root = node?.getRootNode?.();
|
|
150
|
+
return root && root.nodeType === 11 && 'host' in root ? root : null;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The element that gets the cursor and the "no text selection" while the visitor drags a panel or one of its edges.
|
|
154
|
+
*
|
|
155
|
+
* On the site that is the page's `<body>`, so the cursor holds even when the pointer leaves the creator. Where the creator
|
|
156
|
+
* is in a shadow root it is the element the creator was mounted into: the `<body>` of a partner's page is not the
|
|
157
|
+
* creator's to restyle, and clearing it afterwards would erase a cursor or a selection rule the page set itself.
|
|
158
|
+
*/
|
|
159
|
+
export function getDragFeedbackTarget(node) {
|
|
160
|
+
const host = getCreatorShadowRoot(node)?.host;
|
|
161
|
+
return host ?? document.body;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The cursor and the "no text selection" while the visitor drags a panel or an edge of one: `start` puts them where
|
|
165
|
+
* `getDragFeedbackTarget` says, `stop` takes them off the very element they were put on (so a creator that is gone by
|
|
166
|
+
* then does not leave them behind, or clear a page's own style instead).
|
|
167
|
+
*/
|
|
168
|
+
export function createDragFeedback() {
|
|
169
|
+
let target = null;
|
|
170
|
+
return {
|
|
171
|
+
start(node, cursor) {
|
|
172
|
+
target = getDragFeedbackTarget(node);
|
|
173
|
+
target.style.cursor = cursor;
|
|
174
|
+
target.style.userSelect = 'none';
|
|
175
|
+
},
|
|
176
|
+
stop() {
|
|
177
|
+
if (!target)
|
|
178
|
+
return;
|
|
179
|
+
target.style.cursor = '';
|
|
180
|
+
target.style.userSelect = '';
|
|
181
|
+
target = null;
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Whether a message came from one of the iframes the creator shows itself (a widget's preview), and not from the rest of
|
|
187
|
+
* the page.
|
|
188
|
+
*
|
|
189
|
+
* `window` hears every frame of the page it is in. On the Widgetic site that is only the creator's own previews. In a
|
|
190
|
+
* partner's page it is also an ad, a chat widget or the page's own scripts, so a handler that acts on a message (reload
|
|
191
|
+
* the preview, put a picture on the canvas) must ask first. The creator's iframes are in the same shadow root as the
|
|
192
|
+
* creator, whichever part of it they were put in (a modal included), so that is where to look for the sender.
|
|
193
|
+
*/
|
|
194
|
+
export function messageIsFromCreatorFrame(event, creatorRoot) {
|
|
195
|
+
if (!state.partnerMode)
|
|
196
|
+
return true;
|
|
197
|
+
const sender = event.source;
|
|
198
|
+
if (!sender || !creatorRoot)
|
|
199
|
+
return false;
|
|
200
|
+
const searchRoot = creatorRoot.getRootNode();
|
|
201
|
+
return Array.from(searchRoot.querySelectorAll('iframe')).some((frame) => frame.contentWindow === sender);
|
|
202
|
+
}
|
|
203
|
+
/** Tells the page the session is over. Returns whether the page was told (a page without the handler is not). */
|
|
204
|
+
export function reportSessionEnded(detail) {
|
|
205
|
+
return callHandler('onSessionEnded', detail);
|
|
206
|
+
}
|
|
207
|
+
/** Tells the page the creator wanted to reload it. Returns whether the page was told. */
|
|
208
|
+
export function reportReloadRequested() {
|
|
209
|
+
return callHandler('onReloadRequested', undefined);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* What a "Reload" button in the creator does. The Widgetic site reloads its page; a partner's page is never reloaded
|
|
213
|
+
* by the creator, it is told the creator wants a fresh start and decides how (usually by mounting the creator again).
|
|
214
|
+
*/
|
|
215
|
+
export function reloadThePage() {
|
|
216
|
+
if (state.partnerMode) {
|
|
217
|
+
reportReloadRequested();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
window.location.reload();
|
|
221
|
+
}
|
|
222
|
+
/** Tells the page the visitor reached a limit. Returns whether the page was told. */
|
|
223
|
+
export function reportLimitReached(detail) {
|
|
224
|
+
return callHandler('onLimitReached', detail);
|
|
225
|
+
}
|
|
226
|
+
/** Tells the page the canvas it named cannot be opened by the visitor. Returns whether the page was told. */
|
|
227
|
+
export function reportCanvasUnavailable(detail) {
|
|
228
|
+
return callHandler('onCanvasUnavailable', detail);
|
|
229
|
+
}
|
|
230
|
+
/** Calls one of the page's handlers. A handler that throws is logged and does not reach the creator. */
|
|
231
|
+
function callHandler(name, detail) {
|
|
232
|
+
const handler = state.handlers[name];
|
|
233
|
+
if (typeof handler !== 'function')
|
|
234
|
+
return false;
|
|
235
|
+
try {
|
|
236
|
+
handler(detail);
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
console.error(`[CreatorApp] The page's ${name} handler threw:`, error);
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telling the page that the visitor reached a limit.
|
|
3
|
+
*
|
|
4
|
+
* In a partner's page the creator is paid for by the partner's account, and the visitor has no plan of their own to
|
|
5
|
+
* upgrade. When the API refuses a request because of a limit (the wallet is empty, the plan does not include it, the
|
|
6
|
+
* storage is full, the hourly budget of this customer is used up) the visitor is shown a neutral sentence by the
|
|
7
|
+
* creator, and the page is told with `onLimitReached`, so the partner can show whatever they have for it (their own
|
|
8
|
+
* top-up, their own message) instead of leaving the visitor with a failed message in a chat.
|
|
9
|
+
*
|
|
10
|
+
* What counts as a limit is decided here, from the status and the API's error envelope, in one place:
|
|
11
|
+
*
|
|
12
|
+
* - 402, whatever the code: the partner's credits cannot pay for it (`BILLING_INSUFFICIENT_CREDITS`);
|
|
13
|
+
* - 403 that the API marks as a limit (`error.details.limit` is `credits`, `plan` or `storage`), or whose code is
|
|
14
|
+
* `AUTHZ_PLAN_RESTRICTION`. A 403 is also what an ordinary refusal looks like (a route a session may not use, a
|
|
15
|
+
* widget that is not the visitor's), so the code alone is not enough: without the mark it is not a limit;
|
|
16
|
+
* - 413 `QUOTA_STORAGE_EXCEEDED` (or marked `storage`): the partner's storage allowance is full. A file that is too
|
|
17
|
+
* large (`UPLOAD_TOO_LARGE`) is not a limit: the visitor can pick a smaller one;
|
|
18
|
+
* - 429 `COST_LIMIT_EXCEEDED`: this customer used the budget the partner gave each of their customers for the hour or
|
|
19
|
+
* the day. Plain rate limiting (`RATE_LIMIT_*`) is not: it clears in a minute and the creator already says so.
|
|
20
|
+
*
|
|
21
|
+
* The page is told the same sentence every time (the creator does not relay the API's, which is for the visitor) and
|
|
22
|
+
* the API's own code, which says which limit it was.
|
|
23
|
+
*/
|
|
24
|
+
/** What the page is told about a limit. The code is the API's (or `LIMIT_REACHED` when the visitor asked to upgrade). */
|
|
25
|
+
export interface LimitReachedDetail {
|
|
26
|
+
message: string;
|
|
27
|
+
code: string;
|
|
28
|
+
}
|
|
29
|
+
/** The sentence the page is told. */
|
|
30
|
+
export declare const LIMIT_REACHED_MESSAGE = "The visitor reached a limit.";
|
|
31
|
+
/** How long the page is not told about the same limit again: a visitor who retries gets the same answer. */
|
|
32
|
+
export declare const LIMIT_REPORT_QUIET_MS = 30000;
|
|
33
|
+
/** Whether an answer with this status could be a limit, so a caller reads the body of those and no others. */
|
|
34
|
+
export declare function answerMayBeALimit(status: number): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The code to tell the page when `status` and `body` are a refusal because of a limit, or null when they are not.
|
|
37
|
+
* The body is whatever the API sent (parsed JSON, or anything: a body that is not an error envelope is no limit).
|
|
38
|
+
*/
|
|
39
|
+
export declare function limitCodeOf(status: number, body: unknown): string | null;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the answer in `response` is a refusal because of a limit. It reads a copy of the body, so the caller can still
|
|
42
|
+
* read the answer itself. For the places that must word a failure differently when it is a limit.
|
|
43
|
+
*/
|
|
44
|
+
export declare function responseIsALimit(response: Response | null | undefined): Promise<boolean>;
|
|
45
|
+
/** What a reporter needs from its surroundings; the clock is replaceable so a test does not have to wait. */
|
|
46
|
+
export interface LimitReporterOptions {
|
|
47
|
+
/** Tells the page. Returns whether the page took it (a page that gave no handler did not). */
|
|
48
|
+
report: (detail: LimitReachedDetail) => boolean;
|
|
49
|
+
now?: () => number;
|
|
50
|
+
quietForMs?: number;
|
|
51
|
+
}
|
|
52
|
+
export interface LimitReporter {
|
|
53
|
+
/**
|
|
54
|
+
* Looks at an answer the API gave and tells the page when it is a limit the page has not just been told about.
|
|
55
|
+
* `where` is only for the log ("POST /agent/code/generate"). Returns whether the page was told.
|
|
56
|
+
*/
|
|
57
|
+
noteAnswer(status: number, body: unknown, where?: string): boolean;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Makes the reporter for one creator. A visitor out of credits makes several requests fail in a row (the message, the
|
|
61
|
+
* chat save, a retry); the page is told once per limit, and told again only after {@link LIMIT_REPORT_QUIET_MS}.
|
|
62
|
+
* A page that took nothing (no handler) does not start the quiet time, so a handler it adds later hears the next one.
|
|
63
|
+
*/
|
|
64
|
+
export declare function createLimitReporter(options: LimitReporterOptions): LimitReporter;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telling the page that the visitor reached a limit.
|
|
3
|
+
*
|
|
4
|
+
* In a partner's page the creator is paid for by the partner's account, and the visitor has no plan of their own to
|
|
5
|
+
* upgrade. When the API refuses a request because of a limit (the wallet is empty, the plan does not include it, the
|
|
6
|
+
* storage is full, the hourly budget of this customer is used up) the visitor is shown a neutral sentence by the
|
|
7
|
+
* creator, and the page is told with `onLimitReached`, so the partner can show whatever they have for it (their own
|
|
8
|
+
* top-up, their own message) instead of leaving the visitor with a failed message in a chat.
|
|
9
|
+
*
|
|
10
|
+
* What counts as a limit is decided here, from the status and the API's error envelope, in one place:
|
|
11
|
+
*
|
|
12
|
+
* - 402, whatever the code: the partner's credits cannot pay for it (`BILLING_INSUFFICIENT_CREDITS`);
|
|
13
|
+
* - 403 that the API marks as a limit (`error.details.limit` is `credits`, `plan` or `storage`), or whose code is
|
|
14
|
+
* `AUTHZ_PLAN_RESTRICTION`. A 403 is also what an ordinary refusal looks like (a route a session may not use, a
|
|
15
|
+
* widget that is not the visitor's), so the code alone is not enough: without the mark it is not a limit;
|
|
16
|
+
* - 413 `QUOTA_STORAGE_EXCEEDED` (or marked `storage`): the partner's storage allowance is full. A file that is too
|
|
17
|
+
* large (`UPLOAD_TOO_LARGE`) is not a limit: the visitor can pick a smaller one;
|
|
18
|
+
* - 429 `COST_LIMIT_EXCEEDED`: this customer used the budget the partner gave each of their customers for the hour or
|
|
19
|
+
* the day. Plain rate limiting (`RATE_LIMIT_*`) is not: it clears in a minute and the creator already says so.
|
|
20
|
+
*
|
|
21
|
+
* The page is told the same sentence every time (the creator does not relay the API's, which is for the visitor) and
|
|
22
|
+
* the API's own code, which says which limit it was.
|
|
23
|
+
*/
|
|
24
|
+
/** The sentence the page is told. */
|
|
25
|
+
export const LIMIT_REACHED_MESSAGE = 'The visitor reached a limit.';
|
|
26
|
+
/** How long the page is not told about the same limit again: a visitor who retries gets the same answer. */
|
|
27
|
+
export const LIMIT_REPORT_QUIET_MS = 30_000;
|
|
28
|
+
/** The kinds of limit the API marks a session's refusal with (`error.details.limit`). */
|
|
29
|
+
const MARKED_LIMITS = new Set(['credits', 'plan', 'storage']);
|
|
30
|
+
/** The statuses worth reading an answer for. Everything else is a limit never, so its body is left alone. */
|
|
31
|
+
const STATUSES_THAT_MAY_BE_A_LIMIT = new Set([402, 403, 413, 429]);
|
|
32
|
+
/** Whether an answer with this status could be a limit, so a caller reads the body of those and no others. */
|
|
33
|
+
export function answerMayBeALimit(status) {
|
|
34
|
+
return STATUSES_THAT_MAY_BE_A_LIMIT.has(status);
|
|
35
|
+
}
|
|
36
|
+
/** The error envelope of an answer: `{ error: { code, message, details } }`, or the older flat `{ code, ... }`. */
|
|
37
|
+
function errorEnvelopeOf(body) {
|
|
38
|
+
if (!body || typeof body !== 'object')
|
|
39
|
+
return null;
|
|
40
|
+
const candidate = body.error;
|
|
41
|
+
const envelope = candidate && typeof candidate === 'object' ? candidate : body;
|
|
42
|
+
return envelope;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The code to tell the page when `status` and `body` are a refusal because of a limit, or null when they are not.
|
|
46
|
+
* The body is whatever the API sent (parsed JSON, or anything: a body that is not an error envelope is no limit).
|
|
47
|
+
*/
|
|
48
|
+
export function limitCodeOf(status, body) {
|
|
49
|
+
if (!answerMayBeALimit(status))
|
|
50
|
+
return null;
|
|
51
|
+
const envelope = errorEnvelopeOf(body);
|
|
52
|
+
const code = typeof envelope?.code === 'string' && envelope.code ? envelope.code : undefined;
|
|
53
|
+
const details = envelope?.details && typeof envelope.details === 'object' ? envelope.details : null;
|
|
54
|
+
const isMarkedAsALimit = MARKED_LIMITS.has(details?.limit);
|
|
55
|
+
switch (status) {
|
|
56
|
+
case 402:
|
|
57
|
+
return code ?? 'BILLING_INSUFFICIENT_CREDITS';
|
|
58
|
+
case 403:
|
|
59
|
+
return isMarkedAsALimit || code === 'AUTHZ_PLAN_RESTRICTION' ? (code ?? 'AUTHZ_PLAN_RESTRICTION') : null;
|
|
60
|
+
case 413:
|
|
61
|
+
return isMarkedAsALimit || code === 'QUOTA_STORAGE_EXCEEDED' ? (code ?? 'QUOTA_STORAGE_EXCEEDED') : null;
|
|
62
|
+
case 429:
|
|
63
|
+
return code === 'COST_LIMIT_EXCEEDED' ? code : null;
|
|
64
|
+
default:
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Whether the answer in `response` is a refusal because of a limit. It reads a copy of the body, so the caller can still
|
|
70
|
+
* read the answer itself. For the places that must word a failure differently when it is a limit.
|
|
71
|
+
*/
|
|
72
|
+
export async function responseIsALimit(response) {
|
|
73
|
+
if (!response || !answerMayBeALimit(response.status))
|
|
74
|
+
return false;
|
|
75
|
+
try {
|
|
76
|
+
return limitCodeOf(response.status, await response.clone().json()) !== null;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// Not JSON, or already read: not an answer of the gateway's error handler.
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Makes the reporter for one creator. A visitor out of credits makes several requests fail in a row (the message, the
|
|
85
|
+
* chat save, a retry); the page is told once per limit, and told again only after {@link LIMIT_REPORT_QUIET_MS}.
|
|
86
|
+
* A page that took nothing (no handler) does not start the quiet time, so a handler it adds later hears the next one.
|
|
87
|
+
*/
|
|
88
|
+
export function createLimitReporter(options) {
|
|
89
|
+
const now = options.now ?? Date.now;
|
|
90
|
+
const quietForMs = options.quietForMs ?? LIMIT_REPORT_QUIET_MS;
|
|
91
|
+
const lastToldAtByCode = new Map();
|
|
92
|
+
return {
|
|
93
|
+
noteAnswer(status, body, where) {
|
|
94
|
+
const code = limitCodeOf(status, body);
|
|
95
|
+
if (!code)
|
|
96
|
+
return false;
|
|
97
|
+
const lastToldAt = lastToldAtByCode.get(code);
|
|
98
|
+
if (lastToldAt !== undefined && now() - lastToldAt < quietForMs)
|
|
99
|
+
return false;
|
|
100
|
+
const taken = options.report({ message: LIMIT_REACHED_MESSAGE, code });
|
|
101
|
+
if (taken) {
|
|
102
|
+
lastToldAtByCode.set(code, now());
|
|
103
|
+
// Once per limit, not once per refused request (see the quiet time above).
|
|
104
|
+
console.info(`[CreatorApp] The visitor reached a limit (${status} ${code}${where ? ` on ${where}` : ''}); the page was told.`);
|
|
105
|
+
}
|
|
106
|
+
return taken;
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CREATOR_MESSAGES, type CreatorMessageKey } from './messages.js';
|
|
2
|
+
export { ALL_CREATOR_MESSAGES, CREATOR_CATALOGS, checkCreatorMessages, provideCreatorLanguage, splitCreatorMessages, translateEnglish, useCreatorLanguage, type CreatorCatalogs, type CreatorMessageSlices } from './language.js';
|
|
3
|
+
export { embedFormatLabel, versionDateText } from './labels.js';
|
|
4
|
+
export { sentenceParts, type SentencePart } from './slots.js';
|
|
5
|
+
export { conversationLineLabel, errorLabel, statusLabel } from './statusText.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CREATOR_MESSAGES } from './messages.js';
|
|
2
|
+
export { ALL_CREATOR_MESSAGES, CREATOR_CATALOGS, checkCreatorMessages, provideCreatorLanguage, splitCreatorMessages, translateEnglish, useCreatorLanguage } from './language.js';
|
|
3
|
+
export { embedFormatLabel, versionDateText } from './labels.js';
|
|
4
|
+
export { sentenceParts } from './slots.js';
|
|
5
|
+
export { conversationLineLabel, errorLabel, statusLabel } from './statusText.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Translate } from '@widgetic/design-system/i18n';
|
|
2
|
+
import type { EmbedFormat } from '../utils/embedCode.js';
|
|
3
|
+
import type { CreatorMessageKey } from './messages.js';
|
|
4
|
+
/**
|
|
5
|
+
* The name of a way of embedding a widget, in the visitor's language. The code (`'iframe'`, `'web-component'`) is what the
|
|
6
|
+
* creator works with; this is only what the Embed step shows next to it.
|
|
7
|
+
*/
|
|
8
|
+
export declare function embedFormatLabel(format: EmbedFormat, t: Translate<CreatorMessageKey>): string;
|
|
9
|
+
/**
|
|
10
|
+
* The date of a version, as the version lists show it: the day, month and year the way `locale` writes them, then the time
|
|
11
|
+
* on a 24 hour clock ("10/01/2026 10:00" in English, "01.10.2026 10:00" in German). The time is the one of the visitor's own
|
|
12
|
+
* clock. A date that is not a date is written as a dash.
|
|
13
|
+
*/
|
|
14
|
+
export declare function versionDateText(when: Date, locale: string): string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The name of a way of embedding a widget, in the visitor's language. The code (`'iframe'`, `'web-component'`) is what the
|
|
3
|
+
* creator works with; this is only what the Embed step shows next to it.
|
|
4
|
+
*/
|
|
5
|
+
export function embedFormatLabel(format, t) {
|
|
6
|
+
switch (format) {
|
|
7
|
+
case 'iframe':
|
|
8
|
+
return t('creator.embed.formatIframe');
|
|
9
|
+
case 'web-component':
|
|
10
|
+
return t('creator.embed.formatWebComponent');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The date of a version, as the version lists show it: the day, month and year the way `locale` writes them, then the time
|
|
15
|
+
* on a 24 hour clock ("10/01/2026 10:00" in English, "01.10.2026 10:00" in German). The time is the one of the visitor's own
|
|
16
|
+
* clock. A date that is not a date is written as a dash.
|
|
17
|
+
*/
|
|
18
|
+
export function versionDateText(when, locale) {
|
|
19
|
+
if (!(when instanceof Date) || Number.isNaN(when.getTime()))
|
|
20
|
+
return '—';
|
|
21
|
+
const options = {
|
|
22
|
+
year: 'numeric',
|
|
23
|
+
month: '2-digit',
|
|
24
|
+
day: '2-digit',
|
|
25
|
+
hour: '2-digit',
|
|
26
|
+
minute: '2-digit',
|
|
27
|
+
hourCycle: 'h23'
|
|
28
|
+
};
|
|
29
|
+
let written;
|
|
30
|
+
try {
|
|
31
|
+
written = new Intl.DateTimeFormat(locale, options).format(when);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// A tag this browser cannot use (the language kit has already settled on one it can, so this is only a guard): English.
|
|
35
|
+
written = new Intl.DateTimeFormat('en', options).format(when);
|
|
36
|
+
}
|
|
37
|
+
// English puts a comma between the date and the time ("10/01/2026, 10:00"); the lists have always read "10/01/2026 10:00".
|
|
38
|
+
return written.replace(', ', ' ');
|
|
39
|
+
}
|