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: src/culPlugins/mirador-nativeObjectViewer plugin for text r…
…esources - Rudimentary fullscreen resizing for NativeObjectViewer - NativeObjectViewer use a ResizeObserver effect directly
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
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
70 changes: 70 additions & 0 deletions
70
src/culPlugins/mirador-nativeObjectViewer/components/NativeObjectViewer.js
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 | ||
}; |
23 changes: 23 additions & 0 deletions
23
src/culPlugins/mirador-nativeObjectViewer/containers/NativeObjectViewer.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import NativeObjectViewerComponent from './containers/NativeObjectViewer'; | ||
|
||
export default [ | ||
{ | ||
component: NativeObjectViewerComponent, | ||
mode: 'wrap', | ||
name: 'NativeObjectViewer', | ||
target: 'TextViewer', | ||
}, | ||
]; | ||
|
||
export { NativeObjectViewerComponent }; |