fix(web): 字数计算考虑换行符

This commit is contained in:
Xwite
2023-05-09 18:19:45 +08:00
parent 8c730266a7
commit 596ea46916
2 changed files with 6 additions and 6 deletions
@@ -41,9 +41,8 @@ const calculateWordCount = (paragraph) => {
const imgPattern = /<img[^>]*src="[^"]*(?:"[^>]+\})?"[^>]*>/g; const imgPattern = /<img[^>]*src="[^"]*(?:"[^>]+\})?"[^>]*>/g;
//内嵌图片文字为1 //内嵌图片文字为1
const imagePlaceHolder = " "; const imagePlaceHolder = " ";
return paragraph.trim().replaceAll(imgPattern, imagePlaceHolder).length; return paragraph.replaceAll(imgPattern, imagePlaceHolder).length;
}; };
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
+5 -4
View File
@@ -292,8 +292,9 @@ const toChapterPos = (pos) => {
nextTick(() => { nextTick(() => {
let wordCount = 0; let wordCount = 0;
for (let element of chapter.value[0].children) { for (let element of chapter.value[0].children) {
wordCount += parseInt(element.getAttribute("wordCount")); wordCount += parseInt(element.getAttribute("wordCount")) + 1;//计算换行符
if (wordCount >= pos) { if (wordCount - 1 >= pos) {
//最后一段没有换行符
jump(element, { jump(element, {
duration: 0, duration: 0,
}); });
@@ -313,9 +314,9 @@ const computeChapterPos = () => {
//计算已读字数 //计算已读字数
let mChapterPos = 0; let mChapterPos = 0;
for (let paragraph of chapterElement.children) { for (let paragraph of chapterElement.children) {
mChapterPos += parseInt(paragraph.getAttribute("wordCount")); mChapterPos += parseInt(paragraph.getAttribute("wordCount")) + 1; //计算换行符
if (paragraph.getBoundingClientRect().top >= 0) { if (paragraph.getBoundingClientRect().top >= 0) {
chapterPos.value = mChapterPos; chapterPos.value = mChapterPos - 1; //最后一段没有换行符
break; break;
} }
} }