Add SearchPersistentStateController (#1676)

This commit is contained in:
toasted-nutbread 2021-05-15 15:11:42 -04:00 committed by GitHub
parent de6db32aa6
commit 8442a8ba22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 36 deletions

View File

@ -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()) {

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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>