Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: jsonforms image upload renderer #272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5360635
fix(Image-Upload-Rendere): add image upload renderer to jsonforms, mo…
hanpuliu-charles Jul 5, 2024
67e821c
fix(image-control): added logic to display current image
hanpuliu-charles Jul 5, 2024
a171f1f
fix(image-control): add text for max image size
hanpuliu-charles Jul 5, 2024
671aa2a
Merge branch 'main' of github.com:opengovsg/isomer into hanpu/isom-12…
hanpuliu-charles Jul 9, 2024
3077683
Merge remote-tracking branch 'origin/main' into hanpu/isom-1216-creat…
hanpuliu-charles Jul 12, 2024
83928fd
fix(image-renderer): logic change to proxy preview image fetch throug…
hanpuliu-charles Jul 12, 2024
906b259
fix(image-prop-renderer): add feature(fetch image once on load, other…
hanpuliu-charles Jul 16, 2024
85bf482
style(image-renderer): clean up code remove useless prints
hanpuliu-charles Jul 16, 2024
687854f
Merge remote-tracking branch 'origin/main' into hanpu/isom-1216-creat…
hanpuliu-charles Jul 16, 2024
09ff019
fix(image-renderer): add error messages
hanpuliu-charles Jul 16, 2024
ad99c39
fix(schema): add format:'image' for schemas where imageurl is expecte…
hanpuliu-charles Jul 17, 2024
509669a
fix(pagerouter,-image-renderer,-constants): fix linting errors
hanpuliu-charles Jul 17, 2024
5e5a18e
Merge remote-tracking branch 'origin/main' into hanpu/isom-1216-creat…
hanpuliu-charles Jul 17, 2024
20810af
style(trpc.ts-iron-session.ts): linter fix
hanpuliu-charles Jul 17, 2024
d4d743c
fix(jsonformsimagecontrol): modify to presigned url upload and direct…
hanpuliu-charles Jul 19, 2024
392cb6b
Merge remote-tracking branch 'origin/main' into hanpu/isom-1216-creat…
hanpuliu-charles Jul 19, 2024
93d5005
fix(page.router.ts): remove read image endpoint from page.router.ts
hanpuliu-charles Jul 22, 2024
743fb97
fix(page.router.ts): fix import
hanpuliu-charles Jul 23, 2024
09d9253
Merge branch 'main' into hanpu/isom-1216-create-image-upload-custom-r…
hanpuliu-charles Jul 23, 2024
1543498
Merge branch 'main' into hanpu/isom-1216-create-image-upload-custom-r…
hanpuliu-charles Jul 23, 2024
8fbc23b
style(page.router.ts): rename getPresign... to getPresigned
hanpuliu-charles Jul 24, 2024
a119866
style(page.router.ts): correct import
hanpuliu-charles Jul 24, 2024
7d37a8a
fix(image-renderer): remove upload logic
hanpuliu-charles Jul 25, 2024
af13e3e
fix(next.config.mjs): remove blob from img-src
hanpuliu-charles Jul 25, 2024
09c69dd
fix(page.router): remove added endpoints for presignedurl generation
hanpuliu-charles Jul 25, 2024
08c5982
fix(0.1.0): remove 0.1.0.json
hanpuliu-charles Jul 25, 2024
a27117e
fix(next.config.mjs): need to add blob: to imgsrc for content policy …
hanpuliu-charles Jul 25, 2024
ae050c4
Merge branch 'main' into hanpu/isom-1216-create-image-upload-custom-r…
hanpuliu-charles Jul 26, 2024
4b733a4
Merge branch 'main' into hanpu/isom-1216-create-image-upload-custom-r…
hanpuliu-charles Jul 26, 2024
6ebea0d
style(JsonFormsImageControl): remove unused imports
hanpuliu-charles Jul 26, 2024
db0752e
style(utils(blob-<=>-dataurl-conversion-helper_): removed utils file(…
hanpuliu-charles Jul 29, 2024
e0b0e7d
style(image-custom-renderer): change placeholder path to placeholder …
hanpuliu-charles Jul 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/studio/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ContentSecurityPolicy = `
font-src 'self' https: data:;
form-action 'self';
frame-ancestors 'self';
img-src * data:;
img-src * data: blob:;
frame-src 'self';
object-src 'none';
script-src 'self' 'unsafe-eval';
Expand Down
1 change: 1 addition & 0 deletions apps/studio/src/constants/formBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const JSON_FORMS_RANKING = {
ArrayControl: 4,
BooleanControl: 2,
DropdownControl: 2,
ImageControl: 2,
hanpuliu-charles marked this conversation as resolved.
Show resolved Hide resolved
IntegerControl: 4,
TextControl: 1,
ObjectControl: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
jsonFormsDropdownControlTester,
jsonFormsGroupLayoutRenderer,
jsonFormsGroupLayoutTester,
JsonFormsImageControl,
jsonFormsImageControlTester,
JsonFormsIntegerControl,
jsonFormsIntegerControlTester,
JsonFormsLinkControl,
Expand All @@ -43,6 +45,7 @@ const renderers: JsonFormsRendererRegistryEntry[] = [
renderer: JsonFormsDropdownControl,
},
{ tester: jsonFormsIntegerControlTester, renderer: JsonFormsIntegerControl },
{ tester: jsonFormsImageControlTester, renderer: JsonFormsImageControl },
{ tester: jsonFormsLinkControlTester, renderer: JsonFormsLinkControl },
{ tester: jsonFormsTextControlTester, renderer: JsonFormsTextControl },
{ tester: jsonFormsAllOfControlTester, renderer: JsonFormsAllOfControl },
Expand Down
hanpuliu-charles marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { ControlProps, RankedTester } from "@jsonforms/core"
import { useState } from "react"
import { Box, FormControl, Text } from "@chakra-ui/react"
import { and, isStringControl, rankWith, schemaMatches } from "@jsonforms/core"
import { withJsonFormsControlProps } from "@jsonforms/react"
import { Attachment, FormLabel, useToast } from "@opengovsg/design-system-react"

import { JSON_FORMS_RANKING } from "~/constants/formBuilder"
import {
IMAGE_UPLOAD_ACCEPTED_MIME_TYPES,
MAX_IMG_FILE_SIZE_BYTES,
} from "./constants"

export const jsonFormsImageControlTester: RankedTester = rankWith(
JSON_FORMS_RANKING.ImageControl,
and(
isStringControl,
schemaMatches((schema) => schema.format === "image"),
),
)
export function JsonFormsImageControl({
label,
handleChange,
path,
description,
required,
}: ControlProps) {
const toast = useToast()

const [pendingFile, setPendingFile] = useState<File | undefined>()

return (
<Box py={2}>
<FormControl isRequired={required} isInvalid={!pendingFile}>
<FormLabel description={description}>{label}</FormLabel>
<Attachment
name="image-upload"
imagePreview="large"
multiple={false}
value={pendingFile}
onChange={(file) => {
if (file) {
setPendingFile(file)
// TODO: Upload file logic?
handleChange(path, "https://picsum.photos/id/237/200/300")
} else {
// NOTE: Do we need to update backend on removal of file?
hanpuliu-charles marked this conversation as resolved.
Show resolved Hide resolved
handleChange(path, "")
setPendingFile(undefined)
}
}}
onError={(error) => {
toast({
title: "Image error",
description: error,
status: "error",
})
}}
onRejection={(rejections) => {
if (rejections[0]?.errors[0]) {
toast({
title: "Image rejected",
description: rejections[0].errors[0].message,
status: "error",
})
}
}}
maxSize={MAX_IMG_FILE_SIZE_BYTES}
accept={IMAGE_UPLOAD_ACCEPTED_MIME_TYPES}
/>
<Text textStyle="body-2" textColor="base.content.medium" pt="0.5rem">
{`Maximum file size: ${MAX_IMG_FILE_SIZE_BYTES / 1000000} MB`}
</Text>
</FormControl>
</Box>
)
}

export default withJsonFormsControlProps(JsonFormsImageControl)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const MAX_IMG_FILE_SIZE_BYTES = 5000000
export const IMAGE_UPLOAD_ACCEPTED_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/gif",
"image/svg+xml",
"image/tiff",
"image/bmp",
"image/webp",
]
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export {
default as JsonFormsTextControl,
jsonFormsTextControlTester,
} from "./JsonFormsTextControl"
export {
default as JsonFormsImageControl,
jsonFormsImageControlTester,
} from "./JsonFormsImageControl"
1 change: 1 addition & 0 deletions packages/components/src/interfaces/complex/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const ImageSchema = Type.Object(
type: Type.Literal("image", { default: "image" }),
src: Type.String({
title: "Upload image",
format: "image",
}),
alt: Type.String({
title: "Alternate text",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/interfaces/complex/InfoCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const SingleCardSchema = Type.Object({
}),
imageUrl: Type.String({
title: "Upload image",
format: "image",
}),
imageAlt: Type.String({
title: "Alternate text",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/interfaces/complex/Infopic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const InfopicSchema = Type.Object(
imageSrc: Type.String({
title: "Upload image",
description: "The URL to the image",
format: "image",
}),
imageAlt: Type.Optional(
Type.String({
Expand Down
Loading