Skip to content

Commit

Permalink
Use a top-level context to track OSD viewers in the workspace instead…
Browse files Browse the repository at this point in the history
… of the OSDReferences global.
  • Loading branch information
cbeer committed Dec 10, 2024
1 parent acccdda commit a977eb3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
29 changes: 17 additions & 12 deletions src/components/AppProviders.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { FullScreen, useFullScreenHandle } from 'react-full-screen';
import { I18nextProvider } from 'react-i18next';
Expand All @@ -14,6 +14,7 @@ import { CacheProvider } from '@emotion/react';
import createCache from '@emotion/cache';
import createI18nInstance from '../i18n';
import FullScreenContext from '../contexts/FullScreenContext';
import OpenSeadragonWorkspaceReferencesContext from '../contexts/OpenSeadragonWorkspaceReferencesContext';

/**
* Allow applications to opt-out of (or provide their own) drag and drop context
Expand Down Expand Up @@ -117,19 +118,23 @@ export function AppProviders({
theme, translations,
dndManager = undefined,
}) {
const osdReferences = useRef({});

return (
<FullScreenShim>
<StoreAwareI18nextProvider language={language} translations={translations}>
<StyledEngineProvider injectFirst>
<CacheProvider value={theme.direction === 'rtl' ? cacheRtl : cacheDefault}>
<ThemeProvider theme={createTheme((theme))}>
<MaybeDndProvider dndManager={dndManager}>
{children}
</MaybeDndProvider>
</ThemeProvider>
</CacheProvider>
</StyledEngineProvider>
</StoreAwareI18nextProvider>
<OpenSeadragonWorkspaceReferencesContext.Provider value={osdReferences}>
<StoreAwareI18nextProvider language={language} translations={translations}>
<StyledEngineProvider injectFirst>
<CacheProvider value={theme.direction === 'rtl' ? cacheRtl : cacheDefault}>
<ThemeProvider theme={createTheme((theme))}>
<MaybeDndProvider dndManager={dndManager}>
{children}
</MaybeDndProvider>
</ThemeProvider>
</CacheProvider>
</StyledEngineProvider>
</StoreAwareI18nextProvider>
</OpenSeadragonWorkspaceReferencesContext.Provider>
</FullScreenShim>
);
}
Expand Down
10 changes: 7 additions & 3 deletions src/components/OpenSeadragonViewer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
useRef, Children, cloneElement, useCallback, useState, useEffect,
useRef, Children, cloneElement, useCallback, useState, useEffect, useContext,
} from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
Expand All @@ -12,6 +12,7 @@ import { PluginHook } from './PluginHook';
import { OSDReferences } from '../plugins/OSDReferences';
import OpenSeadragonComponent from './OpenSeadragonComponent';
import TileSource from './OpenSeadragonTileSource';
import OpenSeadragonWorkspaceReferencesContext from '../contexts/OpenSeadragonWorkspaceReferencesContext';

const StyledSection = styled('section')({
cursor: 'grab',
Expand All @@ -28,6 +29,7 @@ export function OpenSeadragonViewer({
drawAnnotations = false, infoResponses = [], canvasWorld, nonTiledImages = [], updateViewport,
}) {
const apiRef = useRef();
const workspaceReferencesContext = useContext(OpenSeadragonWorkspaceReferencesContext);
const [viewer, setViewer] = useState(null);
const onViewportChange = useCallback(({
flip, rotation, x, y, zoom,
Expand All @@ -53,10 +55,12 @@ export function OpenSeadragonViewer({
useEffect(() => {
OSDReferences.set(windowId, apiRef);
}, [apiRef, windowId]);

Check failure on line 58 in src/components/OpenSeadragonViewer.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Trailing spaces not allowed

Check failure on line 58 in src/components/OpenSeadragonViewer.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Trailing spaces not allowed
useEffect(() => {
apiRef.current = viewer;
}, [apiRef, viewer]);

workspaceReferencesContext.current[windowId] = apiRef;
}, [apiRef, windowId, viewer, workspaceReferencesContext]);

const enhancedChildren = Children.map(children, child => (
cloneElement(
Expand Down
5 changes: 5 additions & 0 deletions src/contexts/OpenSeadragonWorkspaceReferencesContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react';

const OpenSeadragonWorkspaceReferencesContext = createContext({ current: {} });

export default OpenSeadragonWorkspaceReferencesContext;

0 comments on commit a977eb3

Please sign in to comment.