recogito / recogito-js

A JavaScript library for text annotation
BSD 3-Clause "New" or "Revised" License
349 stars 38 forks source link

Exception if an annotation has no TextQuoteSelector #58

Closed reckart closed 2 years ago

reckart commented 2 years ago

The quote() method throws an exception if an annotation has no TextQuoteSelector. That is probably fine, but does SelectionUtils.getExactOverlaps have to use the quote really?

Screenshot 2022-01-01 at 00 35 51
/**
 * Util function that checks if the given selection is an exact overlap to any
 * existing annotations, and returns them, if so
 */
export const getExactOverlaps = (newAnnotation, selectedSpans) => {
  // All existing annotations at this point
  const existingAnnotations = [];

  selectedSpans.forEach(span => {
    const enclosingAnnotationSpan = span.closest('.r6o-annotation');
    const enclosingAnnotation = enclosingAnnotationSpan?.annotation;

    if (enclosingAnnotation && !existingAnnotations.includes(enclosingAnnotation))
      existingAnnotations.push(enclosingAnnotation);
  });

  if (existingAnnotations.length > 0)
    return existingAnnotations.filter(anno => {
      const isSameAnchor = anno.anchor == newAnnotation.anchor;
      const isSameQuote = anno.quote == newAnnotation.quote; // <<<< HERE
      return isSameAnchor && isSameQuote;
    });
  else
    return [];
};

This happens when trying to select text which has already been annotated.

rsimon commented 2 years ago

Ok, something weird is going on here. This is almost certainly a leftover from the dark ages or RecogitoJS's predecessor code. There is no anno.anchor (it's always undefined). Therefore the current check is exclusively based on the quote.

But I agree: the check should be made based on the start and end offset instead. I'll investigate and add a fix!