Commit 1e5ec877 authored by liujiangnan's avatar liujiangnan

feat: 定位

parent cc0374ee
...@@ -153,28 +153,35 @@ html, body { ...@@ -153,28 +153,35 @@ html, body {
height: 100%; height: 100%;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
position: relative;
background: white;
} }
#waveform { #waveform {
flex: 1; flex: 1;
min-height: 120px; min-height: 120px;
position: relative;
background: white;
} }
#wave-timeline { #wave-timeline {
height: 30px; height: 30px;
margin-bottom: 5px; margin-bottom: 5px;
position: relative;
} }
.audio-controls { .audio-controls {
padding: 5px 0; padding: 5px 0;
display: flex; display: flex;
align-items: center; align-items: center;
position: relative;
z-index: 4;
} }
/* 分页器样式 */ /* 分页器样式 */
.pagination-container { .pagination-container {
position: fixed; position: fixed;
bottom: 200px; /* 音频控制器的高度 */ bottom: 200px;
left: 0; left: 0;
right: 0; right: 0;
z-index: 1000; z-index: 1000;
...@@ -182,6 +189,11 @@ html, body { ...@@ -182,6 +189,11 @@ html, body {
text-align: center; text-align: center;
background: rgba(255, 255, 255, 0.9); background: rgba(255, 255, 255, 0.9);
border-top: 1px solid #EBEEF5; border-top: 1px solid #EBEEF5;
pointer-events: none;
}
.pagination-container .el-pagination {
pointer-events: all;
} }
/* 矩形框样式 */ /* 矩形框样式 */
...@@ -295,12 +307,25 @@ html, body { ...@@ -295,12 +307,25 @@ html, body {
} }
.wavesurfer-region { .wavesurfer-region {
z-index: 2 !important;
pointer-events: all !important;
}
/* 页面区间样式 */
.wavesurfer-region[data-id^="page-"] {
z-index: 3 !important; z-index: 3 !important;
background: rgba(255, 192, 203, 0.3) !important;
border: 2px solid rgba(255, 192, 203, 0.8) !important;
pointer-events: all !important;
}
/* 组区间样式 */
.wavesurfer-region:not([data-id^="page-"]) {
z-index: 1 !important;
} }
.wavesurfer-handle { .wavesurfer-handle {
background-color: #409EFF !important; display: none !important;
width: 4px !important;
} }
.app{ .app{
......
...@@ -72,6 +72,30 @@ new Vue({ ...@@ -72,6 +72,30 @@ new Vue({
this.audioPoints = data.audio_points; this.audioPoints = data.audio_points;
this.pages = data.pages; this.pages = data.pages;
// 计算每个页面的时间范围
this.pages.forEach(page => {
if (page.groups && page.groups.length > 0) {
// 找出所有组的最早开始时间和最晚结束时间
const times = page.groups.reduce((acc, group) => {
if (group.start !== null && group.start !== undefined) {
acc.starts.push(group.start);
}
if (group.end !== null && group.end !== undefined) {
acc.ends.push(group.end);
}
return acc;
}, { starts: [], ends: [] });
if (times.starts.length > 0) {
page.start = Math.min(...times.starts);
}
if (times.ends.length > 0) {
page.end = Math.max(...times.ends);
}
}
});
this.currentPageIndex = 1; this.currentPageIndex = 1;
this.currentPage = this.pages[0]; this.currentPage = this.pages[0];
...@@ -167,8 +191,11 @@ new Vue({ ...@@ -167,8 +191,11 @@ new Vue({
autoCenter: true, // 自动居中 autoCenter: true, // 自动居中
cursorWidth: 2, // 光标宽度 cursorWidth: 2, // 光标宽度
interact: true, // 允许交互 interact: true, // 允许交互
backend: 'MediaElement', // 使用 MediaElement 后端代替默认的 WebAudio
plugins: [ plugins: [
WaveSurfer.regions.create(), WaveSurfer.regions.create({
dragSelection: false // 禁用拖动选择,避免与我们的区域交互冲突
}),
WaveSurfer.timeline.create({ WaveSurfer.timeline.create({
container: '#wave-timeline', container: '#wave-timeline',
height: 20, height: 20,
...@@ -184,17 +211,17 @@ new Vue({ ...@@ -184,17 +211,17 @@ new Vue({
// 监听事件 // 监听事件
this.wavesurfer.on('ready', () => { this.wavesurfer.on('ready', () => {
this.audioLoading = false; this.audioLoading = false;
console.log('WaveSurfer ready');
// 添加一个小延时确保 WaveSurfer 完全准备好
setTimeout(() => {
// 音频加载完成后,创建区域并显示第一页 // 音频加载完成后,创建区域并显示第一页
this.createAllAudioRegions(); this.createAllAudioRegions();
// 重置缩放级别为1,显示完整波形 // 重置缩放级别为1,显示完整波形
this.zoomLevel = 1; this.zoomLevel = 1;
this.wavesurfer.zoom(1); this.wavesurfer.zoom(1);
}, 100);
}); });
// 添加音频处理事件监听
this.wavesurfer.on('audioprocess', this.handleAudioProcess);
// 添加点击波形定位事件 // 添加点击波形定位事件
this.wavesurfer.on('click', (e, progress) => { this.wavesurfer.on('click', (e, progress) => {
// 保存当前位置作为后续缩放的锚点 // 保存当前位置作为后续缩放的锚点
...@@ -207,8 +234,6 @@ new Vue({ ...@@ -207,8 +234,6 @@ new Vue({
this.audioLoading = false; this.audioLoading = false;
}); });
this.wavesurfer.on('region-updated', this.handleRegionUpdated);
// 加载音频 // 加载音频
this.audioLoading = true; this.audioLoading = true;
this.wavesurfer.load(this.audioPoints.audio_url); this.wavesurfer.load(this.audioPoints.audio_url);
...@@ -222,38 +247,244 @@ new Vue({ ...@@ -222,38 +247,244 @@ new Vue({
// 创建所有页面的音频区域 // 创建所有页面的音频区域
createAllAudioRegions() { createAllAudioRegions() {
if (!this.wavesurfer || !this.wavesurfer.isReady) { if (!this.wavesurfer || !this.wavesurfer.isReady) {
console.warn('WaveSurfer not ready yet'); console.error('WaveSurfer not ready for creating regions');
return; return;
} }
try {
// 清除现有的区域 // 清除现有的区域
this.wavesurfer.clearRegions(); this.wavesurfer.clearRegions();
this.audioRegions = {}; this.audioRegions = {};
// 为每个页面创建区域 // 先收集所有有效的页面时间范围
const pageRanges = [];
this.pages.forEach((page, pageIndex) => {
if (page.groups && page.groups.length > 0) {
const times = page.groups.reduce((acc, group) => {
if (typeof group.start === 'number' && !isNaN(group.start)) {
acc.starts.push(group.start);
}
if (typeof group.end === 'number' && !isNaN(group.end)) {
acc.ends.push(group.end);
}
return acc;
}, { starts: [], ends: [] });
if (times.starts.length > 0 && times.ends.length > 0) {
const start = Math.min(...times.starts);
const end = Math.max(...times.ends);
pageRanges.push({
pageIndex,
start,
end
});
}
}
});
// 按开始时间排序
pageRanges.sort((a, b) => a.start - b.start);
// 创建页面区间,确保不重叠
pageRanges.forEach((range, index) => {
const pageRegionId = `page-${range.pageIndex}`;
try {
// 创建区域
const region = this.wavesurfer.addRegion({
id: pageRegionId,
start: range.start,
end: range.end,
color: 'rgba(255, 192, 203, 0.3)',
drag: false,
resize: false,
attributes: {
label: `Page ${range.pageIndex + 1}`
}
});
if (region) {
this.audioRegions[pageRegionId] = region;
// 添加点击事件
region.element.addEventListener('click', (event) => {
// 阻止事件冒泡
event.stopPropagation();
event.preventDefault();
console.log('Page region clicked:', {
pageIndex: range.pageIndex,
pageRegionId,
currentPage: this.currentPageIndex,
timeRange: { start: range.start, end: range.end }
});
// 切换到对应的页面
this.currentPageIndex = range.pageIndex + 1;
this.currentPage = this.pages[range.pageIndex];
console.log('Page switched to:', {
newPageIndex: this.currentPageIndex,
newPage: this.currentPage
});
// 取消当前选中的组
this.selectedGroup = null;
this.currentGroup = null;
// 将区域滚动到视图中心
this.$nextTick(() => {
const center = (range.start + range.end) / 2;
const duration = this.wavesurfer.getDuration();
// 定位到中心位置
this.wavesurfer.seekTo(center / duration);
// 更新所有区域的样式
Object.keys(this.audioRegions).forEach(regionId => {
// 跳过页面区间
if (regionId.startsWith('page-')) return;
const [rPageIndex, groupIndex] = regionId.split('-').map(Number);
const region = this.audioRegions[regionId];
const isCurrentPage = rPageIndex === range.pageIndex;
if (region) {
region.update({
color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)',
drag: isCurrentPage,
resize: isCurrentPage
});
}
});
console.log('Region styles updated');
});
});
// 验证区域元素是否存在
console.log('Page region created:', {
pageIndex: range.pageIndex,
pageRegionId,
hasElement: !!region.element,
element: region.element
});
} else {
console.error(`Failed to create page region ${pageRegionId}`);
}
} catch (err) {
console.error(`Error creating page region ${pageRegionId}:`, err);
}
});
// 创建组区间
this.pages.forEach((page, pageIndex) => { this.pages.forEach((page, pageIndex) => {
if (page.groups) { if (page.groups) {
page.groups.forEach((group, groupIndex) => { page.groups.forEach((group, groupIndex) => {
if (group.start !== null && group.end !== null) { if (typeof group.start === 'number' && !isNaN(group.start) &&
const regionId = `${pageIndex}-${groupIndex}`; typeof group.end === 'number' && !isNaN(group.end)) {
const groupRegionId = `${pageIndex}-${groupIndex}`;
const isCurrentPage = pageIndex === this.currentPageIndex - 1; const isCurrentPage = pageIndex === this.currentPageIndex - 1;
try { try {
this.audioRegions[regionId] = this.wavesurfer.addRegion({ const region = this.wavesurfer.addRegion({
id: regionId, id: groupRegionId,
start: group.start, start: group.start,
end: group.end, end: group.end,
color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)', color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)',
drag: isCurrentPage, drag: isCurrentPage,
resize: isCurrentPage resize: isCurrentPage
}); });
if (region) {
this.audioRegions[groupRegionId] = region;
} else {
console.error(`Failed to create group region ${groupRegionId}`);
}
} catch (err) { } catch (err) {
console.error(`Error creating region ${regionId}:`, err); console.error(`Error creating group region ${groupRegionId}:`, err);
} }
} }
}); });
} }
}); });
} catch (err) {
console.error('Error in createAllAudioRegions:', err);
}
},
// 处理页面区间点击
handlePageRegionClick(pageIndex) {
// 如果点击的不是当前页,切换到该页
if (pageIndex !== this.currentPageIndex - 1) {
// 更新页面索引
this.currentPageIndex = pageIndex + 1;
this.currentPage = this.pages[pageIndex];
// 取消当前选中的组
this.selectedGroup = null;
this.currentGroup = null;
// 更新所有区域的样式
Object.keys(this.audioRegions).forEach(regionId => {
// 跳过页面区间
if (regionId.startsWith('page-')) return;
const [rPageIndex, groupIndex] = regionId.split('-').map(Number);
const region = this.audioRegions[regionId];
const isCurrentPage = rPageIndex === pageIndex;
if (region) {
region.update({
color: isCurrentPage ? 'rgba(64, 158, 255, 0.2)' : 'rgba(144, 147, 153, 0.1)',
drag: isCurrentPage,
resize: isCurrentPage
});
}
});
}
// 调整缩放以显示整个页面区间
const page = this.pages[pageIndex];
if (page && page.groups && page.groups.length > 0) {
// 找出页面的时间范围
const times = page.groups.reduce((acc, group) => {
if (typeof group.start === 'number' && !isNaN(group.start)) {
acc.starts.push(group.start);
}
if (typeof group.end === 'number' && !isNaN(group.end)) {
acc.ends.push(group.end);
}
return acc;
}, { starts: [], ends: [] });
if (times.starts.length > 0 && times.ends.length > 0) {
const pageStart = Math.min(...times.starts);
const pageEnd = Math.max(...times.ends);
const duration = pageEnd - pageStart;
const padding = duration * 0.1; // 添加10%的padding
// 计算合适的缩放级别
const zoomLevel = this.calculateZoomLevel(pageStart - padding, pageEnd + padding);
this.zoomLevel = zoomLevel;
this.wavesurfer.zoom(zoomLevel);
// 将区域滚动到视图中心
this.$nextTick(() => {
const center = (pageStart + pageEnd) / 2;
const duration = this.wavesurfer.getDuration();
// 先定位到中心位置
this.wavesurfer.seekTo(center / duration);
// 确保波形图滚动到正确位置
const scrollWidth = this.wavesurfer.drawer.wrapper.scrollWidth;
const containerWidth = this.wavesurfer.drawer.wrapper.clientWidth;
const scrollPosition = Math.max(0, (center / duration * scrollWidth) - (containerWidth / 2));
this.wavesurfer.drawer.wrapper.scrollLeft = scrollPosition;
});
}
}
}, },
// 处理音频区域更新 // 处理音频区域更新
...@@ -364,8 +595,19 @@ new Vue({ ...@@ -364,8 +595,19 @@ new Vue({
// 更新所有区域的样式 // 更新所有区域的样式
Object.keys(this.audioRegions).forEach(regionId => { Object.keys(this.audioRegions).forEach(regionId => {
const [pageIndex, groupIndex] = regionId.split('-').map(Number);
const region = this.audioRegions[regionId]; const region = this.audioRegions[regionId];
if (!region) return;
// 如果是页面区间,设置为默认颜色
if (regionId.startsWith('page-')) {
region.update({
color: 'rgba(255, 192, 203, 0.3)'
});
return;
}
// 处理组区间
const [pageIndex, groupIndex] = regionId.split('-').map(Number);
const isCurrentPage = pageIndex === index; const isCurrentPage = pageIndex === index;
region.update({ region.update({
...@@ -375,12 +617,25 @@ new Vue({ ...@@ -375,12 +617,25 @@ new Vue({
}); });
}); });
// 定位到当前页面的时间范围,但不自动缩放 // 定位到当前页面的时间范围(如果有)
if (this.currentPage) { if (this.currentPage && this.currentPage.groups && this.currentPage.groups.length > 0) {
const pageGroups = this.currentPage.groups || []; // 找出所有组的时间范围
if (pageGroups.length > 0) { const times = this.currentPage.groups.reduce((acc, group) => {
const pageStart = Math.min(...pageGroups.map(g => g.start)); if (typeof group.start === 'number' && !isNaN(group.start)) {
this.wavesurfer.seekTo(pageStart / this.wavesurfer.getDuration()); acc.starts.push(group.start);
}
if (typeof group.end === 'number' && !isNaN(group.end)) {
acc.ends.push(group.end);
}
return acc;
}, { starts: [], ends: [] });
// 如果有有效的时间范围,定位到中心位置
if (times.starts.length > 0 && times.ends.length > 0) {
const pageStart = Math.min(...times.starts);
const pageEnd = Math.max(...times.ends);
const center = (pageStart + pageEnd) / 2;
this.wavesurfer.seekTo(center / this.wavesurfer.getDuration());
} }
} }
} catch (err) { } catch (err) {
...@@ -442,11 +697,6 @@ new Vue({ ...@@ -442,11 +697,6 @@ new Vue({
this.imageHeight = finalHeight; this.imageHeight = finalHeight;
this.imageRect = img.getBoundingClientRect(); this.imageRect = img.getBoundingClientRect();
this.imageLoaded = true; this.imageLoaded = true;
console.log('Image loaded:', {
container: { width: containerWidth, height: containerHeight, ratio: containerRatio },
image: { width: finalWidth, height: finalHeight, ratio: imageRatio }
});
}, },
// 获取矩形样式 // 获取矩形样式
...@@ -465,12 +715,6 @@ new Vue({ ...@@ -465,12 +715,6 @@ new Vue({
const actualWidth = width * this.imageWidth; const actualWidth = width * this.imageWidth;
const actualHeight = height * this.imageHeight; const actualHeight = height * this.imageHeight;
console.log('Rectangle position:', {
normalized: { x, y, width, height },
actual: { x: actualX, y: actualY, width: actualWidth, height: actualHeight },
image: { width: this.imageWidth, height: this.imageHeight }
});
return { return {
position: 'absolute', position: 'absolute',
left: actualX + 'px', left: actualX + 'px',
...@@ -522,14 +766,6 @@ new Vue({ ...@@ -522,14 +766,6 @@ new Vue({
this.selectedItemIndex = itemIndex; this.selectedItemIndex = itemIndex;
this.originalRect = this.currentGroup.group[itemIndex].rect ? { ...this.currentGroup.group[itemIndex].rect } : null; this.originalRect = this.currentGroup.group[itemIndex].rect ? { ...this.currentGroup.group[itemIndex].rect } : null;
console.log('Start resizing:', {
startX: this.dragStartX,
startY: this.dragStartY,
originalRect: this.originalRect,
groupIndex,
itemIndex
});
event.preventDefault(); event.preventDefault();
}, },
...@@ -549,13 +785,6 @@ new Vue({ ...@@ -549,13 +785,6 @@ new Vue({
currentRect.x = Math.max(0, Math.min(1 - currentRect.width, this.originalRect.x + deltaX)); currentRect.x = Math.max(0, Math.min(1 - currentRect.width, this.originalRect.x + deltaX));
currentRect.y = Math.max(0, Math.min(1 - currentRect.height, this.originalRect.y + deltaY)); currentRect.y = Math.max(0, Math.min(1 - currentRect.height, this.originalRect.y + deltaY));
console.log('Dragging:', {
x: currentRect.x,
y: currentRect.y,
deltaX,
deltaY,
itemIndex: this.selectedItemIndex
});
} else if (this.isResizing) { } else if (this.isResizing) {
// 计算最小宽度和高度(5像素对应的相对值) // 计算最小宽度和高度(5像素对应的相对值)
const minWidth = 5 / this.imageWidth; const minWidth = 5 / this.imageWidth;
...@@ -565,15 +794,6 @@ new Vue({ ...@@ -565,15 +794,6 @@ new Vue({
currentRect.width = Math.max(minWidth, Math.min(1 - currentRect.x, this.originalRect.width + deltaX)); currentRect.width = Math.max(minWidth, Math.min(1 - currentRect.x, this.originalRect.width + deltaX));
currentRect.height = Math.max(minHeight, Math.min(1 - currentRect.y, this.originalRect.height + deltaY)); currentRect.height = Math.max(minHeight, Math.min(1 - currentRect.y, this.originalRect.height + deltaY));
console.log('Resizing:', {
width: currentRect.width,
height: currentRect.height,
minWidth,
minHeight,
deltaX,
deltaY,
itemIndex: this.selectedItemIndex
});
} }
}, },
...@@ -821,17 +1041,6 @@ new Vue({ ...@@ -821,17 +1041,6 @@ new Vue({
this.fullscreenImageWidth = finalWidth; this.fullscreenImageWidth = finalWidth;
this.fullscreenImageHeight = finalHeight; this.fullscreenImageHeight = finalHeight;
this.fullscreenImageRect = img.getBoundingClientRect(); this.fullscreenImageRect = img.getBoundingClientRect();
console.log('Fullscreen image loaded:', {
container: { width: containerWidth, height: containerHeight, ratio: containerRatio },
image: {
naturalWidth: imgNaturalWidth,
naturalHeight: imgNaturalHeight,
finalWidth,
finalHeight,
ratio: imageRatio
}
});
}, },
// 获取全屏模式下矩形框的样式 // 获取全屏模式下矩形框的样式
......
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