Skip to content

Commit bb4a11b

Browse files
fix
1 parent c9d7d12 commit bb4a11b

6 files changed

Lines changed: 72 additions & 12 deletions

File tree

packages/react-tei/src/SidePanel/unitex/UnitexAnnotation.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { render } from "vitest-browser-react";
33
import { I18nProvider } from "../../i18n/I18nProvider";
44
import { UnitexAnnotation } from "./UnitexAnnotation";
55

6+
vi.mock("../../DocumentContextProvider");
7+
68
function TestWrapper({ children }: { children: React.ReactNode }) {
79
return <I18nProvider>{children}</I18nProvider>;
810
}

packages/react-tei/src/SidePanel/unitex/UnitexAnnotation.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import ArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
2+
import ArrowUpIcon from "@mui/icons-material/KeyboardArrowUp";
13
import Box from "@mui/material/Box";
24
import Checkbox from "@mui/material/Checkbox";
35
import Chip from "@mui/material/Chip";
46
import { grey } from "@mui/material/colors";
7+
import IconButton from "@mui/material/IconButton";
8+
import Stack from "@mui/material/Stack";
59
import Tooltip from "@mui/material/Tooltip";
610
import { useTranslation } from "react-i18next";
711
import type { TermStatistic } from "../../unitex/parseUnitexEnrichment";
@@ -57,14 +61,22 @@ export function UnitexAnnotation({
5761
slotProps={{
5862
root: {
5963
sx: {
60-
backgroundColor: annotation.displayed ? color : grey[600],
61-
color: "white",
64+
backgroundColor: annotation.displayed ? color : grey[100],
65+
color: "black",
6266
},
6367
},
6468
}}
6569
/>
6670
</Tooltip>
6771
</Box>
72+
<Stack gap={0.5} direction="row">
73+
<IconButton size="small" disabled={!annotation.displayed}>
74+
<ArrowUpIcon />
75+
</IconButton>
76+
<IconButton size="small" disabled={!annotation.displayed}>
77+
<ArrowDownIcon />
78+
</IconButton>
79+
</Stack>
6880
</>
6981
);
7082
}

