fix: remove false highlights

This commit is contained in:
alam00000
2026-03-11 15:54:01 +05:30
parent 18b469a334
commit 272a8cd038
2 changed files with 70 additions and 7 deletions

View File

@@ -41,8 +41,16 @@ export function diffAnnotations(
baseId: number
): CompareTextChange[] {
const changes: CompareTextChange[] = [];
const beforeMap = new Map(before.map((a) => [annotationKey(a), a]));
const afterMap = new Map(after.map((a) => [annotationKey(a), a]));
const beforeMap = new Map(
before
.filter(shouldCompareAnnotation)
.map((annotation) => [annotationKey(annotation), annotation])
);
const afterMap = new Map(
after
.filter(shouldCompareAnnotation)
.map((annotation) => [annotationKey(annotation), annotation])
);
let idx = baseId;
for (const [key, ann] of beforeMap) {
@@ -51,8 +59,7 @@ export function diffAnnotations(
id: `annotation-removed-${idx++}`,
type: 'removed',
category: 'annotation',
description:
`Removed ${ann.subtype} annotation: "${ann.contents || ann.title || ''}"`.trim(),
description: formatAnnotationDescription('Removed', ann),
beforeText: ann.contents || ann.title || '',
afterText: '',
beforeRects: [ann.rect],
@@ -67,8 +74,7 @@ export function diffAnnotations(
id: `annotation-added-${idx++}`,
type: 'added',
category: 'annotation',
description:
`Added ${ann.subtype} annotation: "${ann.contents || ann.title || ''}"`.trim(),
description: formatAnnotationDescription('Added', ann),
beforeText: '',
afterText: ann.contents || ann.title || '',
beforeRects: [],
@@ -80,6 +86,26 @@ export function diffAnnotations(
return changes;
}
function shouldCompareAnnotation(annotation: CompareAnnotation): boolean {
if (annotation.subtype !== 'Highlight') {
return true;
}
return Boolean(annotation.contents || annotation.title);
}
function formatAnnotationDescription(
action: 'Added' | 'Removed',
annotation: CompareAnnotation
): string {
const label = annotation.contents || annotation.title;
if (!label) {
return `${action} ${annotation.subtype} annotation`;
}
return `${action} ${annotation.subtype} annotation: "${label}"`;
}
function annotationKey(ann: CompareAnnotation): string {
return `${ann.subtype}|${ann.contents}|${Math.round(ann.rect.x)},${Math.round(ann.rect.y)}`;
}