Skip to content

Commit

Permalink
Merge pull request #812 from digirati-co-uk/feature/polygon-selector-…
Browse files Browse the repository at this point in the history
…visibility

Fixed visibility of selectors in document panel
  • Loading branch information
stephenwf committed Feb 7, 2024
2 parents 9566381 + 85cda18 commit 24f2757
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed Locale name in heading (MAD-1152)
- Fixed token refreshing
- Fixed activity stream pagination (NS-52)
- Fixed visibility of selectors in document panel

### Changed
- Add short descriptions for admin Search & Metadata actions + correct site config. page titles (MAD-1466)
Expand Down
111 changes: 77 additions & 34 deletions services/madoc-ts/src/frontend/shared/atlas/ViewReadOnlyAnnotation.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
import React, { useEffect, useMemo, useState } from 'react';
import { HTMLPortal, mergeStyles, useAtlas } from '@atlas-viewer/atlas';
import { useMemo, useState } from 'react';
import { HTMLPortal } from '@atlas-viewer/atlas';
import { HotSpot } from '../atoms/HotSpot';
import { useSelectorEvents } from '../capture-models/editor/stores/selectors/selector-helper';
import { ReadOnlyAnnotation } from '../hooks/use-read-only-annotations';
import { useCanvas } from 'react-iiif-vault';

// Avoid bad types from Atlas.
const CustomHTMLPortal = HTMLPortal as any;

export function ViewReadOnlyAnnotation({ id, style, target, ...props }: ReadOnlyAnnotation) {
function isBox(target: any): target is { x: number; y: number; width: number; height: number } {
return typeof target === 'object' && 'x' in target && 'y' in target && 'width' in target && 'height' in target;
}

function getBounds(target: ReadOnlyAnnotation['target'], { width, height }: { width?: number; height?: number }) {
if (isBox(target)) {
return target;
}

if (target && target.shape && target.shape.points) {
const x = target.shape.points.map((p: any) => p[0]);
const y = target.shape.points.map((p: any) => p[1]);
return {
x: Math.min(...x),
y: Math.min(...y),
width: Math.max(...x) - Math.min(...x),
height: Math.max(...y) - Math.min(...y),
};
}

return { x: 0, y: 0, width: 0, height: 0 };
}

export function ViewReadOnlyAnnotation({ id, style, target: originalTarget, ...props }: ReadOnlyAnnotation) {
const canvas = useCanvas();
const { onClick, onHover } = useSelectorEvents(id);
const [isOpen, setIsOpen] = useState(false);
const atlas = useAtlas();
const [isHover, setIsHover] = useState(false);
const { interactive, hotspot, hotspotSize, hidden, ...allStyles } = style;
const { interactive, hotspot, hotspotSize, hidden: _, ...allStyles } = style;

const styleToApply = useMemo(() => {
if (hotspot) {
Expand All @@ -22,30 +46,56 @@ export function ViewReadOnlyAnnotation({ id, style, target, ...props }: ReadOnly
return allStyles;
}, [allStyles, hotspot, isHover, isOpen]);

if (!canvas) {
return null;
}

const target = getBounds(originalTarget, canvas);

const Shape = 'shape' as any;
const Box = 'box' as any;

const getShape = () => {
if (!originalTarget || !target) {
return null;
}

if (isBox(originalTarget)) {
return (
<Box
id={`${hotspot ? 'hotspot' : 'non-hotspot'}/box-${id}`}
html
interactive={interactive}
{...props}
style={styleToApply}
target={{ x: 0, y: 0, width: target.width, height: target.height }}
onClick={onClick}
onMouseEnter={onHover}
/>
);
}

return (
<Shape
id={`${hotspot ? 'hotspot' : 'non-hotspot'}/shape-${id}`}
points={originalTarget.shape.points.map(p => [p[0] - target.x, p[1] - target.y])}
open={originalTarget.shape.open}
relativeStyle={true}
{...props}
style={style}
target={{ x: 0, y: 0, width: target.width, height: target.height }}
onClick={onClick}
onMouseEnter={onHover}
/>
);
};

return (
<world-object
id={`readonly-${id}`}
x={target.x}
y={target.y}
width={target.width}
height={target.height}
onClick={() => {
onClick();
}}
onMouseEnter={() => {
onHover();
}}
>
<world-object id={`readonly-${id}`} x={target.x} y={target.y} width={target.width} height={target.height}>
{hotspot ? (
<>
<box
id={`hotspot/${id}`}
html
interactive={interactive}
{...props}
style={styleToApply}
target={{ x: 0, y: 0, width: target.width, height: target.height }}
/>
{getShape()}

<CustomHTMLPortal
id={`portal/${id}`}
relativeStyle
Expand All @@ -64,14 +114,7 @@ export function ViewReadOnlyAnnotation({ id, style, target, ...props }: ReadOnly
</CustomHTMLPortal>
</>
) : (
<box
id={`non-hotspot/${id}`}
html
interactive={interactive}
{...props}
style={styleToApply}
target={{ x: 0, y: 0, width: target.width, height: target.height }}
/>
getShape()
)}
</world-object>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function ViewEntity({
const ref = useRef<HTMLDivElement>(null);
const helper = useSelectorHelper();
const [isCollapsed, setIsCollapsed] = useState(collapsed || !!entity.selector);
const selector = entity.selector ? resolveSelector(entity.selector, highlightRevisionChanges) : undefined;
const selector = entity.selector ? resolveSelector(entity.selector, highlightRevisionChanges, true) : undefined;
const selectorId = selector?.id;
const { t: tModel } = useModelTranslation();
const label = getEntityLabel(entity, undefined, false, tModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function ViewField({

const helper = useSelectorHelper();

const selector = field.selector ? resolveSelector(field.selector, revisionId) : undefined;
const selector = field.selector ? resolveSelector(field.selector, revisionId, true) : undefined;
const selectorId = selector?.id;
const [isOn, trigger] = useDecayState();

Expand Down
1 change: 1 addition & 0 deletions services/madoc-ts/translations/en/madoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@
"Error - Unable to save your submission": "Error - Unable to save your submission",
"Errored": "Errored",
"Errored / Rejected": "Errored / Rejected",
"Exit Correction": "Exit Correction",
"Exit edit mode": "Exit edit mode",
"Expand to fill available space": "Expand to fill available space",
"Export": "Export",
Expand Down

0 comments on commit 24f2757

Please sign in to comment.