]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5447 @0.75
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 17 Mar 2023 07:44:45 +0000 (08:44 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 17 Mar 2023 07:44:45 +0000 (08:44 +0100)
resources/linkeditor/js/linkeditor.links.js

index 99f784240688bbb744e9c9bed741e2b90a953139..32e73799d9c79fffb583731c2396011b2149dc53 100644 (file)
@@ -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;