frontend cleanup
This commit is contained in:
parent
211e5d1155
commit
3362a68e06
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
|
||||||
* Author: Alex Yatskov <alex@foosoft.net>
|
* Author: Alex Yatskov <alex@foosoft.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
@ -21,10 +21,10 @@ class Frontend {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.popup = new Popup();
|
this.popup = new Popup();
|
||||||
this.popupTimer = null;
|
this.popupTimer = null;
|
||||||
this.lastMousePos = null;
|
this.mousePosLast = null;
|
||||||
this.mouseDownLeft = false;
|
this.mouseDownLeft = false;
|
||||||
this.mouseDownMiddle = false;
|
this.mouseDownMiddle = false;
|
||||||
this.lastTextSource = null;
|
this.textSourceLast = null;
|
||||||
this.pendingLookup = false;
|
this.pendingLookup = false;
|
||||||
this.options = null;
|
this.options = null;
|
||||||
}
|
}
|
||||||
@ -53,7 +53,7 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMouseMove(e) {
|
onMouseMove(e) {
|
||||||
this.lastMousePos = {x: e.clientX, y: e.clientY};
|
this.mousePosLast = {x: e.clientX, y: e.clientY};
|
||||||
this.popupTimerClear();
|
this.popupTimerClear();
|
||||||
|
|
||||||
if (!this.options.general.enable) {
|
if (!this.options.general.enable) {
|
||||||
@ -75,7 +75,7 @@ class Frontend {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const searchFunc = () => this.searchAt(this.lastMousePos);
|
const searchFunc = () => this.searchAt(this.mousePosLast);
|
||||||
if (this.options.scanning.modifier === 'none') {
|
if (this.options.scanning.modifier === 'none') {
|
||||||
this.popupTimerSet(searchFunc);
|
this.popupTimerSet(searchFunc);
|
||||||
} else {
|
} else {
|
||||||
@ -84,7 +84,7 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMouseDown(e) {
|
onMouseDown(e) {
|
||||||
this.lastMousePos = {x: e.clientX, y: e.clientY};
|
this.mousePosLast = {x: e.clientX, y: e.clientY};
|
||||||
this.popupTimerClear();
|
this.popupTimerClear();
|
||||||
this.searchClear();
|
this.searchClear();
|
||||||
|
|
||||||
@ -143,13 +143,7 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onError(error) {
|
onError(error) {
|
||||||
if (window.yomichan_orphaned) {
|
window.alert(`Error: ${error}`);
|
||||||
if (this.lastTextSource && this.options.scanning.modifier !== 'none') {
|
|
||||||
this.popup.showOrphaned(this.lastTextSource.getRect(), this.options);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
window.alert(`Error: ${error}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
popupTimerSet(callback) {
|
popupTimerSet(callback) {
|
||||||
@ -165,18 +159,20 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async searchAt(point) {
|
async searchAt(point) {
|
||||||
|
let textSource = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.pendingLookup) {
|
if (this.pendingLookup) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const textSource = docRangeFromPoint(point);
|
textSource = docRangeFromPoint(point);
|
||||||
if (!textSource || !textSource.containsPoint(point)) {
|
if (!textSource || !textSource.containsPoint(point)) {
|
||||||
docImposterDestroy();
|
docImposterDestroy();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.lastTextSource && this.lastTextSource.equals(textSource)) {
|
if (this.textSourceLast && this.textSourceLast.equals(textSource)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,7 +182,13 @@ class Frontend {
|
|||||||
await this.searchKanji(textSource);
|
await this.searchKanji(textSource);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.onError(e);
|
if (window.yomichan_orphaned) {
|
||||||
|
if (textSource && this.options.scanning.modifier !== 'none') {
|
||||||
|
this.popup.showOrphaned(textSource.getRect(), this.options);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.onError(e);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
docImposterDestroy();
|
docImposterDestroy();
|
||||||
this.pendingLookup = false;
|
this.pendingLookup = false;
|
||||||
@ -212,7 +214,7 @@ class Frontend {
|
|||||||
{sentence, url}
|
{sentence, url}
|
||||||
);
|
);
|
||||||
|
|
||||||
this.lastTextSource = textSource;
|
this.textSourceLast = textSource;
|
||||||
if (this.options.scanning.selectText) {
|
if (this.options.scanning.selectText) {
|
||||||
textSource.select();
|
textSource.select();
|
||||||
}
|
}
|
||||||
@ -237,7 +239,7 @@ class Frontend {
|
|||||||
{sentence, url}
|
{sentence, url}
|
||||||
);
|
);
|
||||||
|
|
||||||
this.lastTextSource = textSource;
|
this.textSourceLast = textSource;
|
||||||
if (this.options.scanning.selectText) {
|
if (this.options.scanning.selectText) {
|
||||||
textSource.select();
|
textSource.select();
|
||||||
}
|
}
|
||||||
@ -249,11 +251,11 @@ class Frontend {
|
|||||||
docImposterDestroy();
|
docImposterDestroy();
|
||||||
this.popup.hide();
|
this.popup.hide();
|
||||||
|
|
||||||
if (this.options.scanning.selectText && this.lastTextSource) {
|
if (this.options.scanning.selectText && this.textSourceLast) {
|
||||||
this.lastTextSource.deselect();
|
this.textSourceLast.deselect();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.lastTextSource = null;
|
this.textSourceLast = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
|
||||||
* Author: Alex Yatskov <alex@foosoft.net>
|
* Author: Alex Yatskov <alex@foosoft.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
@ -87,6 +87,11 @@ class Popup {
|
|||||||
this.container.style.visibility = 'visible';
|
this.container.style.visibility = 'visible';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async showOrphaned(elementRect, options) {
|
||||||
|
await this.show(elementRect, options);
|
||||||
|
this.invokeApi('orphaned');
|
||||||
|
}
|
||||||
|
|
||||||
hide() {
|
hide() {
|
||||||
this.container.style.visibility = 'hidden';
|
this.container.style.visibility = 'hidden';
|
||||||
}
|
}
|
||||||
@ -108,9 +113,4 @@ class Popup {
|
|||||||
invokeApi(action, params={}) {
|
invokeApi(action, params={}) {
|
||||||
this.container.contentWindow.postMessage({action, params}, '*');
|
this.container.contentWindow.postMessage({action, params}, '*');
|
||||||
}
|
}
|
||||||
|
|
||||||
async onOrphaned(elementRect, options) {
|
|
||||||
await this.show(elementRect, options);
|
|
||||||
this.invokeApi('orphaned');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
|
||||||
* Author: Alex Yatskov <alex@foosoft.net>
|
* Author: Alex Yatskov <alex@foosoft.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
@ -17,6 +17,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TextSourceRange
|
||||||
|
*/
|
||||||
|
|
||||||
class TextSourceRange {
|
class TextSourceRange {
|
||||||
constructor(range, content='') {
|
constructor(range, content='') {
|
||||||
this.range = range;
|
this.range = range;
|
||||||
@ -176,6 +180,10 @@ class TextSourceRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TextSourceElement
|
||||||
|
*/
|
||||||
|
|
||||||
class TextSourceElement {
|
class TextSourceElement {
|
||||||
constructor(element, content='') {
|
constructor(element, content='') {
|
||||||
this.element = element;
|
this.element = element;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
|
||||||
* Author: Alex Yatskov <alex@foosoft.net>
|
* Author: Alex Yatskov <alex@foosoft.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
Loading…
Reference in New Issue
Block a user