Commit 998aca24 authored by liujiangnan's avatar liujiangnan

feat: 合页逻辑处理

parent 84d21769
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -41,6 +41,63 @@ body { ...@@ -41,6 +41,63 @@ body {
justify-content: center; justify-content: center;
} }
/* 多页显示样式 */
.page-wrapper {
position: relative;
display: flex;
align-items: center;
justify-content: center;
flex: 1;
margin: 0;
padding: 0;
}
/* 多页时的容器样式 */
.image-container.multi-page-container {
justify-content: stretch;
gap: 0;
}
.image-container.multi-page-container .page-wrapper {
flex: 1;
width: 50%;
margin: 0;
padding: 0;
}
/* 单页时的容器样式 */
.image-container:not(.multi-page-container) {
justify-content: center;
}
.image-container:not(.multi-page-container) .page-wrapper {
flex: none;
width: auto;
}
.page-image {
max-width: 100%;
max-height: 100%;
object-fit: contain;
display: block;
}
/* 多页时的样式 */
.page-image.multi-page {
width: 100%;
height: 100%;
object-fit: contain;
margin: 0;
padding: 0;
}
/* 当只有一页时的样式 */
.page-image:not(.multi-page) {
max-width: 100%;
max-height: 100%;
object-fit: contain;
}
.highlight-rect { .highlight-rect {
position: absolute; position: absolute;
border: 2px dashed #007bff; border: 2px dashed #007bff;
...@@ -61,15 +118,6 @@ body { ...@@ -61,15 +118,6 @@ body {
box-shadow: 0 0 5px rgba(255, 235, 59, 0.5); box-shadow: 0 0 5px rgba(255, 235, 59, 0.5);
} }
.image-container {
position: relative;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.controls { .controls {
position: absolute; position: absolute;
bottom: 20px; bottom: 20px;
......
...@@ -47,15 +47,19 @@ ...@@ -47,15 +47,19 @@
</button> </button>
<!-- 图片容器 --> <!-- 图片容器 -->
<div class="image-container"> <div class="image-container" :class="{ 'multi-page-container': currentPages.length > 1 }">
<img <!-- 多页显示 -->
v-if="currentPage" <div v-for="(page, pageIndex) in currentPages" :key="pageIndex" class="page-wrapper">
:src="currentPage.image_url" <img
:alt="'第' + (currentPageIndex + 1) + '页'" v-if="page.image_url"
class="page-image" :src="page.image_url"
@load="onImageLoad" :alt="'第' + (currentPageIndices[pageIndex] + 1) + '页'"
@error="onImageError" class="page-image"
/> :class="{ 'multi-page': currentPages.length > 1 }"
@load="onImageLoad"
@error="onImageError"
/>
</div>
<!-- 蓝色矩形框 --> <!-- 蓝色矩形框 -->
<div <div
......
...@@ -5,7 +5,7 @@ $(function () { ...@@ -5,7 +5,7 @@ $(function () {
window.courseware.getData(function (data) { window.courseware.getData(function (data) {
if (!data) { if (!data) {
// 修正默认数据路径为根目录 // 修正默认数据路径为根目录
$.getJSON('./defaultData_1.json', function (defaultData) { $.getJSON('./defaultData.json', function (defaultData) {
console.log('加载默认数据:', defaultData); console.log('加载默认数据:', defaultData);
initializeApp(defaultData); initializeApp(defaultData);
}).fail(function () { }).fail(function () {
...@@ -25,6 +25,7 @@ function initializeApp(data) { ...@@ -25,6 +25,7 @@ function initializeApp(data) {
// 数据相关 // 数据相关
bookData: data, bookData: data,
pages: data.pages || [], pages: data.pages || [],
continuity: data.continuity || [],
audioUrl: data.audio_url, audioUrl: data.audio_url,
// 状态相关 // 状态相关
...@@ -69,20 +70,164 @@ function initializeApp(data) { ...@@ -69,20 +70,164 @@ function initializeApp(data) {
}, },
computed: { computed: {
// 根据continuity数组长度计算总页数
totalPages() { totalPages() {
return this.pages.length; return this.continuity.length;
}, },
currentPage() { // 获取当前continuity项
return this.pages[this.currentPageIndex] || null; currentContinuity() {
return this.continuity[this.currentPageIndex] || null;
}, },
// 获取当前页面对应的原始页面索引数组
currentPageIndices() {
if (!this.currentContinuity) return [];
return this.currentContinuity.split(',').map(index => parseInt(index.trim()));
},
// 获取当前页面对应的原始页面数据
currentPages() {
return this.currentPageIndices.map(index => this.pages[index]).filter(page => page);
},
// 获取当前页面的开始时间(考虑多页合并)
currentPageStartTime() { currentPageStartTime() {
return this.currentPage ? this.currentPage.start : 0; if (!this.currentPages.length) return 0;
const firstPage = this.currentPages[0];
const lastPage = this.currentPages[this.currentPages.length - 1];
// 如果第一页的开始时间是null,则取第二页的开始时间
if (firstPage.start === null && this.currentPages.length > 1) {
return lastPage.start;
}
return firstPage.start || 0;
}, },
// 获取当前页面的结束时间(考虑多页合并)
currentPageEndTime() { currentPageEndTime() {
return this.currentPage ? this.currentPage.end : 0; if (!this.currentPages.length) return 0;
const firstPage = this.currentPages[0];
const lastPage = this.currentPages[this.currentPages.length - 1];
// 如果最后一页的结束时间是null,则取第一页的结束时间
if (lastPage.end === null && this.currentPages.length > 1) {
return firstPage.end;
}
return lastPage.end || 0;
},
// 获取当前页面的图片URL(显示第一页的图片)
currentPageImageUrl() {
if (!this.currentPages.length) return '';
return this.currentPages[0].image_url || '';
},
// 获取当前页面的所有矩形框数据(合并多页)
currentPageRects() {
const allRects = [];
this.currentPages.forEach((page, pageIndex) => {
if (!page || !page.groups) return;
page.groups.forEach((group, groupIndex) => {
// 安全检查
if (!group || !group.group) {
console.warn(`跳过无效的组:`, group);
return;
}
// 即使没有时间信息也要显示内容
// 为没有时间信息的组设置默认时间
let startTime = group.start;
let endTime = group.end;
// 如果时间信息为空,使用页面级别的时间
if (startTime === null || startTime === undefined) {
startTime = page.start || 0;
}
if (endTime === null || endTime === undefined) {
endTime = page.end || 0;
}
group.group.forEach((textGroup, textIndex) => {
if (textGroup && textGroup.rect) {
const rectWithData = {
...textGroup.rect,
_start: startTime,
_end: endTime,
_text: textGroup.text,
_groupIndex: groupIndex,
_sentence: group.sentence,
_pageIndex: pageIndex, // 添加页面索引
_originalPageIndex: this.currentPageIndices[pageIndex], // 添加原始页面索引
_hasTimeInfo: group.start !== null && group.end !== null // 标记是否有时间信息
};
allRects.push(rectWithData);
}
});
});
});
return allRects;
},
// 获取当前页面的所有单词数据(合并多页)
currentPageWords() {
const allWords = [];
this.currentPages.forEach((page, pageIndex) => {
if (!page || !page.groups) return;
page.groups.forEach((group, groupIndex) => {
// 安全检查
if (!group || !group.group) {
console.warn(`跳过无效的组:`, group);
return;
}
let wordIndex = 0; // 每个groups的单词索引从0开始
group.group.forEach((textGroup, textIndex) => {
if (textGroup && textGroup.words) {
textGroup.words.forEach(word => {
if (word && word.rect) {
const wordWithData = {
...word,
_text: word.text,
_sentence: group.sentence,
_groupIndex: groupIndex,
_textGroupIndex: textIndex,
_groupsIndex: wordIndex, // 添加基于当前groups的索引
_pageIndex: pageIndex,
_originalPageIndex: this.currentPageIndices[pageIndex],
_hasTimeInfo: word.start !== null && word.end !== null
};
console.log('添加单词:', {
text: word.text,
sentence: group.sentence,
groupsIndex: wordIndex,
pageIndex: pageIndex,
originalPageIndex: this.currentPageIndices[pageIndex]
});
allWords.push(wordWithData);
wordIndex++; // 递增单词索引
}
});
}
});
});
});
return allWords;
} }
}, },
...@@ -186,7 +331,7 @@ function initializeApp(data) { ...@@ -186,7 +331,7 @@ function initializeApp(data) {
// 根据时间更新页面 // 根据时间更新页面
updatePageByTime() { updatePageByTime() {
if (!this.audio || !this.pages.length) return; if (!this.audio || !this.continuity.length) return;
// 如果处于点读模式或翻译模式,不自动跳页 // 如果处于点读模式或翻译模式,不自动跳页
if (this.isPointReadMode || this.isTranslateMode) return; if (this.isPointReadMode || this.isTranslateMode) return;
...@@ -194,9 +339,12 @@ function initializeApp(data) { ...@@ -194,9 +339,12 @@ function initializeApp(data) {
const currentTime = this.audio.currentTime; const currentTime = this.audio.currentTime;
// 找到当前时间对应的页面 // 找到当前时间对应的页面
for (let i = 0; i < this.pages.length; i++) { for (let i = 0; i < this.continuity.length; i++) {
const page = this.pages[i]; const pageIndices = this.continuity[i].split(',').map(index => parseInt(index.trim()));
if (currentTime >= page.start && currentTime <= page.end) { const startTime = this.getPageStartTime(pageIndices);
const endTime = this.getPageEndTime(pageIndices);
if (currentTime >= startTime && currentTime <= endTime) {
if (this.currentPageIndex !== i) { if (this.currentPageIndex !== i) {
console.log('切换到页面:', i, '时间:', currentTime); console.log('切换到页面:', i, '时间:', currentTime);
this.currentPageIndex = i; this.currentPageIndex = i;
...@@ -207,9 +355,45 @@ function initializeApp(data) { ...@@ -207,9 +355,45 @@ function initializeApp(data) {
} }
}, },
// 获取页面开始时间(辅助方法)
getPageStartTime(pageIndices) {
if (!pageIndices || pageIndices.length === 0) return 0;
const pages = pageIndices.map(index => this.pages[index]).filter(page => page);
if (pages.length === 0) return 0;
const firstPage = pages[0];
const lastPage = pages[pages.length - 1];
// 如果第一页的开始时间是null,则取第二页的开始时间
if (firstPage.start === null && pages.length > 1) {
return lastPage.start;
}
return firstPage.start || 0;
},
// 获取页面结束时间(辅助方法)
getPageEndTime(pageIndices) {
if (!pageIndices || pageIndices.length === 0) return 0;
const pages = pageIndices.map(index => this.pages[index]).filter(page => page);
if (pages.length === 0) return 0;
const firstPage = pages[0];
const lastPage = pages[pages.length - 1];
// 如果最后一页的结束时间是null,则取第一页的结束时间
if (lastPage.end === null && pages.length > 1) {
return firstPage.end;
}
return lastPage.end || 0;
},
// 更新高亮 // 更新高亮
updateHighlights() { updateHighlights() {
if (!this.currentPage || !this.imageLoaded) return; if (!this.currentPages.length || !this.imageLoaded) return;
// 如果处于点读模式或翻译模式,不更新高亮 // 如果处于点读模式或翻译模式,不更新高亮
if (this.isPointReadMode || this.isTranslateMode) return; if (this.isPointReadMode || this.isTranslateMode) return;
...@@ -219,36 +403,34 @@ function initializeApp(data) { ...@@ -219,36 +403,34 @@ function initializeApp(data) {
const newWords = []; const newWords = [];
// 检查是否在当前页的时间范围内 // 检查是否在当前页的时间范围内
if (currentTime >= this.currentPage.start && currentTime <= this.currentPage.end) { if (currentTime >= this.currentPageStartTime && currentTime <= this.currentPageEndTime) {
// 遍历当前页面的所有组 // 使用计算属性获取所有矩形框和单词数据
this.currentPage.groups.forEach(group => { const allRects = this.currentPageRects;
// 检查句子是否有有效的时间信息 const allWords = this.currentPageWords;
if (group.start === null || group.start === undefined ||
group.end === null || group.end === undefined) { // 遍历所有矩形框,检查时间
return; // 跳过没有时间信息的句子 allRects.forEach(rect => {
if (currentTime >= rect._start && currentTime <= rect._end) {
newRects.push(rect);
} }
});
// 检查句子时间
if (currentTime >= group.start && currentTime <= group.end) { // 遍历所有单词,检查时间
// 遍历句子中的文本组 allWords.forEach(word => {
group.group.forEach(textGroup => { let wordStart = word.start;
// 添加文本组的矩形框 let wordEnd = word.end;
if (textGroup.rect) {
newRects.push(textGroup.rect); // 为没有时间信息的单词设置默认时间
} if (wordStart === null || wordStart === undefined) {
wordStart = word._start || 0;
// 检查单词时间 - 单词高亮应该保持到切换页面 }
if (textGroup.words) { if (wordEnd === null || wordEnd === undefined) {
textGroup.words.forEach(word => { wordEnd = word._end || 0;
if (word.start !== null && word.end !== null) { }
// 如果当前时间已经过了这个单词的开始时间,就保持高亮直到切换页面
if (currentTime >= word.start) { // 如果当前时间已经过了这个单词的开始时间,就保持高亮直到切换页面
newWords.push(word); if (currentTime >= wordStart) {
} newWords.push(word);
}
});
}
});
} }
}); });
} }
...@@ -270,7 +452,7 @@ function initializeApp(data) { ...@@ -270,7 +452,7 @@ function initializeApp(data) {
// 加载当前页面 // 加载当前页面
loadCurrentPage() { loadCurrentPage() {
console.log('加载页面:', this.currentPageIndex, this.currentPage); console.log('加载页面:', this.currentPageIndex, this.currentPages);
this.clearHighlights(); this.clearHighlights();
this.imageLoaded = false; this.imageLoaded = false;
...@@ -297,17 +479,25 @@ function initializeApp(data) { ...@@ -297,17 +479,25 @@ function initializeApp(data) {
this.imageLoaded = true; this.imageLoaded = true;
console.log('图片加载完成:', this.imageWidth, this.imageHeight); console.log('图片加载完成:', this.imageWidth, this.imageHeight);
// 如果处于点读模式,显示所有矩形框 // 检查是否所有图片都加载完成
if (this.isPointReadMode) { const allImages = document.querySelectorAll('.page-image');
this.showAllRects(); const loadedImages = document.querySelectorAll('.page-image[src]');
}
// 如果处于翻译模式,显示所有单词框 if (allImages.length === loadedImages.length) {
else if (this.isTranslateMode) { console.log('所有图片加载完成');
this.showAllTranslateWords();
} // 如果处于点读模式,显示所有矩形框
// 如果音频正在播放,立即更新高亮 if (this.isPointReadMode) {
else if (this.isPlaying) { this.showAllRects();
this.updateHighlights(); }
// 如果处于翻译模式,显示所有单词框
else if (this.isTranslateMode) {
this.showAllTranslateWords();
}
// 如果音频正在播放,立即更新高亮
else if (this.isPlaying) {
this.updateHighlights();
}
} }
}, },
...@@ -321,11 +511,15 @@ function initializeApp(data) { ...@@ -321,11 +511,15 @@ function initializeApp(data) {
if (!this.imageLoaded || !rect) return {}; if (!this.imageLoaded || !rect) return {};
try { try {
const imgElement = document.querySelector('.page-image'); // 根据页面索引找到对应的图片元素
if (!imgElement) return {}; const pageIndex = rect._pageIndex; // 使用当前页面中的索引,而不是原始页面索引
const imgElements = document.querySelectorAll('.page-image');
const targetImg = imgElements[pageIndex] || imgElements[0];
if (!targetImg) return {};
const imgRect = imgElement.getBoundingClientRect(); const imgRect = targetImg.getBoundingClientRect();
const containerRect = imgElement.parentElement.getBoundingClientRect(); const containerRect = targetImg.closest('.image-container').getBoundingClientRect();
// 计算图片在容器中的实际位置和尺寸 // 计算图片在容器中的实际位置和尺寸
const imgLeft = imgRect.left - containerRect.left; const imgLeft = imgRect.left - containerRect.left;
...@@ -339,6 +533,8 @@ function initializeApp(data) { ...@@ -339,6 +533,8 @@ function initializeApp(data) {
const width = parseFloat(rect.width) * imgWidth; const width = parseFloat(rect.width) * imgWidth;
const height = parseFloat(rect.height) * imgHeight; const height = parseFloat(rect.height) * imgHeight;
return { return {
position: 'absolute', position: 'absolute',
left: left + 'px', left: left + 'px',
...@@ -357,11 +553,15 @@ function initializeApp(data) { ...@@ -357,11 +553,15 @@ function initializeApp(data) {
if (!this.imageLoaded || !word || !word.rect) return {}; if (!this.imageLoaded || !word || !word.rect) return {};
try { try {
const imgElement = document.querySelector('.page-image'); // 根据页面索引找到对应的图片元素
if (!imgElement) return {}; const pageIndex = word._pageIndex; // 使用当前页面中的索引,而不是原始页面索引
const imgElements = document.querySelectorAll('.page-image');
const targetImg = imgElements[pageIndex] || imgElements[0];
if (!targetImg) return {};
const imgRect = imgElement.getBoundingClientRect(); const imgRect = targetImg.getBoundingClientRect();
const containerRect = imgElement.parentElement.getBoundingClientRect(); const containerRect = targetImg.closest('.image-container').getBoundingClientRect();
// 计算图片在容器中的实际位置和尺寸 // 计算图片在容器中的实际位置和尺寸
const imgLeft = imgRect.left - containerRect.left; const imgLeft = imgRect.left - containerRect.left;
...@@ -375,6 +575,8 @@ function initializeApp(data) { ...@@ -375,6 +575,8 @@ function initializeApp(data) {
const width = parseFloat(word.rect.width) * imgWidth; const width = parseFloat(word.rect.width) * imgWidth;
const height = parseFloat(word.rect.height) * imgHeight; const height = parseFloat(word.rect.height) * imgHeight;
return { return {
position: 'absolute', position: 'absolute',
left: left + 'px', left: left + 'px',
...@@ -471,7 +673,7 @@ function initializeApp(data) { ...@@ -471,7 +673,7 @@ function initializeApp(data) {
this.isPointReadMode = !this.isPointReadMode; this.isPointReadMode = !this.isPointReadMode;
console.log('切换点读模式:', this.isPointReadMode ? '开启' : '关闭'); console.log('切换点读模式:', this.isPointReadMode ? '开启' : '关闭');
console.log('当前页面:', this.currentPage); console.log('当前页面:', this.currentPages);
console.log('图片是否加载:', this.imageLoaded); console.log('图片是否加载:', this.imageLoaded);
if (this.isPointReadMode) { if (this.isPointReadMode) {
...@@ -519,52 +721,17 @@ function initializeApp(data) { ...@@ -519,52 +721,17 @@ function initializeApp(data) {
// 显示当前页面所有矩形框 // 显示当前页面所有矩形框
showAllRects() { showAllRects() {
console.log('showAllRects 被调用'); console.log('showAllRects 被调用');
console.log('当前页面:', this.currentPage); console.log('当前页面:', this.currentPages);
console.log('图片是否加载:', this.imageLoaded); console.log('图片是否加载:', this.imageLoaded);
if (!this.currentPage || !this.imageLoaded) { if (!this.currentPages.length || !this.imageLoaded) {
console.log('条件不满足,退出 showAllRects'); console.log('条件不满足,退出 showAllRects');
return; return;
} }
const allRects = []; // 使用计算属性获取所有矩形框
this.activeRects = this.currentPageRects;
// 遍历当前页面的所有组 console.log('显示所有矩形框:', this.activeRects.length, '个');
this.currentPage.groups.forEach((group, groupIndex) => {
console.log(`处理组 ${groupIndex}:`, group);
// 检查句子是否有有效的时间信息
if (group.start === null || group.start === undefined ||
group.end === null || group.end === undefined) {
console.log(`组 ${groupIndex} 缺少时间信息,跳过:`, group);
return;
}
// 遍历句子中的文本组
group.group.forEach((textGroup, textIndex) => {
console.log(`处理文本组 ${textIndex}:`, textGroup);
// 添加文本组的矩形框
if (textGroup.rect) {
// 为每个矩形框添加额外的数据属性,包括句子信息
const rectWithData = {
...textGroup.rect,
_start: group.start,
_end: group.end,
_text: textGroup.text,
_groupIndex: groupIndex, // 添加组索引,用于识别同一句子的矩形框
_sentence: group.sentence // 添加句子文本
};
allRects.push(rectWithData);
console.log('添加矩形框:', rectWithData);
} else {
console.log('文本组没有矩形框:', textGroup);
}
});
});
this.activeRects = allRects;
console.log('显示所有矩形框:', allRects.length, '个');
console.log('activeRects 已更新:', this.activeRects); console.log('activeRects 已更新:', this.activeRects);
// 强制更新视图 // 强制更新视图
...@@ -579,6 +746,9 @@ function initializeApp(data) { ...@@ -579,6 +746,9 @@ function initializeApp(data) {
const sentenceStart = rectData._start; const sentenceStart = rectData._start;
const sentenceEnd = rectData._end; const sentenceEnd = rectData._end;
const groupIndex = rectData._groupIndex; const groupIndex = rectData._groupIndex;
const pageIndex = rectData._pageIndex;
const originalPageIndex = rectData._originalPageIndex;
const hasTimeInfo = rectData._hasTimeInfo;
console.log('播放句子音频:', { console.log('播放句子音频:', {
index, index,
...@@ -586,12 +756,16 @@ function initializeApp(data) { ...@@ -586,12 +756,16 @@ function initializeApp(data) {
sentenceEnd, sentenceEnd,
duration: sentenceEnd - sentenceStart, duration: sentenceEnd - sentenceStart,
sentence: rectData._sentence, sentence: rectData._sentence,
groupIndex: groupIndex groupIndex: groupIndex,
pageIndex: pageIndex,
originalPageIndex: originalPageIndex,
hasTimeInfo: hasTimeInfo
}); });
// 如果正在播放同一个句子,则忽略点击 // 如果正在播放同一个句子,则忽略点击
if (this.isPlaying && this.currentPlayingRect && if (this.isPlaying && this.currentPlayingRect &&
this.currentPlayingRect._groupIndex === groupIndex) { this.currentPlayingRect._groupIndex === groupIndex &&
this.currentPlayingRect._originalPageIndex === originalPageIndex) {
console.log('正在播放同一个句子,忽略点击'); console.log('正在播放同一个句子,忽略点击');
return; return;
} }
...@@ -602,8 +776,20 @@ function initializeApp(data) { ...@@ -602,8 +776,20 @@ function initializeApp(data) {
this.audioTimer = null; this.audioTimer = null;
} }
// 如果没有时间信息,使用页面级别的时间
let startTime = sentenceStart;
let endTime = sentenceEnd;
if (!hasTimeInfo) {
const page = this.currentPages[pageIndex];
if (page) {
startTime = page.start || 0;
endTime = page.end || 0;
}
}
// 设置音频时间并播放整个句子 // 设置音频时间并播放整个句子
this.audio.currentTime = sentenceStart; this.audio.currentTime = startTime;
this.audio.play().then(() => { this.audio.play().then(() => {
this.isPlaying = true; this.isPlaying = true;
this.currentPlayingRect = rectData; this.currentPlayingRect = rectData;
...@@ -618,7 +804,7 @@ function initializeApp(data) { ...@@ -618,7 +804,7 @@ function initializeApp(data) {
this.isPlaying = false; this.isPlaying = false;
this.currentPlayingRect = null; this.currentPlayingRect = null;
} }
}, (sentenceEnd - sentenceStart) * 1000); }, (endTime - startTime) * 1000);
}).catch(error => { }).catch(error => {
console.error('播放失败:', error); console.error('播放失败:', error);
...@@ -675,11 +861,12 @@ function initializeApp(data) { ...@@ -675,11 +861,12 @@ function initializeApp(data) {
if (!rectData || !this.isPointReadMode) return; if (!rectData || !this.isPointReadMode) return;
const groupIndex = rectData._groupIndex; const groupIndex = rectData._groupIndex;
console.log('高亮句子,组索引:', groupIndex); const originalPageIndex = rectData._originalPageIndex;
console.log('高亮句子,组索引:', groupIndex, '原始页面索引:', originalPageIndex);
// 找到同一句子的所有矩形框并高亮 // 找到同一句子的所有矩形框并高亮
this.activeRects.forEach(rect => { this.activeRects.forEach(rect => {
if (rect._groupIndex === groupIndex) { if (rect._groupIndex === groupIndex && rect._originalPageIndex === originalPageIndex) {
rect._highlighted = true; rect._highlighted = true;
} }
}); });
...@@ -703,65 +890,12 @@ function initializeApp(data) { ...@@ -703,65 +890,12 @@ function initializeApp(data) {
// 获取当前页面所有矩形框的数据 // 获取当前页面所有矩形框的数据
getAllRectsData() { getAllRectsData() {
if (!this.currentPage) return []; return this.currentPageRects;
const allRects = [];
// 遍历当前页面的所有组
this.currentPage.groups.forEach(group => {
// 检查句子是否有有效的时间信息
if (group.start === null || group.start === undefined ||
group.end === null || group.end === undefined) {
return; // 跳过没有时间信息的句子
}
// 遍历句子中的文本组
group.group.forEach(textGroup => {
// 添加文本组的矩形框数据
if (textGroup.rect) {
allRects.push({
...textGroup.rect,
_start: group.start,
_end: group.end,
_text: textGroup.text
});
}
});
});
return allRects;
}, },
// 获取当前页面所有单词的扁平化数组 // 获取当前页面所有单词的扁平化数组
getAllWordsFlat() { getAllWordsFlat() {
if (!this.currentPage) return []; return this.currentPageWords;
const allWords = [];
// 遍历当前页面的所有组
this.currentPage.groups.forEach((group, groupIndex) => {
// 遍历句子中的文本组
group.group.forEach((textGroup, textIndex) => {
// 检查单词
if (textGroup.words) {
textGroup.words.forEach(word => {
if (word.rect) {
// 为每个单词添加额外的数据属性
const wordWithData = {
...word,
_text: word.text,
_sentence: group.sentence,
_groupIndex: groupIndex,
_textGroupIndex: textIndex
};
allWords.push(wordWithData);
}
});
}
});
});
return allWords;
}, },
...@@ -769,49 +903,17 @@ function initializeApp(data) { ...@@ -769,49 +903,17 @@ function initializeApp(data) {
// 显示当前页面所有翻译单词 // 显示当前页面所有翻译单词
showAllTranslateWords() { showAllTranslateWords() {
console.log('showAllTranslateWords 被调用'); console.log('showAllTranslateWords 被调用');
console.log('当前页面:', this.currentPage); console.log('当前页面:', this.currentPages);
console.log('图片是否加载:', this.imageLoaded); console.log('图片是否加载:', this.imageLoaded);
if (!this.currentPage || !this.imageLoaded) { if (!this.currentPages.length || !this.imageLoaded) {
console.log('条件不满足,退出 showAllTranslateWords'); console.log('条件不满足,退出 showAllTranslateWords');
return; return;
} }
const allWords = []; // 使用计算属性获取所有单词
this.translateWords = this.currentPageWords;
// 遍历当前页面的所有组 console.log('显示所有翻译单词:', this.translateWords.length, '个');
this.currentPage.groups.forEach((group, groupIndex) => {
console.log(`处理组 ${groupIndex}:`, group);
let wordIndex = 0; // 每个groups的单词索引从0开始
// 遍历句子中的文本组
group.group.forEach((textGroup, textIndex) => {
console.log(`处理文本组 ${textIndex}:`, textGroup);
// 检查单词
if (textGroup.words) {
textGroup.words.forEach(word => {
if (word.rect) {
// 为每个单词添加额外的数据属性
const wordWithData = {
...word,
_text: word.text, // 根据schema,单词文本在text字段
_sentence: group.sentence,
_groupIndex: groupIndex,
_groupsIndex: wordIndex // 添加基于当前groups的索引
};
allWords.push(wordWithData);
console.log('添加翻译单词:', wordWithData);
wordIndex++; // 递增单词索引
}
});
}
});
});
this.translateWords = allWords;
console.log('显示所有翻译单词:', allWords.length, '个');
console.log('translateWords 已更新:', this.translateWords); console.log('translateWords 已更新:', this.translateWords);
// 强制更新视图 // 强制更新视图
...@@ -991,7 +1093,18 @@ function initializeApp(data) { ...@@ -991,7 +1093,18 @@ function initializeApp(data) {
requestTranslation(word) { requestTranslation(word) {
const wordText = word._text || word.text; const wordText = word._text || word.text;
const sentence = word._sentence || ''; const sentence = word._sentence || '';
const groupsIndex = word._groupsIndex || 0; const groupsIndex = word._groupsIndex || 0; // 使用单词在句子中的索引
const pageIndex = word._pageIndex || 0;
const originalPageIndex = word._originalPageIndex || 0;
console.log('请求翻译:', {
wordText,
sentence,
groupsIndex,
pageIndex,
originalPageIndex,
wordData: word
});
// 真实流式请求 // 真实流式请求
this.requestStreamingTranslation(wordText, sentence, groupsIndex); this.requestStreamingTranslation(wordText, sentence, groupsIndex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment