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

Annotate across line breaks on FF, if there are other elements in between #11

Open
leonardlin opened this issue Jan 6, 2024 · 0 comments

Comments

@leonardlin
Copy link

See comment thread here
doccano/doccano#1863

on Firefox
labeling across lines breaks when there is another element in between:
image

  • In the example I try to label: "very loud.\nThe πŸ³οΈβ€πŸŒˆ car"
  • This does not work on Firefox but does work in Chrome.

As comment thread suggests it happens in the method: TextSelectionHandler.getRange()

Do you think this method works before I create a PR?

private getRange(): [number, number] {
    const selection = window.getSelection();
    // get elements.
    const startElement = selection!.anchorNode!.parentNode;
    const endElement = selection!.focusNode!.parentNode;

    // Get TextLine objects.
    // This depends on BaseText.vue component.
    // See the component in detail.
    const startLine = (
      startElement as unknown as { annotatorElement: TextLine }
    ).annotatorElement;
    const endLine = (endElement as unknown as { annotatorElement: TextLine })
      .annotatorElement;

    let startOffset = 0;
    let endOffset = 0;
    if(startLine === undefined) {
      // alternative method

      const altStartElement = (startElement?.firstChild as SVGTextElement);
      const altStartLine = (
        altStartElement as unknown as { annotatorElement: TextLine }
      ).annotatorElement;
      const altStartLineText = altStartElement.innerHTML;

      const selectionTexts = selection!.toString().split(/\r\n|\r|\n/);
      const altSelectionAnchorStart = altStartLineText.lastIndexOf(selectionTexts[0]);
      
      startOffset = altStartLine.startOffset + altSelectionAnchorStart;
      endOffset = endLine.startOffset + selection!.focusOffset;
    } else {
      // get offsets.
      startOffset = startLine.startOffset + selection!.anchorOffset;
      endOffset = endLine.startOffset + selection!.focusOffset;
    }

    selection?.removeAllRanges();
    if (startOffset > endOffset) {
      return [endOffset, startOffset];
    } else {
      return [startOffset, endOffset];
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant