From e12c48b73f26e1a2e9b6bc234ea9be950e51403b Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Wed, 7 Sep 2016 14:52:52 +0200
Subject: [PATCH] Add a `enhanceTextSelection` preference

Please note that this is a hack, but I think that it should be OK for now to atleast get the preference landed. Refer to the code comment for further information.

Re: issue 7584 and PR 7586.
---
 extensions/chromium/preferences_schema.json |  4 ++++
 web/app.js                                  | 15 +++++++++++++--
 web/default_preferences.json                |  1 +
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/extensions/chromium/preferences_schema.json b/extensions/chromium/preferences_schema.json
index 36f2766a3..baa3300c4 100644
--- a/extensions/chromium/preferences_schema.json
+++ b/extensions/chromium/preferences_schema.json
@@ -95,6 +95,10 @@
       "description": "Whether to prevent the extension from reporting the extension and browser version to the extension developers.",
       "default": false
     },
+    "enhanceTextSelection": {
+      "type": "boolean",
+      "default": false
+    },
     "renderInteractiveForms": {
       "type": "boolean",
       "default": false
diff --git a/web/app.js b/web/app.js
index 81e440db7..79a3e7040 100644
--- a/web/app.js
+++ b/web/app.js
@@ -101,7 +101,6 @@ var SCALE_SELECT_CONTAINER_PADDING = 8;
 var SCALE_SELECT_PADDING = 22;
 var PAGE_NUMBER_LOADING_INDICATOR = 'visiblePageIsLoading';
 var DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000;
-var ENHANCE_TEXT_SELECTION = false;
 
 function configure(PDFJS) {
   PDFJS.imageResourcesPath = './images/';
@@ -211,7 +210,7 @@ var PDFViewerApplication = {
       renderingQueue: pdfRenderingQueue,
       linkService: pdfLinkService,
       downloadManager: downloadManager,
-      enhanceTextSelection: ENHANCE_TEXT_SELECTION,
+      enhanceTextSelection: false,
     });
     pdfRenderingQueue.setViewer(this.pdfViewer);
     pdfLinkService.setViewer(this.pdfViewer);
@@ -323,6 +322,18 @@ var PDFViewerApplication = {
       Preferences.get('defaultZoomValue').then(function resolved(value) {
         self.preferenceDefaultZoomValue = value;
       }),
+      Preferences.get('enhanceTextSelection').then(function resolved(value) {
+        // TODO: Move the initialization and fetching of `Preferences` to occur
+        //       before the various viewer components are initialized.
+        //
+        // This was attempted in: https://github.com/mozilla/pdf.js/pull/7586,
+        // but it had to be backed out since it violated implicit assumptions
+        // about some viewer components being synchronously available.
+        //
+        // NOTE: This hack works since the `enhanceTextSelection` option is not
+        //       needed until `PDFViewer.setDocument` has been called.
+        self.pdfViewer.enhanceTextSelection = value;
+      }),
       Preferences.get('disableTextLayer').then(function resolved(value) {
         if (PDFJS.disableTextLayer === true) {
           return;
diff --git a/web/default_preferences.json b/web/default_preferences.json
index 6102064e7..242c30b12 100644
--- a/web/default_preferences.json
+++ b/web/default_preferences.json
@@ -12,5 +12,6 @@
   "disableTextLayer": false,
   "useOnlyCssZoom": false,
   "externalLinkTarget": 0,
+  "enhanceTextSelection": false,
   "renderInteractiveForms": false
 }