diff options
| author | Alex Yatskov <alex@foosoft.net> | 2019-09-23 17:35:36 -0700 | 
|---|---|---|
| committer | Alex Yatskov <alex@foosoft.net> | 2019-09-23 17:35:36 -0700 | 
| commit | f4b6527ed6ed1f0f4f5a63b94766b20f3b90e6ec (patch) | |
| tree | 0d2f733c13597dd4067d3dc01e6da27f96bfe81b /ext/fg/js/frontend.js | |
| parent | cfc6363a01ee00e89866c54709006d6f55d093de (diff) | |
| parent | f5afe590ad0730a695614b32032b7ea70b46c7b0 (diff) | |
Merge branch 'master' into testing
Diffstat (limited to 'ext/fg/js/frontend.js')
| -rw-r--r-- | ext/fg/js/frontend.js | 126 | 
1 files changed, 65 insertions, 61 deletions
| diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 079a7ef2..167e82c0 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -21,13 +21,16 @@ class Frontend {      constructor(popup, ignoreNodes) {          this.popup = popup;          this.popupTimer = null; -        this.mouseDownLeft = false; -        this.mouseDownMiddle = false;          this.textSourceLast = null;          this.pendingLookup = false;          this.options = null;          this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null); +        this.optionsContext = { +            depth: popup.depth, +            url: popup.url +        }; +          this.primaryTouchIdentifier = null;          this.contextMenuChecking = false;          this.contextMenuPrevent = false; @@ -40,9 +43,9 @@ class Frontend {      static create() {          const initializationData = window.frontendInitializationData;          const isNested = (initializationData !== null && typeof initializationData === 'object'); -        const {id, parentFrameId, ignoreNodes} = isNested ? initializationData : {}; +        const {id, depth, parentFrameId, ignoreNodes, url} = isNested ? initializationData : {}; -        const popup = isNested ? new PopupProxy(id, parentFrameId) : PopupProxyHost.instance.createPopup(null); +        const popup = isNested ? new PopupProxy(depth + 1, id, parentFrameId, url) : PopupProxyHost.instance.createPopup(null);          const frontend = new Frontend(popup, ignoreNodes);          frontend.prepare();          return frontend; @@ -50,14 +53,13 @@ class Frontend {      async prepare() {          try { -            this.options = await apiOptionsGet(); +            this.options = await apiOptionsGet(this.getOptionsContext());              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('mouseout', this.onMouseOut.bind(this)); -            window.addEventListener('mouseup', this.onMouseUp.bind(this));              window.addEventListener('resize', this.onResize.bind(this));              if (this.options.scanning.touchInputEnabled) { @@ -76,7 +78,7 @@ class Frontend {      }      onMouseOver(e) { -        if (e.target === this.popup.container && this.popupTimer) { +        if (e.target === this.popup.container && this.popupTimer !== null) {              this.popupTimerClear();          }      } @@ -84,38 +86,32 @@ class Frontend {      onMouseMove(e) {          this.popupTimerClear(); -        if (!this.options.general.enable) { -            return; -        } - -        if (this.mouseDownLeft) { -            return; -        } - -        if (this.pendingLookup) { +        if ( +            this.pendingLookup || +            !this.options.general.enable || +            (e.buttons & 0x1) !== 0x0 // Left mouse button +        ) {              return;          } -        const mouseScan = this.mouseDownMiddle && this.options.scanning.middleMouse; -        const keyScan = -            this.options.scanning.modifier === 'alt' && e.altKey || -            this.options.scanning.modifier === 'ctrl' && e.ctrlKey || -            this.options.scanning.modifier === 'shift' && e.shiftKey || -            this.options.scanning.modifier === 'none'; - -        if (!keyScan && !mouseScan) { +        const scanningOptions = this.options.scanning; +        const scanningModifier = scanningOptions.modifier; +        if (!( +            Frontend.isScanningModifierPressed(scanningModifier, e) || +            (scanningOptions.middleMouse && (e.buttons & 0x4) !== 0x0) // Middle mouse button +        )) {              return;          }          const search = async () => {              try { -                await this.searchAt({x: e.clientX, y: e.clientY}, 'mouse'); +                await this.searchAt(e.clientX, e.clientY, 'mouse');              } catch (e) {                  this.onError(e);              }          }; -        if (this.options.scanning.modifier === 'none') { +        if (scanningModifier === 'none') {              this.popupTimerSet(search);          } else {              search(); @@ -131,23 +127,8 @@ class Frontend {              return false;          } -        this.mousePosLast = {x: e.clientX, y: e.clientY};          this.popupTimerClear();          this.searchClear(); - -        if (e.which === 1) { -            this.mouseDownLeft = true; -        } else if (e.which === 2) { -            this.mouseDownMiddle = true; -        } -    } - -    onMouseUp(e) { -        if (e.which === 1) { -            this.mouseDownLeft = false; -        } else if (e.which === 2) { -            this.mouseDownMiddle = false; -        }      }      onMouseOut(e) { @@ -239,8 +220,8 @@ class Frontend {          }      } -    onAfterSearch(newRange, type, searched, success) { -        if (type === 'mouse') { +    onAfterSearch(newRange, cause, searched, success) { +        if (cause === 'mouse') {              return;          } @@ -250,7 +231,7 @@ class Frontend {              return;          } -        if (type === 'touchStart' && newRange !== null) { +        if (cause === 'touchStart' && newRange !== null) {              this.scrollPrevent = true;          } @@ -261,11 +242,8 @@ class Frontend {      onBgMessage({action, params}, sender, callback) {          const handlers = { -            optionsSet: options => { -                this.options = options; -                if (!this.options.enable) { -                    this.searchClear(); -                } +            optionsUpdate: () => { +                this.updateOptions();              },              popupSetVisible: ({visible}) => { @@ -284,24 +262,35 @@ class Frontend {          console.log(error);      } +    async updateOptions() { +        this.options = await apiOptionsGet(this.getOptionsContext()); +        if (!this.options.enable) { +            this.searchClear(); +        } +    } +      popupTimerSet(callback) { -        this.popupTimerClear(); -        this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); +        const delay = this.options.scanning.delay; +        if (delay > 0) { +            this.popupTimer = window.setTimeout(callback, delay); +        } else { +            Promise.resolve().then(callback); +        }      }      popupTimerClear() { -        if (this.popupTimer) { +        if (this.popupTimer !== null) {              window.clearTimeout(this.popupTimer);              this.popupTimer = null;          }      } -    async searchAt(point, type) { -        if (this.pendingLookup || await this.popup.containsPoint(point)) { +    async searchAt(x, y, cause) { +        if (this.pendingLookup || await this.popup.containsPoint(x, y)) {              return;          } -        const textSource = docRangeFromPoint(point, this.options); +        const textSource = docRangeFromPoint(x, y, this.options);          let hideResults = textSource === null;          let searched = false;          let success = false; @@ -310,7 +299,7 @@ class Frontend {              if (!hideResults && (!this.textSourceLast || !this.textSourceLast.equals(textSource))) {                  searched = true;                  this.pendingLookup = true; -                const focus = (type === 'mouse'); +                const focus = (cause === 'mouse');                  hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus);                  success = true;              } @@ -335,7 +324,7 @@ class Frontend {              }              this.pendingLookup = false; -            this.onAfterSearch(this.textSourceLast, type, searched, success); +            this.onAfterSearch(this.textSourceLast, cause, searched, success);          }      } @@ -347,7 +336,7 @@ class Frontend {              return;          } -        const {definitions, length} = await apiTermsFind(searchText); +        const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext());          if (definitions.length === 0) {              return false;          } @@ -380,7 +369,7 @@ class Frontend {              return;          } -        const definitions = await apiKanjiFind(searchText); +        const definitions = await apiKanjiFind(searchText, this.getOptionsContext());          if (definitions.length === 0) {              return false;          } @@ -477,7 +466,7 @@ class Frontend {          this.clickPrevent = value;      } -    searchFromTouch(x, y, type) { +    searchFromTouch(x, y, cause) {          this.popupTimerClear();          if (!this.options.general.enable || this.pendingLookup) { @@ -486,7 +475,7 @@ class Frontend {          const search = async () => {              try { -                await this.searchAt({x, y}, type); +                await this.searchAt(x, y, cause);              } catch (e) {                  this.onError(e);              } @@ -523,6 +512,21 @@ class Frontend {              textSource.setEndOffset(length);          }      } + +    getOptionsContext() { +        this.optionsContext.url = this.popup.url; +        return this.optionsContext; +    } + +    static isScanningModifierPressed(scanningModifier, mouseEvent) { +        switch (scanningModifier) { +            case 'alt': return mouseEvent.altKey; +            case 'ctrl': return mouseEvent.ctrlKey; +            case 'shift': return mouseEvent.shiftKey; +            case 'none': return true; +            default: return false; +        } +    }  }  window.yomichan_frontend = Frontend.create(); |