@canva/cli 0.0.1-beta.7 → 0.0.1-beta.8

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.
@@ -0,0 +1,252 @@
1
+ import { TableWrapper } from "../table_wrapper";
2
+ import type { Cell, TableElement } from "@canva/design";
3
+
4
+ describe("TableWrapper", () => {
5
+ describe("create", () => {
6
+ it("should create a new table", () => {
7
+ const wrapper = TableWrapper.create(2, 3);
8
+ const element = wrapper.toElement();
9
+ expect(element.rows.length).toBe(2);
10
+ expect(element.rows[0].cells.length).toBe(3);
11
+ expect(element.rows[1].cells.length).toBe(3);
12
+ });
13
+ it("should throw an error if the row count is less than 1", () => {
14
+ expect(() => TableWrapper.create(0, 1)).toThrow();
15
+ });
16
+ it("should throw an error if the column count is less than 1", () => {
17
+ expect(() => TableWrapper.create(1, 0)).toThrow();
18
+ });
19
+ it("should throw an error if the number of cells exceeds 225", () => {
20
+ expect(() => TableWrapper.create(16, 15)).toThrow();
21
+ });
22
+ });
23
+
24
+ describe("fromElement", () => {
25
+ it("should create a table wrapper from an existing table element", () => {
26
+ const element: TableElement = {
27
+ type: "table",
28
+ rows: [
29
+ {
30
+ cells: [
31
+ { colSpan: 2, type: "empty" },
32
+ { type: "empty" },
33
+ { rowSpan: 2, type: "empty" },
34
+ ],
35
+ },
36
+ {
37
+ cells: [
38
+ {
39
+ type: "string",
40
+ value: "hello",
41
+ },
42
+ {
43
+ type: "empty",
44
+ attributes: { backgroundColor: "#ff00ff" },
45
+ },
46
+ { type: "empty" },
47
+ ],
48
+ },
49
+ ],
50
+ };
51
+ const wrapper = TableWrapper.fromElement(element);
52
+ expect(wrapper.toElement()).toEqual(element);
53
+
54
+ // Make sure the new element is not a reference
55
+ if (element.rows[1].cells[1]?.attributes) {
56
+ element.rows[1].cells[1].attributes.backgroundColor = "#ff0000";
57
+ }
58
+ expect(wrapper.toElement()).not.toEqual(element);
59
+ });
60
+ it("should throw an error if the element is not a table", () => {
61
+ const element = {
62
+ type: "TEXT",
63
+ children: ["hello"],
64
+ };
65
+ expect(() => {
66
+ TableWrapper.fromElement(element as unknown as TableElement);
67
+ }).toThrow();
68
+ });
69
+ it("should throw an error if the element has no rows", () => {
70
+ expect(() => {
71
+ TableWrapper.fromElement({
72
+ type: "TABLE",
73
+ } as unknown as TableElement);
74
+ }).toThrow();
75
+ expect(() => {
76
+ TableWrapper.fromElement({
77
+ type: "table",
78
+ rows: [],
79
+ });
80
+ }).toThrow();
81
+ });
82
+ it("should throw an error if any row of the element has no cells", () => {
83
+ const element: TableElement = {
84
+ type: "table",
85
+ rows: [{ cells: [] }],
86
+ };
87
+ expect(() => {
88
+ TableWrapper.fromElement(element);
89
+ }).toThrow();
90
+ });
91
+ it("should throw an error if number of cells is inconsistent", () => {
92
+ const element: TableElement = {
93
+ type: "table",
94
+ rows: [
95
+ { cells: [{ type: "empty" }, { type: "empty" }] },
96
+ { cells: [{ type: "empty" }] },
97
+ ],
98
+ };
99
+ expect(() => {
100
+ TableWrapper.fromElement(element);
101
+ }).toThrow();
102
+ });
103
+ });
104
+
105
+ describe("addRow", () => {
106
+ it("should add a row to the table", () => {
107
+ const wrapper = TableWrapper.create(2, 3);
108
+ wrapper.addRow(0);
109
+ const element = wrapper.toElement();
110
+ expect(element.rows.length).toBe(3);
111
+ expect(element.rows[0].cells.length).toBe(3);
112
+ });
113
+ it("should copy the fill value if top and bottom cells are the same", () => {
114
+ const wrapper = TableWrapper.create(2, 3);
115
+ wrapper.setCellDetails(1, 1, {
116
+ type: "empty",
117
+ attributes: { backgroundColor: "#ff0000" },
118
+ });
119
+ wrapper.setCellDetails(2, 1, {
120
+ type: "empty",
121
+ attributes: { backgroundColor: "#ff0000" },
122
+ });
123
+ wrapper.addRow(1);
124
+ const newCell = wrapper.getCellDetails(2, 1);
125
+ expect(newCell?.attributes).toBeDefined();
126
+ expect(newCell?.attributes?.backgroundColor).toBe("#ff0000");
127
+ });
128
+ it("should expand the row span if top and bottom cells belong to the same merged cell", () => {
129
+ const wrapper = TableWrapper.create(2, 3);
130
+ wrapper.setCellDetails(1, 1, { rowSpan: 2, type: "empty" });
131
+ wrapper.addRow(1);
132
+ const mergedCell = wrapper.getCellDetails(1, 1);
133
+ expect(mergedCell?.rowSpan).toBe(3);
134
+ });
135
+ it("should throw an error if the row position is out of bounds", () => {
136
+ const wrapper = TableWrapper.create(2, 3);
137
+ expect(() => wrapper.addRow(-1)).toThrow();
138
+ expect(() => wrapper.addRow(3)).toThrow();
139
+ });
140
+ });
141
+
142
+ describe("addColumn", () => {
143
+ it("should add a column to the table", () => {
144
+ const wrapper = TableWrapper.create(2, 3);
145
+ wrapper.addColumn(0);
146
+ const element = wrapper.toElement();
147
+ expect(element.rows.length).toBe(2);
148
+ expect(element.rows[0].cells.length).toBe(4);
149
+ });
150
+ it("should copy the fill value if left and right cells are the same", () => {
151
+ const wrapper = TableWrapper.create(2, 3);
152
+ wrapper.setCellDetails(1, 1, {
153
+ type: "empty",
154
+ attributes: { backgroundColor: "#ff0000" },
155
+ });
156
+ wrapper.setCellDetails(1, 2, {
157
+ type: "empty",
158
+ attributes: { backgroundColor: "#ff0000" },
159
+ });
160
+ wrapper.addColumn(1);
161
+ const newCell = wrapper.getCellDetails(1, 2);
162
+ expect(newCell?.attributes).toBeDefined();
163
+ expect(newCell?.attributes?.backgroundColor).toBe("#ff0000");
164
+ });
165
+ it("should expand the column span if left and right cells belong to the same merged cell", () => {
166
+ const wrapper = TableWrapper.create(2, 3);
167
+ wrapper.setCellDetails(1, 1, { type: "empty", colSpan: 2 });
168
+ wrapper.addColumn(1);
169
+ const mergedCell = wrapper.getCellDetails(1, 1);
170
+ expect(mergedCell?.colSpan).toBe(3);
171
+ });
172
+ it("should throw an error if the column position is out of bounds", () => {
173
+ const wrapper = TableWrapper.create(2, 3);
174
+ expect(() => wrapper.addColumn(-1)).toThrow();
175
+ expect(() => wrapper.addColumn(4)).toThrow();
176
+ });
177
+ });
178
+
179
+ describe("isGhostCell", () => {
180
+ it("should return true if the cell is a ghost cell", () => {
181
+ const wrapper = TableWrapper.create(2, 3);
182
+ wrapper.setCellDetails(1, 1, { type: "empty", colSpan: 2 });
183
+ expect(wrapper.isGhostCell(1, 1)).toBe(false);
184
+ expect(wrapper.isGhostCell(1, 2)).toBe(true);
185
+ expect(wrapper.isGhostCell(1, 3)).toBe(false);
186
+ });
187
+ it("should throw an error if the position is out of bounds", () => {
188
+ const wrapper = TableWrapper.create(2, 3);
189
+ expect(() => wrapper.isGhostCell(0, 1)).toThrow();
190
+ expect(() => wrapper.isGhostCell(3, 1)).toThrow();
191
+ expect(() => wrapper.isGhostCell(1, 0)).toThrow();
192
+ expect(() => wrapper.isGhostCell(1, 4)).toThrow();
193
+ });
194
+ });
195
+
196
+ describe("setCellDetails", () => {
197
+ it("should set the cell details", () => {
198
+ const wrapper = TableWrapper.create(2, 3);
199
+ wrapper.setCellDetails(1, 1, {
200
+ colSpan: 2,
201
+ rowSpan: 2,
202
+ type: "string",
203
+ value: "hello",
204
+ attributes: { backgroundColor: "#ff0000" },
205
+ });
206
+ expect(wrapper.isGhostCell(1, 1)).toBe(false);
207
+ expect(wrapper.isGhostCell(1, 2)).toBe(true);
208
+ expect(wrapper.isGhostCell(2, 1)).toBe(true);
209
+ expect(wrapper.isGhostCell(2, 2)).toBe(true);
210
+ const element = wrapper.toElement();
211
+ const firstCell = element.rows[0].cells[0] as Cell & {
212
+ type: "string";
213
+ value: string;
214
+ };
215
+ expect(firstCell?.type).toBe("string");
216
+ expect(firstCell.value).toBe("hello");
217
+ expect(firstCell.attributes).toBeDefined();
218
+ expect(firstCell.attributes?.backgroundColor).toBe("#ff0000");
219
+ });
220
+ it("should throw an error if the position is out of bounds", () => {
221
+ const wrapper = TableWrapper.create(2, 3);
222
+ expect(() => wrapper.setCellDetails(0, 1, { type: "empty" })).toThrow();
223
+ expect(() => wrapper.setCellDetails(3, 1, { type: "empty" })).toThrow();
224
+ expect(() => wrapper.setCellDetails(1, 0, { type: "empty" })).toThrow();
225
+ expect(() => wrapper.setCellDetails(1, 4, { type: "empty" })).toThrow();
226
+ });
227
+ it("should throw an error if the rowSpan is invalid", () => {
228
+ const wrapper = TableWrapper.create(2, 3);
229
+ expect(() =>
230
+ wrapper.setCellDetails(1, 1, { type: "empty", rowSpan: -1 }),
231
+ ).toThrow();
232
+ expect(() =>
233
+ wrapper.setCellDetails(1, 1, { type: "empty", rowSpan: 0 }),
234
+ ).toThrow();
235
+ expect(() =>
236
+ wrapper.setCellDetails(1, 1, { type: "empty", rowSpan: 3 }),
237
+ ).toThrow();
238
+ });
239
+ it("should throw an error if the colSpan is invalid", () => {
240
+ const wrapper = TableWrapper.create(2, 3);
241
+ expect(() =>
242
+ wrapper.setCellDetails(1, 1, { type: "empty", colSpan: -1 }),
243
+ ).toThrow();
244
+ expect(() =>
245
+ wrapper.setCellDetails(1, 1, { type: "empty", colSpan: 0 }),
246
+ ).toThrow();
247
+ expect(() =>
248
+ wrapper.setCellDetails(1, 1, { type: "empty", colSpan: 4 }),
249
+ ).toThrow();
250
+ });
251
+ });
252
+ });
@@ -0,0 +1,48 @@
1
+ import type {
2
+ EmbedElement,
3
+ ImageElement,
4
+ RichtextElement,
5
+ TableElement,
6
+ TextElement,
7
+ VideoElement,
8
+ } from "@canva/design";
9
+ import { addElementAtCursor, addElementAtPoint } from "@canva/design";
10
+ import { useFeatureSupport } from "./use_feature_support";
11
+ import { features } from "@canva/platform";
12
+ import { useEffect, useState } from "react";
13
+
14
+ type AddElementParams =
15
+ | ImageElement
16
+ | VideoElement
17
+ | EmbedElement
18
+ | TextElement
19
+ | RichtextElement
20
+ | TableElement;
21
+
22
+ export const useAddElement = () => {
23
+ const isSupported = useFeatureSupport();
24
+
25
+ // Store a wrapped addElement function that checks feature support
26
+ const [addElement, setAddElement] = useState(() => {
27
+ return (element: AddElementParams) => {
28
+ if (features.isSupported(addElementAtPoint)) {
29
+ return addElementAtPoint(element);
30
+ } else if (features.isSupported(addElementAtCursor)) {
31
+ return addElementAtCursor(element);
32
+ }
33
+ };
34
+ });
35
+
36
+ useEffect(() => {
37
+ const addElement = (element: AddElementParams) => {
38
+ if (isSupported(addElementAtPoint)) {
39
+ return addElementAtPoint(element);
40
+ } else if (isSupported(addElementAtCursor)) {
41
+ return addElementAtCursor(element);
42
+ }
43
+ };
44
+ setAddElement(() => addElement);
45
+ }, [isSupported]);
46
+
47
+ return addElement;
48
+ };
@@ -0,0 +1,28 @@
1
+ import { features } from "@canva/platform";
2
+ import type { Feature } from "@canva/platform";
3
+ import { useState, useEffect } from "react";
4
+
5
+ /**
6
+ * This hook allows re-rendering of a React component whenever
7
+ * the state of feature support changes in Canva.
8
+ *
9
+ * @returns isSupported - callback to inspect a Canva SDK method.
10
+ **/
11
+ export function useFeatureSupport() {
12
+ // Store a wrapped function that checks feature support
13
+ const [isSupported, setIsSupported] = useState(() => {
14
+ return (...args: Feature[]) => features.isSupported(...args);
15
+ });
16
+
17
+ useEffect(() => {
18
+ // create new function ref when feature support changes to trigger
19
+ // re-render
20
+ return features.registerOnSupportChange(() => {
21
+ setIsSupported(() => {
22
+ return (...args: Feature[]) => features.isSupported(...args);
23
+ });
24
+ });
25
+ }, []);
26
+
27
+ return isSupported;
28
+ }
@@ -0,0 +1,74 @@
1
+ import type {
2
+ AppProcessId,
3
+ OverlayOpenableEvent,
4
+ OverlayTarget,
5
+ } from "@canva/design";
6
+ import { overlay as designOverlay } from "@canva/design";
7
+ import type { CloseParams } from "@canva/platform";
8
+ import { appProcess } from "@canva/platform";
9
+ import { useEffect, useState } from "react";
10
+
11
+ const initialOverlayEvent: OverlayOpenableEvent<OverlayTarget> = {
12
+ canOpen: false,
13
+ reason: "",
14
+ };
15
+
16
+ /**
17
+ * Returns an object which contains the following field:
18
+ * 1. canOpen - a boolean indicate whether the overlay can be opened on the specified target.
19
+ * 2. isOpen - a boolean indicate whether the overlay is currently open.
20
+ * 3. open - a function to open an overlay on the specified target.
21
+ * 4. close - a function close the currently opened overlay.
22
+ * @param target The overlay target to register for whether we can open an overlay.
23
+ */
24
+ export function useOverlay<
25
+ T extends OverlayTarget,
26
+ C extends CloseParams = CloseParams,
27
+ >(
28
+ target: T,
29
+ ): {
30
+ canOpen: boolean;
31
+ isOpen: boolean;
32
+ open: (opts?: {
33
+ launchParameters?: unknown;
34
+ }) => Promise<AppProcessId | undefined>;
35
+ close: (opts: C) => Promise<void>;
36
+ } {
37
+ const [overlay, setOverlay] =
38
+ useState<OverlayOpenableEvent<T>>(initialOverlayEvent);
39
+ const [overlayId, setOverlayId] = useState<AppProcessId>();
40
+ const [isOpen, setIsOpen] = useState<boolean>(false);
41
+
42
+ useEffect(() => {
43
+ return designOverlay.registerOnCanOpen({
44
+ target,
45
+ onCanOpen: setOverlay,
46
+ });
47
+ }, []);
48
+
49
+ useEffect(() => {
50
+ if (overlayId) {
51
+ appProcess.registerOnStateChange(overlayId, ({ state }) =>
52
+ setIsOpen(state === "open"),
53
+ );
54
+ }
55
+ }, [overlayId]);
56
+
57
+ const open = async (
58
+ opts: { launchParameters?: unknown } = {},
59
+ ): Promise<AppProcessId | undefined> => {
60
+ if (overlay && overlay.canOpen) {
61
+ const overlayId = await overlay.open(opts);
62
+ setOverlayId(overlayId);
63
+ return overlayId;
64
+ }
65
+ };
66
+
67
+ const close = async (opts: C) => {
68
+ if (overlayId) {
69
+ appProcess.requestClose<C>(overlayId, opts);
70
+ }
71
+ };
72
+
73
+ return { canOpen: overlay.canOpen, isOpen, open, close };
74
+ }
@@ -0,0 +1,37 @@
1
+ import { selection as designSelection } from "@canva/design";
2
+ import type { SelectionEvent, SelectionScope } from "@canva/design";
3
+ import { useEffect, useState } from "react";
4
+
5
+ /**
6
+ * Returns a selection event, representing a user selection of the specified content type. The
7
+ * event contains methods to read a snapshot of the selected content, and optionally mutate it.
8
+ * This is a reactive value. Calling this multiple times will return different instances
9
+ * representing the same selection.
10
+ * @param scope The type of content to listen for selection changes on
11
+ */
12
+ export function useSelection<S extends SelectionScope>(
13
+ scope: S,
14
+ ): SelectionEvent<S> {
15
+ const [selection, setSelection] = useState<SelectionEvent<S>>({
16
+ scope,
17
+ count: 0,
18
+ read() {
19
+ return Promise.resolve({
20
+ contents: Object.freeze([]),
21
+ save() {
22
+ return Promise.resolve();
23
+ },
24
+ });
25
+ },
26
+ });
27
+
28
+ useEffect(() => {
29
+ const disposer = designSelection.registerOnChange({
30
+ scope,
31
+ onChange: setSelection,
32
+ });
33
+ return disposer;
34
+ }, [scope]);
35
+
36
+ return selection;
37
+ }
@@ -33,7 +33,10 @@
33
33
  "@formatjs/ts-transformer": "3.13.22",
34
34
  "@ngrok/ngrok": "1.4.1",
35
35
  "@svgr/webpack": "8.1.0",
36
+ "@types/express": "4.17.21",
37
+ "@types/express-serve-static-core": "4.19.6",
36
38
  "@types/jest": "29.5.14",
39
+ "@types/jsonwebtoken": "9.0.7",
37
40
  "@types/node": "20.10.0",
38
41
  "@types/node-fetch": "2.6.11",
39
42
  "@types/node-forge": "1.3.11",
@@ -54,7 +57,11 @@
54
57
  "eslint-plugin-formatjs": "5.2.2",
55
58
  "eslint-plugin-jest": "28.9.0",
56
59
  "eslint-plugin-react": "7.37.2",
60
+ "express": "4.21.1",
61
+ "express-basic-auth": "1.2.1",
57
62
  "jest": "29.7.0",
63
+ "jsonwebtoken": "9.0.2",
64
+ "jwks-rsa": "3.1.0",
58
65
  "mini-css-extract-plugin": "2.9.2",
59
66
  "node-fetch": "3.3.2",
60
67
  "node-forge": "1.3.1",