@canva/design 0.0.1-rc.1 → 1.4.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,73 @@
1
+ # @canva/design
2
+
3
+ A package for Canva's Apps SDK that provides methods for interacting with a user's design.
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/design` is an npm package for Canva's [Apps SDK](https://www.canva.dev/docs/apps). It provides methods for interacting with a user's design. For example, the `addPage` method adds a page to the user's design, while `requestExport` exports the user's design as one or more static files.
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/design
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ 1. Import a method or namespace from the `@canva/design` package:
32
+
33
+ ```ts
34
+ import { addPage } from '@canva/design';
35
+ ```
36
+
37
+ 2. Call a method, passing in the required arguments (if any):
38
+
39
+ ```ts
40
+ await addPage();
41
+ ```
42
+
43
+ ## API reference
44
+
45
+ - [`addAudioTrack`](https://www.canva.dev/docs/apps/api/design-add-audio-track)
46
+ - [`addNativeElement`](https://www.canva.dev/docs/apps/api/design-add-native-element)
47
+ - [`addPage`](https://www.canva.dev/docs/apps/api/design-add-page)
48
+ - [`getCurrentPageContext`](https://www.canva.dev/docs/apps/api/design-get-current-page-context)
49
+ - [`getDefaultPageDimensions`](https://www.canva.dev/docs/apps/api/design-get-default-page-dimensions)
50
+ - [`initAppElement`](https://www.canva.dev/docs/apps/api/design-init-app-element)
51
+ - [`requestExport`](https://www.canva.dev/docs/apps/api/design-request-export)
52
+ - [`selection.registerOnChange`](https://www.canva.dev/docs/apps/api/design-selection-register-on-change)
53
+ - [`setCurrentPageBackground`](https://www.canva.dev/docs/apps/api/design-set-current-page-background)
54
+ - [`ui.startDrag`](https://www.canva.dev/docs/apps/api/design-ui-start-drag)
55
+
56
+ ## Related packages
57
+
58
+ The Apps SDK is made up of the following packages:
59
+
60
+ - [`@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.
61
+ - [`@canva/asset`](https://www.npmjs.com/package/@canva/asset) - Provides methods for working with assets, such as image and video files.
62
+ - [`@canva/design`](https://www.npmjs.com/package/@canva/design) - Provides methods for interacting with the user's design, such as creating elements.
63
+ - [`@canva/error`](https://www.npmjs.com/package/@canva/error) - Provides a `CanvaError` class for handling errors.
64
+ - [`@canva/platform`](https://www.npmjs.com/package/@canva/platform) - Provides utility methods, such as a method for opening external links.
65
+ - [`@canva/user`](https://www.npmjs.com/package/@canva/user) - Provides methods for accessing user data and authenticating users.
66
+
67
+ ## Contributing
68
+
69
+ 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/).
70
+
71
+ ## License
72
+
73
+ See the `LICENSE.md` file.
package/index.d.ts CHANGED
@@ -22,7 +22,10 @@ export declare function addNativeElement(
22
22
  export declare function addPage(opts?: {
23
23
  /** Elements to be added to the page */
24
24
  elements?: NativeElementWithBox[];
25
-
25
+ /**
26
+ * The page background. This can be a solid color, an image or a video.
27
+ **/
28
+ background?: PageBackgroundFill;
26
29
  /** A page title which must be no longer than 255 characters */
27
30
  title?: string;
28
31
  }): Promise<void>;
@@ -94,7 +97,10 @@ export declare type AppElementRenderer<A extends AppElementData> = (
94
97
  * A return value of {@link AppElementRenderer} function.
95
98
  * It is an array of elements to render within an app element.
96
99
  */
97
- export declare type AppElementRendererOutput = NativeSimpleElementWithBox[];
100
+ export declare type AppElementRendererOutput = Exclude<
101
+ NativeSimpleElementWithBox,
102
+ NativeVideoElementWithBox
103
+ >[];
98
104
 
99
105
  /**
100
106
  * @public
@@ -164,6 +170,29 @@ declare type CommonImageDragConfig = {
164
170
  previewSize: Dimensions;
165
171
  };
166
172
 
173
+ /**
174
+ * @public
175
+ * A snapshot of some part of the document content.
176
+ *
177
+ * @remarks
178
+ * You can mutate the values in the `contents` array and then persist those changes with the `save` method.
179
+ */
180
+ export declare interface ContentDraft<T> {
181
+ /**
182
+ * The selected content.
183
+ */
184
+ readonly contents: readonly T[];
185
+ /**
186
+ * Saves changes made to items in the `contents` array.
187
+ * Once saved, those changes are reflected in the user's design.
188
+ *
189
+ * @remarks
190
+ * Any other changes to the content since calling the `read` method, such as the user editing the content directly, will be overwritten.
191
+ * Only properties that are different from the original state are written to the design.
192
+ */
193
+ save(): Promise<void>;
194
+ }
195
+
167
196
  /**
168
197
  * @public
169
198
  * Represents X and Y coordinates.
@@ -179,6 +208,29 @@ export declare type Coordinates = {
179
208
  y: number;
180
209
  };
181
210
 
211
+ /**
212
+ * @public
213
+ * Provides methods for interacting with the user's selected content, such as images or text.
214
+ */
215
+ export declare type DesignSelection = {
216
+ /**
217
+ * Registers a callback that runs when a user selects an element that contains the specified type of content.
218
+ *
219
+ * @remarks
220
+ * The callback fires immediately if content is already selected.
221
+ */
222
+ registerOnChange<Scope extends SelectionScope>(opts: {
223
+ /**
224
+ * The type of content that, when selected, triggers the `onChange` callback.
225
+ */
226
+ scope: Scope;
227
+ /**
228
+ * The callback to run when the selection changes.
229
+ */
230
+ onChange(event: SelectionEvent<Scope>): void;
231
+ }): () => void;
232
+ };
233
+
182
234
  /**
183
235
  * @public
184
236
  * Represents a width and a height.
@@ -442,6 +494,21 @@ export declare type Fill = {
442
494
  asset?: ImageFill | VideoFill;
443
495
  };
444
496
 
497
+ /**
498
+ * @public
499
+ * Font weights supported in the SDK.
500
+ **/
501
+ declare type FontWeight =
502
+ | "normal"
503
+ | "thin"
504
+ | "extralight"
505
+ | "light"
506
+ | "medium"
507
+ | "semibold"
508
+ | "bold"
509
+ | "ultrabold"
510
+ | "heavy";
511
+
445
512
  /**
446
513
  * Allows to get the context of currently selected page.
447
514
  * @public
@@ -658,7 +725,6 @@ export declare type NativeImageElement = {
658
725
  | {
659
726
  /**
660
727
  * A data URL that contains the image data.
661
- * @deprecated Instead, use the `upload` method from the `@canva/asset` package and pass the returned asset reference into the `ref` property.
662
728
  */
663
729
  dataUrl: string;
664
730
  /**
@@ -853,6 +919,12 @@ export declare type NativeVideoElementWithBox = NativeVideoElement & Box;
853
919
  */
854
920
  declare type ObjectPrimitive = Boolean | String;
855
921
 
922
+ /**
923
+ * @public
924
+ * The appearance of a page's background.
925
+ */
926
+ export declare type PageBackgroundFill = Pick<Fill, "asset" | "color">;
927
+
856
928
  /**
857
929
  * @public
858
930
  * Page context
@@ -934,9 +1006,9 @@ declare type Position = {
934
1006
  * The types of primitive values that can be stored within an app element's data.
935
1007
  *
936
1008
  * @remarks
937
- * All types of primitives are supported except for symbols, bigints and undefined.
1009
+ * All types of primitives are supported except for symbols and bigints.
938
1010
  */
939
- declare type Primitive = null | number | boolean | string;
1011
+ declare type Primitive = undefined | null | number | boolean | string;
940
1012
 
941
1013
  /**
942
1014
  * @public
@@ -947,6 +1019,90 @@ export declare function requestExport(
947
1019
  request: ExportRequest
948
1020
  ): Promise<ExportResponse>;
949
1021
 
1022
+ /**
1023
+ * @public
1024
+ * An alias for the DesignSelection interface, providing access to design selection related functionality
1025
+ */
1026
+ export declare const selection: DesignSelection;
1027
+
1028
+ /**
1029
+ * @public
1030
+ * Information about the user's selection. To access the selected content, call the `read` method.
1031
+ */
1032
+ export declare interface SelectionEvent<Scope extends SelectionScope> {
1033
+ /**
1034
+ * The type of content that's selected.
1035
+ */
1036
+ readonly scope: Scope;
1037
+ /**
1038
+ * The number of selected elements.
1039
+ */
1040
+ readonly count: number;
1041
+ /**
1042
+ * Creates a snapshot of the selected content and returns a *draft* object.
1043
+ * The draft has a mutable `contents` property for making changes to the selected content.
1044
+ * Any changes made to `contents` are not immediately persisted or reflected in the user's design.
1045
+ * To persist the changes, call the `save` method that's available via the draft.
1046
+ *
1047
+ * @example Replacing text
1048
+ * ```
1049
+ * const draft = await selectionEvent.read();
1050
+ *
1051
+ * for(const content of draft.contents) {
1052
+ * // This change won't immediately appear in the user's design
1053
+ * content.text = "This is the new text";
1054
+ * }
1055
+ *
1056
+ * // The changes will now appear in the user's design
1057
+ * await draft.save();
1058
+ * ```
1059
+ */
1060
+ read(): Promise<ContentDraft<SelectionValue<Scope>>>;
1061
+ }
1062
+
1063
+ /**
1064
+ * @public
1065
+ * The type of content that apps can detect selection changes on.
1066
+ */
1067
+ export declare type SelectionScope = "plaintext" | "image";
1068
+
1069
+ /**
1070
+ * @public
1071
+ * The selected content.
1072
+ *
1073
+ * @remarks
1074
+ * The value depends on the {@link SelectionScope}.
1075
+ */
1076
+ export declare type SelectionValue<Scope extends SelectionScope> = {
1077
+ /**
1078
+ * The selected content when {@link SelectionScope} is `"plaintext"`.
1079
+ */
1080
+ ["plaintext"]: {
1081
+ /**
1082
+ * The text content of the element as plain text.
1083
+ */
1084
+ text: string;
1085
+ };
1086
+ /**
1087
+ * The selected content when {@link SelectionScope} is `"image"`.
1088
+ */
1089
+ ["image"]: {
1090
+ /**
1091
+ * A unique identifier that points to an image asset in Canva's backend.
1092
+ */
1093
+ ref: ImageRef;
1094
+ };
1095
+ }[Scope];
1096
+
1097
+ /**
1098
+ * @public
1099
+ * Updates the background of the user's current page. The background can be a solid color,
1100
+ * an image or a video.
1101
+ */
1102
+ export declare function setCurrentPageBackground(
1103
+ opts: PageBackgroundFill
1104
+ ): Promise<void>;
1105
+
950
1106
  /**
951
1107
  * @public
952
1108
  * A path that defines the shape of a shape element.
@@ -1029,10 +1185,11 @@ declare type TextAttributes = {
1029
1185
  * (e.g. #ff0099). The default value is #000000.
1030
1186
  */
1031
1187
  color?: string;
1188
+
1032
1189
  /**
1033
1190
  * The weight of the font. The default value is 'normal'.
1034
1191
  */
1035
- fontWeight?: "normal" | "bold";
1192
+ fontWeight?: FontWeight;
1036
1193
  /**
1037
1194
  * The style of the font. The default value is 'normal'.
1038
1195
  */
@@ -1273,7 +1430,7 @@ export declare type UserSuppliedVideoDragData = {
1273
1430
  export declare type Value =
1274
1431
  | Primitive
1275
1432
  | ObjectPrimitive
1276
- | Value[]
1433
+ | Exclude<Value, undefined>[]
1277
1434
  | {
1278
1435
  [key: string]: Value;
1279
1436
  };
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initAppElement = exports.getCurrentPageContext = exports.addAudioTrack = exports.addNativeElement = exports.ui = exports.requestExport = exports.getDefaultPageDimensions = exports.addPage = void 0;
3
+ exports.initAppElement = exports.getCurrentPageContext = exports.addAudioTrack = exports.addNativeElement = exports.ui = exports.requestExport = exports.getDefaultPageDimensions = exports.setCurrentPageBackground = exports.addPage = exports.selection = void 0;
4
4
  const { canva } = window;
5
+ /**
6
+ * @public
7
+ * An alias for the DesignSelection interface, providing access to design selection related functionality
8
+ */
9
+ exports.selection = canva.designInteraction.selection;
5
10
  /**
6
11
  * @public
7
12
  * Adds a new page immediately after the currently selected page.
@@ -11,6 +16,15 @@ function addPage(opts) {
11
16
  return canva.designInteraction.addPage(opts);
12
17
  }
13
18
  exports.addPage = addPage;
19
+ /**
20
+ * @public
21
+ * Updates the background of the user's current page. The background can be a solid color,
22
+ * an image or a video.
23
+ */
24
+ function setCurrentPageBackground(opts) {
25
+ return canva.designInteraction.setCurrentPageBackground(opts);
26
+ }
27
+ exports.setCurrentPageBackground = setCurrentPageBackground;
14
28
  /**
15
29
  * @public
16
30
  * Gets the default dimensions that a new page will have when it is added to a design.
@@ -1,4 +1,9 @@
1
1
  const { canva } = window;
2
+ /**
3
+ * @public
4
+ * An alias for the DesignSelection interface, providing access to design selection related functionality
5
+ */
6
+ export const selection = canva.designInteraction.selection;
2
7
  /**
3
8
  * @public
4
9
  * Adds a new page immediately after the currently selected page.
@@ -7,6 +12,14 @@ const { canva } = window;
7
12
  export function addPage(opts) {
8
13
  return canva.designInteraction.addPage(opts);
9
14
  }
15
+ /**
16
+ * @public
17
+ * Updates the background of the user's current page. The background can be a solid color,
18
+ * an image or a video.
19
+ */
20
+ export function setCurrentPageBackground(opts) {
21
+ return canva.designInteraction.setCurrentPageBackground(opts);
22
+ }
10
23
  /**
11
24
  * @public
12
25
  * Gets the default dimensions that a new page will have when it is added to a design.
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@canva/design",
3
- "version": "0.0.1-rc.1",
3
+ "version": "1.4.0",
4
4
  "description": "The Canva Apps SDK design 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/design/index.js",
11
11
  "module": "lib/esm/sdk/design/index.js",
@@ -16,4 +16,4 @@
16
16
  }
17
17
  },
18
18
  "typings": "./index.d.ts"
19
- }
19
+ }