diff options
Diffstat (limited to 'ext/bg/js/yomichan.js')
-rw-r--r-- | ext/bg/js/yomichan.js | 96 |
1 files changed, 25 insertions, 71 deletions
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 12dd89ac..2cdcf1c8 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -21,6 +21,7 @@ class Yomichan { constructor() { Handlebars.partials = Handlebars.templates; Handlebars.registerHelper('kanjiLinks', kanjiLinks); + Handlebars.registerHelper('multiLine', multiLine); this.translator = new Translator(); this.anki = new AnkiNull(); @@ -117,75 +118,6 @@ class Yomichan { chrome.tabs.sendMessage(tabId, {action, params}, () => null); } - formatField(field, definition, mode) { - const markers = [ - 'audio', - 'character', - 'expression', - 'expression-furigana', - 'glossary', - 'glossary-list', - 'kunyomi', - 'onyomi', - 'reading', - 'sentence', - 'tags', - 'url', - ]; - - for (const marker of markers) { - let value = definition[marker] || null; - switch (marker) { - case 'audio': - value = ''; - break; - case 'expression': - if (mode === 'term_kana' && definition.reading) { - value = definition.reading; - } - break; - case 'expression-furigana': - if (mode === 'term_kana' && definition.reading) { - value = definition.reading; - } else { - value = `<ruby>${definition.expression}<rt>${definition.reading}</rt></ruby>`; - } - break; - case 'reading': - if (mode === 'term_kana') { - value = null; - } - break; - case 'glossary-list': - if (definition.glossary) { - if (definition.glossary.length > 1) { - value = '<ol style="white-space: pre; text-align: left; overflow-x: auto;">'; - for (const gloss of definition.glossary) { - value += `<li>${gloss}</li>`; - } - value += '</ol>'; - } else { - value = `<p style="white-space: pre; overflow-x: auto;">${definition.glossary.join('')}</p>`; - } - } - break; - case 'tags': - if (definition.tags) { - value = definition.tags.map(t => t.name); - } - break; - } - - if (value !== null && typeof(value) !== 'string') { - value = value.join(', '); - } - - field = field.replace(`{${marker}}`, value || ''); - } - - return field; - } - formatNote(definition, mode) { const note = {fields: {}, tags: this.options.ankiCardTags}; @@ -217,7 +149,7 @@ class Yomichan { } for (const name in fields) { - note.fields[name] = this.formatField(fields[name], definition, mode); + note.fields[name] = formatField(fields[name], definition, mode); } return note; @@ -254,7 +186,29 @@ class Yomichan { } promiseCallback( - this.translator.findTerm(text, dictionaries, this.options.enableSoftKatakanaSearch), + this.translator.findTerm( + text, + dictionaries, + this.options.enableSoftKatakanaSearch + ), + callback + ); + } + + api_findTermGrouped({text, callback}) { + const dictionaries = []; + for (const title in this.options.dictionaries) { + if (this.options.dictionaries[title].enableTerms) { + dictionaries.push(title); + } + } + + promiseCallback( + this.translator.findTermGrouped( + text, + dictionaries, + this.options.enableSoftKatakanaSearch + ), callback ); } |