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
|
|
|
|
* docRangeFromPoint
|
|
|
|
*/
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-04-27 22:10:37 +00:00
|
|
|
class TextScanner extends EventDispatcher {
|
2020-05-08 23:05:50 +00:00
|
|
|
constructor({node, ignoreElements, ignorePoint, search}) {
|
2020-04-27 22:10:37 +00:00
|
|
|
super();
|
2020-05-07 23:45:08 +00:00
|
|
|
this._node = node;
|
|
|
|
this._ignoreElements = ignoreElements;
|
2020-05-08 23:05:50 +00:00
|
|
|
this._ignorePoint = ignorePoint;
|
|
|
|
this._search = search;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
this._isPrepared = false;
|
2020-05-07 23:45:08 +00:00
|
|
|
this._ignoreNodes = null;
|
2020-03-13 21:23:08 +00:00
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._causeCurrent = null;
|
|
|
|
this._scanTimerPromise = null;
|
|
|
|
this._textSourceCurrent = null;
|
|
|
|
this._textSourceCurrentSelected = false;
|
|
|
|
this._pendingLookup = false;
|
|
|
|
this._options = null;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._enabled = false;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._primaryTouchIdentifier = null;
|
|
|
|
this._preventNextContextMenu = false;
|
|
|
|
this._preventNextMouseDown = false;
|
|
|
|
this._preventNextClick = false;
|
|
|
|
this._preventScroll = false;
|
2020-05-02 16:47:15 +00:00
|
|
|
|
|
|
|
this._canClearSelection = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
get canClearSelection() {
|
|
|
|
return this._canClearSelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
set canClearSelection(value) {
|
|
|
|
this._canClearSelection = value;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
get ignoreNodes() {
|
|
|
|
return this._ignoreNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
set ignoreNodes(value) {
|
|
|
|
this._ignoreNodes = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get causeCurrent() {
|
|
|
|
return this._causeCurrent;
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
prepare() {
|
|
|
|
this._isPrepared = true;
|
|
|
|
this.setEnabled(this._enabled);
|
|
|
|
}
|
|
|
|
|
2020-05-08 23:05:50 +00:00
|
|
|
setEnabled(enabled) {
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
this._enabled = enabled;
|
2020-06-21 20:14:05 +00:00
|
|
|
if (this._enabled && this._isPrepared) {
|
2020-05-08 23:05:50 +00:00
|
|
|
this._hookEvents();
|
|
|
|
} else {
|
|
|
|
this.clearSelection(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setOptions(options) {
|
|
|
|
this._options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
async searchAt(x, y, cause) {
|
|
|
|
try {
|
|
|
|
this._scanTimerClear();
|
|
|
|
|
|
|
|
if (this._pendingLookup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this._ignorePoint === 'function' && await this._ignorePoint(x, y)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const textSource = docRangeFromPoint(x, y, this._options.scanning.deepDomScan);
|
|
|
|
try {
|
|
|
|
if (this._textSourceCurrent !== null && this._textSourceCurrent.equals(textSource)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._pendingLookup = true;
|
|
|
|
const result = await this._search(textSource, cause);
|
|
|
|
if (result !== null) {
|
|
|
|
this._causeCurrent = cause;
|
|
|
|
this.setCurrentTextSource(textSource);
|
|
|
|
}
|
|
|
|
this._pendingLookup = false;
|
|
|
|
} finally {
|
|
|
|
if (textSource !== null) {
|
|
|
|
textSource.cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
yomichan.logError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:07:51 +00:00
|
|
|
getTextSourceContent(textSource, length, layoutAwareScan) {
|
2020-05-08 23:05:50 +00:00
|
|
|
const clonedTextSource = textSource.clone();
|
|
|
|
|
2020-06-21 20:07:51 +00:00
|
|
|
clonedTextSource.setEndOffset(length, layoutAwareScan);
|
2020-05-08 23:05:50 +00:00
|
|
|
|
|
|
|
if (this._ignoreNodes !== null && clonedTextSource.range) {
|
|
|
|
length = clonedTextSource.text().length;
|
|
|
|
while (clonedTextSource.range && length > 0) {
|
2020-06-21 20:07:51 +00:00
|
|
|
const nodes = DOM.getNodesInRange(clonedTextSource.range);
|
|
|
|
if (!DOM.anyNodeMatchesSelector(nodes, this._ignoreNodes)) {
|
2020-05-08 23:05:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
--length;
|
2020-06-21 20:07:51 +00:00
|
|
|
clonedTextSource.setEndOffset(length, layoutAwareScan);
|
2020-05-08 23:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clonedTextSource.text();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSelection(passive) {
|
|
|
|
if (!this._canClearSelection) { return; }
|
|
|
|
if (this._textSourceCurrent !== null) {
|
|
|
|
if (this._textSourceCurrentSelected) {
|
|
|
|
this._textSourceCurrent.deselect();
|
|
|
|
}
|
|
|
|
this._textSourceCurrent = null;
|
|
|
|
this._textSourceCurrentSelected = false;
|
|
|
|
}
|
|
|
|
this.trigger('clearSelection', {passive});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentTextSource() {
|
|
|
|
return this._textSourceCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentTextSource(textSource) {
|
|
|
|
this._textSourceCurrent = textSource;
|
|
|
|
if (this._options.scanning.selectText) {
|
|
|
|
this._textSourceCurrent.select();
|
|
|
|
this._textSourceCurrentSelected = true;
|
|
|
|
} else {
|
|
|
|
this._textSourceCurrentSelected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onMouseOver(e) {
|
|
|
|
if (this._ignoreElements().includes(e.target)) {
|
|
|
|
this._scanTimerClear();
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onMouseMove(e) {
|
|
|
|
this._scanTimerClear();
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
if (this._pendingLookup || DOM.isMouseButtonDown(e, 'primary')) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:39:24 +00:00
|
|
|
const modifiers = DOM.getActiveModifiers(e);
|
|
|
|
this.trigger('activeModifiersChanged', {modifiers});
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
const scanningOptions = this._options.scanning;
|
2019-12-05 01:58:35 +00:00
|
|
|
const scanningModifier = scanningOptions.modifier;
|
|
|
|
if (!(
|
2020-05-07 23:45:08 +00:00
|
|
|
this._isScanningModifierPressed(scanningModifier, e) ||
|
2019-12-05 01:58:35 +00:00
|
|
|
(scanningOptions.middleMouse && DOM.isMouseButtonDown(e, 'auxiliary'))
|
|
|
|
)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const search = async () => {
|
|
|
|
if (scanningModifier === 'none') {
|
2020-05-07 23:45:08 +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();
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onMouseDown(e) {
|
|
|
|
if (this._preventNextMouseDown) {
|
|
|
|
this._preventNextMouseDown = false;
|
|
|
|
this._preventNextClick = true;
|
2019-12-05 01:58:35 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:12:43 +00:00
|
|
|
if (DOM.isMouseButtonDown(e, 'primary')) {
|
2020-05-07 23:45:08 +00:00
|
|
|
this._scanTimerClear();
|
2020-04-27 22:10:37 +00:00
|
|
|
this.clearSelection(false);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onMouseOut() {
|
|
|
|
this._scanTimerClear();
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onClick(e) {
|
|
|
|
if (this._preventNextClick) {
|
|
|
|
this._preventNextClick = false;
|
2019-12-05 01:58:35 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onAuxClick() {
|
|
|
|
this._preventNextContextMenu = false;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onContextMenu(e) {
|
|
|
|
if (this._preventNextContextMenu) {
|
|
|
|
this._preventNextContextMenu = false;
|
2019-12-05 01:58:35 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onTouchStart(e) {
|
|
|
|
if (this._primaryTouchIdentifier !== null || e.changedTouches.length === 0) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._preventScroll = false;
|
|
|
|
this._preventNextContextMenu = false;
|
|
|
|
this._preventNextMouseDown = false;
|
|
|
|
this._preventNextClick = false;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
|
|
|
const primaryTouch = e.changedTouches[0];
|
2020-04-10 21:11:41 +00:00
|
|
|
if (DOM.isPointInSelection(primaryTouch.clientX, primaryTouch.clientY, window.getSelection())) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._primaryTouchIdentifier = primaryTouch.identifier;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
if (this._pendingLookup) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
const textSourceCurrentPrevious = this._textSourceCurrent !== null ? this._textSourceCurrent.clone() : null;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
|
|
|
this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchStart')
|
2020-02-16 00:51:53 +00:00
|
|
|
.then(() => {
|
|
|
|
if (
|
2020-05-07 23:45:08 +00:00
|
|
|
this._textSourceCurrent === null ||
|
|
|
|
this._textSourceCurrent.equals(textSourceCurrentPrevious)
|
2020-02-16 00:51:53 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._preventScroll = true;
|
|
|
|
this._preventNextContextMenu = true;
|
|
|
|
this._preventNextMouseDown = true;
|
2020-02-16 00:51:53 +00:00
|
|
|
});
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onTouchEnd(e) {
|
2019-12-05 01:58:35 +00:00
|
|
|
if (
|
2020-05-07 23:45:08 +00:00
|
|
|
this._primaryTouchIdentifier === null ||
|
|
|
|
this._getTouch(e.changedTouches, this._primaryTouchIdentifier) === null
|
2019-12-05 01:58:35 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
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.
|
|
|
|
// I.e. this._preventNextContextMenu and this._preventNextMouseDown should not be assigned to false.
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onTouchCancel(e) {
|
|
|
|
this._onTouchEnd(e);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onTouchMove(e) {
|
|
|
|
if (!this._preventScroll || !e.cancelable || this._primaryTouchIdentifier === null) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
const primaryTouch = this._getTouch(e.changedTouches, this._primaryTouchIdentifier);
|
2020-02-15 18:46:21 +00:00
|
|
|
if (primaryTouch === null) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchMove');
|
|
|
|
|
|
|
|
e.preventDefault(); // Disable scroll
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
async _scanTimerWait() {
|
|
|
|
const delay = this._options.scanning.delay;
|
2019-12-05 01:58:35 +00:00
|
|
|
const promise = promiseTimeout(delay, true);
|
2020-05-07 23:45:08 +00:00
|
|
|
this._scanTimerPromise = promise;
|
2019-12-05 01:58:35 +00:00
|
|
|
try {
|
|
|
|
return await promise;
|
|
|
|
} finally {
|
2020-05-07 23:45:08 +00:00
|
|
|
if (this._scanTimerPromise === promise) {
|
|
|
|
this._scanTimerPromise = null;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_scanTimerClear() {
|
|
|
|
if (this._scanTimerPromise !== null) {
|
|
|
|
this._scanTimerPromise.resolve(false);
|
|
|
|
this._scanTimerPromise = null;
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_hookEvents() {
|
|
|
|
const eventListenerInfos = this._getMouseEventListeners();
|
|
|
|
if (this._options.scanning.touchInputEnabled) {
|
|
|
|
eventListenerInfos.push(...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) {
|
2020-05-07 23:45:08 +00:00
|
|
|
this._eventListeners.addEventListener(node, type, listener, options);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_getMouseEventListeners() {
|
2019-12-06 19:39:29 +00:00
|
|
|
return [
|
2020-05-07 23:45:08 +00:00
|
|
|
[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)]
|
2019-12-06 19:39:29 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_getTouchEventListeners() {
|
2019-12-06 19:39:29 +00:00
|
|
|
return [
|
2020-05-07 23:45:08 +00:00
|
|
|
[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)]
|
2019-12-06 19:39:29 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_isScanningModifierPressed(scanningModifier, mouseEvent) {
|
2019-12-05 01:58:35 +00:00
|
|
|
switch (scanningModifier) {
|
|
|
|
case 'alt': return mouseEvent.altKey;
|
|
|
|
case 'ctrl': return mouseEvent.ctrlKey;
|
|
|
|
case 'shift': return mouseEvent.shiftKey;
|
2020-05-04 21:14:21 +00:00
|
|
|
case 'meta': return mouseEvent.metaKey;
|
2019-12-05 01:58:35 +00:00
|
|
|
case 'none': return true;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_getTouch(touchList, identifier) {
|
2020-02-15 18:46:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|