|
143 | 143 | * @return {void} |
144 | 144 | */ |
145 | 145 | init : function(postid) { |
146 | | - var t = this, old = $('#image-editor-' + t.postid), |
147 | | - x = t.intval( $('#imgedit-x-' + postid).val() ), |
148 | | - y = t.intval( $('#imgedit-y-' + postid).val() ); |
| 146 | + var t = this, old = $('#image-editor-' + t.postid); |
149 | 147 |
|
150 | 148 | if ( t.postid !== postid && old.length ) { |
151 | 149 | t.close(t.postid); |
152 | 150 | } |
153 | 151 |
|
154 | | - t.hold.w = t.hold.ow = x; |
155 | | - t.hold.h = t.hold.oh = y; |
156 | | - t.hold.xy_ratio = x / y; |
157 | 152 | t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() ); |
158 | 153 | t.postid = postid; |
159 | 154 | $('#imgedit-response-' + postid).empty(); |
|
188 | 183 | $( document ).on( 'image-editor-ui-ready', this.focusManager ); |
189 | 184 | }, |
190 | 185 |
|
| 186 | + /** |
| 187 | + * Calculate the image size and save it to memory. |
| 188 | + * |
| 189 | + * @since 6.7.0 |
| 190 | + * |
| 191 | + * @memberof imageEdit |
| 192 | + * |
| 193 | + * @param {number} postid The post ID. |
| 194 | + * |
| 195 | + * @return {void} |
| 196 | + */ |
| 197 | + calculateImgSize: function( postid ) { |
| 198 | + var t = this, |
| 199 | + x = t.intval( $( '#imgedit-x-' + postid ).val() ), |
| 200 | + y = t.intval( $( '#imgedit-y-' + postid ).val() ); |
| 201 | + |
| 202 | + t.hold.w = t.hold.ow = x; |
| 203 | + t.hold.h = t.hold.oh = y; |
| 204 | + t.hold.xy_ratio = x / y; |
| 205 | + t.hold.sizer = parseFloat( $( '#imgedit-sizer-' + postid ).val() ); |
| 206 | + t.currentCropSelection = null; |
| 207 | + }, |
| 208 | + |
191 | 209 | /** |
192 | 210 | * Toggles the wait/load icon in the editor. |
193 | 211 | * |
|
525 | 543 | for ( n in history ) { |
526 | 544 | i = history[n]; |
527 | 545 | if ( i.hasOwnProperty('c') ) { |
528 | | - op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h } }; |
| 546 | + op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h, 'r': i.c.r } }; |
529 | 547 | } else if ( i.hasOwnProperty('r') ) { |
530 | 548 | op[n] = { 'r': i.r.r }; |
531 | 549 | } else if ( i.hasOwnProperty('f') ) { |
|
860 | 878 | if ( 'undefined' === typeof this.hold.sizer ) { |
861 | 879 | this.init( postid ); |
862 | 880 | } |
| 881 | + this.calculateImgSize( postid ); |
863 | 882 |
|
864 | 883 | this.initCrop(postid, img, parent); |
865 | 884 | this.setCropSelection( postid, { 'x1': 0, 'y1': 0, 'x2': 0, 'y2': 0, 'width': img.innerWidth(), 'height': img.innerHeight() } ); |
|
909 | 928 | var t = this, |
910 | 929 | selW = $('#imgedit-sel-width-' + postid), |
911 | 930 | selH = $('#imgedit-sel-height-' + postid), |
912 | | - selX = $('#imgedit-start-x-' + postid), |
913 | | - selY = $('#imgedit-start-y-' + postid), |
914 | 931 | $image = $( image ), |
915 | 932 | $img; |
916 | 933 |
|
|
945 | 962 | * |
946 | 963 | * @return {void} |
947 | 964 | */ |
948 | | - parent.children().on( 'mousedown, touchstart', function(e){ |
949 | | - var ratio = false, sel, defRatio; |
950 | | - |
951 | | - if ( e.shiftKey ) { |
952 | | - sel = t.iasapi.getSelection(); |
953 | | - defRatio = t.getSelRatio(postid); |
954 | | - ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio; |
| 965 | + parent.children().on( 'mousedown touchstart', function(e) { |
| 966 | + var ratio = false, |
| 967 | + sel = t.iasapi.getSelection(), |
| 968 | + cx = t.intval( $( '#imgedit-crop-width-' + postid ).val() ), |
| 969 | + cy = t.intval( $( '#imgedit-crop-height-' + postid ).val() ); |
| 970 | + |
| 971 | + if ( cx && cy ) { |
| 972 | + ratio = t.getSelRatio( postid ); |
| 973 | + } else if ( e.shiftKey && sel && sel.width && sel.height ) { |
| 974 | + ratio = sel.width + ':' + sel.height; |
955 | 975 | } |
956 | 976 |
|
957 | 977 | t.iasapi.setOptions({ |
|
1000 | 1020 | * @return {void} |
1001 | 1021 | */ |
1002 | 1022 | onSelectChange: function(img, c) { |
1003 | | - var sizer = imageEdit.hold.sizer; |
1004 | | - selW.val( imageEdit.round(c.width / sizer) ); |
1005 | | - selH.val( imageEdit.round(c.height / sizer) ); |
1006 | | - selX.val( imageEdit.round(c.x1 / sizer) ); |
1007 | | - selY.val( imageEdit.round(c.y1 / sizer) ); |
| 1023 | + var sizer = imageEdit.hold.sizer, |
| 1024 | + oldSel = imageEdit.currentCropSelection; |
| 1025 | + |
| 1026 | + if ( oldSel != null && oldSel.width == c.width && oldSel.height == c.height ) { |
| 1027 | + return; |
| 1028 | + } |
| 1029 | + |
| 1030 | + selW.val( Math.min( imageEdit.hold.w, imageEdit.round( c.width / sizer ) ) ); |
| 1031 | + selH.val( Math.min( imageEdit.hold.h, imageEdit.round( c.height / sizer ) ) ); |
| 1032 | + |
| 1033 | + t.currentCropSelection = c; |
1008 | 1034 | } |
1009 | 1035 | }); |
1010 | 1036 | }, |
|
1022 | 1048 | * @return {boolean} |
1023 | 1049 | */ |
1024 | 1050 | setCropSelection : function(postid, c) { |
1025 | | - var sel; |
| 1051 | + var sel, |
| 1052 | + selW = $( '#imgedit-sel-width-' + postid ), |
| 1053 | + selH = $( '#imgedit-sel-height-' + postid ), |
| 1054 | + sizer = this.hold.sizer, |
| 1055 | + hold = this.hold; |
1026 | 1056 |
|
1027 | 1057 | c = c || 0; |
1028 | 1058 |
|
|
1037 | 1067 | return false; |
1038 | 1068 | } |
1039 | 1069 |
|
1040 | | - sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height }; |
| 1070 | + // adjust the selection within the bounds of the image on 100% scale |
| 1071 | + var excessW = hold.w - ( Math.round( c.x1 / sizer ) + parseInt( selW.val() ) ); |
| 1072 | + var excessH = hold.h - ( Math.round( c.y1 / sizer ) + parseInt( selH.val() ) ); |
| 1073 | + var x = Math.round( c.x1 / sizer ) + Math.min( 0, excessW ); |
| 1074 | + var y = Math.round( c.y1 / sizer ) + Math.min( 0, excessH ); |
| 1075 | + |
| 1076 | + // use 100% scaling to prevent rounding errors |
| 1077 | + sel = { 'r': 1, 'x': x, 'y': y, 'w': selW.val(), 'h': selH.val() }; |
| 1078 | + |
1041 | 1079 | this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1); |
1042 | 1080 | $('#imgedit-selection-' + postid).val( JSON.stringify(sel) ); |
1043 | 1081 | }, |
|
1165 | 1203 | } |
1166 | 1204 | this.closePopup(t); |
1167 | 1205 | this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce); |
| 1206 | + |
| 1207 | + // Clear the selection fields after rotating. |
| 1208 | + $( '#imgedit-sel-width-' + postid ).val( '' ); |
| 1209 | + $( '#imgedit-sel-height-' + postid ).val( '' ); |
| 1210 | + this.currentCropSelection = null; |
1168 | 1211 | }, |
1169 | 1212 |
|
1170 | 1213 | /** |
|
1187 | 1230 | } |
1188 | 1231 | this.closePopup(t); |
1189 | 1232 | this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce); |
| 1233 | + |
| 1234 | + // Clear the selection fields after flipping. |
| 1235 | + $( '#imgedit-sel-width-' + postid ).val( '' ); |
| 1236 | + $( '#imgedit-sel-height-' + postid ).val( '' ); |
| 1237 | + this.currentCropSelection = null; |
1190 | 1238 | }, |
1191 | 1239 |
|
1192 | 1240 | /** |
|
1219 | 1267 | } |
1220 | 1268 |
|
1221 | 1269 | // Clear the selection fields after cropping. |
1222 | | - $('#imgedit-sel-width-' + postid).val(''); |
1223 | | - $('#imgedit-sel-height-' + postid).val(''); |
1224 | | - $('#imgedit-start-x-' + postid).val('0'); |
1225 | | - $('#imgedit-start-y-' + postid).val('0'); |
| 1270 | + $( '#imgedit-sel-width-' + postid ).val( '' ); |
| 1271 | + $( '#imgedit-sel-height-' + postid ).val( '' ); |
| 1272 | + $( '#imgedit-start-x-' + postid ).val( '0' ); |
| 1273 | + $( '#imgedit-start-y-' + postid ).val( '0' ); |
| 1274 | + this.currentCropSelection = null; |
1226 | 1275 | }, |
1227 | 1276 |
|
1228 | 1277 | /** |
|
1312 | 1361 | img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(), |
1313 | 1362 | sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi; |
1314 | 1363 |
|
| 1364 | + this.currentCropSelection = null; |
| 1365 | + |
1315 | 1366 | if ( false === this.validateNumeric( el ) ) { |
1316 | 1367 | return; |
1317 | 1368 | } |
|
1335 | 1386 | if ( x2 > imgw ) { |
1336 | 1387 | x1 = 0; |
1337 | 1388 | x2 = imgw; |
1338 | | - elX.val( Math.round( x2 / sizer ) ); |
| 1389 | + elX.val( Math.min( this.hold.w, Math.round( x2 / sizer ) ) ); |
1339 | 1390 | } |
1340 | 1391 |
|
1341 | 1392 | if ( y2 > imgh ) { |
1342 | 1393 | y1 = 0; |
1343 | 1394 | y2 = imgh; |
1344 | | - elY.val( Math.round( y2 / sizer ) ); |
| 1395 | + elY.val( Math.min( this.hold.h, Math.round( y2 / sizer ) ) ); |
1345 | 1396 | } |
1346 | 1397 |
|
1347 | 1398 | ias.setSelection( x1, y1, x2, y2 ); |
1348 | 1399 | ias.update(); |
1349 | 1400 | this.setCropSelection(postid, ias.getSelection()); |
| 1401 | + this.currentCropSelection = ias.getSelection(); |
1350 | 1402 | } |
1351 | 1403 | }, |
1352 | 1404 |
|
|
0 commit comments