From: Vincent Vanwaelscappel Date: Fri, 2 Jun 2023 13:29:52 +0000 (+0200) Subject: wip #5984 @0.25 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=93a431ca2699956bae6ce87c0b49fc7f495b8d0d;p=fluidbook-toolbox.git wip #5984 @0.25 --- diff --git a/resources/linkeditor/js/linkeditor.clipboard.js b/resources/linkeditor/js/linkeditor.clipboard.js new file mode 100644 index 000000000..63e14016a --- /dev/null +++ b/resources/linkeditor/js/linkeditor.clipboard.js @@ -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('') === 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(['' + this.content + ''], {type}); + const data = [new ClipboardItem({[type]: blob})]; + + navigator.clipboard.write(data).then( + () => { + /* success */ + }, + () => { + /* failure */ + } + ); + } + }, +} + +module.exports = LinkeditorClipboard; diff --git a/resources/linkeditor/js/linkeditor.js b/resources/linkeditor/js/linkeditor.js index 0bc0178e8..444928afd 100644 --- a/resources/linkeditor/js/linkeditor.js +++ b/resources/linkeditor/js/linkeditor.js @@ -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); diff --git a/resources/linkeditor/js/linkeditor.links.js b/resources/linkeditor/js/linkeditor.links.js index 7fce58c6e..262f02d39 100644 --- a/resources/linkeditor/js/linkeditor.links.js +++ b/resources/linkeditor/js/linkeditor.links.js @@ -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 = $('
').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 = $('
'); $.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; diff --git a/resources/linkeditor/js/linkeditor.utils.js b/resources/linkeditor/js/linkeditor.utils.js index 00e5c1758..e8b2b5d1b 100644 --- a/resources/linkeditor/js/linkeditor.utils.js +++ b/resources/linkeditor/js/linkeditor.utils.js @@ -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; } };