Merge pull request #326 from siikamiika/query-parser-text-scanner

use TextScanner in QueryParser
This commit is contained in:
siikamiika 2020-01-17 02:02:33 +02:00 committed by GitHub
commit 0e1e737afd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 73 deletions

View File

@ -17,73 +17,47 @@
*/ */
class QueryParser { class QueryParser extends TextScanner {
constructor(search) { constructor(search) {
super(document.querySelector('#query-parser'), [], [], []);
this.search = search; this.search = search;
this.pendingLookup = false;
this.clickScanPrevent = false;
this.parseResults = []; this.parseResults = [];
this.selectedParser = null; this.selectedParser = null;
this.queryParser = document.querySelector('#query-parser'); this.queryParser = document.querySelector('#query-parser');
this.queryParserSelect = document.querySelector('#query-parser-select'); this.queryParserSelect = document.querySelector('#query-parser-select');
this.queryParser.addEventListener('mousedown', (e) => this.onMouseDown(e));
this.queryParser.addEventListener('mouseup', (e) => this.onMouseUp(e));
} }
onError(error) { onError(error) {
logError(error, false); logError(error, false);
} }
onMouseDown(e) { onClick(e) {
if (DOM.isMouseButtonPressed(e, 'primary')) { super.onClick(e);
this.clickScanPrevent = false; this.searchAt(e.clientX, e.clientY, 'click');
}
} }
onMouseUp(e) { async onSearchSource(textSource, cause) {
if ( if (textSource === null) { return null; }
this.search.options.scanning.enablePopupSearch &&
!this.clickScanPrevent &&
DOM.isMouseButtonPressed(e, 'primary')
) {
const selectText = this.search.options.scanning.selectText;
this.onTermLookup(e, {disableScroll: true, selectText});
}
}
onMouseMove(e) { this.setTextSourceScanLength(textSource, this.search.options.scanning.length);
if (this.pendingLookup || DOM.isMouseButtonDown(e, 'primary')) { const searchText = textSource.text();
return; if (searchText.length === 0) { return; }
}
const scanningOptions = this.search.options.scanning; const {definitions, length} = await apiTermsFind(searchText, {}, this.search.getOptionsContext());
const scanningModifier = scanningOptions.modifier; if (definitions.length === 0) { return null; }
if (!(
TextScanner.isScanningModifierPressed(scanningModifier, e) ||
(scanningOptions.middleMouse && DOM.isMouseButtonDown(e, 'auxiliary'))
)) {
return;
}
const selectText = this.search.options.scanning.selectText; textSource.setEndOffset(length);
this.onTermLookup(e, {disableScroll: true, disableHistory: true, selectText});
}
onMouseLeave(e) { this.search.setContent('terms', {definitions, context: {
this.clickScanPrevent = true; focus: false,
clearTimeout(e.target.dataset.timer); disableHistory: cause === 'mouse' ? true : false,
delete e.target.dataset.timer; sentence: {text: searchText, offset: 0},
} url: window.location.href
}});
onTermLookup(e, params) { return {definitions, type: 'terms'};
this.pendingLookup = true;
(async () => {
await this.search.onTermLookup(e, params);
this.pendingLookup = false;
})();
} }
onParserChange(e) { onParserChange(e) {
@ -93,6 +67,27 @@ class QueryParser {
this.renderParseResult(this.getParseResult()); this.renderParseResult(this.getParseResult());
} }
getMouseEventListeners() {
return [
[this.node, 'click', this.onClick.bind(this)],
[this.node, 'mousedown', this.onMouseDown.bind(this)],
[this.node, 'mousemove', this.onMouseMove.bind(this)],
[this.node, 'mouseover', this.onMouseOver.bind(this)],
[this.node, 'mouseout', this.onMouseOut.bind(this)]
];
}
getTouchEventListeners() {
return [
[this.node, 'auxclick', this.onAuxClick.bind(this)],
[this.node, 'touchstart', this.onTouchStart.bind(this)],
[this.node, 'touchend', this.onTouchEnd.bind(this)],
[this.node, 'touchcancel', this.onTouchCancel.bind(this)],
[this.node, 'touchmove', this.onTouchMove.bind(this), {passive: false}],
[this.node, 'contextmenu', this.onContextMenu.bind(this)]
];
}
refreshSelectedParser() { refreshSelectedParser() {
if (this.parseResults.length > 0) { if (this.parseResults.length > 0) {
if (this.selectedParser === null) { if (this.selectedParser === null) {
@ -156,10 +151,6 @@ class QueryParser {
terms: previewTerms, terms: previewTerms,
preview: true preview: true
}); });
for (const charElement of this.queryParser.querySelectorAll('.query-parser-char')) {
this.activateScanning(charElement);
}
} }
renderParserSelect() { renderParserSelect() {
@ -190,27 +181,6 @@ class QueryParser {
'query-parser.html', 'query-parser.html',
{terms: QueryParser.processParseResultForDisplay(parseResult.parsedText)} {terms: QueryParser.processParseResultForDisplay(parseResult.parsedText)}
); );
for (const charElement of this.queryParser.querySelectorAll('.query-parser-char')) {
this.activateScanning(charElement);
}
}
activateScanning(element) {
element.addEventListener('mousemove', (e) => {
clearTimeout(e.target.dataset.timer);
if (this.search.options.scanning.modifier === 'none') {
e.target.dataset.timer = setTimeout(() => {
this.onMouseMove(e);
delete e.target.dataset.timer;
}, this.search.options.scanning.delay);
} else {
this.onMouseMove(e);
}
});
element.addEventListener('mouseleave', (e) => {
this.onMouseLeave(e);
});
} }
static processParseResultForDisplay(result) { static processParseResultForDisplay(result) {

View File

@ -236,6 +236,11 @@ class DisplaySearch extends Display {
} }
} }
async updateOptions(options) {
await super.updateOptions(options);
this.queryParser.setOptions(this.options);
}
initClipboardMonitor() { initClipboardMonitor() {
// ignore copy from search page // ignore copy from search page
window.addEventListener('copy', () => { window.addEventListener('copy', () => {

View File

@ -90,9 +90,8 @@ class Frontend extends TextScanner {
} }
async updateOptions() { async updateOptions() {
this.options = await apiOptionsGet(this.getOptionsContext()); this.setOptions(await apiOptionsGet(this.getOptionsContext()));
await this.popup.setOptions(this.options); await this.popup.setOptions(this.options);
this.setEnabled(this.options.general.enable);
} }
async onSearchSource(textSource, cause) { async onSearchSource(textSource, cause) {

View File

@ -281,6 +281,7 @@ class TextScanner {
setOptions(options) { setOptions(options) {
this.options = options; this.options = options;
this.setEnabled(this.options.general.enable);
} }
async searchAt(x, y, cause) { async searchAt(x, y, cause) {