Skip to content
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

Preserve viewport maybe #4117

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions __tests__/integration/mirador/mirador-configs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ export default {
transitions: {},
},
windows: [{
canvasId: 'https://iiif.harvardartmuseums.org/manifests/object/299843/canvas/canvas-47174892',
manifestId: 'https://iiif.harvardartmuseums.org/manifests/object/299843',
},
{
canvasId: 'https://iiif.bodleian.ox.ac.uk/iiif/canvas/e58b8c60-005c-4c41-a22f-07d49cb25ede.json',
manifestId: 'https://iiif.bodleian.ox.ac.uk/iiif/manifest/e32a277e-91e2-4a6d-8ba6-cc4bad230410.json',
thumbnailNavigationPosition: 'far-bottom',
manifestId: 'https://wellcomelibrary.org/iiif/b18035723/manifest',
}],
};
51 changes: 43 additions & 8 deletions src/components/OpenSeadragonComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/** Handle setting up OSD for use in mirador + react */
function OpenSeadragonComponent({
children = undefined, Container = 'div', osdConfig = {}, viewerConfig = {}, onUpdateViewport = () => {}, setViewer = () => {}, style = {}, ...passThruProps
children = undefined, Container = 'div', osdConfig = {}, viewerConfig = {}, worldBounds = undefined, onUpdateViewport = () => {}, setViewer = () => {}, style = {}, ...passThruProps
}) {
const id = useId();
const ref = useRef();
Expand All @@ -33,6 +33,13 @@
bounds: viewport.getBounds(),
flip: viewport.getFlip(),
rotation: viewport.getRotation(),
worldBounds: (() => {
const homeBounds = viewport.viewer?.world?.getHomeBounds();

if (!homeBounds) return undefined;

return [homeBounds.x, homeBounds.y, homeBounds.width, homeBounds.height];

Check warning on line 41 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L41

Added line #L41 was not covered by tests
})(),
x: Math.round(viewport.centerSpringX.target.value),
y: Math.round(viewport.centerSpringY.target.value),
zoom: viewport.zoomSpring.target.value,
Expand All @@ -59,14 +66,16 @@
viewport.setFlip(viewerConfig.flip);
}

const bounds = viewerConfig.bounds || worldBounds;

if (!viewerConfig.x && !viewerConfig.y && !viewerConfig.zoom) {
if (viewerConfig.bounds) {
viewport.fitBounds(new Openseadragon.Rect(...viewerConfig.bounds), true);
if (bounds) {
viewport.fitBounds(new Openseadragon.Rect(...bounds), true);
} else {
viewport.goHome(true);
}
}
}, [initialViewportSet, viewerConfig]);
}, [initialViewportSet, viewerConfig, worldBounds]);

useEffect(() => {
const viewer = viewerRef.current;
Expand Down Expand Up @@ -100,19 +109,44 @@
viewport.setFlip(viewerConfig.flip);
}

if (viewerConfig.bounds && !viewerConfig.x && !viewerConfig.y && !viewerConfig.zoom) {
const rect = new Openseadragon.Rect(...viewerConfig.bounds);
if (rect.equals(viewport.getBounds())) {
const bounds = viewerConfig.bounds || worldBounds;
if (bounds && !viewerConfig.x && !viewerConfig.y && !viewerConfig.zoom) {
const rect = new Openseadragon.Rect(...bounds);
if (!rect.equals(viewport.getBounds())) {
viewport.fitBounds(rect, false);
}
}
}, [initialViewportSet, setInitialBounds, viewerConfig, viewerRef, worldBounds]);

useEffect(() => {
if (!osdConfig.preserveViewport) return;
if (!viewerConfig?.worldBounds || !worldBounds) return;

const viewer = viewerRef.current;
if (!viewer) return;
const { viewport } = viewer;

Check warning on line 127 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L125-L127

Added lines #L125 - L127 were not covered by tests

const [_x, _y, width, height] = viewerConfig.worldBounds;
const [_x1, _y1, width1, height1] = worldBounds;

Check warning on line 130 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L129-L130

Added lines #L129 - L130 were not covered by tests

const previousAspectRatio = (1.0 * width) / height;
const newAspectRatio = (1.0 * width1) / height1;

Check warning on line 133 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L132-L133

Added lines #L132 - L133 were not covered by tests

if ((previousAspectRatio < (1 - osdConfig.resetViewportAfterAspectRatioDelta) * newAspectRatio)

Check warning on line 135 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L135

Added line #L135 was not covered by tests
|| (previousAspectRatio > (1 + osdConfig.resetViewportAfterAspectRatioDelta) * newAspectRatio)) {
const rect = new Openseadragon.Rect(...worldBounds);
if (!rect.equals(viewport.getBounds())) {

Check warning on line 138 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L137-L138

Added lines #L137 - L138 were not covered by tests
viewport.fitBounds(rect, false);
}
}
}, [initialViewportSet, setInitialBounds, viewerConfig, viewerRef]);
}, [osdConfig, viewerConfig, worldBounds, viewerRef]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if worldBounds is an array, I think this effect is always going to run because the object reference will change. I think the approach is sound, but we'd need to unroll the array into integers, right?


// initialize OSD stuff when this component is mounted
useEffect(() => {
const viewer = Openseadragon({
element: ref.current,
...osdConfig,
preserveViewportAspectRatio: undefined,
});

viewer.addHandler('canvas-drag', () => {
Expand Down Expand Up @@ -200,6 +234,7 @@
y: PropTypes.number,
zoom: PropTypes.number,
}),
worldBounds: PropTypes.arrayOf(PropTypes.number),
};

export default OpenSeadragonComponent;
8 changes: 5 additions & 3 deletions src/components/OpenSeadragonViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ const StyledSection = styled('section')({
* and rendering OSD.
*/
export function OpenSeadragonViewer({
children = null, label = null, windowId, osdConfig = {}, viewerConfig = null,
children = null, label = null, windowId, osdConfig = {}, viewerConfig = undefined,
drawAnnotations = false, infoResponses = [], canvasWorld, nonTiledImages = [], updateViewport,
...rest
}) {
const { t } = useTranslation();
const apiRef = useRef();
const [viewer, setViewer] = useState(null);
const onViewportChange = useCallback(({
flip, rotation, x, y, zoom,
flip, rotation, worldBounds, x, y, zoom,
}) => {
updateViewport(windowId, {
flip,
rotation,
worldBounds,
x,
y,
zoom,
Expand Down Expand Up @@ -89,7 +90,8 @@ export function OpenSeadragonViewer({
className={classNames(ns('osd-container'))}
Container={StyledSection}
osdConfig={osdConfig}
viewerConfig={viewerConfig || (canvasWorld.hasDimensions() ? { bounds: canvasWorld.worldBounds() } : undefined)}
viewerConfig={viewerConfig}
worldBounds={(canvasWorld.hasDimensions() ? canvasWorld.worldBounds() : undefined)}
onUpdateViewport={onViewportChange}
setViewer={setViewer}
aria-label={t('item', { label })}
Expand Down
1 change: 1 addition & 0 deletions src/config/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ export default {
blendTime: 0.1,
preserveImageSizeOnResize: true,
preserveViewport: true,
resetViewportAfterAspectRatioDelta: 0.25,
showNavigationControl: false,
zoomPerClick: 1, // disable zoom-to-click
zoomPerDoubleClick: 2.0
Expand Down