UNPKG

@compdfkit_pdf_sdk/react_native

Version:

ComPDFKit for React Native is a comprehensive SDK that allows you to quickly add PDF functionality to Android, iOS, and React Native applications.

158 lines 5.8 kB
"use strict"; /** * Copyright © 2014-2026 PDF Technologies, Inc. All Rights Reserved. * * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. * This notice may not be removed from this file. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CPDFAnnotationHistoryManager = void 0; const react_native_1 = require("react-native"); const CPDFHistoryManagerBase_1 = require("./CPDFHistoryManagerBase"); const { CPDFViewManager } = react_native_1.NativeModules; /** * Manages the undo and redo history for PDF annotation actions within a viewer component. * * This class extends `CPDFHistoryManagerBase` and provides methods to interact with the annotation history, * including checking if undo/redo actions are available and performing those actions. It also allows * registering a listener to monitor changes in the annotation history state. * * **Usage:** * - Instantiate with a reference to the PDF viewer component. * - Use `canUndo()` and `canRedo()` to check if undo/redo actions are available. * - Call `undo()` and `redo()` to perform undo/redo operations. * - Register a listener with `setOnHistoryStateChangedListener()` to be notified when the history state changes. * * * @example * ```typescript * const historyManager = pdfReader._annotationsHistoryManager; * historyManager.setOnHistoryStateChangedListener((canUndo, canRedo) => { * // Update UI based on undo/redo availability * }); * ``` */ class CPDFAnnotationHistoryManager extends CPDFHistoryManagerBase_1.CPDFHistoryManagerBase { _viewerRef; _onHistoryStateChanged; constructor(viewerRef) { super(); this._viewerRef = viewerRef; } handle(event) { const { canUndo, canRedo } = event.nativeEvent.onAnnotationHistoryChanged; if (this._onHistoryStateChanged) { this._onHistoryStateChanged(canUndo, canRedo); } } /** * Sets a listener to monitor changes in annotation history state. * This should be called during annotation operations to indicate whether undo or redo actions are available. * @param listener A callback function with two parameters: * canUndo (whether undo is available) and canRedo (whether redo is available). * @example * ```typescript * const historyManager = pdfReader._annotationsHistoryManager; * historyManager.setOnHistoryStateChangedListener((canUndo, canRedo) => { * // Update UI based on undo/redo availability * }); * ``` */ setOnHistoryStateChangedListener(listener) { this._onHistoryStateChanged = listener; } /** * Checks if an undo operation is possible. * * @example * ```typescript * const manager = pdfReader._annotationsHistoryManager * await manager.canUndo(); * ``` * @returns {Promise<boolean>} A promise that resolves to `true` if an undo operation is possible, otherwise `false`. */ async canUndo() { const tag = (0, react_native_1.findNodeHandle)(this._viewerRef); if (tag) { try { return await CPDFViewManager.annotationCanUndo(tag); } catch (e) { return false; } } return false; } /** * Checks if an undo operation is possible. * * @example * ```typescript * const manager = pdfReader._annotationsHistoryManager * await manager.canRedo(); * ``` * @returns {Promise<boolean>} A promise that resolves to `true` if a redo operation is possible, otherwise `false`. */ async canRedo() { const tag = (0, react_native_1.findNodeHandle)(this._viewerRef); if (tag) { try { return await CPDFViewManager.annotationCanRedo(tag); } catch (e) { return false; } } return false; } /** * Checks if a redo operation is possible. * * @example * ```typescript * const manager = pdfReader._annotationsHistoryManager * await manager.redo(); * ``` * * @returns {Promise<void>} A promise that resolves when the undo operation is complete. */ async undo() { const tag = (0, react_native_1.findNodeHandle)(this._viewerRef); if (tag) { try { return await CPDFViewManager.annotationUndo(tag); } catch (e) { return Promise.reject(new Error('Unable to undo the annotation action')); } } return Promise.reject(new Error('Unable to find the native view reference')); } /** * Performs a redo operation on the annotation history. * * @example * ```typescript * const manager = pdfReader._annotationsHistoryManager * await manager.redo(); * ``` * * @returns {Promise<void>} A promise that resolves when the redo operation is complete. */ async redo() { const tag = (0, react_native_1.findNodeHandle)(this._viewerRef); if (tag) { try { return await CPDFViewManager.annotationRedo(tag); } catch (e) { return Promise.reject(new Error('Unable to redo the annotation action')); } } return Promise.reject(new Error('Unable to find the native view reference')); } } exports.CPDFAnnotationHistoryManager = CPDFAnnotationHistoryManager; //# sourceMappingURL=CPDFAnnotationHistoryManager.js.map