Code Monkey home page Code Monkey logo

Comments (4)

vitonsky avatar vitonsky commented on September 26, 2024

Sounds good. I would implement it as another popup, not as a "selected text translation" feature.

How this feature can be implemented technically?

If you need to translate one word, we have to detect what word user hover on. We can track hovered element with pointeover event, but how to detect exact word? Have you some ideas?

from linguist.

WofWca avatar WofWca commented on September 26, 2024

How this feature can be implemented technically?

The extension that I mentioned appears to be doing some funky markup manipulation: https://github.com/artemave/translate_onhover/blob/12aab5217677b0ba3579106c1200443454c87a16/contentscript.js#L166, I'm not sure what's exactly going on there, maybe it transforms a text node into a bunch of text nodes each containing one word (which, I assume, may be the reason for some bugs like this one).

Another possible implementation would be to determine the word being hovered over by looking at the position of the cursor relative to the text node's rectangle. E.g. if a text node has two equally long words, we check whether the cursor is on the right or on the left side of the text element. But yeah, it's gonna be pretty inaccurate, at least for a very basic implementation. I'm sure this algorithm is implemented in some very low-level things like OS or some rendering engines (LibreOffice, game engines, text editors).

It makes sense to search for another solution.

from linguist.

vitonsky avatar vitonsky commented on September 26, 2024

@WofWca nothing to add, all your points are right. 1st solution unacceptable, because Linguist must never change original DOM structure, 2nd solution may be inaccurate by many reasons and we cannot to ensure nothing with this way.

But i like your idea, so i will keep it in mind, if you will find a possible solution, please return back here and add a new information

from linguist.

vitonsky avatar vitonsky commented on September 26, 2024

We could potentially clone hovered element to shadowRoot and split nodes there to calculate position, but this approach are not trivial, hard to maintain and may generate a lot of bugs like "texts styles are different in virtual element".

I imagine an ideal solution as some intersection API for chars - we detect hovered char and get chars around. Maybe we could hack text selection API.

But if such API does not exist, we could think how to simplify solution above

from linguist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.