@canva/platform 1.0.1 → 2.0.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,3 +1,222 @@
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
+
1
220
  /**
2
221
  * @public
3
222
  * Returns information about the platform on which the app is running.
@@ -6,40 +225,77 @@ export declare function getPlatformInfo(): PlatformInfo;
6
225
 
7
226
  /**
8
227
  * @public
9
- * The result of opening an external URL when a user chooses to not navigate away from Canva.
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.
10
266
  */
11
267
  export declare type OpenExternalUrlAborted = {
12
268
  /**
13
269
  * The status of the request.
14
270
  */
15
- status: "ABORTED";
271
+ status: "aborted";
16
272
  };
17
273
 
18
274
  /**
19
275
  * @public
20
- * The result of opening an external URL when a user agrees to navigate away from Canva.
276
+ * The result when a user agrees to navigate to an external URL.
21
277
  */
22
278
  export declare type OpenExternalUrlCompleted = {
23
279
  /**
24
280
  * The status of the request.
25
281
  */
26
- status: "COMPLETED";
282
+ status: "completed";
27
283
  };
28
284
 
29
285
  /**
30
286
  * @public
31
- * The options for opening an external URL.
287
+ * Options for prompting the user to open an external URL.
32
288
  */
33
289
  export declare type OpenExternalUrlRequest = {
34
290
  /**
35
- * The URL to open in a new window
291
+ * The URL to open.
36
292
  */
37
293
  url: string;
38
294
  };
39
295
 
40
296
  /**
41
297
  * @public
42
- * The result of opening an external URL.
298
+ * The result of prompting the user to open an external URL.
43
299
  */
44
300
  export declare type OpenExternalUrlResponse =
45
301
  | OpenExternalUrlCompleted
@@ -60,18 +316,36 @@ export declare type PlatformInfo = {
60
316
  * to render payment-related call-to-actions while running on those platforms.
61
317
  *
62
318
  * @example
319
+ * ```ts
63
320
  * const info = getPlatformInfo();
64
321
  *
65
322
  * if (info.canAcceptPayments) {
66
323
  * // Display payment links and upgrade flows
67
324
  * } else {
68
325
  * // Hide payment links and upgrade flows
69
- * // Optionally show an appropriate message
326
+ * // Optionally, show an appropriate message
70
327
  * }
328
+ * ```
71
329
  */
72
330
  canAcceptPayments: boolean;
73
331
  };
74
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
+
75
349
  /**
76
350
  * @public
77
351
  * Opens an external URL.
@@ -1,17 +1,21 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./public"), exports);
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./public"), 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
+ }
19
+ var _window___canva___sdkRegistration, _window___canva__;
20
+ (_window___canva__ =
21
+ 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.0.0', 'ga');
@@ -1,25 +1,33 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPlatformInfo = exports.requestOpenExternalUrl = void 0;
4
- const { canva } = window;
5
- /**
6
- * @public
7
- * Opens an external URL.
8
- *
9
- * @remarks
10
- * The URL is opened natively, such as in a new browser tab on desktop or in a browser sheet on mobile.
11
- *
12
- * In some browsers, the user must enable popup permissions before any URL can be opened.
13
- */
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ appProcess: function() {
13
+ return appProcess;
14
+ },
15
+ features: function() {
16
+ return features;
17
+ },
18
+ requestOpenExternalUrl: function() {
19
+ return requestOpenExternalUrl;
20
+ },
21
+ getPlatformInfo: function() {
22
+ return getPlatformInfo;
23
+ }
24
+ });
25
+ const { canva_sdk } = window;
26
+ const appProcess = canva_sdk.platform.v2.appProcess;
27
+ const features = canva_sdk.platform.v2.features;
14
28
  function requestOpenExternalUrl(request) {
15
- return canva.platform.requestOpenExternalUrl(request);
29
+ return canva_sdk.platform.v2.platform.requestOpenExternalUrl(request);
16
30
  }
17
- exports.requestOpenExternalUrl = requestOpenExternalUrl;
18
- /**
19
- * @public
20
- * Returns information about the platform on which the app is running.
21
- */
22
31
  function getPlatformInfo() {
23
- return canva.platform.getPlatformInfo();
32
+ return canva_sdk.platform.v2.platform.getPlatformInfo();
24
33
  }
25
- exports.getPlatformInfo = getPlatformInfo;
@@ -1 +1,4 @@
1
+ var _window___canva___sdkRegistration, _window___canva__;
1
2
  export * from './public';
3
+ (_window___canva__ =
4
+ 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.0.0', 'ga');
@@ -1,20 +1,9 @@
1
- const { canva } = window;
2
- /**
3
- * @public
4
- * Opens an external URL.
5
- *
6
- * @remarks
7
- * The URL is opened natively, such as in a new browser tab on desktop or in a browser sheet on mobile.
8
- *
9
- * In some browsers, the user must enable popup permissions before any URL can be opened.
10
- */
1
+ const { canva_sdk } = window;
2
+ export const appProcess = canva_sdk.platform.v2.appProcess;
3
+ export const features = canva_sdk.platform.v2.features;
11
4
  export function requestOpenExternalUrl(request) {
12
- return canva.platform.requestOpenExternalUrl(request);
5
+ return canva_sdk.platform.v2.platform.requestOpenExternalUrl(request);
13
6
  }
14
- /**
15
- * @public
16
- * Returns information about the platform on which the app is running.
17
- */
18
7
  export function getPlatformInfo() {
19
- return canva.platform.getPlatformInfo();
8
+ return canva_sdk.platform.v2.platform.getPlatformInfo();
20
9
  }
package/package.json CHANGED
@@ -1,18 +1,24 @@
1
1
  {
2
2
  "name": "@canva/platform",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
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
- "@canva/error": "^1.0.0"
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/index.js",
11
+ "module": "./lib/esm/sdk/platform/index.js",
12
12
  "exports": {
13
13
  ".": {
14
- "require": "./lib/cjs/sdk/platform/index.js",
15
- "import": "./lib/esm/sdk/platform/index.js"
14
+ "require": {
15
+ "types": "./index.d.ts",
16
+ "default": "./lib/cjs/sdk/platform/index.js"
17
+ },
18
+ "import": {
19
+ "types": "./index.d.ts",
20
+ "default": "./lib/esm/sdk/platform/index.js"
21
+ }
16
22
  }
17
23
  },
18
24
  "typings": "./index.d.ts"