@canva/platform 2.2.0 → 2.2.1-beta.0

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/index.d.ts CHANGED
@@ -1,724 +1 @@
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
- * @public
480
- * Provides methods for interacting with notifications.
481
- */
482
- export declare const notification: NotificationClient;
483
-
484
- /**
485
- * @public
486
- *
487
- * Provides methods for interacting with notifications.
488
- */
489
- export declare interface NotificationClient {
490
- /**
491
- * @public
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
- * @public
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
- * @public
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
- * @public
719
- *
720
- * The response from adding a toast notification.
721
- */
722
- export declare type ToastResponse = ToastCompleted;
723
-
724
- export {};
1
+ export * from "./beta";