Make the imposter element tracked using TextSourceRange
This commit is contained in:
parent
e47e041217
commit
ad0dca7bb1
@ -70,12 +70,6 @@ function docImposterCreate(element) {
|
|||||||
return imposter;
|
return imposter;
|
||||||
}
|
}
|
||||||
|
|
||||||
function docImposterDestroy() {
|
|
||||||
for (const element of document.getElementsByClassName('yomichan-imposter')) {
|
|
||||||
element.parentNode.removeChild(element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function docRangeFromPoint(point) {
|
function docRangeFromPoint(point) {
|
||||||
const element = document.elementFromPoint(point.x, point.y);
|
const element = document.elementFromPoint(point.x, point.y);
|
||||||
let imposter = null;
|
let imposter = null;
|
||||||
@ -92,12 +86,18 @@ function docRangeFromPoint(point) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const range = document.caretRangeFromPoint(point.x, point.y);
|
const range = document.caretRangeFromPoint(point.x, point.y);
|
||||||
if (imposter !== null) {
|
if (range !== null && isPointInRange(point, range)) {
|
||||||
imposter.style.zIndex = -2147483646;
|
if (imposter !== null) {
|
||||||
imposter.style.pointerEvents = 'none';
|
imposter.style.zIndex = -2147483646;
|
||||||
|
imposter.style.pointerEvents = 'none';
|
||||||
|
}
|
||||||
|
return new TextSourceRange(range, '', imposter);
|
||||||
|
} else {
|
||||||
|
if (imposter !== null) {
|
||||||
|
imposter.parentNode.removeChild(imposter);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return range !== null && isPointInRange(point, range) ? new TextSourceRange(range) : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function docSentenceExtract(source, extent) {
|
function docSentenceExtract(source, extent) {
|
||||||
|
@ -307,10 +307,11 @@ class Frontend {
|
|||||||
this.onError(e);
|
this.onError(e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
if (textSource !== null) {
|
||||||
|
textSource.cleanup();
|
||||||
|
}
|
||||||
if (hideResults && this.options.scanning.autoHideResults) {
|
if (hideResults && this.options.scanning.autoHideResults) {
|
||||||
this.searchClear();
|
this.searchClear();
|
||||||
} else {
|
|
||||||
docImposterDestroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.pendingLookup = false;
|
this.pendingLookup = false;
|
||||||
@ -371,7 +372,6 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
searchClear() {
|
searchClear() {
|
||||||
docImposterDestroy();
|
|
||||||
this.popup.hide();
|
this.popup.hide();
|
||||||
this.popup.clearAutoPlayTimer();
|
this.popup.clearAutoPlayTimer();
|
||||||
|
|
||||||
|
@ -25,13 +25,20 @@ const IGNORE_TEXT_PATTERN = /\u200c/;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class TextSourceRange {
|
class TextSourceRange {
|
||||||
constructor(range, content='') {
|
constructor(range, content, imposter) {
|
||||||
this.range = range;
|
this.range = range;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
this.imposter = imposter;
|
||||||
}
|
}
|
||||||
|
|
||||||
clone() {
|
clone() {
|
||||||
return new TextSourceRange(this.range.cloneRange(), this.content);
|
return new TextSourceRange(this.range.cloneRange(), this.content, this.imposter);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
if (this.imposter !== null && this.imposter.parentNode !== null) {
|
||||||
|
this.imposter.parentNode.removeChild(this.imposter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
@ -221,6 +228,10 @@ class TextSourceElement {
|
|||||||
return new TextSourceElement(this.element, this.content);
|
return new TextSourceElement(this.element, this.content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
// NOP
|
||||||
|
}
|
||||||
|
|
||||||
text() {
|
text() {
|
||||||
return this.content;
|
return this.content;
|
||||||
}
|
}
|
||||||
|
@ -84,17 +84,23 @@ class Display {
|
|||||||
if (textSource === null) {
|
if (textSource === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
textSource.setEndOffset(this.options.scanning.length);
|
|
||||||
|
|
||||||
const {definitions, length} = await apiTermsFind(textSource.text());
|
let definitions, length, sentence;
|
||||||
if (definitions.length === 0) {
|
try {
|
||||||
return false;
|
textSource.setEndOffset(this.options.scanning.length);
|
||||||
|
|
||||||
|
({definitions, length} = await apiTermsFind(textSource.text()));
|
||||||
|
if (definitions.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
textSource.setEndOffset(length);
|
||||||
|
|
||||||
|
sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
|
||||||
|
} finally {
|
||||||
|
textSource.cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
textSource.setEndOffset(length);
|
|
||||||
|
|
||||||
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
|
|
||||||
|
|
||||||
const context = {
|
const context = {
|
||||||
source: {
|
source: {
|
||||||
definitions: this.definitions,
|
definitions: this.definitions,
|
||||||
|
Loading…
Reference in New Issue
Block a user