@canva/design 0.0.1-rc.1 → 1.3.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 +73 -0
- package/index.d.ts +160 -6
- package/lib/cjs/sdk/design/public.js +15 -1
- package/lib/esm/sdk/design/public.js +13 -0
- package/package.json +2 -2
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
|
+

|
|
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>;
|
|
@@ -164,6 +167,29 @@ declare type CommonImageDragConfig = {
|
|
|
164
167
|
previewSize: Dimensions;
|
|
165
168
|
};
|
|
166
169
|
|
|
170
|
+
/**
|
|
171
|
+
* @public
|
|
172
|
+
* A snapshot of some part of the document content.
|
|
173
|
+
*
|
|
174
|
+
* @remarks
|
|
175
|
+
* You can mutate the values in the `contents` array and then persist those changes with the `save` method.
|
|
176
|
+
*/
|
|
177
|
+
export declare interface ContentDraft<T> {
|
|
178
|
+
/**
|
|
179
|
+
* The selected content.
|
|
180
|
+
*/
|
|
181
|
+
readonly contents: readonly T[];
|
|
182
|
+
/**
|
|
183
|
+
* Saves changes made to items in the `contents` array.
|
|
184
|
+
* Once saved, those changes are reflected in the user's design.
|
|
185
|
+
*
|
|
186
|
+
* @remarks
|
|
187
|
+
* Any other changes to the content since calling the `read` method, such as the user editing the content directly, will be overwritten.
|
|
188
|
+
* Only properties that are different from the original state are written to the design.
|
|
189
|
+
*/
|
|
190
|
+
save(): Promise<void>;
|
|
191
|
+
}
|
|
192
|
+
|
|
167
193
|
/**
|
|
168
194
|
* @public
|
|
169
195
|
* Represents X and Y coordinates.
|
|
@@ -179,6 +205,29 @@ export declare type Coordinates = {
|
|
|
179
205
|
y: number;
|
|
180
206
|
};
|
|
181
207
|
|
|
208
|
+
/**
|
|
209
|
+
* @public
|
|
210
|
+
* Provides methods for interacting with the user's selected content, such as images or text.
|
|
211
|
+
*/
|
|
212
|
+
export declare type DesignSelection = {
|
|
213
|
+
/**
|
|
214
|
+
* Registers a callback that runs when a user selects an element that contains the specified type of content.
|
|
215
|
+
*
|
|
216
|
+
* @remarks
|
|
217
|
+
* The callback fires immediately if content is already selected.
|
|
218
|
+
*/
|
|
219
|
+
registerOnChange<Scope extends SelectionScope>(opts: {
|
|
220
|
+
/**
|
|
221
|
+
* The type of content that, when selected, triggers the `onChange` callback.
|
|
222
|
+
*/
|
|
223
|
+
scope: Scope;
|
|
224
|
+
/**
|
|
225
|
+
* The callback to run when the selection changes.
|
|
226
|
+
*/
|
|
227
|
+
onChange(event: SelectionEvent<Scope>): void;
|
|
228
|
+
}): () => void;
|
|
229
|
+
};
|
|
230
|
+
|
|
182
231
|
/**
|
|
183
232
|
* @public
|
|
184
233
|
* Represents a width and a height.
|
|
@@ -442,6 +491,21 @@ export declare type Fill = {
|
|
|
442
491
|
asset?: ImageFill | VideoFill;
|
|
443
492
|
};
|
|
444
493
|
|
|
494
|
+
/**
|
|
495
|
+
* @public
|
|
496
|
+
* Font weights supported in the SDK.
|
|
497
|
+
**/
|
|
498
|
+
declare type FontWeight =
|
|
499
|
+
| "normal"
|
|
500
|
+
| "thin"
|
|
501
|
+
| "extralight"
|
|
502
|
+
| "light"
|
|
503
|
+
| "medium"
|
|
504
|
+
| "semibold"
|
|
505
|
+
| "bold"
|
|
506
|
+
| "ultrabold"
|
|
507
|
+
| "heavy";
|
|
508
|
+
|
|
445
509
|
/**
|
|
446
510
|
* Allows to get the context of currently selected page.
|
|
447
511
|
* @public
|
|
@@ -658,7 +722,6 @@ export declare type NativeImageElement = {
|
|
|
658
722
|
| {
|
|
659
723
|
/**
|
|
660
724
|
* 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
725
|
*/
|
|
663
726
|
dataUrl: string;
|
|
664
727
|
/**
|
|
@@ -853,6 +916,12 @@ export declare type NativeVideoElementWithBox = NativeVideoElement & Box;
|
|
|
853
916
|
*/
|
|
854
917
|
declare type ObjectPrimitive = Boolean | String;
|
|
855
918
|
|
|
919
|
+
/**
|
|
920
|
+
* @public
|
|
921
|
+
* The appearance of a page's background.
|
|
922
|
+
*/
|
|
923
|
+
export declare type PageBackgroundFill = Pick<Fill, "asset" | "color">;
|
|
924
|
+
|
|
856
925
|
/**
|
|
857
926
|
* @public
|
|
858
927
|
* Page context
|
|
@@ -934,9 +1003,9 @@ declare type Position = {
|
|
|
934
1003
|
* The types of primitive values that can be stored within an app element's data.
|
|
935
1004
|
*
|
|
936
1005
|
* @remarks
|
|
937
|
-
* All types of primitives are supported except for symbols
|
|
1006
|
+
* All types of primitives are supported except for symbols and bigints.
|
|
938
1007
|
*/
|
|
939
|
-
declare type Primitive = null | number | boolean | string;
|
|
1008
|
+
declare type Primitive = undefined | null | number | boolean | string;
|
|
940
1009
|
|
|
941
1010
|
/**
|
|
942
1011
|
* @public
|
|
@@ -947,6 +1016,90 @@ export declare function requestExport(
|
|
|
947
1016
|
request: ExportRequest
|
|
948
1017
|
): Promise<ExportResponse>;
|
|
949
1018
|
|
|
1019
|
+
/**
|
|
1020
|
+
* @public
|
|
1021
|
+
* An alias for the DesignSelection interface, providing access to design selection related functionality
|
|
1022
|
+
*/
|
|
1023
|
+
export declare const selection: DesignSelection;
|
|
1024
|
+
|
|
1025
|
+
/**
|
|
1026
|
+
* @public
|
|
1027
|
+
* Information about the user's selection. To access the selected content, call the `read` method.
|
|
1028
|
+
*/
|
|
1029
|
+
export declare interface SelectionEvent<Scope extends SelectionScope> {
|
|
1030
|
+
/**
|
|
1031
|
+
* The type of content that's selected.
|
|
1032
|
+
*/
|
|
1033
|
+
readonly scope: Scope;
|
|
1034
|
+
/**
|
|
1035
|
+
* The number of selected elements.
|
|
1036
|
+
*/
|
|
1037
|
+
readonly count: number;
|
|
1038
|
+
/**
|
|
1039
|
+
* Creates a snapshot of the selected content and returns a *draft* object.
|
|
1040
|
+
* The draft has a mutable `contents` property for making changes to the selected content.
|
|
1041
|
+
* Any changes made to `contents` are not immediately persisted or reflected in the user's design.
|
|
1042
|
+
* To persist the changes, call the `save` method that's available via the draft.
|
|
1043
|
+
*
|
|
1044
|
+
* @example Replacing text
|
|
1045
|
+
* ```
|
|
1046
|
+
* const draft = await selectionEvent.read();
|
|
1047
|
+
*
|
|
1048
|
+
* for(const content of draft.contents) {
|
|
1049
|
+
* // This change won't immediately appear in the user's design
|
|
1050
|
+
* content.text = "This is the new text";
|
|
1051
|
+
* }
|
|
1052
|
+
*
|
|
1053
|
+
* // The changes will now appear in the user's design
|
|
1054
|
+
* await draft.save();
|
|
1055
|
+
* ```
|
|
1056
|
+
*/
|
|
1057
|
+
read(): Promise<ContentDraft<SelectionValue<Scope>>>;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/**
|
|
1061
|
+
* @public
|
|
1062
|
+
* The type of content that apps can detect selection changes on.
|
|
1063
|
+
*/
|
|
1064
|
+
export declare type SelectionScope = "plaintext" | "image";
|
|
1065
|
+
|
|
1066
|
+
/**
|
|
1067
|
+
* @public
|
|
1068
|
+
* The selected content.
|
|
1069
|
+
*
|
|
1070
|
+
* @remarks
|
|
1071
|
+
* The value depends on the {@link SelectionScope}.
|
|
1072
|
+
*/
|
|
1073
|
+
export declare type SelectionValue<Scope extends SelectionScope> = {
|
|
1074
|
+
/**
|
|
1075
|
+
* The selected content when {@link SelectionScope} is `"plaintext"`.
|
|
1076
|
+
*/
|
|
1077
|
+
["plaintext"]: {
|
|
1078
|
+
/**
|
|
1079
|
+
* The text content of the element as plain text.
|
|
1080
|
+
*/
|
|
1081
|
+
text: string;
|
|
1082
|
+
};
|
|
1083
|
+
/**
|
|
1084
|
+
* The selected content when {@link SelectionScope} is `"image"`.
|
|
1085
|
+
*/
|
|
1086
|
+
["image"]: {
|
|
1087
|
+
/**
|
|
1088
|
+
* A unique identifier that points to an image asset in Canva's backend.
|
|
1089
|
+
*/
|
|
1090
|
+
ref: ImageRef;
|
|
1091
|
+
};
|
|
1092
|
+
}[Scope];
|
|
1093
|
+
|
|
1094
|
+
/**
|
|
1095
|
+
* @public
|
|
1096
|
+
* Updates the background of the user's current page. The background can be a solid color,
|
|
1097
|
+
* an image or a video.
|
|
1098
|
+
*/
|
|
1099
|
+
export declare function setCurrentPageBackground(
|
|
1100
|
+
opts: PageBackgroundFill
|
|
1101
|
+
): Promise<void>;
|
|
1102
|
+
|
|
950
1103
|
/**
|
|
951
1104
|
* @public
|
|
952
1105
|
* A path that defines the shape of a shape element.
|
|
@@ -1029,10 +1182,11 @@ declare type TextAttributes = {
|
|
|
1029
1182
|
* (e.g. #ff0099). The default value is #000000.
|
|
1030
1183
|
*/
|
|
1031
1184
|
color?: string;
|
|
1185
|
+
|
|
1032
1186
|
/**
|
|
1033
1187
|
* The weight of the font. The default value is 'normal'.
|
|
1034
1188
|
*/
|
|
1035
|
-
fontWeight?:
|
|
1189
|
+
fontWeight?: FontWeight;
|
|
1036
1190
|
/**
|
|
1037
1191
|
* The style of the font. The default value is 'normal'.
|
|
1038
1192
|
*/
|
|
@@ -1273,7 +1427,7 @@ export declare type UserSuppliedVideoDragData = {
|
|
|
1273
1427
|
export declare type Value =
|
|
1274
1428
|
| Primitive
|
|
1275
1429
|
| ObjectPrimitive
|
|
1276
|
-
| Value[]
|
|
1430
|
+
| Exclude<Value, undefined>[]
|
|
1277
1431
|
| {
|
|
1278
1432
|
[key: string]: Value;
|
|
1279
1433
|
};
|
|
@@ -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": "
|
|
3
|
+
"version": "1.3.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
|
|
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",
|