Skip to content

Commit

Permalink
Extract usePlugins for additive plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 28, 2025
1 parent f22e042 commit 57d48fe
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/components/AttributionPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function AttributionPanel({
</CompanionWindowSection>
)}

<PluginHook {...pluginProps} />
<PluginHook targetName="AttributionPanel" {...pluginProps} />
</CompanionWindow>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/BackgroundPluginArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PluginHook } from './PluginHook';
/** invisible area where background plugins can add to */
export const BackgroundPluginArea = ({ PluginComponents = [], ...props }) => (
<div className={ns('background-plugin-area')} style={{ display: 'none' }}>
<PluginHook PluginComponents={PluginComponents} {...props} />
<PluginHook targetName="BackgroundPluginArea" PluginComponents={PluginComponents} {...props} />
</div>
);

Expand Down
2 changes: 1 addition & 1 deletion src/components/CanvasInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function CanvasInfo({
{canvasMetadata && canvasMetadata.length > 0 && (
<LabelValueMetadata labelValuePairs={canvasMetadata} />
)}
<PluginHook {...pluginProps} />
<PluginHook targetName="CanvasInfo" {...pluginProps} />
</CollapsibleSection>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ErrorContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function ErrorContent({
</AccordionDetails>
</InlineAccordion>
)}
<PluginHook {...pluginProps} />
<PluginHook targetName="ErrorContent" {...pluginProps} />
</Alert>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ManifestInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function ManifestInfo({
<LabelValueMetadata labelValuePairs={manifestMetadata} />
)}

<PluginHook {...pluginProps} />
<PluginHook targetName="ManifestInfo" {...pluginProps} />
</CollapsibleSection>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ManifestRelatedLinks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function ManifestRelatedLinks({
</>
)}
</StyledDl>
<PluginHook {...pluginProps} />
<PluginHook targetName="ManifestRelatedLinks" {...pluginProps} />
</CollapsibleSection>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/OpenSeadragonViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function OpenSeadragonViewer({
{ drawAnnotations
&& <AnnotationsOverlay viewer={viewer} windowId={windowId} /> }
{ enhancedChildren }
<PluginHook viewer={viewer} {...pluginProps} />
<PluginHook targetName="OpenSeadragonViewer" viewer={viewer} {...pluginProps} />
</OpenSeadragonComponent>
);
}
Expand Down
13 changes: 10 additions & 3 deletions src/components/PluginHook.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { forwardRef, isValidElement, cloneElement } from 'react';
import PropTypes from 'prop-types';
import { usePlugins } from '../extend/usePlugins';

/** Renders plugins */
export const PluginHook = forwardRef((props, ref) => {
const { PluginComponents } = props; // eslint-disable-line react/prop-types
const { classes, ...otherProps } = props; // eslint-disable-line react/prop-types
export const PluginHook = forwardRef(({ classes = {}, targetName, ...otherProps }, ref) => {
const { PluginComponents } = usePlugins(targetName);

return PluginComponents ? (
PluginComponents.map((PluginComponent, index) => ( // eslint-disable-line react/prop-types
isValidElement(PluginComponent)
Expand All @@ -20,3 +22,8 @@ export const PluginHook = forwardRef((props, ref) => {
});

PluginHook.displayName = 'PluginHook';

PluginHook.propTypes = {
classes: PropTypes.object, // eslint-disable-line react/forbid-prop-types, react/require-default-props
targetName: PropTypes.string.isRequired,
};
2 changes: 1 addition & 1 deletion src/components/Window.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function Window({
</StyledCompanionAreaRight>
</ContentRow>
<CompanionArea windowId={windowId} position="far-bottom" />
<PluginHook {...ownerState} />
<PluginHook targetName="Window" {...ownerState} />
</Root>
</ErrorBoundary>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/WindowAuthenticationBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function WindowAuthenticationBar({
<Typography component="h3" variant="body1" color="inherit">
{ ruleSet ? <SanitizedHtml htmlString={label} ruleSet={ruleSet} /> : label }
</Typography>
<PluginHook {...pluginProps} />
<PluginHook targetName="WindowAuthenticationBar" {...pluginProps} />
{ button }
</StyledTopBar>
</Paper>
Expand Down Expand Up @@ -106,7 +106,7 @@ export function WindowAuthenticationBar({
<Typography sx={{ paddingBlockEnd: 1, paddingBlockStart: 1 }} component="h3" variant="body1" color="inherit">
{ ruleSet ? <SanitizedHtml htmlString={label} ruleSet={ruleSet} /> : label }
</Typography>
<PluginHook {...pluginProps} />
<PluginHook targetName="WindowAuthenticationBar" {...pluginProps} />
<StyledFauxButton>
{ !open && (
<Typography variant="button" color="inherit">
Expand Down
2 changes: 1 addition & 1 deletion src/components/WindowCanvasNavigationControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const WindowCanvasNavigationControls = forwardRef(({
</Stack>
<ViewerInfo windowId={windowId} />

<PluginHook {...pluginProps} />
<PluginHook targetName="WindowCanvasNavigationControls" {...pluginProps} />
</Root>
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/WindowTopBarPluginArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { PluginHook } from './PluginHook';
*/
export function WindowTopBarPluginArea(props) {
return (
<PluginHook {...props} />
<PluginHook targetName="WindowTopBarPluginArea" {...props} />
);
}
2 changes: 1 addition & 1 deletion src/components/WindowTopBarPluginMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function WindowTopBarPluginMenu({
open={open}
onClose={handleMenuClose}
>
<PluginHook handleClose={handleMenuClose} {...pluginProps} />
<PluginHook targetName="WindowTopBarPluginMenu" handleClose={handleMenuClose} {...pluginProps} />
</Menu>
</>
);
Expand Down
15 changes: 11 additions & 4 deletions src/components/WindowTopMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@ import { useContext } from 'react';
import ListSubheader from '@mui/material/ListSubheader';
import Popover from '@mui/material/Popover';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';
import WindowThumbnailSettings from '../containers/WindowThumbnailSettings';
import WindowViewSettings from '../containers/WindowViewSettings';
import { PluginHook } from './PluginHook';
import WorkspaceContext from '../contexts/WorkspaceContext';
import { usePlugins } from '../extend/usePlugins';

/** Renders plugins */
function PluginHookWithHeader(props) {
const { PluginComponents, t } = props; // eslint-disable-line react/prop-types
function PluginHookWithHeader({ targetName, ...props }) {
const PluginComponents = usePlugins(targetName);
const { t } = useTranslation();
return PluginComponents ? (
<>
<ListSubheader role="presentation" disableSticky tabIndex={-1}>{t('windowPluginButtons')}</ListSubheader>
<PluginHook {...props} />
<PluginHook targetName={targetName} {...props} />
</>
) : null;
}

PluginHookWithHeader.propTypes = {
targetName: PropTypes.string.isRequired,
};

/**
*/
export function WindowTopMenu({
Expand Down Expand Up @@ -51,7 +58,7 @@ export function WindowTopMenu({
<WindowViewSettings windowId={windowId} handleClose={handleClose} />
{showThumbnailNavigationSettings
&& <WindowThumbnailSettings windowId={windowId} handleClose={handleClose} />}
<PluginHookWithHeader {...pluginProps} />
<PluginHookWithHeader targetName="WindowTopMenu" {...pluginProps} />
</Popover>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/WorkspaceAdd.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function WorkspaceAdd({
) : (
<Paper sx={{ margin: 2 }}>
<Typography style={visuallyHidden} component="h1">{t('miradorResources')}</Typography>
<PluginHook {...pluginProps} />
<PluginHook targetName="WorkspaceAdd" {...pluginProps} />
<List disablePadding>
{manifestList}
</List>
Expand Down
2 changes: 1 addition & 1 deletion src/components/WorkspaceControlPanelButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function WorkspaceControlPanelButtons({ ...rest }) {
<WorkspaceMenuButton />
<WorkspaceOptionsButton />
<FullScreenButton />
<PluginHook {...rest} />
<PluginHook targetName="WorkspaceControlPanelButtons" {...rest} />
</>
);
}
2 changes: 1 addition & 1 deletion src/components/WorkspaceMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function WorkspaceMenu({
<Typography variant="body1">{t('changeTheme')}</Typography>
</MenuItem>
)}
<PluginHook {...pluginProps} />
<PluginHook targetName="WorkspaceMenu" {...pluginProps} />
</Menu>
{selectedOption === 'changeTheme' && (
<ChangeThemeDialog
Expand Down
2 changes: 1 addition & 1 deletion src/components/WorkspaceOptionsMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function WorkspaceOptionsMenu({
</ListItemIcon>
<Typography variant="body1">{t('importWorkspace')}</Typography>
</MenuItem>
<PluginHook {...pluginProps} />
<PluginHook targetName="WorkspaceOptionsMenu" {...pluginProps} />
</Menu>
{selectedOption === 'exportWorkspace' && (
<WorkspaceExport
Expand Down
13 changes: 13 additions & 0 deletions src/extend/usePlugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useContext } from 'react';
import PluginContext from './PluginContext';

/** withPlugins should be the innermost HOC */
export function usePlugins(targetName) {
const pluginMap = useContext(PluginContext);

const plugins = (pluginMap || {})[targetName];

const PluginComponents = (plugins?.add || []).map(plugin => plugin.component);

return { PluginComponents };
}
10 changes: 1 addition & 9 deletions src/extend/withPlugins.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ function _withPlugins(targetName, TargetComponent) { // eslint-disable-line no-u

const plugins = (pluginMap || {})[targetName];

if (isEmpty(plugins) || (isEmpty(plugins.wrap) && isEmpty(plugins.add))) {
if (isEmpty(plugins) || isEmpty(plugins.wrap)) {
return <TargetComponent {...passDownProps} />;
}

const PluginComponents = (plugins.add || []).map(plugin => plugin.component);
const targetComponent = (
<TargetComponent {...passDownProps} PluginComponents={PluginComponents} />
);

if (isEmpty(plugins.wrap)) return targetComponent;

/** */
const pluginWrapper = (children, plugin) => {
const WrapPluginComponent = plugin.component;
Expand All @@ -35,7 +28,6 @@ function _withPlugins(targetName, TargetComponent) { // eslint-disable-line no-u
<WrapPluginComponent
targetProps={passDownProps}
{...passDownProps}
PluginComponents={PluginComponents}
TargetComponent={TargetComponent}
>
{children}
Expand Down

0 comments on commit 57d48fe

Please sign in to comment.