feat:移除无用的UNO封装代码

This commit is contained in:
PingChuan
2025-11-25 18:58:58 +08:00
parent 08ebcf1935
commit ba83796a44
6 changed files with 116 additions and 235 deletions
+71 -72
View File
@@ -9,23 +9,22 @@
* @encoding UTF-8
*/
import { RefObject, useCallback, useEffect, useState, useMemo } from 'react';
import { useFetcher } from '@remix-run/react';
import { toastService } from '../ui/Toast';
import type { CollaboraConfig } from './types';
import {
unoSearchText,
unoReplaceText,
unoHighlightText,
unoRemoveHighlight,
unoEscape,
unoScrollToTop,
unoSave,
unoZoomPlus,
unoZoomMinus,
unoSetZoom,
} from './lib';
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';
// ==================== 1. 配置加载 ====================
@@ -142,32 +141,32 @@ export function useCollaboraUnoCommands(iframeRef: RefObject<HTMLIFrameElement>)
[iframeRef]
);
/**
* 定位文本(搜索 + 立即取消选中)
* 用于"只看不改"的场景,避免蓝色选中背景
*/
const locateText = useCallback(
async (text: string) => {
if (!iframeRef.current?.contentWindow) {
console.warn('[UNO] iframe 不可用');
return;
}
// /**
// * 定位文本(搜索 + 立即取消选中)
// * 用于"只看不改"的场景,避免蓝色选中背景
// */
// const locateText = useCallback(
// async (text: string) => {
// if (!iframeRef.current?.contentWindow) {
// console.warn('[UNO] iframe 不可用');
// return;
// }
console.log(`[UNO] 定位文本(无选中): "${text}"`);
// console.log(`[UNO] 定位文本(无选中): "${text}"`);
// 1. 执行搜索(滚动到目标并选中)
await searchText(text);
// // 1. 执行搜索(滚动到目标并选中)
// await searchText(text);
// 2. 等待渲染完成
await new Promise((resolve) => setTimeout(resolve, 50));
// // 2. 等待渲染完成
// await new Promise((resolve) => setTimeout(resolve, 50));
// 3. 取消选中(去除蓝色背景,保留视图位置)
unoEscape(iframeRef.current.contentWindow);
// // 3. 取消选中(去除蓝色背景,保留视图位置)
// unoEscape(iframeRef.current.contentWindow);
console.log(`[UNO] 定位完成,已取消选中`);
},
[searchText, iframeRef]
);
// console.log(`[UNO] 定位完成,已取消选中`);
// },
// [searchText, iframeRef]
// );
/**
* 替换文本(ReplaceAll
@@ -185,37 +184,37 @@ export function useCollaboraUnoCommands(iframeRef: RefObject<HTMLIFrameElement>)
[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 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]
);
// /**
// * 移除高亮
// */
// 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
@@ -309,10 +308,10 @@ export function useCollaboraUnoCommands(iframeRef: RefObject<HTMLIFrameElement>)
return useMemo(
() => ({
searchText,
locateText,
// locateText,
replaceText,
highlightText,
removeHighlight,
// highlightText,
// removeHighlight,
escapeSelection,
scrollToTop,
saveDocument,
@@ -322,10 +321,10 @@ export function useCollaboraUnoCommands(iframeRef: RefObject<HTMLIFrameElement>)
}),
[
searchText,
locateText,
// locateText,
replaceText,
highlightText,
removeHighlight,
// highlightText,
// removeHighlight,
escapeSelection,
scrollToTop,
saveDocument,
-64
View File
@@ -1,64 +0,0 @@
/**
* Collabora 高亮功能模块
*
* @encoding UTF-8
*/
import { sendUnoCommand } from '../Uno';
/**
* 高亮文本
* @param iframeWindow - iframe 的 contentWindow
* @param text - 要高亮的文本
* @param color - 高亮颜色,默认 16776960 = 黄色
*/
export function unoHighlightText(
iframeWindow: Window,
text: string,
color: number = 16776960
): void {
// 1. 查找所有
sendUnoCommand(iframeWindow, '.uno:ExecuteSearch', {
'SearchItem.SearchString': { type: 'string', value: text },
'SearchItem.Command': { type: 'long', value: 1 }, // 1 = FindAll
'SearchItem.SearchFlags': { type: 'long', value: 0 },
'SearchItem.AlgorithmType': { type: 'short', value: 0 },
'SearchItem.Backward': { type: 'boolean', value: false },
'Quiet': { type: 'boolean', value: true },
});
// 2. 设置背景色
sendUnoCommand(iframeWindow, '.uno:BackColor', {
BackColor: { type: 'long', value: color },
});
}
/**
* 移除高亮
* @param iframeWindow - iframe 的 contentWindow
* @param text - 要移除高亮的文本
*/
export function unoRemoveHighlight(iframeWindow: Window, text: string): void {
// 1. 查找所有
sendUnoCommand(iframeWindow, '.uno:ExecuteSearch', {
'SearchItem.SearchString': { type: 'string', value: text },
'SearchItem.Command': { type: 'long', value: 1 }, // 1 = FindAll
'SearchItem.SearchFlags': { type: 'long', value: 0 },
'SearchItem.AlgorithmType': { type: 'short', value: 0 },
'SearchItem.Backward': { type: 'boolean', value: false },
'Quiet': { type: 'boolean', value: true },
});
// 2. 移除背景色 -1 = 无色
sendUnoCommand(iframeWindow, '.uno:BackColor', {
BackColor: { type: 'long', value: -1 },
});
}
/**
* 取消 - Escape
* @param iframeWindow - iframe 的 contentWindow
*/
export function unoEscape(iframeWindow: Window): void {
sendUnoCommand(iframeWindow, '.uno:Escape', {});
}
+12 -10
View File
@@ -4,22 +4,23 @@
* @encoding UTF-8
*/
// 搜索功能
export { unoSearchText } from './search';
// // 搜索功能
// export { unoSearchText } from './search';
// 替换功能
export { unoReplaceText } from './replace';
// // 替换功能
// export { unoReplaceText } from './replace';
// 高亮功能
export { unoHighlightText, unoRemoveHighlight, unoEscape } from './highlight';
// 高亮,已经封装好了
export { switchHighlight } from './Highlightselecttext';
// 导航/跳转功能
export {
unoScrollToTop,
unoScrollToTop
} from './navigation';
// 缩放功能
export { unoZoomPlus, unoZoomMinus, unoSetZoom } from './zoom';
// export { unoSetZoom, unoZoomMinus, unoZoomPlus } from './zoom';
// 文档操作
export { unoSave } from './document';
@@ -27,11 +28,12 @@ export { unoSave } from './document';
// 页数信息
export {
requestPageInfo,
type PageInfo,
type PageInfo
} from './pageInfo';
// 自定义页面跳转功能
export {
customGotoPage,
type GotoPageResponse,
type GotoPageResponse
} from './gotoPage';
-29
View File
@@ -1,29 +0,0 @@
/**
* Collabora 替换功能模块
*
* @encoding UTF-8
*/
import { sendUnoCommand } from '../Uno';
/**
* 替换文本
* @param iframeWindow - iframe 的 contentWindow
* @param searchText - 要搜索的文本
* @param replaceText - 替换后的文本
*/
export function unoReplaceText(
iframeWindow: Window,
searchText: string,
replaceText: string
): void {
sendUnoCommand(iframeWindow, '.uno:ExecuteSearch', {
'SearchItem.SearchString': { type: 'string', value: searchText },
'SearchItem.ReplaceString': { type: 'string', value: replaceText },
'SearchItem.Command': { type: 'long', value: 3 }, // 3 = ReplaceAll
'SearchItem.AlgorithmType': { type: 'short', value: 0 },
'SearchItem.SearchFlags': { type: 'long', value: 0 },
'SearchItem.Backward': { type: 'boolean', value: false },
'Quiet': { type: 'boolean', value: true },
});
}
-27
View File
@@ -1,27 +0,0 @@
/**
* Collabora 搜索功能模块
*
* @encoding UTF-8
*/
import { sendUnoCommand } from '../Uno';
/**
* 搜索文本
* @param iframeWindow - iframe 的 contentWindow
* @param text - 要搜索的文本
*/
export function unoSearchText(iframeWindow: Window, text: string): void {
sendUnoCommand(iframeWindow, '.uno:ExecuteSearch', {
'SearchItem.SearchString': { type: 'string', value: text },
'SearchItem.Command': { type: 'long', value: 1 }, // 1 = Search Next (搜索下一个)
'SearchItem.Backward': { type: 'boolean', value: false },
'SearchItem.Pattern': { type: 'boolean', value: false },
'SearchItem.Content': { type: 'boolean', value: false },
'SearchItem.AsianOptions': { type: 'boolean', value: false },
'SearchItem.AlgorithmType': { type: 'short', value: 0 }, // 普通搜索
'SearchItem.SearchFlags': { type: 'long', value: 0 },
'SearchItem.Start': { type: 'boolean', value: true }, // 从头开始搜索
'SearchItem.Quiet': { type: 'boolean', value: true }, // 静默模式
});
}
+33 -33
View File
@@ -1,37 +1,37 @@
/**
* Collabora 缩放功能模块
*
* @encoding UTF-8
*/
// /**
// * Collabora 缩放功能模块
// *
// * @encoding UTF-8
// */
import { sendUnoCommand } from '../Uno';
// import { sendUnoCommand } from '../Uno';
/**
* 放大文档(固定步长)
* @param iframeWindow - iframe 的 contentWindow
*/
export function unoZoomPlus(iframeWindow: Window): void {
sendUnoCommand(iframeWindow, '.uno:ZoomPlus', {});
}
// /**
// * 放大文档(固定步长)
// * @param iframeWindow - iframe 的 contentWindow
// */
// export function unoZoomPlus(iframeWindow: Window): void {
// sendUnoCommand(iframeWindow, '.uno:ZoomPlus', {});
// }
/**
* 缩小文档(固定步长)
* @param iframeWindow - iframe 的 contentWindow
*/
export function unoZoomMinus(iframeWindow: Window): void {
sendUnoCommand(iframeWindow, '.uno:ZoomMinus', {});
}
// /**
// * 缩小文档(固定步长)
// * @param iframeWindow - iframe 的 contentWindow
// */
// export function unoZoomMinus(iframeWindow: Window): void {
// sendUnoCommand(iframeWindow, '.uno:ZoomMinus', {});
// }
/**
* 设置文档缩放比例
* @param iframeWindow - iframe 的 contentWindow
* @param percentage - 缩放百分比(例如:100 表示 100%)
*/
export function unoSetZoom(iframeWindow: Window, percentage: number): void {
sendUnoCommand(iframeWindow, '.uno:Zoom', {
Zoom: {
type: 'short',
value: percentage,
},
});
}
// /**
// * 设置文档缩放比例
// * @param iframeWindow - iframe 的 contentWindow
// * @param percentage - 缩放百分比(例如:100 表示 100%)
// */
// export function unoSetZoom(iframeWindow: Window, percentage: number): void {
// sendUnoCommand(iframeWindow, '.uno:Zoom', {
// Zoom: {
// type: 'short',
// value: percentage,
// },
// });
// }