-
Notifications
You must be signed in to change notification settings - Fork 1
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
Feature/create thumbnail view #77
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
25 changes: 25 additions & 0 deletions
25
packages/core/components/FileList/LazilyRenderedThumbnail.module.css
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 @@ | ||
.small-font { | ||
font-size: var(--smaller-font-size); | ||
} | ||
|
||
.file-label { | ||
overflow-wrap: anywhere; | ||
text-align: center; | ||
} | ||
|
||
.thumbnail-wrapper { | ||
padding: 10px | ||
} | ||
|
||
.no-thumbnail { | ||
fill: var(--grey); | ||
} | ||
|
||
.selected { | ||
background-color: #d4e3fc; | ||
} | ||
|
||
.focused { | ||
margin: 0; | ||
border: 2px solid #669bf4; | ||
} |
173 changes: 173 additions & 0 deletions
173
packages/core/components/FileList/LazilyRenderedThumbnail.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,173 @@ | ||
import classNames from "classnames"; | ||
import * as React from "react"; | ||
import { useSelector } from "react-redux"; | ||
|
||
import FileSet from "../../entity/FileSet"; | ||
import FileThumbnail from "../../components/FileThumbnail"; | ||
import SvgIcon from "../../components/SvgIcon"; | ||
import { selection } from "../../state"; | ||
import { OnSelect } from "./useFileSelector"; | ||
import { RENDERABLE_IMAGE_FORMATS, THUMBNAIL_SIZE_TO_NUM_COLUMNS } from "../../constants"; | ||
import { NO_IMAGE_ICON_PATH_DATA } from "../../icons"; | ||
|
||
import styles from "./LazilyRenderedThumbnail.module.css"; | ||
|
||
/** | ||
* Contextual data passed to LazilyRenderedThumbnails by react-window. Basically a light-weight React context. | ||
* The same data is passed to each LazilyRenderedThumbnail within the same FileGrid. | ||
* Follows the pattern set by LazilyRenderedRow | ||
*/ | ||
export interface LazilyRenderedThumbnailContext { | ||
fileSet: FileSet; | ||
itemCount: number; | ||
measuredWidth: number; | ||
onContextMenu: (evt: React.MouseEvent) => void; | ||
onSelect: OnSelect; | ||
} | ||
|
||
interface LazilyRenderedThumbnailProps { | ||
columnIndex: number; // injected by react-window | ||
data: LazilyRenderedThumbnailContext; // injected by react-window | ||
rowIndex: number; // injected by react-window | ||
style: React.CSSProperties; // injected by react-window | ||
} | ||
|
||
const MARGIN = 20; // px; | ||
|
||
/** | ||
* A single file in the listing of available files FMS. | ||
* Follows the pattern set by LazilyRenderedRow | ||
*/ | ||
export default function LazilyRenderedThumbnail(props: LazilyRenderedThumbnailProps) { | ||
const { | ||
data: { fileSet, itemCount, measuredWidth, onContextMenu, onSelect }, | ||
columnIndex, | ||
rowIndex, | ||
style, | ||
} = props; | ||
|
||
const shouldDisplaySmallFont = useSelector(selection.selectors.getShouldDisplaySmallFont); | ||
const fileSelection = useSelector(selection.selectors.getFileSelection); | ||
const fileGridColCount = useSelector(selection.selectors.getFileGridColumnCount); | ||
const overallIndex = fileGridColCount * rowIndex + columnIndex; | ||
const file = fileSet.getFileByIndex(overallIndex); | ||
const thumbnailSize = measuredWidth / fileGridColCount - 2 * MARGIN; | ||
|
||
const isSelected = React.useMemo(() => { | ||
return fileSelection.isSelected(fileSet, overallIndex); | ||
}, [fileSelection, fileSet, overallIndex]); | ||
|
||
const isFocused = React.useMemo(() => { | ||
return fileSelection.isFocused(fileSet, overallIndex); | ||
}, [fileSelection, fileSet, overallIndex]); | ||
|
||
const onClick = (evt: React.MouseEvent) => { | ||
evt.preventDefault(); | ||
evt.stopPropagation(); | ||
|
||
if (onSelect && file !== undefined) { | ||
onSelect( | ||
{ index: overallIndex, id: file.file_id }, | ||
{ | ||
// Details on different OS keybindings | ||
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent#Properties | ||
ctrlKeyIsPressed: evt.ctrlKey || evt.metaKey, | ||
shiftKeyIsPressed: evt.shiftKey, | ||
} | ||
); | ||
} | ||
}; | ||
|
||
// Display the start of the file name and at least part of the file type | ||
const clipFileName = (filename: string) => { | ||
if (fileGridColCount === THUMBNAIL_SIZE_TO_NUM_COLUMNS.SMALL && filename.length > 15) { | ||
return filename.slice(0, 6) + "..." + filename.slice(-4); | ||
} else if (filename.length > 20) { | ||
return filename.slice(0, 9) + "..." + filename.slice(-8); | ||
} | ||
return filename; | ||
}; | ||
|
||
// If the file has a thumbnail image specified, we want to display the specified thumbnail. | ||
// Otherwise, we want to display the file itself as the thumbnail if possible. | ||
// If there is no thumbnail and the file cannot be displayed as the thumbnail, show a no image icon | ||
// TODO: Add custom icons per file type | ||
let thumbnail = ( | ||
<SvgIcon | ||
height={thumbnailSize} | ||
pathData={NO_IMAGE_ICON_PATH_DATA} | ||
viewBox="0,1,22,22" | ||
width={thumbnailSize} | ||
className={classNames(styles.noThumbnail)} | ||
/> | ||
); | ||
if (file?.thumbnail) { | ||
// thumbnail exists | ||
thumbnail = ( | ||
<div | ||
className={classNames(styles.thumbnail)} | ||
style={{ height: thumbnailSize, maxWidth: thumbnailSize }} | ||
> | ||
<FileThumbnail | ||
uri={`http://aics.corp.alleninstitute.org/labkey/fmsfiles/image${file.thumbnail}`} | ||
/> | ||
</div> | ||
); | ||
} else if (file) { | ||
const isFileRenderableImage = RENDERABLE_IMAGE_FORMATS.some((format) => | ||
file?.file_name.toLowerCase().endsWith(format) | ||
); | ||
if (isFileRenderableImage) { | ||
// render the image as the thumbnail | ||
thumbnail = ( | ||
<div | ||
className={classNames(styles.fileThumbnailContainer, styles.thumbnail)} | ||
style={{ height: thumbnailSize, maxWidth: thumbnailSize }} | ||
> | ||
<FileThumbnail | ||
uri={`http://aics.corp.alleninstitute.org/labkey/fmsfiles/image${file.file_path}`} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
let content; | ||
if (file) { | ||
const filenameForRender = clipFileName(file?.file_name); | ||
content = ( | ||
<div | ||
onClick={onClick} | ||
className={classNames({ | ||
[styles.selected]: isSelected, | ||
[styles.focused]: isFocused, | ||
})} | ||
title={file?.file_name} | ||
> | ||
{thumbnail} | ||
<div | ||
className={classNames(styles.fileLabel, { | ||
[styles.smallFont]: | ||
shouldDisplaySmallFont || | ||
fileGridColCount === THUMBNAIL_SIZE_TO_NUM_COLUMNS.SMALL, | ||
})} | ||
> | ||
{filenameForRender} | ||
</div> | ||
</div> | ||
); | ||
} else if (overallIndex < itemCount) { | ||
// Grid will attempt to render a cell even if we're past the total index | ||
content = "Loading..."; | ||
} // No `else` since if past total index we stil want empty content to fill up the outer grid | ||
|
||
return ( | ||
<div | ||
className={classNames(styles.thumbnailWrapper)} | ||
style={style} | ||
onContextMenu={onContextMenu} | ||
> | ||
{content} | ||
</div> | ||
); | ||
} |
Oops, something went wrong.
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.
Made this change since I was having a hard time reading the buttons with the darker background