UNPKG

@mescius/dspdfviewer

Version:
213 lines (212 loc) 11.6 kB
import { ReplaceTextModel } from "../Models/ViewerTypes"; import PdfReportPlugin from "../plugin"; import { HighlightArgs, HighlightBehaviorArgs, ICustomHighlight, ITextHighlightManager } from "./types"; /** * Manages text highlights for different pages and handles their rendering. */ export declare class TextHighlightManager implements ITextHighlightManager { private plugin; constructor(plugin: PdfReportPlugin); /** * A map of highlights where each page index maps to an array of highlights for that page. * These highlights represent normal text highlights on the page. * * @type {Object<number, ICustomHighlight[]>} * @property {ICustomHighlight[]} [pageIndex] - The array of highlights for the corresponding page index. */ highlights: { [pageIndex: number]: ICustomHighlight[]; }; /** * A map of replaced highlights where each page index maps to an array of highlights for that page. * These highlights represent the replaced search result highlights, which can be undone. * * @type {Object<number, ICustomHighlight[]>} * @property {ICustomHighlight[]} [pageIndex] - The array of replaced highlights for the corresponding page index. */ replaceHighlights: { [pageIndex: number]: ICustomHighlight[]; }; /** * Adds a highlight to the specified page's replaced highlights map. * * @param {number} pageIndex - The index of the page where the highlight should be added. * @param {ICustomHighlight} highlight - The highlight object to add. * @param {HighlightBehaviorArgs} [args] - Optional behavior arguments, such as whether to skip repainting the text layer after adding the highlight. */ addReplaceHighlight(pageIndex: number, highlight: ICustomHighlight, args?: HighlightBehaviorArgs): { start: number; end: number; }; /** * Checks if a replace highlight exists for the given page and hash ID. * * @param {number} pageIndex - The index of the page to check. * @param {string} hashId - The unique hash ID of the replace highlight to check. * @returns {boolean} `true` if the replace highlight exists, otherwise `false`. */ hasReplaceHighlight(pageIndex: number, hashId: string): boolean; /** * Adds replace text highlights to the specified page's replaced highlights map. * * @param {number} pageIndex - The index of the page where the highlight should be added. * @param {ICustomHighlight} highlight - The highlight object to add. * @param {HighlightBehaviorArgs} [args] - Optional behavior arguments, such as whether to skip repainting the text layer after adding the highlight. */ addReplaceHighlights(pageIndex: number, highlights: ICustomHighlight[], args?: HighlightBehaviorArgs): { start: number; end: number; }; get eventBus(): import("../Models/ViewerTypes").IGCEventBus; /** * Removes a highlight from the specified page's replaced highlights map by its index. * * @param {number} pageIndex - The index of the page where the highlight should be removed. * @param {number} index - The index of the highlight in the replaceHighlights array to remove. */ removeReplaceHighlight(pageIndex: number, index: number, args?: HighlightBehaviorArgs): void; /** * Removes highlights from the specified page's replaced highlights map by their index range. * * @param {number} pageIndex - The index of the page where the highlights should be removed. * @param {{ start: number; end: number }} indices - The range of indices specifying which highlights to remove. * @param {HighlightBehaviorArgs} [args] - Optional behavior arguments, such as whether to skip repainting the text layer. */ removeReplaceHighlights(pageIndex: number, indices: { start: number; end: number; }, args?: HighlightBehaviorArgs): void; /** * Clears all replaced highlights. */ clearAllReplaceHighlights(args?: HighlightBehaviorArgs): void; /** * Adds a custom highlight directly to a specified page. * * This method allows you to add a predefined highlight object to a specific page. * * @param {number} pageIndex - The index of the page where the highlight should be added (0-based). * @param {ICustomHighlight} highlight - The highlight object to add. * @param {HighlightBehaviorArgs} [args] - Optional behavior arguments, such as whether to skip repainting the text layer after adding the highlight. * * @example * // Add a custom highlight to page 1: * const highlight = { rects: [{ x: 10, y: 20, w: 100, h: 15 }], color: 'rgba(0, 255, 0, 0.3)' }; * highlightManager.addHighlight(1, highlight); */ addHighlight(pageIndex: number, highlight: ICustomHighlight, args?: HighlightBehaviorArgs): number; /** * Highlights a portion of text on a specified page. * * This method creates a highlight for a segment of text on a page, with customizable properties. Optionally, it can clear previous highlights before adding the new one and skip the immediate repainting of the text layer. * * @param {number} pageIndex - The index of the page where the text segment is located (0-based). * @param {number} startCharIndex - The starting character index (0-based) of the text segment to highlight. * @param {number} endCharIndex - The ending character index (0-based) of the text segment to highlight. * @param {Object} [args] - Optional parameters to customize the appearance and behavior of the highlight. * @param {string} [args.color=DEFAULT_HIGHLIGHT_COLOR] - The fill color for the highlight in `rgba`, `hex`, or named color format. * @param {string} [args.borderColor=DEFAULT_BORDER_COLOR] - The border color for the highlight in `rgba`, `hex`, or named color format. * @param {number} [args.borderWidth=DEFAULT_BORDER_WIDTH] - The width of the highlight border in pixels. * @param {boolean} [args.clearPrevious=false] - If `true`, clears existing highlights on the page before adding the new one. * @param {boolean} [args.skipPaint=false] - If `true`, skips the immediate repaint of the text layer after adding the highlight. This allows for batch updates and more efficient rendering. * * @returns {Promise<void>} A promise that resolves once the highlight has been added and optionally the text layer has been repainted. * * @example * // Add a highlight for a text segment from index 10 to 20 on page 0, with custom color and border, and clear existing highlights: * await viewer.highlightTextSegment(0, 10, 20, { color: 'rgba(255, 255, 0, 0.5)', borderColor: 'rgba(255, 0, 0, 0.75)', borderWidth: 3, clearPrevious: true }); * * @example * // Add a highlight and skip the immediate repaint of the text layer: * await viewer.highlightTextSegment(0, 10, 20, { color: '#00FF00', skipPaint: true }); */ highlightTextSegment(pageIndex: number, startCharIndex: number, endCharIndex: number, args?: HighlightArgs): Promise<boolean>; /** * Clears all highlights from one or more specific pages. * * This method removes all custom highlights from the specified pages. You can pass either a single page index or an array of page indices. * * @param {number | number[]} pageIndex - The index of the page or an array of page indices from which to clear highlights. * @param {HighlightBehaviorArgs} [args] - Optional behavior arguments, such as whether to skip repainting the text layer after clearing highlights. * * @example * // Clear highlights from page 2: * viewer.clearHighlightedSegments(2); * * @example * // Clear highlights from pages 1, 3, and 5: * viewer.clearHighlightedSegments([1, 3, 5]); */ clearHighlightedSegments(pageIndex: number | number[], args?: HighlightBehaviorArgs): void; /** * Removes a specific highlight from a page. * * This method removes a highlight at a specified index from the highlights on a given page. * * @param {number} pageIndex - The index of the page from which to remove the highlight (0-based). * @param {number} index - The index of the highlight to remove within the specified page. * @param {HighlightBehaviorArgs} [args] - Optional behavior arguments, such as whether to skip repainting the text layer after removing the highlight. * * @example * // Remove the first highlight from page 2: * viewer.removeHighlight(2, 0); * * @example * // Remove the second highlight from page 1 and skip repainting: * viewer.removeHighlight(1, 1, { skipPaint: true }); */ removeHighlight(pageIndex: number, index: number, args?: HighlightBehaviorArgs): void; /** * Clears all highlights from all pages in the document. * * This method removes all custom highlights from every page in the document. You can optionally control whether to skip repainting the text layer after clearing the highlights. * * @param {HighlightBehaviorArgs} [args] - Optional behavior arguments. If `skipPaint` is `true`, the text layer will not be repainted immediately after clearing the highlights. * * @example * // Clear all highlights from the entire document and repaint the text layer: * highlightManager.clearAllHighlights(); * * @example * // Clear all highlights from the entire document and skip repainting: * highlightManager.clearAllHighlights({ skipPaint: true }); */ clearAllHighlights(args?: HighlightBehaviorArgs): void; /** * Retrieves all highlights for a specific page. * @param {number} pageIndex - The index of the page to get highlights for. * @returns {ICustomHighlight[]} An array of highlights for the specified page. */ getHighlightsForPage(pageIndex: number): ICustomHighlight[]; /** * Retrieves an array of `ReplaceTextModel` data from the replaced highlights. * This method iterates over the `replaceHighlights` map, collects the `replaceData` * from each highlight, and returns it in an array. * * @returns {ReplaceTextModel[] | undefined} An array of `ReplaceTextModel` objects, * or `undefined` if no replace highlights are found. */ getReplaceTextData(): ReplaceTextModel[] | undefined; /** * Repaints the text layer for one or more specified pages. * * This method updates the display of highlights by repainting the text layer on the specified pages. * * @param {number | number[]} pageIndices - The index or indices of the pages to repaint. * * @example * // Repaint the text layer for page 1: * highlightManager.repaintTextLayer(1); * * @example * // Repaint the text layer for pages 2 and 3: * highlightManager.repaintTextLayer([2, 3]); */ repaintTextLayer(pageIndices?: number | number[]): void; /** * Cleans up the document by resetting the highlights and replaced highlights. * This method clears all current highlights and replaced highlights, * effectively preparing the document for a new state or fresh rendering. */ cleanupDocument(): void; }