Bug 1338094 - [Mortar] Support keyboard copy. f=lchang, r=evelyn
authorRex Lee <rexboy@mozilla.com>
Tue, 14 Feb 2017 11:44:38 +0800
changeset 373034 f39a5e845a1df062b3c2a2e880de691693a05038
parent 373033 ebea9daabb964050c24edaf47a15ad6fe278f3cf
child 373035 7104a8fa9806abebbfb205679266300aca624d9e
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersevelyn
bugs1338094
milestone54.0a1
Bug 1338094 - [Mortar] Support keyboard copy. f=lchang, r=evelyn MozReview-Commit-ID: KvLR3Ajio1n
browser/extensions/mortar/host/pdf/chrome/js/viewport.js
--- a/browser/extensions/mortar/host/pdf/chrome/js/viewport.js
+++ b/browser/extensions/mortar/host/pdf/chrome/js/viewport.js
@@ -1,14 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 'use strict';
 
+const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
+
 class Viewport {
   constructor() {
     this._viewerContainer = document.getElementById('viewerContainer');
     this._fullscreenWrapper = document.getElementById('fullscreenWrapper');
     this._canvasContainer = document.getElementById('canvasContainer');
     this._viewportController = document.getElementById('viewportController');
     this._sizer = document.getElementById('sizer');
 
@@ -34,16 +36,17 @@ class Viewport {
 
     this.onProgressChanged = null;
     this.onZoomChanged = null;
     this.onDimensionChanged = null;
     this.onPageChanged = null;
     this.onPasswordRequest = null;
 
     this._viewportController.addEventListener('scroll', this);
+    this._viewportController.addEventListener('copy', this);
     window.addEventListener('resize', this);
   }
 
   get zoom() {
     return this._zoom;
   }
 
   set zoom(newZoom) {
@@ -470,16 +473,25 @@ class Viewport {
     if (newPage != this._page) {
       this._page = newPage;
       if (typeof this.onPageChanged === 'function') {
         this.onPageChanged(newPage);
       }
     }
   }
 
+  _copyToClipboard(text) {
+    if (!text) {
+      return;
+    }
+    const gClipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"]
+                                .getService(Ci.nsIClipboardHelper);
+    gClipboardHelper.copyString(text);
+  }
+
   verifyPassword(password) {
     this._doAction({
       type: 'getPasswordComplete',
       password: password
     });
   }
 
   handleEvent(evt) {
@@ -490,16 +502,22 @@ class Viewport {
       case 'scroll':
         this._nextPosition = null;
         let position = this.getScrollOffset();
         if (this._runtimePosition.x != position.x ||
             this._runtimePosition.y != position.y) {
           this._refresh();
         }
         break;
+      case 'copy':
+        this._doAction({
+          type: 'getSelectedText'
+        })
+        evt.preventDefault();
+        break;
     }
   }
 
   invokeResize() {
     if (this._fullscreenStatus == 'changing') {
       return;
     }
     this._resize();
@@ -603,11 +621,16 @@ class Viewport {
         this._setDocumentDimensions(message);
         break;
       case 'fullscreenChange':
         this._handleFullscreenChange(message.fullscreen);
         break;
       case 'getPassword':
         this.onPasswordRequest && this.onPasswordRequest();
         break;
+      case 'getSelectedTextReply':
+        // For now this message is used only by text copy so we handle just
+        // that case.
+        this._copyToClipboard(message.selectedText);
+        break;
     }
   }
 }