-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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]) | |
| : [] | |
| ); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.