From 881e13f03f71a5e0f5ab57c6f9566366333e5817 Mon Sep 17 00:00:00 2001 From: Wonday Date: Tue, 18 Sep 2018 21:24:58 +0800 Subject: [PATCH] add minScale, maxScale props --- PdfView.js | 12 ++++-- .../src/main/java/org/wonday/pdf/PdfView.java | 43 ++++++++++++++++++- .../java/org/wonday/pdf/RCTPdfManager.java | 10 +++++ index.d.ts | 2 + index.js | 4 ++ ios/RCTPdf/RCTPdfView.h | 4 +- ios/RCTPdf/RCTPdfView.m | 28 ++++++------ ios/RCTPdf/RCTPdfViewManager.m | 4 +- react-native-pdf.podspec | 1 - 9 files changed, 87 insertions(+), 21 deletions(-) diff --git a/PdfView.js b/PdfView.js index e04b481f..f15ca5d3 100644 --- a/PdfView.js +++ b/PdfView.js @@ -18,7 +18,9 @@ import DoubleTapView from './DoubleTapView'; import PinchZoomView from './PinchZoomView'; import PdfViewFlatList from './PdfViewFlatList'; +const MIN_SCALE = 1; const MAX_SCALE = 3; + const VIEWABILITYCONFIG = {minimumViewTime: 500, itemVisiblePercentThreshold: 10, waitForInteraction: false}; export default class PdfView extends Component { @@ -28,6 +30,8 @@ export default class PdfView extends Component { path: PropTypes.string, password: PropTypes.string, scale: PropTypes.number, + minScale: PropTypes.number, + maxScale: PropTypes.number, spacing: PropTypes.number, fitPolicy: PropTypes.number, horizontal: PropTypes.bool, @@ -41,6 +45,8 @@ export default class PdfView extends Component { path: "", password: "", scale: 1, + minScale: MIN_SCALE, + maxScale: MAX_SCALE, spacing: 10, style: {}, fitPolicy: 2, @@ -222,7 +228,7 @@ export default class PdfView extends Component { _onItemDoubleTap = (index) => { - if (this.state.scale >= MAX_SCALE) { + if (this.state.scale >= this.props.maxScale) { this._onScaleChanged({ scale: 1 / this.state.scale, pageX: this.state.contentContainerSize.width / 2, @@ -241,8 +247,8 @@ export default class PdfView extends Component { _onScaleChanged = (pinchInfo) => { let newScale = pinchInfo.scale * this.state.scale; - newScale = newScale > MAX_SCALE ? MAX_SCALE : newScale; - newScale = newScale < 1 ? 1 : newScale; + newScale = newScale > this.props.maxScale ? this.props.maxScale : newScale; + newScale = newScale < this.props.minScale ? this.props.minScale : newScale; let newContentOffset = { x: (this.state.contentOffset.x + pinchInfo.pageX) * (newScale / this.state.scale) - pinchInfo.pageX, y: (this.state.contentOffset.y + pinchInfo.pageY) * (newScale / this.state.scale) - pinchInfo.pageY diff --git a/android/src/main/java/org/wonday/pdf/PdfView.java b/android/src/main/java/org/wonday/pdf/PdfView.java index ab8208b0..f473fdd4 100644 --- a/android/src/main/java/org/wonday/pdf/PdfView.java +++ b/android/src/main/java/org/wonday/pdf/PdfView.java @@ -29,7 +29,9 @@ import com.github.barteksc.pdfviewer.listener.OnRenderListener; import com.github.barteksc.pdfviewer.listener.OnTapListener; import com.github.barteksc.pdfviewer.listener.OnDrawListener; +import com.github.barteksc.pdfviewer.listener.OnPageScrollListener; import com.github.barteksc.pdfviewer.util.FitPolicy; +import com.github.barteksc.pdfviewer.util.Constants; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactContext; @@ -48,11 +50,13 @@ import java.lang.ClassCastException; -public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompleteListener,OnErrorListener,OnTapListener,OnDrawListener { +public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompleteListener,OnErrorListener,OnTapListener,OnDrawListener,OnPageScrollListener { private ThemedReactContext context; private int page = 1; // start from 1 private boolean horizontal = false; private float scale = 1; + private float minScale = 1; + private float maxScale = 3; private String asset; private String path; private int spacing = 10; @@ -130,9 +134,22 @@ public void onError(Throwable t){ ); } + @Override + public void onPageScrolled(int page, float positionOffset){ + + // maybe change by other instance, restore zoom setting + Constants.Pinch.MINIMUM_ZOOM = this.minScale; + Constants.Pinch.MAXIMUM_ZOOM = this.maxScale; + + } + @Override public boolean onTap(MotionEvent e){ + // maybe change by other instance, restore zoom setting + Constants.Pinch.MINIMUM_ZOOM = this.minScale; + Constants.Pinch.MAXIMUM_ZOOM = this.maxScale; + WritableMap event = Arguments.createMap(); event.putString("message", "pageSingleTap|"+page); @@ -152,6 +169,11 @@ public boolean onTap(MotionEvent e){ public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage){ if (lastPageWidth>0 && lastPageHeight>0 && (pageWidth!=lastPageWidth || pageHeight!=lastPageHeight)) { + + // maybe change by other instance, restore zoom setting + Constants.Pinch.MINIMUM_ZOOM = this.minScale; + Constants.Pinch.MAXIMUM_ZOOM = this.maxScale; + WritableMap event = Arguments.createMap(); event.putString("message", "scaleChanged|"+(pageWidth/lastPageWidth)); @@ -173,6 +195,14 @@ public void drawPdf() { showLog(format("drawPdf path:%s %s", this.path, this.page)); if (this.path != null){ + + // set scale + this.setMinZoom(this.minScale); + this.setMaxZoom(this.maxScale); + this.setMidZoom((this.maxScale+this.minScale)/2); + Constants.Pinch.MINIMUM_ZOOM = this.minScale; + Constants.Pinch.MAXIMUM_ZOOM = this.maxScale; + this.fromUri(getURI(this.path)) .defaultPage(this.page-1) .swipeHorizontal(this.horizontal) @@ -181,6 +211,7 @@ public void drawPdf() { .onError(this) .onTap(this) .onDraw(this) + .onPageScroll(this) .spacing(this.spacing) .password(this.password) .enableAntialiasing(this.enableAntialiasing) @@ -207,6 +238,14 @@ public void setScale(float scale) { this.scale = scale; } + public void setMinScale(float minScale) { + this.minScale = minScale; + } + + public void setMaxScale(float maxScale) { + this.maxScale = maxScale; + } + public void setHorizontal(boolean horizontal) { this.horizontal = horizontal; } @@ -270,4 +309,4 @@ private Uri getURI(final String uri) { } return parsed; } -} +} \ No newline at end of file diff --git a/android/src/main/java/org/wonday/pdf/RCTPdfManager.java b/android/src/main/java/org/wonday/pdf/RCTPdfManager.java index b07a1f3c..d89cddc0 100644 --- a/android/src/main/java/org/wonday/pdf/RCTPdfManager.java +++ b/android/src/main/java/org/wonday/pdf/RCTPdfManager.java @@ -75,6 +75,16 @@ public void setScale(PdfView pdfView, float scale) { pdfView.setScale(scale); } + @ReactProp(name = "minScale") + public void setMinScale(PdfView pdfView, float minScale) { + pdfView.setMinScale(minScale); + } + + @ReactProp(name = "maxScale") + public void setMaxScale(PdfView pdfView, float maxScale) { + pdfView.setMaxScale(maxScale); + } + @ReactProp(name = "horizontal") public void setHorizontal(PdfView pdfView, boolean horizontal) { pdfView.setHorizontal(horizontal); diff --git a/index.d.ts b/index.d.ts index bac8ddcc..2941338d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -14,6 +14,8 @@ interface Props { source: object, page?: number, scale?: number, + minScale?: number, + maxScale?: number, horizontal?: boolean, spacing?: number, password?: string, diff --git a/index.js b/index.js index 940f0947..4dce5d0f 100644 --- a/index.js +++ b/index.js @@ -40,6 +40,8 @@ export default class Pdf extends Component { ]).isRequired, page: PropTypes.number, scale: PropTypes.number, + minScale: PropTypes.number, + maxScale: PropTypes.number, horizontal: PropTypes.bool, spacing: PropTypes.number, password: PropTypes.string, @@ -70,6 +72,8 @@ export default class Pdf extends Component { static defaultProps = { password: "", scale: 1, + minScale: 1, + maxScale: 3, spacing: 10, fitPolicy: 2, //fit both horizontal: false, diff --git a/ios/RCTPdf/RCTPdfView.h b/ios/RCTPdf/RCTPdfView.h index 0c56dd45..66959dc9 100644 --- a/ios/RCTPdf/RCTPdfView.h +++ b/ios/RCTPdf/RCTPdfView.h @@ -25,6 +25,8 @@ @property(nonatomic, strong) NSString *path; @property(nonatomic) int page; @property(nonatomic) float scale; +@property(nonatomic) float minScale; +@property(nonatomic) float maxScale; @property(nonatomic) BOOL horizontal; @property(nonatomic) BOOL enablePaging; @property(nonatomic) BOOL enableRTL; @@ -39,4 +41,4 @@ @end -#endif /* RCTPdfView_h */ +#endif /* RCTPdfView_h */ \ No newline at end of file diff --git a/ios/RCTPdf/RCTPdfView.m b/ios/RCTPdf/RCTPdfView.m index d02f6071..c8daee04 100644 --- a/ios/RCTPdf/RCTPdfView.m +++ b/ios/RCTPdf/RCTPdfView.m @@ -54,6 +54,8 @@ - (instancetype)init _page = 1; _scale = 1; + _minScale = MIN_SCALE; + _maxScale = MAX_SCALE; _horizontal = NO; _enablePaging = NO; _enableRTL = NO; @@ -151,10 +153,10 @@ - (void)didSetProps:(NSArray *)changedProps } } - if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"fitPolicy"])) { + if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"fitPolicy"] || [changedProps containsObject:@"minScale"] || [changedProps containsObject:@"maxScale"])) { - PDFPage *pdfPage = [_pdfDocument pageAtIndex:0]; - CGRect pdfPageRect = [pdfPage boundsForBox:kPDFDisplayBoxMediaBox]; + PDFPage *pdfPage = [_pdfDocument pageAtIndex:_pdfDocument.pageCount-1]; + CGRect pdfPageRect = [pdfPage boundsForBox:kPDFDisplayBoxCropBox]; // some pdf with rotation, then adjust it if (pdfPage.rotation == 90 || pdfPage.rotation == 270) { @@ -164,26 +166,26 @@ - (void)didSetProps:(NSArray *)changedProps if (_fitPolicy == 0) { _fixScaleFactor = self.frame.size.width/pdfPageRect.size.width; _pdfView.scaleFactor = _scale * _fixScaleFactor; - _pdfView.minScaleFactor = _fixScaleFactor*MIN_SCALE; - _pdfView.maxScaleFactor = _fixScaleFactor*MAX_SCALE; + _pdfView.minScaleFactor = _fixScaleFactor*_minScale; + _pdfView.maxScaleFactor = _fixScaleFactor*_maxScale; } else if (_fitPolicy == 1) { _fixScaleFactor = self.frame.size.height/pdfPageRect.size.height; _pdfView.scaleFactor = _scale * _fixScaleFactor; - _pdfView.minScaleFactor = _fixScaleFactor*MIN_SCALE; - _pdfView.maxScaleFactor = _fixScaleFactor*MAX_SCALE; + _pdfView.minScaleFactor = _fixScaleFactor*_minScale; + _pdfView.maxScaleFactor = _fixScaleFactor*_maxScale; } else { float pageAspect = pdfPageRect.size.width/pdfPageRect.size.height; float reactViewAspect = self.frame.size.width/self.frame.size.height; if (reactViewAspect>pageAspect) { _fixScaleFactor = self.frame.size.height/pdfPageRect.size.height; _pdfView.scaleFactor = _scale * _fixScaleFactor; - _pdfView.minScaleFactor = _fixScaleFactor*MIN_SCALE; - _pdfView.maxScaleFactor = _fixScaleFactor*MAX_SCALE; + _pdfView.minScaleFactor = _fixScaleFactor*_minScale; + _pdfView.maxScaleFactor = _fixScaleFactor*_maxScale; } else { _fixScaleFactor = self.frame.size.width/pdfPageRect.size.width; _pdfView.scaleFactor = _scale * _fixScaleFactor; - _pdfView.minScaleFactor = _fixScaleFactor*MIN_SCALE; - _pdfView.maxScaleFactor = _fixScaleFactor*MAX_SCALE; + _pdfView.minScaleFactor = _fixScaleFactor*_minScale; + _pdfView.maxScaleFactor = _fixScaleFactor*_maxScale; } } @@ -217,7 +219,7 @@ - (void)didSetProps:(NSArray *)changedProps PDFPage *pdfPage = [_pdfDocument pageAtIndex:_page-1]; if (pdfPage) { - CGRect pdfPageRect = [pdfPage boundsForBox:kPDFDisplayBoxMediaBox]; + CGRect pdfPageRect = [pdfPage boundsForBox:kPDFDisplayBoxCropBox]; // some pdf with rotation, then adjust it if (pdfPage.rotation == 90 || pdfPage.rotation == 270) { @@ -266,7 +268,7 @@ - (void)onDocumentChanged:(NSNotification *)noti if (_pdfDocument) { unsigned long numberOfPages = _pdfDocument.pageCount; - PDFPage *page = [_pdfDocument pageAtIndex:0]; + PDFPage *page = [_pdfDocument pageAtIndex:_pdfDocument.pageCount-1]; CGSize pageSize = [_pdfView rowSizeForPage:page]; _onChange(@{ @"message": [[NSString alloc] initWithString:[NSString stringWithFormat:@"loadComplete|%lu|%f|%f", numberOfPages, pageSize.width, pageSize.height]]}); } diff --git a/ios/RCTPdf/RCTPdfViewManager.m b/ios/RCTPdf/RCTPdfViewManager.m index 0c518b76..e49b171e 100644 --- a/ios/RCTPdf/RCTPdfViewManager.m +++ b/ios/RCTPdf/RCTPdfViewManager.m @@ -30,6 +30,8 @@ - (UIView *)view RCT_EXPORT_VIEW_PROPERTY(path, NSString); RCT_EXPORT_VIEW_PROPERTY(page, int); RCT_EXPORT_VIEW_PROPERTY(scale, float); +RCT_EXPORT_VIEW_PROPERTY(minScale, float); +RCT_EXPORT_VIEW_PROPERTY(maxScale, float); RCT_EXPORT_VIEW_PROPERTY(horizontal, BOOL); RCT_EXPORT_VIEW_PROPERTY(enablePaging, BOOL); RCT_EXPORT_VIEW_PROPERTY(enableRTL, BOOL); @@ -58,4 +60,4 @@ + (BOOL)requiresMainQueueSetup { - (void)dealloc{ } -@end +@end \ No newline at end of file diff --git a/react-native-pdf.podspec b/react-native-pdf.podspec index 2daf7fd7..555d4c47 100644 --- a/react-native-pdf.podspec +++ b/react-native-pdf.podspec @@ -12,5 +12,4 @@ Pod::Spec.new do |s| s.source = { :git => 'https://github.com/wonday/react-native-pdf.git' } s.platform = :ios, '8.0' s.source_files = "ios/**/*.{h,m}" - s.dependency 'React' end