temp:备份代码

This commit is contained in:
PingChuan
2025-11-25 20:45:41 +08:00
parent ba83796a44
commit a5cf3bc948
4 changed files with 209 additions and 220 deletions
+2 -194
View File
@@ -14,15 +14,7 @@ import { RefObject, useCallback, useEffect, useMemo, useState } from 'react';
import { COLLABORA_URL } from '~/config/api-config';
import { toastService } from '../ui/Toast';
import {
unoEscape,
// unoHighlightText,
unoReplaceText,
unoSave,
unoScrollToTop,
unoSearchText,
unoSetZoom,
unoZoomMinus,
unoZoomPlus,
} from './lib';
import type { CollaboraConfig } from './types';
@@ -125,111 +117,6 @@ export function useDocumentReady(iframeRef: RefObject<HTMLIFrameElement>) {
* @returns UNO 命令方法集合
*/
export function useCollaboraUnoCommands(iframeRef: RefObject<HTMLIFrameElement>) {
/**
* 搜索文本(用于定位)
*/
const searchText = useCallback(
async (text: string) => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
console.log(`[UNO] 搜索文本: "${text}"`);
await unoSearchText(iframeRef.current.contentWindow, text);
await new Promise((resolve) => setTimeout(resolve, 100));
},
[iframeRef]
);
// /**
// * 定位文本(搜索 + 立即取消选中)
// * 用于"只看不改"的场景,避免蓝色选中背景
// */
// const locateText = useCallback(
// async (text: string) => {
// if (!iframeRef.current?.contentWindow) {
// console.warn('[UNO] iframe 不可用');
// return;
// }
// console.log(`[UNO] 定位文本(无选中): "${text}"`);
// // 1. 执行搜索(滚动到目标并选中)
// await searchText(text);
// // 2. 等待渲染完成
// await new Promise((resolve) => setTimeout(resolve, 50));
// // 3. 取消选中(去除蓝色背景,保留视图位置)
// unoEscape(iframeRef.current.contentWindow);
// console.log(`[UNO] 定位完成,已取消选中`);
// },
// [searchText, iframeRef]
// );
/**
* 替换文本(ReplaceAll
*/
const replaceText = useCallback(
async (searchText: string, replaceText: string) => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
console.log(`[UNO] 替换文本: "${searchText}" -> "${replaceText}"`);
await unoReplaceText(iframeRef.current.contentWindow, searchText, replaceText);
await new Promise((resolve) => setTimeout(resolve, 200));
},
[iframeRef]
);
// /**
// * 高亮文本
// */
// const highlightText = useCallback(
// async (text: string, color?: number) => {
// if (!iframeRef.current?.contentWindow) {
// console.warn('[UNO] iframe 不可用');
// return;
// }
// console.log(`[UNO] 高亮文本: "${text}"`);
// await unoHighlightText(iframeRef.current.contentWindow, text, color);
// await new Promise((resolve) => setTimeout(resolve, 200));
// },
// [iframeRef]
// );
// /**
// * 移除高亮
// */
// const removeHighlight = useCallback(
// async (text: string) => {
// if (!iframeRef.current?.contentWindow) {
// console.warn('[UNO] iframe 不可用');
// return;
// }
// console.log(`[UNO] 移除高亮: "${text}"`);
// await unoRemoveHighlight(iframeRef.current.contentWindow, text);
// await new Promise((resolve) => setTimeout(resolve, 200));
// },
// [iframeRef]
// );
/**
* 取消选中(Escape
*/
const escapeSelection = useCallback(async () => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
console.log('[UNO] 取消选中');
await unoEscape(iframeRef.current.contentWindow);
await new Promise((resolve) => setTimeout(resolve, 50));
}, [iframeRef]);
/**
* 滚动到文档顶部
*/
@@ -244,93 +131,14 @@ export function useCollaboraUnoCommands(iframeRef: RefObject<HTMLIFrameElement>)
await new Promise((resolve) => setTimeout(resolve, 100));
}, [iframeRef]);
/**
* 保存文档
*/
const saveDocument = useCallback(async () => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
console.log('[UNO] 保存文档');
await unoSave(iframeRef.current.contentWindow);
await new Promise((resolve) => setTimeout(resolve, 1000));
}, [iframeRef]);
/**
* 放大文档
*/
const zoomIn = useCallback(async () => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
console.log('[UNO] 放大文档');
await unoZoomPlus(iframeRef.current.contentWindow);
await new Promise((resolve) => setTimeout(resolve, 100));
}, [iframeRef]);
/**
* 缩小文档
*/
const zoomOut = useCallback(async () => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
console.log('[UNO] 缩小文档');
await unoZoomMinus(iframeRef.current.contentWindow);
await new Promise((resolve) => setTimeout(resolve, 100));
}, [iframeRef]);
/**
* 设置缩放比例
* @param percentage - 缩放百分比 (例如:100 表示 100%)
*/
const setZoom = useCallback(
async (percentage: number) => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
console.log(`[UNO] 设置缩放比例: ${percentage}%`);
await unoSetZoom(iframeRef.current.contentWindow, percentage);
await new Promise((resolve) => setTimeout(resolve, 100));
},
[iframeRef]
);
return useMemo(
() => ({
searchText,
// locateText,
replaceText,
// highlightText,
// removeHighlight,
escapeSelection,
scrollToTop,
saveDocument,
zoomIn,
zoomOut,
setZoom,
scrollToTop
}),
[
searchText,
// locateText,
replaceText,
// highlightText,
// removeHighlight,
escapeSelection,
scrollToTop,
saveDocument,
zoomIn,
zoomOut,
setZoom,
scrollToTop
]
);
}