-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from digirati-co-uk/feature/image-annotations
Image with caption annotation type
- Loading branch information
Showing
10 changed files
with
211 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...es/creators/src/Annotation/CaptionedImageAnnotation/create-captioned-image-annotation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import type { InternationalString } from "@iiif/presentation-3"; | ||
import type { CreatorContext, CreatorFunctionContext } from "@manifest-editor/creator-api"; | ||
import { Input, InputContainer, InputLabel, LanguageFieldEditor } from "@manifest-editor/editors"; | ||
import { Button } from "@manifest-editor/ui/atoms/Button"; | ||
import { PaddedSidebarContainer } from "@manifest-editor/ui/atoms/PaddedSidebarContainer"; | ||
import { type FormEvent, useState } from "react"; | ||
|
||
export interface CreateCaptionedImageAnnotationPayload { | ||
label?: InternationalString; | ||
body: InternationalString; | ||
imageUrl: string; | ||
motivation?: string; | ||
height?: number; | ||
width?: number; | ||
} | ||
|
||
export async function createCaptionedImageAnnotation( | ||
data: CreateCaptionedImageAnnotationPayload, | ||
ctx: CreatorFunctionContext | ||
) { | ||
const annotation = { | ||
id: ctx.generateId("annotation"), | ||
type: "Annotation", | ||
}; | ||
|
||
const targetType = ctx.options.targetType as "Annotation" | "Canvas"; | ||
|
||
const languages = Object.keys(data.body); | ||
const bodies = []; | ||
for (const language of languages) { | ||
const body = (data.body as any)[language].join("\n"); | ||
if (body) { | ||
bodies.push( | ||
await ctx.create( | ||
"@manifest-editor/html-body-creator", | ||
{ | ||
language, | ||
body, | ||
}, | ||
{ parent: { resource: annotation, property: "items" } } | ||
) | ||
); | ||
} | ||
} | ||
|
||
const resource = await ctx.create( | ||
"@manifest-editor/image-url-creator", | ||
{ url: data.imageUrl }, | ||
{ | ||
parent: { resource: annotation, property: "body" }, | ||
} | ||
); | ||
|
||
if (resource) { | ||
bodies.push(resource); | ||
} | ||
|
||
if (targetType === "Annotation") { | ||
return ctx.embed({ | ||
...annotation, | ||
motivation: data.motivation || ctx.options.initialData?.motivation || "painting", | ||
body: bodies, | ||
target: ctx.getTarget(), | ||
}); | ||
} | ||
|
||
// @todo this might not make sense to be a canvas. | ||
if (targetType === "Canvas") { | ||
const canvasId = ctx.generateId("canvas"); | ||
const pageId = ctx.generateId("annotation-page", { | ||
id: canvasId, | ||
type: "Canvas", | ||
}); | ||
|
||
const annotationResource = ctx.embed({ | ||
...annotation, | ||
motivation: "painting", | ||
body: bodies, | ||
target: { | ||
type: "SpecificResource", | ||
source: { id: canvasId, type: "Canvas" }, | ||
}, | ||
}); | ||
|
||
const page = ctx.embed({ | ||
id: pageId, | ||
type: "AnnotationPage", | ||
items: [annotationResource], | ||
}); | ||
|
||
return ctx.embed({ | ||
id: canvasId, | ||
type: "Canvas", | ||
label: data.label || { en: ["Untitled HTML canvas"] }, | ||
height: data.height || 1000, | ||
width: data.width || 1000, | ||
items: [page], | ||
}); | ||
} | ||
} | ||
|
||
export function CreateCaptionedImageAnnotation(props: CreatorContext<CreateCaptionedImageAnnotationPayload>) { | ||
const [body, setBody] = useState<InternationalString>({ en: [""] }); | ||
|
||
const onSubmit = (e: FormEvent) => { | ||
e.preventDefault(); | ||
const data = new FormData(e.target as HTMLFormElement); | ||
const formData = Object.fromEntries(data.entries()) as any; | ||
|
||
props.runCreate({ | ||
body, | ||
imageUrl: formData.url, | ||
}); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={onSubmit}> | ||
<PaddedSidebarContainer> | ||
<InputContainer> | ||
<InputContainer $wide> | ||
<InputLabel htmlFor="id">Link to Image</InputLabel> | ||
<Input id="url" name="url" defaultValue="" /> | ||
</InputContainer> | ||
</InputContainer> | ||
|
||
<InputContainer> | ||
<InputLabel>Image Caption</InputLabel> | ||
<LanguageFieldEditor | ||
focusId={"html-content"} | ||
label={"HTML Content"} | ||
fields={body} | ||
onSave={(e: any) => setBody(e.toInternationalString())} | ||
/> | ||
</InputContainer> | ||
|
||
<Button type="submit">Create</Button> | ||
</PaddedSidebarContainer> | ||
</form> | ||
); | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/creators/src/Annotation/CaptionedImageAnnotation/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { AddImageIcon, HTMLIcon } from "@manifest-editor/components"; | ||
import type { CreatorDefinition } from "@manifest-editor/creator-api"; | ||
import { CreateCaptionedImageAnnotation, createCaptionedImageAnnotation } from "./create-captioned-image-annotation"; | ||
|
||
export const captionedImageAnnotation: CreatorDefinition = { | ||
id: "@manifest-editor/captioned-image-annotation", | ||
create: createCaptionedImageAnnotation, | ||
label: "Captioned image", | ||
summary: "Add an image from a URL with caption", | ||
icon: <AddImageIcon />, | ||
render(ctx) { | ||
return <CreateCaptionedImageAnnotation {...ctx} />; | ||
}, | ||
resourceType: "Annotation", | ||
resourceFields: ["id", "type", "motivation", "body", "target"], | ||
additionalTypes: ["Canvas"], | ||
supports: { | ||
initialData: true, | ||
parentTypes: ["AnnotationPage"], | ||
parentFields: ["items"], | ||
}, | ||
staticFields: { | ||
type: "Annotation", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters