From d0058cedb909d9261cec1d00e59c32df221d4a46 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Thu, 24 Oct 2019 23:27:43 +0200 Subject: [PATCH] Changes to how crop mode persistence works, extra persistence options --- .../enums/crop-mode-persistence.enum.js | 4 +- src/ext/conf/ActionList.js | 28 ++++++ src/ext/conf/ExtensionConf.js | 88 +++++++++++++++++++ src/ext/lib/Settings.js | 10 +++ src/ext/lib/video-data/PageInfo.js | 36 ++++++-- src/ext/lib/video-transform/Resizer.js | 2 +- 6 files changed, 160 insertions(+), 8 deletions(-) diff --git a/src/common/enums/crop-mode-persistence.enum.js b/src/common/enums/crop-mode-persistence.enum.js index 65bd77c..121eb44 100644 --- a/src/common/enums/crop-mode-persistence.enum.js +++ b/src/common/enums/crop-mode-persistence.enum.js @@ -1,7 +1,9 @@ var CropModePersistence = Object.freeze({ + Default: -1, Disabled: 0, UntilPageReload: 1, - Forever: 2, + CurrentSession: 2, + Forever: 3, }); export default CropModePersistence; \ No newline at end of file diff --git a/src/ext/conf/ActionList.js b/src/ext/conf/ActionList.js index 6457888..c41f906 100644 --- a/src/ext/conf/ActionList.js +++ b/src/ext/conf/ActionList.js @@ -2,6 +2,7 @@ import VideoAlignment from '../../common/enums/video-alignment.enum'; import Stretch from '../../common/enums/stretch.enum'; import ExtensionMode from '../../common/enums/extension-mode.enum'; import AspectRatio from '../../common/enums/aspect-ratio.enum'; +import CropModePersistence from '../../common/enums/crop-mode-persistence.enum'; var ActionList = { 'set-ar': { @@ -30,6 +31,33 @@ var ActionList = { page: true, } }, + 'set-ar-persistence': { + name: 'Set crop mode persistence', + args: [{ + name: 'Never persist', + arg: CropModePersistence.Disabled, + },{ + name: 'While on page', + arg: CropModePersistence.UntilPageReload, + },{ + name: 'Current session', + arg: CropModePersistence.CurrentSession, + },{ + name: 'Always persist', + arg: CropModePersistence.Forever, + }, { + name: 'Default', + arg: CropModePersistence.Default, + scopes: { + site: true, + } + }], + scopes: { + global: true, + site: true, + page: false, + } + }, 'set-stretch': { name: 'Set stretch', args: [{ diff --git a/src/ext/conf/ExtensionConf.js b/src/ext/conf/ExtensionConf.js index 5dc1d9c..73d227a 100644 --- a/src/ext/conf/ExtensionConf.js +++ b/src/ext/conf/ExtensionConf.js @@ -5,6 +5,7 @@ import Stretch from '../../common/enums/stretch.enum'; import ExtensionMode from '../../common/enums/extension-mode.enum'; import AntiGradientMode from '../../common/enums/anti-gradient-mode.enum'; import AspectRatio from '../../common/enums/aspect-ratio.enum'; +import CropModePersistence from '../../common/enums/crop-mode-persistence.enum'; if(Debug.debug) console.log("Loading: ExtensionConf.js"); @@ -364,6 +365,93 @@ var ExtensionConf = { show: true, path: 'crop', } + }, { + name: 'Don\'t persist crop', + label: 'Never persist', + cmd: [{ + action: 'set-ar-persistence', + arg: CropModePersistence.Never, + }], + scopes: { + site: { + show: true, + }, + global: { + show: true, + } + }, + playerUi: { + show: true, + } + }, { + name: 'Persist crop while on page', + label: 'While on page', + cmd: [{ + action: 'set-ar-persistence', + arg: CropModePersistence.UntilPageReload, + }], + scopes: { + site: { + show: true, + }, + global: { + show: true, + } + }, + playerUi: { + show: true, + } + }, { + name: 'Persist crop for current session', + label: 'Always persist', + cmd: [{ + action: 'set-ar-persistence', + arg: CropModePersistence.CurrentSession, + }], + scopes: { + site: { + show: true, + }, + global: { + show: true, + } + }, + playerUi: { + show: true, + } + }, { + name: 'Persist until manually reset', + label: 'Always persist', + cmd: [{ + action: 'set-ar-persistence', + arg: CropModePersistence.Forever, + }], + scopes: { + site: { + show: true, + }, + global: { + show: true, + } + }, + playerUi: { + show: true, + } + }, { + name: 'Default persistence', + label: 'Default', + cmd: [{ + action: 'set-ar-persistence', + arg: CropModePersistence.Default, + }], + scopes: { + site: { + show: true, + }, + }, + playerUi: { + show: true, + } }, { name: 'Zoom in', label: 'Zoom', diff --git a/src/ext/lib/Settings.js b/src/ext/lib/Settings.js index 3918517..1e76a29 100644 --- a/src/ext/lib/Settings.js +++ b/src/ext/lib/Settings.js @@ -6,6 +6,7 @@ import ObjectCopy from '../lib/ObjectCopy'; import Stretch from '../../common/enums/stretch.enum'; import VideoAlignment from '../../common/enums/video-alignment.enum'; import ExtensionConfPatch from '../conf/ExtConfPatches'; +import CropModePersistence from '../../common/enums/crop-mode-persistence.enum'; @@ -515,6 +516,15 @@ class Settings { return this.active.sites['@global'].stretch; } + getDefaultCropPersistenceMode(site) { + if (site && this.active.sites[site] && this.active.sites[site].cropModePersistence !== Stretch.Default) { + return this.active.sites[site].cropModePersistence; + } + + // persistence mode thing is missing from settings by default + return this.active.sites['@global'].cropModePersistence || CropModePersistence.Disabled; + } + getDefaultVideoAlignment(site) { if (site && this.active.sites[site] && this.active.sites[site].videoAlignment !== VideoAlignment.Default) { return this.active.sites[site].videoAlignment; diff --git a/src/ext/lib/video-data/PageInfo.js b/src/ext/lib/video-data/PageInfo.js index fd8fe39..35d2db9 100644 --- a/src/ext/lib/video-data/PageInfo.js +++ b/src/ext/lib/video-data/PageInfo.js @@ -22,6 +22,7 @@ class PageInfo { this.extensionMode = extensionMode; this.readOnly = readOnly; this.defaultCrop = undefined; + this.currentCrop = undefined; if (comms){ this.comms = comms; @@ -34,14 +35,23 @@ class PageInfo { cmd: 'inject-css', cssString: playerStyleString }); + } catch (e) { + // do nothing. It's ok if there's no special settings for the player element or crop persistence + } - // try getting default crop immediately. - if (this.settings.active.sites[window.location.host].cropPersistence === CropModePersistence.Forever) { + // try getting default crop immediately. + const cropModePersistence = this.settings.active.getDefaultCropPersistenceMode(window.location.host); + + try { + if (cropModePersistence === CropModePersistence.Forever) { this.defaultCrop = this.settings.active.sites[window.location.host].defaultCrop; + } else if (cropModePersistence === CropModePersistence.CurrentSession) { + this.defaultCrop = JSON.parse(sessionStorage.getItem('uw-crop-mode-session-persistence')); } } catch (e) { // do nothing. It's ok if there's no special settings for the player element or crop persistence } + this.currentCrop = this.defaultCrop; this.rescan(RescanReason.PERIODIC); this.scheduleUrlCheck(); @@ -565,17 +575,31 @@ class PageInfo { } setDefaultCrop(ar) { + this.currentCrop = ar; // This means crop persistance is disabled. If crop persistance is enabled, then settings for current // site MUST exist (crop persistence mode is disabled by default) - if (!this.settings.active.sites[window.location.host] || - this.settings.active.sites[window.location.host].cropPersistence === CropModePersistence.Disabled) { + + const cropModePersistence = this.settings.active.getDefaultCropPersistenceMode(window.location.host); + + if (cropModePersistence === CropModePersistence.Disabled) { return; } this.defaultCrop = ar; - if (this.settings.active.sites[window.location.host].cropPersistence === CropModePersistence.Forever) { - this.settings.active.sites[window.location.host].defaultAr = ar; + if (cropModePersistence === CropModePersistence.CurrentSession) { + sessionStorage.setItem('uw-crop-mode-session-persistence', JSON.stringify(ar)); + } else if (cropModePersistence === CropModePersistence.Forever) { + if (this.settings.active.sites[window.location.host]) { + // | key may be missing, so we do this + this.settings.active.sites[window.location.host]['defaultAr'] = ar; + } else { + // add options for new site if they're missing! + this.settings.active.sites[window.location.host] = { + defaultAr: ar, + } + } + this.settings.saveWithoutReload(); } } diff --git a/src/ext/lib/video-transform/Resizer.js b/src/ext/lib/video-transform/Resizer.js index 14d164c..d9c35b6 100644 --- a/src/ext/lib/video-transform/Resizer.js +++ b/src/ext/lib/video-transform/Resizer.js @@ -135,7 +135,7 @@ class Resizer { // most everything that could go wrong went wrong by this stage, and returns can happen afterwards // this means here's the optimal place to set or forget aspect ratio - if (siteSettings && siteSettings.cropModePersistance > CropModePersistance.Disabled) { + if (this.settings.getDefaultCropPersistenceMode(window.location.host) > CropModePersistance.Disabled) { if (ar.type === AspectRatio.Automatic || ar.type === AspectRatio.Reset || ar.type === AspectRatio.Initial ) {