feat:完善Collabora组件实现高亮特定文本并跳转页面逻辑
This commit is contained in:
@@ -14,8 +14,9 @@ import { useRef, forwardRef, useImperativeHandle, useState } from 'react';
|
||||
import type { CollaboraViewerProps, CollaboraViewerHandle } from './types';
|
||||
import { useCollaboraConfig, useDocumentReady, useCollaboraUnoCommands } from './hooks';
|
||||
import { sendUnoCommand } from './Uno';
|
||||
import { switchHighlight } from './lib/Highlightselecttext';
|
||||
import { highlightText } from './lib/Highlightselecttext';
|
||||
import { clearHighlights } from './lib/ClearHighlight';
|
||||
import { unoScrollToTop, requestPageInfo, customGotoPage, type PageInfo, type GotoPageResponse } from './lib';
|
||||
|
||||
/**
|
||||
* Collabora 文档查看器组件
|
||||
@@ -34,13 +35,8 @@ export const CollaboraViewer = forwardRef<CollaboraViewerHandle, CollaboraViewer
|
||||
) {
|
||||
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||
|
||||
// 调试面板状态
|
||||
const [unoCmd, setUnoCmd] = useState('.uno:GoToStartOfDoc');
|
||||
const [unoArgs, setUnoArgs] = useState('{}');
|
||||
const [unoResult, setUnoResult] = useState<string | null>(null);
|
||||
|
||||
// 高亮测试面板状态
|
||||
const [highlightText, setHighlightText] = useState('');
|
||||
const [highlightTextInput, setHighlightTextInput] = useState('');
|
||||
const [highlightPage, setHighlightPage] = useState('');
|
||||
const [previousHighlightText, setPreviousHighlightText] = useState<string | null>(null);
|
||||
const [highlightResult, setHighlightResult] = useState<string | null>(null);
|
||||
@@ -49,6 +45,15 @@ export const CollaboraViewer = forwardRef<CollaboraViewerHandle, CollaboraViewer
|
||||
const [clearHighlightResult, setClearHighlightResult] = useState<string | null>(null);
|
||||
const [isClearing, setIsClearing] = useState(false);
|
||||
|
||||
// 页数信息测试状态
|
||||
const [pageInfoResult, setPageInfoResult] = useState<string | null>(null);
|
||||
const [isLoadingPageInfo, setIsLoadingPageInfo] = useState(false);
|
||||
|
||||
// 页面跳转测试状态
|
||||
const [gotoPageInput, setGotoPageInput] = useState('');
|
||||
const [gotoPageResult, setGotoPageResult] = useState<string | null>(null);
|
||||
const [isJumping, setIsJumping] = useState(false);
|
||||
|
||||
// 1. 加载 Collabora 配置
|
||||
const { config, loading, error } = useCollaboraConfig(fileId, mode, userId, userName);
|
||||
|
||||
@@ -134,7 +139,7 @@ export const CollaboraViewer = forwardRef<CollaboraViewerHandle, CollaboraViewer
|
||||
return;
|
||||
}
|
||||
|
||||
if (!highlightText || highlightText.trim() === '') {
|
||||
if (!highlightTextInput || highlightTextInput.trim() === '') {
|
||||
setHighlightResult('请输入要高亮的文本');
|
||||
return;
|
||||
}
|
||||
@@ -151,23 +156,114 @@ export const CollaboraViewer = forwardRef<CollaboraViewerHandle, CollaboraViewer
|
||||
return;
|
||||
}
|
||||
|
||||
await switchHighlight(
|
||||
await highlightText(
|
||||
iframeRef.current.contentWindow,
|
||||
previousHighlightText,
|
||||
highlightText.trim(),
|
||||
highlightTextInput.trim(),
|
||||
{ page }
|
||||
);
|
||||
|
||||
// 更新上一次高亮的文本
|
||||
setPreviousHighlightText(highlightText.trim());
|
||||
setPreviousHighlightText(highlightTextInput.trim());
|
||||
const pageInfo = page ? ` (第${page}页)` : '';
|
||||
setHighlightResult(`✓ 已切换高亮: ${highlightText.trim()}${pageInfo}`);
|
||||
setHighlightResult(`✓ 已切换高亮: ${highlightTextInput.trim()}${pageInfo}`);
|
||||
} catch (e) {
|
||||
console.error('切换高亮失败:', e);
|
||||
setHighlightResult(`切换失败: ${e instanceof Error ? e.message : '未知错误'}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 滚动到顶部处理函数
|
||||
const handleScrollToTop = async () => {
|
||||
if (!iframeRef.current?.contentWindow) {
|
||||
console.error('iframe 未就绪');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await unoScrollToTop(iframeRef.current.contentWindow);
|
||||
console.log('[CollaboraViewer] 已滚动到文档顶部');
|
||||
} catch (e) {
|
||||
console.error('滚动到顶部失败:', e);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取页数信息处理函数
|
||||
const handleGetPageInfo = async () => {
|
||||
if (!iframeRef.current?.contentWindow) {
|
||||
setPageInfoResult('iframe 未就绪');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoadingPageInfo(true);
|
||||
setPageInfoResult('正在获取页数信息...');
|
||||
|
||||
try {
|
||||
const info: PageInfo = await requestPageInfo(iframeRef.current.contentWindow);
|
||||
setPageInfoResult(
|
||||
`✓ 总页数: ${info.totalPages} | 当前页: ${info.currentPage + 1} (索引: ${info.currentPage})`
|
||||
);
|
||||
console.log('[CollaboraViewer] 页数信息:', info);
|
||||
|
||||
// 3秒后清除提示
|
||||
setTimeout(() => {
|
||||
setPageInfoResult(null);
|
||||
}, 3000);
|
||||
} catch (e) {
|
||||
console.error('获取页数信息失败:', e);
|
||||
setPageInfoResult(`✗ 获取失败: ${e instanceof Error ? e.message : '未知错误'}`);
|
||||
|
||||
// 5秒后清除错误提示
|
||||
setTimeout(() => {
|
||||
setPageInfoResult(null);
|
||||
}, 5000);
|
||||
} finally {
|
||||
setIsLoadingPageInfo(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 页面跳转处理函数
|
||||
const handleGotoPage = async () => {
|
||||
if (!iframeRef.current?.contentWindow) {
|
||||
setGotoPageResult('iframe 未就绪');
|
||||
return;
|
||||
}
|
||||
|
||||
const pageNumber = parseInt(gotoPageInput.trim(), 10);
|
||||
if (isNaN(pageNumber) || pageNumber < 1) {
|
||||
setGotoPageResult('请输入有效的页码 (大于0的整数)');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsJumping(true);
|
||||
setGotoPageResult(`正在跳转到第 ${pageNumber} 页...`);
|
||||
|
||||
try {
|
||||
const response: GotoPageResponse = await customGotoPage(
|
||||
iframeRef.current.contentWindow,
|
||||
pageNumber
|
||||
);
|
||||
setGotoPageResult(
|
||||
`✓ 已跳转到第 ${response.pageNumber} 页 (总页数: ${response.totalPages})`
|
||||
);
|
||||
console.log('[CollaboraViewer] 页面跳转成功:', response);
|
||||
|
||||
// 3秒后清除提示
|
||||
setTimeout(() => {
|
||||
setGotoPageResult(null);
|
||||
}, 3000);
|
||||
} catch (e) {
|
||||
console.error('页面跳转失败:', e);
|
||||
setGotoPageResult(`✗ 跳转失败: ${e instanceof Error ? e.message : '未知错误'}`);
|
||||
|
||||
// 5秒后清除错误提示
|
||||
setTimeout(() => {
|
||||
setGotoPageResult(null);
|
||||
}, 5000);
|
||||
} finally {
|
||||
setIsJumping(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 清除高亮处理函数
|
||||
const handleClearHighlights = async () => {
|
||||
if (!iframeRef.current?.contentWindow) {
|
||||
@@ -243,8 +339,8 @@ export const CollaboraViewer = forwardRef<CollaboraViewerHandle, CollaboraViewer
|
||||
<div className="absolute top-2 left-2 z-50 bg-white bg-opacity-90 px-3 py-2 rounded shadow flex items-center gap-2">
|
||||
<input
|
||||
className="px-3 py-1.5 border rounded text-sm w-64"
|
||||
value={highlightText}
|
||||
onChange={(e) => setHighlightText(e.target.value)}
|
||||
value={highlightTextInput}
|
||||
onChange={(e) => setHighlightTextInput(e.target.value)}
|
||||
placeholder="输入要高亮的文本 (如: 合同编号)"
|
||||
aria-label="高亮文本"
|
||||
onKeyPress={(e) => {
|
||||
|
||||
Reference in New Issue
Block a user