From: Vincent Vanwaelscappel Date: Fri, 17 Mar 2023 07:44:45 +0000 (+0100) Subject: wip #5447 @0.75 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=dd30f04c64f1b7541b75c2ca26dd0d62e29c1761;p=fluidbook-toolbox.git wip #5447 @0.75 --- diff --git a/resources/linkeditor/js/linkeditor.links.js b/resources/linkeditor/js/linkeditor.links.js index 99f784240..32e73799d 100644 --- a/resources/linkeditor/js/linkeditor.links.js +++ b/resources/linkeditor/js/linkeditor.links.js @@ -936,44 +936,62 @@ LinkeditorLinks.prototype = { }, alignSelection: function (align) { + let d = this.getMinMaxSelection(align) if (align === 'left' || align === 'top') { - var min = 1000000; - $.each(this.getCurrentSelection(), function () { - min = Math.min(min, parseFloat($(this).attr('fb-' + align))); - }); - $.each(this.getCurrentSelection(), function () { - $(this).attr('fb-' + align, min); + this.getCurrentSelection().each(function () { + $(this).attr('fb-' + d.side, d.min); }); } else if (align === 'right' || align === 'bottom') { - var b = align === 'right' ? 'left' : 'top'; - var l = align === 'right' ? 'width' : 'height'; - var max = -10000000; - $.each(this.getCurrentSelection(), function () { - max = Math.max(max, parseFloat($(this).attr('fb-' + b)) + parseFloat($(this).attr('fb-' + l))); - }); - $.each(this.getCurrentSelection(), function () { - $(this).attr('fb-' + b, max - parseFloat($(this).attr('fb-' + l))); + this.getCurrentSelection().each(function () { + $(this).attr('fb-' + d.side, d.max - parseFloat($(this).attr('fb-' + d.length))); }); } else if (align === 'center' || align === 'middle') { - var b = align === 'center' ? 'left' : 'top'; - var l = align === 'center' ? 'width' : 'height'; - var max = -10000000; - var min = 1000000; - $.each(this.getCurrentSelection(), function () { - min = Math.min(min, parseFloat($(this).attr('fb-' + b))); - max = Math.max(max, parseFloat($(this).attr('fb-' + b)) + parseFloat($(this).attr('fb-' + l))); - }); - var center = min + ((max - min) / 2); - $.each(this.getCurrentSelection(), function () { - $(this).attr('fb-' + b, center - parseFloat($(this).attr('fb-' + l)) / 2); + let center = d.min + ((d.max - d.min) / 2); + this.getCurrentSelection().each(function () { + $(this).attr('fb-' + d.side, center - parseFloat($(this).attr('fb-' + d.length)) / 2); }); } - this.linkeditor.hasChanged(); }, distributeSelection: function (axis) { - + let d = this.getMinMaxSelection(axis); + let totalLength = d.max - d.min; + let lengthSum = 0; + let links = []; + this.getCurrentSelection().each(function () { + let l = parseFloat($(this).attr('fb-' + d.length)); + links.push({pos: $(this).attr('fb-' + d.side), length: l, link: $(this)}); + lengthSum += l; + }); + let space = (totalLength - lengthSum) / (links.length - 1); + links.sort(function (a, b) { + return a.pos - b.pos; + }); + var s = d.min; + $.each(links, function (k, link) { + $(link.link).attr('fb-' + d.side, s); + s += link.length + space; + }); + this.linkeditor.hasChanged(); }, + + getMinMaxSelection: function (axis) { + if (axis === 'left' || axis === 'right' || axis === 'center') { + axis = 'x'; + } else if (axis === 'top' || axis === 'bottom' || axis === 'middle') { + axis = 'y;' + } + + var b = axis === 'x' ? 'left' : 'top'; + var l = axis === 'x' ? 'width' : 'height'; + var max = -10000000; + var min = 100000000; + this.getCurrentSelection().each(function () { + min = Math.min(min, parseFloat($(this).attr('fb-' + b))); + max = Math.max(max, parseFloat($(this).attr('fb-' + b)) + parseFloat($(this).attr('fb-' + l))); + }); + return {min: min, max: max, side: b, length: l}; + } }; module.exports = LinkeditorLinks;