Commit fac33607 authored by liujiangnan's avatar liujiangnan

fix

parent a125aaf4
......@@ -14,7 +14,7 @@ $(function () {
} else {
console.log('获取到数据:', data);
if (!data.continuity) {
data.continuity = data.pages.map((item,i)=>{return i+''});
data.continuity = data.pages.map((item, i) => { return i + '' });
}
initializeApp(data);
}
......@@ -97,30 +97,30 @@ function initializeApp(data) {
// 获取当前页面的开始时间(考虑多页合并)
currentPageStartTime() {
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() {
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;
},
......@@ -133,32 +133,32 @@ function initializeApp(data) {
// 获取当前页面的所有矩形框数据(合并多页)
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;
}
// if (startTime === null || startTime === undefined) {
// startTime = page.start;
// }
// if (endTime === null || endTime === undefined) {
// endTime = page.end;
// }
group.group.forEach((textGroup, textIndex) => {
if (textGroup && textGroup.rect) {
const rectWithData = {
......@@ -178,7 +178,7 @@ function initializeApp(data) {
});
});
});
return allRects;
},
......@@ -186,19 +186,19 @@ function initializeApp(data) {
// 获取当前页面的所有单词数据(合并多页)
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 => {
......@@ -229,12 +229,12 @@ function initializeApp(data) {
});
});
});
return allWords;
}
},
mounted() {
mounted() {
// 根据 staging 字符串,判断是否为 staging 环境
const isStaging = window.location.href.includes('staging');
const isLocalhost = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
......@@ -249,10 +249,10 @@ function initializeApp(data) {
console.log('Vue组件已挂载,数据:', this.bookData);
this.initAudio();
this.loadCurrentPage();
// 监听窗口大小变化,重新计算高亮位置
window.addEventListener('resize', this.onWindowResize);
// 监听窗口大小变化,调整气泡框位置
window.addEventListener('resize', this.adjustBubblePosition);
},
......@@ -346,7 +346,7 @@ function initializeApp(data) {
const pageIndices = this.continuity[i].split(',').map(index => parseInt(index.trim()));
const startTime = this.getPageStartTime(pageIndices);
const endTime = this.getPageEndTime(pageIndices);
if (currentTime >= startTime && currentTime <= endTime) {
if (this.currentPageIndex !== i) {
console.log('切换到页面:', i, '时间:', currentTime);
......@@ -361,36 +361,36 @@ 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;
},
......@@ -410,29 +410,18 @@ function initializeApp(data) {
// 使用计算属性获取所有矩形框和单词数据
const allRects = this.currentPageRects;
const allWords = this.currentPageWords;
// 遍历所有矩形框,检查时间
allRects.forEach(rect => {
if (currentTime >= rect._start && currentTime <= rect._end) {
if (rect._start != null && rect._end != null && currentTime >= rect._start && currentTime <= rect._end) {
newRects.push(rect);
}
});
// 遍历所有单词,检查时间
allWords.forEach(word => {
let wordStart = word.start;
let wordEnd = word.end;
// 为没有时间信息的单词设置默认时间
if (wordStart === null || wordStart === undefined) {
wordStart = word._start || 0;
}
if (wordEnd === null || wordEnd === undefined) {
wordEnd = word._end || 0;
}
// 如果当前时间已经过了这个单词的开始时间,就保持高亮直到切换页面
if (currentTime >= wordStart) {
if (word.start != null && currentTime >= word.start) {
newWords.push(word);
}
});
......@@ -485,10 +474,10 @@ function initializeApp(data) {
// 检查是否所有图片都加载完成
const allImages = document.querySelectorAll('.page-image');
const loadedImages = document.querySelectorAll('.page-image[src]');
if (allImages.length === loadedImages.length) {
console.log('所有图片加载完成');
// 如果处于点读模式,显示所有矩形框
if (this.isPointReadMode) {
this.showAllRects();
......@@ -518,7 +507,7 @@ function initializeApp(data) {
const pageIndex = rect._pageIndex; // 使用当前页面中的索引,而不是原始页面索引
const imgElements = document.querySelectorAll('.page-image');
const targetImg = imgElements[pageIndex] || imgElements[0];
if (!targetImg) return {};
const imgRect = targetImg.getBoundingClientRect();
......@@ -560,7 +549,7 @@ function initializeApp(data) {
const pageIndex = word._pageIndex; // 使用当前页面中的索引,而不是原始页面索引
const imgElements = document.querySelectorAll('.page-image');
const targetImg = imgElements[pageIndex] || imgElements[0];
if (!targetImg) return {};
const imgRect = targetImg.getBoundingClientRect();
......@@ -782,7 +771,7 @@ function initializeApp(data) {
// 如果没有时间信息,使用页面级别的时间
let startTime = sentenceStart;
let endTime = sentenceEnd;
if (!hasTimeInfo) {
const page = this.currentPages[pageIndex];
if (page) {
......@@ -950,71 +939,71 @@ function initializeApp(data) {
this.requestTranslation(word);
},
// 计算气泡框位置
// 计算气泡框位置
calculateBubblePosition(wordStyle) {
const imgElement = document.querySelector('.page-image');
if (!imgElement) return {};
const imgRect = imgElement.getBoundingClientRect();
const containerRect = imgElement.parentElement.getBoundingClientRect();
// 计算单词在容器中的位置
const wordLeft = parseFloat(wordStyle.left);
const wordTop = parseFloat(wordStyle.top);
const wordWidth = parseFloat(wordStyle.width);
const wordHeight = parseFloat(wordStyle.height);
// 获取屏幕尺寸
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
// 动态计算气泡框尺寸
let bubbleWidth = 600;
let bubbleHeight = 400;
// 如果屏幕太小,调整气泡框尺寸
if (screenWidth < 800) {
bubbleWidth = Math.min(600, screenWidth - 40);
bubbleHeight = Math.min(400, screenHeight - 80);
}
// 尝试多个位置策略
let bubbleLeft, bubbleTop;
// 策略1:优先在单词右侧
bubbleLeft = wordLeft + wordWidth + 10;
bubbleTop = wordTop - 20;
// 如果右侧空间不够,尝试左侧
if (bubbleLeft + bubbleWidth > containerRect.width - 20) {
bubbleLeft = wordLeft - bubbleWidth - 10;
}
// 如果左侧也不够,尝试居中
if (bubbleLeft < 20) {
bubbleLeft = Math.max(20, (containerRect.width - bubbleWidth) / 2);
}
// 垂直位置调整 - 考虑动态高度
const maxAvailableHeight = containerRect.height - 40; // 留出边距
const preferredTop = wordTop - 20;
// 如果顶部空间不够,尝试底部
if (preferredTop < 20) {
bubbleTop = wordTop + wordHeight + 10;
} else {
bubbleTop = preferredTop;
}
// 如果底部空间也不够,调整到可见区域
if (bubbleTop + bubbleHeight > containerRect.height - 20) {
bubbleTop = Math.max(20, containerRect.height - bubbleHeight - 20);
}
// 最终边界检查
bubbleLeft = Math.max(20, Math.min(bubbleLeft, containerRect.width - bubbleWidth - 20));
bubbleTop = Math.max(20, Math.min(bubbleTop, containerRect.height - bubbleHeight - 20));
// 调试信息
console.log('气泡框位置计算:', {
wordLeft, wordTop, wordWidth, wordHeight,
......@@ -1023,7 +1012,7 @@ function initializeApp(data) {
screenWidth, screenHeight,
maxAvailableHeight
});
return {
position: 'absolute',
left: bubbleLeft + 'px',
......@@ -1031,44 +1020,44 @@ function initializeApp(data) {
zIndex: 1000
};
},
// 动态调整气泡框位置
adjustBubblePosition() {
if (!this.translateBubble.show) return;
const bubbleElement = document.querySelector('.translate-bubble');
if (!bubbleElement) return;
const bubbleRect = bubbleElement.getBoundingClientRect();
const containerRect = bubbleElement.parentElement.getBoundingClientRect();
// 检查是否需要调整位置
let needsAdjustment = false;
let newTop = parseFloat(this.translateBubble.style.top);
// 检查底部是否超出
if (bubbleRect.bottom > containerRect.height - 20) {
newTop = containerRect.height - bubbleRect.height - 20;
needsAdjustment = true;
}
// 检查顶部是否超出
if (bubbleRect.top < 20) {
newTop = 20;
needsAdjustment = true;
}
// 检查右侧是否超出
if (bubbleRect.right > containerRect.width - 20) {
const newLeft = containerRect.width - bubbleRect.width - 20;
this.translateBubble.style.left = newLeft + 'px';
}
// 检查左侧是否超出
if (bubbleRect.left < 20) {
this.translateBubble.style.left = '20px';
}
// 应用垂直位置调整
if (needsAdjustment) {
this.translateBubble.style.top = newTop + 'px';
......@@ -1113,40 +1102,40 @@ function initializeApp(data) {
this.requestStreamingTranslation(wordText, sentence, groupsIndex);
},
// 真实流式翻译API调用
// 真实流式翻译API调用
async requestStreamingTranslation(word, sentence, pos) {
try {
// 重置翻译结果
this.translateBubble.result = '正在翻译...';
this.translateBubble.parsedData = null;
// 滚动到底部
this.scrollToBottom();
const requestUrl = this.AI_SERVER_URL + '/open/api/ai/dpe/v1/super/translatetoc';
const requestData = {
word: word,
sentence: sentence,
pos: pos
};
await new Promise((resolve, reject) => {
// 使用 XMLHttpRequest 来处理流式响应
const xhr = new XMLHttpRequest();
xhr.open('POST', requestUrl, true);
xhr.setRequestHeader('Content-Type', 'application/json');
let lastResponseLength = 0;
let responseText = '';
xhr.onprogress = (event) => {
if (xhr.readyState === 3 || xhr.readyState === 4) {
const newData = xhr.responseText.substring(lastResponseLength);
lastResponseLength = xhr.responseText.length;
if (newData) {
responseText += newData;
// 尝试解析JSON数据
try {
const parsedData = JSON.parse(responseText);
......@@ -1156,7 +1145,7 @@ function initializeApp(data) {
// 如果JSON不完整,继续显示原始文本
this.translateBubble.result = responseText;
}
// 每次更新后滚动到底部并调整位置
this.$nextTick(() => {
setTimeout(() => {
......@@ -1167,7 +1156,7 @@ function initializeApp(data) {
}
}
};
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
// 翻译完成,尝试最终解析
......@@ -1179,7 +1168,7 @@ function initializeApp(data) {
console.error('JSON解析失败:', e);
this.translateBubble.result = responseText;
}
this.scrollToBottom();
this.adjustBubblePosition(); // 最终位置调整
console.log('翻译完成:', responseText);
......@@ -1191,23 +1180,23 @@ function initializeApp(data) {
reject(error);
}
};
xhr.onerror = () => {
const error = new Error('Network error');
console.error('翻译网络错误:', error);
this.translateBubble.result = '翻译失败: 网络错误';
reject(error);
};
xhr.send(JSON.stringify(requestData));
});
} catch (error) {
console.error('翻译失败:', error);
this.translateBubble.result = `翻译失败: ${error.message}`;
}
},
// 滚动到底部的辅助方法
scrollToBottom() {
console.log('scrollToBottom 被调用');
......@@ -1233,26 +1222,26 @@ function initializeApp(data) {
});
}
};
// 立即滚动
scrollToBottom();
// 使用 requestAnimationFrame
requestAnimationFrame(scrollToBottom);
// 使用 setTimeout
setTimeout(scrollToBottom, 0);
// 再次确保滚动
setTimeout(scrollToBottom, 10);
setTimeout(scrollToBottom, 50);
} else {
console.log('未找到滚动容器 .bubble-content');
}
});
},
// 测试滚动功能
testScroll() {
console.log('测试滚动功能');
......@@ -1266,10 +1255,10 @@ function initializeApp(data) {
clientHeight: bubbleContent.clientHeight,
offsetHeight: bubbleContent.offsetHeight
});
// 手动设置一些测试内容
this.translateBubble.result = '测试内容\n'.repeat(50);
// 等待内容更新后再滚动
this.$nextTick(() => {
setTimeout(() => {
......
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