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

Deploy version #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
276 changes: 138 additions & 138 deletions frontend/src/components/HighlightText.tsx
Original file line number Diff line number Diff line change
@@ -1,138 +1,138 @@
import React from "react";
import { HighlightedError, colorMappings } from "../types";
import "../index.css";

// **Interfaces**

// HighlightTextProps Interface
interface HighlightTextProps {
text: string;
highlights: HighlightedError[];
highlightKey:
| "start_index_orig"
| "end_index_orig"
| "start_index_translation"
| "end_index_translation"
| "error_type";
onMouseEnter?: (
e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
highlight: HighlightedError
) => void;
onMouseLeave?: (
e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
highlight: HighlightedError
) => void;
onMouseMove?: (
e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
highlight: HighlightedError
) => void;
onMouseDown?: () => void;
selectedSpan?: string | null;
}

// **HighlightText Component**
const HighlightText: React.FC<HighlightTextProps> = ({
text,
highlights,
highlightKey,
onMouseEnter,
onMouseLeave,
onMouseMove,
onMouseDown,
selectedSpan,
}) => {
// **Functions**

const mergeRanges = (
ranges: { start: number; end: number; error_type: string }[]
) => {
if (!ranges.length) return [];

ranges.sort((a, b) => a.start - b.start);

const result = [];
let { start, end, error_type } = ranges[0];

for (const range of ranges) {
if (range.start <= end) {
end = Math.max(end, range.end);
if (range.end > end) error_type = range.error_type; // Update error type if necessary
} else {
result.push({ start, end, error_type });
({ start, end, error_type } = range);
}
}

result.push({ start, end, error_type });

return result;
};

// Dynamic Highlghting Function
const getHighlightedText = (
text: string,
highlights: HighlightedError[],
highlightKey: keyof HighlightedError
) => {
const ranges = highlights.map((highlight) => {
const startKey = highlightKey.includes("end")
? (highlightKey.replace("end", "start") as keyof HighlightedError)
: highlightKey;
return {
start: highlight[startKey] as number,
end: highlight[highlightKey] as number,
error_type: highlight.error_type,
};
});

const mergedRanges = mergeRanges(ranges);

let lastIndex = 0;
const elements = [];

mergedRanges.forEach((range, index) => {
const { start, end, error_type } = range;

if (start > lastIndex) {
elements.push(
<span key={`text-${index}`}>{text.substring(lastIndex, start)}</span>
);
}

// Append Nested JSX Elements to elements array
// console.log("Selected Span", selectedSpan);
elements.push(
<span
key={`highlight-${index}`}
className={`highlight ${
selectedSpan === error_type ? "highlight-selected" : ""
}`}
style={{ backgroundColor: colorMappings[error_type] }}
onMouseEnter={(e) =>
onMouseEnter && onMouseEnter(e, highlights[index])
}
onMouseLeave={(e) =>
onMouseLeave && onMouseLeave(e, highlights[index])
}
onMouseMove={(e) => onMouseMove && onMouseMove(e, highlights[index])}
onMouseDown={(e) => onMouseDown && onMouseDown(e, highlights[index])}
>
{text.substring(start, end)}
</span>
);

lastIndex = end;
});

if (lastIndex < text.length) {
elements.push(<span key="text-end">{text.substring(lastIndex)}</span>);
}

return elements;
};

// Return JSX
return <span>{getHighlightedText(text, highlights, highlightKey)}</span>;
};

export default HighlightText;
// import React from "react";
// import { HighlightedError, colorMappings } from "../types";
// import "../index.css";

// // **Interfaces**

// // HighlightTextProps Interface
// interface HighlightTextProps {
// text: string;
// highlights: HighlightedError[];
// highlightKey:
// | "start_index_orig"
// | "end_index_orig"
// | "start_index_translation"
// | "end_index_translation"
// | "error_type";
// onMouseEnter?: (
// e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
// highlight: HighlightedError
// ) => void;
// onMouseLeave?: (
// e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
// highlight: HighlightedError
// ) => void;
// onMouseMove?: (
// e: React.MouseEvent<HTMLSpanElement, MouseEvent>,
// highlight: HighlightedError
// ) => void;
// onMouseDown?: () => void;
// selectedSpan?: string | null;
// }

// // **HighlightText Component**
// const HighlightText: React.FC<HighlightTextProps> = ({
// text,
// highlights,
// highlightKey,
// onMouseEnter,
// onMouseLeave,
// onMouseMove,
// onMouseDown,
// selectedSpan,
// }) => {
// // **Functions**

