-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add canvas paintaings to media dialog #38
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,9 @@ | ||||||||||||||||||
| import { Dialog, Heading, ImageSelect } from "@components"; | ||||||||||||||||||
| import { usePlugin } from "@context"; | ||||||||||||||||||
| import type { CanvasNormalized, ContentResourceNormalized } from "@iiif/presentation-3-normalized"; | ||||||||||||||||||
| import { serializeConfigPresentation3, Traverse } from "@iiif/parser"; | ||||||||||||||||||
| import type { Canvas, ContentResource } from "@iiif/presentation-3"; | ||||||||||||||||||
| import type { Media } from "@types"; | ||||||||||||||||||
| import { getLabelByUserLanguage } from "@utils"; | ||||||||||||||||||
| import { getLabelByUserLanguage, updateIIIFImageRequestURI } from "@utils"; | ||||||||||||||||||
| import { FC, useEffect, useRef } from "react"; | ||||||||||||||||||
| import style from "./style.module.css"; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -23,6 +24,37 @@ function handleSelectedMedia( | |||||||||||||||||
| onUpdate(resources); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Though the resource may have `width` and `height` properties | ||||||||||||||||||
| // it's type does not include them. | ||||||||||||||||||
| // To ensure we can access them safely, without excessive type checking, | ||||||||||||||||||
| // cast the resource to `any` and then try to access them. | ||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||||||||||||
| function getDimensions(resource: any): { height: number; width: number } { | ||||||||||||||||||
| return { | ||||||||||||||||||
| height: resource?.height || 0, | ||||||||||||||||||
| width: resource?.width || 0, | ||||||||||||||||||
| }; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Format a caption string for a IIIF resource including its dimensions if available. | ||||||||||||||||||
| * | ||||||||||||||||||
| * @param topline the top line of the caption, e.g. "Thumbnail" | ||||||||||||||||||
| * @param resource the IIIF resource to get dimensions from | ||||||||||||||||||
| * @returns a formatted caption string | ||||||||||||||||||
| * | ||||||||||||||||||
| * @example | ||||||||||||||||||
| * ```ts | ||||||||||||||||||
| * formatCaption("Thumbnail", resource); | ||||||||||||||||||
| * // "Thumbnail\n(100 x 200)" | ||||||||||||||||||
| * `` | ||||||||||||||||||
| */ | ||||||||||||||||||
| function formatCaption(topline: string, resource: ContentResource) { | ||||||||||||||||||
| const { width, height } = getDimensions(resource); | ||||||||||||||||||
| const dimensions = width && height ? `\n(${width} x ${height})` : ""; | ||||||||||||||||||
| return topline + dimensions; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const CurrentView = () => { | ||||||||||||||||||
| const { state, dispatch } = usePlugin(); | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -88,134 +120,165 @@ const CurrentView = () => { | |||||||||||||||||
| ); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| const Placeholder = () => { | ||||||||||||||||||
| const Paintings: FC<{ canvas: Canvas }> = ({ canvas }) => { | ||||||||||||||||||
| const { state, dispatch } = usePlugin(); | ||||||||||||||||||
|
|
||||||||||||||||||
| function handleAddMedia(selected: boolean, media: Media) { | ||||||||||||||||||
| // when adding the media, update the size to be max | ||||||||||||||||||
| media.src = updateIIIFImageRequestURI(media.src, { | ||||||||||||||||||
| size: `max`, | ||||||||||||||||||
| }); | ||||||||||||||||||
| handleSelectedMedia(selected, media, state.selectedMedia, (resources) => | ||||||||||||||||||
| dispatch({ type: "setSelectedMedia", selectedMedia: resources }), | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Though the resource may have `width` and `height` properties | ||||||||||||||||||
| // it's type, ContentResource, does not include them. | ||||||||||||||||||
| // To ensure we can access them safely, | ||||||||||||||||||
| // cast the resource to `any` and then try to access them. | ||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||||||||||||
| function getDimensions(resource: any): { height: number; width: number } { | ||||||||||||||||||
| return { | ||||||||||||||||||
| height: resource.height || 0, | ||||||||||||||||||
| width: resource.width || 0, | ||||||||||||||||||
| }; | ||||||||||||||||||
| } | ||||||||||||||||||
| const paintings: ContentResource[] = []; | ||||||||||||||||||
| const traverse = new Traverse({ | ||||||||||||||||||
| contentResource: [ | ||||||||||||||||||
| (resource) => { | ||||||||||||||||||
| if (resource.type === "Image") { | ||||||||||||||||||
| paintings.push(resource); | ||||||||||||||||||
| } | ||||||||||||||||||
| }, | ||||||||||||||||||
| ], | ||||||||||||||||||
| }); | ||||||||||||||||||
| traverse.traverseCanvasItems(canvas); | ||||||||||||||||||
|
|
||||||||||||||||||
| function formatCaption(resource: ContentResourceNormalized) { | ||||||||||||||||||
| const { width, height } = getDimensions(resource); | ||||||||||||||||||
| const dimensions = width && height ? `\n(${width} x ${height})` : ""; | ||||||||||||||||||
| return `Placeholder${dimensions}`; | ||||||||||||||||||
| if (!paintings.length) { | ||||||||||||||||||
| return <></>; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const activeCanvas = state.activeCanvas; | ||||||||||||||||||
| const media: Media[] = paintings.reduce((acc, painting, i) => { | ||||||||||||||||||
| if (!painting.id) { | ||||||||||||||||||
| return acc; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!activeCanvas.placeholderCanvas) { | ||||||||||||||||||
| return <></>; | ||||||||||||||||||
| // @ts-expect-error - I don't want to go down the rabbit hole of type checking here | ||||||||||||||||||
| const label = getLabelByUserLanguage(painting?.label); | ||||||||||||||||||
|
Comment on lines
+157
to
+158
|
||||||||||||||||||
| // @ts-expect-error - I don't want to go down the rabbit hole of type checking here | |
| const label = getLabelByUserLanguage(painting?.label); | |
| // Ensure painting.label is of the expected type for getLabelByUserLanguage | |
| const label = getLabelByUserLanguage( | |
| painting && painting.label | |
| ? (painting.label as Parameters<typeof getLabelByUserLanguage>[0]) | |
| : [] | |
| ); |
Outdated
Copilot
AI
Sep 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using array index as React key is an anti-pattern that can cause rendering issues. Use a unique, stable identifier like thumbnail-${thumbnail.id} instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSDoc example has a malformed code block - it should end with three backticks instead of two.