Browse Source

clipboard access added

Michael 10 years ago
parent
commit
8c86b25c46

+ 39 - 2
src/allplatforms/classes/Clipboard.js

@@ -1,15 +1,52 @@
 SC.loadPackage({ 'Clipboard': {
 
-    comment: 'I manage the clipboard for the editing tool.',
+    comment: 'I am a proxy to the clipboard, which is managed by the add-on platform specific code.',
 
 
+    properties: {
+
+        copyField:  { comment: 'I hold a hidden textinput DOM element, which copies text to the clipboard' },
+
+        pasteField: { comment: 'I hold a hidden textinput DOM element, which pastes text from the clipboard' }
+
+    },
+
 
     methods: {
 
         init: { 
-            comment:    'method comment',
+            comment:    'I init the proxy to system clipboard.',
             code:       function(){
 
+                this.set({
+                    copyField:  document.querySelector('#sg-editing-clipboard-copy'),
+                    pasteField: document.querySelector('#sg-editing-clipboard-paste')
+                })
+
+            }
+        },
+
+        copy: {
+            comment: 'I copy things to the system clipboard.',
+            code: function(data){
+
+                this.get('copyField').value = data;
+                this.get('copyField').click();
+
+            }
+        },
+
+        paste: {
+            comment: 'I paste things from the system clipboard.',
+            code: function(callback){
+
+                this.get('pasteField').click();
+
+                var self = this;
+                window.setTimeout(function(){
+                    callback.call(self, self.get('pasteField').value)
+                }, 500)
+
             }
         }
 

+ 37 - 0
src/allplatforms/classes/MenuItemPaste.js

@@ -19,7 +19,44 @@ SC.loadPackage({ 'MenuItemPaste': {
                 this.delegate('MenuItem', 'init', theDocumentMenu);
                 this.set({ isActionButton: true });
 
+                this.get('menuButton').addEventListener('mouseup', function(){
 
+                    SuperGlue.get('clipboard').do('paste', function(pasteData){
+
+                        var domNodes, newNode, newChild, offset;
+
+                        try{
+                            domNodes = (new DOMParser()).parseFromString(pasteData, 'text/html').body.children;
+                            domNodes = Array.prototype.slice.call(domNodes);
+                        }catch(e){
+                            return;
+                        }
+
+
+                        offset = SuperGlue.get('document').get('grid').get('active') 
+                                    ? SuperGlue.get('document').get('grid').get('gridSize')
+                                    : 10;
+
+                        for(var i = 0, l = domNodes.length; i < l; i++){
+
+                            newNode = domNodes[i]
+                            SuperGlue.get('document').get('pageContainer').appendChild(newNode);
+
+                            newChild = SC.do('Element', 'awakeFromDOM', newNode)
+                            newChild.set({
+                                top:    newChild.get('top')  + offset,
+                                left:   newChild.get('left') + offset
+                            })
+
+                            SuperGlue.get('document').get('children').push(newChild);
+                            SuperGlue.get('document').do('afterLayoutHasChanged');
+
+                        }
+
+
+                    });
+
+                }, false)
 
     		}
 

+ 15 - 0
src/allplatforms/classes/WidgetCopy.js

@@ -20,6 +20,21 @@ SC.loadPackage({ 'WidgetCopy': {
                 this.set({ isActionButton: true });
 
 
+                this.get('widgetButton').addEventListener('mouseup', function(){
+
+                    var elements   = theSelection.get('elements'),
+                        copyString = '';
+
+                    for(var i = 0, l = elements.length; i < l; i++){
+
+                        copyString += elements[i].do('renderYourself', { indent: 1 }) + '\n';
+
+                    }
+
+                    SuperGlue.get('clipboard').do('copy', copyString);
+
+
+                }, false)
 
 
     		}

+ 16 - 0
src/allplatforms/injections/clipboard.css

@@ -0,0 +1,16 @@
+
+#sg-editing-clipboard-copy {
+	position: 	fixed;
+	top:		-100px;
+	left:		0px;
+	width:		10px;
+	height: 	10px;
+}
+
+#sg-editing-clipboard-paste {
+	position: 	fixed;
+	top:		-80px;
+	left:		0px;
+	width:		10px;
+	height: 	10px;
+}

+ 0 - 6
src/allplatforms/injections/fileManager.js

@@ -1,6 +0,0 @@
-/*
-* I am injected by the browser add-on to start the fileManager
-*
-*/
-
-SuperGlue.get('fileManager').do('toggleOnOff');

+ 0 - 6
src/allplatforms/injections/notSGpage.js

@@ -1,6 +0,0 @@
-/*
-* I am injected by the browser add-on to connect to a SuperGlue server and start then a new page
-*
-*/
-
-alert('This site was not made with SuperGlue, so you can\'t change it.\n\nGo to the adress of your own SuperGlue mini-server to start!\nYou may find help at www.superglue.it')

+ 35 - 2
src/chromium/background.js

@@ -1,11 +1,44 @@
 
 var powerSwitch = false;
 
+
+
 chrome.runtime.onMessage.addListener(
     function(request, sender, sendResponse) {
-        if (request.contentProbeForSG === "isPowerOn?"){
+
+        if(request.contentProbeForSG === "isPowerOn?"){
             sendResponse({ powerOn: powerSwitch });
-        }   
+        }
+
+        if(request.action === 'copy'){
+
+            var input = document.createElement('textarea');
+            document.body.appendChild(input);
+            input.value = request.value;
+            input.focus();
+            input.select();
+            document.execCommand('copy');
+            input.remove();
+
+        }
+
+        if(request.action === 'paste'){
+
+            var input = document.createElement('textarea');
+            document.body.appendChild(input);
+            input.value = request.value;
+            input.focus();
+            input.select();
+            document.execCommand('paste');
+            sendResponse({
+                action: 'pasteResponse',
+                value:  input.value
+            });
+            input.remove();
+            
+        }
+
+
     }
 );
 

+ 44 - 0
src/chromium/content_probeForSG.js

@@ -142,6 +142,50 @@
 
     var activateSuperGlue = function(){
 
+
+
+
+        var clipboardCss = document.createElement("link");
+        clipboardCss.setAttribute("data-superglue", "editing-interface");
+        clipboardCss.setAttribute("rel", "stylesheet");
+        clipboardCss.setAttribute("href", chrome.extension.getURL("superglue-client//injections/clipboard.css"));
+        document.head.appendChild(clipboardCss);
+
+        var clipboardCopy = document.createElement("textarea");
+        clipboardCopy.setAttribute("data-superglue", "editing-interface");
+        clipboardCopy.setAttribute("id", "sg-editing-clipboard-copy");
+        clipboardCopy.addEventListener('click', function(){
+            chrome.runtime.sendMessage(
+                { 
+                    action: "copy",
+                    value:  clipboardCopy.value
+                }, 
+                function(response){}
+            );
+        }, false);
+        document.body.insertBefore(clipboardCopy, document.body.firstElementChild);
+
+        var clipboardPaste = document.createElement("textarea");
+        clipboardPaste.setAttribute("data-superglue", "editing-interface");
+        clipboardPaste.setAttribute("id", "sg-editing-clipboard-paste");
+        clipboardPaste.addEventListener('click', function(){
+            chrome.runtime.sendMessage(
+                { 
+                    action: "paste",
+                }, 
+                function(response){
+                    if(response.action === 'pasteResponse'){
+                        clipboardPaste.value = response.value;
+                        clipboardPaste.click();
+                    }
+                }
+            );
+        }, false);
+        document.body.insertBefore(clipboardPaste, document.body.firstElementChild);
+
+
+
+
         for (var i = 0; i < scriptsToInject.length; i++) {
             var script = document.createElement("script");
             script.setAttribute("data-superglue", "editing-interface");

+ 1 - 1
src/chromium/manifest.json

@@ -7,7 +7,7 @@
     "author": "The SuperGlue Team (2014). www.superglue.it",
     "version": "1.0",
 
-    "permissions": [ "tabs", "activeTab", "<all_urls>" ],
+    "permissions": [ "tabs", "activeTab", "<all_urls>", "clipboardRead", "clipboardWrite" ],
 
     "web_accessible_resources": [ "*" ],
 

+ 35 - 0
src/firefox/data/content_probeForSG.js

@@ -140,6 +140,41 @@
 
     var activateSuperGlue = function(){
 
+
+        var clipboardCss = document.createElement("link");
+        clipboardCss.setAttribute("data-superglue", "editing-interface");
+        clipboardCss.setAttribute("rel", "stylesheet");
+        clipboardCss.setAttribute("href", self.options.dataPath + "injections/clipboard.css" );
+        document.head.appendChild(clipboardCss);
+
+        var clipboardCopy = document.createElement("textarea");
+        clipboardCopy.setAttribute("data-superglue", "editing-interface");
+        clipboardCopy.setAttribute("id", "sg-editing-clipboard-copy");
+        clipboardCopy.addEventListener('click', function(){
+            self.port.emit('SuperGlueClipboard', { 
+                action: 'copy',
+                value:  this.value
+            });
+        }, false);
+        document.body.insertBefore(clipboardCopy, document.body.firstElementChild);
+
+        var clipboardPaste = document.createElement("textarea");
+        clipboardPaste.setAttribute("data-superglue", "editing-interface");
+        clipboardPaste.setAttribute("id", "sg-editing-clipboard-paste");
+        clipboardPaste.addEventListener('click', function(){
+            self.port.emit('SuperGlueClipboard', { action: 'paste' });
+        }, false);
+        document.body.insertBefore(clipboardPaste, document.body.firstElementChild);
+        self.port.on('SuperGlueClipboard', function(message){
+            if(message.action === 'pasteResponse'){
+                clipboardPaste.value = message.value;
+                clipboardPaste.click()
+            }
+        });
+
+        
+
+
         for (var i = 0; i < scriptsToInject.length; i++) {
             var script = document.createElement("script");
             script.setAttribute("data-superglue", "editing-interface");

+ 16 - 0
src/firefox/lib/main.js

@@ -3,6 +3,7 @@ var buttons     = require('sdk/ui/button/action'),
     tabs        = require('sdk/tabs'),
     self        = require('sdk/self'),
     pageMod     = require('sdk/page-mod'),
+    clipboard   = require('sdk/clipboard'),
     iconsOn     =   {
                         '16': './icon-16-powerOn.png',
                         '32': './icon-32-powerOn.png',
@@ -40,6 +41,21 @@ pageMod.PageMod({
             }
         });
 
+        worker.port.on('SuperGlueClipboard', function(message){
+
+            if(message.action === 'copy'){
+                clipboard.set(message.value);
+            }
+
+            if(message.action === 'paste'){
+                worker.port.emit('SuperGlueClipboard', {
+                    action: 'pasteResponse',
+                    value:  clipboard.get()
+                })
+            }
+
+        });
+
         worker.on('detach', function () {
             detachWorker(this, workers);
         });