packages/react-tei/src/SidePanel/unitex/UnitexAnnotationBlock.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function UnitexAnnotationBlock({ block }: UnitexAnnotationBlockProps) {
6868
<Box
6969
sx={{
7070
display: "grid",
71-
gridTemplateColumns: "max-content 1fr",
71+
gridTemplateColumns: "max-content 1fr max-content",
7272
gridTemplateRows: "auto",
7373
columnGap: 0.5,
7474
rowGap: 1,

packages/react-tei/src/navigation/DocumentNavigationContext.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function DocumentNavigationContextProvider({
9191
const currentSelectorRef = useRef<CurrentSelectorRef | null>(null);
9292

9393
const navigateToTargetLoop = useCallback(
94-
(wrapperElement: HTMLElement, querySelector: string) => {
94+
(wrapperElement: HTMLElement, querySelector: string, direction = 1) => {
9595
const targetElements =
9696
wrapperElement.querySelectorAll<HTMLElement>(querySelector);
9797

@@ -111,7 +111,8 @@ export function DocumentNavigationContextProvider({
111111
};
112112
} else {
113113
currentSelectorRef.current.index =
114-
(currentSelectorRef.current.index + 1) % targetElements.length;
114+
(currentSelectorRef.current.index + direction) %
115+
targetElements.length;
115116
}
116117

117118
const index = currentSelectorRef.current.index;
@@ -133,15 +134,15 @@ export function DocumentNavigationContextProvider({
133134
);
134135

135136
const navigateToBodyTargetSelector = useCallback(
136-
(querySelector: string) => {
137+
(querySelector: string, direction = 1) => {
137138
const documentElement = documentRef.current;
138139

139140
if (!documentElement) {
140141
console.error("Document element not found");
141142
return;
142143
}
143144

144-
navigateToTargetLoop(documentElement, querySelector);
145+
navigateToTargetLoop(documentElement, querySelector, direction);
145146
},
146147
[documentRef, navigateToTargetLoop],
147148
);

packages/react-tei/src/tags/Highlight.spec.tsx

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ function TestWrapper({ children }: { children: React.ReactNode }) {
1111
jsonDocument={[]}
1212
jsonUnitexEnrichment={{
1313
persName: [
14-
{ term: "Paris", displayed: true, frequency: 1 },
14+
{ term: "Albert Einstein", displayed: true, frequency: 1 },
1515
{ term: "Einstein", displayed: true, frequency: 2 },
1616
{ term: "Nancy", displayed: true, frequency: 5 },
1717
],
1818
placeName: [
19-
{ term: "Paris", displayed: true, frequency: 1 },
2019
{
2120
term: "London",
2221
displayed: false,
@@ -92,4 +91,49 @@ describe("Highlight", () => {
9291
await expect.element(element).toHaveAttribute("data-term", "nancy");
9392
await expect.element(element).toHaveAttribute("data-group", "persName");
9493
});
94+
95+
it("should support nested highlights", async () => {
96+
const screen = await render(
97+
<Highlight
98+
data={{
99+
tag: "highlight",
100+
attributes: { groups: "persName", term: "albert-einstein" },
101+
value: [
102+
{
103+
tag: "#text",
104+
value: "Albert ",
105+
},
106+
{
107+
tag: "highlight",
108+
attributes: { groups: "persName", term: "einstein" },
109+
value: "Einstein",
110+
},
111+
],
112+
}}
113+
/>,
114+
{
115+
wrapper: TestWrapper,
116+
},
117+
);
118+
119+
const outerElement = screen.getByRole("mark").filter({
120+
hasText: "Albert Einstein",
121+
exact: true,
122+
});
123+
await expect.element(outerElement).toBeInTheDocument();
124+
await expect.element(outerElement).toHaveTextContent("Albert Einstein");
125+
await expect
126+
.element(outerElement)
127+
.toHaveAttribute("data-term", "albert-einstein");
128+
await expect
129+
.element(outerElement)
130+
.toHaveAttribute("data-group", "persName");
131+
132+
const innerElement = screen.getByText("Einstein", { exact: true });
133+
await expect.element(innerElement).toBeInTheDocument();
134+
await expect.element(innerElement).toHaveAttribute("data-term", "einstein");
135+
await expect
136+
.element(innerElement)
137+
.toHaveAttribute("data-group", "persName");
138+
});
95139
});

packages/react-tei/src/tags/Highlight.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Box } from "@mui/material";
22
import { useMemo } from "react";
33
import { useDocumentContext } from "../DocumentContextProvider";
4+
import { kebabCasify } from "../helper/kebabCasify";
45
import { chipColors } from "../SidePanel/unitex/unitexAnnotationBlocks";
56
import type { ComponentProps } from "./type";
67
import { Value } from "./Value";
@@ -10,17 +11,17 @@ export const Highlight = ({ data }: HighlightProps) => {
1011
const { unitexEnrichment } = useDocumentContext();
1112

1213
const groups = useMemo(() => {
13-
if (!attributes?.groups || !unitexEnrichment || typeof value !== "string") {
14+
if (!attributes?.groups || !unitexEnrichment) {
1415
return [];
1516
}
1617
return ([] as string[]).concat(attributes?.groups).filter((group) => {
1718
const term = unitexEnrichment?.document?.[group]?.find(
18-
(term) => term.term === value,
19+
({ term }) => kebabCasify(term) === attributes?.term,
1920
);
2021

2122
return term?.displayed ?? false;
2223
});
23-
}, [unitexEnrichment, attributes?.groups, value]);
24+
}, [unitexEnrichment, attributes?.groups, attributes?.term]);
2425

2526
if (groups.length === 0) {
2627
return <Value data={value} />;

0 commit comments

Comments
 (0)