react-native-view-pdf
Version:
React native Pdf viewer implementation
98 lines (97 loc) • 4.59 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React from 'react';
import { findNodeHandle, UIManager, Platform, NativeModules, requireNativeComponent, } from 'react-native';
const PDFViewNativeComponent = requireNativeComponent('PDFView');
class PDFView extends React.Component {
constructor() {
super(...arguments);
this.onError = (event) => {
var _a, _b;
const { nativeEvent } = event || {};
(_b = (_a = this.props).onError) === null || _b === void 0 ? void 0 : _b.call(_a, nativeEvent || new Error('unknown error'));
};
this.onPageChanged = (event) => {
var _a, _b;
const { nativeEvent = {} } = event || {};
const { page = -1, pageCount = -1 } = nativeEvent;
(_b = (_a = this.props).onPageChanged) === null || _b === void 0 ? void 0 : _b.call(_a, page, pageCount);
};
this.onScrolled = (event) => {
var _a, _b;
const { nativeEvent = {} } = event || {};
const { offset = -1 } = nativeEvent;
(_b = (_a = this.props).onScrolled) === null || _b === void 0 ? void 0 : _b.call(_a, offset);
};
this._getCommands = () => {
const _PDFView = UIManager.getViewManagerConfig
? UIManager.getViewManagerConfig('PDFView') // RN 0.58
: UIManager.PDFView; // RN 0.57
return _PDFView.Commands;
};
this._setViewRef = (ref) => {
this._viewerRef = ref;
};
}
/**
* A Function. Invoke it when PDF document needs to be reloaded. Use `ref` to
* access it. Throws an exception in case of errors
*/
reload() {
return __awaiter(this, void 0, void 0, function* () {
if (this._viewerRef) {
const handle = findNodeHandle(this._viewerRef);
if (!handle) {
throw new Error('Cannot find node handles');
}
yield Platform.select({
android: () => __awaiter(this, void 0, void 0, function* () {
return UIManager.dispatchViewManagerCommand(handle, this._getCommands().reload, []);
}),
ios: () => __awaiter(this, void 0, void 0, function* () { return NativeModules.PDFView.reload(handle); }),
default: () => __awaiter(this, void 0, void 0, function* () { }),
})();
}
else {
throw new Error('No ref to PDFView component, check that component is mounted');
}
});
}
render() {
const _a = this.props, {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onError,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onPageChanged,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onScrolled } = _a, remainingProps = __rest(_a, ["onError", "onPageChanged", "onScrolled"]);
return (React.createElement(PDFViewNativeComponent, Object.assign({ ref: this._setViewRef }, remainingProps, { onError: this.onError, onPageChanged: this.onPageChanged, onScrolled: this.onScrolled })));
}
}
PDFView.defaultProps = {
fadeInDuration: 0.0,
resourceType: 'url',
textEncoding: 'utf-8',
urlProps: {},
enableAnnotations: false,
fileFrom: 'bundle',
};
export default PDFView;