forked from ProjectMirador/mirador
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
COLUMBIA: rough out a NativeObjectViewer for PDFs
- Rudimentary fullscreen resizing for NativeObjectViewer - Refactor NativeObjectViewer to use a ResizeObserver effect directly - Linter cleanup - NativeObjectViewer.js - remove a console log in NativeObjectViewer.js - linter fixes for src/components/NativeObjectViewer.js - PrimaryWindowComponent passes textResources to TypeSpecificViewer
- Loading branch information
Showing
6 changed files
with
131 additions
and
28 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,70 @@ | ||
import { | ||
Fragment, useEffect, useRef, useState, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { styled } from '@mui/material/styles'; | ||
|
||
const StyledContainer = styled('div')({ | ||
alignItems: 'center', | ||
display: 'flex', | ||
width: '100%', | ||
}); | ||
|
||
const StyledObject = styled('object')({ | ||
width: '100%', | ||
}); | ||
|
||
/** */ | ||
export function NativeObjectViewer({ nativeObjectOptions = {}, nativeObjectResources = [], windowId }) { | ||
/** */ | ||
const defaultDimensions = (originalDimensions) => (originalDimensions || document.body.querySelector(`#${windowId} .mirador-primary-window`)?.getBoundingClientRect()); | ||
|
||
const eleRef = useRef(null); | ||
|
||
const [currentDimensions, setCurrentDimensions] = useState({ | ||
current: eleRef.current?.getBoundingClientRect(), | ||
original: defaultDimensions(null), | ||
}); | ||
|
||
useEffect(() => { | ||
const element = eleRef.current; | ||
|
||
if (!element) return () => {}; | ||
|
||
const originalDimensions = element.getBoundingClientRect(); | ||
const observer = new ResizeObserver((entries) => { | ||
if (entries.length === 0) return; | ||
const current = (document.fullscreenElement) ? entries[0].contentRect : originalDimensions; | ||
|
||
setCurrentDimensions({ current, original: originalDimensions }); | ||
}); | ||
|
||
observer.observe(element); | ||
return () => { | ||
// Cleanup the observer like any event handlers | ||
observer.disconnect(); | ||
}; | ||
}, [eleRef]); | ||
|
||
const dimensions = currentDimensions.current || currentDimensions.original; | ||
/* eslint-disable jsx-a11y/alt-text */ | ||
return ( | ||
<StyledContainer ref={eleRef}> | ||
<StyledObject {...nativeObjectOptions}> | ||
{nativeObjectResources.map((nativeObject) => ( | ||
<Fragment key={nativeObject.id}> | ||
<object data={`${nativeObject.id}`} type={nativeObject.getFormat()} width={`${dimensions?.width}px`} height={`${dimensions?.height}px`} /> | ||
</Fragment> | ||
))} | ||
</StyledObject> | ||
</StyledContainer> | ||
); | ||
/* eslint-disable jsx-a11y/alt-text */ | ||
} | ||
/* eslint-enable jsx-a11y/media-has-caption */ | ||
|
||
NativeObjectViewer.propTypes = { | ||
nativeObjectOptions: PropTypes.object, // eslint-disable-line react/forbid-prop-types | ||
nativeObjectResources: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types | ||
windowId: PropTypes.string.isRequired, // eslint-disable-line react/forbid-prop-types | ||
}; |
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,23 @@ | ||
import { connect } from 'react-redux'; | ||
import { compose } from 'redux'; | ||
import { withTranslation } from 'react-i18next'; | ||
import { withPlugins } from '../extend/withPlugins'; | ||
import { NativeObjectViewer } from '../components/NativeObjectViewer'; | ||
import { getConfig, getVisibleCanvasTextResources } from '../state/selectors'; | ||
|
||
/** */ | ||
const mapStateToProps = (state, { windowId }) => ( | ||
{ | ||
nativeObjectOptions: getConfig(state).nativeObjectOptions, | ||
nativeObjectResources: getVisibleCanvasTextResources(state, { windowId }) || [], | ||
windowId, | ||
} | ||
); | ||
|
||
const enhance = compose( | ||
withTranslation(), | ||
connect(mapStateToProps, null), | ||
withPlugins('NativeObjectViewer'), | ||
); | ||
|
||
export default enhance(NativeObjectViewer); |
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