diff --git a/src/index.js b/assets/js/simple-local-avatars.js similarity index 93% rename from src/index.js rename to assets/js/simple-local-avatars.js index e1b2966c..dd7573d5 100644 --- a/src/index.js +++ b/assets/js/simple-local-avatars.js @@ -23,11 +23,11 @@ jQuery(document).ready(function ($) { if (avatar_working) { return; } + /** * Setup Crop control * The controls used by WordPress Admin are api.CroppedImageControl and api.SiteIconControl. */ - const cropControl = { id: 'control-id', params: { @@ -43,15 +43,15 @@ jQuery(document).ready(function ($) { * * @param {boolean} flexW * @param {boolean} flexH - * @param {number} dstW - * @param {number} dstH + * @param {number} distW + * @param {number} distH * @param {number} imgW * @param {number} imgH * @returns {boolean} */ - cropControl.mustBeCropped = function (flexW, flexH, dstW, dstH, imgW, imgH) { + cropControl.mustBeCropped = function (flexW, flexH, distW, distH, imgW, imgH) { // Skip cropping if the image matches the crop dimension. - if (imgW === dstW && imgH === distH) { + if (imgW === distW && imgH === distH) { return false; } @@ -64,7 +64,6 @@ jQuery(document).ready(function ($) { * simple_local_avatar_frame.setState('library'); * simple_local_avatar_frame.open(); */ - simple_local_avatar_frame = wp.media({ button: { text: i10n_SimpleLocalAvatars.selectCrop, // l10n.selectAndCrop, @@ -92,7 +91,6 @@ jQuery(document).ready(function ($) { * * @param {object} croppedImage Cropped attachment data. */ - simple_local_avatar_frame.on('cropped', function (croppedImage) { const { url } = croppedImage; const attachmentId = croppedImage.id; @@ -157,7 +155,7 @@ jQuery(document).ready(function ($) { _wpnonce: i10n_SimpleLocalAvatars.deleteNonce, }) .done(function (data) { - if (data != '') { + if (data !== '') { avatar_container.innerHTML = data; $('#simple-local-avatar-remove').hide(); avatar_ratings.disabled = true; @@ -171,7 +169,6 @@ jQuery(document).ready(function ($) { /** * Update the Local Avatar image */ - avatar_input.on('change', function (event) { avatar_preview.attr('srcset', ''); avatar_preview.attr('height', 'auto'); @@ -185,15 +182,19 @@ jQuery(document).ready(function ($) { }); }); +/** + * Lock or unlock the avatar editing + * @param {string} lock_or_unlock + */ function avatar_lock(lock_or_unlock) { - if (undefined == avatar_spinner) { + if (undefined === avatar_spinner) { avatar_ratings = document.getElementById('simple-local-avatar-ratings'); avatar_spinner = jQuery(document.getElementById('simple-local-avatar-spinner')); avatar_container = document.getElementById('simple-local-avatar-photo'); avatar_form_button = jQuery(avatar_ratings).closest('form').find('input[type=submit]'); } - if (lock_or_unlock == 'unlock') { + if (lock_or_unlock === 'unlock') { avatar_working = false; avatar_form_button.removeAttr('disabled'); avatar_spinner.hide(); @@ -243,7 +244,7 @@ function simple_local_avatar_calculate_image_select_options(attachment, controll const x1 = (realWidth - xInit) / 2; const y1 = (realHeight - yInit) / 2; - const imgSelectOptions = { + return { handles: true, keys: true, instance: true, @@ -258,8 +259,6 @@ function simple_local_avatar_calculate_image_select_options(attachment, controll y2: yInit + y1, aspectRatio: `${xInit}:${yInit}`, }; - - return imgSelectOptions; } /** @@ -300,7 +299,7 @@ function simple_local_avatar_set_image_from_url(url, attachmentId, width, height _wpnonce: i10n_SimpleLocalAvatars.mediaNonce, }) .done(function (data) { - if (data != '') { + if (data !== '') { avatar_container.innerHTML = data; jQuery('#simple-local-avatar-remove').show(); avatar_ratings.disabled = false; @@ -311,6 +310,11 @@ function simple_local_avatar_set_image_from_url(url, attachmentId, width, height }); } +/** + * Set the avatar image, once it is selected from the media library. + * + * @param {object} attachment + */ function simple_local_avatar_set_image_from_attachment(attachment) { avatar_lock('lock'); jQuery @@ -321,7 +325,7 @@ function simple_local_avatar_set_image_from_attachment(attachment) { _wpnonce: i10n_SimpleLocalAvatars.mediaNonce, }) .done(function (data) { - if (data != '') { + if (data !== '') { avatar_container.innerHTML = data; jQuery('#simple-local-avatar-remove').show(); avatar_ratings.disabled = false; @@ -333,4 +337,4 @@ function simple_local_avatar_set_image_from_attachment(attachment) { }); } -/* eslint-enable no-undef, eqeqeq, no-use-before-define */ +/* eslint-enable no-undef, eqeqeq, no-use-before-define */ \ No newline at end of file diff --git a/build/index.asset.php b/build/index.asset.php deleted file mode 100644 index b2bb54d5..00000000 --- a/build/index.asset.php +++ /dev/null @@ -1 +0,0 @@ - array('wp-polyfill'), 'version' => 'eff1e5b5aa39e7e2022821c13ae1ddfa'); \ No newline at end of file diff --git a/build/index.js b/build/index.js deleted file mode 100644 index dea7eda7..00000000 --- a/build/index.js +++ /dev/null @@ -1 +0,0 @@ -!function(){let e,t,a,i,n,o,l,r,s,c=!1;function d(e){null==t&&(a=document.getElementById("simple-local-avatar-ratings"),t=jQuery(document.getElementById("simple-local-avatar-spinner")),i=document.getElementById("simple-local-avatar-photo"),n=jQuery(a).closest("form").find("input[type=submit]")),"unlock"==e?(c=!1,n.removeAttr("disabled"),t.hide()):(c=!0,n.attr("disabled","disabled"),t.show())}function p(e,t){const a=t.get("control"),i=e.get("width"),n=e.get("height");let o=parseInt(a.params.width,10),l=parseInt(a.params.height,10);const r=o/l;t.set("canSkipCrop",!a.mustBeCropped(!1,!1,o,l,i,n));const s=o,c=l;i/n>r?(l=n,o=l*r):(o=i,l=o/r);const d=(i-o)/2,p=(n-l)/2;return{handles:!0,keys:!0,instance:!0,persistent:!0,imageWidth:i,imageHeight:n,minWidth:s>o?o:s,minHeight:c>l?l:c,x1:d,y1:p,x2:o+d,y2:l+p,aspectRatio:`${o}:${l}`}}function m(e,t,n,o){const l={};l.url=e,l.thumbnail_url=e,l.timestamp=_.now(),t&&(l.attachment_id=t),n&&(l.width=n),o&&(l.height=o),d("lock"),jQuery.post(ajaxurl,{action:"assign_simple_local_avatar_media",media_id:t,user_id:i10n_SimpleLocalAvatars.user_id,_wpnonce:i10n_SimpleLocalAvatars.mediaNonce}).done((function(e){""!=e&&(i.innerHTML=e,jQuery("#simple-local-avatar-remove").show(),a.disabled=!1)})).always((function(){d("unlock")}))}jQuery(document).ready((function(t){l=t("#simple-local-avatar"),o=t("#simple-local-avatar-photo img"),s=o.attr("src"),a=t("#simple-local-avatar-ratings"),i=t("#simple-local-avatar-photo"),t("#simple-local-avatar-media").on("click",(function(t){if(t.preventDefault(),c)return;const n={id:"control-id",params:{flex_width:!1,flex_height:!1,width:200,height:200},mustBeCropped:function(e,t,a,i,n,o){return n!==a||o!==distH}};e=wp.media({button:{text:i10n_SimpleLocalAvatars.selectCrop,close:!1},states:[new wp.media.controller.Library({title:i10n_SimpleLocalAvatars.selectCrop,library:wp.media.query({type:"image"}),multiple:!1,date:!1,priority:20,suggestedWidth:200,suggestedHeight:200}),new wp.media.controller.CustomizeImageCropper({imgSelectOptions:p,control:n})]}),e.on("cropped",(function(e){const{url:t}=e;m(t,e.id,e.width,e.height)})),e.on("skippedcrop",(function(e){const t=e.get("url"),a=e.get("width"),i=e.get("height");m(t,e.id,a,i)})),e.on("select",(function(){const t=e.state().get("selection").first().toJSON();var o;n.params.width!==t.width||n.params.height!==t.height||n.params.flex_width||n.params.flex_height?e.setState("cropper"):(t.dst_width=t.width,t.dst_height=t.height,o=t,d("lock"),jQuery.post(ajaxurl,{action:"assign_simple_local_avatar_media",media_id:o.id,user_id:i10n_SimpleLocalAvatars.user_id,_wpnonce:i10n_SimpleLocalAvatars.mediaNonce}).done((function(e){""!=e&&(i.innerHTML=e,jQuery("#simple-local-avatar-remove").show(),a.disabled=!1,d("unlock"))})).always((function(){d("unlock")})),e.close())})),e.open()})),t("#simple-local-avatar-remove").on("click",(function(e){e.preventDefault(),c||(d("lock"),t.get(ajaxurl,{action:"remove_simple_local_avatar",user_id:i10n_SimpleLocalAvatars.user_id,_wpnonce:i10n_SimpleLocalAvatars.deleteNonce}).done((function(e){""!=e&&(i.innerHTML=e,t("#simple-local-avatar-remove").hide(),a.disabled=!0)})).always((function(){d("unlock")})))})),l.on("change",(function(e){o.attr("srcset",""),o.attr("height","auto"),URL.revokeObjectURL(r),e.target.files.length>0?(r=URL.createObjectURL(e.target.files[0]),o.attr("src",r)):o.attr("src",s)}))}))}(); \ No newline at end of file diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 9e4e59ef..4e0d1780 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -272,7 +272,7 @@ public function admin_enqueue_scripts( $hook_suffix ) { $this->remove_nonce = wp_create_nonce( 'remove_simple_local_avatar_nonce' ); $script_name_append = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.dev' : ''; - wp_enqueue_script( 'simple-local-avatars', plugins_url( '', dirname( __FILE__ ) ) . '/build/index' . $script_name_append . '.js', array( 'jquery' ), false, true ); + wp_enqueue_script( 'simple-local-avatars', plugins_url( '', dirname( __FILE__ ) ) . '/dist/simple-local-avatars' . $script_name_append . '.js', array( 'jquery' ), false, true ); wp_localize_script( 'simple-local-avatars', 'i10n_SimpleLocalAvatars', diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 00000000..fbf1c655 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,13 @@ +const defaultConfig = require( '@wordpress/scripts/config/webpack.config' ); +const path = require( 'path' ); + +module.exports = { + ...defaultConfig, + output: { + ...defaultConfig.output, + path: path.resolve( process.cwd(), 'dist' ), + }, + entry: { + 'simple-local-avatars': path.resolve( process.cwd(), 'assets/js', 'simple-local-avatars.js' ), + } +};