@canva/cli 1.14.0 → 1.15.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/CHANGELOG.md +13 -0
- package/README.md +1 -0
- package/cli.js +308 -308
- package/package.json +1 -1
- package/templates/base/backend/base_backend/create.ts +10 -0
- package/templates/base/backend/routers/auth.ts +12 -9
- package/templates/base/package.json +3 -3
- package/templates/content_publisher/README.md +58 -0
- package/templates/content_publisher/canva-app.json +17 -0
- package/templates/content_publisher/declarations/declarations.d.ts +29 -0
- package/templates/content_publisher/eslint.config.mjs +14 -0
- package/templates/content_publisher/package.json +90 -0
- package/templates/content_publisher/scripts/copy_env.ts +13 -0
- package/templates/content_publisher/scripts/ssl/ssl.ts +131 -0
- package/templates/content_publisher/scripts/start/app_runner.ts +223 -0
- package/templates/content_publisher/scripts/start/context.ts +171 -0
- package/templates/content_publisher/scripts/start/start.ts +46 -0
- package/templates/content_publisher/src/index.tsx +4 -0
- package/templates/content_publisher/src/intents/content_publisher/index.tsx +113 -0
- package/templates/content_publisher/src/intents/content_publisher/post_preview.tsx +226 -0
- package/templates/content_publisher/src/intents/content_publisher/preview_ui.tsx +53 -0
- package/templates/content_publisher/src/intents/content_publisher/settings_ui.tsx +71 -0
- package/templates/content_publisher/src/intents/content_publisher/types.ts +29 -0
- package/templates/content_publisher/styles/components.css +56 -0
- package/templates/content_publisher/styles/preview_ui.css +88 -0
- package/templates/content_publisher/tsconfig.json +56 -0
- package/templates/content_publisher/webpack.config.ts +247 -0
- package/templates/dam/backend/server.ts +2 -3
- package/templates/dam/package.json +5 -4
- package/templates/dam/utils/backend/base_backend/create.ts +10 -0
- package/templates/data_connector/package.json +3 -3
- package/templates/gen_ai/backend/server.ts +2 -3
- package/templates/gen_ai/package.json +4 -3
- package/templates/gen_ai/utils/backend/base_backend/create.ts +10 -0
- package/templates/hello_world/package.json +3 -3
- package/templates/mls/package.json +3 -3
- package/templates/base/backend/jwt_middleware/index.ts +0 -1
- package/templates/base/backend/jwt_middleware/jwt_middleware.ts +0 -224
- package/templates/dam/utils/backend/jwt_middleware/index.ts +0 -1
- package/templates/dam/utils/backend/jwt_middleware/jwt_middleware.ts +0 -224
- package/templates/gen_ai/utils/backend/jwt_middleware/index.ts +0 -1
- package/templates/gen_ai/utils/backend/jwt_middleware/jwt_middleware.ts +0 -224
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
interface CliArgs {
|
|
5
|
+
example?: string;
|
|
6
|
+
useHttps: boolean;
|
|
7
|
+
ngrok: boolean;
|
|
8
|
+
preview: boolean;
|
|
9
|
+
overrideFrontendPort?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface EnvVars {
|
|
13
|
+
frontendPort: number;
|
|
14
|
+
backendPort: number;
|
|
15
|
+
hmrEnabled: boolean;
|
|
16
|
+
appId?: string;
|
|
17
|
+
appOrigin?: string;
|
|
18
|
+
backendHost?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Context {
|
|
22
|
+
private readonly envVars: EnvVars;
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
private env: NodeJS.ProcessEnv = process.env,
|
|
26
|
+
private readonly args: CliArgs,
|
|
27
|
+
) {
|
|
28
|
+
this.envVars = this.parseAndValidateEnvironmentVariables();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static get srcDir() {
|
|
32
|
+
const src = path.join(Context.rootDir, "src");
|
|
33
|
+
|
|
34
|
+
if (!fs.existsSync(src)) {
|
|
35
|
+
throw new Error(`Directory does not exist: ${src}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return src;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static get readmeDir() {
|
|
42
|
+
return path.join(Context.rootDir, "README.md");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get entryDir() {
|
|
46
|
+
return Context.srcDir;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get ngrokEnabled() {
|
|
50
|
+
return this.args.ngrok;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get hmrEnabled() {
|
|
54
|
+
return this.envVars.hmrEnabled;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get httpsEnabled() {
|
|
58
|
+
return this.args.useHttps;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get frontendEntryPath() {
|
|
62
|
+
const frontendEntryPath = path.join(this.entryDir, "index.tsx");
|
|
63
|
+
|
|
64
|
+
if (!fs.existsSync(frontendEntryPath)) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`Entry point for frontend does not exist: ${frontendEntryPath}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return frontendEntryPath;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get frontendUrl() {
|
|
74
|
+
return `${this.protocol}://localhost:${this.frontendPort}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get frontendPort() {
|
|
78
|
+
return this.args.overrideFrontendPort || this.envVars.frontendPort;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get developerBackendEntryPath(): string | undefined {
|
|
82
|
+
const developerBackendEntryPath = path.join(
|
|
83
|
+
Context.rootDir,
|
|
84
|
+
"backend",
|
|
85
|
+
"server.ts",
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
if (!fs.existsSync(developerBackendEntryPath)) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return developerBackendEntryPath;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get backendUrl() {
|
|
96
|
+
return `${this.protocol}://localhost:${this.envVars.backendPort}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get backendHost() {
|
|
100
|
+
let backendHost = this.envVars.backendHost;
|
|
101
|
+
|
|
102
|
+
// if there's no custom URL provided by the developer, we fallback to our localhost backend
|
|
103
|
+
if (!backendHost || backendHost.trim() === "") {
|
|
104
|
+
backendHost = this.backendUrl;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return backendHost;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get backendPort() {
|
|
111
|
+
return this.envVars.backendPort;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get appOrigin(): string | undefined {
|
|
115
|
+
return this.envVars.appOrigin;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get appId(): string | undefined {
|
|
119
|
+
return this.envVars.appId;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get openPreview(): boolean {
|
|
123
|
+
return this.args.preview;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private get protocol(): "https" | "http" {
|
|
127
|
+
return this.httpsEnabled ? "https" : "http";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private static get rootDir() {
|
|
131
|
+
return path.join(process.cwd());
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private parseAndValidateEnvironmentVariables(): EnvVars {
|
|
135
|
+
const {
|
|
136
|
+
CANVA_FRONTEND_PORT,
|
|
137
|
+
CANVA_BACKEND_PORT,
|
|
138
|
+
CANVA_BACKEND_HOST,
|
|
139
|
+
CANVA_APP_ID,
|
|
140
|
+
CANVA_APP_ORIGIN,
|
|
141
|
+
CANVA_HMR_ENABLED,
|
|
142
|
+
} = this.env;
|
|
143
|
+
|
|
144
|
+
if (!CANVA_FRONTEND_PORT) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
"CANVA_FRONTEND_PORT environment variable is not defined",
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!CANVA_BACKEND_PORT) {
|
|
151
|
+
throw new Error("CANVA_BACKEND_PORT environment variable is not defined");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const envVars: EnvVars = {
|
|
155
|
+
frontendPort: parseInt(CANVA_FRONTEND_PORT, 10),
|
|
156
|
+
backendPort: parseInt(CANVA_BACKEND_PORT, 10),
|
|
157
|
+
hmrEnabled: CANVA_HMR_ENABLED?.toLowerCase().trim() === "true",
|
|
158
|
+
appId: CANVA_APP_ID,
|
|
159
|
+
appOrigin: CANVA_APP_ORIGIN,
|
|
160
|
+
backendHost: CANVA_BACKEND_HOST,
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
if (envVars.hmrEnabled && envVars.appOrigin == null) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
"CANVA_HMR_ENABLED environment variable is TRUE, but CANVA_APP_ORIGIN is not set. Refer to the instructions in the README.md on configuring HMR.",
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return envVars;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import yargs from "yargs";
|
|
3
|
+
import { hideBin } from "yargs/helpers";
|
|
4
|
+
import { AppRunner } from "./app_runner";
|
|
5
|
+
import { Context } from "./context";
|
|
6
|
+
|
|
7
|
+
const appRunner = new AppRunner();
|
|
8
|
+
|
|
9
|
+
yargs(hideBin(process.argv))
|
|
10
|
+
.version(false)
|
|
11
|
+
.help()
|
|
12
|
+
.option("ngrok", {
|
|
13
|
+
description: "Run backend server via ngrok.",
|
|
14
|
+
type: "boolean",
|
|
15
|
+
// npm swallows command line args instead of forwarding to the script
|
|
16
|
+
default:
|
|
17
|
+
process.env.npm_config_ngrok?.toLocaleLowerCase().trim() === "true",
|
|
18
|
+
})
|
|
19
|
+
.option("use-https", {
|
|
20
|
+
description: "Start local development server on HTTPS.",
|
|
21
|
+
type: "boolean",
|
|
22
|
+
// npm swallows commands line args instead of forwarding to the script
|
|
23
|
+
default:
|
|
24
|
+
process.env.npm_config_use_https?.toLocaleLowerCase().trim() === "true",
|
|
25
|
+
})
|
|
26
|
+
.option("override-frontend-port", {
|
|
27
|
+
description:
|
|
28
|
+
"Port to run the local development server on. Overrides the frontend port set in the .env file.",
|
|
29
|
+
type: "number",
|
|
30
|
+
alias: "p",
|
|
31
|
+
})
|
|
32
|
+
.option("preview", {
|
|
33
|
+
description: "Open the app in Canva.",
|
|
34
|
+
type: "boolean",
|
|
35
|
+
default: false,
|
|
36
|
+
})
|
|
37
|
+
.command(
|
|
38
|
+
"$0",
|
|
39
|
+
"Starts local development",
|
|
40
|
+
() => {},
|
|
41
|
+
(args) => {
|
|
42
|
+
const ctx = new Context(process.env, args);
|
|
43
|
+
appRunner.run(ctx);
|
|
44
|
+
},
|
|
45
|
+
)
|
|
46
|
+
.parse();
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { AppI18nProvider, initIntl } from "@canva/app-i18n-kit";
|
|
2
|
+
import { AppUiProvider } from "@canva/app-ui-kit";
|
|
3
|
+
import type {
|
|
4
|
+
ContentPublisherIntent,
|
|
5
|
+
GetPublishConfigurationResponse,
|
|
6
|
+
PublishContentRequest,
|
|
7
|
+
PublishContentResponse,
|
|
8
|
+
RenderPreviewUiRequest,
|
|
9
|
+
RenderSettingsUiRequest,
|
|
10
|
+
} from "@canva/intents/content";
|
|
11
|
+
import { createRoot } from "react-dom/client";
|
|
12
|
+
import "@canva/app-ui-kit/styles.css";
|
|
13
|
+
import { PreviewUi } from "./preview_ui";
|
|
14
|
+
import { SettingsUi } from "./settings_ui";
|
|
15
|
+
|
|
16
|
+
const intl = initIntl();
|
|
17
|
+
|
|
18
|
+
// Render the settings UI where users configure publishing options
|
|
19
|
+
function renderSettingsUi({
|
|
20
|
+
updatePublishSettings,
|
|
21
|
+
registerOnSettingsUiContextChange,
|
|
22
|
+
}: RenderSettingsUiRequest) {
|
|
23
|
+
const root = createRoot(document.getElementById("root") as Element);
|
|
24
|
+
root.render(
|
|
25
|
+
<AppI18nProvider>
|
|
26
|
+
<AppUiProvider>
|
|
27
|
+
<SettingsUi
|
|
28
|
+
updatePublishSettings={updatePublishSettings}
|
|
29
|
+
registerOnSettingsUiContextChange={registerOnSettingsUiContextChange}
|
|
30
|
+
/>
|
|
31
|
+
</AppUiProvider>
|
|
32
|
+
</AppI18nProvider>,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Render the preview UI showing how the content will appear after publishing
|
|
37
|
+
function renderPreviewUi({ registerOnPreviewChange }: RenderPreviewUiRequest) {
|
|
38
|
+
const root = createRoot(document.getElementById("root") as Element);
|
|
39
|
+
root.render(
|
|
40
|
+
<AppI18nProvider>
|
|
41
|
+
<AppUiProvider>
|
|
42
|
+
<PreviewUi registerOnPreviewChange={registerOnPreviewChange} />
|
|
43
|
+
</AppUiProvider>
|
|
44
|
+
</AppI18nProvider>,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Define the output types (publishing formats) available to users
|
|
49
|
+
// Canva automatically displays a dropdown selector when more than one output type is defined
|
|
50
|
+
async function getPublishConfiguration(): Promise<GetPublishConfigurationResponse> {
|
|
51
|
+
// TODO: Replace this with the output types supported by your platform (e.g., blog post, social media post, newsletter)
|
|
52
|
+
// https://www.canva.dev/docs/apps/api/preview/intents-content-prepare-content-publisher/#implementation
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
status: "completed",
|
|
56
|
+
outputTypes: [
|
|
57
|
+
{
|
|
58
|
+
id: "post",
|
|
59
|
+
displayName: intl.formatMessage({
|
|
60
|
+
defaultMessage: "Feed Post",
|
|
61
|
+
description:
|
|
62
|
+
"Label for publishing format shown in the output type dropdown",
|
|
63
|
+
}),
|
|
64
|
+
mediaSlots: [
|
|
65
|
+
{
|
|
66
|
+
id: "media",
|
|
67
|
+
displayName: intl.formatMessage({
|
|
68
|
+
defaultMessage: "Media",
|
|
69
|
+
description: "Label for the media upload slot",
|
|
70
|
+
}),
|
|
71
|
+
fileCount: { exact: 1 },
|
|
72
|
+
accepts: {
|
|
73
|
+
image: {
|
|
74
|
+
format: "png",
|
|
75
|
+
// Social media post aspect ratio range (portrait to landscape)
|
|
76
|
+
aspectRatio: { min: 4 / 5, max: 1.91 / 1 },
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Handle the actual publishing when the user clicks the publish button
|
|
87
|
+
// In production, this should make API calls to your platform
|
|
88
|
+
async function publishContent(
|
|
89
|
+
request: PublishContentRequest,
|
|
90
|
+
): Promise<PublishContentResponse> {
|
|
91
|
+
// TODO: Replace this with your actual API integration
|
|
92
|
+
// Example: Upload media to your platform and create a post
|
|
93
|
+
// const uploadedMedia = await uploadToYourPlatform(params.outputMedia);
|
|
94
|
+
// const post = await createPostOnYourPlatform({
|
|
95
|
+
// media: uploadedMedia,
|
|
96
|
+
// caption: JSON.parse(params.publishRef).caption
|
|
97
|
+
// });
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
status: "completed",
|
|
101
|
+
externalId: "1234567890", // Your platform's unique identifier for this post
|
|
102
|
+
externalUrl: "todo_update_with_your_url", // Link to view the published content
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const contentPublisher: ContentPublisherIntent = {
|
|
107
|
+
renderSettingsUi,
|
|
108
|
+
renderPreviewUi,
|
|
109
|
+
getPublishConfiguration,
|
|
110
|
+
publishContent,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default contentPublisher;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Avatar,
|
|
3
|
+
Box,
|
|
4
|
+
ImageCard,
|
|
5
|
+
Placeholder,
|
|
6
|
+
Rows,
|
|
7
|
+
Text,
|
|
8
|
+
TextPlaceholder,
|
|
9
|
+
} from "@canva/app-ui-kit";
|
|
10
|
+
import type { Preview, PreviewMedia } from "@canva/intents/content";
|
|
11
|
+
import { FormattedMessage, useIntl } from "react-intl";
|
|
12
|
+
import * as styles from "../../../styles/preview_ui.css";
|
|
13
|
+
import type { PublishSettings } from "./types";
|
|
14
|
+
|
|
15
|
+
const IMAGE_WIDTH = 400;
|
|
16
|
+
|
|
17
|
+
interface PreviewProps {
|
|
18
|
+
previewMedia: PreviewMedia[] | undefined;
|
|
19
|
+
settings: PublishSettings | undefined;
|
|
20
|
+
username: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// TODO: Customize this preview to match what the published content will look like on your platform.
|
|
24
|
+
// This example shows a generic social media post with avatar, image, and caption.
|
|
25
|
+
// Consider: platform-specific dimensions, branding colors, and UI elements.
|
|
26
|
+
export const PostPreview = ({
|
|
27
|
+
previewMedia,
|
|
28
|
+
settings,
|
|
29
|
+
username,
|
|
30
|
+
}: PreviewProps) => {
|
|
31
|
+
const isLoading = !previewMedia;
|
|
32
|
+
|
|
33
|
+
const caption = settings?.caption;
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<Box
|
|
37
|
+
className={styles.wrapper}
|
|
38
|
+
background="surface"
|
|
39
|
+
borderRadius="large"
|
|
40
|
+
padding="2u"
|
|
41
|
+
border="standard"
|
|
42
|
+
>
|
|
43
|
+
<Rows spacing="2u">
|
|
44
|
+
<UserInfo isLoading={isLoading} username={username} />
|
|
45
|
+
<ImagePreview previewMedia={previewMedia} />
|
|
46
|
+
<Caption isLoading={isLoading} caption={caption} />
|
|
47
|
+
</Rows>
|
|
48
|
+
</Box>
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Renders user profile information with avatar and username
|
|
53
|
+
const UserInfo = ({
|
|
54
|
+
isLoading,
|
|
55
|
+
username,
|
|
56
|
+
}: {
|
|
57
|
+
isLoading: boolean;
|
|
58
|
+
username: string;
|
|
59
|
+
}) => {
|
|
60
|
+
return (
|
|
61
|
+
<div className={styles.user}>
|
|
62
|
+
<Box className={styles.avatar}>
|
|
63
|
+
<Avatar name={username} />
|
|
64
|
+
</Box>
|
|
65
|
+
{isLoading ? (
|
|
66
|
+
<div className={styles.textPlaceholder}>
|
|
67
|
+
<TextPlaceholder size="medium" />
|
|
68
|
+
</div>
|
|
69
|
+
) : (
|
|
70
|
+
<Text size="small" variant="bold">
|
|
71
|
+
{username}
|
|
72
|
+
</Text>
|
|
73
|
+
)}
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Renders the post caption with username
|
|
79
|
+
const Caption = ({
|
|
80
|
+
isLoading,
|
|
81
|
+
caption,
|
|
82
|
+
}: {
|
|
83
|
+
isLoading: boolean;
|
|
84
|
+
caption: string | undefined;
|
|
85
|
+
}) => {
|
|
86
|
+
return (
|
|
87
|
+
<>
|
|
88
|
+
{isLoading ? (
|
|
89
|
+
<div className={styles.textPlaceholder}>
|
|
90
|
+
<TextPlaceholder size="medium" />
|
|
91
|
+
</div>
|
|
92
|
+
) : (
|
|
93
|
+
caption && (
|
|
94
|
+
<Text lineClamp={2} size="small">
|
|
95
|
+
{caption}
|
|
96
|
+
</Text>
|
|
97
|
+
)
|
|
98
|
+
)}
|
|
99
|
+
</>
|
|
100
|
+
);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// Renders a single image post preview
|
|
104
|
+
const ImagePreview = ({
|
|
105
|
+
previewMedia,
|
|
106
|
+
}: {
|
|
107
|
+
previewMedia: PreviewMedia[] | undefined;
|
|
108
|
+
}) => {
|
|
109
|
+
const isLoading = !previewMedia;
|
|
110
|
+
const media = previewMedia?.find((media) => media.mediaSlotId === "media");
|
|
111
|
+
const fullWidth = (media?.previews.length ?? 1) * IMAGE_WIDTH;
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<Box borderRadius="large" className={styles.imageContainer}>
|
|
115
|
+
{isLoading || !media?.previews.length ? (
|
|
116
|
+
<div className={styles.imagePlaceholder}>
|
|
117
|
+
<Placeholder shape="rectangle" />
|
|
118
|
+
</div>
|
|
119
|
+
) : (
|
|
120
|
+
<div className={styles.imageRow} style={{ width: fullWidth }}>
|
|
121
|
+
{media?.previews.map((p) => {
|
|
122
|
+
return (
|
|
123
|
+
<div key={p.id} className={styles.image}>
|
|
124
|
+
<PreviewRenderer preview={p} />
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
})}
|
|
128
|
+
</div>
|
|
129
|
+
)}
|
|
130
|
+
</Box>
|
|
131
|
+
);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// Renders individual preview based on its type and status
|
|
135
|
+
const PreviewRenderer = ({ preview }: { preview: Preview }) => {
|
|
136
|
+
const intl = useIntl();
|
|
137
|
+
// Handle different preview states
|
|
138
|
+
if (preview.status === "loading") {
|
|
139
|
+
return (
|
|
140
|
+
<ImageStatusText
|
|
141
|
+
text={intl.formatMessage({
|
|
142
|
+
defaultMessage: "Loading...",
|
|
143
|
+
description: "Text displayed while the preview is loading",
|
|
144
|
+
})}
|
|
145
|
+
/>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (preview.status === "error") {
|
|
150
|
+
return (
|
|
151
|
+
<ImageStatusText
|
|
152
|
+
text={intl.formatMessage({
|
|
153
|
+
defaultMessage: "Error loading preview",
|
|
154
|
+
description:
|
|
155
|
+
"Text displayed when there is an error loading the preview",
|
|
156
|
+
})}
|
|
157
|
+
/>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Handle image previews (ready status)
|
|
162
|
+
if (isImagePreviewReady(preview)) {
|
|
163
|
+
return (
|
|
164
|
+
<ImageCard
|
|
165
|
+
alt={intl.formatMessage(
|
|
166
|
+
{
|
|
167
|
+
defaultMessage: "Image preview {previewId}",
|
|
168
|
+
description: "Alt text for image preview in the preview UI",
|
|
169
|
+
},
|
|
170
|
+
{ previewId: preview.id },
|
|
171
|
+
)}
|
|
172
|
+
thumbnailUrl={preview.url}
|
|
173
|
+
/>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Fallback for unknown preview types
|
|
178
|
+
return (
|
|
179
|
+
<Box
|
|
180
|
+
width="full"
|
|
181
|
+
height="full"
|
|
182
|
+
padding="2u"
|
|
183
|
+
display="flex"
|
|
184
|
+
alignItems="center"
|
|
185
|
+
justifyContent="center"
|
|
186
|
+
>
|
|
187
|
+
<Text size="medium" tone="tertiary" alignment="center">
|
|
188
|
+
<FormattedMessage
|
|
189
|
+
defaultMessage="Preview not available"
|
|
190
|
+
description="Text displayed when the preview type is not supported"
|
|
191
|
+
/>
|
|
192
|
+
</Text>
|
|
193
|
+
</Box>
|
|
194
|
+
);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Helper component to display status text for loading/error states
|
|
198
|
+
const ImageStatusText = ({ text }: { text: string }) => (
|
|
199
|
+
<Box
|
|
200
|
+
width="full"
|
|
201
|
+
height="full"
|
|
202
|
+
padding="2u"
|
|
203
|
+
display="flex"
|
|
204
|
+
alignItems="center"
|
|
205
|
+
justifyContent="center"
|
|
206
|
+
>
|
|
207
|
+
<Text size="medium" tone="tertiary" alignment="center">
|
|
208
|
+
{text}
|
|
209
|
+
</Text>
|
|
210
|
+
</Box>
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Type guard to check if a preview is an image preview that's ready to display
|
|
215
|
+
*/
|
|
216
|
+
export function isImagePreviewReady(preview: Preview): preview is Preview & {
|
|
217
|
+
kind: "image";
|
|
218
|
+
status: "ready";
|
|
219
|
+
url: string;
|
|
220
|
+
} {
|
|
221
|
+
return (
|
|
222
|
+
preview.kind === "image" &&
|
|
223
|
+
preview.status === "ready" &&
|
|
224
|
+
preview.url != null
|
|
225
|
+
);
|
|
226
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { OutputType, PreviewMedia } from "@canva/intents/content";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import * as styles from "../../../styles/preview_ui.css";
|
|
4
|
+
import { PostPreview } from "./post_preview";
|
|
5
|
+
import { parsePublishSettings } from "./types";
|
|
6
|
+
|
|
7
|
+
interface PreviewUiProps {
|
|
8
|
+
registerOnPreviewChange: (
|
|
9
|
+
callback: (opts: {
|
|
10
|
+
previewMedia: PreviewMedia[];
|
|
11
|
+
outputType: OutputType;
|
|
12
|
+
publishRef?: string;
|
|
13
|
+
}) => void,
|
|
14
|
+
) => () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// TODO: Replace with real user data from your platform
|
|
18
|
+
// After implementing authentication (see README), fetch the connected user's profile
|
|
19
|
+
// to display their actual username and avatar in the preview.
|
|
20
|
+
const username = "username";
|
|
21
|
+
|
|
22
|
+
// Main preview UI component that receives preview updates when settings or pages change.
|
|
23
|
+
// Preview UI is more flexible to align with your platform's design system, so it is not constrained to the Canva design system.
|
|
24
|
+
export const PreviewUi = ({ registerOnPreviewChange }: PreviewUiProps) => {
|
|
25
|
+
const [previewData, setPreviewData] = useState<{
|
|
26
|
+
previewMedia: PreviewMedia[];
|
|
27
|
+
outputType: OutputType;
|
|
28
|
+
publishRef?: string;
|
|
29
|
+
} | null>(null);
|
|
30
|
+
|
|
31
|
+
// Register to receive preview updates whenever settings or pages change
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const dispose = registerOnPreviewChange((data) => {
|
|
34
|
+
setPreviewData(data);
|
|
35
|
+
});
|
|
36
|
+
return dispose;
|
|
37
|
+
}, [registerOnPreviewChange]);
|
|
38
|
+
|
|
39
|
+
const { previewMedia, publishRef, outputType } = previewData ?? {};
|
|
40
|
+
const publishSettings = parsePublishSettings(publishRef);
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<div className={styles.container}>
|
|
44
|
+
{outputType?.id === "post" && (
|
|
45
|
+
<PostPreview
|
|
46
|
+
previewMedia={previewMedia}
|
|
47
|
+
settings={publishSettings}
|
|
48
|
+
username={username}
|
|
49
|
+
/>
|
|
50
|
+
)}
|
|
51
|
+
</div>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { FormField, Rows, Text, TextInput } from "@canva/app-ui-kit";
|
|
2
|
+
import type {
|
|
3
|
+
PublishRefValidityState,
|
|
4
|
+
RenderSettingsUiRequest,
|
|
5
|
+
SettingsUiContext,
|
|
6
|
+
} from "@canva/intents/content";
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
8
|
+
import { useIntl } from "react-intl";
|
|
9
|
+
import * as styles from "styles/components.css";
|
|
10
|
+
import type { PublishSettings } from "./types";
|
|
11
|
+
|
|
12
|
+
// Settings UI component for configuring publish settings
|
|
13
|
+
export const SettingsUi = ({
|
|
14
|
+
updatePublishSettings,
|
|
15
|
+
registerOnSettingsUiContextChange,
|
|
16
|
+
}: RenderSettingsUiRequest) => {
|
|
17
|
+
const intl = useIntl();
|
|
18
|
+
const [settings, setSettings] = useState<PublishSettings>({ caption: "" });
|
|
19
|
+
const [settingsUiContext, setSettingsUiContext] =
|
|
20
|
+
useState<SettingsUiContext | null>(null);
|
|
21
|
+
|
|
22
|
+
// Listen for settings UI context changes (e.g., when output type changes)
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const dispose = registerOnSettingsUiContextChange((context) => {
|
|
25
|
+
setSettingsUiContext(context);
|
|
26
|
+
});
|
|
27
|
+
return dispose;
|
|
28
|
+
}, [registerOnSettingsUiContextChange]);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<div className={styles.scrollContainer}>
|
|
32
|
+
<Rows spacing="2u">
|
|
33
|
+
{settingsUiContext?.outputType.displayName && (
|
|
34
|
+
<Text>{settingsUiContext?.outputType.displayName}</Text>
|
|
35
|
+
)}
|
|
36
|
+
<FormField
|
|
37
|
+
label={intl.formatMessage({
|
|
38
|
+
defaultMessage: "Caption",
|
|
39
|
+
description: "Label for the caption input field",
|
|
40
|
+
})}
|
|
41
|
+
control={(props) => (
|
|
42
|
+
<TextInput
|
|
43
|
+
{...props}
|
|
44
|
+
value={settings.caption}
|
|
45
|
+
onChange={(caption) => {
|
|
46
|
+
const updatedSettings = { ...settings, caption };
|
|
47
|
+
setSettings(updatedSettings);
|
|
48
|
+
updatePublishSettings({
|
|
49
|
+
publishRef: JSON.stringify(settings),
|
|
50
|
+
validityState: validatePublishRef(settings),
|
|
51
|
+
});
|
|
52
|
+
}}
|
|
53
|
+
/>
|
|
54
|
+
)}
|
|
55
|
+
/>
|
|
56
|
+
</Rows>
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Validates the publish settings to enable/disable the publish button
|
|
62
|
+
// Returns "valid" when all required fields are filled
|
|
63
|
+
const validatePublishRef = (
|
|
64
|
+
publishRef: PublishSettings,
|
|
65
|
+
): PublishRefValidityState => {
|
|
66
|
+
// caption is required
|
|
67
|
+
if (publishRef.caption.length === 0) {
|
|
68
|
+
return "invalid_missing_required_fields";
|
|
69
|
+
}
|
|
70
|
+
return "valid";
|
|
71
|
+
};
|