diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-05-15 15:11:42 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-15 15:11:42 -0400 |
commit | 8442a8ba22525f45bcbb81e162962f2c97e5d9dc (patch) | |
tree | bfe192c10392b0368e63c4095168a50e36249a12 /ext | |
parent | de6db32aa69be6c33d17ddb4f2dc4305e8771f59 (diff) |
Add SearchPersistentStateController (#1676)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/js/display/search-display-controller.js | 40 | ||||
-rw-r--r-- | ext/js/display/search-main.js | 6 | ||||
-rw-r--r-- | ext/js/display/search-persistent-state-controller.js | 64 | ||||
-rw-r--r-- | ext/search.html | 1 |
4 files changed, 75 insertions, 36 deletions
diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js index d7d79d54..69c59ab3 100644 --- a/ext/js/display/search-display-controller.js +++ b/ext/js/display/search-display-controller.js @@ -21,10 +21,11 @@ */ class SearchDisplayController { - constructor(tabId, frameId, display, japaneseUtil) { + constructor(tabId, frameId, display, japaneseUtil, searchPersistentStateController) { this._tabId = tabId; this._frameId = frameId; this._display = display; + this._searchPersistentStateController = searchPersistentStateController; this._searchButton = document.querySelector('#search-button'); this._queryInput = document.querySelector('#search-textbox'); this._introElement = document.querySelector('#intro'); @@ -44,12 +45,9 @@ class SearchDisplayController { } }); this._messageHandlers = new Map(); - this._mode = null; } async prepare() { - this._updateMode(); - await this._display.updateOptions(); chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); @@ -93,11 +91,11 @@ class SearchDisplayController { // Messages _onMessageSetMode({mode}) { - this._setMode(mode, true); + this._searchPersistentStateController.mode = mode; } _onMessageGetMode() { - return this._mode; + return this._searchPersistentStateController.mode; } // Private @@ -323,7 +321,7 @@ class SearchDisplayController { _updateClipboardMonitorEnabled() { const enabled = this._clipboardMonitorEnabled; this._clipboardMonitorEnableCheckbox.checked = enabled; - if (enabled && this._mode !== 'popup') { + if (enabled && this._searchPersistentStateController.mode !== 'popup') { this._clipboardMonitor.start(); } else { this._clipboardMonitor.stop(); @@ -406,34 +404,6 @@ class SearchDisplayController { } } - _updateMode() { - let mode = null; - try { - mode = sessionStorage.getItem('mode'); - } catch (e) { - // Browsers can throw a SecurityError when cookie blocking is enabled. - } - this._setMode(mode, false); - } - - _setMode(mode, save) { - if (mode === this._mode) { return; } - if (save) { - try { - if (mode === null) { - sessionStorage.removeItem('mode'); - } else { - sessionStorage.setItem('mode', mode); - } - } catch (e) { - // Browsers can throw a SecurityError when cookie blocking is enabled. - } - } - this._mode = mode; - document.documentElement.dataset.searchMode = (mode !== null ? mode : ''); - this._updateClipboardMonitorEnabled(); - } - _isElementInput(element) { if (element === null) { return false; } switch (element.tagName.toLowerCase()) { diff --git a/ext/js/display/search-main.js b/ext/js/display/search-main.js index 056d4788..04886bc8 100644 --- a/ext/js/display/search-main.js +++ b/ext/js/display/search-main.js @@ -21,6 +21,7 @@ * HotkeyHandler * JapaneseUtil * SearchDisplayController + * SearchPersistentStateController * wanakana */ @@ -29,6 +30,9 @@ const documentFocusController = new DocumentFocusController('#search-textbox'); documentFocusController.prepare(); + const searchPersistentStateController = new SearchPersistentStateController(); + searchPersistentStateController.prepare(); + await yomichan.prepare(); const {tabId, frameId} = await yomichan.api.frameInformationGet(); @@ -41,7 +45,7 @@ const display = new Display(tabId, frameId, 'search', japaneseUtil, documentFocusController, hotkeyHandler); await display.prepare(); - const searchDisplayController = new SearchDisplayController(tabId, frameId, display, japaneseUtil); + const searchDisplayController = new SearchDisplayController(tabId, frameId, display, japaneseUtil, searchPersistentStateController); await searchDisplayController.prepare(); display.initializeState(); diff --git a/ext/js/display/search-persistent-state-controller.js b/ext/js/display/search-persistent-state-controller.js new file mode 100644 index 00000000..56beebc7 --- /dev/null +++ b/ext/js/display/search-persistent-state-controller.js @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +class SearchPersistentStateController { + constructor() { + this._mode = null; + } + + get mode() { + return this._mode; + } + + set mode(value) { + this._setMode(value, true); + } + + prepare() { + this._updateMode(); + } + + // Private + + _updateMode() { + let mode = null; + try { + mode = sessionStorage.getItem('mode'); + } catch (e) { + // Browsers can throw a SecurityError when cookie blocking is enabled. + } + this._setMode(mode, false); + } + + _setMode(mode, save) { + if (mode === this._mode) { return; } + if (save) { + try { + if (mode === null) { + sessionStorage.removeItem('mode'); + } else { + sessionStorage.setItem('mode', mode); + } + } catch (e) { + // Browsers can throw a SecurityError when cookie blocking is enabled. + } + } + this._mode = mode; + document.documentElement.dataset.searchMode = (mode !== null ? mode : ''); + this._updateClipboardMonitorEnabled(); + } +} diff --git a/ext/search.html b/ext/search.html index 41f3b92b..578e1763 100644 --- a/ext/search.html +++ b/ext/search.html @@ -93,6 +93,7 @@ <script src="/js/display/option-toggle-hotkey-handler.js"></script> <script src="/js/display/query-parser.js"></script> <script src="/js/display/search-display-controller.js"></script> +<script src="/js/display/search-persistent-state-controller.js"></script> <script src="/js/dom/document-focus-controller.js"></script> <script src="/js/dom/document-util.js"></script> <script src="/js/dom/dom-text-scanner.js"></script> |