victorsoares96 / epubjs-react-native

ePub.js Reader for React Native
MIT License
150 stars 50 forks source link

Select whole sentence #314

Open oleksandr-serhiienko opened 1 month ago

oleksandr-serhiienko commented 1 month ago

Summary

I am trying to expend the functionality and add an extension to the menuItems so I can select whole sentence. Normal onSelected={handleSelected} works for me. But it is not clear how could I add missing logic. I am checking the injectJavascript options, but I do not find there any clear way to implement it. The example about injectJavascript is very good, but not very helpfull for now, and epubjs.ts does have the implementetion of onSelected, but I need some help in understanding, where my logic should be implemented

What platform(s) does this occur on?

Android

What workflow(s) does this occur on?

Expo Workflow

Environment (or package.json)

expo-env-info@1.2.0

Your .epub file

No response

Minimal reproducible example

No response

I confirm that i have

oleksandr-serhiienko commented 1 week ago

Please, if smb has any suggestion how could I implement it, just let me know. I would be very thanfull!

victorsoares96 commented 1 week ago

Hello! Is the functionality you want to implement based on something that already exists in another reader app? If so, can you give us some examples?

oleksandr-serhiienko commented 3 days ago

Hello!

I am trying to implement the following functionality:

Single Tap: Select the word. Double Tap: Select the entire sentence. My question is: Can I somehow implement this using injectJavaScript? Or I need to rewrite the classes directly?

Current Progress:

I have been experimenting with JavaScript in epubjs.ts, and I added the following function to select a sentence:

selectSentence(range) {
  try {
    let node = range.startContainer;
    let startOffset = range.startOffset;
    let endOffset = range.endOffset;

    while (startOffset > 0 && !".!?".includes(node.textContent[startOffset - 1])) {
      startOffset--;
    }

    while (endOffset < node.textContent.length && !".!?".includes(node.textContent[endOffset])) {
      endOffset++;
    }
    endOffset++;

    let sentenceRange = this.document.createRange();
    sentenceRange.setStart(node, startOffset);
    sentenceRange.setEnd(node, endOffset);

    alert('Range selection completed');
    alert(sentenceRange);

    return sentenceRange;
  } catch (error) {
    alert('Error in set range: ' + error.message);
    return null;
  }
}

This code successfully returns the entire sentence when given a range from the triggerSelectedEvent(t)method.

The Challenge:

I am now stuck trying to integrate this into Single Tap and Double Tap gestures.

I attempted to trigger these gestures using onSingleTap and onDoubleTap from the GestureHandler in View.tsx. However, I couldn't get these gestures to fire (I added an alert in both handlers, but nothing was triggered on actual taps). Even if I manage to trigger the gestures, I am unsure how to pass the appropriate range to the selectSentence(range) method.