-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
30,261 additions
and
15,417 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
interface LoadFileArgs { | ||
defaultRepresentation?: boolean | ||
ext: string | ||
} | ||
|
||
interface IDecompressorRegistry { | ||
get(ext: string): (input: string) => string | ||
} | ||
|
||
interface StageParams { | ||
backgroundColor: string | ||
} | ||
|
||
declare module 'ngl' { | ||
export class Stage { | ||
constructor(wrapper: HTMLDivElement, options: StageParams) | ||
handleResize(): void | ||
loadFile(blob: Blob, options: LoadFileArgs): Promise<void> | ||
dispose(): void | ||
} | ||
|
||
export const DecompressorRegistry: IDecompressorRegistry | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { PromiseResult } from 'aws-sdk/lib/request' | ||
import * as R from 'ramda' | ||
import * as React from 'react' | ||
import { DecompressorRegistry } from 'ngl' | ||
|
||
import type { S3HandleBase } from 'utils/s3paths' | ||
|
||
import { PreviewData } from '../types' | ||
|
||
import * as utils from './utils' | ||
|
||
export const detect = R.pipe(utils.stripCompression, utils.extIs('.pdb')) | ||
|
||
const gzipDecompress = DecompressorRegistry.get('gz') | ||
|
||
interface NglLoaderProps { | ||
children: (result: $TSFixMe) => React.ReactNode | ||
handle: S3HandleBase | ||
} | ||
|
||
export const Loader = function NglLoader({ handle, children }: NglLoaderProps) { | ||
const data = utils.useObjectGetter(handle) | ||
const processed = utils.useProcessing( | ||
data.result, | ||
(r: PromiseResult<{ Body: Uint8Array | string }, null>) => { | ||
const compression = utils.getCompression(handle.key) | ||
const body = compression === 'gz' ? gzipDecompress(r.Body as string) : r.Body | ||
return PreviewData.Ngl({ blob: new Blob([body]) }) | ||
}, | ||
) | ||
const handled = utils.useErrorHandling(processed, { handle, retry: data.fetch }) | ||
return children(handled) | ||
} |
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,61 @@ | ||
import * as NGL from 'ngl' | ||
import * as React from 'react' | ||
import * as M from '@material-ui/core' | ||
|
||
async function renderNgl(blob: Blob, wrapperEl: HTMLDivElement, t: M.Theme) { | ||
const stage = new NGL.Stage(wrapperEl, { backgroundColor: t.palette.common.white }) | ||
|
||
const resizeObserver = new window.ResizeObserver(() => stage.handleResize()) | ||
resizeObserver.observe(wrapperEl) | ||
|
||
await stage.loadFile(blob, { | ||
defaultRepresentation: true, | ||
ext: 'pdb', | ||
}) | ||
return stage | ||
} | ||
|
||
const useStyles = M.makeStyles((t) => ({ | ||
root: { | ||
height: t.spacing(50), | ||
overflow: 'auto', | ||
resize: 'vertical', | ||
width: '100%', | ||
}, | ||
})) | ||
|
||
interface NglProps extends React.HTMLAttributes<HTMLDivElement> { | ||
blob: Blob | ||
} | ||
|
||
function Ngl({ blob, ...props }: NglProps) { | ||
const classes = useStyles() | ||
|
||
const t = M.useTheme() | ||
const viewport = React.useRef<HTMLDivElement | null>(null) | ||
|
||
const handleWheel = React.useCallback( | ||
(event) => { | ||
if (viewport.current?.contains(event.target)) { | ||
event.preventDefault() | ||
} | ||
}, | ||
[viewport], | ||
) | ||
|
||
React.useEffect(() => { | ||
let stage: NGL.Stage | ||
if (viewport.current) { | ||
renderNgl(blob, viewport.current, t).then((s) => (stage = s)) | ||
window.addEventListener('wheel', handleWheel, { passive: false }) | ||
} | ||
return () => { | ||
stage?.dispose() | ||
window.removeEventListener('wheel', handleWheel) | ||
} | ||
}, [viewport, blob, handleWheel, t]) | ||
|
||
return <div ref={viewport} className={classes.root} {...props} /> | ||
} | ||
|
||
export default (img: NglProps, props: NglProps) => <Ngl {...img} {...props} /> |
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
Oops, something went wrong.