Add SearchPersistentStateController (#1676)
This commit is contained in:
parent
de6db32aa6
commit
8442a8ba22
@ -21,10 +21,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class SearchDisplayController {
|
class SearchDisplayController {
|
||||||
constructor(tabId, frameId, display, japaneseUtil) {
|
constructor(tabId, frameId, display, japaneseUtil, searchPersistentStateController) {
|
||||||
this._tabId = tabId;
|
this._tabId = tabId;
|
||||||
this._frameId = frameId;
|
this._frameId = frameId;
|
||||||
this._display = display;
|
this._display = display;
|
||||||
|
this._searchPersistentStateController = searchPersistentStateController;
|
||||||
this._searchButton = document.querySelector('#search-button');
|
this._searchButton = document.querySelector('#search-button');
|
||||||
this._queryInput = document.querySelector('#search-textbox');
|
this._queryInput = document.querySelector('#search-textbox');
|
||||||
this._introElement = document.querySelector('#intro');
|
this._introElement = document.querySelector('#intro');
|
||||||
@ -44,12 +45,9 @@ class SearchDisplayController {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
this._messageHandlers = new Map();
|
this._messageHandlers = new Map();
|
||||||
this._mode = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
this._updateMode();
|
|
||||||
|
|
||||||
await this._display.updateOptions();
|
await this._display.updateOptions();
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
|
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
|
||||||
@ -93,11 +91,11 @@ class SearchDisplayController {
|
|||||||
// Messages
|
// Messages
|
||||||
|
|
||||||
_onMessageSetMode({mode}) {
|
_onMessageSetMode({mode}) {
|
||||||
this._setMode(mode, true);
|
this._searchPersistentStateController.mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessageGetMode() {
|
_onMessageGetMode() {
|
||||||
return this._mode;
|
return this._searchPersistentStateController.mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
@ -323,7 +321,7 @@ class SearchDisplayController {
|
|||||||
_updateClipboardMonitorEnabled() {
|
_updateClipboardMonitorEnabled() {
|
||||||
const enabled = this._clipboardMonitorEnabled;
|
const enabled = this._clipboardMonitorEnabled;
|
||||||
this._clipboardMonitorEnableCheckbox.checked = enabled;
|
this._clipboardMonitorEnableCheckbox.checked = enabled;
|
||||||
if (enabled && this._mode !== 'popup') {
|
if (enabled && this._searchPersistentStateController.mode !== 'popup') {
|
||||||
this._clipboardMonitor.start();
|
this._clipboardMonitor.start();
|
||||||
} else {
|
} else {
|
||||||
this._clipboardMonitor.stop();
|
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) {
|
_isElementInput(element) {
|
||||||
if (element === null) { return false; }
|
if (element === null) { return false; }
|
||||||
switch (element.tagName.toLowerCase()) {
|
switch (element.tagName.toLowerCase()) {
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
* HotkeyHandler
|
* HotkeyHandler
|
||||||
* JapaneseUtil
|
* JapaneseUtil
|
||||||
* SearchDisplayController
|
* SearchDisplayController
|
||||||
|
* SearchPersistentStateController
|
||||||
* wanakana
|
* wanakana
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -29,6 +30,9 @@
|
|||||||
const documentFocusController = new DocumentFocusController('#search-textbox');
|
const documentFocusController = new DocumentFocusController('#search-textbox');
|
||||||
documentFocusController.prepare();
|
documentFocusController.prepare();
|
||||||
|
|
||||||
|
const searchPersistentStateController = new SearchPersistentStateController();
|
||||||
|
searchPersistentStateController.prepare();
|
||||||
|
|
||||||
await yomichan.prepare();
|
await yomichan.prepare();
|
||||||
|
|
||||||
const {tabId, frameId} = await yomichan.api.frameInformationGet();
|
const {tabId, frameId} = await yomichan.api.frameInformationGet();
|
||||||
@ -41,7 +45,7 @@
|
|||||||
const display = new Display(tabId, frameId, 'search', japaneseUtil, documentFocusController, hotkeyHandler);
|
const display = new Display(tabId, frameId, 'search', japaneseUtil, documentFocusController, hotkeyHandler);
|
||||||
await display.prepare();
|
await display.prepare();
|
||||||
|
|
||||||
const searchDisplayController = new SearchDisplayController(tabId, frameId, display, japaneseUtil);
|
const searchDisplayController = new SearchDisplayController(tabId, frameId, display, japaneseUtil, searchPersistentStateController);
|
||||||
await searchDisplayController.prepare();
|
await searchDisplayController.prepare();
|
||||||
|
|
||||||
display.initializeState();
|
display.initializeState();
|
||||||
|
64
ext/js/display/search-persistent-state-controller.js
Normal file
64
ext/js/display/search-persistent-state-controller.js
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -93,6 +93,7 @@
|
|||||||
<script src="/js/display/option-toggle-hotkey-handler.js"></script>
|
<script src="/js/display/option-toggle-hotkey-handler.js"></script>
|
||||||
<script src="/js/display/query-parser.js"></script>
|
<script src="/js/display/query-parser.js"></script>
|
||||||
<script src="/js/display/search-display-controller.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-focus-controller.js"></script>
|
||||||
<script src="/js/dom/document-util.js"></script>
|
<script src="/js/dom/document-util.js"></script>
|
||||||
<script src="/js/dom/dom-text-scanner.js"></script>
|
<script src="/js/dom/dom-text-scanner.js"></script>
|
||||||
|
Loading…
Reference in New Issue
Block a user