aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-08-13 16:11:51 -0700
committerAlex Yatskov <alex@foosoft.net>2017-08-13 16:11:51 -0700
commitaac2a58b5f821c6f90c95bb19f3b0a755d5e1739 (patch)
treed6c4b503f6a8daeabfa15f757866ef18ef4c5c64 /ext/fg/js
parent7fbe2ddaf33bad05fb26aec759806e0f6ae250d2 (diff)
wip
Diffstat (limited to 'ext/fg/js')
-rw-r--r--ext/fg/js/display-frame.js58
-rw-r--r--ext/fg/js/frontend.js141
-rw-r--r--ext/fg/js/util.js2
3 files changed, 95 insertions, 106 deletions
diff --git a/ext/fg/js/display-frame.js b/ext/fg/js/display-frame.js
index 09bd9255..5ea376c2 100644
--- a/ext/fg/js/display-frame.js
+++ b/ext/fg/js/display-frame.js
@@ -17,65 +17,45 @@
*/
-window.displayFrame = new class extends Display {
+window.yomichan_frame = new class extends Display {
constructor() {
super($('#spinner'), $('#content'));
$(window).on('message', this.onMessage.bind(this));
}
- definitionAdd(definition, mode) {
- return apiDefinitionAdd(definition, mode);
- }
-
- definitionsAddable(definitions, modes) {
- return apiDefinitionsAddable(definitions, modes);
- }
-
- noteView(noteId) {
- return apiNoteView(noteId);
- }
-
- templateRender(template, data) {
- return apiTemplateRender(template, data);
- }
-
- kanjiFind(character) {
- return apiKanjiFind(character);
- }
-
- handleError(error) {
- if (window.yomichanOrphaned) {
- this.showOrphaned();
+ onError(error) {
+ if (window.yomichan_orphaned) {
+ this.onOrphaned();
} else {
window.alert(`Error: ${error}`);
}
}
- clearSearch() {
- window.parent.postMessage('popupClose', '*');
+ onOrphaned() {
+ $('#definitions').hide();
+ $('#error-orphaned').show();
}
- selectionCopy() {
- window.parent.postMessage('selectionCopy', '*');
+ onSearchClear() {
+ window.parent.postMessage('popupClose', '*');
}
- showOrphaned() {
- $('#content').hide();
- $('#orphan').show();
+ onSelectionCopy() {
+ window.parent.postMessage('selectionCopy', '*');
}
onMessage(e) {
const handlers = {
- showTermDefs: ({definitions, options, context}) => {
- this.showTermDefs(definitions, options, context);
+ termsShow: ({definitions, options, context}) => {
+ this.termsShow(definitions, options, context);
},
- showKanjiDefs: ({definitions, options, context}) => {
- this.showKanjiDefs(definitions, options, context);
+ kanjiShow: ({definitions, options, context}) => {
+ this.kanjiShow(definitions, options, context);
},
- showOrphaned: () => {
- this.showOrphaned();
+ orphaned: () => {
+ this.onOrphaned();
}
};
@@ -89,8 +69,8 @@ window.displayFrame = new class extends Display {
onKeyDown(e) {
const handlers = {
67: /* c */ () => {
- if (e.ctrlKey && window.getSelection().toString() === '') {
- this.selectionCopy();
+ if (e.ctrlKey && !window.getSelection().toString()) {
+ this.onSelectionCopy();
return true;
}
}
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index 9974d878..37389766 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -17,7 +17,7 @@
*/
-window.yomichanFrontend = new class {
+window.yomichan_frontend = new class {
constructor() {
this.popup = new Popup();
this.popupTimer = null;
@@ -27,17 +27,23 @@ window.yomichanFrontend = new class {
this.lastTextSource = null;
this.pendingLookup = false;
this.options = null;
+ }
+
+ async prepare() {
+ try {
+ this.options = await apiOptionsGet();
+ } catch (e) {
+ this.onError(e);
+ }
- apiOptionsGet().then(options => {
- this.options = options;
- window.addEventListener('mouseover', this.onMouseOver.bind(this));
- window.addEventListener('mousedown', this.onMouseDown.bind(this));
- window.addEventListener('mouseup', this.onMouseUp.bind(this));
- window.addEventListener('mousemove', this.onMouseMove.bind(this));
- window.addEventListener('resize', e => this.searchClear());
- window.addEventListener('message', this.onFrameMessage.bind(this));
- chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
- }).catch(this.handleError.bind(this));
+ window.addEventListener('message', this.onFrameMessage.bind(this));
+ window.addEventListener('mousedown', this.onMouseDown.bind(this));
+ window.addEventListener('mousemove', this.onMouseMove.bind(this));
+ window.addEventListener('mouseover', this.onMouseOver.bind(this));
+ window.addEventListener('mouseup', this.onMouseUp.bind(this));
+ window.addEventListener('resize', this.onResize.bind(this));
+
+ chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
}
popupTimerSet(callback) {
@@ -144,7 +150,11 @@ window.yomichanFrontend = new class {
callback();
}
- searchAt(point) {
+ onResize() {
+ this.onSearchClear();
+ }
+
+ async searchAt(point) {
if (this.pendingLookup) {
return;
}
@@ -160,70 +170,69 @@ window.yomichanFrontend = new class {
}
this.pendingLookup = true;
- this.searchTerms(textSource).then(found => {
- if (!found) {
- return this.searchKanji(textSource);
+
+ try {
+ if (!await this.searchTerms(textSource)) {
+ await this.searchKanji(textSource);
}
- }).catch(error => {
- this.handleError(error, textSource);
- }).then(() => {
- docImposterDestroy();
- this.pendingLookup = false;
- });
+ } catch (e) {
+ this.onError(e);
+ }
+
+ docImposterDestroy();
+ this.pendingLookup = false;
}
- searchTerms(textSource) {
+ async searchTerms(textSource) {
textSource.setEndOffset(this.options.scanning.length);
- return apiTermsFind(textSource.text()).then(({definitions, length}) => {
- if (definitions.length === 0) {
- return false;
- } else {
- textSource.setEndOffset(length);
-
- const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
- const url = window.location.href;
- this.popup.showTermDefs(
- textSource.getRect(),
- definitions,
- this.options,
- {sentence, url}
- );
-
- this.lastTextSource = textSource;
- if (this.options.scanning.selectText) {
- textSource.select();
- }
+ const {definitions, length} = await apiTermsFind(textSource.text());
+ if (definitions.length === 0) {
+ return false;
+ }
- return true;
- }
- });
+ textSource.setEndOffset(length);
+
+ const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
+ const url = window.location.href;
+ this.popup.termsShow(
+ textSource.getRect(),
+ definitions,
+ this.options,
+ {sentence, url}
+ );
+
+ this.lastTextSource = textSource;
+ if (this.options.scanning.selectText) {
+ textSource.select();
+ }
+
+ return true;
}
- searchKanji(textSource) {
+ async searchKanji(textSource) {
textSource.setEndOffset(1);
- return apiKanjiFind(textSource.text()).then(definitions => {
- if (definitions.length === 0) {
- return false;
- } else {
- const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
- const url = window.location.href;
- this.popup.showKanjiDefs(
- textSource.getRect(),
- definitions,
- this.options,
- {sentence, url}
- );
-
- this.lastTextSource = textSource;
- if (this.options.scanning.selectText) {
- textSource.select();
- }
+ const definitions = await apiKanjiFind(textSource.text());
+ if (definitions.length === 0) {
+ return false;
+ }
- return true;
- }
- });
+ const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
+ const url = window.location.href;
+ this.popup.showKanji(
+ textSource.getRect(),
+ definitions,
+ this.options,
+ {sentence, url}
+ );
+
+ this.lastTextSource = textSource;
+ if (this.options.scanning.selectText) {
+ textSource.select();
+ }
+
+ return true;
}
searchClear() {
@@ -238,7 +247,7 @@ window.yomichanFrontend = new class {
}
handleError(error, textSource) {
- if (window.yomichanOrphaned) {
+ if (window.yomichan_orphaned) {
if (textSource && this.options.scanning.modifier !== 'none') {
this.popup.showOrphaned(textSource.getRect(), this.options);
}
diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js
index 311fc065..afa895ba 100644
--- a/ext/fg/js/util.js
+++ b/ext/fg/js/util.js
@@ -28,7 +28,7 @@ function utilInvoke(action, params={}) {
}
});
} catch (e) {
- window.yomichanOrphaned = true;
+ window.yomichan_orphaned = true;
reject(e.message);
}
});