// const mergeRanges = (
// ranges: { start: number; end: number; error_type: string }[]
// ) => {
// if (!ranges.length) return [];

// ranges.sort((a, b) => a.start - b.start);

// const result = [];
// let { start, end, error_type } = ranges[0];

// for (const range of ranges) {
// if (range.start <= end) {
// end = Math.max(end, range.end);
// if (range.end > end) error_type = range.error_type; // Update error type if necessary
// } else {
// result.push({ start, end, error_type });
// ({ start, end, error_type } = range);
// }
// }

// result.push({ start, end, error_type });

// return result;
// };

// // Dynamic Highlghting Function
// const getHighlightedText = (
// text: string,
// highlights: HighlightedError[],
// highlightKey: keyof HighlightedError
// ) => {
// const ranges = highlights.map((highlight) => {
// const startKey = highlightKey.includes("end")
// ? (highlightKey.replace("end", "start") as keyof HighlightedError)
// : highlightKey;
// return {
// start: highlight[startKey] as number,
// end: highlight[highlightKey] as number,
// error_type: highlight.error_type,
// };
// });

// const mergedRanges = mergeRanges(ranges);

// let lastIndex = 0;
// const elements = [];

// mergedRanges.forEach((range, index) => {
// const { start, end, error_type } = range;

// if (start > lastIndex) {
// elements.push(
// <span key={`text-${index}`}>{text.substring(lastIndex, start)}</span>
// );
// }

// // Append Nested JSX Elements to elements array
// // console.log("Selected Span", selectedSpan);
// elements.push(
// <span
// key={`highlight-${index}`}
// className={`highlight ${
// selectedSpan === error_type ? "highlight-selected" : ""
// }`}
// style={{ backgroundColor: colorMappings[error_type] }}
// onMouseEnter={(e) =>
// onMouseEnter && onMouseEnter(e, highlights[index])
// }
// onMouseLeave={(e) =>
// onMouseLeave && onMouseLeave(e, highlights[index])
// }
// onMouseMove={(e) => onMouseMove && onMouseMove(e, highlights[index])}
// onMouseDown={(e) => onMouseDown && onMouseDown(e, highlights[index])}
// >
// {text.substring(start, end)}
// </span>
// );

// lastIndex = end;
// });

// if (lastIndex < text.length) {
// elements.push(<span key="text-end">{text.substring(lastIndex)}</span>);
// }

// return elements;
// };

// // Return JSX
// return <span>{getHighlightedText(text, highlights, highlightKey)}</span>;
// };

// export default HighlightText;
5 changes: 4 additions & 1 deletion frontend/src/componentsStatic/SpanEvalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
} from "react";

import { HighlightedError } from "../types";
import inputJson from "../static/input_sample.json";

// import inputJson from "../static/input_sample.json";
import inputJson from "../static/input.json";

// Provides context to components responsible for evaluating / editing translations.
// Currently only stores selected span index and span scores. Down the road, the provider can be used to track all evaluation/edit status.
Expand Down Expand Up @@ -132,6 +134,7 @@ export const SpanEvalProvider = ({ children }: SpanEvalProviderProps) => {
);
};

// eslint-disable-next-line react-refresh/only-export-components
export const useSpanEvalContext = () => {
const value = useContext(SpanEvalContext);

Expand Down
7 changes: 4 additions & 3 deletions frontend/src/componentsStatic/postEdit/HighlightedText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ const HighlightedText: React.FC<HighlightTextProps> = ({
};

useEffect(() => {
const handleClickOutsideDropDown = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
const handleClickOutsideDropDown = () => {
// if (dropdownRef.current && !dropdownRef.current!.contains(event.target)) {
if (dropdownRef.current) {
setSpanDropdown(false);
}
};
Expand Down Expand Up @@ -307,7 +308,7 @@ const HighlightedText: React.FC<HighlightTextProps> = ({
<div className="dropdown-selection" key={errorType}>
<li
style={{
"--hover-color": colorMappings[errorType],
color: colorMappings[errorType],
}}
onClick={() => handleTypeSelect(errorType)}
>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/componentsStatic/scoring/SpanScoreSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const SpanScoreSlider: React.FC = () => {
// **Functions**
const { selectedSpanIdx, spanScores, setSpanScores } = useSpanEvalContext();
const handleSpanScoreChange = (index: number, score: number) => {
setSpanScores((prevScores) => ({ ...prevScores, [index]: score }));
setSpanScores!((prevScores) => ({ ...prevScores, [index]: score }));
};

// Return JSX
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import App from "./componentsStatic/App";
import "./styles.css";

ReactDOM.render(<App />, document.getElementById("root"));
Loading