]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5479 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 22 Sep 2022 17:01:00 +0000 (19:01 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 22 Sep 2022 17:01:00 +0000 (19:01 +0200)
resources/linkeditor/js/linkeditor.js
resources/linkeditor/js/linkeditor.links.js
resources/linkeditor/js/linkeditor.rulers.js
resources/linkeditor/js/linkeditor.utils.js
resources/linkeditor/js/linkeditor.zoom.js
resources/linkeditor/style/rulers.sass

index 7d8ffb453cd030402f322dd9ddcc3ff892032642..2a7827654a34a540b992f3828ffc1b54cb33ec9a 100644 (file)
@@ -236,6 +236,21 @@ LinkEditor.prototype = {
         this.loader.preloadPages();
     },
 
+    getCurrentPages: function (page) {
+        if (page === undefined) {
+            page = this.currentPage;
+        } else {
+            page = this.utils.normalizePage(page);
+        }
+        if (this.single) {
+            return [page];
+        }
+        if (page % 2 === 1) {
+            page--;
+        }
+        return [page, page + 1];
+    },
+
 
 // Convert global coordinates to fluidbook ones
     globalToFluidbook: function (x, y, onePage) {
index d444ac3ac42fae76c8f0db4435bef4db38ef23e4..db2373aa6c96995c9807b8b48e9d4ae4a7a639dc 100644 (file)
@@ -5,12 +5,15 @@ var LinkeditorLinks = function (linkeditor) {
     this.dragLinkPos = null;
     this.resizeLinkPos = null;
 
+    this.magnetValuesX = [];
+    this.magnetValuesY = [];
+
     this.init();
 }
 
 LinkeditorLinks.prototype = {
     init: function () {
-        var $this = this;
+        let $this = this;
         $(document).on('mousedown', '.link .corners div', function (e) {
             e.preventDefault();
             e.stopPropagation();
@@ -84,11 +87,11 @@ LinkeditorLinks.prototype = {
         if (this.resizeLinkPos === null) {
             return;
         }
-        var f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
-        var dx = (this.linkeditor.mx - this.resizeLinkPos.x) * f;
-        var dy = (this.linkeditor.my - this.resizeLinkPos.y) * f;
+        let f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
+        let dx = (this.linkeditor.mx - this.resizeLinkPos.x) * f;
+        let dy = (this.linkeditor.my - this.resizeLinkPos.y) * f;
 
-        var top = 0, left = 0, width = 0, height = 0;
+        let top = 0, left = 0, width = 0, height = 0;
 
         if (['n', 'ne', 'nw'].indexOf(this.resizeLinkPos.corner) >= 0) {
             top = dy;
@@ -104,14 +107,14 @@ LinkeditorLinks.prototype = {
         }
 
         $(".link.selected").each(function () {
-            var newWidth = $(this).data('drag-orig-width') + width;
-            var newHeight = $(this).data('drag-orig-height') + height;
+            let newWidth = $(this).data('drag-orig-width') + width;
+            let newHeight = $(this).data('drag-orig-height') + height;
 
             if (key.ctrl) {
                 // Keep ratio
-                var ratio = $(this).data('drag-orig-width') / $(this).data('drag-orig-height');
-                var xscale = newWidth / $(this).data('drag-orig-width');
-                var yscale = newHeight / $(this).data('drag-orig-height');
+                let ratio = $(this).data('drag-orig-width') / $(this).data('drag-orig-height');
+                let xscale = newWidth / $(this).data('drag-orig-width');
+                let yscale = newHeight / $(this).data('drag-orig-height');
                 if (['ne', 'nw', 'se', 'sw'].indexOf(this.resizeLinkPos.corner) >= 0) {
                     if (xscale < yscale) {
                         newHeight = newWidth / ratio;
@@ -139,8 +142,8 @@ LinkeditorLinks.prototype = {
                 }
             }
 
-            var newLeft = $(this).data('drag-orig-left') + left;
-            var newTop = $(this).data('drag-orig-top') + top;
+            let newLeft = $(this).data('drag-orig-left') + left;
+            let newTop = $(this).data('drag-orig-top') + top;
 
             $(this).attr('fb-left', newLeft)
                 .attr('fb-top', newTop)
@@ -156,6 +159,8 @@ LinkeditorLinks.prototype = {
         $(".link.selected").each(function () {
             $(this).data('drag-orig-left', parseFloat($(this).attr('fb-left')));
             $(this).data('drag-orig-top', parseFloat($(this).attr('fb-top')));
+            $(this).data('drag-orig-width', parseFloat($(this).attr('fb-width')));
+            $(this).data('drag-orig-height', parseFloat($(this).attr('fb-height')));
         });
     },
 
@@ -168,11 +173,15 @@ LinkeditorLinks.prototype = {
         if (this.dragLinkPos === null) {
             return;
         }
-        var f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
-        var dx = (this.linkeditor.mx - this.dragLinkPos.x) * f;
-        var dy = (this.linkeditor.my - this.dragLinkPos.y) * f;
+
+        let $this = this;
+        let f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
+        let dx = (this.linkeditor.mx - this.dragLinkPos.x) * f;
+        let dy = (this.linkeditor.my - this.dragLinkPos.y) * f;
         $(".link.selected").each(function () {
-            $(this).attr('fb-left', $(this).data('drag-orig-left') + dx).attr('fb-top', $(this).data('drag-orig-top') + dy).attr('fb-update', '1');
+            $(this).attr('fb-left', $this.linkeditor.utils.magnetize($(this).data('drag-orig-left') + dx, $this.magnetValuesX, $(this).data('drag-orig-width')))
+                .attr('fb-top', $this.linkeditor.utils.magnetize($(this).data('drag-orig-top') + dy, $this.magnetValuesY, $(this).data('drag-orig-height')))
+                .attr('fb-update', '1');
         });
         this.linkeditor.updateFBElements(false);
     },
@@ -193,7 +202,7 @@ LinkeditorLinks.prototype = {
 
     offsetSelectedLinks: function (dim, value) {
         $('.link.selected').each(function () {
-            var v = parseFloat($(this).attr('fb-' + dim));
+            let v = parseFloat($(this).attr('fb-' + dim));
             $(this).attr('fb-' + dim, v + value).attr('fb-update', '1');
         });
         this.linkeditor.updateFBElements();
@@ -220,7 +229,7 @@ LinkeditorLinks.prototype = {
     },
 
     loadLinks: function (page, side) {
-        var $this = this;
+        let $this = this;
         $.each(LINKS, function (uid, link) {
             if (link.page != page) {
                 return;
@@ -233,7 +242,7 @@ LinkeditorLinks.prototype = {
     },
 
     addLink: function (link) {
-        var $this = this;
+        let $this = this;
         if (link.uid === undefined) {
             link.uid = generateUID();
             LINKS[link.uid] = link;
@@ -241,7 +250,7 @@ LinkeditorLinks.prototype = {
             this.linkeditor.hasChanged();
         }
 
-        var attrs = {};
+        let attrs = {};
         $.each(link, function (k, v) {
             if ($this.linkeditor.dimensionProperties.indexOf(k) >= 0) {
                 attrs['fb-' + k] = v;
@@ -250,11 +259,23 @@ LinkeditorLinks.prototype = {
             }
         });
 
-        var e = $('<div class="link" fb-ref="editor" fb-update="1"></div>');
+        let e = $('<div class="link" fb-ref="editor" fb-update="1"></div>');
         $(e).attr(attrs);
         $("#linkeditor-links").append(e);
     },
 
+    updateMagnetValues: function () {
+        let $this = this;
+        this.magnetValuesX = [];
+        this.magnetValuesY = [];
+        $.each(this.linkeditor.rulers.getRulersOfPage(), function (uid, ruler) {
+            if (ruler.type === 'y') {
+                $this.magnetValuesY.push(ruler.pos);
+            } else {
+                $this.magnetValuesX.push(ruler.pos);
+            }
+        });
+    },
 
     clear: function () {
         $("#linkeditor-links").html('');
index 014d16c1371ee732de7a85afec3657462d38113e..62ef11bcd0954c4e599ffc556449d780b7068227 100644 (file)
@@ -35,15 +35,24 @@ LinkeditorRulers.prototype = {
 
     loadRulers: function (page, side) {
         let $this = this;
+        $.each(this.getRulersOfPage(page), function (uid, ruler) {
+            $this.addRuler(ruler.type, ruler.pos, ruler.uid);
+        });
+        this.linkeditor.links.updateMagnetValues();
+    },
+
+    getRulersOfPage(page) {
+        var pages = this.linkeditor.getCurrentPages(page);
+        var res = {};
         $.each(RULERS, function (uid, ruler) {
-            if (ruler.page != page) {
+            if (pages.indexOf(parseInt(ruler.page)) === -1) {
                 return;
             }
-            $this.addRuler(ruler.type, ruler.pos, ruler.uid);
+            res[uid] = ruler;
         });
+        return res;
     },
 
-
     updateRulers: function () {
         // Update rects
         this.linkeditor.fluidbookRect = $("#linkeditor-fluidbook").get(0).getBoundingClientRect();
@@ -194,6 +203,7 @@ LinkeditorRulers.prototype = {
         $(this.movingRuler).css(css).attr(attrs);
         RULERS[$(this.movingRuler).data('uid')].pos = fbv;
         this.linkeditor.updateFBElements();
+        this.linkeditor.links.updateMagnetValues();
     },
 
     addRuler: function (axis, pos, uid) {
@@ -220,6 +230,7 @@ LinkeditorRulers.prototype = {
         delete RULERS[$(ruler).data('uid')];
         $(ruler).remove();
         this.movingRuler = null;
+        this.linkeditor.links.updateMagnetValues();
         this.linkeditor.hasChanged();
     },
 
index f5122f7e21fbb2bc70d728017cd9c6b29866a320..e7f10942ff6c532f06548d2a00ed959f4f2e2a00 100644 (file)
@@ -44,10 +44,15 @@ LinkeditorUtils.prototype = {
         return result;
     },
 
-    magnetize: function (value, values) {
+    magnetize: function (value, values, width) {
+        if (width === undefined) {
+            width = 0;
+        }
         let sensibility = 8 / (this.linkeditor.zoom.zoom * this.linkeditor.fs);
         let min = 100000;
         let magnetValue;
+        let magnetizeOtherSide = false;
+
         if (values.length === 0) {
             return value;
         }
@@ -55,16 +60,27 @@ LinkeditorUtils.prototype = {
         for (let i in values) {
             let v = values[i];
             let diff = Math.abs(v - value);
+            let diffOtherSide = Math.abs(v - (value + width));
             if (diff < min) {
                 min = diff;
                 magnetValue = v;
+                magnetizeOtherSide = false;
+            }
+            if (diffOtherSide < min) {
+                min = diffOtherSide;
+                magnetValue = v;
+                magnetizeOtherSide = true;
             }
         }
 
         if (min > sensibility) {
             return value;
         }
-        return magnetValue;
+        if (magnetizeOtherSide) {
+            return magnetValue - width;
+        } else {
+            return magnetValue;
+        }
     },
 };
 
index 13abe497c19cd83146308b8cc7fcb242ce26ba77..ce1a00cfcb4717815c84cd3999847c6475367636 100644 (file)
@@ -12,7 +12,7 @@ LinkeditorZoom.prototype = {
 
         $("#linkeditor-main").on('wheel', function (e) {
             let delta = e.originalEvent.deltaY;
-            if (delta == 0) {
+            if (delta === 0) {
                 return true;
             }
             e.stopPropagation();
index b00e2553add2b110bbbe2497205ffbfa49cf382f..52478a6414a53800d29ac34b0a72c29e554b6a77 100644 (file)
@@ -4,7 +4,7 @@
     position: absolute
     top: 0
     left: 0
-    z-index: 600
+    z-index: 400
     border-width: 0
     border-color: #0ff
     border-style: solid