Skip to content

Commit

Permalink
[CRX] Make textLayerMode pref visible and add migration logic
Browse files Browse the repository at this point in the history
In a1cfa5f, the textLayerMode
preference was introduced, to replace the disableTextLayer and
enhanceTextSelection preferences.

As a result, the text selection preference was no longer visible
in Chrome (because preferences are only rendered by default for
boolean preferences, not for enumerations).

This commit adds the necessary bits to
extensions/chromium/options/options.{html,js}
so that the textLayerMode preference can be changed again.

Also, migration logic has been added to move over preferences
from the old to the new names:
- In web/chromecom.js, the logic is added to translate
  preferences that were set by an administrator (it is read-only,
  so this layer is unavoidable).
- In extensions/chromium/options/migration.js, similar logic is
  added, except in this case the preference storage is writable,
  so this migration logic happens only once.
  • Loading branch information
Rob--W committed Feb 16, 2018
1 parent 4fdb364 commit 394a716
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
20 changes: 20 additions & 0 deletions extensions/chromium/options/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ limitations under the License.
storageSync.get([
'enableHandToolOnLoad',
'cursorToolOnLoad',
'disableTextLayer',
'enhanceTextSelection',
'textLayerMode',
], function(items) {
// Migration code for https://github.com/mozilla/pdf.js/pull/7635.
if (typeof items.enableHandToolOnLoad === 'boolean') {
Expand All @@ -93,6 +96,23 @@ limitations under the License.
storageSync.remove('enableHandToolOnLoad');
}
}
// Migration code for https://github.com/mozilla/pdf.js/pull/9479.
if (typeof items.disableTextLayer === 'boolean') {
var textLayerMode = items.disableTextLayer ? 0 :
items.enhanceTextSelection ? 2 : 1;
if (textLayerMode !== 1) {
// Overwrite if computed textLayerMode is not the default value (1).
storageSync.set({
textLayerMode: textLayerMode,
}, function() {
if (!chrome.runtime.lastError) {
storageSync.remove(['disableTextLayer', 'enhanceTextSelection']);
}
});
} else {
storageSync.remove(['disableTextLayer', 'enhanceTextSelection']);
}
}
});
}
})();
13 changes: 13 additions & 0 deletions extensions/chromium/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@
</div>
</template>

<template id="textLayerMode-template">
<div class="settings-row">
<label>
<span></span>
<select>
<option value="0">Disable text selection</option>
<option value="1">Enable text selection</option>
<option value="2">Enable enhanced text selection</option>
</select>
</label>
</div>
</template>

<template id="externalLinkTarget-template">
<div class="settings-row">
<label>
Expand Down
19 changes: 19 additions & 0 deletions extensions/chromium/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Promise.all([
renderPreference = renderSidebarViewOnLoad(prefSchema.title);
} else if (prefName === 'cursorToolOnLoad') {
renderPreference = renderCursorToolOnLoad(prefSchema.title);
} else if (prefName === 'textLayerMode') {
renderPreference = renderTextLayerMode(prefSchema.title);
} else if (prefName === 'externalLinkTarget') {
renderPreference = renderExternalLinkTarget(prefSchema.title);
} else {
Expand Down Expand Up @@ -217,6 +219,23 @@ function renderCursorToolOnLoad(shortDescription) {
return renderPreference;
}

function renderTextLayerMode(shortDescription) {
var wrapper = importTemplate('textLayerMode-template');
var select = wrapper.querySelector('select');
select.onchange = function() {
storageArea.set({
textLayerMode: parseInt(this.value),
});
};
wrapper.querySelector('span').textContent = shortDescription;
document.getElementById('settings-boxes').appendChild(wrapper);

function renderPreference(value) {
select.value = value;
}
return renderPreference;
}

function renderExternalLinkTarget(shortDescription) {
var wrapper = importTemplate('externalLinkTarget-template');
var select = wrapper.querySelector('select');
Expand Down
10 changes: 10 additions & 0 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
"type": "boolean",
"default": false
},
"disableTextLayer": {
"description": "Deprecated. Set textLayerMode to 0 to disable the text selection layer by default.",
"type": "boolean",
"default": false
},
"enhanceTextSelection": {
"description": "Deprecated. Set textLayerMode to 2 to use the enhanced text selection layer by default.",
"type": "boolean",
"default": false
},
"textLayerMode": {
"title": "Text layer mode",
"description": "Controls if the text layer is enabled, and the selection mode that is used.\n 0 = Disabled.\n 1 = Enabled.\n 2 = (Experimental) Enabled, with enhanced text selection.",
Expand Down
11 changes: 11 additions & 0 deletions web/chromecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ class ChromePreferences extends BasePreferences {
// compatibility with managed preferences.
var defaultManagedPrefs = Object.assign({
enableHandToolOnLoad: false,
disableTextLayer: false,
enhanceTextSelection: false,
}, this.defaults);

chrome.storage.managed.get(defaultManagedPrefs, function(items) {
Expand All @@ -327,6 +329,15 @@ class ChromePreferences extends BasePreferences {
items.cursorToolOnLoad = 1;
}
delete items.enableHandToolOnLoad;
if (items.textLayerMode !== 1) {
if (items.disableTextLayer) {
items.textLayerMode = 0;
} else if (items.enhanceTextSelection) {
items.textLayerMode = 2;
}
}
delete items.disableTextLayer;
delete items.enhanceTextSelection;
getPreferences(items);
});
} else {
Expand Down

0 comments on commit 394a716

Please sign in to comment.