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

Use a top-level context to track OSD viewers in the workspace instead… #4030

Draft
wants to merge 1 commit 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
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
8 changes: 6 additions & 2 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 Down Expand Up @@ -56,7 +58,9 @@ export function OpenSeadragonViewer({

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;