@canva/platform 2.1.0 → 2.1.1-beta.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/beta.d.ts ADDED
@@ -0,0 +1,724 @@
1
+ /**
2
+ * @public
3
+ * Provides methods for interacting with an app process.
4
+ */
5
+ export declare interface AppProcess {
6
+ /**
7
+ * The current app process.
8
+ */
9
+ readonly current: CurrentAppProcess;
10
+ /**
11
+ * @public
12
+ * Requests the termination of the specified app process.
13
+ *
14
+ * @param target - The ID of an app process.
15
+ * @param params - Parameters to pass to the `setOnDispose` callback. Any kind of structured data can be passed via this property.
16
+ *
17
+ * @remarks
18
+ * Once called, this method:
19
+ *
20
+ * 1. Transitions the state of the process to `"closing"`.
21
+ * 2. Invokes all registered `setOnDispose` callbacks.
22
+ * 3. Waits for the process to finish closing.
23
+ * 4. Transitions the state of the process to `"closed"`.
24
+ *
25
+ * Each time the state changes, all of the `registerOnStateChange` callbacks are called.
26
+ *
27
+ * @example Close a process
28
+ * ```typescript
29
+ * import { appProcess, type AppProcessId } from '@canva/platform';
30
+ *
31
+ * const placeholderProcessId = "PLACEHOLDER_PROCESS_ID" as AppProcessId;
32
+ *
33
+ * await appProcess.requestClose(placeholderProcessId, { reason: 'completed' });
34
+ * ```
35
+ *
36
+ * @example Pass structured data to a process as it closes
37
+ * ```typescript
38
+ * import { appProcess, type AppProcessId, type CloseParams } from '@canva/platform';
39
+ *
40
+ * type DetailedCloseParams = CloseParams & {
41
+ * savePoint: string;
42
+ * timestamp: number;
43
+ * userInitiated: boolean;
44
+ * };
45
+ *
46
+ * const placeholderProcessId = "PLACEHOLDER_PROCESS_ID" as AppProcessId;
47
+ *
48
+ * await appProcess.requestClose<DetailedCloseParams>(placeholderProcessId, {
49
+ * reason: 'completed',
50
+ * savePoint: 'auto_backup_1',
51
+ * timestamp: Date.now(),
52
+ * userInitiated: true
53
+ * });
54
+ * ```
55
+ */
56
+ requestClose<T extends CloseParams>(
57
+ target: AppProcessId,
58
+ params: T,
59
+ ): Promise<void>;
60
+ /**
61
+ * @public
62
+ * Registers a callback that runs when the state of the specified app process changes.
63
+ *
64
+ * @param target - The ID of an app process.
65
+ * @param callback - The callback to run when the state of the process changes.
66
+ *
67
+ * @returns
68
+ * A disposer function that cleans up the registered callback.
69
+ *
70
+ * @example Listen for process state changes
71
+ * ```typescript
72
+ * import { appProcess } from '@canva/platform';
73
+ *
74
+ * const stateDisposer = appProcess.registerOnStateChange(
75
+ * processId,
76
+ * ({ state }) => {
77
+ * switch (state) {
78
+ * case 'opening':
79
+ * // Process is starting up
80
+ * break;
81
+ * case 'open':
82
+ * // Process is active and visible
83
+ * break;
84
+ * case 'closing':
85
+ * // Process is about to close
86
+ * // Save state, cleanup resources
87
+ * break;
88
+ * case 'closed':
89
+ * // Process has been terminated
90
+ * // Final cleanup if needed
91
+ * break;
92
+ * }
93
+ * }
94
+ * );
95
+ *
96
+ * // Later: cleanup the listener
97
+ * await stateDisposer();
98
+ * ```
99
+ */
100
+ registerOnStateChange(
101
+ target: AppProcessId,
102
+ callback: OnStateChangeCallback,
103
+ ): () => Promise<void>;
104
+ /**
105
+ * @public
106
+ * Registers a callback that listens for broadcasted messages.
107
+ *
108
+ * @param callback - The callback that listens for broadcasted messages.
109
+ *
110
+ * @returns
111
+ * A disposer function that cleans up the registered callback.
112
+ *
113
+ * @example Listen for inter-process messages
114
+ * ```typescript
115
+ * import { appProcess } from '@canva/platform';
116
+ *
117
+ * const messageDisposer = appProcess.registerOnMessage(async (sender, message) => {
118
+ * const { appProcessId, surface } = sender;
119
+ * // Handle message from other process
120
+ * });
121
+ *
122
+ * // Later: cleanup the listener
123
+ * await messageDisposer();
124
+ * ```
125
+ */
126
+ registerOnMessage(callback: OnMessageCallback): () => Promise<void>;
127
+ /**
128
+ * @public
129
+ * Broadcasts a message to all of the app's active processes, not including the current process.
130
+ *
131
+ * @param message - The message to be broadcasted. This can be any kind of structured data.
132
+ *
133
+ * @example Broadcast primitive values
134
+ * ```typescript
135
+ * import { appProcess } from '@canva/platform';
136
+ *
137
+ * // Broadcasting a string
138
+ * appProcess.broadcastMessage('REFRESH_REQUESTED');
139
+ *
140
+ * // Broadcasting a number
141
+ * appProcess.broadcastMessage(42);
142
+ *
143
+ * // Broadcasting a boolean
144
+ * appProcess.broadcastMessage(true);
145
+ * ```
146
+ *
147
+ * @example Broadcast simple objects
148
+ * ```typescript
149
+ * import { appProcess } from '@canva/platform';
150
+ *
151
+ * appProcess.broadcastMessage({
152
+ * id: 'user-123',
153
+ * name: 'John Doe',
154
+ * active: true
155
+ * });
156
+ * ```
157
+ *
158
+ * @example Broadcast complex objects
159
+ * ```typescript
160
+ * import { appProcess } from '@canva/platform';
161
+ *
162
+ * appProcess.broadcastMessage({
163
+ * type: 'DOCUMENT_UPDATE',
164
+ * timestamp: Date.now(),
165
+ * payload: {
166
+ * documentId: 'doc-123',
167
+ * version: 2,
168
+ * metadata: {
169
+ * title: 'Project Alpha',
170
+ * tags: ['draft', 'review-needed'],
171
+ * collaborators: [
172
+ * { id: 'user-1', role: 'editor' },
173
+ * { id: 'user-2', role: 'viewer' }
174
+ * ]
175
+ * }
176
+ * }
177
+ * });
178
+ * ```
179
+ */
180
+ broadcastMessage(message: any): void;
181
+ }
182
+
183
+ /**
184
+ * @public
185
+ * Provides methods for interacting with app processes.
186
+ */
187
+ export declare const appProcess: AppProcess;
188
+
189
+ /**
190
+ * @public
191
+ * The unique identifier of an app process.
192
+ */
193
+ export declare type AppProcessId = string & {
194
+ __appProcessId: never;
195
+ };
196
+
197
+ /**
198
+ * @public
199
+ * Information about an app process.
200
+ */
201
+ export declare type AppProcessInfo<T> = {
202
+ /**
203
+ * The surface on which the app process is running.
204
+ */
205
+ surface: AppSurface;
206
+ /**
207
+ * The unique identifier of the app process.
208
+ */
209
+ processId: AppProcessId;
210
+ /**
211
+ * Parameters passed to the app process when it was opened.
212
+ */
213
+ launchParams?: T;
214
+ };
215
+
216
+ /**
217
+ * @public
218
+ * The type of surface on which an app process can run.
219
+ *
220
+ * @remarks
221
+ * The possible surfaces include:
222
+ *
223
+ * - `"headless"` - A surface for when there is no visible user interface.
224
+ * - `"object_panel"` - A surface that renders a user interface in the side panel of the Canva editor.
225
+ * - `"selected_image_overlay"` - A surface that can be opened on top of a selected image.
226
+ */
227
+ export declare type AppSurface =
228
+ | "object_panel"
229
+ | "selected_image_overlay"
230
+ | "headless";
231
+
232
+ /**
233
+ * @public
234
+ * Parameters passed to the `setOnDispose` callback when a process is about to close.
235
+ */
236
+ export declare type CloseParams = {
237
+ /**
238
+ * The reason the app process is closing.
239
+ */
240
+ reason: CloseReason;
241
+ };
242
+
243
+ /**
244
+ * @public
245
+ * The reason why an app process is closing.
246
+ *
247
+ * @remarks
248
+ * The possible reasons include:
249
+ *
250
+ * - `"completed"` - Indicates that a workflow has been completed and unsaved changes should be saved.
251
+ * - `"aborted"` - Indicates that a workflow has been abandoned and unsaved changes should be discarded.
252
+ */
253
+ export declare type CloseReason = "completed" | "aborted";
254
+
255
+ /**
256
+ * @public
257
+ * Provides methods for interacting with the current app process.
258
+ */
259
+ export declare type CurrentAppProcess = {
260
+ /**
261
+ * @public
262
+ * Returns information about the current app process.
263
+ *
264
+ * @example Get current process information
265
+ * ```typescript
266
+ * import { appProcess } from '@canva/platform';
267
+ *
268
+ * const currentProcess = appProcess.current;
269
+ * const processInfo = currentProcess.getInfo();
270
+ * ```
271
+ *
272
+ * @example Check current process surface type
273
+ * ```typescript
274
+ * import { appProcess } from '@canva/platform';
275
+ *
276
+ * const currentProcess = appProcess.current;
277
+ * const { surface } = currentProcess.getInfo();
278
+ *
279
+ * if (surface === 'object_panel') {
280
+ * // This app is running in the object panel
281
+ * }
282
+ * ```
283
+ *
284
+ * @example Read current process launch parameters
285
+ * ```typescript
286
+ * import { appProcess } from '@canva/platform';
287
+ *
288
+ * type MyLaunchParams ={
289
+ * mode: 'edit' | 'view';
290
+ * id: string;
291
+ * }
292
+ *
293
+ * const currentProcess = appProcess.current;
294
+ * const { launchParams } = currentProcess.getInfo<MyLaunchParams>();
295
+ *
296
+ * if (launchParams) {
297
+ * const { mode, id } = launchParams;
298
+ * // Use launch parameters
299
+ * }
300
+ * ```
301
+ */
302
+ getInfo<T>(): AppProcessInfo<T>;
303
+ /**
304
+ * @public
305
+ * Requests the termination of the current process.
306
+ *
307
+ * @param params - Parameters to pass to the `setOnDispose` callback. Any structured data can be passed via this property.
308
+ *
309
+ * @remarks
310
+ * Once called, this method:
311
+ *
312
+ * 1. Transitions the state of the process to `"closing"`.
313
+ * 2. Invokes all registered `setOnDispose` callbacks.
314
+ * 3. Waits for the process to finish closing.
315
+ * 4. Transitions the state of the process to `"closed"`.
316
+ *
317
+ * Each time the state changes, all of the `registerOnStateChange` callbacks are called.
318
+ *
319
+ * @example Close current process
320
+ * ```typescript
321
+ * import { appProcess } from '@canva/platform';
322
+ *
323
+ * await appProcess.current.requestClose({ reason: 'completed' });
324
+ * ```
325
+ *
326
+ * @example Pass structured data to current process as it closes
327
+ * ```typescript
328
+ * import { appProcess, type CloseParams } from '@canva/platform';
329
+ *
330
+ * type DetailedCloseParams = CloseParams & {
331
+ * metadata: {
332
+ * savePoint: string;
333
+ * timestamp: number;
334
+ * userInitiated: boolean;
335
+ * }
336
+ * };
337
+ *
338
+ * await appProcess.current.requestClose<DetailedCloseParams>({
339
+ * reason: 'completed',
340
+ * metadata: {
341
+ * savePoint: 'auto_backup_1',
342
+ * timestamp: Date.now(),
343
+ * userInitiated: true
344
+ * }
345
+ * });
346
+ * ```
347
+ */
348
+ requestClose<T extends CloseParams>(params: T): Promise<void>;
349
+ /**
350
+ * @public
351
+ * Registers a callback that runs when the current app process is about to close.
352
+ *
353
+ * @param callback - The callback to run when the current app process is about to close.
354
+ *
355
+ * @returns
356
+ * A disposer function that cleans up the registered callback.
357
+ *
358
+ * @remarks
359
+ * - Apps can't register multiple callbacks.
360
+ * - If an app attempts to register multiple callbacks, only the last callback will be registered.
361
+ * - The app process will remain open until the callback resolves or a timeout error occurs.
362
+ * - The complete execution of the callback is not guaranteed as some user actions (e.g. closing tabs) may close the process prematurely.
363
+ *
364
+ * @example Handle process cleanup
365
+ * ```typescript
366
+ * import { appProcess } from '@canva/platform';
367
+ *
368
+ * const cleanupDisposer = appProcess.current.setOnDispose(async (params) => {
369
+ * if (params.reason === 'completed') {
370
+ * await saveChanges();
371
+ * }
372
+ * });
373
+ *
374
+ * // Later: cleanup the listener
375
+ * await cleanupDisposer();
376
+ * ```
377
+ */
378
+ setOnDispose<T extends CloseParams>(
379
+ callback: OnDisposeCallback<T>,
380
+ ): () => Promise<void>;
381
+ };
382
+
383
+ /**
384
+ * @public
385
+ * Disposes an event listener.
386
+ */
387
+ declare type Disposer = () => void;
388
+
389
+ /**
390
+ * @public
391
+ * An SDK method that can be inspected for feature support.
392
+ */
393
+ export declare type Feature = (...args: any[]) => unknown;
394
+
395
+ /**
396
+ * @public
397
+ * Provides methods for checking if a feature is supported.
398
+ */
399
+ export declare const features: FeatureSupport;
400
+
401
+ /**
402
+ * @public
403
+ * Provides methods for checking if an SDK method is supported in the current context.
404
+ */
405
+ export declare interface FeatureSupport {
406
+ /**
407
+ * @public
408
+ * Checks if the specified SDK methods are supported in the current context.
409
+ *
410
+ * @param features - The SDK methods to be checked for support.
411
+ *
412
+ * @example Checking a single feature
413
+ * ```typescript
414
+ * import { features } from '@canva/platform';
415
+ * import { addElementAtPoint } from '@canva/design';
416
+ *
417
+ * const isSupported = features.isSupported(addElementAtPoint);
418
+ * ```
419
+ *
420
+ * @example Checking multiple features
421
+ * ```typescript
422
+ * import { features } from '@canva/platform';
423
+ * import { addElementAtPoint, addElementAtCursor } from '@canva/design';
424
+ *
425
+ * const areSupported = features.isSupported(addElementAtPoint, addElementAtCursor);
426
+ * ```
427
+ */
428
+ isSupported(...features: Feature[]): boolean;
429
+ /**
430
+ * @public
431
+ * Registers a callback that runs when the context changes and an SDK method becomes supported or unsupported.
432
+ *
433
+ * @param onSupportChange - The callback that runs when the support status of an SDK method changes.
434
+ *
435
+ * @example Monitoring feature support changes
436
+ * ```typescript
437
+ * import { features } from '@canva/platform';
438
+ * import { addElementAtPoint } from '@canva/design';
439
+ *
440
+ * const supportDisposer = features.registerOnSupportChange(() => {
441
+ * const isNowSupported = features.isSupported(addElementAtPoint);
442
+ * // Update UI based on new support status
443
+ * });
444
+ *
445
+ * // Later: cleanup the listener
446
+ * await supportDisposer();
447
+ * ```
448
+ */
449
+ registerOnSupportChange(onSupportChange: () => void): Disposer;
450
+ }
451
+
452
+ /**
453
+ * @public
454
+ * Returns information about the platform on which the app is running.
455
+ *
456
+ * @example Get platform information
457
+ * ```typescript
458
+ * import { getPlatformInfo } from '@canva/platform';
459
+ *
460
+ * const platformInfo = await getPlatformInfo();
461
+ * ```
462
+ *
463
+ * @example Check if app is running on platform that allows payments
464
+ * ```typescript
465
+ * import { getPlatformInfo } from '@canva/platform';
466
+ *
467
+ * const platformInfo = await getPlatformInfo();
468
+ *
469
+ * if (platformInfo.canAcceptPayments) {
470
+ * // Show payment-related UI elements
471
+ * } else {
472
+ * // Hide payment-related UI elements
473
+ * }
474
+ * ```
475
+ */
476
+ export declare const getPlatformInfo: () => PlatformInfo;
477
+
478
+ /**
479
+ * @beta
480
+ * Provides methods for interacting with notifications.
481
+ */
482
+ export declare const notification: NotificationClient;
483
+
484
+ /**
485
+ * @beta
486
+ *
487
+ * Provides methods for interacting with notifications.
488
+ */
489
+ export declare interface NotificationClient {
490
+ /**
491
+ * @beta
492
+ *
493
+ * A method that shows a toast notification to the user.
494
+ *
495
+ * @example
496
+ * ```tsx
497
+ * import { notification } from '@canva/platform';
498
+ * import type { ToastRequest } from '@canva/platform';
499
+ *
500
+ * const showToast = () => {
501
+ * const request: ToastRequest = {
502
+ * messageText: "Hello world!",
503
+ * };
504
+ * notification.addToast(request);
505
+ * };
506
+ *
507
+ * <Button onClick={() => showToast()}>Show Toast</Button>
508
+ * ```
509
+ */
510
+ addToast: (request: ToastRequest) => Promise<ToastResponse>;
511
+ }
512
+
513
+ /**
514
+ * @public
515
+ * A callback that runs when an app process is about to close.
516
+ * @param opts - Parameters passed to the `setOnDispose` callback when a process is about to close.
517
+ */
518
+ export declare type OnDisposeCallback<T extends CloseParams> = (
519
+ opts: T,
520
+ ) => Promise<void>;
521
+
522
+ /**
523
+ * @public
524
+ * A callback that runs when an app process receives a broadcasted message.
525
+ *
526
+ * @param sender - Information about the process that sent the message.
527
+ * - sender.appProcessId - The ID of the process that sent the message.
528
+ * - sender.surface - The surface of the process that sent the message.
529
+ * @param message - The broadcasted message.
530
+ */
531
+ export declare type OnMessageCallback = (
532
+ sender: {
533
+ appProcessId: AppProcessId;
534
+ surface: AppSurface;
535
+ },
536
+ message: any,
537
+ ) => Promise<void>;
538
+
539
+ /**
540
+ * @public
541
+ * A callback that runs when the state of a process changes.
542
+ *
543
+ * @param opts - Information about the state change.
544
+ * - opts.state - The state of the process.
545
+ */
546
+ export declare type OnStateChangeCallback = (opts: {
547
+ state: ProcessState;
548
+ }) => void;
549
+
550
+ /**
551
+ * @public
552
+ * The result when a user doesn't agree to navigate to an external URL.
553
+ */
554
+ export declare type OpenExternalUrlAborted = {
555
+ /**
556
+ * The status of the request.
557
+ */
558
+ status: "aborted";
559
+ };
560
+
561
+ /**
562
+ * @public
563
+ * The result when a user agrees to navigate to an external URL.
564
+ */
565
+ export declare type OpenExternalUrlCompleted = {
566
+ /**
567
+ * The status of the request.
568
+ */
569
+ status: "completed";
570
+ };
571
+
572
+ /**
573
+ * @public
574
+ * Options for prompting the user to open an external URL.
575
+ */
576
+ export declare type OpenExternalUrlRequest = {
577
+ /**
578
+ * The URL to open.
579
+ */
580
+ url: string;
581
+ };
582
+
583
+ /**
584
+ * @public
585
+ * The result of prompting the user to open an external URL.
586
+ */
587
+ export declare type OpenExternalUrlResponse =
588
+ | OpenExternalUrlCompleted
589
+ | OpenExternalUrlAborted;
590
+
591
+ /**
592
+ * @public
593
+ * Information about the platform on which the app is running.
594
+ */
595
+ export declare type PlatformInfo = {
596
+ /**
597
+ * If `true`, the app is allowed to directly link to payment and upgrade flows.
598
+ *
599
+ * @remarks
600
+ * This property is always `true` when the app is running in a web browser, but may otherwise be `false` in
601
+ * order to comply with the policies of the platforms on which Canva is available. For example, some platforms
602
+ * only allow payment-related actions that use their own payment mechanisms and apps are therefore not allowed
603
+ * to render payment-related call-to-actions while running on those platforms.
604
+ *
605
+ * @example
606
+ * ```ts
607
+ * const info = getPlatformInfo();
608
+ *
609
+ * if (info.canAcceptPayments) {
610
+ * // Display payment links and upgrade flows
611
+ * } else {
612
+ * // Hide payment links and upgrade flows
613
+ * // Optionally, show an appropriate message
614
+ * }
615
+ * ```
616
+ */
617
+ canAcceptPayments: boolean;
618
+ };
619
+
620
+ /**
621
+ * @public
622
+ * The state of an app process.
623
+ *
624
+ * @remarks
625
+ * The possible states include:
626
+ *
627
+ * - `"opening"` - The app process is opening.
628
+ * - `"open"` - The app process is open, active, and visible on the designated surface.
629
+ * - `"closing"` - The app process is closing.
630
+ * - `"closed"` - The app process has been closed and is no longer active.
631
+ *
632
+ * While a process is closing, it won't receive any events or messages from other processes.
633
+ */
634
+ export declare type ProcessState = "opening" | "open" | "closing" | "closed";
635
+
636
+ /**
637
+ * @public
638
+ * Opens an external URL.
639
+ *
640
+ * @remarks
641
+ * The URL is opened natively, such as in a new browser tab on desktop or in a browser sheet on mobile.
642
+ *
643
+ * In some browsers, the user must enable popup permissions before any URL can be opened.
644
+ *
645
+ * @example Open an external URL
646
+ * ```typescript
647
+ * import { requestOpenExternalUrl } from '@canva/platform';
648
+ *
649
+ * await requestOpenExternalUrl({
650
+ * url: 'https://www.example.com',
651
+ * });
652
+ * ```
653
+ *
654
+ * @example Detect when a user navigates to the external URL
655
+ * ```typescript
656
+ * import { requestOpenExternalUrl } from '@canva/platform';
657
+ *
658
+ * const response = await requestOpenExternalUrl({
659
+ * url: 'https://www.example.com',
660
+ * });
661
+ *
662
+ * if (response.status === 'completed') {
663
+ * // URL opened successfully
664
+ * }
665
+ * ```
666
+ *
667
+ * @example Detect when a user doesn't navigate to the external URL
668
+ * ```typescript
669
+ * import { requestOpenExternalUrl } from '@canva/platform';
670
+ *
671
+ * const response = await requestOpenExternalUrl({
672
+ * url: 'https://www.example.com',
673
+ * });
674
+ *
675
+ * if (response.status === 'aborted') {
676
+ * // User declined to open URL
677
+ * }
678
+ * ```
679
+ */
680
+ export declare const requestOpenExternalUrl: (
681
+ request: OpenExternalUrlRequest,
682
+ ) => Promise<OpenExternalUrlResponse>;
683
+
684
+ /**
685
+ * @beta
686
+ * The result when a toast notification is successfully added.
687
+ */
688
+ export declare type ToastCompleted = {
689
+ /**
690
+ * The status of the request.
691
+ */
692
+ status: "completed";
693
+ };
694
+
695
+ /**
696
+ * @beta
697
+ *
698
+ * Options for configuring a toast notification.
699
+ */
700
+ export declare type ToastRequest = {
701
+ /**
702
+ * Text to show within the toast notification.
703
+ */
704
+ messageText: string;
705
+ /**
706
+ * The duration that the notification will be visible.
707
+ *
708
+ * If set to `"infinite"`, the notification will be displayed until manually dismissed by the user.
709
+ *
710
+ * If set to a number, the notification will automatically disappear after that duration (in milliseconds).
711
+ *
712
+ * @defaultValue 5000
713
+ */
714
+ timeoutMs?: number | "infinite";
715
+ };
716
+
717
+ /**
718
+ * @beta
719
+ *
720
+ * The response from adding a toast notification.
721
+ */
722
+ export declare type ToastResponse = ToastCompleted;
723
+
724
+ export {};
@@ -2,6 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
+ Object.defineProperty(exports, "notification", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return notification;
9
+ }
10
+ });
5
11
  _export_star(require("./public"), exports);
