Skip to content

Commit

Permalink
COLUMBIA: Refactor NativeObjectViewer to use a ResizeObserver effect …
Browse files Browse the repository at this point in the history
…directly
  • Loading branch information
barmintor committed Apr 11, 2024
1 parent 4d9a01d commit 94eaa75
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
40 changes: 31 additions & 9 deletions src/components/NativeObjectViewer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Fragment, useContext, useRef, useState } from 'react';
import { Fragment, useEffect, useRef, useState } from 'react';

Check failure on line 1 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected a line break after this opening brace

Check failure on line 1 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected a line break before this closing brace
import { FullScreen } from 'react-full-screen';

Check warning on line 2 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'FullScreen' is defined but never used
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import { getCurrentCanvasWorld } from '../state/selectors';
import FullScreenContext from '../contexts/FullScreenContext';

const StyledContainer = styled('div')({
alignItems: 'center',
Expand All @@ -20,24 +18,48 @@ export function NativeObjectViewer(props) {
/* eslint-disable jsx-a11y/media-has-caption */
/** */
const {
dimensions, nativeObjectOptions, nativeObjectResources, windowId,
nativeObjectOptions, nativeObjectResources, windowId,
} = props;

const fullscreenHandle = useContext(FullScreenContext);
const defaultDimensions = (originalDimensions) => {

Check failure on line 24 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing JSDoc comment

Check failure on line 24 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return originalDimensions || document.body.querySelector(`#${windowId} .mirador-primary-window`)?.getBoundingClientRect();
};

const eleRef = useRef(null);
const [fullscreen, setFullscreen] = useState(false);
const originalDimensions = defaultDimensions(null);

const [currentDimensions, setCurrentDimensions] = useState({
original: originalDimensions,
current: eleRef.current?.getBoundingClientRect(),

Check failure on line 33 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected object keys to be in insensitive ascending order. 'current' should be before 'original'
});

useEffect(() => {
const element = eleRef.current;

if (!element) return;

if (fullscreenHandle.active != fullscreen) setFullscreen(fullscreenHandle.active);
const observer = new ResizeObserver((entries) => {
if (entries.length == 0) return;

Check warning on line 42 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected '===' and instead saw '=='
console.log(entries[0]);
const current = (document.fullscreenElement) ? entries[0].contentRect : originalDimensions;

const boundingRect = (fullscreen && eleRef.current) ? eleRef.current.closest(`.mirador-primary-window`).getBoundingClientRect() : dimensions;
setCurrentDimensions({original: originalDimensions, current: current});

Check failure on line 46 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

A space is required after '{'

Check failure on line 46 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected object keys to be in insensitive ascending order. 'current' should be before 'original'

Check failure on line 46 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected property shorthand

Check failure on line 46 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

A space is required before '}'
});

observer.observe(element);
return () => {

Check failure on line 50 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Arrow function expected no return value
// Cleanup the observer like any event handlers
observer.disconnect();
};
}, []); // this will loop endlessly if the refs are included in the dependencies

const dimensions = currentDimensions.current || currentDimensions.original;
return (
<StyledContainer ref={eleRef}>
<StyledObject {...nativeObjectOptions}>
{nativeObjectResources.map((nativeObject) => (
<Fragment key={nativeObject.id}>
<object data={`${nativeObject.id}`} type={nativeObject.getFormat()} width={`${boundingRect.width}px`} height={`${boundingRect.height}px`} />
<object data={`${nativeObject.id}`} type={nativeObject.getFormat()} width={`${dimensions?.width}px`} height={`${dimensions?.height}px`} />

Check warning on line 62 in src/components/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Embedded <object> elements must have alternative text by providing inner text, aria-label or aria-labelledby props
</Fragment>
))}
</StyledObject>
Expand Down
3 changes: 1 addition & 2 deletions src/containers/NativeObjectViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { getConfig, getCurrentCanvas, getFullScreenEnabled, getVisibleCanvasText
/** */
const mapStateToProps = (state, { windowId }) => {
const currentCanvas = getCurrentCanvas(state, { windowId });

Check warning on line 11 in src/containers/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'currentCanvas' is assigned a value but never used
const dimensions = (document.body.querySelector(`#${windowId} .mirador-primary-window`)?.getBoundingClientRect() || {});
const hold = `#${windowId} .mirador-primary-window`;

Check warning on line 12 in src/containers/NativeObjectViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'hold' is assigned a value but never used
return {
dimensions,
nativeObjectOptions: getConfig(state).nativeObjectOptions,
nativeObjectResources: getVisibleCanvasTextResources(state, { windowId }) || [],
windowId,
Expand Down

0 comments on commit 94eaa75

Please sign in to comment.