From 3362a68e06a16efa87a2ad7cc75f18f8f4b2ea25 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 15 Aug 2017 20:04:15 -0700 Subject: [PATCH] frontend cleanup --- ext/fg/js/frontend.js | 44 ++++++++++++++++++++++--------------------- ext/fg/js/popup.js | 12 ++++++------ ext/fg/js/source.js | 10 +++++++++- ext/fg/js/util.js | 2 +- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 7d26f946..58a721bf 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -21,10 +21,10 @@ class Frontend { constructor() { this.popup = new Popup(); this.popupTimer = null; - this.lastMousePos = null; + this.mousePosLast = null; this.mouseDownLeft = false; this.mouseDownMiddle = false; - this.lastTextSource = null; + this.textSourceLast = null; this.pendingLookup = false; this.options = null; } @@ -53,7 +53,7 @@ class Frontend { } onMouseMove(e) { - this.lastMousePos = {x: e.clientX, y: e.clientY}; + this.mousePosLast = {x: e.clientX, y: e.clientY}; this.popupTimerClear(); if (!this.options.general.enable) { @@ -75,7 +75,7 @@ class Frontend { return; } - const searchFunc = () => this.searchAt(this.lastMousePos); + const searchFunc = () => this.searchAt(this.mousePosLast); if (this.options.scanning.modifier === 'none') { this.popupTimerSet(searchFunc); } else { @@ -84,7 +84,7 @@ class Frontend { } onMouseDown(e) { - this.lastMousePos = {x: e.clientX, y: e.clientY}; + this.mousePosLast = {x: e.clientX, y: e.clientY}; this.popupTimerClear(); this.searchClear(); @@ -143,13 +143,7 @@ class Frontend { } onError(error) { - if (window.yomichan_orphaned) { - if (this.lastTextSource && this.options.scanning.modifier !== 'none') { - this.popup.showOrphaned(this.lastTextSource.getRect(), this.options); - } - } else { - window.alert(`Error: ${error}`); - } + window.alert(`Error: ${error}`); } popupTimerSet(callback) { @@ -165,18 +159,20 @@ class Frontend { } async searchAt(point) { + let textSource = null; + try { if (this.pendingLookup) { return; } - const textSource = docRangeFromPoint(point); + textSource = docRangeFromPoint(point); if (!textSource || !textSource.containsPoint(point)) { docImposterDestroy(); return; } - if (this.lastTextSource && this.lastTextSource.equals(textSource)) { + if (this.textSourceLast && this.textSourceLast.equals(textSource)) { return; } @@ -186,7 +182,13 @@ class Frontend { await this.searchKanji(textSource); } } 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 { docImposterDestroy(); this.pendingLookup = false; @@ -212,7 +214,7 @@ class Frontend { {sentence, url} ); - this.lastTextSource = textSource; + this.textSourceLast = textSource; if (this.options.scanning.selectText) { textSource.select(); } @@ -237,7 +239,7 @@ class Frontend { {sentence, url} ); - this.lastTextSource = textSource; + this.textSourceLast = textSource; if (this.options.scanning.selectText) { textSource.select(); } @@ -249,11 +251,11 @@ class Frontend { docImposterDestroy(); this.popup.hide(); - if (this.options.scanning.selectText && this.lastTextSource) { - this.lastTextSource.deselect(); + if (this.options.scanning.selectText && this.textSourceLast) { + this.textSourceLast.deselect(); } - this.lastTextSource = null; + this.textSourceLast = null; } } diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 8cb16b5a..03958832 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -87,6 +87,11 @@ class Popup { this.container.style.visibility = 'visible'; } + async showOrphaned(elementRect, options) { + await this.show(elementRect, options); + this.invokeApi('orphaned'); + } + hide() { this.container.style.visibility = 'hidden'; } @@ -108,9 +113,4 @@ class Popup { invokeApi(action, params={}) { this.container.contentWindow.postMessage({action, params}, '*'); } - - async onOrphaned(elementRect, options) { - await this.show(elementRect, options); - this.invokeApi('orphaned'); - } } diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index 210dda12..3b6ecb2a 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify @@ -17,6 +17,10 @@ */ +/* + * TextSourceRange + */ + class TextSourceRange { constructor(range, content='') { this.range = range; @@ -176,6 +180,10 @@ class TextSourceRange { } +/* + * TextSourceElement + */ + class TextSourceElement { constructor(element, content='') { this.element = element; diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 3faf3b47..5eff4018 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Alex Yatskov + * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify