Commit fac33607 authored by liujiangnan's avatar liujiangnan

fix

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