From f63e8e4be0e7bdb1a2e45e349bf667ea7ca4adab Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 29 Oct 2019 23:49:36 +0200 Subject: add simple query parser --- ext/bg/js/search-query-parser.js | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ext/bg/js/search-query-parser.js (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js new file mode 100644 index 00000000..debe53b4 --- /dev/null +++ b/ext/bg/js/search-query-parser.js @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2019 Alex Yatskov + * Author: Alex Yatskov + * + * 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 . + */ + + +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; + } +} -- cgit v1.2.3 From c35a05cd62d43ff435c022a353de55510b020277 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 30 Oct 2019 03:58:24 +0200 Subject: add kana to text --- ext/bg/js/api.js | 37 +++++++++++++++ ext/bg/js/backend.js | 1 + ext/bg/js/search-query-parser.js | 97 ++++++++++++++++++++++++++++++++++++---- ext/fg/js/api.js | 4 ++ ext/mixed/css/display.css | 4 ++ ext/mixed/js/display.js | 78 +++++++++++++++++++++----------- 6 files changed, 188 insertions(+), 33 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index df73aa2a..064903ca 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -79,6 +79,43 @@ async function apiTermsFind(text, details, optionsContext) { return {length, definitions}; } +async function apiTextParse(text, optionsContext) { + const options = await apiOptionsGet(optionsContext); + const translator = utilBackend().translator; + + const results = []; + while (text) { + let [definitions, length] = await translator.findTerms(text, {}, options); + if (definitions.length > 0) { + definitions = dictTermsSort(definitions); + const {expression, source, reading} = definitions[0]; + + let stemLength = source.length; + while (source[stemLength - 1] !== expression[stemLength - 1]) { + --stemLength; + } + const offset = source.length - stemLength; + + for (const result of jpDistributeFurigana( + source.slice(0, offset === 0 ? source.length : source.length - offset), + reading.slice(0, offset === 0 ? reading.length : source.length + (reading.length - expression.length) - offset) + )) { + results.push(result); + } + + if (stemLength !== source.length) { + results.push({text: source.slice(stemLength)}); + } + + text = text.slice(source.length); + } else { + results.push({text: text[0]}); + text = text.slice(1); + } + } + return results; +} + async function apiKanjiFind(text, optionsContext) { const options = await apiOptionsGet(optionsContext); const definitions = await utilBackend().translator.findKanji(text, options); diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index efad153a..d0e404f2 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -180,6 +180,7 @@ Backend.messageHandlers = { optionsSet: ({changedOptions, optionsContext, source}) => apiOptionsSet(changedOptions, optionsContext, source), kanjiFind: ({text, optionsContext}) => apiKanjiFind(text, optionsContext), termsFind: ({text, details, optionsContext}) => apiTermsFind(text, details, optionsContext), + textParse: ({text, optionsContext}) => apiTextParse(text, optionsContext), definitionAdd: ({definition, mode, context, optionsContext}) => apiDefinitionAdd(definition, mode, context, optionsContext), definitionsAddable: ({definitions, modes, optionsContext}) => apiDefinitionsAddable(definitions, modes, optionsContext), noteView: ({noteId}) => apiNoteView(noteId), diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index debe53b4..c3a3900b 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -23,22 +23,103 @@ class QueryParser { this.queryParser = document.querySelector('#query-parser'); - // TODO also enable for mouseover scanning - this.queryParser.addEventListener('click', (e) => this.onTermLookup(e)); + this.queryParser.addEventListener('click', (e) => this.onClick(e)); + this.queryParser.addEventListener('mousemove', (e) => this.onMouseMove(e)); } onError(error) { logError(error, false); } - async onTermLookup(e) { - const {textSource} = await this.search.onTermLookup(e, {isQueryParser: true}); - if (textSource) { - textSource.select(); + onClick(e) { + this.onTermLookup(e, {disableScroll: true, selectText: true}); + } + + async onMouseMove(e) { + if ( + (e.buttons & 0x1) !== 0x0 // Left mouse button + ) { + return; + } + + const scanningOptions = this.search.options.scanning; + const scanningModifier = scanningOptions.modifier; + if (!( + QueryParser.isScanningModifierPressed(scanningModifier, e) || + (scanningOptions.middleMouse && (e.buttons & 0x4) !== 0x0) // Middle mouse button + )) { + return; } + + await this.onTermLookup(e, {disableScroll: true, selectText: true, disableHistory: true}) + } + + onTermLookup(e, params) { + this.search.onTermLookup(e, params); } - setText(text) { - this.queryParser.innerText = text; + async setText(text) { + this.search.setSpinnerVisible(true); + + const results = await apiTextParse(text, this.search.getOptionsContext()); + + const tempContainer = document.createElement('div'); + for (const {text, furigana} of results) { + if (furigana) { + const rubyElement = document.createElement('ruby'); + const furiganaElement = document.createElement('rt'); + furiganaElement.innerText = furigana; + rubyElement.appendChild(document.createTextNode(text)); + rubyElement.appendChild(furiganaElement); + tempContainer.appendChild(rubyElement); + } else { + tempContainer.appendChild(document.createTextNode(text)); + } + } + this.queryParser.innerHTML = ''; + this.queryParser.appendChild(tempContainer); + + this.search.setSpinnerVisible(false); + } + + async parseText(text) { + const results = []; + while (text) { + const {definitions, length} = await apiTermsFind(text, {}, this.search.getOptionsContext()); + if (length) { + results.push(definitions); + text = text.slice(length); + } else { + results.push(text[0]); + text = text.slice(1); + } + } + return results; + } + + popupTimerSet(callback) { + const delay = this.options.scanning.delay; + if (delay > 0) { + this.popupTimer = window.setTimeout(callback, delay); + } else { + Promise.resolve().then(callback); + } + } + + popupTimerClear() { + if (this.popupTimer !== null) { + window.clearTimeout(this.popupTimer); + this.popupTimer = null; + } + } + + static isScanningModifierPressed(scanningModifier, mouseEvent) { + switch (scanningModifier) { + case 'alt': return mouseEvent.altKey; + case 'ctrl': return mouseEvent.ctrlKey; + case 'shift': return mouseEvent.shiftKey; + case 'none': return true; + default: return false; + } } } diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index 945ba076..cc1e0e90 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -29,6 +29,10 @@ function apiTermsFind(text, details, optionsContext) { return utilInvoke('termsFind', {text, details, optionsContext}); } +function apiTextParse(text, optionsContext) { + return utilInvoke('textParse', {text, optionsContext}); +} + function apiKanjiFind(text, optionsContext) { return utilInvoke('kanjiFind', {text, optionsContext}); } diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index 5e5213ff..81109fc6 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -73,6 +73,10 @@ ol, ul { } +html:root[data-yomichan-page=search] body { + min-height: 101vh; /* always show scroll bar to avoid scanning problems */ +} + /* * Search page */ diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 07a851f5..82385bf9 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -98,17 +98,62 @@ class Display { } } - async onTermLookup(e) { + async onTermLookup(e, {disableScroll, selectText, disableHistory}) { + const termLookupResults = await this.termLookup(e); + if (!termLookupResults) { + return false; + } + + try { + const {textSource, definitions} = termLookupResults; + + const scannedElement = e.target; + const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); + + if (!disableScroll) { + this.windowScroll.toY(0); + } + let context; + if (disableHistory) { + const {url, source} = this.context || {}; + context = {sentence, url, source, disableScroll}; + } else { + context = { + disableScroll, + source: { + definitions: this.definitions, + index: this.entryIndexFind(scannedElement), + scroll: this.windowScroll.y + } + }; + + if (this.context) { + context.sentence = sentence; + context.url = this.context.url; + context.source.source = this.context.source; + } + } + + this.setContentTerms(definitions, context); + + if (selectText) { + textSource.select(); + } + } catch (error) { + this.onError(error); + } + } + + async termLookup(e) { try { e.preventDefault(); - const clickedElement = e.target; const textSource = docRangeFromPoint(e.clientX, e.clientY, this.options); if (textSource === null) { return false; } - let definitions, length, sentence; + let definitions, length; try { textSource.setEndOffset(this.options.scanning.length); @@ -118,30 +163,11 @@ class Display { } textSource.setEndOffset(length); - - sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); } finally { textSource.cleanup(); } - this.windowScroll.toY(0); - const context = { - source: { - definitions: this.definitions, - index: this.entryIndexFind(clickedElement), - scroll: this.windowScroll.y - } - }; - - if (this.context) { - context.sentence = sentence; - context.url = this.context.url; - context.source.source = this.context.source; - } - - this.setContentTerms(definitions, context); - - return {textSource}; + return {textSource, definitions}; } catch (error) { this.onError(error); } @@ -338,8 +364,10 @@ class Display { const content = await apiTemplateRender('terms.html', params); this.container.innerHTML = content; - const {index, scroll} = context || {}; - this.entryScrollIntoView(index || 0, scroll); + const {index, scroll, disableScroll} = context || {}; + if (!disableScroll) { + this.entryScrollIntoView(index || 0, scroll); + } if (options.audio.enabled && options.audio.autoPlay) { this.autoPlayAudio(); -- cgit v1.2.3 From 627e16d44b19bdae1bc51fa6f76bc766939e0786 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 30 Oct 2019 13:01:06 +0200 Subject: improve text preview --- ext/bg/js/search-query-parser.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index c3a3900b..9bea6508 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -59,25 +59,39 @@ class QueryParser { } async setText(text) { + this.queryParser.innerHTML = ''; this.search.setSpinnerVisible(true); + let previewText = text; + while (previewText) { + const tempText = previewText.slice(0, 2); + previewText = previewText.slice(2); + + const tempRuby = document.createElement('ruby'); + const tempFurigana = document.createElement('rt'); + tempRuby.appendChild(document.createTextNode(tempText)); + tempRuby.appendChild(tempFurigana); + this.queryParser.appendChild(tempRuby); + } + const results = await apiTextParse(text, this.search.getOptionsContext()); - const tempContainer = document.createElement('div'); + const textContainer = document.createElement('div'); for (const {text, furigana} of results) { + const rubyElement = document.createElement('ruby'); + const furiganaElement = document.createElement('rt'); if (furigana) { - const rubyElement = document.createElement('ruby'); - const furiganaElement = document.createElement('rt'); furiganaElement.innerText = furigana; rubyElement.appendChild(document.createTextNode(text)); rubyElement.appendChild(furiganaElement); - tempContainer.appendChild(rubyElement); } else { - tempContainer.appendChild(document.createTextNode(text)); + rubyElement.appendChild(document.createTextNode(text)); + rubyElement.appendChild(furiganaElement); } + textContainer.appendChild(rubyElement); } this.queryParser.innerHTML = ''; - this.queryParser.appendChild(tempContainer); + this.queryParser.appendChild(textContainer); this.search.setSpinnerVisible(false); } -- cgit v1.2.3 From 3881457e4ed3f9c7833ac21a5e7fc44c2ba00b0f Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 31 Oct 2019 23:56:44 +0200 Subject: use handlebars templates for query parser --- ext/bg/js/api.js | 12 +++++----- ext/bg/js/search-query-parser.js | 40 ++++++++++++++------------------- ext/bg/js/templates.js | 48 ++++++++++++++++++++++++++++++++++++++++ ext/mixed/css/display.css | 9 ++++---- tmpl/query-parser.html | 23 +++++++++++++++++++ 5 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 tmpl/query-parser.html (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index dbbe7368..7c9a72a7 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -85,6 +85,7 @@ async function apiTextParse(text, optionsContext) { const results = []; while (text) { + const term = []; let [definitions, sourceLength] = await translator.findTerms(text, {}, options); if (definitions.length > 0) { definitions = dictTermsSort(definitions); @@ -98,22 +99,23 @@ async function apiTextParse(text, optionsContext) { } const offset = source.length - stemLength; - for (const result of jpDistributeFurigana( + for (const {text, furigana} of jpDistributeFurigana( source.slice(0, offset === 0 ? source.length : source.length - offset), - reading.slice(0, offset === 0 ? reading.length : source.length + (reading.length - expression.length) - offset) + reading.slice(0, offset === 0 ? reading.length : reading.length - expression.length + stemLength) )) { - results.push(result); + term.push({text, reading: furigana || ''}); } if (stemLength !== source.length) { - results.push({text: source.slice(stemLength)}); + term.push({text: source.slice(stemLength)}); } text = text.slice(source.length); } else { - results.push({text: text[0]}); + term.push({text: text[0]}); text = text.slice(1); } + results.push(term); } return results; } diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 9bea6508..8a7db69a 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -59,39 +59,33 @@ class QueryParser { } async setText(text) { - this.queryParser.innerHTML = ''; this.search.setSpinnerVisible(true); + const previewTerms = []; let previewText = text; while (previewText) { const tempText = previewText.slice(0, 2); + previewTerms.push([{text: tempText}]); previewText = previewText.slice(2); - - const tempRuby = document.createElement('ruby'); - const tempFurigana = document.createElement('rt'); - tempRuby.appendChild(document.createTextNode(tempText)); - tempRuby.appendChild(tempFurigana); - this.queryParser.appendChild(tempRuby); } + this.queryParser.innerHTML = await apiTemplateRender('query-parser.html', { + terms: previewTerms, + preview: true + }); + const results = await apiTextParse(text, this.search.getOptionsContext()); - const textContainer = document.createElement('div'); - for (const {text, furigana} of results) { - const rubyElement = document.createElement('ruby'); - const furiganaElement = document.createElement('rt'); - if (furigana) { - furiganaElement.innerText = furigana; - rubyElement.appendChild(document.createTextNode(text)); - rubyElement.appendChild(furiganaElement); - } else { - rubyElement.appendChild(document.createTextNode(text)); - rubyElement.appendChild(furiganaElement); - } - textContainer.appendChild(rubyElement); - } - this.queryParser.innerHTML = ''; - this.queryParser.appendChild(textContainer); + const content = await apiTemplateRender('query-parser.html', { + terms: results.map((term) => { + return term.map((part) => { + part.raw = !part.text.trim() && (!part.reading || !part.reading.trim()); + return part; + }); + }) + }); + + this.queryParser.innerHTML = content; this.search.setSpinnerVisible(false); } diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 823b9e6f..cc233d49 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -162,6 +162,54 @@ templates['kanji.html'] = template({"1":function(container,depth0,helpers,partia return fn; } +,"useDecorators":true,"usePartial":true,"useData":true,"useDepths":true}); +templates['query-parser.html'] = template({"1":function(container,depth0,helpers,partials,data) { + var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); + + return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.preview : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.program(4, data, 0),"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(alias1,depth0,{"name":"each","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ""; +},"2":function(container,depth0,helpers,partials,data) { + return ""; +},"4":function(container,depth0,helpers,partials,data) { + return ""; +},"6":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = container.invokePartial(partials.part,depth0,{"name":"part","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); +},"8":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.raw : depth0),{"name":"if","hash":{},"fn":container.program(9, data, 0),"inverse":container.program(11, data, 0),"data":data})) != null ? stack1 : ""); +},"9":function(container,depth0,helpers,partials,data) { + var helper; + + return container.escapeExpression(((helper = (helper = helpers.text || (depth0 != null ? depth0.text : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"text","hash":{},"data":data}) : helper))); +},"11":function(container,depth0,helpers,partials,data) { + var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; + + return "" + + alias4(((helper = (helper = helpers.text || (depth0 != null ? depth0.text : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"text","hash":{},"data":data}) : helper))) + + "" + + alias4(((helper = (helper = helpers.reading || (depth0 != null ? depth0.reading : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"reading","hash":{},"data":data}) : helper))) + + ""; +},"13":function(container,depth0,helpers,partials,data,blockParams,depths) { + var stack1; + + return ((stack1 = container.invokePartial(partials.term,depth0,{"name":"term","hash":{"preview":(depths[1] != null ? depths[1].preview : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); +},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { + var stack1; + + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.terms : depth0),{"name":"each","hash":{},"fn":container.program(13, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"main_d": function(fn, props, container, depth0, data, blockParams, depths) { + + var decorators = container.decorators; + + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(1, data, 0, blockParams, depths),"inverse":container.noop,"args":["term"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(8, data, 0, blockParams, depths),"inverse":container.noop,"args":["part"],"data":data}) || fn; + return fn; + } + ,"useDecorators":true,"usePartial":true,"useData":true,"useDepths":true}); templates['terms.html'] = template({"1":function(container,depth0,helpers,partials,data) { var stack1, helper, options, alias1=depth0 != null ? depth0 : (container.nullContext || {}), buffer = diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index 65b8b466..d24aa58c 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -93,13 +93,12 @@ ol, ul { font-size: 24px; } -html:root[data-yomichan-page=search] body { - min-height: 101vh; /* always show scroll bar to avoid scanning problems */ +.query-parser-term { + margin-right: 5px; } -#query-parser { - margin-top: 10px; - font-size: 24px; +html:root[data-yomichan-page=search] body { + min-height: 101vh; /* always show scroll bar to avoid scanning problems */ } diff --git a/tmpl/query-parser.html b/tmpl/query-parser.html new file mode 100644 index 00000000..818650e6 --- /dev/null +++ b/tmpl/query-parser.html @@ -0,0 +1,23 @@ +{{~#*inline "term"~}} +{{~#if preview~}} + +{{~else~}} + +{{~/if~}} +{{~#each this~}} +{{> part }} +{{~/each~}} + +{{~/inline~}} + +{{~#*inline "part"~}} +{{~#if raw~}} +{{text}} +{{~else~}} +{{text}}{{reading}} +{{~/if~}} +{{~/inline~}} + +{{~#each terms~}} +{{> term preview=../preview }} +{{~/each~}} -- cgit v1.2.3 From 41020289ab68ef22a0691a9f268a79d6a706df6b Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 05:08:57 +0200 Subject: add mecab support --- ext/bg/background.html | 1 + ext/bg/js/api.js | 48 ++++++++++++++++++------------ ext/bg/js/backend.js | 2 ++ ext/bg/js/mecab.js | 63 ++++++++++++++++++++++++++++++++++++++++ ext/bg/js/search-query-parser.js | 3 +- ext/fg/js/api.js | 4 +++ ext/manifest.json | 3 +- ext/mixed/js/japanese.js | 35 ++++++++++++++++++++-- 8 files changed, 136 insertions(+), 23 deletions(-) create mode 100644 ext/bg/js/mecab.js (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/background.html b/ext/bg/background.html index bbfbd1e1..6e6e7c26 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -21,6 +21,7 @@ + diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 7c9a72a7..2ab01af3 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -91,25 +91,10 @@ async function apiTextParse(text, optionsContext) { definitions = dictTermsSort(definitions); const {expression, reading} = definitions[0]; const source = text.slice(0, sourceLength); - - let stemLength = 0; - const shortest = Math.min(source.length, expression.length); - while (stemLength < shortest && source[stemLength] === expression[stemLength]) { - ++stemLength; - } - const offset = source.length - stemLength; - - for (const {text, furigana} of jpDistributeFurigana( - source.slice(0, offset === 0 ? source.length : source.length - offset), - reading.slice(0, offset === 0 ? reading.length : reading.length - expression.length + stemLength) - )) { - term.push({text, reading: furigana || ''}); - } - - if (stemLength !== source.length) { - term.push({text: source.slice(stemLength)}); + for (const {text, furigana} of jpDistributeFuriganaInflected(expression, reading, source)) { + // can't use 'furigana' in templates + term.push({text, reading: furigana}); } - text = text.slice(source.length); } else { term.push({text: text[0]}); @@ -120,6 +105,33 @@ async function apiTextParse(text, optionsContext) { return results; } +async function apiTextParseMecab(text, optionsContext) { + const options = await apiOptionsGet(optionsContext); + const mecab = utilBackend().mecab; + + const results = []; + for (const parsedLine of await mecab.parseText(text)) { + for (const {expression, reading, source} of parsedLine) { + const term = []; + if (expression && reading) { + for (const {text, furigana} of jpDistributeFuriganaInflected( + expression, + jpKatakanaToHiragana(reading), + source + )) { + // can't use 'furigana' in templates + term.push({text, reading: furigana}); + } + } else { + term.push({text: source}); + } + results.push(term); + } + results.push([{text: '\n'}]); + } + return results; +} + async function apiKanjiFind(text, optionsContext) { const options = await apiOptionsGet(optionsContext); const definitions = await utilBackend().translator.findKanji(text, options); diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index d0e404f2..e97f32b5 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -21,6 +21,7 @@ class Backend { constructor() { this.translator = new Translator(); this.anki = new AnkiNull(); + this.mecab = new Mecab(); this.options = null; this.optionsContext = { depth: 0, @@ -181,6 +182,7 @@ Backend.messageHandlers = { kanjiFind: ({text, optionsContext}) => apiKanjiFind(text, optionsContext), termsFind: ({text, details, optionsContext}) => apiTermsFind(text, details, optionsContext), textParse: ({text, optionsContext}) => apiTextParse(text, optionsContext), + textParseMecab: ({text, optionsContext}) => apiTextParseMecab(text, optionsContext), definitionAdd: ({definition, mode, context, optionsContext}) => apiDefinitionAdd(definition, mode, context, optionsContext), definitionsAddable: ({definitions, modes, optionsContext}) => apiDefinitionsAddable(definitions, modes, optionsContext), noteView: ({noteId}) => apiNoteView(noteId), diff --git a/ext/bg/js/mecab.js b/ext/bg/js/mecab.js new file mode 100644 index 00000000..dc46ded2 --- /dev/null +++ b/ext/bg/js/mecab.js @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2019 Alex Yatskov + * Author: Alex Yatskov + * + * 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 . + */ + + +class Mecab { + constructor() { + this.listeners = {}; + this.sequence = 0; + this.startListener(); + } + + async parseText(text) { + return await this.invoke('parse_text', {text}); + } + + startListener() { + this.port = chrome.runtime.connectNative('mecab'); + this.port.onMessage.addListener((message) => { + const {sequence, data} = message; + const {callback, timer} = this.listeners[sequence] || {}; + if (timer) { + clearTimeout(timer); + delete this.listeners[sequence]; + callback(data); + } + }); + } + + invoke(action, params) { + return new Promise((resolve, reject) => { + const sequence = this.sequence++; + + this.listeners[sequence] = { + callback: (data) => { + resolve(data); + }, + timer: setTimeout(() => { + delete this.listeners[sequence]; + reject(`Mecab invoke timed out in ${Mecab.timeout} ms`); + }, 1000) + } + + this.port.postMessage({action, params, sequence}); + }); + } +} + +Mecab.timeout = 1000; diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 8a7db69a..0c74e550 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -74,7 +74,8 @@ class QueryParser { preview: true }); - const results = await apiTextParse(text, this.search.getOptionsContext()); + // const results = await apiTextParse(text, this.search.getOptionsContext()); + const results = await apiTextParseMecab(text, this.search.getOptionsContext()); const content = await apiTemplateRender('query-parser.html', { terms: results.map((term) => { diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index cc1e0e90..92330d9c 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -33,6 +33,10 @@ function apiTextParse(text, optionsContext) { return utilInvoke('textParse', {text, optionsContext}); } +function apiTextParseMecab(text, optionsContext) { + return utilInvoke('textParseMecab', {text, optionsContext}); +} + function apiKanjiFind(text, optionsContext) { return utilInvoke('kanjiFind', {text, optionsContext}); } diff --git a/ext/manifest.json b/ext/manifest.json index fabceafd..4d75cd54 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -42,7 +42,8 @@ "", "storage", "clipboardWrite", - "unlimitedStorage" + "unlimitedStorage", + "nativeMessaging" ], "optional_permissions": [ "clipboardRead" diff --git a/ext/mixed/js/japanese.js b/ext/mixed/js/japanese.js index d24f56a6..78c419b2 100644 --- a/ext/mixed/js/japanese.js +++ b/ext/mixed/js/japanese.js @@ -61,12 +61,11 @@ function jpDistributeFurigana(expression, reading) { const group = groups[0]; if (group.mode === 'kana') { - if (reading.startsWith(group.text)) { - const readingUsed = reading.substring(0, group.text.length); + if (jpKatakanaToHiragana(reading).startsWith(jpKatakanaToHiragana(group.text))) { const readingLeft = reading.substring(group.text.length); const segs = segmentize(readingLeft, groups.splice(1)); if (segs) { - return [{text: readingUsed}].concat(segs); + return [{text: group.text}].concat(segs); } } } else { @@ -95,3 +94,33 @@ function jpDistributeFurigana(expression, reading) { return segmentize(reading, groups) || fallback; } + +function jpDistributeFuriganaInflected(expression, reading, source) { + const output = []; + + let stemLength = 0; + const shortest = Math.min(source.length, expression.length); + const sourceHiragana = jpKatakanaToHiragana(source); + const expressionHiragana = jpKatakanaToHiragana(expression); + while ( + stemLength < shortest && + // sometimes an expression can use a kanji that's different from the source + (!jpIsKana(source[stemLength]) || (sourceHiragana[stemLength] === expressionHiragana[stemLength])) + ) { + ++stemLength; + } + const offset = source.length - stemLength; + + for (const segment of jpDistributeFurigana( + source.slice(0, offset === 0 ? source.length : source.length - offset), + reading.slice(0, offset === 0 ? reading.length : reading.length - expression.length + stemLength) + )) { + output.push(segment); + } + + if (stemLength !== source.length) { + output.push({text: source.slice(stemLength)}); + } + + return output; +} -- cgit v1.2.3 From 5a3e8c819c94be628101311e53c164b0cdd234b4 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 15:19:42 +0200 Subject: optimize mouseover scanning in query parser --- ext/bg/js/search-query-parser.js | 87 +++++++++++++++++++++++----------------- ext/bg/js/templates.js | 22 +++++----- tmpl/query-parser.html | 8 +++- 3 files changed, 70 insertions(+), 47 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 0c74e550..8c3159cd 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -20,11 +20,11 @@ class QueryParser { constructor(search) { this.search = search; + this.pendingLookup = false; this.queryParser = document.querySelector('#query-parser'); this.queryParser.addEventListener('click', (e) => this.onClick(e)); - this.queryParser.addEventListener('mousemove', (e) => this.onMouseMove(e)); } onError(error) { @@ -35,8 +35,9 @@ class QueryParser { this.onTermLookup(e, {disableScroll: true, selectText: true}); } - async onMouseMove(e) { + onMouseEnter(e) { if ( + this.pendingLookup || (e.buttons & 0x1) !== 0x0 // Left mouse button ) { return; @@ -51,46 +52,76 @@ class QueryParser { return; } - await this.onTermLookup(e, {disableScroll: true, selectText: true, disableHistory: true}) + this.onTermLookup(e, {disableScroll: true, selectText: true, disableHistory: true}); } onTermLookup(e, params) { - this.search.onTermLookup(e, params); + this.pendingLookup = true; + (async () => { + await this.search.onTermLookup(e, params); + this.pendingLookup = false; + })(); } async setText(text) { this.search.setSpinnerVisible(true); - - const previewTerms = []; - let previewText = text; - while (previewText) { - const tempText = previewText.slice(0, 2); - previewTerms.push([{text: tempText}]); - previewText = previewText.slice(2); - } - - this.queryParser.innerHTML = await apiTemplateRender('query-parser.html', { - terms: previewTerms, - preview: true - }); + await this.setPreview(text); // const results = await apiTextParse(text, this.search.getOptionsContext()); const results = await apiTextParseMecab(text, this.search.getOptionsContext()); const content = await apiTemplateRender('query-parser.html', { terms: results.map((term) => { - return term.map((part) => { - part.raw = !part.text.trim() && (!part.reading || !part.reading.trim()); - return part; + return term.filter(part => part.text.trim()).map((part) => { + return { + text: Array.from(part.text), + reading: part.reading, + raw: !part.reading || !part.reading.trim(), + }; }); }) }); this.queryParser.innerHTML = content; + this.queryParser.querySelectorAll('.query-parser-char').forEach((charElement) => { + this.activateScanning(charElement); + }); + this.search.setSpinnerVisible(false); } + async setPreview(text) { + const previewTerms = []; + while (text) { + const tempText = text.slice(0, 2); + previewTerms.push([{text: Array.from(tempText)}]); + text = text.slice(2); + } + + this.queryParser.innerHTML = await apiTemplateRender('query-parser.html', { + terms: previewTerms, + preview: true + }); + + this.queryParser.querySelectorAll('.query-parser-char').forEach((charElement) => { + this.activateScanning(charElement); + }); + } + + activateScanning(element) { + element.addEventListener('mouseenter', (e) => { + e.target.dataset.timer = setTimeout(() => { + this.onMouseEnter(e); + delete e.target.dataset.timer; + }, this.search.options.scanning.delay); + }); + element.addEventListener('mouseleave', (e) => { + clearTimeout(e.target.dataset.timer); + delete e.target.dataset.timer; + }); + } + async parseText(text) { const results = []; while (text) { @@ -106,22 +137,6 @@ class QueryParser { return results; } - popupTimerSet(callback) { - const delay = this.options.scanning.delay; - if (delay > 0) { - this.popupTimer = window.setTimeout(callback, delay); - } else { - Promise.resolve().then(callback); - } - } - - popupTimerClear() { - if (this.popupTimer !== null) { - window.clearTimeout(this.popupTimer); - this.popupTimer = null; - } - } - static isScanningModifierPressed(scanningModifier, mouseEvent) { switch (scanningModifier) { case 'alt': return mouseEvent.altKey; diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index cc233d49..6e377957 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -180,27 +180,31 @@ templates['query-parser.html'] = template({"1":function(container,depth0,helpers },"8":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.raw : depth0),{"name":"if","hash":{},"fn":container.program(9, data, 0),"inverse":container.program(11, data, 0),"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.raw : depth0),{"name":"if","hash":{},"fn":container.program(9, data, 0),"inverse":container.program(12, data, 0),"data":data})) != null ? stack1 : ""); },"9":function(container,depth0,helpers,partials,data) { - var helper; + var stack1; - return container.escapeExpression(((helper = (helper = helpers.text || (depth0 != null ? depth0.text : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"text","hash":{},"data":data}) : helper))); -},"11":function(container,depth0,helpers,partials,data) { - var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.text : depth0),{"name":"each","hash":{},"fn":container.program(10, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"10":function(container,depth0,helpers,partials,data) { + return "" + + container.escapeExpression(container.lambda(depth0, depth0)) + + ""; +},"12":function(container,depth0,helpers,partials,data) { + var stack1, helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}); return "" - + alias4(((helper = (helper = helpers.text || (depth0 != null ? depth0.text : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"text","hash":{},"data":data}) : helper))) + + ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.text : depth0),{"name":"each","hash":{},"fn":container.program(10, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "" - + alias4(((helper = (helper = helpers.reading || (depth0 != null ? depth0.reading : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"reading","hash":{},"data":data}) : helper))) + + container.escapeExpression(((helper = (helper = helpers.reading || (depth0 != null ? depth0.reading : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(alias1,{"name":"reading","hash":{},"data":data}) : helper))) + ""; -},"13":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"14":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return ((stack1 = container.invokePartial(partials.term,depth0,{"name":"term","hash":{"preview":(depths[1] != null ? depths[1].preview : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.terms : depth0),{"name":"each","hash":{},"fn":container.program(13, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.terms : depth0),{"name":"each","hash":{},"fn":container.program(14, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); },"main_d": function(fn, props, container, depth0, data, blockParams, depths) { var decorators = container.decorators; diff --git a/tmpl/query-parser.html b/tmpl/query-parser.html index 818650e6..db98b5ff 100644 --- a/tmpl/query-parser.html +++ b/tmpl/query-parser.html @@ -12,9 +12,13 @@ {{~#*inline "part"~}} {{~#if raw~}} -{{text}} +{{~#each text~}} +{{this}} +{{~/each~}} {{~else~}} -{{text}}{{reading}} +{{~#each text~}} +{{this}} +{{~/each~}}{{reading}} {{~/if~}} {{~/inline~}} -- cgit v1.2.3 From 8825c481b5139ce41ad227da4d7e8725666aa072 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 15:43:27 +0200 Subject: respect text selection option in query parser --- ext/bg/js/search-query-parser.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 8c3159cd..dfab0d9a 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -32,7 +32,8 @@ class QueryParser { } onClick(e) { - this.onTermLookup(e, {disableScroll: true, selectText: true}); + const selectText = this.search.options.scanning.selectText; + this.onTermLookup(e, {disableScroll: true, selectText}); } onMouseEnter(e) { @@ -52,7 +53,8 @@ class QueryParser { return; } - this.onTermLookup(e, {disableScroll: true, selectText: true, disableHistory: true}); + const selectText = this.search.options.scanning.selectText; + this.onTermLookup(e, {disableScroll: true, disableHistory: true, selectText}); } onTermLookup(e, params) { -- cgit v1.2.3 From c78ca36f3dca8a3ca32923bd65e84a59d0f51613 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 15:45:07 +0200 Subject: switch to mousemove events in query parser --- ext/bg/js/search-query-parser.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index dfab0d9a..cff48c46 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -36,7 +36,7 @@ class QueryParser { this.onTermLookup(e, {disableScroll: true, selectText}); } - onMouseEnter(e) { + onMouseMove(e) { if ( this.pendingLookup || (e.buttons & 0x1) !== 0x0 // Left mouse button @@ -112,11 +112,16 @@ class QueryParser { } activateScanning(element) { - element.addEventListener('mouseenter', (e) => { - e.target.dataset.timer = setTimeout(() => { - this.onMouseEnter(e); - delete e.target.dataset.timer; - }, this.search.options.scanning.delay); + 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) => { clearTimeout(e.target.dataset.timer); -- cgit v1.2.3 From bc66f254ea719bb254cafabe5eb127ec7c340b1e Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 17:04:32 +0200 Subject: click & text selection improvements on search page --- ext/bg/js/search-query-parser.js | 52 +++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index cff48c46..60b94ca8 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -21,25 +21,39 @@ class QueryParser { constructor(search) { this.search = search; this.pendingLookup = false; + this.clickScanPrevent = false; this.queryParser = document.querySelector('#query-parser'); - this.queryParser.addEventListener('click', (e) => this.onClick(e)); + this.queryParser.addEventListener('mousedown', (e) => this.onMouseDown(e)); + this.queryParser.addEventListener('mouseup', (e) => this.onMouseUp(e)); } onError(error) { logError(error, false); } - onClick(e) { - const selectText = this.search.options.scanning.selectText; - this.onTermLookup(e, {disableScroll: true, selectText}); + onMouseDown(e) { + if (QueryParser.isMouseButton('primary', e)) { + this.clickScanPrevent = false; + } + } + + onMouseUp(e) { + if ( + this.search.options.scanning.clickGlossary && + !this.clickScanPrevent && + QueryParser.isMouseButton('primary', e) + ) { + const selectText = this.search.options.scanning.selectText; + this.onTermLookup(e, {disableScroll: true, selectText}); + } } onMouseMove(e) { if ( this.pendingLookup || - (e.buttons & 0x1) !== 0x0 // Left mouse button + QueryParser.isMouseButton('primary', e) ) { return; } @@ -48,7 +62,7 @@ class QueryParser { const scanningModifier = scanningOptions.modifier; if (!( QueryParser.isScanningModifierPressed(scanningModifier, e) || - (scanningOptions.middleMouse && (e.buttons & 0x4) !== 0x0) // Middle mouse button + (scanningOptions.middleMouse && QueryParser.isMouseButton('auxiliary', e)) )) { return; } @@ -57,6 +71,12 @@ class QueryParser { this.onTermLookup(e, {disableScroll: true, disableHistory: true, selectText}); } + onMouseLeave(e) { + this.clickScanPrevent = true; + clearTimeout(e.target.dataset.timer); + delete e.target.dataset.timer; + } + onTermLookup(e, params) { this.pendingLookup = true; (async () => { @@ -124,8 +144,7 @@ class QueryParser { } }); element.addEventListener('mouseleave', (e) => { - clearTimeout(e.target.dataset.timer); - delete e.target.dataset.timer; + this.onMouseLeave(e); }); } @@ -153,4 +172,21 @@ class QueryParser { default: return false; } } + + static isMouseButton(button, mouseEvent) { + if (['mouseup', 'mousedown'].includes(mouseEvent.type)) { + switch (button) { + case 'primary': return mouseEvent.button === 0; + case 'secondary': return mouseEvent.button === 2; + case 'auxiliary': return mouseEvent.button === 1; + default: return false; + } + } + switch (button) { + case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; + case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; + case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; + default: return false; + } + } } -- cgit v1.2.3 From 515345ba0a9844ff55518779f799e4cf2a003d62 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 18:03:35 +0200 Subject: remove code duplication --- ext/bg/js/search-query-parser.js | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 60b94ca8..1cf00425 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -34,7 +34,7 @@ class QueryParser { } onMouseDown(e) { - if (QueryParser.isMouseButton('primary', e)) { + if (Frontend.isMouseButton('primary', e)) { this.clickScanPrevent = false; } } @@ -43,7 +43,7 @@ class QueryParser { if ( this.search.options.scanning.clickGlossary && !this.clickScanPrevent && - QueryParser.isMouseButton('primary', e) + Frontend.isMouseButton('primary', e) ) { const selectText = this.search.options.scanning.selectText; this.onTermLookup(e, {disableScroll: true, selectText}); @@ -51,18 +51,15 @@ class QueryParser { } onMouseMove(e) { - if ( - this.pendingLookup || - QueryParser.isMouseButton('primary', e) - ) { + if (this.pendingLookup || Frontend.isMouseButton('primary', e)) { return; } const scanningOptions = this.search.options.scanning; const scanningModifier = scanningOptions.modifier; if (!( - QueryParser.isScanningModifierPressed(scanningModifier, e) || - (scanningOptions.middleMouse && QueryParser.isMouseButton('auxiliary', e)) + Frontend.isScanningModifierPressed(scanningModifier, e) || + (scanningOptions.middleMouse && Frontend.isMouseButton('auxiliary', e)) )) { return; } @@ -162,31 +159,4 @@ class QueryParser { } return results; } - - static isScanningModifierPressed(scanningModifier, mouseEvent) { - switch (scanningModifier) { - case 'alt': return mouseEvent.altKey; - case 'ctrl': return mouseEvent.ctrlKey; - case 'shift': return mouseEvent.shiftKey; - case 'none': return true; - default: return false; - } - } - - static isMouseButton(button, mouseEvent) { - if (['mouseup', 'mousedown'].includes(mouseEvent.type)) { - switch (button) { - case 'primary': return mouseEvent.button === 0; - case 'secondary': return mouseEvent.button === 2; - case 'auxiliary': return mouseEvent.button === 1; - default: return false; - } - } - switch (button) { - case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; - case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; - case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; - default: return false; - } - } } -- cgit v1.2.3 From 955e131f9673e006556bc2c5e0b3551a614ccc48 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 5 Nov 2019 15:56:45 +0200 Subject: add parser selection options --- ext/bg/js/api.js | 37 +++++++++++++++++++++---------------- ext/bg/js/mecab.js | 2 +- ext/bg/js/options.js | 5 +++++ ext/bg/js/search-query-parser.js | 40 +++++++++++++++++++++++++--------------- ext/bg/js/settings.js | 6 ++++++ ext/bg/settings.html | 29 +++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 32 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 2ab01af3..967bded7 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -109,25 +109,30 @@ async function apiTextParseMecab(text, optionsContext) { const options = await apiOptionsGet(optionsContext); const mecab = utilBackend().mecab; - const results = []; - for (const parsedLine of await mecab.parseText(text)) { - for (const {expression, reading, source} of parsedLine) { - const term = []; - if (expression && reading) { - for (const {text, furigana} of jpDistributeFuriganaInflected( - expression, - jpKatakanaToHiragana(reading), - source - )) { - // can't use 'furigana' in templates - term.push({text, reading: furigana}); + const results = {}; + const rawResults = await mecab.parseText(text); + for (const mecabName in rawResults) { + const result = []; + for (const parsedLine of rawResults[mecabName]) { + for (const {expression, reading, source} of parsedLine) { + const term = []; + if (expression && reading) { + for (const {text, furigana} of jpDistributeFuriganaInflected( + expression, + jpKatakanaToHiragana(reading), + source + )) { + // can't use 'furigana' in templates + term.push({text, reading: furigana}); + } + } else { + term.push({text: source}); } - } else { - term.push({text: source}); + result.push(term); } - results.push(term); + result.push([{text: '\n'}]); } - results.push([{text: '\n'}]); + results[mecabName] = result; } return results; } diff --git a/ext/bg/js/mecab.js b/ext/bg/js/mecab.js index 14f68393..fba9b2eb 100644 --- a/ext/bg/js/mecab.js +++ b/ext/bg/js/mecab.js @@ -60,4 +60,4 @@ class Mecab { } } -Mecab.timeout = 1000; +Mecab.timeout = 5000; diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index be1ccfbb..f1bafaf9 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -311,6 +311,11 @@ function profileOptionsCreateDefaults() { dictionaries: {}, + parsing: { + enableScanningParser: true, + enableMecabParser: false + }, + anki: { enable: false, server: 'http://127.0.0.1:8765', diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 1cf00425..81eb18c3 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -86,22 +86,32 @@ class QueryParser { this.search.setSpinnerVisible(true); await this.setPreview(text); - // const results = await apiTextParse(text, this.search.getOptionsContext()); - const results = await apiTextParseMecab(text, this.search.getOptionsContext()); - - const content = await apiTemplateRender('query-parser.html', { - terms: results.map((term) => { - return term.filter(part => part.text.trim()).map((part) => { - return { - text: Array.from(part.text), - reading: part.reading, - raw: !part.reading || !part.reading.trim(), - }; - }); - }) - }); + const results = {}; + if (this.search.options.parsing.enableScanningParser) { + results['scan'] = await apiTextParse(text, this.search.getOptionsContext()); + } + if (this.search.options.parsing.enableMecabParser) { + let mecabResults = await apiTextParseMecab(text, this.search.getOptionsContext()); + for (const mecabDictName in mecabResults) { + results[`mecab-${mecabDictName}`] = mecabResults[mecabDictName]; + } + } - this.queryParser.innerHTML = content; + const contents = await Promise.all(Object.values(results).map(async result => { + return await apiTemplateRender('query-parser.html', { + terms: result.map((term) => { + return term.filter(part => part.text.trim()).map((part) => { + return { + text: Array.from(part.text), + reading: part.reading, + raw: !part.reading || !part.reading.trim(), + }; + }); + }) + }); + })); + + this.queryParser.innerHTML = contents.join('
'); this.queryParser.querySelectorAll('.query-parser-char').forEach((charElement) => { this.activateScanning(charElement); diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index e562c54e..f4fe032a 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -64,6 +64,9 @@ async function formRead(options) { options.scanning.modifier = $('#scan-modifier-key').val(); options.scanning.popupNestingMaxDepth = parseInt($('#popup-nesting-max-depth').val(), 10); + options.parsing.enableScanningParser = $('#parsing-scan-enable').prop('checked'); + options.parsing.enableMecabParser = $('#parsing-mecab-enable').prop('checked'); + const optionsAnkiEnableOld = options.anki.enable; options.anki.enable = $('#anki-enable').prop('checked'); options.anki.tags = utilBackgroundIsolate($('#card-tags').val().split(/[,; ]+/)); @@ -126,6 +129,9 @@ async function formWrite(options) { $('#scan-modifier-key').val(options.scanning.modifier); $('#popup-nesting-max-depth').val(options.scanning.popupNestingMaxDepth); + $('#parsing-scan-enable').prop('checked', options.parsing.enableScanningParser); + $('#parsing-mecab-enable').prop('checked', options.parsing.enableMecabParser); + $('#anki-enable').prop('checked', options.anki.enable); $('#card-tags').val(options.anki.tags.join(' ')); $('#sentence-detection-extent').val(options.anki.sentenceExt); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index bdcc11d3..8505567b 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -587,6 +587,35 @@ +
+

Text Parsing Options

+ +

+ Yomichan can attempt to parse entire sentences or longer text blocks on the search page, + adding furigana above words and a small space between words. +

+ +

+ Two types of parsers are supported. The first one, enabled by default, works using the built-in + scanning functionality by automatically advancing in the sentence after a matching word. +

+ +

+ The second type is an external program called MeCab + that uses its own dictionaries and a special parsing algorithm. To get it working, you must first + install it and a native messaging component + that acts as a bridge between the program and Yomichan. +

+ +
+ +
+ +
+ +
+
+
-- cgit v1.2.3 From 8d9a635d5c01e9a954284c82d1cddfab89f8dd5b Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 11 Nov 2019 01:03:35 +0200 Subject: remove dead code --- ext/bg/js/search-query-parser.js | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 81eb18c3..2e17688e 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -154,19 +154,4 @@ class QueryParser { this.onMouseLeave(e); }); } - - async parseText(text) { - const results = []; - while (text) { - const {definitions, length} = await apiTermsFind(text, {}, this.search.getOptionsContext()); - if (length) { - results.push(definitions); - text = text.slice(length); - } else { - results.push(text[0]); - text = text.slice(1); - } - } - return results; - } } -- cgit v1.2.3 From b336ab3a9a0a2ac9a569ae217b506ceeeab6fda0 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 11 Nov 2019 01:43:35 +0200 Subject: use const --- ext/bg/js/api.js | 4 ++-- ext/bg/js/search-query-parser.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 967bded7..40e9b6d2 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -86,9 +86,9 @@ async function apiTextParse(text, optionsContext) { const results = []; while (text) { const term = []; - let [definitions, sourceLength] = await translator.findTerms(text, {}, options); + const [definitions, sourceLength] = await translator.findTerms(text, {}, options); if (definitions.length > 0) { - definitions = dictTermsSort(definitions); + dictTermsSort(definitions); const {expression, reading} = definitions[0]; const source = text.slice(0, sourceLength); for (const {text, furigana} of jpDistributeFuriganaInflected(expression, reading, source)) { diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 2e17688e..f8e53963 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -113,9 +113,9 @@ class QueryParser { this.queryParser.innerHTML = contents.join('
'); - this.queryParser.querySelectorAll('.query-parser-char').forEach((charElement) => { + for (const charElement of this.queryParser.querySelectorAll('.query-parser-char')) { this.activateScanning(charElement); - }); + } this.search.setSpinnerVisible(false); } @@ -133,9 +133,9 @@ class QueryParser { preview: true }); - this.queryParser.querySelectorAll('.query-parser-char').forEach((charElement) => { + for (const charElement of this.queryParser.querySelectorAll('.query-parser-char')) { this.activateScanning(charElement); - }); + } } activateScanning(element) { -- cgit v1.2.3 From f97877a2097c8b27b75099e489b93db255cb68be Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 11 Nov 2019 02:28:00 +0200 Subject: promise improvements --- ext/bg/js/mecab.js | 6 ++---- ext/bg/js/search-query-parser.js | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/mecab.js b/ext/bg/js/mecab.js index 4c62c2b0..b9f2d0b3 100644 --- a/ext/bg/js/mecab.js +++ b/ext/bg/js/mecab.js @@ -58,12 +58,10 @@ class Mecab { const sequence = this.sequence++; this.listeners[sequence] = { - callback: (data) => { - resolve(data); - }, + callback: resolve, timer: setTimeout(() => { delete this.listeners[sequence]; - reject(`Mecab invoke timed out in ${Mecab.timeout} ms`); + reject(new Error(`Mecab invoke timed out in ${Mecab.timeout} ms`)); }, Mecab.timeout) } diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index f8e53963..2aee45dd 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -97,8 +97,8 @@ class QueryParser { } } - const contents = await Promise.all(Object.values(results).map(async result => { - return await apiTemplateRender('query-parser.html', { + const contents = await Promise.all(Object.values(results).map(result => { + return apiTemplateRender('query-parser.html', { terms: result.map((term) => { return term.filter(part => part.text.trim()).map((part) => { return { -- cgit v1.2.3 From b02a30a2fddb3b660a62f66541e90dba519cb270 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 11 Nov 2019 21:43:35 +0200 Subject: explicit checks in while and if --- ext/bg/js/api.js | 4 ++-- ext/bg/js/search-query-parser.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 40e9b6d2..bc9dfba1 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -84,7 +84,7 @@ async function apiTextParse(text, optionsContext) { const translator = utilBackend().translator; const results = []; - while (text) { + while (text.length > 0) { const term = []; const [definitions, sourceLength] = await translator.findTerms(text, {}, options); if (definitions.length > 0) { @@ -116,7 +116,7 @@ async function apiTextParseMecab(text, optionsContext) { for (const parsedLine of rawResults[mecabName]) { for (const {expression, reading, source} of parsedLine) { const term = []; - if (expression && reading) { + if (expression !== null && reading !== null) { for (const {text, furigana} of jpDistributeFuriganaInflected( expression, jpKatakanaToHiragana(reading), diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 2aee45dd..14b78105 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -122,7 +122,7 @@ class QueryParser { async setPreview(text) { const previewTerms = []; - while (text) { + while (text.length > 0) { const tempText = text.slice(0, 2); previewTerms.push([{text: Array.from(tempText)}]); text = text.slice(2); -- cgit v1.2.3 From 9dff658640d864fbabe063161b68e752a6bd3b3b Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 12 Nov 2019 23:57:21 +0200 Subject: add parser selection --- ext/bg/js/options.js | 3 +- ext/bg/js/search-query-parser.js | 113 ++++++++++++++++++++++++++++++--------- ext/bg/search.html | 7 ++- 3 files changed, 95 insertions(+), 28 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index f1bafaf9..abfe5fc6 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -313,7 +313,8 @@ function profileOptionsCreateDefaults() { parsing: { enableScanningParser: true, - enableMecabParser: false + enableMecabParser: false, + selectedParser = null }, anki: { diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 14b78105..15c394fe 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -23,7 +23,10 @@ class QueryParser { this.pendingLookup = false; this.clickScanPrevent = false; + this.parseResults = []; + this.queryParser = document.querySelector('#query-parser'); + this.queryParserSelect = document.querySelector('#query-parser-select'); this.queryParser.addEventListener('mousedown', (e) => this.onMouseDown(e)); this.queryParser.addEventListener('mouseup', (e) => this.onMouseUp(e)); @@ -82,42 +85,55 @@ class QueryParser { })(); } + onParserChange(e) { + const selectedParser = e.target.value; + apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); + this.renderParseResult(this.getParseResult()); + } + + getParseResult() { + return this.parseResults.find(r => r.id === this.search.options.parsing.selectedParser); + } + async setText(text) { this.search.setSpinnerVisible(true); + await this.setPreview(text); - const results = {}; + this.parseResults = await this.parseText(text); + if (this.parseResults.length > 0) { + if (this.search.options.parsing.selectedParser === null || !this.getParseResult()) { + const selectedParser = this.parseResults[0].id; + apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); + } + } + + this.renderParserSelect(); + await this.renderParseResult(); + + this.search.setSpinnerVisible(false); + } + + async parseText(text) { + const results = []; if (this.search.options.parsing.enableScanningParser) { - results['scan'] = await apiTextParse(text, this.search.getOptionsContext()); + results.push({ + name: 'Scanning parser', + id: 'scan', + parsedText: await apiTextParse(text, this.search.getOptionsContext()) + }); } if (this.search.options.parsing.enableMecabParser) { let mecabResults = await apiTextParseMecab(text, this.search.getOptionsContext()); for (const mecabDictName in mecabResults) { - results[`mecab-${mecabDictName}`] = mecabResults[mecabDictName]; + results.push({ + name: `MeCab: ${mecabDictName}`, + id: `mecab-${mecabDictName}`, + parsedText: mecabResults[mecabDictName] + }); } } - - const contents = await Promise.all(Object.values(results).map(result => { - return apiTemplateRender('query-parser.html', { - terms: result.map((term) => { - return term.filter(part => part.text.trim()).map((part) => { - return { - text: Array.from(part.text), - reading: part.reading, - raw: !part.reading || !part.reading.trim(), - }; - }); - }) - }); - })); - - this.queryParser.innerHTML = contents.join('
'); - - for (const charElement of this.queryParser.querySelectorAll('.query-parser-char')) { - this.activateScanning(charElement); - } - - this.search.setSpinnerVisible(false); + return results; } async setPreview(text) { @@ -127,7 +143,6 @@ class QueryParser { previewTerms.push([{text: Array.from(tempText)}]); text = text.slice(2); } - this.queryParser.innerHTML = await apiTemplateRender('query-parser.html', { terms: previewTerms, preview: true @@ -138,6 +153,40 @@ class QueryParser { } } + renderParserSelect() { + this.queryParserSelect.innerHTML = ''; + if (this.parseResults.length > 1) { + const select = document.createElement('select'); + select.classList.add('form-control'); + for (const parseResult of this.parseResults) { + const option = document.createElement('option'); + option.value = parseResult.id; + option.innerText = parseResult.name; + option.defaultSelected = this.search.options.parsing.selectedParser === parseResult.id; + select.appendChild(option); + } + select.addEventListener('change', this.onParserChange.bind(this)); + this.queryParserSelect.appendChild(select); + } + } + + async renderParseResult() { + const parseResult = this.getParseResult(); + if (!parseResult) { + this.queryParser.innerHTML = ''; + return; + } + + this.queryParser.innerHTML = await apiTemplateRender( + 'query-parser.html', + {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); @@ -154,4 +203,16 @@ class QueryParser { this.onMouseLeave(e); }); } + + static processParseResultForDisplay(result) { + return result.map((term) => { + return term.filter(part => part.text.trim()).map((part) => { + return { + text: Array.from(part.text), + reading: part.reading, + raw: !part.reading || !part.reading.trim(), + }; + }); + }); + } } diff --git a/ext/bg/search.html b/ext/bg/search.html index 48e7dbf5..e819ebe6 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -47,7 +47,12 @@
-
+
+
+
+
+ +
-- cgit v1.2.3 From 707b039927f27443f9e32cffe342e7f778dff955 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 13 Nov 2019 13:09:23 +0200 Subject: store local copy of selected parser Options don't update early enough even after awaiting --- ext/bg/js/search-query-parser.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 15c394fe..71ebb389 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -24,6 +24,7 @@ class QueryParser { this.clickScanPrevent = false; this.parseResults = []; + this.selectedParser = null; this.queryParser = document.querySelector('#query-parser'); this.queryParserSelect = document.querySelector('#query-parser-select'); @@ -85,14 +86,15 @@ class QueryParser { })(); } - onParserChange(e) { + async onParserChange(e) { const selectedParser = e.target.value; + this.selectedParser = selectedParser; apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); this.renderParseResult(this.getParseResult()); } getParseResult() { - return this.parseResults.find(r => r.id === this.search.options.parsing.selectedParser); + return this.parseResults.find(r => r.id === this.selectedParser); } async setText(text) { @@ -102,8 +104,12 @@ class QueryParser { this.parseResults = await this.parseText(text); if (this.parseResults.length > 0) { - if (this.search.options.parsing.selectedParser === null || !this.getParseResult()) { + if (this.selectedParser === null) { + this.selectedParser = this.search.options.parsing.selectedParser; + } + if (this.selectedParser === null || !this.getParseResult()) { const selectedParser = this.parseResults[0].id; + this.selectedParser = selectedParser; apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); } } @@ -162,7 +168,7 @@ class QueryParser { const option = document.createElement('option'); option.value = parseResult.id; option.innerText = parseResult.name; - option.defaultSelected = this.search.options.parsing.selectedParser === parseResult.id; + option.defaultSelected = this.selectedParser === parseResult.id; select.appendChild(option); } select.addEventListener('change', this.onParserChange.bind(this)); -- cgit v1.2.3 From 933fd77957cac4027c389b43cddaeb3ea65d9faa Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 13 Nov 2019 13:11:41 +0200 Subject: remove async --- ext/bg/js/search-query-parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 71ebb389..c0cad773 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -86,7 +86,7 @@ class QueryParser { })(); } - async onParserChange(e) { + onParserChange(e) { const selectedParser = e.target.value; this.selectedParser = selectedParser; apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); -- cgit v1.2.3 From f6253216505737d588ccff1f07cb5ce0332297a5 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 13 Nov 2019 15:29:53 +0200 Subject: refactor selected parser refreshing --- ext/bg/js/search-query-parser.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'ext/bg/js/search-query-parser.js') diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index c0cad773..42e53989 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -93,16 +93,7 @@ class QueryParser { this.renderParseResult(this.getParseResult()); } - getParseResult() { - return this.parseResults.find(r => r.id === this.selectedParser); - } - - async setText(text) { - this.search.setSpinnerVisible(true); - - await this.setPreview(text); - - this.parseResults = await this.parseText(text); + refreshSelectedParser() { if (this.parseResults.length > 0) { if (this.selectedParser === null) { this.selectedParser = this.search.options.parsing.selectedParser; @@ -113,6 +104,19 @@ class QueryParser { apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); } } + } + + getParseResult() { + return this.parseResults.find(r => r.id === this.selectedParser); + } + + async setText(text) { + this.search.setSpinnerVisible(true); + + await this.setPreview(text); + + this.parseResults = await this.parseText(text); + this.refreshSelectedParser(); this.renderParserSelect(); await this.renderParseResult(); -- cgit v1.2.3