2019-12-05 01:58:35 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2019-2020 Yomichan Authors
|
2019-12-05 01:58:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-05 01:58:35 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* DOM
|
|
|
|
* TextSourceRange
|
|
|
|
* docRangeFromPoint
|
|
|
|
*/
|
2019-12-05 01:58:35 +00:00
|
|
|
|
|
|
|
class TextScanner {
|
2020-04-11 13:20:12 +00:00
|
|
|
constructor(node, ignoreElements, ignorePoints) {
|
2019-12-05 01:58:35 +00:00
|
|
|
this.node = node;
|
2019-12-05 20:12:43 +00:00
|
|
|
this.ignoreElements = ignoreElements;
|
|
|
|
this.ignorePoints = ignorePoints;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-03-13 21:23:08 +00:00
|
|
|
this.ignoreNodes = null;
|
|
|
|
|
2019-12-05 20:12:43 +00:00
|
|
|
this.scanTimerPromise = null;
|
2020-03-13 21:23:08 +00:00
|
|
|
this.causeCurrent = null;
|
2019-12-05 01:58:35 +00:00
|
|
|
this.textSourceCurrent = null;
|
|
|
|
this.pendingLookup = false;
|
|
|
|
this.options = null;
|
|
|
|
|
|
|
|
this.enabled = false;
|
2020-02-16 21:33:48 +00:00
|
|
|
this.eventListeners = new EventListenerCollection();
|
2019-12-05 01:58:35 +00:00
|
|
|
|
|
|
|
this.primaryTouchIdentifier = null;
|
|
|
|
this.preventNextContextMenu = false;
|
|
|
|
this.preventNextMouseDown = false;
|
|
|
|
this.preventNextClick = false;
|
|
|
|
this.preventScroll = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseOver(e) {
|
2020-04-10 12:49:56 +00:00
|
|
|
if (this.ignoreElements().includes(e.target)) {
|
2019-12-05 20:12:43 +00:00
|
|
|
this.scanTimerClear();
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseMove(e) {
|
2019-12-05 20:12:43 +00:00
|
|
|
this.scanTimerClear();
|
2019-12-05 01:58:35 +00:00
|
|
|
|
|
|
|
if (this.pendingLookup || DOM.isMouseButtonDown(e, 'primary')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scanningOptions = this.options.scanning;
|
|
|
|
const scanningModifier = scanningOptions.modifier;
|
|
|
|
if (!(
|
|
|
|
TextScanner.isScanningModifierPressed(scanningModifier, e) ||
|
|
|
|
(scanningOptions.middleMouse && DOM.isMouseButtonDown(e, 'auxiliary'))
|
|
|
|
)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const search = async () => {
|
|
|
|
if (scanningModifier === 'none') {
|
2019-12-05 20:12:43 +00:00
|
|
|
if (!await this.scanTimerWait()) {
|
2019-12-05 01:58:35 +00:00
|
|
|
// Aborted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.searchAt(e.clientX, e.clientY, 'mouse');
|
|
|
|
};
|
|
|
|
|
|
|
|
search();
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseDown(e) {
|
|
|
|
if (this.preventNextMouseDown) {
|
|
|
|
this.preventNextMouseDown = false;
|
|
|
|
this.preventNextClick = true;
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:12:43 +00:00
|
|
|
if (DOM.isMouseButtonDown(e, 'primary')) {
|
|
|
|
this.scanTimerClear();
|
2019-12-06 19:39:29 +00:00
|
|
|
this.onSearchClear(true);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseOut() {
|
2019-12-05 20:12:43 +00:00
|
|
|
this.scanTimerClear();
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onClick(e) {
|
|
|
|
if (this.preventNextClick) {
|
|
|
|
this.preventNextClick = false;
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onAuxClick() {
|
|
|
|
this.preventNextContextMenu = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onContextMenu(e) {
|
|
|
|
if (this.preventNextContextMenu) {
|
|
|
|
this.preventNextContextMenu = false;
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onTouchStart(e) {
|
|
|
|
if (this.primaryTouchIdentifier !== null || e.changedTouches.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.preventScroll = false;
|
|
|
|
this.preventNextContextMenu = false;
|
|
|
|
this.preventNextMouseDown = false;
|
|
|
|
this.preventNextClick = false;
|
|
|
|
|
|
|
|
const primaryTouch = e.changedTouches[0];
|
|
|
|
if (DOM.isPointInSelection(primaryTouch.clientX, primaryTouch.clientY, this.node.getSelection())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.primaryTouchIdentifier = primaryTouch.identifier;
|
|
|
|
|
|
|
|
if (this.pendingLookup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null;
|
|
|
|
|
|
|
|
this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchStart')
|
2020-02-16 00:51:53 +00:00
|
|
|
.then(() => {
|
|
|
|
if (
|
|
|
|
this.textSourceCurrent === null ||
|
|
|
|
this.textSourceCurrent.equals(textSourceCurrentPrevious)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-02-16 00:51:53 +00:00
|
|
|
this.preventScroll = true;
|
|
|
|
this.preventNextContextMenu = true;
|
|
|
|
this.preventNextMouseDown = true;
|
|
|
|
});
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onTouchEnd(e) {
|
|
|
|
if (
|
|
|
|
this.primaryTouchIdentifier === null ||
|
2020-02-15 18:46:21 +00:00
|
|
|
TextScanner.getTouch(e.changedTouches, this.primaryTouchIdentifier) === null
|
2019-12-05 01:58:35 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.primaryTouchIdentifier = null;
|
|
|
|
this.preventScroll = false;
|
|
|
|
this.preventNextClick = false;
|
|
|
|
// Don't revert context menu and mouse down prevention,
|
|
|
|
// since these events can occur after the touch has ended.
|
|
|
|
// this.preventNextContextMenu = false;
|
|
|
|
// this.preventNextMouseDown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onTouchCancel(e) {
|
|
|
|
this.onTouchEnd(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
onTouchMove(e) {
|
|
|
|
if (!this.preventScroll || !e.cancelable || this.primaryTouchIdentifier === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:46:21 +00:00
|
|
|
const primaryTouch = TextScanner.getTouch(e.changedTouches, this.primaryTouchIdentifier);
|
|
|
|
if (primaryTouch === null) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchMove');
|
|
|
|
|
|
|
|
e.preventDefault(); // Disable scroll
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:39:29 +00:00
|
|
|
async onSearchSource(_textSource, _cause) {
|
|
|
|
throw new Error('Override me');
|
2019-12-05 20:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onError(error) {
|
|
|
|
logError(error, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async scanTimerWait() {
|
2019-12-05 01:58:35 +00:00
|
|
|
const delay = this.options.scanning.delay;
|
|
|
|
const promise = promiseTimeout(delay, true);
|
2019-12-05 20:12:43 +00:00
|
|
|
this.scanTimerPromise = promise;
|
2019-12-05 01:58:35 +00:00
|
|
|
try {
|
|
|
|
return await promise;
|
|
|
|
} finally {
|
2019-12-05 20:12:43 +00:00
|
|
|
if (this.scanTimerPromise === promise) {
|
|
|
|
this.scanTimerPromise = null;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:12:43 +00:00
|
|
|
scanTimerClear() {
|
|
|
|
if (this.scanTimerPromise !== null) {
|
|
|
|
this.scanTimerPromise.resolve(false);
|
|
|
|
this.scanTimerPromise = null;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 13:20:12 +00:00
|
|
|
setEnabled(enabled, canEnable) {
|
|
|
|
if (enabled && canEnable) {
|
2019-12-05 01:58:35 +00:00
|
|
|
if (!this.enabled) {
|
|
|
|
this.hookEvents();
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.enabled) {
|
2020-02-16 21:33:48 +00:00
|
|
|
this.eventListeners.removeAllEventListeners();
|
2019-12-05 01:58:35 +00:00
|
|
|
this.enabled = false;
|
|
|
|
}
|
2019-12-06 19:39:29 +00:00
|
|
|
this.onSearchClear(false);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hookEvents() {
|
2020-02-16 21:33:48 +00:00
|
|
|
let eventListenerInfos = this.getMouseEventListeners();
|
2019-12-05 01:58:35 +00:00
|
|
|
if (this.options.scanning.touchInputEnabled) {
|
2020-02-16 21:33:48 +00:00
|
|
|
eventListenerInfos = eventListenerInfos.concat(this.getTouchEventListeners());
|
2019-12-06 19:39:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 21:33:48 +00:00
|
|
|
for (const [node, type, listener, options] of eventListenerInfos) {
|
|
|
|
this.eventListeners.addEventListener(node, type, listener, options);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:39:29 +00:00
|
|
|
getMouseEventListeners() {
|
|
|
|
return [
|
|
|
|
[this.node, 'mousedown', this.onMouseDown.bind(this)],
|
|
|
|
[this.node, 'mousemove', this.onMouseMove.bind(this)],
|
|
|
|
[this.node, 'mouseover', this.onMouseOver.bind(this)],
|
|
|
|
[this.node, 'mouseout', this.onMouseOut.bind(this)]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getTouchEventListeners() {
|
|
|
|
return [
|
|
|
|
[this.node, 'click', this.onClick.bind(this)],
|
|
|
|
[this.node, 'auxclick', this.onAuxClick.bind(this)],
|
|
|
|
[this.node, 'touchstart', this.onTouchStart.bind(this)],
|
|
|
|
[this.node, 'touchend', this.onTouchEnd.bind(this)],
|
|
|
|
[this.node, 'touchcancel', this.onTouchCancel.bind(this)],
|
|
|
|
[this.node, 'touchmove', this.onTouchMove.bind(this), {passive: false}],
|
|
|
|
[this.node, 'contextmenu', this.onContextMenu.bind(this)]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-04-11 13:20:12 +00:00
|
|
|
setOptions(options, canEnable=true) {
|
2019-12-05 01:58:35 +00:00
|
|
|
this.options = options;
|
2020-04-11 13:20:12 +00:00
|
|
|
this.setEnabled(this.options.general.enable, canEnable);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async searchAt(x, y, cause) {
|
|
|
|
try {
|
2019-12-05 20:12:43 +00:00
|
|
|
this.scanTimerClear();
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2019-12-05 20:12:43 +00:00
|
|
|
if (this.pendingLookup) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:12:43 +00:00
|
|
|
for (const ignorePointFn of this.ignorePoints) {
|
|
|
|
if (await ignorePointFn(x, y)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:56:32 +00:00
|
|
|
const textSource = docRangeFromPoint(x, y, this.options.scanning.deepDomScan);
|
2019-12-05 01:58:35 +00:00
|
|
|
try {
|
2020-01-25 16:17:39 +00:00
|
|
|
if (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:39:29 +00:00
|
|
|
this.pendingLookup = true;
|
|
|
|
const result = await this.onSearchSource(textSource, cause);
|
|
|
|
if (result !== null) {
|
2020-03-13 21:23:08 +00:00
|
|
|
this.causeCurrent = cause;
|
2019-12-06 19:39:29 +00:00
|
|
|
this.textSourceCurrent = textSource;
|
|
|
|
if (this.options.scanning.selectText) {
|
|
|
|
textSource.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.pendingLookup = false;
|
2019-12-05 01:58:35 +00:00
|
|
|
} finally {
|
|
|
|
if (textSource !== null) {
|
|
|
|
textSource.cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.onError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTextSourceScanLength(textSource, length) {
|
|
|
|
textSource.setEndOffset(length);
|
|
|
|
if (this.ignoreNodes === null || !textSource.range) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = textSource.text().length;
|
|
|
|
while (textSource.range && length > 0) {
|
|
|
|
const nodes = TextSourceRange.getNodesInRange(textSource.range);
|
|
|
|
if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--length;
|
|
|
|
textSource.setEndOffset(length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:39:29 +00:00
|
|
|
onSearchClear(_) {
|
2019-12-05 01:58:35 +00:00
|
|
|
if (this.textSourceCurrent !== null) {
|
|
|
|
if (this.options.scanning.selectText) {
|
|
|
|
this.textSourceCurrent.deselect();
|
|
|
|
}
|
|
|
|
this.textSourceCurrent = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentTextSource() {
|
|
|
|
return this.textSourceCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentTextSource(textSource) {
|
|
|
|
return this.textSourceCurrent = textSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:46:21 +00:00
|
|
|
static getTouch(touchList, identifier) {
|
|
|
|
for (const touch of touchList) {
|
|
|
|
if (touch.identifier === identifier) {
|
|
|
|
return touch;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-15 18:46:21 +00:00
|
|
|
return null;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|