Skip to content

Commit

Permalink
Merge pull request #28 from eduardomozart/patch-2
Browse files Browse the repository at this point in the history
Add image alignment support
  • Loading branch information
tillbiskup committed Feb 2, 2024
2 parents 26b2f08 + 05dee20 commit 7932794
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions script.js
@@ -0,0 +1,60 @@
jQuery(document).ready(function() {
if (JSINFO.ACT === "admin") {return;}
jQuery('.plugin_caption_figure').each(function(i,e) {
let figureElem = jQuery(this).find("img").first();
let figureCaptionElem = jQuery(this).find("figcaption").first();

getImageSize(figureElem, function(width, height){
figureElemWidth = width;
if (figureElem.attr("width") !== 0 && figureElem.attr("width") !== undefined && figureElem.attr("width") !== false) {
figureElemWidth = figureElem.attr('width');
}

let figureClassList = figureElem.attr('class').split(/\s+/);
jQuery(figureClassList).each(function(index){
switch(figureClassList[index]) {
case 'medialeft':
figureCaptionElem.addClass("medialeft");
figureCaptionElem.css("width", figureElemWidth);
break;
case 'mediaright':
figureCaptionElem.addClass("mediaright");
figureCaptionElem.css("width", figureElemWidth);
break;
}
});
});
});
});

function getImageSize(img, callback){
img = jQuery(img);

var wait = setInterval(function(){
var w = img.width(),
h = img.height();

if(w && h){
done(w, h);
}
}, 0);

var onLoad;
img.on('load', onLoad = function(){
done(img.width(), img.height());
});


var isDone = false;
function done(){
if(isDone){
return;
}
isDone = true;

clearInterval(wait);
img.off('load', onLoad);

callback.apply(this, arguments);
}
}

0 comments on commit 7932794

Please sign in to comment.