Skip to content

Commit

Permalink
add minScale, maxScale props
Browse files Browse the repository at this point in the history
  • Loading branch information
Wonday committed Sep 18, 2018
1 parent 4ab92de commit 881e13f
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 21 deletions.
12 changes: 9 additions & 3 deletions PdfView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
43 changes: 41 additions & 2 deletions android/src/main/java/org/wonday/pdf/PdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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));

Expand All @@ -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)
Expand All @@ -181,6 +211,7 @@ public void drawPdf() {
.onError(this)
.onTap(this)
.onDraw(this)
.onPageScroll(this)
.spacing(this.spacing)
.password(this.password)
.enableAntialiasing(this.enableAntialiasing)
Expand All @@ -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;
}
Expand Down Expand Up @@ -270,4 +309,4 @@ private Uri getURI(final String uri) {
}
return parsed;
}
}
}
10 changes: 10 additions & 0 deletions android/src/main/java/org/wonday/pdf/RCTPdfManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface Props {
source: object,
page?: number,
scale?: number,
minScale?: number,
maxScale?: number,
horizontal?: boolean,
spacing?: number,
password?: string,
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion ios/RCTPdf/RCTPdfView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,4 +41,4 @@

@end

#endif /* RCTPdfView_h */
#endif /* RCTPdfView_h */
28 changes: 15 additions & 13 deletions ios/RCTPdf/RCTPdfView.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ - (instancetype)init

_page = 1;
_scale = 1;
_minScale = MIN_SCALE;
_maxScale = MAX_SCALE;
_horizontal = NO;
_enablePaging = NO;
_enableRTL = NO;
Expand Down Expand Up @@ -151,10 +153,10 @@ - (void)didSetProps:(NSArray<NSString *> *)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) {
Expand All @@ -164,26 +166,26 @@ - (void)didSetProps:(NSArray<NSString *> *)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;
}
}

Expand Down Expand Up @@ -217,7 +219,7 @@ - (void)didSetProps:(NSArray<NSString *> *)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) {
Expand Down Expand Up @@ -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]]});
}
Expand Down
4 changes: 3 additions & 1 deletion ios/RCTPdf/RCTPdfViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -58,4 +60,4 @@ + (BOOL)requiresMainQueueSetup {
- (void)dealloc{
}

@end
@end
1 change: 0 additions & 1 deletion react-native-pdf.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 881e13f

Please sign in to comment.