Skip to content

Commit 36b46ee

Browse files
Added onZoom and onRotation functions
1 parent f9169df commit 36b46ee

8 files changed

Lines changed: 116 additions & 33 deletions

File tree

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,43 +112,55 @@ React component prop. types:
112112
- Required: **false**
113113
- Description: A function that will be called on clicking on the next page button, page number can be accessed in the function.
114114

115-
- `css`:
115+
- `onZoom`:
116+
117+
- Type: _Function_
118+
- Required: **false**
119+
- Description: A function that will be called on clicking on Zoom controls, zoom scale can be accessed in the function.
120+
121+
- `onRotation`:
122+
123+
- Type: _Function_
124+
- Required: **false**
125+
- Description: A function that will be called on clicking on Rotation controls, rotation angle can be accessed in the function.
126+
127+
* `css`:
116128

117129
- Type: _String_
118130
- Required: **false**
119131
- Description: CSS classes that will be setted for the component wrapper
120132

121-
- `canvasCss`:
133+
* `canvasCss`:
122134

123135
- Type: _String_
124136
- Required: **false**
125137
- Description: CSS classes that will be setted for the PDF page
126138

127-
- `hideNavbar`:
139+
* `hideNavbar`:
128140

129141
- Type: _Boolean_
130142
- Required: **false**
131143
- Description: By default navbar is displayed, but can be hidden by passing this prop
132144

133-
- `navbarOnTop`:
145+
* `navbarOnTop`:
134146

135147
- Type: _Boolean_
136148
- Required: **false**
137149
- Description: By default navbar is displayed on bottom, but can be placed on top by passing this prop
138150

139-
- `hideZoom`:
151+
* `hideZoom`:
140152

141153
- Type: _Boolean_
142154
- Required: **false**
143155
- Description: By default zoom buttons are displayed, but can be hidden by passing this prop
144156

145-
- `hideRotation`:
157+
* `hideRotation`:
146158

147159
- Type: _Boolean_
148160
- Required: **false**
149161
- Description: By default rotation buttons are displayed, but can be hidden by passing this prop
150162

151-
- `navigation`:
163+
* `navigation`:
152164

153165
- Type:
154166

dist/pdf-viewer-reactjs.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ ZoomOut.propTypes = {
126126
};
127127

128128
var ResetZoom = function ResetZoom(_ref) {
129-
var css = _ref.css,
129+
var scale = _ref.scale,
130+
css = _ref.css,
130131
handleResetZoom = _ref.handleResetZoom;
131132

132-
var resetZoomClass = css ? css : 'btn btn-sm btn-link text-white px-2';
133+
var resetZoomClass = (css ? css : 'btn btn-sm btn-link text-white px-2') + '\n ' + (scale === 1 ? ' disabled' : '');
133134

134135
return React.createElement(
135136
'button',
@@ -266,6 +267,7 @@ var Navigation = function Navigation(_ref) {
266267
handleZoomOut: handleZoomOut
267268
}),
268269
React.createElement(ResetZoom, {
270+
scale: scale,
269271
css: css.resetZoomBtn,
270272
handleResetZoom: handleResetZoom
271273
}),
@@ -523,48 +525,78 @@ var PDFViewer = function (_React$Component) {
523525
_this.setState({
524526
page: _this.state.page - 1
525527
});
526-
_this.props.onPrevBtnClick(_this.state.page - 1);
528+
529+
if (_this.props.onPrevBtnClick) {
530+
_this.props.onPrevBtnClick(_this.state.page - 1);
531+
}
527532
}, _this.handleNextClick = function () {
528533
if (_this.state.page === _this.state.pages) return;
529534

530535
_this.setState({
531536
page: _this.state.page + 1
532537
});
533-
_this.props.onNextBtnClick(_this.state.page + 1);
538+
539+
if (_this.props.onNextBtnClick) {
540+
_this.props.onNextBtnClick(_this.state.page + 1);
541+
}
534542
}, _this.handleZoomIn = function () {
535543
if (_this.state.scale < _this.state.maxScale) {
536544
_this.setState({
537545
scale: _this.state.scale + _this.state.scaleStep
538546
});
539547
}
548+
549+
if (_this.props.onZoom) {
550+
_this.props.onZoom(_this.state.scale + _this.state.scaleStep);
551+
}
540552
}, _this.handleResetZoom = function () {
541553
_this.setState({
542554
scale: 1
543555
});
556+
557+
if (_this.props.onZoom) {
558+
_this.props.onZoom(1);
559+
}
544560
}, _this.handleZoomOut = function () {
545561
if (_this.state.scale > 1) {
546562
_this.setState({
547563
scale: _this.state.scale - _this.state.scaleStep
548564
});
549565
}
566+
567+
if (_this.props.onZoom) {
568+
_this.props.onZoom(_this.state.scale - _this.state.scaleStep);
569+
}
550570
}, _this.handleRotateLeft = function () {
551571
if (_this.state.rotationAngle !== -90) {
552572
_this.setState({
553573
rotationAngle: -90
554574
});
555575
}
576+
577+
if (_this.props.onZoom) {
578+
_this.props.onRotation(-90);
579+
}
556580
}, _this.handleResetRotation = function () {
557581
if (_this.state.rotationAngle !== 0 || _this.state.rotationAngle !== 360) {
558582
_this.setState({
559583
rotationAngle: 360
560584
});
561585
}
586+
587+
if (_this.props.onZoom) {
588+
_this.props.onRotation(0);
589+
}
562590
}, _this.handleRotateRight = function () {
563591
if (_this.state.rotationAngle !== 90) {
564592
_this.setState({
565593
rotationAngle: 90
566594
});
567595
}
596+
597+
if (_this.props.onZoom) {
598+
_this.props.onRotation(90);
599+
}
568600
}, _temp), possibleConstructorReturn(_this, _ret);
569601
}
570602

