@inappstory/slide-api 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 InAppStory
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.cjs.d.ts ADDED
@@ -0,0 +1,372 @@
1
+ type WidgetRangeSliderSharedData = Record<string,
2
+ // element_id
3
+ {
4
+ total_value: number;
5
+ total_user: number;
6
+ }> & {
7
+ ts: number; // unixtimestamp
8
+ };
9
+ type WidgetPollSharedData = Record<string,
10
+ // element_id
11
+ {
12
+ 0: number; // answer_yes total count
13
+ 1: number; // answer_no total count
14
+ }> & {
15
+ ts: number; // unixtimestamp
16
+ };
17
+ type WidgetVoteSharedData = Record<string, Array<number> // variant_index total count
18
+ > & {
19
+ ts: number; // unixtimestamp
20
+ };
21
+ type WidgetOptionsBase = {
22
+ slide: HTMLElement | null;
23
+ activateAfterCreate: boolean;
24
+ create: boolean;
25
+ localData: Record<string, any>;
26
+ } & {
27
+ [P in string]: any;
28
+ };
29
+ declare const enum Widgets {
30
+ DataInput = "dataInput",
31
+ Quiz = "quiz",
32
+ QuizGrouped = "quizGrouped",
33
+ RangeSlider = "rangeSlider",
34
+ Poll = "poll",
35
+ PollLayers = "pollLayers",
36
+ Vote = "vote",
37
+ Rate = "rate",
38
+ Test = "test",
39
+ Copy = "copy",
40
+ Share = "share",
41
+ DateCountdown = "dateCountdown",
42
+ Quest = "quest"
43
+ }
44
+ interface WidgetsSharedDataMap {
45
+ rangeSlider: WidgetRangeSliderSharedData;
46
+ poll: WidgetPollSharedData;
47
+ vote: WidgetVoteSharedData;
48
+ }
49
+ interface IAnimation {
50
+ start(slide: HTMLElement, animate?: boolean): void;
51
+ stop(slide: HTMLElement, animate?: boolean): void;
52
+ pause(slide: HTMLElement): void;
53
+ resume(slide: HTMLElement): void;
54
+ reset(slide: HTMLElement): void;
55
+ }
56
+ type APIResponse<DTO> = {
57
+ status: number;
58
+ data: DTO;
59
+ headers?: {
60
+ [key: string]: string;
61
+ };
62
+ requestId: number | string;
63
+ };
64
+ interface SDKApi {
65
+ getStoryServerData(storyId: number): Record<string, any> | null;
66
+ getSlideDuration(storyId: number, slideIndex: number): number | null;
67
+ showNextSlide(duration: number): void;
68
+ sendStatisticEvent(name: string, data: Record<string, string | number>, devPayload?: Record<string, string | number>): void;
69
+ getStoryLocalData(): Promise<Record<string, any>>;
70
+ isExistsShowLayer(): boolean;
71
+ showLayer(index: number): void;
72
+ storyAnimation: IAnimation | undefined;
73
+ isAndroid: boolean;
74
+ isWeb: boolean;
75
+ isIOS: boolean;
76
+ pauseUI(): void;
77
+ resumeUI(): void;
78
+ isExistsShowStoryTextInput: boolean;
79
+ showStoryTextInput(id: string, data: Record<string, any>): void;
80
+ setStoryLocalData(keyValue: Record<string, any>, sendToServer: boolean): void;
81
+ getWidgetsSharedData<WidgetType extends keyof WidgetsSharedDataMap>(storyId: number, widget: Widgets): WidgetsSharedDataMap[WidgetType] | null;
82
+ vibrate(pattern: number): void;
83
+ openUrl(target: string): void;
84
+ sendApiRequest<ResponseDTO>(url: string, method: string, params: Record<string, any> | null, headers: Record<string, any> | null, data: Record<string, any> | null, profilingKey: string): Promise<APIResponse<ResponseDTO>>;
85
+ sendApiRequestSupported(): boolean;
86
+ showToast(text: string): void;
87
+ sdkCanSendShareComplete: boolean;
88
+ isExistsShare: boolean;
89
+ share(id: string, config: {
90
+ url?: string | null;
91
+ text?: string | null;
92
+ title?: string | null;
93
+ files?: Array<{
94
+ file: File;
95
+ name: string;
96
+ type: string;
97
+ }> | null;
98
+ }): void;
99
+ shareSlideScreenshot(shareId: string, hideElementsSelector?: string): void;
100
+ isExistsShowStorySlide: boolean;
101
+ showStorySlide(index: number): void;
102
+ isExistsShowNextStory: boolean;
103
+ storyShowNext(): void;
104
+ }
105
+ declare class WidgetsService {
106
+ private _env;
107
+ private _sdkApi;
108
+ constructor(_env: Window, _sdkApi: SDKApi);
109
+ get env(): Window;
110
+ get sdkApi(): SDKApi;
111
+ }
112
+ declare class WidgetBase<WidgetOptions extends WidgetOptionsBase> {
113
+ /**
114
+ * @see https://github.com/Microsoft/TypeScript/issues/3841#issuecomment-337560146
115
+ */
116
+ ["constructor"]: typeof WidgetBase;
117
+ static DEFAULTS: WidgetOptionsBase;
118
+ static widgetIndex: number;
119
+ protected env: Window;
120
+ protected sdkApi: SDKApi;
121
+ protected options: WidgetOptions;
122
+ protected element: HTMLElement;
123
+ protected elementId: string;
124
+ protected slide: HTMLElement;
125
+ protected slideIndex: number;
126
+ protected disableTimer: boolean;
127
+ protected layoutDirection: "ltr" | "rtl";
128
+ protected storyId: number;
129
+ protected widgetDone: Element | null;
130
+ protected showWidgetCompleteToast: boolean;
131
+ protected submitButtonAnimatedView: HTMLElement | null;
132
+ protected submitButtonView: HTMLElement | null;
133
+ protected submitButtonViewHeight: number;
134
+ protected savedData: Record<string, any> | null;
135
+ protected localData: Record<string, any>;
136
+ protected firstOpenTime: number;
137
+ protected id: string;
138
+ constructor(element: HTMLElement, options: Partial<WidgetOptions>, elementIdGetter?: (element: HTMLElement) => string, slideGetter?: (element: HTMLElement) => HTMLElement);
139
+ refreshUserData(localData: Record<string, any>): void;
140
+ static widgetCacheKey: string;
141
+ static widgetClassName: string;
142
+ static getInstance<Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(element: HTMLElement): Widget | undefined;
143
+ static getInstanceById<Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(id: string): Widget | undefined;
144
+ static createInstance<Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(instantiate: (element: HTMLElement, options: Partial<WidgetOptions>) => Widget, element: HTMLElement, options: Partial<WidgetOptions>): Widget;
145
+ static initWidgets<Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(instantiate: (element: HTMLElement, options: Partial<WidgetOptions>) => Widget, elements: Array<HTMLElement>, localData?: Record<string, any>): Promise<Record<string, any>>;
146
+ static get widgetsService(): WidgetsService;
147
+ static getLocalData(): Promise<Record<string, any>>;
148
+ getLocalData(): Promise<Record<string, any>>;
149
+ static setLocalData(keyValue: Record<string, any>, sendToServer?: boolean, syncWithRuntimeLocalData?: boolean): void;
150
+ setLocalData(keyValue: Record<string, any>, sendToServer?: boolean, syncWithRuntimeLocalData?: boolean): void;
151
+ static sendStatisticEventToApp(name: string, data: Record<string, string | number>, devPayload?: Record<string, string | number>): void;
152
+ sendStatisticEventToApp(name: string, data: Record<string, string | number>, devPayload?: Record<string, string | number>): void;
153
+ showNextSlide(): void;
154
+ protected _showLayout(layers: Array<HTMLElement>, selectIndex: number, withStatEvent?: boolean): void;
155
+ protected _statEventLayoutShow(layoutIndex: number): void;
156
+ static api: {
157
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions_1>, WidgetOptions_1 extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
158
+ };
159
+ }
160
+ interface SDKInterface {
161
+ /**
162
+ * Return object from widgetsData._narrative_saved_data[storyId] or null
163
+ *
164
+ * Possible implementation
165
+ * if ("_narrative_saved_data" in window && window._narrative_saved_data[storyId] != null && isObject(window._narrative_saved_data[storyId])) {
166
+ * return window._narrative_saved_data[storyId];
167
+ * }
168
+ */
169
+ getStoryServerData(storyId: number): Record<string, any> | null;
170
+ /**
171
+ * Return slide duration or null
172
+ *
173
+ * Possible implementation
174
+ * if (
175
+ * "_narrative_slides_duration" in window &&
176
+ * window._narrative_slides_duration[storyId] != null &&
177
+ * isArray(window._narrative_slides_duration[storyId]) &&
178
+ * window._narrative_slides_duration[storyId][slideIndex] != null &&
179
+ * isNumber(window._narrative_slides_duration[storyId][slideIndex])
180
+ * ) {
181
+ * return window._narrative_slides_duration[storyId][slideIndex];
182
+ * }
183
+ */
184
+ getSlideDuration(storyId: number, slideIndex: number): number | null;
185
+ showNextSlide(duration: number): void;
186
+ /**
187
+ * From native sdk, do`t implement in web|react-sdk
188
+ */
189
+ sendStatisticEvent(name: string, data: Record<string, string | number>, devPayload?: Record<string, string | number>): void;
190
+ getStoryLocalData(): Promise<Record<string, any>>;
191
+ setStoryLocalData(keyValue: Record<string, any>, sendToServer: boolean): void;
192
+ showLayer(index: number): void;
193
+ /**
194
+ * Pause timer (story timeline), SlideApi used it on ShowTextInput appear
195
+ */
196
+ pauseUI(): void;
197
+ /**
198
+ * Resume timer (story timeline), SlideApi used it on ShowTextInput disappear
199
+ */
200
+ resumeUI(): void;
201
+ showStoryTextInput(id: string, data: Record<string, any>): void;
202
+ /**
203
+ * Possible implementation
204
+ *
205
+ * getWidgetsSharedData<WidgetType extends keyof WidgetsSharedDataMap>(storyId: number, widget: Widgets): WidgetsSharedDataMap[WidgetType] | null {
206
+ * switch (widget) {
207
+ * case Widgets.RangeSlider: {
208
+ * if ("_narrative_range_slider_data" in window && isObject(window._narrative_range_slider_data[storyId])) {
209
+ * return window._narrative_range_slider_data[storyId] as WidgetsSharedDataMap[WidgetType];
210
+ * }
211
+ * break;
212
+ * }
213
+ * case Widgets.Poll: {
214
+ * if ("_narrative_poll_data" in window && isObject(window._narrative_poll_data[storyId])) {
215
+ * return window._narrative_poll_data[storyId] as WidgetsSharedDataMap[WidgetType];
216
+ * }
217
+ * break;
218
+ * }
219
+ * case Widgets.Vote: {
220
+ * if ("_narrative_vote_data" in window && isObject(window._narrative_vote_data[storyId])) {
221
+ * return window._narrative_vote_data[storyId] as WidgetsSharedDataMap[WidgetType];
222
+ * }
223
+ * break;
224
+ * }
225
+ * }
226
+ *
227
+ * return null;
228
+ * }
229
+ */
230
+ getWidgetsSharedData<WidgetType extends keyof WidgetsSharedDataMap>(storyId: number, widget: Widgets): WidgetsSharedDataMap[WidgetType] | null;
231
+ /**
232
+ * Possible implementation
233
+ *
234
+ * if ("_showNarrativeLink" in window) {
235
+ * window._showNarrativeLink(target);
236
+ * }
237
+ */
238
+ openUrl(target: string): void;
239
+ /**
240
+ * Possible implementation
241
+ *
242
+ * return window._sendApiRequest(url, method, params, headers, data, profilingKey);
243
+ */
244
+ sendApiRequest<ResponseDTO>(url: string, method: string, params: Record<string, any> | null, headers: Record<string, any> | null, data: Record<string, any> | null, profilingKey: string): Promise<APIResponse<ResponseDTO>>;
245
+ /**
246
+ * Possible implementation
247
+ *
248
+ * window._share(id, config);
249
+ */
250
+ share(id: string, config: {
251
+ url?: string | null;
252
+ text?: string | null;
253
+ title?: string | null;
254
+ files?: Array<{
255
+ file: File;
256
+ name: string;
257
+ type: string;
258
+ }> | null;
259
+ }): void;
260
+ /**
261
+ * Possible implementation
262
+ *
263
+ * window.share_slide_screenshot(shareId, hideElementsSelector);
264
+ */
265
+ shareSlideScreenshot(shareId: string, hideElementsSelector?: string): void;
266
+ /**
267
+ * Possible implementation
268
+ *
269
+ * window._showNarrativeSlide(index);
270
+ */
271
+ showStorySlide(index: number): void;
272
+ /**
273
+ * Possible implementation
274
+ *
275
+ * window._storyShowNext();
276
+ */
277
+ storyShowNext(): void;
278
+ }
279
+ declare let sdkInterface: SDKInterface;
280
+ declare const getSlideApi: (_sdkInterface: SDKInterface) => {
281
+ Animation: IAnimation;
282
+ WidgetCopy: {
283
+ initWidget: (nodeList: NodeListOf<HTMLElement>, localData?: Record<string, any> | undefined) => void;
284
+ click: (element: HTMLElement) => boolean;
285
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
286
+ };
287
+ WidgetDataInput: {
288
+ initWidget: (nodeList: NodeListOf<HTMLElement>, localData?: Record<string, any> | undefined) => void;
289
+ click: (element: HTMLElement) => boolean;
290
+ setUserData: (id: string, text: string) => void;
291
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
292
+ };
293
+ WidgetDateCountdown: {
294
+ initWidget: (nodeList: NodeListOf<HTMLElement>, layers: HTMLElement[], localData?: Record<string, any> | undefined) => void;
295
+ pause: (nodeList: NodeListOf<HTMLElement>) => void;
296
+ resume: (nodeList: NodeListOf<HTMLElement>) => void;
297
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
298
+ };
299
+ WidgetMultiSlide: {
300
+ init: (slides: HTMLElement[], localData?: Record<string, any> | undefined) => void;
301
+ };
302
+ WidgetPoll: {
303
+ initWidget: (element: HTMLElement, localData?: Record<string, any> | undefined) => void;
304
+ select: (element: HTMLElement) => boolean;
305
+ slidePollIsDone: (element: HTMLElement) => boolean;
306
+ setUserData: (id: string, text: string) => void;
307
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
308
+ };
309
+ WidgetPollLayers: {
310
+ initWidget: (element: HTMLElement, layers: HTMLElement[], localData?: Record<string, any> | undefined) => void;
311
+ select: (element: HTMLElement) => boolean;
312
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
313
+ };
314
+ WidgetQuest: {
315
+ initWidget: (element: HTMLElement, localData?: Record<string, any> | undefined) => Promise<boolean>;
316
+ select: (element: HTMLElement) => boolean;
317
+ slideTestIsDone: (element: HTMLElement) => boolean;
318
+ handleRouteClick: (element: HTMLElement, event: {
319
+ direction: "forward" | "backward";
320
+ slideIndex: number;
321
+ slideCount: number;
322
+ click: () => void;
323
+ }) => void;
324
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
325
+ };
326
+ WidgetQuiz: {
327
+ initWidget: (element: HTMLElement, localData?: Record<string, any> | undefined) => void;
328
+ select: (element: HTMLElement) => boolean;
329
+ slideQuizIsDone: (element: HTMLElement) => boolean;
330
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
331
+ };
332
+ WidgetQuizGrouped: {
333
+ initWidget: (element: HTMLElement, localData?: Record<string, any> | undefined) => void;
334
+ select: (element: HTMLElement) => boolean;
335
+ slideQuizIsDone: (element: HTMLElement) => boolean;
336
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
337
+ };
338
+ WidgetRangeSlider: {
339
+ initWidget: (element: HTMLElement, localData?: Record<string, any> | undefined) => void;
340
+ click: (element: HTMLElement) => boolean;
341
+ isClickCapturedBySlider: (element: HTMLElement) => boolean;
342
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
343
+ };
344
+ WidgetRate: {
345
+ initWidget: (nodeList: NodeListOf<HTMLElement>, localData?: Record<string, any> | undefined) => void;
346
+ select: (element: HTMLElement) => boolean;
347
+ slidePollIsDone: (element: HTMLElement) => boolean;
348
+ setUserData: (id: string, text: string) => void;
349
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
350
+ };
351
+ WidgetShare: {
352
+ initWidget: (nodeList: NodeListOf<HTMLElement>, layers: HTMLElement[], localData?: Record<string, any> | undefined) => void;
353
+ click: (element: HTMLElement) => void;
354
+ complete: (id: string, isSuccess: boolean) => void;
355
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
356
+ };
357
+ WidgetTest: {
358
+ initWidget: (element: HTMLElement, localData?: Record<string, any> | undefined) => void;
359
+ select: (element: HTMLElement) => boolean;
360
+ slideTestIsDone: (element: HTMLElement) => boolean;
361
+ slideTestWithTimer: (element: HTMLElement) => boolean;
362
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
363
+ };
364
+ WidgetVote: {
365
+ fallbackInitOnMultiSlide: (element: HTMLElement, localData?: Record<string, any> | undefined) => void;
366
+ initWidget: (element: HTMLElement, localData?: Record<string, any> | undefined) => void;
367
+ select: (element: HTMLElement) => boolean;
368
+ slideVoteIsDone: (element: HTMLElement) => boolean;
369
+ refreshUserData: <Widget extends WidgetBase<WidgetOptions>, WidgetOptions extends WidgetOptionsBase>(nodes: NodeListOf<HTMLElement>, localData: Record<string, any>) => void;
370
+ };
371
+ };
372
+ export { sdkInterface, getSlideApi };