@canva/platform 2.1.0 → 2.1.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/index.d.ts CHANGED
@@ -22,7 +22,36 @@ export declare interface AppProcess {
22
22
  * 3. Waits for the process to finish closing.
23
23
  * 4. Transitions the state of the process to `"closed"`.
24
24
  *
25
- * Each time the state changes, all of the `registerOnStateChange` callbacks are called..
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
+ * ```
26
55
  */
27
56
  requestClose<T extends CloseParams>(
28
57
  target: AppProcessId,
@@ -37,6 +66,36 @@ export declare interface AppProcess {
37
66
  *
38
67
  * @returns
39
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
+ * ```
40
99
  */
41
100
  registerOnStateChange(
42
101
  target: AppProcessId,
@@ -50,6 +109,19 @@ export declare interface AppProcess {
50
109
  *
51
110
  * @returns
52
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
+ * ```
53
125
  */
54
126
  registerOnMessage(callback: OnMessageCallback): () => Promise<void>;
55
127
  /**
@@ -57,13 +129,60 @@ export declare interface AppProcess {
57
129
  * Broadcasts a message to all of the app's active processes, not including the current process.
58
130
  *
59
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
+ * ```
60
179
  */
61
180
  broadcastMessage(message: any): void;
62
181
  }
63
182
 
64
183
  /**
65
- * An alias for the AppProcess interface for interacting with the App Process.
66
184
  * @public
185
+ * Provides methods for interacting with app processes.
67
186
  */
68
187
  export declare const appProcess: AppProcess;
69
188
 
@@ -141,8 +260,46 @@ export declare type CurrentAppProcess = {
141
260
  /**
142
261
  * @public
143
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
+ * ```
144
301
  */
145
- getInfo<T extends any>(): AppProcessInfo<T>;
302
+ getInfo<T>(): AppProcessInfo<T>;
146
303
  /**
147
304
  * @public
148
305
  * Requests the termination of the current process.
@@ -158,6 +315,35 @@ export declare type CurrentAppProcess = {
158
315
  * 4. Transitions the state of the process to `"closed"`.
159
316
  *
160
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
+ * ```
161
347
  */
162
348
  requestClose<T extends CloseParams>(params: T): Promise<void>;
163
349
  /**
@@ -174,6 +360,20 @@ export declare type CurrentAppProcess = {
174
360
  * - If an app attempts to register multiple callbacks, only the last callback will be registered.
175
361
  * - The app process will remain open until the callback resolves or a timeout error occurs.
176
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
+ * ```
177
377
  */
178
378
  setOnDispose<T extends CloseParams>(
179
379
  callback: OnDisposeCallback<T>,
@@ -193,26 +393,58 @@ declare type Disposer = () => void;
193
393
  export declare type Feature = (...args: any[]) => unknown;
194
394
 
195
395
  /**
196
- * An alias for the FeatureSupport interface for interacting with feature controls.
197
396
  * @public
397
+ * Provides methods for checking if a feature is supported.
198
398
  */
199
399
  export declare const features: FeatureSupport;
200
400
 
201
401
  /**
202
402
  * @public
203
- * Client interface for checking if a given SDK method is supported the currently presented context.
403
+ * Provides methods for checking if an SDK method is supported in the current context.
204
404
  */
205
405
  export declare interface FeatureSupport {
206
406
  /**
207
407
  * @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.
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
+ * ```
210
427
  */
211
428
  isSupported(...features: Feature[]): boolean;
212
429
  /**
213
430
  * @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.
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
+ * ```
216
448
  */
217
449
  registerOnSupportChange(onSupportChange: () => void): Disposer;
218
450
  }
@@ -220,6 +452,26 @@ export declare interface FeatureSupport {
220
452
  /**
221
453
  * @public
222
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
+ * ```
223
475
  */
224
476
  export declare const getPlatformInfo: () => PlatformInfo;
225
477
 
@@ -354,6 +606,41 @@ export declare type ProcessState = "opening" | "open" | "closing" | "closed";
354
606
  * The URL is opened natively, such as in a new browser tab on desktop or in a browser sheet on mobile.
355
607
  *
356
608
  * In some browsers, the user must enable popup permissions before any URL can be opened.
609
+ *
610
+ * @example Open an external URL
611
+ * ```typescript
612
+ * import { requestOpenExternalUrl } from '@canva/platform';
613
+ *
614
+ * await requestOpenExternalUrl({
615
+ * url: 'https://www.example.com',
616
+ * });
617
+ * ```
618
+ *
619
+ * @example Detect when a user navigates to the external URL
620
+ * ```typescript
621
+ * import { requestOpenExternalUrl } from '@canva/platform';
622
+ *
623
+ * const response = await requestOpenExternalUrl({
624
+ * url: 'https://www.example.com',
625
+ * });
626
+ *
627
+ * if (response.status === 'completed') {
628
+ * // URL opened successfully
629
+ * }
630
+ * ```
631
+ *
632
+ * @example Detect when a user doesn't navigate to the external URL
633
+ * ```typescript
634
+ * import { requestOpenExternalUrl } from '@canva/platform';
635
+ *
636
+ * const response = await requestOpenExternalUrl({
637
+ * url: 'https://www.example.com',
638
+ * });
639
+ *
640
+ * if (response.status === 'aborted') {
641
+ * // User declined to open URL
642
+ * }
643
+ * ```
357
644
  */
358
645
  export declare const requestOpenExternalUrl: (
359
646
  request: OpenExternalUrlRequest,
@@ -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
+ }
@@ -17,4 +17,4 @@ function _export_star(from, to) {
17
17
  return from;
18
18
  }
19
19
  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');
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.1', 'ga');
@@ -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;
@@ -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,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
+ }
@@ -1,3 +1,3 @@
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', 'ga');
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@canva/platform",
3
- "version": "2.1.0",
3
+ "version": "2.1.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",