From ba83796a44f9e782a839072978cd7dd424213568 Mon Sep 17 00:00:00 2001 From: PingChuan <1259732256@qq.com> Date: Tue, 25 Nov 2025 18:58:58 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E7=A7=BB=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E7=9A=84UNO=E5=B0=81=E8=A3=85=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/collabora/hooks.ts | 143 +++++++++++----------- app/components/collabora/lib/highlight.ts | 64 ---------- app/components/collabora/lib/index.ts | 22 ++-- app/components/collabora/lib/replace.ts | 29 ----- app/components/collabora/lib/search.ts | 27 ---- app/components/collabora/lib/zoom.ts | 66 +++++----- 6 files changed, 116 insertions(+), 235 deletions(-) delete mode 100644 app/components/collabora/lib/highlight.ts delete mode 100644 app/components/collabora/lib/replace.ts delete mode 100644 app/components/collabora/lib/search.ts diff --git a/app/components/collabora/hooks.ts b/app/components/collabora/hooks.ts index f976a22..869243c 100644 --- a/app/components/collabora/hooks.ts +++ b/app/components/collabora/hooks.ts @@ -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) [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) [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) return useMemo( () => ({ searchText, - locateText, + // locateText, replaceText, - highlightText, - removeHighlight, + // highlightText, + // removeHighlight, escapeSelection, scrollToTop, saveDocument, @@ -322,10 +321,10 @@ export function useCollaboraUnoCommands(iframeRef: RefObject) }), [ searchText, - locateText, + // locateText, replaceText, - highlightText, - removeHighlight, + // highlightText, + // removeHighlight, escapeSelection, scrollToTop, saveDocument, diff --git a/app/components/collabora/lib/highlight.ts b/app/components/collabora/lib/highlight.ts deleted file mode 100644 index 6ec1ca4..0000000 --- a/app/components/collabora/lib/highlight.ts +++ /dev/null @@ -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', {}); -} diff --git a/app/components/collabora/lib/index.ts b/app/components/collabora/lib/index.ts index 9e1215d..fa648bb 100644 --- a/app/components/collabora/lib/index.ts +++ b/app/components/collabora/lib/index.ts @@ -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'; + diff --git a/app/components/collabora/lib/replace.ts b/app/components/collabora/lib/replace.ts deleted file mode 100644 index e548c16..0000000 --- a/app/components/collabora/lib/replace.ts +++ /dev/null @@ -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 }, - }); -} diff --git a/app/components/collabora/lib/search.ts b/app/components/collabora/lib/search.ts deleted file mode 100644 index 3448c38..0000000 --- a/app/components/collabora/lib/search.ts +++ /dev/null @@ -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 }, // 静默模式 - }); -} diff --git a/app/components/collabora/lib/zoom.ts b/app/components/collabora/lib/zoom.ts index 35c41bb..354af0a 100644 --- a/app/components/collabora/lib/zoom.ts +++ b/app/components/collabora/lib/zoom.ts @@ -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, +// }, +// }); +// }