Skip to content

Commit

Permalink
refactor: extract javascript strings
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Oct 17, 2024
1 parent c725046 commit 3838834
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 83 deletions.
36 changes: 36 additions & 0 deletions app/src/main/assets/select_paragraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
let selection = window.getSelection();
if (selection.rangeCount === 0) return;

let range = selection.getRangeAt(0);
let startContainer = range.startContainer;
let endContainer = range.endContainer;

// Check if the selection is within a single text node
if (startContainer !== endContainer || startContainer.nodeType !== Node.TEXT_NODE) {
return;
}

let textContent = startContainer.textContent;
let startOffset = range.startOffset;
let endOffset = range.endOffset;

let paragraphStart = startOffset;
let paragraphEnd = endOffset;

// Move the start of the range to the start of the paragraph (i.e., look for newline or start of the node)
while (paragraphStart > 0 && textContent[paragraphStart - 1] !== '\n') {
paragraphStart--;
}

// Move the end of the range to the end of the paragraph (i.e., look for newline or end of the node)
while (paragraphEnd < textContent.length && textContent[paragraphEnd] !== '\n') {
paragraphEnd++;
}

// Set the range to the paragraph boundaries
range.setStart(startContainer, paragraphStart);
range.setEnd(startContainer, paragraphEnd);

// Clear previous selection and set the new one
selection.removeAllRanges();
selection.addRange(range);
36 changes: 36 additions & 0 deletions app/src/main/assets/select_sentence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
let selection = window.getSelection();
if (selection.rangeCount === 0) return;

let range = selection.getRangeAt(0);
let startContainer = range.startContainer;
let endContainer = range.endContainer;

if (startContainer !== endContainer || startContainer.nodeType !== Node.TEXT_NODE) {
// Only handle cases where the selection is within a single text node
return;
}

let textContent = startContainer.textContent;
let startOffset = range.startOffset;
let endOffset = range.endOffset;

let sentenceStart = startOffset;
let sentenceEnd = endOffset;

// Move the start of the range to the start of the sentence
while (sentenceStart > 0 && ![".", "?", "。", "!"].includes(textContent[sentenceStart - 1])) {
sentenceStart--;
}

// Move the end of the range to the end of the sentence
while (sentenceEnd < textContent.length && ![".", "?", "。", "!"].includes(textContent[sentenceEnd])) {
sentenceEnd++;
}

// Set the range to the sentence boundaries
range.setStart(startContainer, sentenceStart);
range.setEnd(startContainer, sentenceEnd);

// Clear previous selection and set the new one
selection.removeAllRanges();
selection.addRange(range);
90 changes: 7 additions & 83 deletions app/src/main/java/info/plateaukao/einkbro/view/NinjaWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 +969,13 @@ open class NinjaWebView(
}

fun selectSentence(point: Point) {
evaluateJavascript(jsSelectSentence) {
evaluateJavascript(loadJsFile("select_sentence.js")) {
this.postDelayed({ simulateClick(point) }, 100)
}
}

fun selectParagraph(point: Point) {
evaluateJavascript(jsSelectParagraph) {
evaluateJavascript(loadJsFile("select_paragraph.js")) {
this.postDelayed({ simulateClick(point) }, 100)
}
}
Expand Down Expand Up @@ -1007,89 +1007,13 @@ open class NinjaWebView(
dispatchKeyEvent(upEvent)
}

companion object {
private const val FAKE_PRE_PROGRESS = 5

private const val jsSelectParagraph = """
javascript:(function () {
let selection = window.getSelection();
if (selection.rangeCount === 0) return;
let range = selection.getRangeAt(0);
let startContainer = range.startContainer;
let endContainer = range.endContainer;
// Check if the selection is within a single text node
if (startContainer !== endContainer || startContainer.nodeType !== Node.TEXT_NODE) {
return;
}
let textContent = startContainer.textContent;
let startOffset = range.startOffset;
let endOffset = range.endOffset;
let paragraphStart = startOffset;
let paragraphEnd = endOffset;
// Move the start of the range to the start of the paragraph (i.e., look for newline or start of the node)
while (paragraphStart > 0 && textContent[paragraphStart - 1] !== '\n') {
paragraphStart--;
private fun loadJsFile(fileName: String): String {
val jsContent = String(getByteArrayFromAsset(fileName), Charsets.UTF_8)
return "javascript:(function() {$jsContent})()"
}

// Move the end of the range to the end of the paragraph (i.e., look for newline or end of the node)
while (paragraphEnd < textContent.length && textContent[paragraphEnd] !== '\n') {
paragraphEnd++;
}
// Set the range to the paragraph boundaries
range.setStart(startContainer, paragraphStart);
range.setEnd(startContainer, paragraphEnd);
// Clear previous selection and set the new one
selection.removeAllRanges();
selection.addRange(range);
})();
"""
private const val jsSelectSentence = """
javascript:(function () {
let selection = window.getSelection();
if (selection.rangeCount === 0) return;
let range = selection.getRangeAt(0);
let startContainer = range.startContainer;
let endContainer = range.endContainer;
if (startContainer !== endContainer || startContainer.nodeType !== Node.TEXT_NODE) {
// Only handle cases where the selection is within a single text node
return;
}
let textContent = startContainer.textContent;
let startOffset = range.startOffset;
let endOffset = range.endOffset;
let sentenceStart = startOffset;
let sentenceEnd = endOffset;
// Move the start of the range to the start of the sentence
while (sentenceStart > 0 && ![".", "?", "。", "!"].includes(textContent[sentenceStart - 1])) {
sentenceStart--;
}
// Move the end of the range to the end of the sentence
while (sentenceEnd < textContent.length && ![".", "?", "。", "!"].includes(textContent[sentenceEnd])) {
sentenceEnd++;
}
// Set the range to the sentence boundaries
range.setStart(startContainer, sentenceStart);
range.setEnd(startContainer, sentenceEnd);
// Clear previous selection and set the new one
selection.removeAllRanges();
selection.addRange(range);
})();
"""
companion object {
private const val FAKE_PRE_PROGRESS = 5

private const val jsGetSelectedTextWithContextV2 = """
javascript:(function() {
Expand Down

0 comments on commit 3838834

Please sign in to comment.