Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Add SearchPersistentStateController #1676

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 5 additions & 35 deletions ext/js/display/search-display-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()) {
Expand Down
6 changes: 5 additions & 1 deletion ext/js/display/search-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* HotkeyHandler
* JapaneseUtil
* SearchDisplayController
* SearchPersistentStateController
* wanakana
*/

Expand All @@ -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();
Expand All @@ -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();
Expand Down
64 changes: 64 additions & 0 deletions ext/js/display/search-persistent-state-controller.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions ext/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ <h1>Yomichan Search</h1>
<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>
Expand Down