6
12
  function _export_star(from, to) {
7
13
  Object.keys(from).forEach(function(k) {
@@ -17,4 +23,6 @@ function _export_star(from, to) {
17
23
  return from;
18
24
  }
19
25
  var _window___canva___sdkRegistration, _window___canva__;
20
- (_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : (_window___canva___sdkRegistration = _window___canva__.sdkRegistration) === null || _window___canva___sdkRegistration === void 0 ? void 0 : _window___canva___sdkRegistration.registerPackageVersion('platform', '2.1.0', 'ga');
26
+ (_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : (_window___canva___sdkRegistration = _window___canva__.sdkRegistration) === null || _window___canva___sdkRegistration === void 0 ? void 0 : _window___canva___sdkRegistration.registerPackageVersion('platform', '2.1.1', 'beta');
27
+ const { canva_sdk } = window;
28
+ const notification = canva_sdk.platform.v2.notification;
@@ -10,11 +10,13 @@ Object.defineProperty(exports, "createFakePlatformClients", {
10
10
  });
11
11
  const _fake_app_process_client = require("./fake_app_process_client");
12
12
  const _fake_feature_support_client = require("./fake_feature_support_client");
13
+ const _fake_notification_client = require("./fake_notification_client");
13
14
  const _fake_platform_client = require("./fake_platform_client");
14
15
  function createFakePlatformClients() {
15
16
  const appProcess = new _fake_app_process_client.FakeAppProcessClient();
16
17
  const platform = new _fake_platform_client.FakePlatformClient();
17
18
  const features = new _fake_feature_support_client.FakeFeatureSupportClient();
19
+ const notification = new _fake_notification_client.FakeNotificationClient();
18
20
  const i18n = undefined;
19
21
  return {
20
22
  platform: {
@@ -22,7 +24,8 @@ function createFakePlatformClients() {
22
24
  platform,
23
25
  appProcess,
24
26
  i18n,
25
- features
27
+ features,
28
+ notification
26
29
  }
27
30
  }
28
31
  };
@@ -0,0 +1,17 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "FakeNotificationClient", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return FakeNotificationClient;
9
+ }
10
+ });
11
+ class FakeNotificationClient {
12
+ async addToast(options) {
13
+ return {
14
+ status: 'completed'
15
+ };
16
+ }
17
+ }
@@ -15,11 +15,11 @@ _export(exports, {
15
15
  features: function() {
16
16
  return features;
17
17
  },
18
- requestOpenExternalUrl: function() {
19
- return requestOpenExternalUrl;
20
- },
21
18
  getPlatformInfo: function() {
22
19
  return getPlatformInfo;
20
+ },
21
+ requestOpenExternalUrl: function() {
22
+ return requestOpenExternalUrl;
23
23
  }
24
24
  });
25
25
  const { canva_sdk } = window;
@@ -0,0 +1,18 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./index"), exports);
6
+ function _export_star(from, to) {
7
+ Object.keys(from).forEach(function(k) {
8
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
+ Object.defineProperty(to, k, {
10
+ enumerable: true,
11
+ get: function() {
12
+ return from[k];
13
+ }
14
+ });
15
+ }
16
+ });
17
+ return from;
18
+ }
@@ -9,12 +9,15 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
- getCanvaSdk: function() {
13
- return getCanvaSdk;
14
- },
15
12
  assertIsTestCanvaSdk: function() {
16
13
  return assertIsTestCanvaSdk;
17
14
  },
15
+ bindMethodsToClients: function() {
16
+ return bindMethodsToClients;
17
+ },
18
+ getCanvaSdk: function() {
19
+ return getCanvaSdk;
20
+ },
18
21
  injectFakeAPIClients: function() {
19
22
  return injectFakeAPIClients;
20
23
  }
@@ -34,8 +37,29 @@ function assertIsTestCanvaSdk() {
34
37
  }
35
38
  }
36
39
  function injectFakeAPIClients(clients) {
40
+ bindMethodsToClients(clients);
37
41
  window.canva_sdk = {
38
42
  ...getCanvaSdk(),
39
43
  ...clients
40
44
  };
41
45
  }
46
+ function bindMethodsToClients(objectToBind) {
47
+ if (typeof objectToBind !== 'object' || objectToBind == null)
48
+ return;
49
+ const classMethods = new Set();
50
+ let currentPrototype = Object.getPrototypeOf(objectToBind);
51
+ while(currentPrototype && currentPrototype !== Object.prototype){
52
+ Object.getOwnPropertyNames(currentPrototype).forEach((method)=>classMethods.add(method));
53
+ currentPrototype = Object.getPrototypeOf(currentPrototype);
54
+ }
55
+ classMethods.delete('constructor');
56
+ for (const method of classMethods) {
57
+ const originalFn = objectToBind[method];
58
+ if (typeof originalFn === 'function') Object.defineProperty(objectToBind, method, {
59
+ value: (...args)=>{
60
+ return originalFn.call(objectToBind, ...args);
61
+ }
62
+ });
63
+ }
64
+ Object.values(objectToBind).forEach(bindMethodsToClients);
65
+ }
@@ -1,3 +1,5 @@
1
1
  var _window___canva___sdkRegistration, _window___canva__;
2
2
  export * from './public';
3
- (_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : (_window___canva___sdkRegistration = _window___canva__.sdkRegistration) === null || _window___canva___sdkRegistration === void 0 ? void 0 : _window___canva___sdkRegistration.registerPackageVersion('platform', '2.1.0', 'ga');
3
+ (_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : (_window___canva___sdkRegistration = _window___canva__.sdkRegistration) === null || _window___canva___sdkRegistration === void 0 ? void 0 : _window___canva___sdkRegistration.registerPackageVersion('platform', '2.1.1', 'beta');
4
+ const { canva_sdk } = window;
5
+ export const notification = canva_sdk.platform.v2.notification;
@@ -1,10 +1,12 @@
1
1
  import { FakeAppProcessClient } from './fake_app_process_client';
2
2
  import { FakeFeatureSupportClient } from './fake_feature_support_client';
3
+ import { FakeNotificationClient } from './fake_notification_client';
3
4
  import { FakePlatformClient } from './fake_platform_client';
4
5
  export function createFakePlatformClients() {
5
6
  const appProcess = new FakeAppProcessClient();
6
7
  const platform = new FakePlatformClient();
7
8
  const features = new FakeFeatureSupportClient();
9
+ const notification = new FakeNotificationClient();
8
10
  const i18n = undefined;
9
11
  return {
10
12
  platform: {
@@ -12,7 +14,8 @@ export function createFakePlatformClients() {
12
14
  platform,
13
15
  appProcess,
14
16
  i18n,
15
- features
17
+ features,
18
+ notification
16
19
  }
17
20
  }
18
21
  };
@@ -0,0 +1,7 @@
1
+ export class FakeNotificationClient {
2
+ async addToast(options) {
3
+ return {
4
+ status: 'completed'
5
+ };
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ export * from './index';
@@ -13,8 +13,29 @@ export function assertIsTestCanvaSdk() {
13
13
  }
14
14
  }
15
15
  export function injectFakeAPIClients(clients) {
16
+ bindMethodsToClients(clients);
16
17
  window.canva_sdk = {
17
18
  ...getCanvaSdk(),
18
19
  ...clients
19
20
  };
20
21
  }
22
+ export function bindMethodsToClients(objectToBind) {
23
+ if (typeof objectToBind !== 'object' || objectToBind == null)
24
+ return;
25
+ const classMethods = new Set();
26
+ let currentPrototype = Object.getPrototypeOf(objectToBind);
27
+ while(currentPrototype && currentPrototype !== Object.prototype){
28
+ Object.getOwnPropertyNames(currentPrototype).forEach((method)=>classMethods.add(method));
29
+ currentPrototype = Object.getPrototypeOf(currentPrototype);
30
+ }
31
+ classMethods.delete('constructor');
32
+ for (const method of classMethods) {
33
+ const originalFn = objectToBind[method];
34
+ if (typeof originalFn === 'function') Object.defineProperty(objectToBind, method, {
35
+ value: (...args)=>{
36
+ return originalFn.call(objectToBind, ...args);
37
+ }
38
+ });
39
+ }
40
+ Object.values(objectToBind).forEach(bindMethodsToClients);
41
+ }
package/package.json CHANGED
@@ -1,25 +1,25 @@
1
1
  {
2
2
  "name": "@canva/platform",
3
- "version": "2.1.0",
3
+ "version": "2.1.1-beta.1",
4
4
  "description": "The Canva Apps SDK app platform library",
5
5
  "author": "Canva Pty Ltd.",
6
6
  "license": "SEE LICENSE IN LICENSE.md FILE",
7
7
  "peerDependencies": {
8
8
  "@canva/error": "^2.0.0"
9
9
  },
10
- "main": "./lib/cjs/sdk/platform/index.js",
11
- "module": "./lib/esm/sdk/platform/index.js",
10
+ "main": "./lib/cjs/sdk/platform/beta.js",
11
+ "module": "./lib/esm/sdk/platform/beta.js",
12
12
  "exports": {
13
13
  ".": {
14
- "types": "./index.d.ts",
15
- "require": "./lib/cjs/sdk/platform/index.js",
16
- "import": "./lib/esm/sdk/platform/index.js"
14
+ "types": "./beta.d.ts",
15
+ "require": "./lib/cjs/sdk/platform/beta.js",
16
+ "import": "./lib/esm/sdk/platform/beta.js"
17
17
  },
18
18
  "./test": {
19
- "types": "./test/index.d.ts",
20
- "require": "./lib/cjs/sdk/platform/test/index.js",
21
- "import": "./lib/esm/sdk/platform/test/index.js"
19
+ "types": "./test/beta.d.ts",
20
+ "require": "./lib/cjs/sdk/platform/test/beta.js",
21
+ "import": "./lib/esm/sdk/platform/test/beta.js"
22
22
  }
23
23
  },
24
- "typings": "./index.d.ts"
24
+ "typings": "./beta.d.ts"
25
25
  }
package/index.d.ts DELETED
@@ -1,362 +0,0 @@
1
- /**
2
- * @public
3
- * Provides methods for interacting with an app process.
4
- */
5
- export declare interface AppProcess {
6
- /**
7
- * The current app process.
8
- */
9
- readonly current: CurrentAppProcess;
10
- /**
11
- * @public
12
- * Requests the termination of the specified app process.
13
- *
14
- * @param target - The ID of an app process.
15
- * @param params - Parameters to pass to the `setOnDispose` callback. Any kind of structured data can be passed via this property.
16
- *
17
- * @remarks
18
- * Once called, this method:
19
- *
20
- * 1. Transitions the state of the process to `"closing"`.
21
- * 2. Invokes all registered `setOnDispose` callbacks.
22
- * 3. Waits for the process to finish closing.
23
- * 4. Transitions the state of the process to `"closed"`.
24
- *
25
- * Each time the state changes, all of the `registerOnStateChange` callbacks are called..
26
- */
27
- requestClose<T extends CloseParams>(
28
- target: AppProcessId,
29
- params: T,
30
- ): Promise<void>;
31
- /**
32
- * @public
33
- * Registers a callback that runs when the state of the specified app process changes.
34
- *
35
- * @param target - The ID of an app process.
36
- * @param callback - The callback to run when the state of the process changes.
37
- *
38
- * @returns
39
- * A disposer function that cleans up the registered callback.
40
- */
41
- registerOnStateChange(
42
- target: AppProcessId,
43
- callback: OnStateChangeCallback,
44
- ): () => Promise<void>;
45
- /**
46
- * @public
47
- * Registers a callback that listens for broadcasted messages.
48
- *
49
- * @param callback - The callback that listens for broadcasted messages.
50
- *
51
- * @returns
52
- * A disposer function that cleans up the registered callback.
53
- */
54
- registerOnMessage(callback: OnMessageCallback): () => Promise<void>;
55
- /**
56
- * @public
57
- * Broadcasts a message to all of the app's active processes, not including the current process.
58
- *
59
- * @param message - The message to be broadcasted. This can be any kind of structured data.
60
- */
61
- broadcastMessage(message: any): void;
62
- }
63
-
64
- /**
65
- * An alias for the AppProcess interface for interacting with the App Process.
66
- * @public
67
- */
68
- export declare const appProcess: AppProcess;
69
-
70
- /**
71
- * @public
72
- * The unique identifier of an app process.
73
- */
74
- export declare type AppProcessId = string & {
75
- __appProcessId: never;
76
- };
77
-
78
- /**
79
- * @public
80
- * Information about an app process.
81
- */
82
- export declare type AppProcessInfo<T> = {
83
- /**
84
- * The surface on which the app process is running.
85
- */
86
- surface: AppSurface;
87
- /**
88
- * The unique identifier of the app process.
89
- */
90
- processId: AppProcessId;
91
- /**
92
- * Parameters passed to the app process when it was opened.
93
- */
94
- launchParams?: T;
95
- };
96
-
97
- /**
98
- * @public
99
- * The type of surface on which an app process can run.
100
- *
101
- * @remarks
102
- * The possible surfaces include:
103
- *
104
- * - `"headless"` - A surface for when there is no visible user interface.
105
- * - `"object_panel"` - A surface that renders a user interface in the side panel of the Canva editor.
106
- * - `"selected_image_overlay"` - A surface that can be opened on top of a selected image.
107
- */
108
- export declare type AppSurface =
109
- | "object_panel"
110
- | "selected_image_overlay"
111
- | "headless";
112
-
113
- /**
114
- * @public
115
- * Parameters passed to the `setOnDispose` callback when a process is about to close.
116
- */
117
- export declare type CloseParams = {
118
- /**
119
- * The reason the app process is closing.
120
- */
121
- reason: CloseReason;
122
- };
123
-
124
- /**
125
- * @public
126
- * The reason why an app process is closing.
127
- *
128
- * @remarks
129
- * The possible reasons include:
130
- *
131
- * - `"completed"` - Indicates that a workflow has been completed and unsaved changes should be saved.
132
- * - `"aborted"` - Indicates that a workflow has been abandoned and unsaved changes should be discarded.
133
- */
134
- export declare type CloseReason = "completed" | "aborted";
135
-
136
- /**
137
- * @public
138
- * Provides methods for interacting with the current app process.
139
- */
140
- export declare type CurrentAppProcess = {
141
- /**
142
- * @public
143
- * Returns information about the current app process.
144
- */
145
- getInfo<T extends any>(): AppProcessInfo<T>;
146
- /**
147
- * @public
148
- * Requests the termination of the current process.
149
- *
150
- * @param params - Parameters to pass to the `setOnDispose` callback. Any structured data can be passed via this property.
151
- *
152
- * @remarks
153
- * Once called, this method:
154
- *
155
- * 1. Transitions the state of the process to `"closing"`.
156
- * 2. Invokes all registered `setOnDispose` callbacks.
157
- * 3. Waits for the process to finish closing.
158
- * 4. Transitions the state of the process to `"closed"`.
159
- *
160
- * Each time the state changes, all of the `registerOnStateChange` callbacks are called.
161
- */
162
- requestClose<T extends CloseParams>(params: T): Promise<void>;
163
- /**
164
- * @public
165
- * Registers a callback that runs when the current app process is about to close.
166
- *
167
- * @param callback - The callback to run when the current app process is about to close.
168
- *
169
- * @returns
170
- * A disposer function that cleans up the registered callback.
171
- *
172
- * @remarks
173
- * - Apps can't register multiple callbacks.
174
- * - If an app attempts to register multiple callbacks, only the last callback will be registered.
175
- * - The app process will remain open until the callback resolves or a timeout error occurs.
176
- * - The complete execution of the callback is not guaranteed as some user actions (e.g. closing tabs) may close the process prematurely.
177
- */
178
- setOnDispose<T extends CloseParams>(
179
- callback: OnDisposeCallback<T>,
180
- ): () => Promise<void>;
181
- };
182
-
183
- /**
184
- * @public
185
- * Disposes an event listener.
186
- */
187
- declare type Disposer = () => void;
188
-
189
- /**
190
- * @public
191
- * An SDK method that can be inspected for feature support.
192
- */
193
- export declare type Feature = (...args: any[]) => unknown;
194
-
195
- /**
196
- * An alias for the FeatureSupport interface for interacting with feature controls.
197
- * @public
198
- */
199
- export declare const features: FeatureSupport;
200
-
201
- /**
202
- * @public
203
- * Client interface for checking if a given SDK method is supported the currently presented context.
204
- */
205
- export declare interface FeatureSupport {
206
- /**
207
- * @public
208
- * Checks whether a given SDK method is supported under the current context.
209
- * @param features - Canva SDK methods to be checked for support.
210
- */
211
- isSupported(...features: Feature[]): boolean;
212
- /**
213
- * @public
214
- * Registers an event listener for changes in what SDK methods are currently supported.
215
- * @param onSupportChange - A callback that gets fired upon any change to the feature support context.
216
- */
217
- registerOnSupportChange(onSupportChange: () => void): Disposer;
218
- }
219
-
220
- /**
221
- * @public
222
- * Returns information about the platform on which the app is running.
223
- */
224
- export declare const getPlatformInfo: () => PlatformInfo;
225
-
226
- /**
227
- * @public
228
- * A callback that runs when an app process is about to close.
229
- * @param opts - Parameters passed to the `setOnDispose` callback when a process is about to close.
230
- */
231
- export declare type OnDisposeCallback<T extends CloseParams> = (
232
- opts: T,
233
- ) => Promise<void>;
234
-
235
- /**
236
- * @public
237
- * A callback that runs when an app process receives a broadcasted message.
238
- *
239
- * @param sender - Information about the process that sent the message.
240
- * - sender.appProcessId - The ID of the process that sent the message.
241
- * - sender.surface - The surface of the process that sent the message.
242
- * @param message - The broadcasted message.
243
- */
244
- export declare type OnMessageCallback = (
245
- sender: {
246
- appProcessId: AppProcessId;
247
- surface: AppSurface;
248
- },
249
- message: any,
250
- ) => Promise<void>;
251
-
252
- /**
253
- * @public
254
- * A callback that runs when the state of a process changes.
255
- *
256
- * @param opts - Information about the state change.
257
- * - opts.state - The state of the process.
258
- */
259
- export declare type OnStateChangeCallback = (opts: {
260
- state: ProcessState;
261
- }) => void;
262
-
263
- /**
264
- * @public
265
- * The result when a user doesn't agree to navigate to an external URL.
266
- */
267
- export declare type OpenExternalUrlAborted = {
268
- /**
269
- * The status of the request.
270
- */
271
- status: "aborted";
272
- };
273
-
274
- /**
275
- * @public
276
- * The result when a user agrees to navigate to an external URL.
277
- */
278
- export declare type OpenExternalUrlCompleted = {
279
- /**
280
- * The status of the request.
281
- */
282
- status: "completed";
283
- };
284
-
285
- /**
286
- * @public
287
- * Options for prompting the user to open an external URL.
288
- */
289
- export declare type OpenExternalUrlRequest = {
290
- /**
291
- * The URL to open.
292
- */
293
- url: string;
294
- };
295
-
296
- /**
297
- * @public
298
- * The result of prompting the user to open an external URL.
299
- */
300
- export declare type OpenExternalUrlResponse =
301
- | OpenExternalUrlCompleted
302
- | OpenExternalUrlAborted;
303
-
304
- /**
305
- * @public
306
- * Information about the platform on which the app is running.
307
- */
308
- export declare type PlatformInfo = {
309
- /**
310
- * If `true`, the app is allowed to directly link to payment and upgrade flows.
311
- *
312
- * @remarks
313
- * This property is always `true` when the app is running in a web browser, but may otherwise be `false` in
314
- * order to comply with the policies of the platforms on which Canva is available. For example, some platforms
315
- * only allow payment-related actions that use their own payment mechanisms and apps are therefore not allowed
316
- * to render payment-related call-to-actions while running on those platforms.
317
- *
318
- * @example
319
- * ```ts
320
- * const info = getPlatformInfo();
321
- *
322
- * if (info.canAcceptPayments) {
323
- * // Display payment links and upgrade flows
324
- * } else {
325
- * // Hide payment links and upgrade flows
326
- * // Optionally, show an appropriate message
327
- * }
328
- * ```
329
- */
330
- canAcceptPayments: boolean;
331
- };
332
-
333
- /**
334
- * @public
335
- * The state of an app process.
336
- *
337
- * @remarks
338
- * The possible states include:
339
- *
340
- * - `"opening"` - The app process is opening.
341
- * - `"open"` - The app process is open, active, and visible on the designated surface.
342
- * - `"closing"` - The app process is closing.
343
- * - `"closed"` - The app process has been closed and is no longer active.
344
- *
345
- * While a process is closing, it won't receive any events or messages from other processes.
346
- */
347
- export declare type ProcessState = "opening" | "open" | "closing" | "closed";
348
-
349
- /**
350
- * @public
351
- * Opens an external URL.
352
- *
353
- * @remarks
354
- * The URL is opened natively, such as in a new browser tab on desktop or in a browser sheet on mobile.
355
- *
356
- * In some browsers, the user must enable popup permissions before any URL can be opened.
357
- */
358
- export declare const requestOpenExternalUrl: (
359
- request: OpenExternalUrlRequest,
360
- ) => Promise<OpenExternalUrlResponse>;
361
-
362
- export {};
File without changes