@canva/platform 0.0.1-rc.2 → 1.1.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/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # @canva/platform
2
+
3
+ A package for Canva's Apps SDK that provides utility methods.
4
+
5
+ ![](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white)
6
+
7
+ ## Table of contents
8
+
9
+ - [Introduction](#introduction)
10
+ - [Installation](#installation)
11
+ - [Usage](#usage)
12
+ - [API reference](#api-reference)
13
+ - [Related packages](#related-packages)
14
+ - [Contributing](#contributing)
15
+ - [License](#license)
16
+
17
+ ## Introduction
18
+
19
+ `@canva/platform` is an npm package for Canva's [Apps SDK](https://www.canva.dev/docs/apps). It provides utility methods that are useful for a broad range of apps. For example, the `requestOpenExternalUrl` method opens an external web page.
20
+
21
+ **Note:** To get up and running with the Apps SDK, check out [the quick start guide](https://www.canva.dev/docs/apps/quick-start).
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ npm install @canva/platform
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ 1. Import a method or namespace from the `@canva/platform` package:
32
+
33
+ ```ts
34
+ import { getPlatformInfo } from '@canva/platform';
35
+ ```
36
+
37
+ 2. Call a method, passing in the required arguments (if any):
38
+
39
+ ```ts
40
+ getPlatformInfo();
41
+ ```
42
+
43
+ ## API reference
44
+
45
+ - [`getPlatformInfo`](https://www.canva.dev/docs/apps/api/platform-get-platform-info)
46
+ - [`requestOpenExternalUrl`](https://www.canva.dev/docs/apps/api/platform-request-open-external-url)
47
+
48
+ ## Related packages
49
+
50
+ The Apps SDK is made up of the following packages:
51
+
52
+ - [`@canva/app-ui-kit`](https://www.npmjs.com/package/@canva/app-ui-kit) - React-based component library for creating apps that mimic the look and feel of Canva.
53
+ - [`@canva/asset`](https://www.npmjs.com/package/@canva/asset) - Provides methods for working with assets, such as image and video files.
54
+ - [`@canva/design`](https://www.npmjs.com/package/@canva/design) - Provides methods for interacting with the user's design, such as creating elements.
55
+ - [`@canva/error`](https://www.npmjs.com/package/@canva/error) - Provides a `CanvaError` class for handling errors.
56
+ - [`@canva/platform`](https://www.npmjs.com/package/@canva/platform) - Provides utility methods, such as a method for opening external links.
57
+ - [`@canva/user`](https://www.npmjs.com/package/@canva/user) - Provides methods for accessing user data and authenticating users.
58
+
59
+ ## Contributing
60
+
61
+ We're actively developing this package but are not currently accepting third-party contributions. If you'd like to request any changes or additions to the package, submit a feature request via the [Canva Developers Community](https://community.canva.dev/).
62
+
63
+ ## License
64
+
65
+ See the `LICENSE.md` file.
package/index.d.ts CHANGED
@@ -1,9 +1,191 @@
1
+ /**
2
+ * @public
3
+ * An API for interacting with the App Process.
4
+ */
5
+ export declare interface AppProcess {
6
+ readonly current: CurrentAppProcess;
7
+ /**
8
+ * @public
9
+ * Request the termination of an app process.
10
+ * @param target - The id of the app process to close.
11
+ * @param params - A parameters object passed to all callback functions registered via registerOnBeforeClose API for the provided AppProcessId.
12
+ * In addition to the required 'reason' field, app can choose to pass any structured data via params.
13
+ * {@link https://html.spec.whatwg.org/multipage/structured-data.html#safe-passing-of-structured-data | safe passing of structured data}
14
+ * @remarks
15
+ * A successful invocation indicates the platform has begun shutdown following procedure for the target AppProcessId, which:
16
+ * 1. Transitions process state to `CLOSING`, triggering all registered state change callbacks.
17
+ * 2. Invokes all registered onBeforeClose callbacks.
18
+ * 3. Waits for the process to close.
19
+ * 4. Transitions process state to `CLOSED`, triggering all registered state change callbacks.
20
+ * @remarks
21
+ * Once initiated, the closing process is irreversible and the process will NOT receive any events or messages from other processes.
22
+ */
23
+ requestClose<T extends CloseParams>(
24
+ target: AppProcessId,
25
+ params: T
26
+ ): Promise<void>;
27
+ /**
28
+ * @public
29
+ * Registers a callback to be executed when the state of the specified app process changes.
30
+ * @param target - The id of the app process for which to register the callback.
31
+ * @param callback - Callback function triggered on state change
32
+ * @returns a disposer function to unsubscribe the callback.
33
+ */
34
+ registerOnStateChange(
35
+ target: AppProcessId,
36
+ callback: (opts: { state: ProcessState }) => void
37
+ ): () => Promise<void>;
38
+ /**
39
+ * Registers a handler to receive messages sent from other app processes.
40
+ * @param callback - Handler function to receive incoming messages.
41
+ * @returns a disposer function to unsubscribe the handler.
42
+ */
43
+ registerOnMessage(
44
+ callback: (
45
+ sender: {
46
+ appProcessId: AppProcessId;
47
+ surface: AppSurface;
48
+ },
49
+ message: any
50
+ ) => void
51
+ ): () => Promise<void>;
52
+ /**
53
+ * Broadcasts a message to all other running app processes which share the caller's AppId.
54
+ * @param message - app-defined message to be broadcast. message needs to be structured data.
55
+ */
56
+ broadcastMessage(message: any): void;
57
+ }
58
+
59
+ /**
60
+ * An alias for the AppProcess interface for interacting with the App Process.
61
+ * @public
62
+ */
63
+ export declare const appProcess: AppProcess;
64
+
65
+ /**
66
+ * @public
67
+ * The unique identifier for an app process.
68
+ */
69
+ export declare type AppProcessId = string & {
70
+ __appProcessId: never;
71
+ };
72
+
73
+ /**
74
+ * @public
75
+ * Information about an app process.
76
+ */
77
+ export declare type AppProcessInfo<T> = {
78
+ /**
79
+ * The surface on which the app is running.
80
+ */
81
+ surface: AppSurface;
82
+ /**
83
+ * The unique identifier of the app process.
84
+ */
85
+ processId: AppProcessId;
86
+ /**
87
+ * The parameters provided to the app at the time of its process launch.
88
+ */
89
+ launchParams?: T;
90
+ };
91
+
92
+ /**
93
+ * @public
94
+ * The types of surfaces that can run an app process.
95
+ */
96
+ export declare type AppSurface =
97
+ /**
98
+ * The object panel on the left side of the editing UI.
99
+ */
100
+ | "object_panel"
101
+ /**
102
+ * The overlay surface on top of a selected image in the design.
103
+ */
104
+ | "selected_image_overlay";
105
+
106
+ /**
107
+ * @public
108
+ * The parameters specified when closing an app process.
109
+ * @remarks
110
+ * CloseParams are passed on to the callback function registered for handling the termination of an app process,
111
+ * and provide additional context for ending the process.
112
+ * For example, Apps should persist unsaved changes when the close reason is 'completed'.
113
+ */
114
+ export declare type CloseParams = {
115
+ reason: CloseReason;
116
+ };
117
+
118
+ /**
119
+ * @public
120
+ * The reasons why an app process is closed.
121
+ */
122
+ export declare type CloseReason =
123
+ /**
124
+ * Indicates the workflow is completed, for example, when user clicks a 'Save and Close' in the App.
125
+ * Any unsaved changes should be saved before closing.
126
+ */
127
+ | "completed"
128
+ /**
129
+ * Indicates user has aborted the workflow, for example, when user clicked the 'cancel' button in the App.
130
+ * App should be closed without saving any current changes.
131
+ */
132
+ | "aborted";
133
+
134
+ /**
135
+ * @public
136
+ * Represents and exposes functionality specific to the currently running app process.
137
+ */
138
+ export declare type CurrentAppProcess = {
139
+ /**
140
+ * @public
141
+ * Retrieves information about the current app process.
142
+ */
143
+ getInfo<T extends any>(): AppProcessInfo<T>;
144
+ /**
145
+ * @public
146
+ * Requests the current app process be closed.
147
+ * @param params - A parameters object passed to all callback functions registered via registerOnBeforeClose API for the current process.
148
+ * In addition to the required 'reason' field, app can choose to pass any structured data via params.
149
+ * @remarks
150
+ * A successful invocation indicates the platform has begun shutdown following procedure for the current process, which:
151
+ * 1. Transitions process state to `CLOSING`, triggering all registered state change callbacks.
152
+ * 2. Invokes all registered onBeforeClose callbacks.
153
+ * 3. Waits for the process to close.
154
+ * 4. Transitions process state to `CLOSED`, triggering all registered state change callbacks.
155
+ * @remarks
156
+ * Once initiated, the closing process is irreversible and the process will NOT receive any events or messages from other processes.
157
+ */
158
+ requestClose<T extends CloseParams>(params: T): Promise<void>;
159
+ /**
160
+ * @public
161
+ * Registers a callback to be executed when the current app process is about to close.
162
+ * @remarks Only allow registering one callback.
163
+ * Subsequential invokes of the API will override the existing callback.
164
+ * The process remains open until the callback resolve or a timeout occurs.
165
+ * @param callback - The callback function to be invoked before closure.
166
+ * @returns a disposer function to unsubscribe the callback.
167
+ * @remarks Complete execution of the callback is not guaranteed. Certain user actions (e.g. closing tabs, top level navigation) may terminate app processes prematurely. Consider these callbacks **best effort only.**
168
+ */
169
+ setOnDispose<T extends CloseParams>(
170
+ callback: OnDisposeCallback<T>
171
+ ): () => Promise<void>;
172
+ };
173
+
1
174
  /**
2
175
  * @public
3
176
  * Returns information about the platform on which the app is running.
4
177
  */
5
178
  export declare function getPlatformInfo(): PlatformInfo;
6
179
 
180
+ /**
181
+ * @public
182
+ * The type of a callback that is invoked when an app process is being closed.
183
+ * @returns a promise.
184
+ */
185
+ export declare type OnDisposeCallback<T extends CloseParams> = (
186
+ opts: T
187
+ ) => Promise<void>;
188
+
7
189
  /**
8
190
  * @public
9
191
  * The result of opening an external URL when a user chooses to not navigate away from Canva.
@@ -54,12 +236,14 @@ export declare type PlatformInfo = {
54
236
  * If `true`, the app is allowed to directly link to payment and upgrade flows.
55
237
  *
56
238
  * @remarks
57
- * An example of when this is `false` is when the app is opened in Canva's iOS app.
58
- * This is because Apple's Human Interface Guidelines don't allow "calls to action that
59
- * direct customers to purchasing mechanisms other than in-app purchase."
239
+ * This property is always `true` when the app is running in a web browser, but may otherwise be `false` in
240
+ * order to comply with the policies of the platforms on which Canva is available. For example, some platforms
241
+ * only allow payment-related actions that use their own payment mechanisms and apps are therefore not allowed
242
+ * to render payment-related call-to-actions while running on those platforms.
60
243
  *
61
244
  * @example
62
245
  * const info = getPlatformInfo();
246
+ *
63
247
  * if (info.canAcceptPayments) {
64
248
  * // Display payment links and upgrade flows
65
249
  * } else {
@@ -70,6 +254,29 @@ export declare type PlatformInfo = {
70
254
  canAcceptPayments: boolean;
71
255
  };
72
256
 
257
+ /**
258
+ * @public
259
+ * The states are an app process.
260
+ */
261
+ export declare type ProcessState =
262
+ /**
263
+ * The app process is in the process of opening.
264
+ */
265
+ | "opening"
266
+ /**
267
+ * The app process is active and visible to the user on its designated surface.
268
+ */
269
+ | "open"
270
+ /**
271
+ * The app process is in the process of closing.
272
+ * At this state, the process becomes non-interactive and won't receive any events and messages from other processes.
273
+ */
274
+ | "closing"
275
+ /**
276
+ * The app process has been terminated and is no longer running.
277
+ */
278
+ | "closed";
279
+
73
280
  /**
74
281
  * @public
75
282
  * Opens an external URL.
@@ -1,17 +1,19 @@
1
+ // Copyright 2023 Canva Inc. All Rights Reserved.
1
2
  "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);
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ _export_star(require("./public"), exports);
7
+ function _export_star(from, to) {
8
+ Object.keys(from).forEach(function(k) {
9
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
10
+ Object.defineProperty(to, k, {
11
+ enumerable: true,
12
+ get: function() {
13
+ return from[k];
14
+ }
15
+ });
16
+ }
17
+ });
18
+ return from;
19
+ }
@@ -1,25 +1,30 @@
1
+ // Copyright 2023 Canva Inc. All Rights Reserved.
1
2
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPlatformInfo = exports.requestOpenExternalUrl = void 0;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ function _export(target, all) {
7
+ for(var name in all)Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ appProcess: function() {
14
+ return appProcess;
15
+ },
16
+ requestOpenExternalUrl: function() {
17
+ return requestOpenExternalUrl;
18
+ },
19
+ getPlatformInfo: function() {
20
+ return getPlatformInfo;
21
+ }
22
+ });
4
23
  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
- */
24
+ const appProcess = canva.appProcess;
14
25
  function requestOpenExternalUrl(request) {
15
26
  return canva.platform.requestOpenExternalUrl(request);
16
27
  }
17
- exports.requestOpenExternalUrl = requestOpenExternalUrl;
18
- /**
19
- * @public
20
- * Returns information about the platform on which the app is running.
21
- */
22
28
  function getPlatformInfo() {
23
29
  return canva.platform.getPlatformInfo();
24
30
  }
25
- exports.getPlatformInfo = getPlatformInfo;
@@ -1 +1,2 @@
1
+ // Copyright 2023 Canva Inc. All Rights Reserved.
1
2
  export * from './public';
@@ -1,4 +1,9 @@
1
+ // Copyright 2023 Canva Inc. All Rights Reserved.
1
2
  const { canva } = window;
3
+ /**
4
+ * An alias for the AppProcess interface for interacting with the App Process.
5
+ * @public
6
+ */ export const appProcess = canva.appProcess;
2
7
  /**
3
8
  * @public
4
9
  * Opens an external URL.
@@ -7,14 +12,12 @@ const { canva } = window;
7
12
  * The URL is opened natively, such as in a new browser tab on desktop or in a browser sheet on mobile.
8
13
  *
9
14
  * In some browsers, the user must enable popup permissions before any URL can be opened.
10
- */
11
- export function requestOpenExternalUrl(request) {
15
+ */ export function requestOpenExternalUrl(request) {
12
16
  return canva.platform.requestOpenExternalUrl(request);
13
17
  }
14
18
  /**
15
19
  * @public
16
20
  * Returns information about the platform on which the app is running.
17
- */
18
- export function getPlatformInfo() {
21
+ */ export function getPlatformInfo() {
19
22
  return canva.platform.getPlatformInfo();
20
23
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@canva/platform",
3
- "version": "0.0.1-rc.2",
4
- "description": "The Canva Apps SDK App Platform Library",
3
+ "version": "1.1.0",
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": "^0.0.1-rc.1"
8
+ "@canva/error": "^1.0.0"
9
9
  },
10
10
  "main": "lib/cjs/sdk/platform/index.js",
11
11
  "module": "lib/esm/sdk/platform/index.js",