inject doc functions as dependencies

This commit is contained in:
siikamiika 2019-08-03 15:06:28 +03:00
parent 0ee6d05cb1
commit afc771de9f
3 changed files with 43 additions and 35 deletions

View File

@ -25,6 +25,8 @@ class DisplaySearch extends Display {
this.query = $('#query').on('input', this.onSearchInput.bind(this));
this.intro = $('#intro');
this.dependencies = {...this.dependencies, ...{docRangeFromPoint, docSentenceExtract}};
window.wanakana.bind(this.query.get(0));
}

View File

@ -23,6 +23,8 @@ class DisplayFloat extends Display {
this.autoPlayAudioTimer = null;
this.styleNode = null;
this.dependencies = {...this.dependencies, ...{docRangeFromPoint, docSentenceExtract}};
$(window).on('message', utilAsync(this.onMessage.bind(this)));
}
@ -123,41 +125,6 @@ class DisplayFloat extends Display {
parent.appendChild(this.styleNode);
}
}
async onTermLookup(e) {
try {
e.preventDefault();
const clickedElement = $(e.target);
const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY});
textSource.setEndOffset(this.options.scanning.length);
const {definitions, length} = await apiTermsFind(textSource.text());
if (definitions.length === 0) {
return false;
}
textSource.setEndOffset(length);
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const context = {
source: {
definitions: this.definitions,
index: Display.entryIndexFind(clickedElement)
}
};
if (this.context) {
context.sentence = sentence;
context.url = this.context.url;
}
this.termsShow(definitions, this.options, context);
} catch (e) {
this.onError(e);
}
}
}
window.yomichan_display = new DisplayFloat();

View File

@ -28,6 +28,8 @@ class Display {
this.index = 0;
this.audioCache = {};
this.dependencies = {};
$(document).keydown(this.onKeyDown.bind(this));
$(document).on('wheel', this.onWheel.bind(this));
}
@ -69,6 +71,43 @@ class Display {
}
}
async onTermLookup(e) {
try {
e.preventDefault();
const {docRangeFromPoint, docSentenceExtract} = this.dependencies;
const clickedElement = $(e.target);
const textSource = docRangeFromPoint({x: e.clientX, y: e.clientY});
textSource.setEndOffset(this.options.scanning.length);
const {definitions, length} = await apiTermsFind(textSource.text());
if (definitions.length === 0) {
return false;
}
textSource.setEndOffset(length);
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const context = {
source: {
definitions: this.definitions,
index: Display.entryIndexFind(clickedElement)
}
};
if (this.context) {
context.sentence = sentence;
context.url = this.context.url;
}
this.termsShow(definitions, this.options, context);
} catch (e) {
this.onError(e);
}
}
onAudioPlay(e) {
e.preventDefault();
const link = $(e.currentTarget);