@widgetic/ui-sdk 0.1.5 → 0.1.7
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 +63 -10
- package/dist/{index.js → index.cjs} +13 -23
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +52 -0
- package/dist/index.esm.js +12 -22
- package/dist/index.esm.js.map +1 -1
- package/dist/sdk/core/component.d.cts +46 -0
- package/dist/sdk/core/component.d.ts +1 -1
- package/dist/sdk/core/component.d.ts.map +1 -1
- package/dist/sdk/core/events.d.cts +25 -0
- package/dist/sdk/core/events.d.ts +1 -1
- package/dist/sdk/core/events.d.ts.map +1 -1
- package/dist/sdk/core/index.d.cts +32 -0
- package/dist/sdk/core/index.d.ts +6 -6
- package/dist/sdk/core/index.d.ts.map +1 -1
- package/dist/sdk/core/lifecycle.d.cts +96 -0
- package/dist/sdk/core/lifecycle.d.ts +3 -3
- package/dist/sdk/core/lifecycle.d.ts.map +1 -1
- package/dist/sdk/core/local-events.d.cts +24 -0
- package/dist/sdk/core/local-events.d.ts +1 -1
- package/dist/sdk/core/local-events.d.ts.map +1 -1
- package/dist/sdk/core/messaging.d.cts +57 -0
- package/dist/sdk/core/messaging.d.ts +2 -2
- package/dist/sdk/core/messaging.d.ts.map +1 -1
- package/dist/sdk/core/state.d.cts +64 -0
- package/dist/sdk/core/state.d.ts +10 -3
- package/dist/sdk/core/state.d.ts.map +1 -1
- package/dist/sdk/core/theme-mode.d.cts +43 -0
- package/dist/sdk/core/theme-mode.d.ts +1 -1
- package/dist/sdk/core/theme-mode.d.ts.map +1 -1
- package/dist/sdk/core/theme.d.cts +57 -0
- package/dist/sdk/core/theme.d.ts +2 -2
- package/dist/sdk/core/theme.d.ts.map +1 -1
- package/dist/sdk/index.d.cts +3 -0
- package/dist/sdk/index.d.ts +1 -1
- package/dist/sdk/index.d.ts.map +1 -1
- package/dist/sdk/integration/host.d.cts +53 -0
- package/dist/sdk/integration/host.d.ts +2 -2
- package/dist/sdk/integration/host.d.ts.map +1 -1
- package/dist/sdk/integration/index.d.cts +18 -0
- package/dist/sdk/integration/index.d.ts +1 -1
- package/dist/sdk/integration/index.d.ts.map +1 -1
- package/dist/sdk/layers/index.d.cts +35 -0
- package/dist/sdk/loader/ui.d.cts +207 -0
- package/dist/sdk/loader/ui.d.ts +49 -6
- package/dist/sdk/loader/ui.d.ts.map +1 -1
- package/dist/sdk/modules/index.d.cts +8 -0
- package/dist/sdk/modules/index.d.ts +1 -1
- package/dist/sdk/modules/index.d.ts.map +1 -1
- package/dist/sdk/modules/loader.d.cts +83 -0
- package/dist/sdk/modules/loader.d.ts +1 -1
- package/dist/sdk/modules/loader.d.ts.map +1 -1
- package/dist/sdk/types/component.d.cts +214 -0
- package/dist/sdk/types/component.d.ts +27 -12
- package/dist/sdk/types/component.d.ts.map +1 -1
- package/dist/sdk/types/events.d.cts +111 -0
- package/dist/sdk/types/events.d.ts.map +1 -1
- package/dist/sdk/types/index.d.cts +43 -0
- package/dist/sdk/types/index.d.ts +8 -7
- package/dist/sdk/types/index.d.ts.map +1 -1
- package/dist/sdk/types/messaging.d.cts +40 -0
- package/dist/sdk/types/module-events.d.cts +212 -0
- package/dist/sdk/types/module-events.d.ts +213 -0
- package/dist/sdk/types/module-events.d.ts.map +1 -0
- package/dist/sdk/types/module-language.d.cts +46 -0
- package/dist/sdk/types/module-media-library.d.cts +143 -0
- package/dist/sdk/types/module-theme.d.cts +96 -0
- package/dist/sdk/types/module-theme.d.ts +1 -1
- package/dist/sdk/types/module-theme.d.ts.map +1 -1
- package/dist/sdk/types/state.d.cts +37 -0
- package/dist/sdk/types/state.d.ts.map +1 -1
- package/dist/sdk/types/theme.d.cts +75 -0
- package/dist/sdk/utils/config.d.cts +69 -0
- package/dist/sdk/utils/config.d.ts.map +1 -1
- package/dist/sdk/utils/errors.d.cts +55 -0
- package/dist/sdk/utils/errors.d.ts +1 -1
- package/dist/sdk/utils/errors.d.ts.map +1 -1
- package/dist/sdk/utils/http.d.cts +20 -0
- package/dist/sdk/utils/security.d.cts +94 -0
- package/dist/sdk/utils/validation.d.cts +63 -0
- package/dist/sdk/utils/validation.d.ts +1 -1
- package/dist/sdk/utils/validation.d.ts.map +1 -1
- package/dist/sdk/version.d.cts +10 -0
- package/dist/sdk/version.d.ts +1 -1
- package/dist/services/dataService.d.cts +9 -0
- package/dist/services/dataService.d.ts +4 -4
- package/dist/services/dataService.d.ts.map +1 -1
- package/dist/umd/ui-sdk.js +1 -1
- package/dist/umd/ui-sdk.js.map +1 -1
- package/dist/utils/caseConversion.d.cts +2 -0
- package/dist/utils/caseConversion.d.ts +2 -2
- package/dist/utils/caseConversion.d.ts.map +1 -1
- package/modules/README.md +62 -35
- package/package.json +21 -9
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the modules tell your page, typed: the payload of every event of the File Browser, the composition editor and the
|
|
3
|
+
* widget creator, and the types that give `on` its event names and payloads from the name of the module you mount.
|
|
4
|
+
*
|
|
5
|
+
* const editor = await mountModule('composition-editor', {
|
|
6
|
+
* container: '#editor',
|
|
7
|
+
* compositionId,
|
|
8
|
+
* getToken,
|
|
9
|
+
* on: {
|
|
10
|
+
* saved: ({ savedAt }) => showSavedAt(savedAt), // `savedAt` is a number
|
|
11
|
+
* error: ({ message, fatal, sessionEnded }) => report(message, fatal, sessionEnded),
|
|
12
|
+
* savd: () => {}, // does not compile: this module has no such event
|
|
13
|
+
* },
|
|
14
|
+
* });
|
|
15
|
+
* editor.on('dirty', ({ dirty }) => setUnsaved(dirty)); // `dirty` is a boolean
|
|
16
|
+
* const onError = (error: CompositionEditorEvents['error']) => report(error.message, error.fatal, error.sessionEnded);
|
|
17
|
+
*
|
|
18
|
+
* Only these three modules are typed. A module with any other name still works and its events are not checked: every name
|
|
19
|
+
* is accepted and every payload is `any`, as it was before these types existed. The same happens when the name is not a
|
|
20
|
+
* literal (a `string` variable): the compiler cannot know which module it is.
|
|
21
|
+
*
|
|
22
|
+
* The modules define the same shapes for their own insides (`modules/src/<name>/index.ts`). They are written out here, so
|
|
23
|
+
* that this package stays free of the modules, and `modules/tests/rootModuleEventsTypes.test.ts` fails the modules' type
|
|
24
|
+
* check when what is written here is no longer what a module says. It holds types only.
|
|
25
|
+
*/
|
|
26
|
+
/** One file the visitor picked in the File Browser. */
|
|
27
|
+
export interface PickedFile {
|
|
28
|
+
/** The id the file has in the library it came from (Widgetic's, or your own `mediaLibrary`). */
|
|
29
|
+
id: string;
|
|
30
|
+
/** The file's name, for display. */
|
|
31
|
+
name: string;
|
|
32
|
+
/** Where the file can be fetched from. */
|
|
33
|
+
url: string;
|
|
34
|
+
/** The file's media type, like `image/png`, when known. */
|
|
35
|
+
mimeType?: string;
|
|
36
|
+
/** Size in bytes, when known. */
|
|
37
|
+
size?: number;
|
|
38
|
+
/** A small preview image, when the file is an image. */
|
|
39
|
+
thumbnail?: string;
|
|
40
|
+
}
|
|
41
|
+
/** What the visitor was doing when your media library failed. */
|
|
42
|
+
export type LibraryAction = 'setup' | 'load' | 'upload' | 'rename' | 'delete';
|
|
43
|
+
/** What every module tells with its `error` event. The composition editor and the widget creator add fields to it. */
|
|
44
|
+
export interface ModuleErrorEvent {
|
|
45
|
+
/** What went wrong, in words. */
|
|
46
|
+
message: string;
|
|
47
|
+
/** The API's error code, or one of the module's own (`TOKEN_UNAVAILABLE`, `TOKEN_REFRESH_FAILED`, `NETWORK` ...). */
|
|
48
|
+
code?: string;
|
|
49
|
+
/**
|
|
50
|
+
* `true`: the module cannot continue and has stopped. `false`: it carries on (a token refresh that failed and will be
|
|
51
|
+
* retried, for example).
|
|
52
|
+
*/
|
|
53
|
+
fatal: boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The `error` event of the File Browser. A failure of your media library arrives with `fatal: false` (the File Browser
|
|
57
|
+
* stays on screen and shows the problem, with "Try again" where that helps), a `code` starting `MEDIA_LIBRARY_`
|
|
58
|
+
* (`UNAUTHORIZED`, `FORBIDDEN`, `NOT_FOUND`, `NETWORK`, `INVALID_RESPONSE`, `NOT_SUPPORTED`, `FAILED`) and `action`,
|
|
59
|
+
* what the visitor was doing.
|
|
60
|
+
*/
|
|
61
|
+
export interface FileBrowserErrorEvent extends ModuleErrorEvent {
|
|
62
|
+
/** What the visitor was doing when the media library failed. */
|
|
63
|
+
action?: LibraryAction;
|
|
64
|
+
/** The name of the file an upload failed for. */
|
|
65
|
+
file?: string;
|
|
66
|
+
}
|
|
67
|
+
/** What the composition editor was doing when something went wrong. */
|
|
68
|
+
export type CompositionEditorErrorDuring = 'load' | 'schemas' | 'save' | 'token' | 'unmount';
|
|
69
|
+
/**
|
|
70
|
+
* The `error` event of the composition editor. The editor shows the problem to the visitor itself (with "Try again" where
|
|
71
|
+
* that helps); this is for the page. `fatal: true` means the editor cannot go on by itself: the composition cannot be
|
|
72
|
+
* opened (it does not exist, or the session may not open it) or the session has ended, so the page has to act (give a new
|
|
73
|
+
* session, or take the editor away).
|
|
74
|
+
*/
|
|
75
|
+
export interface CompositionEditorErrorEvent extends ModuleErrorEvent {
|
|
76
|
+
during?: CompositionEditorErrorDuring;
|
|
77
|
+
/** The HTTP status; `0` when there was no answer. */
|
|
78
|
+
status?: number;
|
|
79
|
+
/** Trying the same thing again later may work. */
|
|
80
|
+
retryable?: boolean;
|
|
81
|
+
/** The session cannot be used any more: the page has to give a new one. */
|
|
82
|
+
sessionEnded?: boolean;
|
|
83
|
+
/** Quote it to Widgetic's support: it finds the request in the logs. */
|
|
84
|
+
correlationId?: string;
|
|
85
|
+
}
|
|
86
|
+
/** What the widget creator was doing when something went wrong. `presets` is the lookup and copy of starting points. */
|
|
87
|
+
export type WidgetCreatorErrorDuring = 'start' | 'token' | 'session' | 'canvas' | 'presets';
|
|
88
|
+
/**
|
|
89
|
+
* The `error` event of the widget creator. The creator shows the problem to the user itself; this is for the page.
|
|
90
|
+
* `fatal: true` means the creator cannot go on: either the session has ended (`sessionEnded`), so the page has to give a
|
|
91
|
+
* new one (mount again with a fresh `getToken`), or the canvas it was told to open is not the user's (`during: 'canvas'`,
|
|
92
|
+
* code `NOT_FOUND`), so the page has to mount it again without that `canvasId`; or take the creator away.
|
|
93
|
+
*/
|
|
94
|
+
export interface WidgetCreatorErrorEvent extends ModuleErrorEvent {
|
|
95
|
+
during?: WidgetCreatorErrorDuring;
|
|
96
|
+
/** The session cannot be used any more: the page has to give a new one. */
|
|
97
|
+
sessionEnded?: boolean;
|
|
98
|
+
}
|
|
99
|
+
/** The events of the File Browser (`'file-browser'`): what `on` and `mounted.on` take, and the payload of each. */
|
|
100
|
+
export interface FileBrowserEvents {
|
|
101
|
+
/** The File Browser is on screen and ready to use. */
|
|
102
|
+
ready: undefined;
|
|
103
|
+
/** The visitor chose one or more files. */
|
|
104
|
+
select: {
|
|
105
|
+
files: PickedFile[];
|
|
106
|
+
};
|
|
107
|
+
error: FileBrowserErrorEvent;
|
|
108
|
+
}
|
|
109
|
+
/** The events of the composition editor (`'composition-editor'`): what `on` and `mounted.on` take, and the payload of each. */
|
|
110
|
+
export interface CompositionEditorEvents {
|
|
111
|
+
/** The composition is loaded and the editor is on screen. */
|
|
112
|
+
ready: {
|
|
113
|
+
name: string;
|
|
114
|
+
widgetId: string;
|
|
115
|
+
};
|
|
116
|
+
/** There is, or is no longer, work that is not saved. */
|
|
117
|
+
dirty: {
|
|
118
|
+
dirty: boolean;
|
|
119
|
+
};
|
|
120
|
+
/** A save went through. `composition` is what the API answered. */
|
|
121
|
+
saved: {
|
|
122
|
+
composition: Record<string, unknown>;
|
|
123
|
+
savedAt: number;
|
|
124
|
+
};
|
|
125
|
+
error: CompositionEditorErrorEvent;
|
|
126
|
+
}
|
|
127
|
+
/** The events of the widget creator (`'widget-creator'`): what `on` and `mounted.on` take, and the payload of each. */
|
|
128
|
+
export interface WidgetCreatorEvents {
|
|
129
|
+
/** The first canvas has loaded and the creator is ready to be used. `canvasId` is the one that is open. */
|
|
130
|
+
ready: {
|
|
131
|
+
canvasId: string | null;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* The canvas that is open changed: the creator opened one, or made a new one. Keep the id and give it back as
|
|
135
|
+
* `canvasId` the next time, so the user finds their work where they left it.
|
|
136
|
+
*/
|
|
137
|
+
canvasChanged: {
|
|
138
|
+
canvasId: string;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* The visitor started from a starting point of yours, or chose to start from scratch (`key: null`), or your page's
|
|
142
|
+
* `startFromPreset` was copied for them (`automatic: true`). Said before the creator opens on the copy; the creator
|
|
143
|
+
* then says `canvasChanged` for it as it does for any canvas it opens.
|
|
144
|
+
*/
|
|
145
|
+
presetChosen: {
|
|
146
|
+
/** The starting point's key; `null` when the visitor chose to start from scratch. */
|
|
147
|
+
key: string | null;
|
|
148
|
+
/** The canvas and widget made for the visitor; `null` when they started from scratch. */
|
|
149
|
+
canvasId: string | null;
|
|
150
|
+
widgetId: string | null;
|
|
151
|
+
widgetName: string | null;
|
|
152
|
+
/** Sentences for you, not the visitor (for instance: the copy opens as a preview). Usually empty. */
|
|
153
|
+
warnings: string[];
|
|
154
|
+
/** True when your page's `startFromPreset` was copied without asking the visitor. */
|
|
155
|
+
automatic: boolean;
|
|
156
|
+
};
|
|
157
|
+
/** The user published a widget. */
|
|
158
|
+
widgetPublished: {
|
|
159
|
+
widgetId: string;
|
|
160
|
+
version?: number;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* The main widget of the canvas has this name: said once when the canvas opens, and again each time the user renames
|
|
164
|
+
* it (never twice in a row for the same name), so your page can keep a title for the user's work.
|
|
165
|
+
*/
|
|
166
|
+
widgetRenamed: {
|
|
167
|
+
widgetId: string;
|
|
168
|
+
name: string;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* The user ran into a limit (credits used up, a cap reached, the storage full). The creator tells the user itself, in
|
|
172
|
+
* plain words; this is for your page, for instance to offer more. `code` is what the API answered
|
|
173
|
+
* (`BILLING_INSUFFICIENT_CREDITS`, `AUTHZ_PLAN_RESTRICTION`, `QUOTA_STORAGE_EXCEEDED`, `COST_LIMIT_EXCEEDED`), or
|
|
174
|
+
* `LIMIT_REACHED` when the user pressed an "Upgrade" button; your page hears each code once in half a minute.
|
|
175
|
+
* Ordinary rate limiting is not a limit and is not reported.
|
|
176
|
+
*/
|
|
177
|
+
limitReached: {
|
|
178
|
+
message: string;
|
|
179
|
+
code?: string;
|
|
180
|
+
};
|
|
181
|
+
/** The creator would have reloaded the page to recover. Unmount it and mount a new one to start over. */
|
|
182
|
+
reloadRequested: undefined;
|
|
183
|
+
error: WidgetCreatorErrorEvent;
|
|
184
|
+
}
|
|
185
|
+
/** The modules whose events are typed, by the name you mount them with. */
|
|
186
|
+
export interface ModuleEventMap {
|
|
187
|
+
'file-browser': FileBrowserEvents;
|
|
188
|
+
'composition-editor': CompositionEditorEvents;
|
|
189
|
+
'widget-creator': WidgetCreatorEvents;
|
|
190
|
+
}
|
|
191
|
+
/** The names of the modules whose events are typed. */
|
|
192
|
+
export type KnownModuleName = keyof ModuleEventMap;
|
|
193
|
+
/** The events of a module this SDK has no types for (one published after this release): any name, any payload. */
|
|
194
|
+
type UncheckedModuleEvents = Record<string, any>;
|
|
195
|
+
/**
|
|
196
|
+
* The events of the module with this name. For a name that is not one of {@link KnownModuleName}, or not a single literal
|
|
197
|
+
* (`string`), any event name is accepted and every payload is `any`. For a union of known names it is the union of their
|
|
198
|
+
* events, so only what all of them say can be used.
|
|
199
|
+
*/
|
|
200
|
+
export type ModuleEvents<Name extends string = string> = [Name] extends [KnownModuleName] ? ModuleEventMap[Name] : UncheckedModuleEvents;
|
|
201
|
+
/**
|
|
202
|
+
* What the `on` option of a mount call takes for the module with this name: a handler per event, each optional, each given
|
|
203
|
+
* the payload of that event. Naming an event the module does not have does not compile.
|
|
204
|
+
*/
|
|
205
|
+
export type ModuleEventHandlers<Name extends string = string> = {
|
|
206
|
+
[Event in keyof ModuleEvents<Name>]?: (payload: ModuleEvents<Name>[Event]) => void;
|
|
207
|
+
};
|
|
208
|
+
/** The names of the events of the module with this name (any `string` for a module that is not typed). */
|
|
209
|
+
export type ModuleEventName<Name extends string = string> = Extract<keyof ModuleEvents<Name>, string>;
|
|
210
|
+
/** The payload of one event of the module with this name (`any` for a module that is not typed). */
|
|
211
|
+
export type ModuleEventPayload<Name extends string, Event extends ModuleEventName<Name>> = ModuleEvents<Name>[Event & keyof ModuleEvents<Name>];
|
|
212
|
+
export {};
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the modules tell your page, typed: the payload of every event of the File Browser, the composition editor and the
|
|
3
|
+
* widget creator, and the types that give `on` its event names and payloads from the name of the module you mount.
|
|
4
|
+
*
|
|
5
|
+
* const editor = await mountModule('composition-editor', {
|
|
6
|
+
* container: '#editor',
|
|
7
|
+
* compositionId,
|
|
8
|
+
* getToken,
|
|
9
|
+
* on: {
|
|
10
|
+
* saved: ({ savedAt }) => showSavedAt(savedAt), // `savedAt` is a number
|
|
11
|
+
* error: ({ message, fatal, sessionEnded }) => report(message, fatal, sessionEnded),
|
|
12
|
+
* savd: () => {}, // does not compile: this module has no such event
|
|
13
|
+
* },
|
|
14
|
+
* });
|
|
15
|
+
* editor.on('dirty', ({ dirty }) => setUnsaved(dirty)); // `dirty` is a boolean
|
|
16
|
+
* const onError = (error: CompositionEditorEvents['error']) => report(error.message, error.fatal, error.sessionEnded);
|
|
17
|
+
*
|
|
18
|
+
* Only these three modules are typed. A module with any other name still works and its events are not checked: every name
|
|
19
|
+
* is accepted and every payload is `any`, as it was before these types existed. The same happens when the name is not a
|
|
20
|
+
* literal (a `string` variable): the compiler cannot know which module it is.
|
|
21
|
+
*
|
|
22
|
+
* The modules define the same shapes for their own insides (`modules/src/<name>/index.ts`). They are written out here, so
|
|
23
|
+
* that this package stays free of the modules, and `modules/tests/rootModuleEventsTypes.test.ts` fails the modules' type
|
|
24
|
+
* check when what is written here is no longer what a module says. It holds types only.
|
|
25
|
+
*/
|
|
26
|
+
/** One file the visitor picked in the File Browser. */
|
|
27
|
+
export interface PickedFile {
|
|
28
|
+
/** The id the file has in the library it came from (Widgetic's, or your own `mediaLibrary`). */
|
|
29
|
+
id: string;
|
|
30
|
+
/** The file's name, for display. */
|
|
31
|
+
name: string;
|
|
32
|
+
/** Where the file can be fetched from. */
|
|
33
|
+
url: string;
|
|
34
|
+
/** The file's media type, like `image/png`, when known. */
|
|
35
|
+
mimeType?: string;
|
|
36
|
+
/** Size in bytes, when known. */
|
|
37
|
+
size?: number;
|
|
38
|
+
/** A small preview image, when the file is an image. */
|
|
39
|
+
thumbnail?: string;
|
|
40
|
+
}
|
|
41
|
+
/** What the visitor was doing when your media library failed. */
|
|
42
|
+
export type LibraryAction = 'setup' | 'load' | 'upload' | 'rename' | 'delete';
|
|
43
|
+
/** What every module tells with its `error` event. The composition editor and the widget creator add fields to it. */
|
|
44
|
+
export interface ModuleErrorEvent {
|
|
45
|
+
/** What went wrong, in words. */
|
|
46
|
+
message: string;
|
|
47
|
+
/** The API's error code, or one of the module's own (`TOKEN_UNAVAILABLE`, `TOKEN_REFRESH_FAILED`, `NETWORK` ...). */
|
|
48
|
+
code?: string;
|
|
49
|
+
/**
|
|
50
|
+
* `true`: the module cannot continue and has stopped. `false`: it carries on (a token refresh that failed and will be
|
|
51
|
+
* retried, for example).
|
|
52
|
+
*/
|
|
53
|
+
fatal: boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The `error` event of the File Browser. A failure of your media library arrives with `fatal: false` (the File Browser
|
|
57
|
+
* stays on screen and shows the problem, with "Try again" where that helps), a `code` starting `MEDIA_LIBRARY_`
|
|
58
|
+
* (`UNAUTHORIZED`, `FORBIDDEN`, `NOT_FOUND`, `NETWORK`, `INVALID_RESPONSE`, `NOT_SUPPORTED`, `FAILED`) and `action`,
|
|
59
|
+
* what the visitor was doing.
|
|
60
|
+
*/
|
|
61
|
+
export interface FileBrowserErrorEvent extends ModuleErrorEvent {
|
|
62
|
+
/** What the visitor was doing when the media library failed. */
|
|
63
|
+
action?: LibraryAction;
|
|
64
|
+
/** The name of the file an upload failed for. */
|
|
65
|
+
file?: string;
|
|
66
|
+
}
|
|
67
|
+
/** What the composition editor was doing when something went wrong. */
|
|
68
|
+
export type CompositionEditorErrorDuring = 'load' | 'schemas' | 'save' | 'token' | 'unmount';
|
|
69
|
+
/**
|
|
70
|
+
* The `error` event of the composition editor. The editor shows the problem to the visitor itself (with "Try again" where
|
|
71
|
+
* that helps); this is for the page. `fatal: true` means the editor cannot go on by itself: the composition cannot be
|
|
72
|
+
* opened (it does not exist, or the session may not open it) or the session has ended, so the page has to act (give a new
|
|
73
|
+
* session, or take the editor away).
|
|
74
|
+
*/
|
|
75
|
+
export interface CompositionEditorErrorEvent extends ModuleErrorEvent {
|
|
76
|
+
during?: CompositionEditorErrorDuring;
|
|
77
|
+
/** The HTTP status; `0` when there was no answer. */
|
|
78
|
+
status?: number;
|
|
79
|
+
/** Trying the same thing again later may work. */
|
|
80
|
+
retryable?: boolean;
|
|
81
|
+
/** The session cannot be used any more: the page has to give a new one. */
|
|
82
|
+
sessionEnded?: boolean;
|
|
83
|
+
/** Quote it to Widgetic's support: it finds the request in the logs. */
|
|
84
|
+
correlationId?: string;
|
|
85
|
+
}
|
|
86
|
+
/** What the widget creator was doing when something went wrong. `presets` is the lookup and copy of starting points. */
|
|
87
|
+
export type WidgetCreatorErrorDuring = 'start' | 'token' | 'session' | 'canvas' | 'presets';
|
|
88
|
+
/**
|
|
89
|
+
* The `error` event of the widget creator. The creator shows the problem to the user itself; this is for the page.
|
|
90
|
+
* `fatal: true` means the creator cannot go on: either the session has ended (`sessionEnded`), so the page has to give a
|
|
91
|
+
* new one (mount again with a fresh `getToken`), or the canvas it was told to open is not the user's (`during: 'canvas'`,
|
|
92
|
+
* code `NOT_FOUND`), so the page has to mount it again without that `canvasId`; or take the creator away.
|
|
93
|
+
*/
|
|
94
|
+
export interface WidgetCreatorErrorEvent extends ModuleErrorEvent {
|
|
95
|
+
during?: WidgetCreatorErrorDuring;
|
|
96
|
+
/** The session cannot be used any more: the page has to give a new one. */
|
|
97
|
+
sessionEnded?: boolean;
|
|
98
|
+
}
|
|
99
|
+
/** The events of the File Browser (`'file-browser'`): what `on` and `mounted.on` take, and the payload of each. */
|
|
100
|
+
export interface FileBrowserEvents {
|
|
101
|
+
/** The File Browser is on screen and ready to use. */
|
|
102
|
+
ready: undefined;
|
|
103
|
+
/** The visitor chose one or more files. */
|
|
104
|
+
select: {
|
|
105
|
+
files: PickedFile[];
|
|
106
|
+
};
|
|
107
|
+
error: FileBrowserErrorEvent;
|
|
108
|
+
}
|
|
109
|
+
/** The events of the composition editor (`'composition-editor'`): what `on` and `mounted.on` take, and the payload of each. */
|
|
110
|
+
export interface CompositionEditorEvents {
|
|
111
|
+
/** The composition is loaded and the editor is on screen. */
|
|
112
|
+
ready: {
|
|
113
|
+
name: string;
|
|
114
|
+
widgetId: string;
|
|
115
|
+
};
|
|
116
|
+
/** There is, or is no longer, work that is not saved. */
|
|
117
|
+
dirty: {
|
|
118
|
+
dirty: boolean;
|
|
119
|
+
};
|
|
120
|
+
/** A save went through. `composition` is what the API answered. */
|
|
121
|
+
saved: {
|
|
122
|
+
composition: Record<string, unknown>;
|
|
123
|
+
savedAt: number;
|
|
124
|
+
};
|
|
125
|
+
error: CompositionEditorErrorEvent;
|
|
126
|
+
}
|
|
127
|
+
/** The events of the widget creator (`'widget-creator'`): what `on` and `mounted.on` take, and the payload of each. */
|
|
128
|
+
export interface WidgetCreatorEvents {
|
|
129
|
+
/** The first canvas has loaded and the creator is ready to be used. `canvasId` is the one that is open. */
|
|
130
|
+
ready: {
|
|
131
|
+
canvasId: string | null;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* The canvas that is open changed: the creator opened one, or made a new one. Keep the id and give it back as
|
|
135
|
+
* `canvasId` the next time, so the user finds their work where they left it.
|
|
136
|
+
*/
|
|
137
|
+
canvasChanged: {
|
|
138
|
+
canvasId: string;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* The visitor started from a starting point of yours, or chose to start from scratch (`key: null`), or your page's
|
|
142
|
+
* `startFromPreset` was copied for them (`automatic: true`). Said before the creator opens on the copy; the creator
|
|
143
|
+
* then says `canvasChanged` for it as it does for any canvas it opens.
|
|
144
|
+
*/
|
|
145
|
+
presetChosen: {
|
|
146
|
+
/** The starting point's key; `null` when the visitor chose to start from scratch. */
|
|
147
|
+
key: string | null;
|
|
148
|
+
/** The canvas and widget made for the visitor; `null` when they started from scratch. */
|
|
149
|
+
canvasId: string | null;
|
|
150
|
+
widgetId: string | null;
|
|
151
|
+
widgetName: string | null;
|
|
152
|
+
/** Sentences for you, not the visitor (for instance: the copy opens as a preview). Usually empty. */
|
|
153
|
+
warnings: string[];
|
|
154
|
+
/** True when your page's `startFromPreset` was copied without asking the visitor. */
|
|
155
|
+
automatic: boolean;
|
|
156
|
+
};
|
|
157
|
+
/** The user published a widget. */
|
|
158
|
+
widgetPublished: {
|
|
159
|
+
widgetId: string;
|
|
160
|
+
version?: number;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* The main widget of the canvas has this name: said once when the canvas opens, and again each time the user renames
|
|
164
|
+
* it (never twice in a row for the same name), so your page can keep a title for the user's work.
|
|
165
|
+
*/
|
|
166
|
+
widgetRenamed: {
|
|
167
|
+
widgetId: string;
|
|
168
|
+
name: string;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* The user ran into a limit (credits used up, a cap reached, the storage full). The creator tells the user itself, in
|
|
172
|
+
* plain words; this is for your page, for instance to offer more. `code` is what the API answered
|
|
173
|
+
* (`BILLING_INSUFFICIENT_CREDITS`, `AUTHZ_PLAN_RESTRICTION`, `QUOTA_STORAGE_EXCEEDED`, `COST_LIMIT_EXCEEDED`), or
|
|
174
|
+
* `LIMIT_REACHED` when the user pressed an "Upgrade" button; your page hears each code once in half a minute.
|
|
175
|
+
* Ordinary rate limiting is not a limit and is not reported.
|
|
176
|
+
*/
|
|
177
|
+
limitReached: {
|
|
178
|
+
message: string;
|
|
179
|
+
code?: string;
|
|
180
|
+
};
|
|
181
|
+
/** The creator would have reloaded the page to recover. Unmount it and mount a new one to start over. */
|
|
182
|
+
reloadRequested: undefined;
|
|
183
|
+
error: WidgetCreatorErrorEvent;
|
|
184
|
+
}
|
|
185
|
+
/** The modules whose events are typed, by the name you mount them with. */
|
|
186
|
+
export interface ModuleEventMap {
|
|
187
|
+
'file-browser': FileBrowserEvents;
|
|
188
|
+
'composition-editor': CompositionEditorEvents;
|
|
189
|
+
'widget-creator': WidgetCreatorEvents;
|
|
190
|
+
}
|
|
191
|
+
/** The names of the modules whose events are typed. */
|
|
192
|
+
export type KnownModuleName = keyof ModuleEventMap;
|
|
193
|
+
/** The events of a module this SDK has no types for (one published after this release): any name, any payload. */
|
|
194
|
+
type UncheckedModuleEvents = Record<string, any>;
|
|
195
|
+
/**
|
|
196
|
+
* The events of the module with this name. For a name that is not one of {@link KnownModuleName}, or not a single literal
|
|
197
|
+
* (`string`), any event name is accepted and every payload is `any`. For a union of known names it is the union of their
|
|
198
|
+
* events, so only what all of them say can be used.
|
|
199
|
+
*/
|
|
200
|
+
export type ModuleEvents<Name extends string = string> = [Name] extends [KnownModuleName] ? ModuleEventMap[Name] : UncheckedModuleEvents;
|
|
201
|
+
/**
|
|
202
|
+
* What the `on` option of a mount call takes for the module with this name: a handler per event, each optional, each given
|
|
203
|
+
* the payload of that event. Naming an event the module does not have does not compile.
|
|
204
|
+
*/
|
|
205
|
+
export type ModuleEventHandlers<Name extends string = string> = {
|
|
206
|
+
[Event in keyof ModuleEvents<Name>]?: (payload: ModuleEvents<Name>[Event]) => void;
|
|
207
|
+
};
|
|
208
|
+
/** The names of the events of the module with this name (any `string` for a module that is not typed). */
|
|
209
|
+
export type ModuleEventName<Name extends string = string> = Extract<keyof ModuleEvents<Name>, string>;
|
|
210
|
+
/** The payload of one event of the module with this name (`any` for a module that is not typed). */
|
|
211
|
+
export type ModuleEventPayload<Name extends string, Event extends ModuleEventName<Name>> = ModuleEvents<Name>[Event & keyof ModuleEvents<Name>];
|
|
212
|
+
export {};
|
|
213
|
+
//# sourceMappingURL=module-events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-events.d.ts","sourceRoot":"","sources":["../../../src/sdk/types/module-events.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a partner can say about the language of a UI module: the `locale` and `messages` options of a mount call, and
|
|
3
|
+
* `setLanguage(...)` of a mounted module.
|
|
4
|
+
*
|
|
5
|
+
* const fileBrowser = await ui.mount('file-browser', {
|
|
6
|
+
* container,
|
|
7
|
+
* locale: 'de', // plural rules, numbers, file sizes, dates
|
|
8
|
+
* messages: { 'action.cancel': 'Abbrechen', 'upload.clear': 'Leeren' }, // the words; any key left out stays English
|
|
9
|
+
* });
|
|
10
|
+
* fileBrowser.setLanguage({ locale: 'fr', messages: frenchMessages }); // while it is on screen
|
|
11
|
+
*
|
|
12
|
+
* The composition editor takes the same options with ONE `messages` object for everything it shows (its own words, its
|
|
13
|
+
* file picker's and its rich text editor's); the editor splits it between them. The widget creator does the same for
|
|
14
|
+
* the whole creator (its own words, the chat's, the canvas's, the starting-point picker's, and the composition editor's
|
|
15
|
+
* with the file picker and the rich text editor inside it).
|
|
16
|
+
*
|
|
17
|
+
* Only the words the visitor reads are covered: what the module tells the page's code (`error` events, thrown errors,
|
|
18
|
+
* the console) stays English.
|
|
19
|
+
*
|
|
20
|
+
* This file is the single source of these types: the modules (`modules/src/shared/languageTypes.ts`) re-export it, so what
|
|
21
|
+
* the loader accepts and what a module accepts cannot drift apart. It holds types only.
|
|
22
|
+
*/
|
|
23
|
+
/** The language of a module: how numbers, dates and file sizes are written, and the words itself. */
|
|
24
|
+
export interface ModuleLanguage {
|
|
25
|
+
/**
|
|
26
|
+
* A BCP 47 language tag such as `de` or `pt-BR`. It sets the plural rules ("1 file", "2 files", and the extra forms
|
|
27
|
+
* of Polish or Arabic) and how numbers, dates and file sizes are written. When the module has a **translation pack**
|
|
28
|
+
* for the tag (see the modules' README), it loads that pack from the CDN and applies those words under your
|
|
29
|
+
* `messages`. Tags with no pack keep English for words; formatting and text direction still follow `locale`. A tag
|
|
30
|
+
* this browser has no plural data for is reported in the console and English formatting is used.
|
|
31
|
+
*
|
|
32
|
+
* It is also the `lang` of the module's elements, so a screen reader says the words in that language. The File
|
|
33
|
+
* Browser is read the way the language is written: a language written right to left (Arabic, Hebrew, Persian, Urdu
|
|
34
|
+
* ...) turns the whole screen around, and `setLanguage` turns it around or back while it is on screen. The page's own
|
|
35
|
+
* direction does not decide. The composition editor and the widget creator follow `locale` the same way (panels, toolbars,
|
|
36
|
+
* menus and dialogs mirror); the canvas board, live widget previews and code fields stay left to right on purpose.
|
|
37
|
+
*/
|
|
38
|
+
locale?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The words of the module in your language: a message key and its text, like `{ 'action.cancel': 'Abbrechen' }`.
|
|
41
|
+
* `{name}` puts a value in; `{count, plural, one {# file} other {# files}}` picks a form for a number. A key left
|
|
42
|
+
* out keeps its English text. A module lists its keys: the File Browser's are in its README, "Languages", and every
|
|
43
|
+
* module that has words offers them as `defaultMessages`.
|
|
44
|
+
*/
|
|
45
|
+
messages?: Record<string, string>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a partner can say about its own files: the `mediaLibrary` option of the File Browser, the composition editor
|
|
3
|
+
* and the widget creator, and the `pickFile` option of the composition editor and the widget creator.
|
|
4
|
+
*
|
|
5
|
+
* await ui.mount('composition-editor', {
|
|
6
|
+
* container,
|
|
7
|
+
* compositionId,
|
|
8
|
+
* mediaLibrary: {
|
|
9
|
+
* id: 'acme-assets',
|
|
10
|
+
* list: async ({ cursor, limit, signal }) => ({ items: await listAcmeFiles(cursor, limit, signal), nextCursor: null }),
|
|
11
|
+
* urlResolver: async (file) => `https://cdn.acme.com/assets/${file.id}`, // the address that is saved
|
|
12
|
+
* },
|
|
13
|
+
* pickFile: async ({ accept, multiple, signal }) => openAcmePicker({ accept, multiple, signal }), // [] when cancelled
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* The File Browser package (`@widgetic/file-browser`) defines the same shapes for its own insides. They are written out
|
|
17
|
+
* here, so that the package stays free of it, and `modules/tests/rootMediaLibraryTypes.test.ts` fails the modules' type
|
|
18
|
+
* check when what you write against these types is no longer what the modules accept. It holds types only.
|
|
19
|
+
*/
|
|
20
|
+
/** One file, as your library or your picker hands it to a module. */
|
|
21
|
+
export interface MediaLibraryFile {
|
|
22
|
+
/**
|
|
23
|
+
* The file's id in your library: stable and unique there. It is handed back unchanged when the visitor picks the
|
|
24
|
+
* file and when a rename or a delete is asked for. A widget keeps it only so the picker shows the file as selected
|
|
25
|
+
* the next time it opens.
|
|
26
|
+
*/
|
|
27
|
+
id: string;
|
|
28
|
+
/** The name shown to the visitor, with its extension (`logo.png`). */
|
|
29
|
+
name: string;
|
|
30
|
+
/**
|
|
31
|
+
* Where the visitor's browser can fetch the file: an `http:` or `https:` address, or a path on the page's own site.
|
|
32
|
+
* This is what a widget stores and shows, and everyone who sees the published widget loads it, so give one that
|
|
33
|
+
* stays valid and needs no sign-in (see `mediaLibrary.urlResolver`).
|
|
34
|
+
*/
|
|
35
|
+
url: string;
|
|
36
|
+
/** The file's media type, like `image/png`. It tells images from audio, video and documents. */
|
|
37
|
+
mimeType?: string;
|
|
38
|
+
/** Size in bytes. */
|
|
39
|
+
size?: number;
|
|
40
|
+
/** When the file last changed: an ISO 8601 string, a `Date`, or milliseconds since 1970. */
|
|
41
|
+
modified?: string | number | Date;
|
|
42
|
+
/** A small preview picture (an image address), for a file that is not itself a small image. */
|
|
43
|
+
thumbnail?: string;
|
|
44
|
+
/** A content hash, handed back to you as it is. Never interpreted. */
|
|
45
|
+
hash?: string;
|
|
46
|
+
}
|
|
47
|
+
/** What `list` is asked for. */
|
|
48
|
+
export interface MediaLibraryListRequest {
|
|
49
|
+
/** The `nextCursor` of the page before. Absent for the first page. */
|
|
50
|
+
cursor?: string;
|
|
51
|
+
/** The most files the module wants in this page. Returning fewer is fine. */
|
|
52
|
+
limit: number;
|
|
53
|
+
/** Aborted when the module no longer wants the answer (it was closed, or reloaded). */
|
|
54
|
+
signal: AbortSignal;
|
|
55
|
+
}
|
|
56
|
+
/** One page of your library, newest files first. */
|
|
57
|
+
export interface MediaLibraryPage {
|
|
58
|
+
items: MediaLibraryFile[];
|
|
59
|
+
/** Pass this back as `cursor` to get the next page. Absent, `null` or empty when there is no more. */
|
|
60
|
+
nextCursor?: string | null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Where in the product a file is being stored, when the module can say.
|
|
64
|
+
* - `field`: a file field of the composition editor, or of the widget creator's settings panel.
|
|
65
|
+
* - `chat`: an attachment of the widget creator's chat.
|
|
66
|
+
* - `canvas`: a picture dropped on the widget creator's canvas or into its prompt.
|
|
67
|
+
*/
|
|
68
|
+
export type MediaUploadPurpose = 'canvas' | 'chat' | 'field';
|
|
69
|
+
/**
|
|
70
|
+
* What `upload` is asked for. `purpose` is given by the composition editor and the widget creator; the File Browser
|
|
71
|
+
* module leaves it out.
|
|
72
|
+
*/
|
|
73
|
+
export interface MediaLibraryUploadRequest {
|
|
74
|
+
file: File;
|
|
75
|
+
purpose?: MediaUploadPurpose;
|
|
76
|
+
/** Tell the module how far the upload is, from 0 to 100. Optional to call. */
|
|
77
|
+
onProgress?: (percentage: number) => void;
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
}
|
|
80
|
+
/** What `rename` is asked for. */
|
|
81
|
+
export interface MediaLibraryRenameRequest {
|
|
82
|
+
id: string;
|
|
83
|
+
/** The new name, with its extension. */
|
|
84
|
+
name: string;
|
|
85
|
+
signal: AbortSignal;
|
|
86
|
+
}
|
|
87
|
+
/** What `delete` is asked for. */
|
|
88
|
+
export interface MediaLibraryDeleteRequest {
|
|
89
|
+
id: string;
|
|
90
|
+
signal: AbortSignal;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Turns a file of your library (or one your `pickFile` returned) into the address that is saved with the widget. It may
|
|
94
|
+
* answer at once or later; the module waits up to 10 seconds. When it throws, takes longer, or answers something that
|
|
95
|
+
* is not an `http:` or `https:` address, the file's own `url` is kept and one warning is written to the console.
|
|
96
|
+
*/
|
|
97
|
+
export type MediaLibraryUrlResolver = (file: MediaLibraryFile) => string | Promise<string>;
|
|
98
|
+
/** Your own media library: plain functions that read and write your files. Only `list` is required. */
|
|
99
|
+
export interface PartnerMediaLibrary {
|
|
100
|
+
/** A short stable name for the library, like `acme-assets`. Used in logs. */
|
|
101
|
+
readonly id: string;
|
|
102
|
+
/** What the visitor sees as the list's name. Default: "My uploads". */
|
|
103
|
+
readonly label?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Switches actions off. A button is shown only for the functions you give, so this can only take actions away:
|
|
106
|
+
* `capabilities: { delete: false }` hides Delete although `delete` is given.
|
|
107
|
+
*/
|
|
108
|
+
readonly capabilities?: {
|
|
109
|
+
upload?: boolean;
|
|
110
|
+
rename?: boolean;
|
|
111
|
+
delete?: boolean;
|
|
112
|
+
download?: boolean;
|
|
113
|
+
};
|
|
114
|
+
/** Gives one page of your library. Required. */
|
|
115
|
+
list(request: MediaLibraryListRequest): Promise<MediaLibraryPage>;
|
|
116
|
+
/** Stores a new file and gives back how your library knows it now. */
|
|
117
|
+
upload?(request: MediaLibraryUploadRequest): Promise<MediaLibraryFile>;
|
|
118
|
+
/** Renames a file. Gives back the file as it is now, or nothing. */
|
|
119
|
+
rename?(request: MediaLibraryRenameRequest): Promise<MediaLibraryFile | void>;
|
|
120
|
+
/** Removes a file from your library. */
|
|
121
|
+
delete?(request: MediaLibraryDeleteRequest): Promise<void>;
|
|
122
|
+
/** See {@link MediaLibraryUrlResolver}. */
|
|
123
|
+
urlResolver?: MediaLibraryUrlResolver;
|
|
124
|
+
}
|
|
125
|
+
/** What `pickFile` is asked for. */
|
|
126
|
+
export interface PickFileRequest {
|
|
127
|
+
/**
|
|
128
|
+
* The file types the field allows, passed on as the field gives them (for example `['image/*']`), and the one
|
|
129
|
+
* pattern that matches every type (an asterisk, a slash and an asterisk: see the README) when the field names none.
|
|
130
|
+
*/
|
|
131
|
+
accept: string[];
|
|
132
|
+
/** Whether more than one file may be chosen. File fields ask for one: `false`. */
|
|
133
|
+
multiple: boolean;
|
|
134
|
+
/** Aborted when the visitor closes the editor or starts another pick. */
|
|
135
|
+
signal: AbortSignal;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Opens your own file picker instead of the built-in File Browser, for the file fields of the composition editor and of
|
|
139
|
+
* the widget creator's settings panel. Resolve the files the visitor chose (each needs an `id` and a `url`; a field
|
|
140
|
+
* takes the first one), or `[]` when the visitor cancelled. Rejecting with an `AbortError` also means cancelled; any other
|
|
141
|
+
* error is written to the console once and counts as nothing picked.
|
|
142
|
+
*/
|
|
143
|
+
export type PickFileFn = (request: PickFileRequest) => Promise<MediaLibraryFile[]>;
|