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