@@ -721,6 +753,8 @@ PDFViewer.propTypes = {
721753
onDocumentClick: PropTypes.func,
722754
onPrevBtnClick: PropTypes.func,
723755
onNextBtnClick: PropTypes.func,
756+
onZoom: PropTypes.func,
757+
onRotation: PropTypes.func,
724758
hideNavbar: PropTypes.bool,
725759
navbarOnTop: PropTypes.bool,
726760
hideZoom: PropTypes.bool,

example/package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/App.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,19 @@ const WithCustomNavigation = () => (
127127
const WithOnDocumentClick = () => (
128128
<div className="col-md-auto text-center">
129129
<h1 className="text-white bg-info rounded">
130-
With onDocumentClick, onPrevBtnClick and onNextBtnClick handler
130+
With onDocumentClick, onPrevBtnClick, onNextBtnClick, onZoom and
131+
onRotation handler
131132
</h1>
132133
<div className="border rounded">
133134
<PDFViewer
134135
document={{
135136
url: sources.url
136137
}}
137138
onDocumentClick={() => alert('Document was clicked')}
138-
onPrevBtnClick={page => alert(`Page ${page} was selected`)}
139-
onNextBtnClick={page => alert(`Page ${page} was selected`)}
139+
onPrevBtnClick={page => alert(`Page ${page} selected`)}
140+
onNextBtnClick={page => alert(`Page ${page} selected`)}
141+
onZoom={scale => alert(`Zoom scale is ${scale}`)}
142+
onRotation={angle => alert(`Page angle is ${angle}`)}
140143
/>
141144
</div>
142145
</div>

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Navigation/ResetZoom.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4-
const ResetZoom = ({ css, handleResetZoom }) => {
5-
const resetZoomClass = css ? css : 'btn btn-sm btn-link text-white px-2';
4+
const ResetZoom = ({ scale, css, handleResetZoom }) => {
5+
const resetZoomClass = `${css ? css : 'btn btn-sm btn-link text-white px-2'}
6+
${scale === 1 ? ' disabled' : ''}`;
67

78
return (
89
<button

src/Navigation/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const Navigation = ({
4747
handleZoomOut={handleZoomOut}
4848
/>
4949
<ResetZoom
50+
scale={scale}
5051
css={css.resetZoomBtn}
5152
handleResetZoom={handleResetZoom}
5253
/>

src/index.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ class PDFViewer extends React.Component {
4242
this.setState({
4343
page: this.state.page - 1
4444
});
45-
this.props.onPrevBtnClick(this.state.page - 1);
45+
46+
if (this.props.onPrevBtnClick) {
47+
this.props.onPrevBtnClick(this.state.page - 1);
48+
}
4649
};
4750

4851
handleNextClick = () => {
@@ -51,7 +54,10 @@ class PDFViewer extends React.Component {
5154
this.setState({
5255
page: this.state.page + 1
5356
});
54-
this.props.onNextBtnClick(this.state.page + 1);
57+
58+
if (this.props.onNextBtnClick) {
59+
this.props.onNextBtnClick(this.state.page + 1);
60+
}
5561
};
5662

5763
handleZoomIn = () => {
@@ -60,12 +66,20 @@ class PDFViewer extends React.Component {
6066
scale: this.state.scale + this.state.scaleStep
6167
});
6268
}
69+
70+
if (this.props.onZoom) {
71+
this.props.onZoom(this.state.scale + this.state.scaleStep);
72+
}
6373
};
6474

6575
handleResetZoom = () => {
6676
this.setState({
6777
scale: 1
6878
});
79+
80+
if (this.props.onZoom) {
81+
this.props.onZoom(1);
82+
}
6983
};
7084

7185
handleZoomOut = () => {
@@ -74,6 +88,10 @@ class PDFViewer extends React.Component {
7488
scale: this.state.scale - this.state.scaleStep
7589
});
7690
}
91+
92+
if (this.props.onZoom) {
93+
this.props.onZoom(this.state.scale - this.state.scaleStep);
94+
}
7795
};
7896

7997
handleRotateLeft = () => {
@@ -82,6 +100,10 @@ class PDFViewer extends React.Component {
82100
rotationAngle: -90
83101
});
84102
}
103+
104+
if (this.props.onZoom) {
105+
this.props.onRotation(-90);
106+
}
85107
};
86108

87109
handleResetRotation = () => {
@@ -93,6 +115,10 @@ class PDFViewer extends React.Component {
93115
rotationAngle: 360
94116
});
95117
}
118+
119+
if (this.props.onZoom) {
120+
this.props.onRotation(0);
121+
}
96122
};
97123

98124
handleRotateRight = () => {
@@ -101,6 +127,10 @@ class PDFViewer extends React.Component {
101127
rotationAngle: 90
102128
});
103129
}
130+
131+
if (this.props.onZoom) {
132+
this.props.onRotation(90);
133+
}
104134
};
105135

106136
render() {
@@ -232,6 +262,8 @@ PDFViewer.propTypes = {
232262
onDocumentClick: PropTypes.func,
233263
onPrevBtnClick: PropTypes.func,
234264
onNextBtnClick: PropTypes.func,
265+
onZoom: PropTypes.func,
266+
onRotation: PropTypes.func,
235267
hideNavbar: PropTypes.bool,
236268
navbarOnTop: PropTypes.bool,
237269
hideZoom: PropTypes.bool,

0 commit comments

Comments
 (0)