refactor and tune wanakana toggling

This commit is contained in:
siikamiika 2019-10-27 01:26:17 +03:00
parent 01ffb052e6
commit 7ee87265cd

View File

@ -60,21 +60,26 @@ class DisplaySearch extends Display {
window.wanakana.bind(this.query); window.wanakana.bind(this.query);
} }
this.wanakanaEnable.addEventListener('change', (e) => { this.wanakanaEnable.addEventListener('change', (e) => {
let query = DisplaySearch.getSearchQueryFromLocation(window.location.href);
if (e.target.checked) { if (e.target.checked) {
window.wanakana.bind(this.query); window.wanakana.bind(this.query);
this.query.value = window.wanakana.toKana(query);
} else { } else {
window.wanakana.unbind(this.query); window.wanakana.unbind(this.query);
this.query.value = query;
} }
this.onSearchQueryUpdated(this.query.value, false);
}); });
} }
let query = DisplaySearch.getSearchQueryFromLocation(window.location.href); const query = DisplaySearch.getSearchQueryFromLocation(window.location.href);
if (query !== null) { if (query !== null) {
if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) { if (this.isWanakanaEnabled()) {
query = window.wanakana.toKana(query); this.query.value = window.wanakana.toKana(query);
} } else {
this.query.value = query; this.query.value = query;
this.onSearchQueryUpdated(query, false); }
this.onSearchQueryUpdated(this.query.value, false);
} }
} }
if (this.clipboardMonitorEnable !== null) { if (this.clipboardMonitorEnable !== null) {
@ -123,9 +128,7 @@ class DisplaySearch extends Display {
return; return;
} }
if (e) {
e.preventDefault(); e.preventDefault();
}
const query = this.query.value; const query = this.query.value;
const queryString = query.length > 0 ? `?query=${encodeURIComponent(query)}` : ''; const queryString = query.length > 0 ? `?query=${encodeURIComponent(query)}` : '';
@ -136,10 +139,14 @@ class DisplaySearch extends Display {
onPopState(e) { onPopState(e) {
const query = DisplaySearch.getSearchQueryFromLocation(window.location.href) || ''; const query = DisplaySearch.getSearchQueryFromLocation(window.location.href) || '';
if (this.query !== null) { if (this.query !== null) {
if (this.isWanakanaEnabled()) {
this.query.value = window.wanakana.toKana(query);
} else {
this.query.value = query; this.query.value = query;
} }
}
this.onSearchQueryUpdated(query, false); this.onSearchQueryUpdated(this.query.value, false);
} }
onKeyDown(e) { onKeyDown(e) {
@ -213,23 +220,24 @@ class DisplaySearch extends Display {
initClipboardMonitor() { initClipboardMonitor() {
// ignore copy from search page // ignore copy from search page
window.addEventListener('copy', (e) => { window.addEventListener('copy', (e) => {
let prevText = document.getSelection().toString().trim(); this.clipboardPrevText = document.getSelection().toString().trim();
if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) {
prevText = window.wanakana.toKana(prevText);
}
this.clipboardPrevText = prevText;
}); });
} }
startClipboardMonitor() { startClipboardMonitor() {
this.clipboardMonitorIntervalId = setInterval(async () => { this.clipboardMonitorIntervalId = setInterval(async () => {
let curText = (await navigator.clipboard.readText()).trim(); const curText = (await navigator.clipboard.readText()).trim();
if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) {
curText = window.wanakana.toKana(curText);
}
if (curText && (curText !== this.clipboardPrevText)) { if (curText && (curText !== this.clipboardPrevText)) {
if (this.isWanakanaEnabled()) {
this.query.value = window.wanakana.toKana(curText);
} else {
this.query.value = curText; this.query.value = curText;
this.onSearch(); }
const queryString = curText.length > 0 ? `?query=${encodeURIComponent(curText)}` : '';
window.history.pushState(null, '', `${window.location.pathname}${queryString}`);
this.onSearchQueryUpdated(this.query.value, true);
this.clipboardPrevText = curText; this.clipboardPrevText = curText;
} }
}, 100); }, 100);
@ -242,6 +250,10 @@ class DisplaySearch extends Display {
} }
} }
isWanakanaEnabled() {
return this.wanakanaEnable !== null && this.wanakanaEnable.checked;
}
getOptionsContext() { getOptionsContext() {
return this.optionsContext; return this.optionsContext;
} }