-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract javascript strings
- Loading branch information
1 parent
c725046
commit 3838834
Showing
3 changed files
with
79 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters