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

Turn highlightText into a class TextHighlight #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Treora
Copy link
Contributor

@Treora Treora commented Nov 23, 2022

Currently, highlightText returns a function that removes the highlight again. In some cases, one may want more possibilities, e.g. attaching a hover event listener to the highlight elements, measuring its location on the page, etc.

This change introduces a TextHighlight class. Instead of highlightText(…) one would write new TextHighlight(…), and the created object has a remove() method. That object’s highlightElements property gives access to the array of <mark> (or whichever) elements that used to be an internal, inaccessible variable.

The previous API is kept available for now using a simple wrapper; we could remove it in a new release.

Keeping the previous API available for now, can remove in a new release.
@reckart reckart added the ⭐️ Enhancement Improvement or new feature for users label Oct 3, 2023
@AdamMYoung
Copy link

Any chance we'll be seeing this merged any time soon? It would be super useful to my use-case, as currently I'm having to remove and re-highlight all my selectors due to having to change the highlight colour based on annotation order. This would greatly optimize how I'm using the lib

@Treora
Copy link
Contributor Author

Treora commented Apr 25, 2024 via email

@AdamMYoung
Copy link

Hey @Treora, not to worry. I've adopted this library at work for building out an annotator for HTML documents we receive, and it's been fantastic so far. As a work around I ended up abstracting this away in a similar way as your PR, and querying the mark tags with document.getElementsByClassName and a unique class identifier, so no worries there.

The only real issues I've ran into is around tables really which I believe was mentioned in an issue somewhere, but have worked around it by iterating down through table cells and highlighting text nodes manually (which isn't very performant, but works well enough for our use case).

Other than that, it's all been smooth sailing. Really appreciate all the work that's been put into it so far, especially having something based on W3C standards for future-proofing what we're building!

@tilgovi
Copy link
Contributor

tilgovi commented Apr 27, 2024

I had eventually wanted to replace this highlighter with the Highlight API. I imagined that if we did this we might totally hide the fact that there are elements at all in the fallback implementation.

Can either of you say more about what you need to access the mark elements for? Some things could be handled by the Highlight API (like getting the boundary rectangles via the ranges) but I want to understand what you need.

@reckart
Copy link
Member

reckart commented Apr 28, 2024

Other use-case, but I certainly "access" the highlight elements from CSS rules in order to style them and occasionally also query them from the DOM and post-process them. The Highlight API would not be viable for me I believe - to inflexible.

Here an example:

Screenshot 2024-04-28 at 09 05 03

@AdamMYoung
Copy link

AdamMYoung commented Apr 29, 2024

In my use-case, the styles are pre-determined so the Highlight API is a viable option. I think the only downside like @reckart mentioned is the lack of run-time styling. Going to take a look into the Highlighter API though, thanks for the heads up

As an aside, the Highlight API would also fix the issue raised by @Treora for table highlighting in #131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐️ Enhancement Improvement or new feature for users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants