]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5646 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 21 Feb 2023 10:16:48 +0000 (11:16 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 21 Feb 2023 10:16:48 +0000 (11:16 +0100)
resources/linkeditor/js/linkeditor.links.js

index fcc70c1ed81589753e6bf1dd99f169e0d7f8f38b..9c59fef3193263b01cf0a6ef38d24fd59b3a2827 100644 (file)
@@ -10,6 +10,8 @@ var LinkeditorLinks = function (linkeditor) {
     this.lastSelectedLink = null;
     this.lastSelectedLinkData = {'width': 100, 'height': 100, 'to': '', type: '2', target: '_blank'};
 
+    this.contextMenuPosition = null;
+
     this.init();
 }
 
@@ -85,7 +87,7 @@ LinkeditorLinks.prototype = {
             return false;
         });
         this.key('ctrl+v', function () {
-            $this.pasteInPlace();
+            $this.paste();
             return false;
         });
         this.key('del', function () {
@@ -135,6 +137,9 @@ LinkeditorLinks.prototype = {
         $.contextMenu({
             selector: '#linkeditor-canvas,.link,#linkeditor-panel-layers, #linkeditor-panel-layers label',
             events: {
+                show: function (e) {
+                    $this.contextMenuPosition = {x: $this.linkeditor.mx, y: $this.linkeditor.my};
+                },
                 preShow: function (e) {
                     if ($(e).is('.link:not(.selected)')) {
                         $this.deselectAllLinks();
@@ -212,14 +217,14 @@ LinkeditorLinks.prototype = {
                             isHtmlName: true,
                             name: TRANSLATIONS.paste_here,
                             callback: function () {
-                                $this.pasteHere();
+                                $this.paste($this.contextMenuPosition);
                             },
                         };
                         res.items.paste_in_place = {
                             isHtmlName: true,
                             name: TRANSLATIONS.paste_in_place + ' <kbd>Ctrl+V</kbd>',
                             callback: function () {
-                                $this.pasteInPlace();
+                                $this.paste();
                             },
                         };
                     }
@@ -249,57 +254,83 @@ LinkeditorLinks.prototype = {
         var $this = this;
         key(shortcut, function (event, handler) {
             if ($this.allowsKeyboardShortcut(shortcut)) {
-                scope(event, handler);
+                var res = scope(event, handler);
+                return res;
             }
-        })
+        });
     },
 
     copy: function () {
-        this.copySelectionToClipboard(true);
+        this.copySelectionToClipboard(false);
     },
 
     cut: function () {
-        this.copySelectionToClipboard(false);
+        this.copySelectionToClipboard(true);
     },
 
-    pasteInPlace: function () {
+    paste: function (frommouse) {
         var linksInClipboard = $("#linkeditor-clipboard .link");
         if (!linksInClipboard.length) {
             return;
         }
         var $this = this;
 
+        let offset = {x: 0, y: 0};
+        if (frommouse !== undefined) {
+            // Base mouse position should be the start of context menu (and not the mouse position when we click on the Paste in place item menu)
+            offset = this.linkeditor.globalToFluidbook(frommouse.x, frommouse.y, this.linkeditor.single);
+
+            var top = 1000000;
+            var left = 1000000;
+            // Get the coordinates of the top left corner of the links in clipboard
+            $(linksInClipboard).each(function () {
+                top = Math.min(parseFloat($(this).attr('fb-top')), top);
+                left = Math.min(parseFloat($(this).attr('fb-left')), left);
+            });
+            // The links will be pasted in order the links will be disposed as initially but starting from the mouse current position
+            offset.x -= left;
+            offset.y -= top;
+        }
+
         $(linksInClipboard).each(function () {
-            var item = $(this).clone();
-            if ($this.hasUIDLink($(item).attr('fb-uid'))) {
-                var uid = $this.linkeditor.utils.generateUID();
-                $(item).attr('fb-uid', uid);
-            }
-            $("#linkeditor-links").append(item);
+            let data = $this._duplicateLink($(this), false);
+
+            data.top += offset.y;
+            data.left += offset.x;
+
+            $this.addLink(data);
         });
-    },
 
-    pasteHere: function () {
-        this.pasteInPlace();
+        this.linkeditor.hasChanged();
+        this.updateLayers();
     },
 
     hasUIDLink: function (uid) {
-        return true;
+        return LINKS[uid] !== undefined && LINKS[uid] !== null;
     },
 
-    copySelectionToClipboard: function (clone) {
+    copySelectionToClipboard: function (cut) {
+
         var selection = this.getCurrentSelection();
         if (selection.length === 0) {
             return;
         }
+        var $this = this;
         this.emptyClipboard();
         $.each(selection, function () {
             var item = $(this);
-            if (clone) {
-                item = $(item).clone();
+            clone = $(item).clone();
+            if (cut) {
+                $this.deleteLink(item, false);
             }
-            $("#linkeditor-clipboard").append(item);
+            $("#linkeditor-clipboard").append(clone);
         });
+
+        if (cut) {
+            this.linkeditor.form.emptyForm();
+            this.linkeditor.hasChanged();
+            this.updateLayers();
+        }
     },
 
     emptyClipboard: function () {
@@ -325,7 +356,6 @@ LinkeditorLinks.prototype = {
         });
     },
 
-
     allowsKeyboardShortcut: function (shortcut) {
         if (shortcut === 'pageup' || shortcut === 'pagedown' || shortcut === 'enter') {
             return true;
@@ -684,6 +714,9 @@ LinkeditorLinks.prototype = {
     loadLinks: function (page, side) {
         let $this = this;
         $.each(LINKS, function (uid, link) {
+            if ($('#linkeditor-links [fb-uid="' + uid + '"]').length > 0) {
+                return;
+            }
             if (!$this.linkeditor.single && page % 2 === 1 && link.page % 2 === 0 && link.left > $this.linkeditor.pw) {
                 link.page++;
                 link.left -= $this.linkeditor.pw;
@@ -695,7 +728,7 @@ LinkeditorLinks.prototype = {
                 link.page--;
                 link.left = parseInt(link.left) + $this.linkeditor.pw;
             }
-            $this.addLink(link, side);
+            $this.addLink(link);
         });
         this.updateLayers();
     },
@@ -704,8 +737,13 @@ LinkeditorLinks.prototype = {
         let change = false;
         if (link.uid === undefined) {
             link.uid = this.linkeditor.utils.generateUID();
-            LINKS[link.uid] = link;
             change = true;
+        } else if (!this.hasUIDLink(link.uid)) {
+            console.log('::)', link);
+            change = true;
+        }
+        if (change) {
+            LINKS[link.uid] = link;
         }
 
         link.origuid = link.uid;
@@ -740,20 +778,32 @@ LinkeditorLinks.prototype = {
         });
     },
 
-    _duplicateLink: function () {
-        var data = this.lastSelectedLinkData;
+    _duplicateLink: function (link, pos) {
+        var data;
+        if (link === undefined) {
+            data = this.lastSelectedLinkData;
+        } else {
+            data = this.getLinkData(link);
+        }
         data.page = this.linkeditor.currentPage;
-        var pos = this.linkeditor.globalToFluidbook(this.linkeditor.mx, this.linkeditor.my, this.linkeditor.single);
-        data.left = pos.x;
-        data.top = pos.y;
-        delete data.uid;
+        if (pos === undefined) {
+            pos = this.linkeditor.globalToFluidbook(this.linkeditor.mx, this.linkeditor.my, this.linkeditor.single);
+        }
+        if (pos !== false) {
+            data.left = pos.x;
+            data.top = pos.y;
+        } else {
+            data.left = parseFloat(data.left);
+            data.top = parseFloat(data.top);
+        }
+        if (this.hasUIDLink(data.uid)) {
+            delete data.uid;
+        }
         return data;
     },
 
     duplicateLinkClick: function () {
-        var data = this._duplicateLink();
-
-        let link = this.addLink(data);
+        let link = this.addLink(this._duplicateLink());
 
         this.deselectAllLinks();
         this.selectLink($(link));