]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5984 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 2 Jun 2023 13:29:52 +0000 (15:29 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 2 Jun 2023 13:29:52 +0000 (15:29 +0200)
resources/linkeditor/js/linkeditor.clipboard.js [new file with mode: 0644]
resources/linkeditor/js/linkeditor.js
resources/linkeditor/js/linkeditor.links.js
resources/linkeditor/js/linkeditor.utils.js

diff --git a/resources/linkeditor/js/linkeditor.clipboard.js b/resources/linkeditor/js/linkeditor.clipboard.js
new file mode 100644 (file)
index 0000000..63e1401
--- /dev/null
@@ -0,0 +1,77 @@
+function LinkeditorClipboard(linkeditor) {
+    this.linkeditor = linkeditor;
+    this.content = '';
+    this._isEmpty = true;
+
+    this.init();
+}
+
+LinkeditorClipboard.prototype = {
+    init: function () {
+
+    },
+
+    enabled: function () {
+        return !this.linkeditor.utils.isFirefox();
+    },
+
+    empty: function () {
+        this.set('');
+    },
+
+    get: function (callback) {
+        let $this = this;
+        if (this.enabled()) {
+            navigator.clipboard.read()
+                .then(items => {
+                    items[0].getType('text/html').then(blob => {
+                        blob.text().then(text => {
+                            if (text.indexOf('<fluidbooklinks>') === 0) {
+                                $this.content = text;
+                                callback($this.content);
+                            }
+                        });
+                    });
+                })
+                .catch(err => {
+                    console.error('Failed to read clipboard contents: ', err);
+                });
+        } else {
+            callback(this.content);
+        }
+    },
+
+    isEmtpy: function () {
+        if (!this.enabled()) {
+            return this.content === '';
+        }
+        return this._isEmpty;
+    },
+
+    checkEmpty: function () {
+        if (!this.enabled()) {
+            return;
+        }
+    },
+
+    set: function (content) {
+        this.content = content;
+
+        if (this.enabled()) {
+            const type = "text/html";
+            const blob = new Blob(['<fluidbooklinks>' + this.content + '</fluidbooklinks>'], {type});
+            const data = [new ClipboardItem({[type]: blob})];
+
+            navigator.clipboard.write(data).then(
+                () => {
+                    /* success */
+                },
+                () => {
+                    /* failure */
+                }
+            );
+        }
+    },
+}
+
+module.exports = LinkeditorClipboard;
index 0bc0178e81a9c7c8619ed4b8866ced0d2717c8a6..444928afd410a137001fcf3d0cb2c9d5186a7cbd 100644 (file)
@@ -20,6 +20,7 @@ import LinkeditorVersions from './linkeditor.versions';
 import LinkeditorPopup from './linkeditor.popup';
 import LinkeditorLayers from "./linkeditor.layers";
 import LinkeditorUndo from './linkeditor.undo';
+import LinkeditorClipboard from './linkeditor.clipboard';
 
 window.$ = window.jQuery = require('jquery');
 window.key = require('keymaster-reloaded');
@@ -88,6 +89,7 @@ LinkEditor.prototype = {
     init: function () {
         var $this = this;
 
+        this.clipboard = new LinkeditorClipboard(this);
         this.utils = new LinkeditorUtils(this);
         this.toolbar = new LinkeditorToolbar(this);
         this.resize = new LinkeditorResize(this);
index 7fce58c6ea2102f3983bd6a5c902dd12a56c5304..262f02d392b7214cefe532960972daf1048a7789 100644 (file)
@@ -223,7 +223,6 @@ LinkeditorLinks.prototype = {
                 var selection = $(".link.selected");
                 var multiple = selection.length > 1;
                 var hasSelection = selection.length > 0;
-                var hasClipboard = $("#linkeditor-clipboard").find('.link').length > 0;
 
                 res.items = {
                     'select_all': {
@@ -366,7 +365,16 @@ LinkeditorLinks.prototype = {
     },
 
     paste: function (frommouse) {
-        var linksInClipboard = $("#linkeditor-clipboard .link");
+        let $this = this;
+        this.linkeditor.clipboard.get(function (content) {
+            $this._paste(content, frommouse);
+        })
+    },
+
+    _paste: function (content, frommouse) {
+        var clipboard = $('<div />').html(content);
+
+        var linksInClipboard = clipboard.find(".link");
         if (!linksInClipboard.length) {
             return;
         }
@@ -413,20 +421,22 @@ LinkeditorLinks.prototype = {
             return;
         }
         var $this = this;
-        this.emptyClipboard();
 
         let nb = 0;
 
+        var clipboardContent = $('<div />');
         $.each(selection, function () {
             var item = $(this);
             clone = $(item).clone();
             if (cut) {
                 $this.deleteLink(item, false);
             }
-            $("#linkeditor-clipboard").append(clone);
+            clipboardContent.append(clone);
             nb++;
         });
 
+        this.linkeditor.clipboard.set(clipboardContent.html());
+
         if (cut) {
             this.linkeditor.form.emptyForm();
             this.linkeditor.hasChanged();
@@ -438,9 +448,6 @@ LinkeditorLinks.prototype = {
         this.linkeditor.notification(msg);
     },
 
-    emptyClipboard: function () {
-        $("#linkeditor-clipboard").html('');
-    },
 
     fixDriftedLinks: function () {
         var $this = this;
index 00e5c175838e9dd88cb7847a8e2f1f597adfd01c..e8b2b5d1b6bd9338f9f7617ef5417a12cde3bd94 100644 (file)
@@ -146,6 +146,10 @@ LinkeditorUtils.prototype = {
     },
     isfocusOnFormItem: function () {
         return $(document.activeElement).is('input[type="text"],input[type="email"],input[type="number"],input[type="tel"],input[type="search"],textarea,select');
+    },
+
+    isFirefox: function () {
+        return navigator.userAgent.search("Firefox") >= 0;
     }
 };