2017-03-05 01:56:10 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2016-2020 Yomichan Authors
|
2017-03-05 01:56:10 +00:00
|
|
|
*
|
|
|
|
* 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
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-03-05 01:56:10 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* ClipboardMonitor
|
|
|
|
* Display
|
2020-08-10 01:07:11 +00:00
|
|
|
* DocumentUtil
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-04-21 02:18:37 +00:00
|
|
|
* wanakana
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2020-02-01 20:00:34 +00:00
|
|
|
|
2017-08-15 02:55:04 +00:00
|
|
|
class DisplaySearch extends Display {
|
2020-12-28 22:41:59 +00:00
|
|
|
constructor(japaneseUtil, documentFocusController) {
|
|
|
|
super('search', japaneseUtil, documentFocusController);
|
2020-11-08 17:34:23 +00:00
|
|
|
this._searchButton = document.querySelector('#search-button');
|
|
|
|
this._queryInput = document.querySelector('#search-textbox');
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement = document.querySelector('#intro');
|
|
|
|
this._clipboardMonitorEnableCheckbox = document.querySelector('#clipboard-monitor-enable');
|
|
|
|
this._wanakanaEnableCheckbox = document.querySelector('#wanakana-enable');
|
2020-11-08 17:34:23 +00:00
|
|
|
this._queryInputEvents = new EventListenerCollection();
|
|
|
|
this._wanakanaEnabled = false;
|
2020-03-14 00:51:39 +00:00
|
|
|
this._isPrepared = false;
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introVisible = true;
|
|
|
|
this._introAnimationTimer = null;
|
2020-08-10 01:03:11 +00:00
|
|
|
this._clipboardMonitorEnabled = false;
|
2020-07-03 19:58:29 +00:00
|
|
|
this._clipboardMonitor = new ClipboardMonitor({
|
2020-11-29 18:09:02 +00:00
|
|
|
japaneseUtil,
|
2020-09-20 19:10:57 +00:00
|
|
|
clipboardReader: {
|
|
|
|
getText: async () => (await api.clipboardGet())
|
|
|
|
}
|
2020-07-03 16:02:21 +00:00
|
|
|
});
|
2020-02-27 00:34:25 +00:00
|
|
|
this._onKeyDownIgnoreKeys = new Map([
|
|
|
|
['ANY_MOD', new Set([
|
2020-11-22 02:17:39 +00:00
|
|
|
'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End', 'Enter', 'Escape',
|
2020-02-27 00:34:25 +00:00
|
|
|
'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10',
|
|
|
|
'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20',
|
|
|
|
'F21', 'F22', 'F23', 'F24'
|
|
|
|
])],
|
|
|
|
['Control', new Set(['C', 'A', 'Z', 'Y', 'X', 'F', 'G'])],
|
|
|
|
['Meta', new Set(['C', 'A', 'Z', 'Y', 'X', 'F', 'G'])],
|
2020-02-29 15:00:28 +00:00
|
|
|
['OS', new Set()],
|
|
|
|
['Alt', new Set()],
|
|
|
|
['AltGraph', new Set()],
|
|
|
|
['Shift', new Set()]
|
2020-02-27 00:34:25 +00:00
|
|
|
]);
|
2020-11-22 20:29:51 +00:00
|
|
|
this.autoPlayAudioDelay = 0;
|
2019-10-12 18:20:54 +00:00
|
|
|
}
|
2019-08-03 12:06:28 +00:00
|
|
|
|
2019-10-12 18:20:54 +00:00
|
|
|
async prepare() {
|
2020-06-21 20:14:05 +00:00
|
|
|
await super.prepare();
|
|
|
|
await this.updateOptions();
|
2020-11-24 01:31:48 +00:00
|
|
|
yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this));
|
2020-07-24 20:03:11 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
this.on('contentUpdating', this._onContentUpdating.bind(this));
|
2020-08-09 17:11:41 +00:00
|
|
|
this.on('modeChange', this._onModeChange.bind(this));
|
|
|
|
|
|
|
|
this.registerMessageHandlers([
|
|
|
|
['updateSearchQuery', {async: false, handler: this._onExternalSearchUpdate.bind(this)}]
|
|
|
|
]);
|
2020-07-26 20:51:54 +00:00
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
this.queryParserVisible = true;
|
2020-07-26 20:51:54 +00:00
|
|
|
this.setHistorySettings({useBrowserHistory: true});
|
2020-07-24 20:03:11 +00:00
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
const enableWanakana = !!this.getOptions().general.enableWanakana;
|
|
|
|
this._wanakanaEnableCheckbox.checked = enableWanakana;
|
|
|
|
this._setWanakanaEnabled(enableWanakana);
|
2020-02-05 11:26:25 +00:00
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
this._searchButton.addEventListener('click', this._onSearch.bind(this), false);
|
|
|
|
this._wanakanaEnableCheckbox.addEventListener('change', this._onWanakanaEnableChange.bind(this));
|
2020-07-03 19:58:29 +00:00
|
|
|
window.addEventListener('copy', this._onCopy.bind(this));
|
|
|
|
this._clipboardMonitor.on('change', this._onExternalSearchUpdate.bind(this));
|
2020-08-10 01:03:11 +00:00
|
|
|
this._clipboardMonitorEnableCheckbox.addEventListener('change', this._onClipboardMonitorEnableChange.bind(this));
|
2020-02-09 23:16:06 +00:00
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
this._onModeChange();
|
2019-10-26 13:39:43 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
this.initializeState();
|
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
this._isPrepared = true;
|
2017-03-05 01:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:10:37 +00:00
|
|
|
onEscape() {
|
2020-08-10 01:03:11 +00:00
|
|
|
if (this._queryInput === null) {
|
2019-09-15 16:12:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
this._queryInput.focus();
|
|
|
|
this._queryInput.select();
|
2017-08-06 02:11:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
onKeyDown(e) {
|
2020-08-10 01:07:11 +00:00
|
|
|
const key = DocumentUtil.getKeyFromEvent(e);
|
2020-07-03 19:58:29 +00:00
|
|
|
const ignoreKeys = this._onKeyDownIgnoreKeys;
|
2019-10-26 11:30:36 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
const activeModifierMap = new Map([
|
|
|
|
['Control', e.ctrlKey],
|
|
|
|
['Meta', e.metaKey],
|
|
|
|
['Shift', e.shiftKey],
|
|
|
|
['Alt', e.altKey],
|
|
|
|
['ANY_MOD', true]
|
|
|
|
]);
|
|
|
|
|
|
|
|
let preventFocus = false;
|
|
|
|
for (const [modifier, keys] of ignoreKeys.entries()) {
|
|
|
|
const modifierActive = activeModifierMap.get(modifier);
|
|
|
|
if (key === modifier || (modifierActive && keys.has(key))) {
|
|
|
|
preventFocus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
if (!super.onKeyDown(e) && !preventFocus && document.activeElement !== this._queryInput) {
|
|
|
|
this._queryInput.focus({preventScroll: true});
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
postProcessQuery(query) {
|
2020-11-08 17:34:23 +00:00
|
|
|
if (this._wanakanaEnabled) {
|
2020-08-01 20:22:00 +00:00
|
|
|
try {
|
2020-11-29 18:09:02 +00:00
|
|
|
query = this._japaneseUtil.convertToKana(query);
|
2020-08-01 20:22:00 +00:00
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
// Private
|
2020-07-24 18:54:54 +00:00
|
|
|
|
2020-11-24 01:31:48 +00:00
|
|
|
async _onOptionsUpdated() {
|
|
|
|
await this.updateOptions();
|
|
|
|
const query = this._queryInput.value;
|
|
|
|
if (query) {
|
2020-12-22 00:19:59 +00:00
|
|
|
this.searchLast();
|
2020-11-24 01:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:22:00 +00:00
|
|
|
_onContentUpdating({type, content, source}) {
|
2020-07-26 20:51:54 +00:00
|
|
|
let animate = false;
|
|
|
|
let valid = false;
|
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
case 'kanji':
|
2020-11-24 16:54:08 +00:00
|
|
|
animate = !!content.animate;
|
2020-07-26 20:51:54 +00:00
|
|
|
valid = content.definitions.length > 0;
|
2020-11-22 02:17:39 +00:00
|
|
|
this.blurElement(this._queryInput);
|
2020-07-26 20:51:54 +00:00
|
|
|
break;
|
|
|
|
case 'clear':
|
|
|
|
valid = false;
|
|
|
|
animate = true;
|
|
|
|
source = '';
|
|
|
|
break;
|
|
|
|
}
|
2020-07-26 23:29:12 +00:00
|
|
|
|
2020-07-26 20:51:54 +00:00
|
|
|
if (typeof source !== 'string') { source = ''; }
|
2020-07-26 23:29:12 +00:00
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
this._queryInput.value = source;
|
2020-11-08 17:34:23 +00:00
|
|
|
this._updateSearchHeight();
|
2020-07-26 20:51:54 +00:00
|
|
|
this._setIntroVisible(!valid, animate);
|
2020-07-03 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchInput() {
|
2020-11-08 17:34:23 +00:00
|
|
|
this._updateSearchHeight();
|
2017-03-25 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
_onSearchKeydown(e) {
|
|
|
|
if (e.code !== 'Enter' || e.shiftKey) { return; }
|
2019-09-15 16:12:30 +00:00
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
// Search
|
2019-10-26 22:26:17 +00:00
|
|
|
e.preventDefault();
|
2020-11-08 17:34:23 +00:00
|
|
|
e.stopImmediatePropagation();
|
2020-11-22 02:17:39 +00:00
|
|
|
this.blurElement(e.currentTarget);
|
2020-11-24 16:54:08 +00:00
|
|
|
this._search(true, true);
|
2020-11-08 17:34:23 +00:00
|
|
|
}
|
2019-10-08 23:49:08 +00:00
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
_onSearch(e) {
|
|
|
|
e.preventDefault();
|
2020-11-24 16:54:08 +00:00
|
|
|
this._search(true, true);
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onCopy() {
|
2020-01-26 01:02:33 +00:00
|
|
|
// ignore copy from search page
|
2020-07-03 19:58:29 +00:00
|
|
|
this._clipboardMonitor.setPreviousText(window.getSelection().toString().trim());
|
2020-01-26 01:02:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 00:30:10 +00:00
|
|
|
_onExternalSearchUpdate({text, animate=true}) {
|
2020-12-18 16:24:43 +00:00
|
|
|
const {general: {maximumClipboardSearchLength}} = this.getOptions();
|
|
|
|
if (text.length > maximumClipboardSearchLength) {
|
|
|
|
text = text.substring(0, maximumClipboardSearchLength);
|
|
|
|
}
|
2020-11-24 01:31:48 +00:00
|
|
|
this._queryInput.value = text;
|
2020-11-24 16:54:08 +00:00
|
|
|
this._search(animate, false);
|
2017-03-05 01:56:10 +00:00
|
|
|
}
|
2019-09-15 16:04:21 +00:00
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onWanakanaEnableChange(e) {
|
2020-05-06 23:32:28 +00:00
|
|
|
const value = e.target.checked;
|
2020-11-08 17:34:23 +00:00
|
|
|
this._setWanakanaEnabled(value);
|
2020-05-24 17:30:40 +00:00
|
|
|
api.modifySettings([{
|
2020-05-06 23:32:28 +00:00
|
|
|
action: 'set',
|
|
|
|
path: 'general.enableWanakana',
|
|
|
|
value,
|
|
|
|
scope: 'profile',
|
|
|
|
optionsContext: this.getOptionsContext()
|
|
|
|
}], 'search');
|
2020-02-27 00:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_onClipboardMonitorEnableChange(e) {
|
2020-08-09 17:11:41 +00:00
|
|
|
const enabled = e.target.checked;
|
|
|
|
this._setClipboardMonitorEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onModeChange() {
|
|
|
|
let mode = this.mode;
|
|
|
|
if (mode === null) { mode = ''; }
|
|
|
|
document.documentElement.dataset.searchMode = mode;
|
|
|
|
this._updateClipboardMonitorEnabled();
|
2020-02-27 00:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 17:34:23 +00:00
|
|
|
_setWanakanaEnabled(enabled) {
|
|
|
|
const input = this._queryInput;
|
|
|
|
this._queryInputEvents.removeAllEventListeners();
|
|
|
|
|
|
|
|
this._queryInputEvents.addEventListener(input, 'keydown', this._onSearchKeydown.bind(this), false);
|
|
|
|
|
|
|
|
if (this._wanakanaEnabled !== enabled) {
|
|
|
|
this._wanakanaEnabled = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
wanakana.bind(input);
|
|
|
|
} else {
|
|
|
|
wanakana.unbind(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._queryInputEvents.addEventListener(input, 'input', this._onSearchInput.bind(this), false);
|
2019-10-26 22:26:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_setIntroVisible(visible, animate) {
|
|
|
|
if (this._introVisible === visible) {
|
2019-09-15 16:04:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introVisible = visible;
|
2019-09-15 16:04:21 +00:00
|
|
|
|
2020-08-10 01:03:11 +00:00
|
|
|
if (this._introElement === null) {
|
2019-09-15 16:04:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
if (this._introAnimationTimer !== null) {
|
|
|
|
clearTimeout(this._introAnimationTimer);
|
|
|
|
this._introAnimationTimer = null;
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (visible) {
|
2020-07-03 19:58:29 +00:00
|
|
|
this._showIntro(animate);
|
2019-10-08 23:49:08 +00:00
|
|
|
} else {
|
2020-07-03 19:58:29 +00:00
|
|
|
this._hideIntro(animate);
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_showIntro(animate) {
|
2019-10-08 23:49:08 +00:00
|
|
|
if (animate) {
|
|
|
|
const duration = 0.4;
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.transition = '';
|
|
|
|
this._introElement.style.height = '';
|
|
|
|
const size = this._introElement.getBoundingClientRect();
|
|
|
|
this._introElement.style.height = '0px';
|
|
|
|
this._introElement.style.transition = `height ${duration}s ease-in-out 0s`;
|
|
|
|
window.getComputedStyle(this._introElement).getPropertyValue('height'); // Commits height so next line can start animation
|
|
|
|
this._introElement.style.height = `${size.height}px`;
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introAnimationTimer = setTimeout(() => {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.height = '';
|
2020-07-03 19:58:29 +00:00
|
|
|
this._introAnimationTimer = null;
|
2019-10-08 23:49:08 +00:00
|
|
|
}, duration * 1000);
|
|
|
|
} else {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.transition = '';
|
|
|
|
this._introElement.style.height = '';
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:58:29 +00:00
|
|
|
_hideIntro(animate) {
|
2019-10-08 23:49:08 +00:00
|
|
|
if (animate) {
|
|
|
|
const duration = 0.4;
|
2020-08-10 01:03:11 +00:00
|
|
|
const size = this._introElement.getBoundingClientRect();
|
|
|
|
this._introElement.style.height = `${size.height}px`;
|
|
|
|
this._introElement.style.transition = `height ${duration}s ease-in-out 0s`;
|
|
|
|
window.getComputedStyle(this._introElement).getPropertyValue('height'); // Commits height so next line can start animation
|
2019-10-08 23:49:08 +00:00
|
|
|
} else {
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.transition = '';
|
2019-10-08 23:49:08 +00:00
|
|
|
}
|
2020-08-10 01:03:11 +00:00
|
|
|
this._introElement.style.height = '0';
|
2019-09-15 16:04:21 +00:00
|
|
|
}
|
2019-10-08 23:49:08 +00:00
|
|
|
|
2020-08-09 17:11:41 +00:00
|
|
|
async _setClipboardMonitorEnabled(value) {
|
|
|
|
let modify = true;
|
|
|
|
if (value) {
|
|
|
|
value = await this._requestPermissions(['clipboardRead']);
|
|
|
|
modify = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._clipboardMonitorEnabled = value;
|
|
|
|
this._updateClipboardMonitorEnabled();
|
|
|
|
|
|
|
|
if (!modify) { return; }
|
|
|
|
|
|
|
|
await api.modifySettings([{
|
|
|
|
action: 'set',
|
|
|
|
path: 'general.enableClipboardMonitor',
|
|
|
|
value,
|
|
|
|
scope: 'profile',
|
|
|
|
optionsContext: this.getOptionsContext()
|
|
|
|
}], 'search');
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateClipboardMonitorEnabled() {
|
|
|
|
const mode = this.mode;
|
|
|
|
const enabled = this._clipboardMonitorEnabled;
|
2020-08-10 01:03:11 +00:00
|
|
|
this._clipboardMonitorEnableCheckbox.checked = enabled;
|
2020-08-09 17:11:41 +00:00
|
|
|
if (enabled && mode !== 'popup') {
|
|
|
|
this._clipboardMonitor.start();
|
|
|
|
} else {
|
|
|
|
this._clipboardMonitor.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_requestPermissions(permissions) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.permissions.request(
|
|
|
|
{permissions},
|
|
|
|
(granted) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
resolve(!e && granted);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2020-11-08 17:34:23 +00:00
|
|
|
|
2020-11-24 16:54:08 +00:00
|
|
|
_search(animate, history) {
|
2020-11-08 17:34:23 +00:00
|
|
|
const query = this._queryInput.value;
|
2020-11-24 01:31:48 +00:00
|
|
|
const details = {
|
|
|
|
focus: false,
|
2020-11-24 16:54:08 +00:00
|
|
|
history,
|
2020-11-24 01:31:48 +00:00
|
|
|
params: {
|
|
|
|
query
|
|
|
|
},
|
|
|
|
state: {
|
|
|
|
focusEntry: 0,
|
|
|
|
sentence: {text: query, offset: 0},
|
|
|
|
url: window.location.href
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
definitions: null,
|
|
|
|
animate
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.setContent(details);
|
2020-11-08 17:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateSearchHeight() {
|
|
|
|
const node = this._queryInput;
|
|
|
|
const {scrollHeight} = node;
|
|
|
|
const currentHeight = node.getBoundingClientRect().height;
|
|
|
|
if (scrollHeight >= currentHeight - 1) {
|
|
|
|
node.style.height = `${scrollHeight}px`;
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 02:55:04 +00:00
|
|
|
}
|