Skip to content
Merged
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
34 changes: 33 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function App() {
const [pendingSelection, setPendingSelection] = useState<PendingSelection | null>(null);
const [currentPage, setCurrentPage] = useState<number | null>(null);
const [pageSizes, setPageSizes] = useState<Record<number, { width: number; height: number }>>({});
const [isDrawingRect, setIsDrawingRect] = useState(false);
const [presets, setPresets] = useState<Record<PresetKey, boolean>>(EMPTY_PRESETS);
const [isDragOver, setIsDragOver] = useState(false);

Expand All @@ -44,6 +45,7 @@ export default function App() {
} | null>(null);

const fileInputRef = useRef<HTMLInputElement | null>(null);
const nextRectangleNumberRef = useRef(1);

const clearNotices = () => {
setErrorInfo(null);
Expand Down Expand Up @@ -81,7 +83,9 @@ export default function App() {
}, [rules]);

const rectsForApi = useMemo(() => {
return rules.flatMap((r) => (r.kind === "selection" ? r.rects : r.kind === "page" ? [r.rect] : []));
return rules.flatMap((r) =>
r.kind === "selection" ? r.rects : r.kind === "page" || r.kind === "rectangle" ? [r.rect] : []
);
}, [rules]);

const hasAnythingToDo = rules.length > 0 || selectedPresets.length > 0;
Expand All @@ -104,6 +108,8 @@ export default function App() {
setPendingSelection(null);
setCurrentPage(null);
setPageSizes({});
setIsDrawingRect(false);
nextRectangleNumberRef.current = 1;
return;
}

Expand All @@ -113,6 +119,8 @@ export default function App() {
setFile(null);
setCurrentPage(null);
setPageSizes({});
setIsDrawingRect(false);
nextRectangleNumberRef.current = 1;
setErrorInfo({ rawMessage: t("form.file.invalidType") });
return;
}
Expand All @@ -121,6 +129,8 @@ export default function App() {
setPendingSelection(null);
setCurrentPage(1);
setPageSizes({});
setIsDrawingRect(false);
nextRectangleNumberRef.current = 1;
setRules([]);
setPresets(EMPTY_PRESETS);
};
Expand Down Expand Up @@ -183,6 +193,24 @@ export default function App() {
setRules((prev) => [...prev, newRule]);
};


const addDrawnRectangleRule = (params: { pageNumber: number; rect: UiRect }) => {
clearNotices();
const rectangleNumber = nextRectangleNumberRef.current;
nextRectangleNumberRef.current += 1;

const newRule: UiRule = {
id: newId(),
kind: "rectangle",
value: `${t("rules.rectangle.title")} ${rectangleNumber} (${t("rules.page.title")} ${params.pageNumber})`,
rectangleNumber,
pageNumber: params.pageNumber,
rect: params.rect,
};

setRules((prev) => [...prev, newRule]);
};

const handleSubmit: React.FormEventHandler<HTMLFormElement> = async (e) => {
e.preventDefault();
setErrorInfo(null);
Expand Down Expand Up @@ -244,6 +272,8 @@ export default function App() {
onSelectionChange={setPendingSelection}
onCurrentPageChange={setCurrentPage}
onPageSizeChange={handlePageSizeChange}
isDrawingRect={isDrawingRect}
onAddDrawnRect={addDrawnRectangleRule}
/>
) : (
<div
Expand Down Expand Up @@ -283,6 +313,8 @@ export default function App() {
pendingSelectionText={pendingSelection?.text ?? ""}
canAddSelection={!!pendingSelection && pendingSelection.rects.length > 0}
onAddSelection={addPendingSelection}
isDrawingRect={isDrawingRect}
onToggleDrawSelection={() => setIsDrawingRect((prev) => !prev)}
canCensorPage={!!currentPage && !!pageSizes[currentPage]}
onCensorPage={addCurrentPageRule}
/>
Expand Down
136 changes: 132 additions & 4 deletions frontend/src/components/PdfViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useMemo, useRef, useState } from "react";
import type { PointerEvent as ReactPointerEvent } from "react";
import type { PresetKey } from "../api";
import type { UiRect, UiRule } from "../types/uiRules";

Expand Down Expand Up @@ -28,6 +29,15 @@ type PdfDocumentProxy = {
destroy: () => void;
};


type DrawDraft = {
pageIndex: number;
startX: number;
startY: number;
currentX: number;
currentY: number;
};

type PdfJsLib = {
GlobalWorkerOptions: { workerSrc: string };
getDocument: (params: { data: Uint8Array }) => { promise: Promise<PdfDocumentProxy> };
Expand Down Expand Up @@ -55,8 +65,10 @@ export function PdfViewer(props: {
onSelectionChange: (selection: { text: string; rects: UiRect[] } | null) => void;
onCurrentPageChange: (pageNumber: number | null) => void;
onPageSizeChange: (pageNumber: number, size: { width: number; height: number }) => void;
isDrawingRect: boolean;
onAddDrawnRect: (params: { pageNumber: number; rect: UiRect }) => void;
}) {
const { file, rules, presetKeys, t, onSelectionChange, onCurrentPageChange, onPageSizeChange } = props;
const { file, rules, presetKeys, t, onSelectionChange, onCurrentPageChange, onPageSizeChange, isDrawingRect, onAddDrawnRect } = props;

const [pdfDoc, setPdfDoc] = useState<PdfDocumentProxy | null>(null);
const [isLoading, setIsLoading] = useState(true);
Expand All @@ -70,6 +82,7 @@ export function PdfViewer(props: {
const pageRefs = useRef<Array<HTMLDivElement | null>>([]);
const pageScalesRef = useRef<Array<number>>([]);
const pdfjsRef = useRef<PdfJsLib | null>(null);
const [drawDraft, setDrawDraft] = useState<DrawDraft | null>(null);

useEffect(() => {
const el = containerRef.current;
Expand Down Expand Up @@ -97,6 +110,7 @@ export function PdfViewer(props: {
setPdfDoc(null);
setError(null);
setIsLoading(true);
setDrawDraft(null);

(async () => {
try {
Expand Down Expand Up @@ -244,6 +258,11 @@ export function PdfViewer(props: {

useEffect(() => {
const computeSelection = () => {
if (isDrawingRect) {
onSelectionChange(null);
return;
}

const selection = window.getSelection();
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
onSelectionChange(null);
Expand Down Expand Up @@ -309,7 +328,77 @@ export function PdfViewer(props: {
return () => {
document.removeEventListener("selectionchange", computeSelection);
};
}, [onSelectionChange, onCurrentPageChange]);
}, [onSelectionChange, onCurrentPageChange, isDrawingRect]);


const clampPoint = (value: number, max: number) => Math.max(0, Math.min(value, max));

const beginDraw = (pageIndex: number, event: ReactPointerEvent<HTMLDivElement>) => {
if (!isDrawingRect) return;

const layer = event.currentTarget;
const bounds = layer.getBoundingClientRect();
const x = clampPoint(event.clientX - bounds.left, bounds.width);
const y = clampPoint(event.clientY - bounds.top, bounds.height);

layer.setPointerCapture(event.pointerId);
setDrawDraft({ pageIndex, startX: x, startY: y, currentX: x, currentY: y });
event.preventDefault();
};

const moveDraw = (pageIndex: number, event: ReactPointerEvent<HTMLDivElement>) => {
if (!isDrawingRect || !drawDraft || drawDraft.pageIndex !== pageIndex) return;

const layer = event.currentTarget;
const bounds = layer.getBoundingClientRect();
const x = clampPoint(event.clientX - bounds.left, bounds.width);
const y = clampPoint(event.clientY - bounds.top, bounds.height);

setDrawDraft((prev) => (prev && prev.pageIndex === pageIndex ? { ...prev, currentX: x, currentY: y } : prev));
event.preventDefault();
};

const endDraw = (pageIndex: number, event: ReactPointerEvent<HTMLDivElement>) => {
if (!isDrawingRect || !drawDraft || drawDraft.pageIndex !== pageIndex) return;

const layer = event.currentTarget;
if (layer.hasPointerCapture(event.pointerId)) {
layer.releasePointerCapture(event.pointerId);
}

const bounds = layer.getBoundingClientRect();
const x = clampPoint(event.clientX - bounds.left, bounds.width);
const y = clampPoint(event.clientY - bounds.top, bounds.height);

const left = Math.min(drawDraft.startX, x);
const right = Math.max(drawDraft.startX, x);
const top = Math.min(drawDraft.startY, y);
const bottom = Math.max(drawDraft.startY, y);

const minSize = 4;
if (right - left >= minSize && bottom - top >= minSize) {
const scale = pageScalesRef.current[pageIndex] ?? 1;
if (scale > 0) {
onAddDrawnRect({
pageNumber: pageIndex + 1,
rect: {
page: pageIndex,
x0: left / scale,
y0: top / scale,
x1: right / scale,
y1: bottom / scale,
},
});
}
}

setDrawDraft(null);
event.preventDefault();
};

const cancelDraw = () => {
if (drawDraft) setDrawDraft(null);
};

if (error) {
return <div className="pdfViewerMessage bad">{error}</div>;
Expand Down Expand Up @@ -342,7 +431,26 @@ export function PdfViewer(props: {
}}
/>
<div
className="pdfTextLayer textLayer"
className={`pdfDrawLayer ${isDrawingRect ? "pdfDrawLayerActive" : ""}`.trim()}
onPointerDown={(event) => beginDraw(pageNumber - 1, event)}
onPointerMove={(event) => moveDraw(pageNumber - 1, event)}
onPointerUp={(event) => endDraw(pageNumber - 1, event)}
onPointerCancel={cancelDraw}
>
{drawDraft && drawDraft.pageIndex === pageNumber - 1 ? (
<div
className="redactionPreviewRect"
style={{
left: `${Math.min(drawDraft.startX, drawDraft.currentX)}px`,
top: `${Math.min(drawDraft.startY, drawDraft.currentY)}px`,
width: `${Math.abs(drawDraft.currentX - drawDraft.startX)}px`,
height: `${Math.abs(drawDraft.currentY - drawDraft.startY)}px`,
}}
/>
) : null}
</div>
<div
className={`pdfTextLayer textLayer ${isDrawingRect ? "pdfTextLayerNoPointer" : ""}`.trim()}
ref={(el) => {
textLayerRefs.current[pageNumber - 1] = el;
}}
Expand Down Expand Up @@ -411,6 +519,18 @@ function applyPreviewHighlights(
});
}

const rectangleRules = rules.filter((r): r is Extract<UiRule, { kind: "rectangle" }> => r.kind === "rectangle");
for (const rectangleRule of rectangleRules) {
if (rectangleRule.pageNumber !== pageIndex + 1) continue;
const rect = rectangleRule.rect;
drawPreviewRect(previewLayer, {
left: rect.x0 * scale,
top: rect.y0 * scale,
width: (rect.x1 - rect.x0) * scale,
height: (rect.y1 - rect.y0) * scale,
}, String(rectangleRule.rectangleNumber));
}

const selectionRules = rules.filter((r): r is Extract<UiRule, { kind: "selection" }> => r.kind === "selection");
for (const selectionRule of selectionRules) {
for (const rect of selectionRule.rects) {
Expand All @@ -428,6 +548,7 @@ function applyPreviewHighlights(
function drawPreviewRect(
previewLayer: HTMLDivElement,
rect: { left: number; top: number; width: number; height: number },
label?: string,
) {
const width = rect.width;
const height = rect.height;
Expand All @@ -439,6 +560,13 @@ function drawPreviewRect(
highlight.style.top = `${rect.top}px`;
highlight.style.width = `${width}px`;
highlight.style.height = `${height}px`;
if (label) {
const badge = document.createElement("div");
badge.className = "redactionPreviewRectLabel";
badge.textContent = label;
highlight.appendChild(badge);
}

previewLayer.appendChild(highlight);
}

Expand All @@ -449,7 +577,7 @@ function collectMatches(
): Array<{ start: number; end: number }> {
const matches: Array<{ start: number; end: number }> = [];
for (const rule of rules) {
if (rule.kind === "selection" || rule.kind === "page") continue;
if (rule.kind === "selection" || rule.kind === "page" || rule.kind === "rectangle") continue;
const value = rule.value.trim();
if (!value) continue;
const ranges =
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/Rules/RulesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function RuleOptionPills(props: {
export function RulesList(props: {
t: (k: string) => string;
rules: UiRule[];
kindLabel: (k: RuleKind | "selection" | "page") => string;
kindLabel: (k: RuleKind | "selection" | "page" | "rectangle") => string;
onEdit: (r: UiRule) => void;
onDelete: (id: string) => void;
}) {
Expand Down Expand Up @@ -138,11 +138,11 @@ export function RulesList(props: {
{kindLabel(r.kind)}
</div>

{r.kind !== "selection" && r.kind !== "page" ? <RuleOptionPills rule={r} t={t} /> : null}
{r.kind !== "selection" && r.kind !== "page" && r.kind !== "rectangle" ? <RuleOptionPills rule={r} t={t} /> : null}
</div>

<div style={{ display: "flex", gap: 8, flexShrink: 0 }}>
{r.kind !== "selection" && r.kind !== "page" ? (
{r.kind !== "selection" && r.kind !== "page" && r.kind !== "rectangle" ? (
<button
type="button"
style={actionButtonStyle}
Expand Down
21 changes: 16 additions & 5 deletions frontend/src/components/Rules/RulesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { RuleAddBar } from "./RuleAddBar";
import { RulesList } from "./RulesList";
import { EditRuleModal } from "./EditRuleModal";

function kindLabel(t: (k: string) => string, kind: RuleKind | "selection" | "page") {
function kindLabel(t: (k: string) => string, kind: RuleKind | "selection" | "page" | "rectangle") {
if (kind === "selection") return t("rules.badge.selection");
if (kind === "page") return t("rules.badge.page");
if (kind === "rectangle") return t("rules.badge.rectangle");
return t("rules.badge.request");
}

Expand All @@ -29,10 +30,12 @@ export function RulesSection(props: {
pendingSelectionText: string;
canAddSelection: boolean;
onAddSelection: () => void;
isDrawingRect: boolean;
onToggleDrawSelection: () => void;
canCensorPage: boolean;
onCensorPage: () => void;
}) {
const { t, rules, setRules, onUserChange, pendingSelectionText, canAddSelection, onAddSelection, canCensorPage, onCensorPage } = props;
const { t, rules, setRules, onUserChange, pendingSelectionText, canAddSelection, onAddSelection, isDrawingRect, onToggleDrawSelection, canCensorPage, onCensorPage } = props;

const [draftKind, setDraftKind] = useState<RuleKind>("exact");
const [draftValue, setDraftValue] = useState("");
Expand Down Expand Up @@ -63,7 +66,7 @@ export function RulesSection(props: {
);

const openEdit = (r: UiRule) => {
if (r.kind === "selection" || r.kind === "page") return;
if (r.kind === "selection" || r.kind === "page" || r.kind === "rectangle") return;
setEditingId(r.id);
};
const closeEdit = () => setEditingId(null);
Expand Down Expand Up @@ -213,8 +216,16 @@ export function RulesSection(props: {
)}

<div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 8, marginTop: 10 }}>
<button type="button" className="buttonSecondary" disabled style={{ marginTop: 0 }}>
{t("rules.action.drawSelection")}
<button
type="button"
className="buttonSecondary"
style={{ marginTop: 0 }}
onClick={() => {
onUserChange();
onToggleDrawSelection();
}}
>
{isDrawingRect ? t("rules.action.stopDrawingSelection") : t("rules.action.drawSelection")}
</button>
<button
type="button"
Expand Down
Loading
Loading