add simple query parser

This commit is contained in:
siikamiika 2019-10-29 23:49:36 +02:00
parent baf0325f62
commit f63e8e4be0
5 changed files with 74 additions and 8 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
class QueryParser {
constructor(search) {
this.search = search;
this.queryParser = document.querySelector('#query-parser');
// TODO also enable for mouseover scanning
this.queryParser.addEventListener('click', (e) => this.onTermLookup(e));
}
onError(error) {
logError(error, false);
}
async onTermLookup(e) {
const {textSource} = await this.search.onTermLookup(e, {isQueryParser: true});
if (textSource) {
textSource.select();
}
}
setText(text) {
this.queryParser.innerText = text;
}
}

View File

@ -32,6 +32,8 @@ class DisplaySearch extends Display {
url: window.location.href
};
this.queryParser = new QueryParser(this);
this.search = document.querySelector('#search');
this.query = document.querySelector('#query');
this.intro = document.querySelector('#intro');
@ -72,11 +74,11 @@ class DisplaySearch extends Display {
const query = DisplaySearch.getSearchQueryFromLocation(window.location.href) || '';
if (e.target.checked) {
window.wanakana.bind(this.query);
this.query.value = window.wanakana.toKana(query);
this.setQuery(window.wanakana.toKana(query));
apiOptionsSet({general: {enableWanakana: true}}, this.getOptionsContext());
} else {
window.wanakana.unbind(this.query);
this.query.value = query;
this.setQuery(query);
apiOptionsSet({general: {enableWanakana: false}}, this.getOptionsContext());
}
this.onSearchQueryUpdated(this.query.value, false);
@ -86,9 +88,9 @@ class DisplaySearch extends Display {
const query = DisplaySearch.getSearchQueryFromLocation(window.location.href);
if (query !== null) {
if (this.isWanakanaEnabled()) {
this.query.value = window.wanakana.toKana(query);
this.setQuery(window.wanakana.toKana(query));
} else {
this.query.value = query;
this.setQuery(query);
}
this.onSearchQueryUpdated(this.query.value, false);
}
@ -159,6 +161,7 @@ class DisplaySearch extends Display {
e.preventDefault();
const query = this.query.value;
this.queryParser.setText(query);
const queryString = query.length > 0 ? `?query=${encodeURIComponent(query)}` : '';
window.history.pushState(null, '', `${window.location.pathname}${queryString}`);
this.onSearchQueryUpdated(query, true);
@ -168,9 +171,9 @@ class DisplaySearch extends Display {
const query = DisplaySearch.getSearchQueryFromLocation(window.location.href) || '';
if (this.query !== null) {
if (this.isWanakanaEnabled()) {
this.query.value = window.wanakana.toKana(query);
this.setQuery(window.wanakana.toKana(query));
} else {
this.query.value = query;
this.setQuery(query);
}
}
@ -258,9 +261,9 @@ class DisplaySearch extends Display {
}
if (curText && (curText !== this.clipboardPrevText) && jpIsJapaneseText(curText)) {
if (this.isWanakanaEnabled()) {
this.query.value = window.wanakana.toKana(curText);
this.setQuery(window.wanakana.toKana(curText));
} else {
this.query.value = curText;
this.setQuery(curText);
}
const queryString = curText.length > 0 ? `?query=${encodeURIComponent(curText)}` : '';
@ -287,6 +290,11 @@ class DisplaySearch extends Display {
return this.optionsContext;
}
setQuery(query) {
this.query.value = query;
this.queryParser.setText(query);
}
setIntroVisible(visible, animate) {
if (this.introVisible === visible) {
return;

View File

@ -47,6 +47,8 @@
<img src="/mixed/img/spinner.gif">
</div>
<div id="query-parser" class="scan-disable"></div>
<div id="content"></div>
</div>
@ -67,6 +69,7 @@
<script src="/mixed/js/japanese.js"></script>
<script src="/mixed/js/scroll.js"></script>
<script src="/bg/js/search-query-parser.js"></script>
<script src="/bg/js/search.js"></script>
<script src="/bg/js/search-frontend.js"></script>
</body>

View File

@ -88,6 +88,15 @@ ol, ul {
user-select: none;
}
#query-parser {
margin-top: 10px;
font-size: 24px;
}
.highlight {
background-color: lightblue;
}
/*
* Entries

View File

@ -140,6 +140,8 @@ class Display {
}
this.setContentTerms(definitions, context);
return {textSource};
} catch (error) {
this.onError(error);
}