Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RTL support #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions component/Cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ class Cropper extends Component {
imgLoaded: false,
styles: deepExtend({}, defaultStyles, styles)
}
this.pageDirection = document.body.dir;
this.isRTL = this.pageDirection === 'rtl';
}

// initialize style, component did mount or component updated.
initStyles () {
const container = findDOMNode(this.container)
Expand Down Expand Up @@ -332,6 +333,7 @@ class Cropper extends Component {

let _x = pageX - startPageX + originX
let _y = pageY - startPageY + originY
if (this.isRTL) _x = -1 * _x;
if (pageX < 0 || pageY < 0) return false

if (_x > maxLeft) _x = maxLeft
Expand Down Expand Up @@ -485,7 +487,6 @@ class Cropper extends Component {
offsetLeft,
offsetTop
} = container

this.setState({
// set offset left and top of new frame
originX: pageX - offsetLeft,
Expand Down Expand Up @@ -568,7 +569,6 @@ class Cropper extends Component {
imgWidth,
imgHeight
} = this.state

this.setState({
originX: offsetLeft,
originY: offsetTop,
Expand Down Expand Up @@ -676,7 +676,7 @@ class Cropper extends Component {
styles.frame,
dragging ? styles.dragging_frame : {}, {
display: 'block',
left: this.state.toImgLeft4Style,
[this.isRTL ? 'right' : 'left']: this.state.toImgLeft4Style,
top: this.state.toImgTop4Style,
width: this.state.frameWidth4Style,
height: this.state.frameHeight4Style
Expand All @@ -699,7 +699,7 @@ class Cropper extends Component {
style={
deepExtend({},
styles.img, {
marginLeft: -1 * this.state.toImgLeft4Style,
[this.isRTL ? 'marginRight' : 'marginLeft']: -1 * this.state.toImgLeft4Style,
marginTop: -1 * this.state.toImgTop4Style
}
)
Expand Down
2 changes: 1 addition & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom'
import './demo.less'
import Cropper from '../component/Cropper'

const DemoImg = 'https://braavos.me/images/posts/gr/8.jpg'
const DemoImg = 'https://wallpaper-mania.com/wp-content/uploads/2018/09/High_resolution_wallpaper_background_ID_77701347521.jpg'

class ImageCropDemo extends Component {
constructor (props) {
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<html lang="ar">
<head>
<meta charset="UTF-8">
<title>react image crop</title>
</head>
<body>
<body dir="rtl">
<div class="container">
<h2>crop demo</h2>
<div id="root"></div>
Expand Down
27 changes: 12 additions & 15 deletions lib/Cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
Expand Down Expand Up @@ -66,9 +68,10 @@ var Cropper = function (_Component) {
imgLoaded: false,
styles: deepExtend({}, defaultStyles, styles)
};
_this.pageDirection = document.body.dir;
_this.isRTL = _this.pageDirection === 'rtl';
return _this;
}

// initialize style, component did mount or component updated.


Expand Down Expand Up @@ -370,6 +373,7 @@ var Cropper = function (_Component) {

var _x = pageX - startPageX + originX;
var _y = pageY - startPageY + originY;
if (this.isRTL) _x = -1 * _x;
if (pageX < 0 || pageY < 0) return false;

if (_x > maxLeft) _x = maxLeft;
Expand Down Expand Up @@ -527,7 +531,6 @@ var Cropper = function (_Component) {
var offsetLeft = container.offsetLeft,
offsetTop = container.offsetTop;


this.setState({
// set offset left and top of new frame
originX: pageX - offsetLeft,
Expand Down Expand Up @@ -622,7 +625,6 @@ var Cropper = function (_Component) {
imgWidth = _state9.imgWidth,
imgHeight = _state9.imgHeight;


this.setState({
originX: offsetLeft,
originY: offsetTop,
Expand All @@ -642,7 +644,9 @@ var Cropper = function (_Component) {
}, {
key: 'render',
value: function render() {
var _this7 = this;
var _this7 = this,
_deepExtend,
_deepExtend2;

var _state10 = this.state,
dragging = _state10.dragging,
Expand Down Expand Up @@ -717,13 +721,9 @@ var Cropper = function (_Component) {
React.createElement(
'div',
{
style: deepExtend({}, styles.frame, dragging ? styles.dragging_frame : {}, {
display: 'block',
left: this.state.toImgLeft4Style,
top: this.state.toImgTop4Style,
width: this.state.frameWidth4Style,
height: this.state.frameHeight4Style
}),
style: deepExtend({}, styles.frame, dragging ? styles.dragging_frame : {}, (_deepExtend = {
display: 'block'
}, _defineProperty(_deepExtend, this.isRTL ? 'right' : 'left', this.state.toImgLeft4Style), _defineProperty(_deepExtend, 'top', this.state.toImgTop4Style), _defineProperty(_deepExtend, 'width', this.state.frameWidth4Style), _defineProperty(_deepExtend, 'height', this.state.frameHeight4Style), _deepExtend)),
ref: function ref(_ref9) {
_this7.frameNode = _ref9;
}
Expand All @@ -738,10 +738,7 @@ var Cropper = function (_Component) {
crossOrigin: 'anonymous',
width: imgWidth,
height: imgHeight,
style: deepExtend({}, styles.img, {
marginLeft: -1 * this.state.toImgLeft4Style,
marginTop: -1 * this.state.toImgTop4Style
}),
style: deepExtend({}, styles.img, (_deepExtend2 = {}, _defineProperty(_deepExtend2, this.isRTL ? 'marginRight' : 'marginLeft', -1 * this.state.toImgLeft4Style), _defineProperty(_deepExtend2, 'marginTop', -1 * this.state.toImgTop4Style), _deepExtend2)),
ref: function ref(_ref8) {
_this7.cloneImg = _ref8;
}
Expand Down
Loading