implement text selection copying

This commit is contained in:
Satindar Dhillon 2022-12-01 13:03:39 -08:00
parent c022ef1874
commit 28c1628a97
6 changed files with 31 additions and 4 deletions

File diff suppressed because one or more lines are too long

View file

@ -279,6 +279,15 @@ class OmnivoreWebView(context: Context) : WebView(context) {
}
true
}
R.id.copyTextSelection -> {
val script = "var event = new Event('copyTextSelection');document.dispatchEvent(event);"
evaluateJavascript(script) {
clearFocus()
mode.finish()
actionMode = null
}
true
}
R.id.removeHighlight -> {
val script = "var event = new Event('remove');document.dispatchEvent(event);"
evaluateJavascript(script) {

View file

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/copyTextSelection"
android:title="@string/copyTextSelection"
app:showAsAction="always">
</item>
<item
android:id="@+id/highlight"
android:title="@string/highlight_menu_action"

View file

@ -5,9 +5,10 @@
<string name="welcome_subtitle">Save articles and read them later in our distraction-free reader.</string>
<string name="highlight_menu_action">Highlight</string>
<string name="copy_menu_action">Copy</string>
<string name="annotate_menu_action">Annotate</string>
<string name="annotate_menu_action">Note</string>
<string name="pdf_remove_highlight">Remove</string>
<string name="pdf_highlight_menu_action">Highlight</string>
<string name="pdf_highlight_copy">Copy</string>
<string name="highlight_note">Note</string>
<string name="copyTextSelection">Copy</string>
</resources>

File diff suppressed because one or more lines are too long

View file

@ -132,6 +132,15 @@ export function useSelection(
[highlightLocations]
)
const copyTextSelection = useCallback(async () => {
// Send message to Android to paste since we don't have the
// correct permissions to write to clipboard from WebView directly
window.AndroidWebKitMessenger?.handleIdentifiableMessage(
'writeToClipboard',
JSON.stringify({ quote: selectionAttributes?.selection.toString() })
)
}, [selectionAttributes?.selection])
useEffect(() => {
if (disabled) {
return
@ -140,13 +149,15 @@ export function useSelection(
document.addEventListener('mouseup', handleFinishTouch)
document.addEventListener('touchend', handleFinishTouch)
document.addEventListener('contextmenu', handleFinishTouch)
document.addEventListener('copyTextSelection', copyTextSelection)
return () => {
document.removeEventListener('mouseup', handleFinishTouch)
document.removeEventListener('touchend', handleFinishTouch)
document.removeEventListener('contextmenu', handleFinishTouch)
document.removeEventListener('copyTextSelection', copyTextSelection)
}
}, [highlightLocations, handleFinishTouch, disabled])
}, [highlightLocations, handleFinishTouch, disabled, copyTextSelection])
return [selectionAttributes, setSelectionAttributes]
}