Skip to content

Commit

Permalink
Only preserve the viewport if the preserved viewport 'makes sense'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 27, 2025
1 parent 8e1a0c9 commit 26b47fd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
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',
}],
};
31 changes: 30 additions & 1 deletion src/components/OpenSeadragonComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ function OpenSeadragonComponent({

onUpdateViewport({
bounds: viewport.getBounds(),
worldBounds: (() => {
const homeBounds = viewport.viewer.world.getHomeBounds();

return [homeBounds.x, homeBounds.y, homeBounds.width, homeBounds.height];
})(),
flip: viewport.getFlip(),

Check failure on line 39 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Expected object keys to be in insensitive ascending order. 'flip' should be before 'worldBounds'

Check failure on line 39 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected object keys to be in insensitive ascending order. 'flip' should be before 'worldBounds'

Check failure on line 39 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected object keys to be in insensitive ascending order. 'flip' should be before 'worldBounds'
rotation: viewport.getRotation(),
x: Math.round(viewport.centerSpringX.target.value),
Expand Down Expand Up @@ -105,12 +110,36 @@ function OpenSeadragonComponent({
const bounds = viewerConfig.bounds || worldBounds;
if (bounds && !viewerConfig.x && !viewerConfig.y && !viewerConfig.zoom) {
const rect = new Openseadragon.Rect(...bounds);
if (rect.equals(viewport.getBounds())) {
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;

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

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

if (previousAspectRatio < 0.75 * newAspectRatio || previousAspectRatio > 1.25 * newAspectRatio) {
console.log("aspect ratio changed, updating viewport");

Check failure on line 134 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Strings must use singlequote

Check failure on line 134 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check failure on line 134 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
const rect = new Openseadragon.Rect(...worldBounds);
if (!rect.equals(viewport.getBounds())) {
viewport.fitBounds(rect, false);
}
}

Check failure on line 139 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (22.x)

Block must not be padded by blank lines

Check failure on line 139 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Block must not be padded by blank lines

Check failure on line 139 in src/components/OpenSeadragonComponent.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Block must not be padded by blank lines

}, [osdConfig, viewerConfig, worldBounds, viewerRef]);

// initialize OSD stuff when this component is mounted
useEffect(() => {
const viewer = Openseadragon({
Expand Down
3 changes: 2 additions & 1 deletion src/components/OpenSeadragonViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ export function OpenSeadragonViewer({
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

0 comments on commit 26b47fd

Please sign in to comment.