1
0
Fork 0

Merge pull request 'Bug412-JiangLetian-Refactor' (#54) from Bug412-JiangLetian-Refactor into master

Reviewed-on: http://121.4.94.30:3000/mrlan/EnglishPal/pulls/54
Bug487-WuYuhan
mrlan 2022-08-05 16:18:06 +08:00
commit 02ffcd3b59
2 changed files with 28 additions and 40 deletions

View File

@ -1,7 +1,7 @@
let isHighlight = true; let isHighlight = true;
function cancelBtnHandler() { function cancelBtnHandler() {
cancel_highLight(); cancelHighlighting();
document.getElementById("text-content").removeEventListener("click", fillInWord, false); document.getElementById("text-content").removeEventListener("click", fillInWord, false);
document.getElementById("text-content").removeEventListener("touchstart", fillInWord, false); document.getElementById("text-content").removeEventListener("touchstart", fillInWord, false);
document.getElementById("text-content").addEventListener("click", fillInWord2, false); document.getElementById("text-content").addEventListener("click", fillInWord2, false);
@ -22,56 +22,45 @@ function getWord() {
function highLight() { function highLight() {
if (!isHighlight) return; if (!isHighlight) return;
let txt = document.getElementById("article").innerText; let articleContent = document.getElementById("article").innerText;
let sel_word1 = document.getElementById("selected-words"); let pickedWords = document.getElementById("selected-words"); // words picked to the text area
let sel_word2 = document.getElementById("selected-words2"); let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
if (sel_word1 != null) { let allWords = pickedWords.value + " " + dictionaryWords.value;
const list = sel_word1.value.split(" "); const list = allWords.split(" ");
for (let i = 0; i < list.length; ++i) { for (let i = 0; i < list.length; ++i) {
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除字符串两边空字符 list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符
if (list[i] !== "" && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) { if (list[i] !== "" && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) {
//将正则表达式进行修改 表示搜索的是两端带有空格的list[i] 那么搜索的便为一个完整单词 然后在mark标签的前后各加了一个空格对其进行替换 //将文章中所有出现该单词word的地方改为" <mark>" + word + "<mark> "。 正则表达式RegExp()中,"\\s"代表单词前后必须要有空格,以防止只对单词中的部分字符高亮的情况出现。
txt = txt.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " <mark>" + list[i] + "</mark> "); articleContent = articleContent.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " <mark>" + list[i] + "</mark> ");
}
} }
} }
if (sel_word2 != null) { document.getElementById("article").innerHTML = articleContent;
const list2 = sel_word2.value.split(" ");
for (let i = 0; i < list2.length; ++i) {
list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
if (list2[i] !== "" && "<mark>".indexOf(list2[i]) === -1 && "</mark>".indexOf(list2[i]) === -1) {
//将正则表达式进行修改 表示搜索的是两端带有空格的list2[i] 那么搜索的便为一个完整单词 然后在mark标签的前后各加了一个空格对其进行替换
txt = txt.replace(new RegExp("\\s"+list2[i]+"\\s", "g"), " <mark>" + list2[i] + "</mark> ");
}
}
}
document.getElementById("article").innerHTML = txt;
} }
function cancel_highLight() { function cancelHighlighting() {
const list = sel_word1.value.split(" "); let articleContent = document.getElementById("article").innerText;
let txt = document.getElementById("article").innerText; let pickedWords = document.getElementById("selected-words");
let sel_word1 = document.getElementById("selected-words"); const dictionaryWords = document.getElementById("selected-words2");
const sel_word2 = document.getElementById("selected-words2"); const list = pickedWords.value.split(" ");
if (sel_word1 != null) { if (pickedWords != null) {
for (let i = 0; i < list.length; ++i) { for (let i = 0; i < list.length; ++i) {
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");
if (list[i] !== "") { if (list[i] !== "") {
txt = txt.replace("<mark>" + list[i] + "</mark>", "list[i]"); articleContent = articleContent.replace("<mark>" + list[i] + "</mark>", "list[i]");
} }
} }
} }
if (sel_word2 != null) { if (dictionaryWords != null) {
let list2 = sel_word1.value.split(" "); let list2 = pickedWords.value.split(" ");
for (let i = 0; i < list2.length; ++i) { for (let i = 0; i < list2.length; ++i) {
list2 = sel_word2.value.split(" "); list2 = dictionaryWords.value.split(" ");
list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, ""); list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
if (list2[i] !== "") { if (list2[i] !== "") {
txt = txt.replace("<mark>" + list[i] + "</mark>", "list[i]"); articleContent = articleContent.replace("<mark>" + list[i] + "</mark>", "list[i]");
} }
} }
} }
document.getElementById("article").innerHTML = txt; document.getElementById("article").innerHTML = articleContent;
} }
function fillInWord() { function fillInWord() {
@ -79,17 +68,16 @@ function fillInWord() {
} }
function fillInWord2() { function fillInWord2() {
cancel_highLight(); cancelHighlighting();
} }
function ChangeHighlight() { function toggleHighlighting() {
if (isHighlight) { if (isHighlight) {
isHighlight = false; isHighlight = false;
cancel_highLight(); cancelHighlighting();
} else { } else {
isHighlight = true; isHighlight = true;
highLight(); highLight();
} }
} }

View File

@ -38,7 +38,7 @@
<p><b>阅读文章并回答问题</b></p> <p><b>阅读文章并回答问题</b></p>
<div id="text-content">{{ today_article|safe }}</div> <div id="text-content">{{ today_article|safe }}</div>
<input type="checkbox" onclick="ChangeHighlight()" checked/>生词高亮 <input type="checkbox" onclick="toggleHighlighting()" checked/>生词高亮
<input type="checkbox" onclick="onReadClick()" checked/>大声朗读 <input type="checkbox" onclick="onReadClick()" checked/>大声朗读
<input type="checkbox" onclick="onChooseClick()" checked/>划词入库 <input type="checkbox" onclick="onChooseClick()" checked/>划词入库
<div class="range"> <div class="range">