summaryrefslogtreecommitdiff
path: root/ext/fg/js/driver.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-09-18 17:32:57 -0700
committerAlex Yatskov <alex@foosoft.net>2016-09-18 17:32:57 -0700
commit2a65060b73ec07a99df9e5922745427f81ea7e51 (patch)
tree9404900e91424a8838c04eb4112750cb14f9d07e /ext/fg/js/driver.js
parent984b4ed0d61ff6b4f15bc99244b18e77e3a381ba (diff)
Hold down ctrl to search for kanji, fixes #13
Diffstat (limited to 'ext/fg/js/driver.js')
-rw-r--r--ext/fg/js/driver.js77
1 files changed, 58 insertions, 19 deletions
diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js
index de097980..68cbcdee 100644
--- a/ext/fg/js/driver.js
+++ b/ext/fg/js/driver.js
@@ -41,21 +41,23 @@ class Driver {
onKeyDown(e) {
if (this.enabled && this.lastMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) {
- this.searchAt(this.lastMousePos);
+ this.searchAt(this.lastMousePos, e.ctrlKey ? 'kanji' : 'terms');
+ } else {
+ this.hidePopup();
}
}
onMouseMove(e) {
this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === 2)) {
- this.searchAt(this.lastMousePos);
+ this.searchAt(this.lastMousePos, e.ctrlKey ? 'kanji' : 'terms');
}
}
onMouseDown(e) {
this.lastMousePos = {x: e.clientX, y: e.clientY};
if (this.enabled && (e.shiftKey || e.which === 2)) {
- this.searchAt(this.lastMousePos);
+ this.searchAt(this.lastMousePos, e.ctrlKey ? 'kanji' : 'terms');
} else {
this.hidePopup();
}
@@ -77,26 +79,12 @@ class Driver {
}
}
- searchAt(point) {
- if (this.pendingLookup) {
- return;
- }
-
- const textSource = textSourceFromPoint(point);
- if (textSource === null || !textSource.containsPoint(point)) {
- this.hidePopup();
- return;
- }
-
- if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) {
- return;
- }
-
+ searchTerms(textSource) {
textSource.setEndOffset(this.options.scanLength);
this.pendingLookup = true;
findTerm(textSource.text()).then(({definitions, length}) => {
- if (length === 0) {
+ if (definitions.length === 0) {
this.pendingLookup = false;
this.hidePopup();
} else {
@@ -123,6 +111,57 @@ class Driver {
});
}
+ searchKanji(textSource) {
+ textSource.setEndOffset(1);
+
+ this.pendingLookup = true;
+ findKanji(textSource.text()).then(definitions => {
+ if (definitions.length === 0) {
+ this.pendingLookup = false;
+ this.hidePopup();
+ } else {
+ definitions.forEach(definition => definition.url = window.location.href);
+
+ const sequence = ++this.sequence;
+ return renderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'kanji-list.html').then(content => {
+ this.definitions = definitions;
+ this.pendingLookup = false;
+ this.showPopup(textSource, content);
+ return canAddDefinitions(definitions, ['kanji']);
+ }).then(states => {
+ if (states !== null) {
+ states.forEach((state, index) => this.popup.invokeApi('setActionState', {index, state, sequence}));
+ }
+ });
+ }
+ });
+ }
+
+ searchAt(point, mode) {
+ if (this.pendingLookup) {
+ return;
+ }
+
+ const textSource = textSourceFromPoint(point);
+ if (textSource === null || !textSource.containsPoint(point)) {
+ this.hidePopup();
+ return;
+ }
+
+ if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) {
+ return;
+ }
+
+ switch (mode) {
+ case 'terms':
+ this.searchTerms(textSource);
+ break;
+ case 'kanji':
+ this.searchKanji(textSource);
+ break;
+ }
+ }
+
showPopup(textSource, content) {
this.popup.showNextTo(textSource.getRect(), content);