2019-12-05 01:58:35 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2019-2022 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
|
2020-08-10 01:07:11 +00:00
|
|
|
* DocumentUtil
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-04-27 22:10:37 +00:00
|
|
|
class TextScanner extends EventDispatcher {
|
2020-11-24 01:31:48 +00:00
|
|
|
constructor({
|
|
|
|
node,
|
|
|
|
documentUtil,
|
2021-01-12 04:13:35 +00:00
|
|
|
getSearchContext,
|
2020-11-24 01:31:48 +00:00
|
|
|
ignoreElements=null,
|
|
|
|
ignorePoint=null,
|
|
|
|
searchTerms=false,
|
|
|
|
searchKanji=false,
|
|
|
|
searchOnClick=false,
|
|
|
|
searchOnClickOnly=false
|
|
|
|
}) {
|
2020-04-27 22:10:37 +00:00
|
|
|
super();
|
2020-05-07 23:45:08 +00:00
|
|
|
this._node = node;
|
2020-08-09 17:27:21 +00:00
|
|
|
this._documentUtil = documentUtil;
|
2021-01-12 04:13:35 +00:00
|
|
|
this._getSearchContext = getSearchContext;
|
2020-11-24 01:31:48 +00:00
|
|
|
this._ignoreElements = ignoreElements;
|
|
|
|
this._ignorePoint = ignorePoint;
|
2020-09-06 01:43:19 +00:00
|
|
|
this._searchTerms = searchTerms;
|
|
|
|
this._searchKanji = searchKanji;
|
|
|
|
this._searchOnClick = searchOnClick;
|
2020-11-24 01:31:48 +00:00
|
|
|
this._searchOnClickOnly = searchOnClickOnly;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
this._isPrepared = false;
|
2020-11-24 01:31:48 +00:00
|
|
|
this._includeSelector = null;
|
|
|
|
this._excludeSelector = null;
|
2020-03-13 21:23:08 +00:00
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
this._inputInfoCurrent = null;
|
2020-05-07 23:45:08 +00:00
|
|
|
this._scanTimerPromise = null;
|
|
|
|
this._textSourceCurrent = null;
|
|
|
|
this._textSourceCurrentSelected = false;
|
|
|
|
this._pendingLookup = false;
|
2021-04-27 01:05:46 +00:00
|
|
|
this._selectionRestoreInfo = null;
|
2020-08-09 17:19:42 +00:00
|
|
|
|
|
|
|
this._deepContentScan = false;
|
|
|
|
this._selectText = false;
|
|
|
|
this._delay = 0;
|
|
|
|
this._touchInputEnabled = false;
|
2020-09-13 15:33:10 +00:00
|
|
|
this._pointerEventsEnabled = false;
|
2020-08-22 18:40:44 +00:00
|
|
|
this._scanLength = 1;
|
|
|
|
this._layoutAwareScan = false;
|
2020-09-26 23:24:21 +00:00
|
|
|
this._preventMiddleMouse = false;
|
2021-12-18 17:38:39 +00:00
|
|
|
this._matchTypePrefix = false;
|
2021-01-10 19:43:06 +00:00
|
|
|
this._sentenceScanExtent = 0;
|
2021-05-16 19:24:38 +00:00
|
|
|
this._sentenceTerminateAtNewlines = true;
|
2021-01-10 19:43:06 +00:00
|
|
|
this._sentenceTerminatorMap = new Map();
|
|
|
|
this._sentenceForwardQuoteMap = new Map();
|
|
|
|
this._sentenceBackwardQuoteMap = new Map();
|
2020-09-09 20:59:03 +00:00
|
|
|
this._inputs = [];
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._enabled = false;
|
2020-09-12 23:36:51 +00:00
|
|
|
this._enabledValue = false;
|
2020-05-07 23:45:08 +00:00
|
|
|
this._eventListeners = new EventListenerCollection();
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2021-03-10 01:01:37 +00:00
|
|
|
this._preventNextClickScan = false;
|
|
|
|
this._preventNextClickScanTimer = null;
|
|
|
|
this._preventNextClickScanTimerDuration = 50;
|
|
|
|
this._preventNextClickScanTimerCallback = this._onPreventNextClickScanTimeout.bind(this);
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
this._primaryTouchIdentifier = null;
|
|
|
|
this._preventNextContextMenu = false;
|
|
|
|
this._preventNextMouseDown = false;
|
|
|
|
this._preventNextClick = false;
|
|
|
|
this._preventScroll = false;
|
2020-09-13 15:33:10 +00:00
|
|
|
this._penPointerPressed = false;
|
|
|
|
this._penPointerReleased = false;
|
2020-09-13 15:58:07 +00:00
|
|
|
this._pointerIdTypeMap = new Map();
|
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-11-24 01:31:48 +00:00
|
|
|
get includeSelector() {
|
|
|
|
return this._includeSelector;
|
|
|
|
}
|
|
|
|
|
|
|
|
set includeSelector(value) {
|
|
|
|
this._includeSelector = value;
|
2020-05-07 23:45:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 01:31:48 +00:00
|
|
|
get excludeSelector() {
|
|
|
|
return this._excludeSelector;
|
|
|
|
}
|
|
|
|
|
|
|
|
set excludeSelector(value) {
|
|
|
|
this._excludeSelector = value;
|
2020-05-07 23:45:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 20:14:05 +00:00
|
|
|
prepare() {
|
|
|
|
this._isPrepared = true;
|
|
|
|
this.setEnabled(this._enabled);
|
|
|
|
}
|
|
|
|
|
2021-05-23 18:30:31 +00:00
|
|
|
isEnabled() {
|
|
|
|
return this._enabled;
|
|
|
|
}
|
|
|
|
|
2020-05-08 23:05:50 +00:00
|
|
|
setEnabled(enabled) {
|
2020-09-12 23:42:09 +00:00
|
|
|
this._enabled = enabled;
|
|
|
|
|
2020-09-12 23:36:51 +00:00
|
|
|
const value = enabled && this._isPrepared;
|
|
|
|
if (this._enabledValue === value) { return; }
|
|
|
|
|
2020-05-08 23:05:50 +00:00
|
|
|
this._eventListeners.removeAllEventListeners();
|
2020-09-11 22:44:53 +00:00
|
|
|
this._primaryTouchIdentifier = null;
|
|
|
|
this._preventNextContextMenu = false;
|
|
|
|
this._preventNextMouseDown = false;
|
|
|
|
this._preventNextClick = false;
|
|
|
|
this._preventScroll = false;
|
2020-09-13 15:33:10 +00:00
|
|
|
this._penPointerPressed = false;
|
|
|
|
this._penPointerReleased = false;
|
2020-09-13 15:58:07 +00:00
|
|
|
this._pointerIdTypeMap.clear();
|
2020-09-11 22:44:53 +00:00
|
|
|
|
2020-09-12 23:36:51 +00:00
|
|
|
this._enabledValue = value;
|
2020-09-11 22:44:53 +00:00
|
|
|
|
2020-09-12 23:36:51 +00:00
|
|
|
if (value) {
|
2020-05-08 23:05:50 +00:00
|
|
|
this._hookEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 04:10:55 +00:00
|
|
|
setOptions({
|
|
|
|
inputs,
|
|
|
|
deepContentScan,
|
|
|
|
selectText,
|
|
|
|
delay,
|
|
|
|
touchInputEnabled,
|
|
|
|
pointerEventsEnabled,
|
|
|
|
scanLength,
|
|
|
|
layoutAwareScan,
|
2021-01-10 19:43:06 +00:00
|
|
|
preventMiddleMouse,
|
2021-12-18 17:38:39 +00:00
|
|
|
sentenceParsingOptions,
|
|
|
|
matchTypePrefix
|
2021-01-10 04:10:55 +00:00
|
|
|
}) {
|
2020-09-09 20:59:03 +00:00
|
|
|
if (Array.isArray(inputs)) {
|
2020-09-13 15:33:10 +00:00
|
|
|
this._inputs = inputs.map(({
|
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
types,
|
2020-09-27 15:53:07 +00:00
|
|
|
options: {
|
|
|
|
searchTerms,
|
|
|
|
searchKanji,
|
|
|
|
scanOnTouchMove,
|
|
|
|
scanOnPenHover,
|
|
|
|
scanOnPenPress,
|
|
|
|
scanOnPenRelease,
|
|
|
|
preventTouchScrolling
|
|
|
|
}
|
2020-09-13 15:33:10 +00:00
|
|
|
}) => ({
|
2020-09-09 20:59:03 +00:00
|
|
|
include: this._getInputArray(include),
|
2020-09-11 18:13:52 +00:00
|
|
|
exclude: this._getInputArray(exclude),
|
2020-09-13 15:33:10 +00:00
|
|
|
types: this._getInputTypeSet(types),
|
2020-09-27 15:53:07 +00:00
|
|
|
options: {
|
|
|
|
searchTerms,
|
|
|
|
searchKanji,
|
|
|
|
scanOnTouchMove,
|
|
|
|
scanOnPenHover,
|
|
|
|
scanOnPenPress,
|
|
|
|
scanOnPenRelease,
|
|
|
|
preventTouchScrolling
|
|
|
|
}
|
2020-09-09 20:59:03 +00:00
|
|
|
}));
|
|
|
|
}
|
2020-08-09 17:19:42 +00:00
|
|
|
if (typeof deepContentScan === 'boolean') {
|
|
|
|
this._deepContentScan = deepContentScan;
|
|
|
|
}
|
|
|
|
if (typeof selectText === 'boolean') {
|
|
|
|
this._selectText = selectText;
|
|
|
|
}
|
|
|
|
if (typeof delay === 'number') {
|
|
|
|
this._delay = delay;
|
|
|
|
}
|
|
|
|
if (typeof touchInputEnabled === 'boolean') {
|
2020-09-06 01:44:05 +00:00
|
|
|
this._touchInputEnabled = touchInputEnabled;
|
2020-08-09 17:19:42 +00:00
|
|
|
}
|
2020-09-13 15:33:10 +00:00
|
|
|
if (typeof pointerEventsEnabled === 'boolean') {
|
|
|
|
this._pointerEventsEnabled = pointerEventsEnabled;
|
|
|
|
}
|
2020-08-22 18:40:44 +00:00
|
|
|
if (typeof scanLength === 'number') {
|
|
|
|
this._scanLength = scanLength;
|
|
|
|
}
|
|
|
|
if (typeof layoutAwareScan === 'boolean') {
|
|
|
|
this._layoutAwareScan = layoutAwareScan;
|
|
|
|
}
|
2020-09-26 23:24:21 +00:00
|
|
|
if (typeof preventMiddleMouse === 'boolean') {
|
|
|
|
this._preventMiddleMouse = preventMiddleMouse;
|
|
|
|
}
|
2021-12-18 17:38:39 +00:00
|
|
|
if (typeof matchTypePrefix === 'boolean') {
|
|
|
|
this._matchTypePrefix = matchTypePrefix;
|
|
|
|
}
|
2021-01-10 19:43:06 +00:00
|
|
|
if (typeof sentenceParsingOptions === 'object' && sentenceParsingOptions !== null) {
|
2021-05-16 19:24:38 +00:00
|
|
|
const {scanExtent, terminationCharacterMode, terminationCharacters} = sentenceParsingOptions;
|
2021-01-10 19:43:06 +00:00
|
|
|
if (typeof scanExtent === 'number') {
|
|
|
|
this._sentenceScanExtent = sentenceParsingOptions.scanExtent;
|
|
|
|
}
|
2021-05-16 19:24:38 +00:00
|
|
|
if (typeof terminationCharacterMode === 'string') {
|
|
|
|
this._sentenceTerminateAtNewlines = (terminationCharacterMode === 'custom' || terminationCharacterMode === 'newlines');
|
2021-01-10 19:43:06 +00:00
|
|
|
const sentenceTerminatorMap = this._sentenceTerminatorMap;
|
|
|
|
const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap;
|
|
|
|
const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap;
|
|
|
|
sentenceTerminatorMap.clear();
|
|
|
|
sentenceForwardQuoteMap.clear();
|
|
|
|
sentenceBackwardQuoteMap.clear();
|
2021-05-16 19:24:38 +00:00
|
|
|
if (
|
|
|
|
typeof terminationCharacters === 'object' &&
|
|
|
|
Array.isArray(terminationCharacters) &&
|
|
|
|
(terminationCharacterMode === 'custom' || terminationCharacterMode === 'custom-no-newlines')
|
|
|
|
) {
|
2021-01-10 19:43:06 +00:00
|
|
|
for (const {enabled, character1, character2, includeCharacterAtStart, includeCharacterAtEnd} of terminationCharacters) {
|
|
|
|
if (!enabled) { continue; }
|
|
|
|
if (character2 === null) {
|
|
|
|
sentenceTerminatorMap.set(character1, [includeCharacterAtStart, includeCharacterAtEnd]);
|
|
|
|
} else {
|
|
|
|
sentenceForwardQuoteMap.set(character1, [character2, includeCharacterAtStart]);
|
|
|
|
sentenceBackwardQuoteMap.set(character2, [character1, includeCharacterAtEnd]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 23:05:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-31 19:21:50 +00:00
|
|
|
const includeSelector = this._includeSelector;
|
|
|
|
const excludeSelector = this._excludeSelector;
|
|
|
|
if (includeSelector !== null || excludeSelector !== null) {
|
|
|
|
this._constrainTextSource(clonedTextSource, includeSelector, excludeSelector, layoutAwareScan);
|
2020-05-08 23:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clonedTextSource.text();
|
|
|
|
}
|
|
|
|
|
2020-09-08 23:40:15 +00:00
|
|
|
hasSelection() {
|
|
|
|
return (this._textSourceCurrent !== null);
|
|
|
|
}
|
|
|
|
|
2021-05-20 00:09:37 +00:00
|
|
|
clearSelection() {
|
2020-05-08 23:05:50 +00:00
|
|
|
if (!this._canClearSelection) { return; }
|
|
|
|
if (this._textSourceCurrent !== null) {
|
|
|
|
if (this._textSourceCurrentSelected) {
|
|
|
|
this._textSourceCurrent.deselect();
|
2021-04-27 01:05:46 +00:00
|
|
|
if (this._selectionRestoreInfo !== null) {
|
|
|
|
this._restoreSelection(this._selectionRestoreInfo);
|
|
|
|
this._selectionRestoreInfo = null;
|
|
|
|
}
|
2020-05-08 23:05:50 +00:00
|
|
|
}
|
|
|
|
this._textSourceCurrent = null;
|
|
|
|
this._textSourceCurrentSelected = false;
|
2020-11-09 03:55:43 +00:00
|
|
|
this._inputInfoCurrent = null;
|
2020-05-08 23:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentTextSource() {
|
|
|
|
return this._textSourceCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentTextSource(textSource) {
|
|
|
|
this._textSourceCurrent = textSource;
|
2020-08-09 17:19:42 +00:00
|
|
|
if (this._selectText) {
|
2020-05-08 23:05:50 +00:00
|
|
|
this._textSourceCurrent.select();
|
|
|
|
this._textSourceCurrentSelected = true;
|
|
|
|
} else {
|
|
|
|
this._textSourceCurrentSelected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:52:23 +00:00
|
|
|
async searchLast() {
|
2020-11-09 03:55:43 +00:00
|
|
|
if (this._textSourceCurrent !== null && this._inputInfoCurrent !== null) {
|
|
|
|
await this._search(this._textSourceCurrent, this._searchTerms, this._searchKanji, this._inputInfoCurrent);
|
2020-09-08 22:52:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
async search(textSource, inputDetail) {
|
|
|
|
const inputInfo = this._createInputInfo(null, 'script', 'script', true, [], [], inputDetail);
|
2020-11-09 03:55:43 +00:00
|
|
|
return await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo);
|
2020-09-08 22:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2021-01-12 04:13:35 +00:00
|
|
|
_createOptionsContextForInput(baseOptionsContext, inputInfo) {
|
|
|
|
const optionsContext = clone(baseOptionsContext);
|
2020-11-13 01:32:46 +00:00
|
|
|
const {modifiers, modifierKeys} = inputInfo;
|
|
|
|
optionsContext.modifiers = [...modifiers];
|
|
|
|
optionsContext.modifierKeys = [...modifierKeys];
|
|
|
|
return optionsContext;
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
async _search(textSource, searchTerms, searchKanji, inputInfo) {
|
2021-04-04 20:22:35 +00:00
|
|
|
let dictionaryEntries = null;
|
2020-09-06 01:43:19 +00:00
|
|
|
let sentence = null;
|
|
|
|
let type = null;
|
|
|
|
let error = null;
|
|
|
|
let searched = false;
|
|
|
|
let optionsContext = null;
|
2021-01-12 04:13:35 +00:00
|
|
|
let detail = null;
|
2020-08-22 18:40:44 +00:00
|
|
|
|
2020-09-06 01:43:19 +00:00
|
|
|
try {
|
2021-04-27 01:05:46 +00:00
|
|
|
const inputInfoDetail = inputInfo.detail;
|
|
|
|
const selectionRestoreInfo = (
|
|
|
|
(isObject(inputInfoDetail) && inputInfoDetail.restoreSelection) ?
|
|
|
|
(this._inputInfoCurrent === null ? this._createSelectionRestoreInfo() : void 0) :
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
2020-10-21 00:54:26 +00:00
|
|
|
if (this._textSourceCurrent !== null && this._textSourceCurrent.hasSameStart(textSource)) {
|
2021-01-17 02:53:52 +00:00
|
|
|
return null;
|
2020-09-06 01:43:19 +00:00
|
|
|
}
|
2020-08-22 18:40:44 +00:00
|
|
|
|
2021-01-12 04:13:35 +00:00
|
|
|
({optionsContext, detail} = await this._getSearchContext());
|
|
|
|
optionsContext = this._createOptionsContextForInput(optionsContext, inputInfo);
|
|
|
|
|
2020-09-06 01:43:19 +00:00
|
|
|
searched = true;
|
2020-08-22 18:40:44 +00:00
|
|
|
|
2021-04-27 01:05:46 +00:00
|
|
|
let valid = false;
|
2021-04-04 20:22:35 +00:00
|
|
|
const result = await this._findDictionaryEntries(textSource, searchTerms, searchKanji, optionsContext);
|
2020-09-06 01:43:19 +00:00
|
|
|
if (result !== null) {
|
2021-04-04 20:22:35 +00:00
|
|
|
({dictionaryEntries, sentence, type} = result);
|
2021-04-27 01:05:46 +00:00
|
|
|
valid = true;
|
2021-09-27 23:07:28 +00:00
|
|
|
} else if (textSource !== null && textSource.type === 'element' && await this._hasJapanese(textSource.fullContent)) {
|
2021-04-04 20:22:35 +00:00
|
|
|
dictionaryEntries = [];
|
2021-02-28 19:18:18 +00:00
|
|
|
sentence = {sentence: '', offset: 0};
|
|
|
|
type = 'terms';
|
2021-04-27 01:05:46 +00:00
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid) {
|
2021-02-28 19:18:18 +00:00
|
|
|
this._inputInfoCurrent = inputInfo;
|
|
|
|
this.setCurrentTextSource(textSource);
|
2021-04-27 01:05:46 +00:00
|
|
|
if (typeof selectionRestoreInfo !== 'undefined') {
|
|
|
|
this._selectionRestoreInfo = selectionRestoreInfo;
|
|
|
|
}
|
2020-09-06 01:43:19 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
2020-08-22 18:40:44 +00:00
|
|
|
|
2021-01-17 02:53:52 +00:00
|
|
|
if (!searched) { return null; }
|
2020-08-22 18:40:44 +00:00
|
|
|
|
2021-01-17 02:53:52 +00:00
|
|
|
const results = {
|
2020-09-06 01:43:19 +00:00
|
|
|
textScanner: this,
|
|
|
|
type,
|
2021-04-04 20:22:35 +00:00
|
|
|
dictionaryEntries,
|
2020-09-06 01:43:19 +00:00
|
|
|
sentence,
|
2020-11-09 03:55:43 +00:00
|
|
|
inputInfo,
|
2020-09-06 01:43:19 +00:00
|
|
|
textSource,
|
|
|
|
optionsContext,
|
2021-01-12 04:13:35 +00:00
|
|
|
detail,
|
2020-09-06 01:43:19 +00:00
|
|
|
error
|
2021-01-17 02:53:52 +00:00
|
|
|
};
|
|
|
|
this.trigger('searched', results);
|
|
|
|
return results;
|
2020-08-22 18:40:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 01:01:37 +00:00
|
|
|
_resetPreventNextClickScan() {
|
|
|
|
this._preventNextClickScan = false;
|
|
|
|
if (this._preventNextClickScanTimer !== null) { clearTimeout(this._preventNextClickScanTimer); }
|
|
|
|
this._preventNextClickScanTimer = setTimeout(this._preventNextClickScanTimerCallback, this._preventNextClickScanTimerDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPreventNextClickScanTimeout() {
|
|
|
|
this._preventNextClickScanTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onSelectionChange() {
|
|
|
|
if (this._preventNextClickScanTimer !== null) { return; } // Ignore deselection that occurs at the start of the click
|
|
|
|
this._preventNextClickScan = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchClickMouseDown(e) {
|
|
|
|
if (e.button !== 0) { return; }
|
|
|
|
this._resetPreventNextClickScan();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchClickTouchStart() {
|
|
|
|
this._resetPreventNextClickScan();
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_onMouseOver(e) {
|
2020-11-24 01:31:48 +00:00
|
|
|
if (this._ignoreElements !== null && this._ignoreElements().includes(e.target)) {
|
2020-05-07 23:45:08 +00:00
|
|
|
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-11-09 03:55:43 +00:00
|
|
|
const inputInfo = this._getMatchingInputGroupFromEvent('mouse', 'mouseMove', e);
|
2020-09-09 20:59:03 +00:00
|
|
|
if (inputInfo === null) { return; }
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-09-13 17:25:03 +00:00
|
|
|
this._searchAtFromMouseMove(e.clientX, e.clientY, inputInfo);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-26 23:24:21 +00:00
|
|
|
switch (e.button) {
|
|
|
|
case 0: // Primary
|
2021-03-10 01:01:37 +00:00
|
|
|
if (this._searchOnClick) { this._resetPreventNextClickScan(); }
|
2020-09-26 23:24:21 +00:00
|
|
|
this._scanTimerClear();
|
2021-05-20 00:09:37 +00:00
|
|
|
this._triggerClear('mousedown');
|
2020-09-26 23:24:21 +00:00
|
|
|
break;
|
|
|
|
case 1: // Middle
|
|
|
|
if (this._preventMiddleMouse) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
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;
|
|
|
|
}
|
2021-03-10 01:01:37 +00:00
|
|
|
|
|
|
|
if (this._searchOnClick) {
|
|
|
|
this._onSearchClick(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchClick(e) {
|
|
|
|
const preventNextClickScan = this._preventNextClickScan;
|
|
|
|
this._preventNextClickScan = false;
|
|
|
|
if (this._preventNextClickScanTimer !== null) {
|
|
|
|
clearTimeout(this._preventNextClickScanTimer);
|
|
|
|
this._preventNextClickScanTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preventNextClickScan) { return; }
|
|
|
|
|
|
|
|
const modifiers = DocumentUtil.getActiveModifiersAndButtons(e);
|
|
|
|
const modifierKeys = DocumentUtil.getActiveModifiers(e);
|
|
|
|
const inputInfo = this._createInputInfo(null, 'mouse', 'click', false, modifiers, modifierKeys);
|
|
|
|
this._searchAt(e.clientX, e.clientY, inputInfo);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
|
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-09-12 00:24:51 +00:00
|
|
|
const {clientX, clientY, identifier} = e.changedTouches[0];
|
|
|
|
this._onPrimaryTouchStart(e, clientX, clientY, identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPrimaryTouchStart(e, x, y, identifier) {
|
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
|
|
|
|
2020-09-12 00:24:51 +00:00
|
|
|
if (DocumentUtil.isPointInSelection(x, y, window.getSelection())) {
|
2019-12-05 01:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:24:51 +00:00
|
|
|
this._primaryTouchIdentifier = identifier;
|
2019-12-05 01:58:35 +00:00
|
|
|
|
2020-09-12 00:24:51 +00:00
|
|
|
this._searchAtFromTouchStart(e, x, y);
|
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-09-12 00:24:51 +00:00
|
|
|
this._onPrimaryTouchEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPrimaryTouchEnd() {
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e);
|
2020-09-11 18:13:52 +00:00
|
|
|
if (inputInfo === null) { return; }
|
|
|
|
|
2020-09-27 15:46:37 +00:00
|
|
|
if (inputInfo.input.options.scanOnTouchMove) {
|
2020-11-09 03:55:43 +00:00
|
|
|
this._searchAt(primaryTouch.clientX, primaryTouch.clientY, inputInfo);
|
2020-09-27 15:46:37 +00:00
|
|
|
}
|
2019-12-05 01:58:35 +00:00
|
|
|
|
|
|
|
e.preventDefault(); // Disable scroll
|
|
|
|
}
|
|
|
|
|
2020-09-13 15:33:10 +00:00
|
|
|
_onPointerOver(e) {
|
2020-09-13 15:58:07 +00:00
|
|
|
const {pointerType, pointerId, isPrimary} = e;
|
|
|
|
if (pointerType === 'pen') {
|
|
|
|
this._pointerIdTypeMap.set(pointerId, pointerType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isPrimary) { return; }
|
|
|
|
switch (pointerType) {
|
2020-09-13 15:33:10 +00:00
|
|
|
case 'mouse': return this._onMousePointerOver(e);
|
|
|
|
case 'touch': return this._onTouchPointerOver(e);
|
|
|
|
case 'pen': return this._onPenPointerOver(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPointerDown(e) {
|
|
|
|
if (!e.isPrimary) { return; }
|
2020-09-13 15:58:07 +00:00
|
|
|
switch (this._getPointerEventType(e)) {
|
2020-09-13 15:33:10 +00:00
|
|
|
case 'mouse': return this._onMousePointerDown(e);
|
|
|
|
case 'touch': return this._onTouchPointerDown(e);
|
|
|
|
case 'pen': return this._onPenPointerDown(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPointerMove(e) {
|
|
|
|
if (!e.isPrimary) { return; }
|
2020-09-13 15:58:07 +00:00
|
|
|
switch (this._getPointerEventType(e)) {
|
2020-09-13 15:33:10 +00:00
|
|
|
case 'mouse': return this._onMousePointerMove(e);
|
|
|
|
case 'touch': return this._onTouchPointerMove(e);
|
|
|
|
case 'pen': return this._onPenPointerMove(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPointerUp(e) {
|
|
|
|
if (!e.isPrimary) { return; }
|
2020-09-13 15:58:07 +00:00
|
|
|
switch (this._getPointerEventType(e)) {
|
2020-09-13 15:33:10 +00:00
|
|
|
case 'mouse': return this._onMousePointerUp(e);
|
|
|
|
case 'touch': return this._onTouchPointerUp(e);
|
|
|
|
case 'pen': return this._onPenPointerUp(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPointerCancel(e) {
|
2020-09-13 15:58:07 +00:00
|
|
|
this._pointerIdTypeMap.delete(e.pointerId);
|
2020-09-13 15:33:10 +00:00
|
|
|
if (!e.isPrimary) { return; }
|
|
|
|
switch (e.pointerType) {
|
|
|
|
case 'mouse': return this._onMousePointerCancel(e);
|
|
|
|
case 'touch': return this._onTouchPointerCancel(e);
|
|
|
|
case 'pen': return this._onPenPointerCancel(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPointerOut(e) {
|
2020-09-13 15:58:07 +00:00
|
|
|
this._pointerIdTypeMap.delete(e.pointerId);
|
2020-09-13 15:33:10 +00:00
|
|
|
if (!e.isPrimary) { return; }
|
|
|
|
switch (e.pointerType) {
|
|
|
|
case 'mouse': return this._onMousePointerOut(e);
|
|
|
|
case 'touch': return this._onTouchPointerOut(e);
|
|
|
|
case 'pen': return this._onPenPointerOut(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMousePointerOver(e) {
|
|
|
|
return this._onMouseOver(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMousePointerDown(e) {
|
|
|
|
return this._onMouseDown(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMousePointerMove(e) {
|
|
|
|
return this._onMouseMove(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMousePointerUp() {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMousePointerCancel(e) {
|
|
|
|
return this._onMouseOut(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMousePointerOut(e) {
|
|
|
|
return this._onMouseOut(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTouchPointerOver() {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTouchPointerDown(e) {
|
|
|
|
const {clientX, clientY, pointerId} = e;
|
|
|
|
return this._onPrimaryTouchStart(e, clientX, clientY, pointerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTouchPointerMove(e) {
|
|
|
|
if (!this._preventScroll || !e.cancelable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e);
|
2020-09-27 15:46:37 +00:00
|
|
|
if (inputInfo === null || !inputInfo.input.options.scanOnTouchMove) { return; }
|
2020-09-13 15:33:10 +00:00
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
this._searchAt(e.clientX, e.clientY, inputInfo);
|
2020-09-13 15:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onTouchPointerUp() {
|
|
|
|
return this._onPrimaryTouchEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTouchPointerCancel() {
|
|
|
|
return this._onPrimaryTouchEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTouchPointerOut() {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTouchMovePreventScroll(e) {
|
|
|
|
if (!this._preventScroll) { return; }
|
|
|
|
|
|
|
|
if (e.cancelable) {
|
|
|
|
e.preventDefault();
|
|
|
|
} else {
|
|
|
|
this._preventScroll = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPenPointerOver(e) {
|
|
|
|
this._penPointerPressed = false;
|
|
|
|
this._penPointerReleased = false;
|
|
|
|
this._searchAtFromPen(e, e.clientX, e.clientY, 'pointerOver', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPenPointerDown(e) {
|
|
|
|
this._penPointerPressed = true;
|
|
|
|
this._searchAtFromPen(e, e.clientX, e.clientY, 'pointerDown', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPenPointerMove(e) {
|
|
|
|
if (this._penPointerPressed && (!this._preventScroll || !e.cancelable)) { return; }
|
|
|
|
this._searchAtFromPen(e, e.clientX, e.clientY, 'pointerMove', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPenPointerUp() {
|
|
|
|
this._penPointerPressed = false;
|
|
|
|
this._penPointerReleased = true;
|
|
|
|
this._preventScroll = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPenPointerCancel(e) {
|
|
|
|
this._onPenPointerOut(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPenPointerOut() {
|
|
|
|
this._penPointerPressed = false;
|
|
|
|
this._penPointerReleased = false;
|
|
|
|
this._preventScroll = false;
|
|
|
|
this._preventNextContextMenu = false;
|
|
|
|
this._preventNextMouseDown = false;
|
|
|
|
this._preventNextClick = false;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
async _scanTimerWait() {
|
2020-08-09 17:19:42 +00:00
|
|
|
const delay = this._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-09-13 15:33:10 +00:00
|
|
|
_arePointerEventsSupported() {
|
|
|
|
return (this._pointerEventsEnabled && typeof PointerEvent !== 'undefined');
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:45:08 +00:00
|
|
|
_hookEvents() {
|
2020-09-13 15:33:10 +00:00
|
|
|
let eventListenerInfos;
|
2020-11-24 01:31:48 +00:00
|
|
|
if (this._searchOnClickOnly) {
|
|
|
|
eventListenerInfos = this._getMouseClickOnlyEventListeners();
|
|
|
|
} else if (this._arePointerEventsSupported()) {
|
2020-09-13 15:33:10 +00:00
|
|
|
eventListenerInfos = this._getPointerEventListeners();
|
|
|
|
} else {
|
|
|
|
eventListenerInfos = this._getMouseEventListeners();
|
|
|
|
if (this._touchInputEnabled) {
|
|
|
|
eventListenerInfos.push(...this._getTouchEventListeners());
|
|
|
|
}
|
2019-12-06 19:39:29 +00:00
|
|
|
}
|
2021-03-10 01:01:37 +00:00
|
|
|
if (this._searchOnClick) {
|
|
|
|
eventListenerInfos.push(...this._getMouseClickOnlyEventListeners2());
|
|
|
|
}
|
2019-12-06 19:39:29 +00:00
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
for (const args of eventListenerInfos) {
|
|
|
|
this._eventListeners.addEventListener(...args);
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 15:33:10 +00:00
|
|
|
_getPointerEventListeners() {
|
|
|
|
return [
|
|
|
|
[this._node, 'pointerover', this._onPointerOver.bind(this)],
|
|
|
|
[this._node, 'pointerdown', this._onPointerDown.bind(this)],
|
|
|
|
[this._node, 'pointermove', this._onPointerMove.bind(this)],
|
|
|
|
[this._node, 'pointerup', this._onPointerUp.bind(this)],
|
|
|
|
[this._node, 'pointercancel', this._onPointerCancel.bind(this)],
|
|
|
|
[this._node, 'pointerout', this._onPointerOut.bind(this)],
|
|
|
|
[this._node, 'touchmove', this._onTouchMovePreventScroll.bind(this), {passive: false}],
|
|
|
|
[this._node, 'mousedown', this._onMouseDown.bind(this)],
|
|
|
|
[this._node, 'click', this._onClick.bind(this)],
|
|
|
|
[this._node, 'auxclick', this._onAuxClick.bind(this)]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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)],
|
2020-09-06 01:43:19 +00:00
|
|
|
[this._node, 'mouseout', this._onMouseOut.bind(this)],
|
|
|
|
[this._node, 'click', this._onClick.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, '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
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:01:37 +00:00
|
|
|
_getMouseClickOnlyEventListeners() {
|
|
|
|
return [
|
|
|
|
[this._node, 'click', this._onClick.bind(this)]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
_getMouseClickOnlyEventListeners2() {
|
|
|
|
const {documentElement} = document;
|
|
|
|
const entries = [
|
|
|
|
[document, 'selectionchange', this._onSelectionChange.bind(this)]
|
|
|
|
];
|
|
|
|
if (documentElement !== null) {
|
|
|
|
entries.push([documentElement, 'mousedown', this._onSearchClickMouseDown.bind(this)]);
|
|
|
|
if (this._touchInputEnabled) {
|
|
|
|
entries.push([documentElement, 'touchstart', this._onSearchClickTouchStart.bind(this)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-09-06 01:43:19 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
async _findDictionaryEntries(textSource, searchTerms, searchKanji, optionsContext) {
|
2020-09-06 01:43:19 +00:00
|
|
|
if (textSource === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-13 17:25:03 +00:00
|
|
|
if (searchTerms) {
|
2021-04-04 20:22:35 +00:00
|
|
|
const results = await this._findTermDictionaryEntries(textSource, optionsContext);
|
2020-09-06 01:43:19 +00:00
|
|
|
if (results !== null) { return results; }
|
|
|
|
}
|
2020-09-13 17:25:03 +00:00
|
|
|
if (searchKanji) {
|
2021-04-04 20:22:35 +00:00
|
|
|
const results = await this._findKanjiDictionaryEntries(textSource, optionsContext);
|
2020-09-06 01:43:19 +00:00
|
|
|
if (results !== null) { return results; }
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
async _findTermDictionaryEntries(textSource, optionsContext) {
|
2020-09-06 01:43:19 +00:00
|
|
|
const scanLength = this._scanLength;
|
2021-01-10 04:10:55 +00:00
|
|
|
const sentenceScanExtent = this._sentenceScanExtent;
|
2021-05-16 19:24:38 +00:00
|
|
|
const sentenceTerminateAtNewlines = this._sentenceTerminateAtNewlines;
|
2021-01-10 19:43:06 +00:00
|
|
|
const sentenceTerminatorMap = this._sentenceTerminatorMap;
|
|
|
|
const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap;
|
|
|
|
const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap;
|
2020-09-06 01:43:19 +00:00
|
|
|
const layoutAwareScan = this._layoutAwareScan;
|
|
|
|
const searchText = this.getTextSourceContent(textSource, scanLength, layoutAwareScan);
|
|
|
|
if (searchText.length === 0) { return null; }
|
|
|
|
|
2021-12-18 17:38:39 +00:00
|
|
|
const details = {};
|
|
|
|
if (this._matchTypePrefix) { details.matchType = 'prefix'; }
|
|
|
|
const {dictionaryEntries, originalTextLength} = await yomichan.api.termsFind(searchText, details, optionsContext);
|
2021-04-04 20:22:35 +00:00
|
|
|
if (dictionaryEntries.length === 0) { return null; }
|
2020-09-06 01:43:19 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
textSource.setEndOffset(originalTextLength, layoutAwareScan);
|
2021-01-10 19:43:06 +00:00
|
|
|
const sentence = this._documentUtil.extractSentence(
|
|
|
|
textSource,
|
|
|
|
layoutAwareScan,
|
|
|
|
sentenceScanExtent,
|
2021-05-16 19:24:38 +00:00
|
|
|
sentenceTerminateAtNewlines,
|
2021-01-10 19:43:06 +00:00
|
|
|
sentenceTerminatorMap,
|
|
|
|
sentenceForwardQuoteMap,
|
|
|
|
sentenceBackwardQuoteMap
|
|
|
|
);
|
2020-09-06 01:43:19 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
return {dictionaryEntries, sentence, type: 'terms'};
|
2020-09-06 01:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
async _findKanjiDictionaryEntries(textSource, optionsContext) {
|
2021-01-10 04:10:55 +00:00
|
|
|
const sentenceScanExtent = this._sentenceScanExtent;
|
2021-05-16 19:24:38 +00:00
|
|
|
const sentenceTerminateAtNewlines = this._sentenceTerminateAtNewlines;
|
2021-01-10 19:43:06 +00:00
|
|
|
const sentenceTerminatorMap = this._sentenceTerminatorMap;
|
|
|
|
const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap;
|
|
|
|
const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap;
|
2020-09-06 01:43:19 +00:00
|
|
|
const layoutAwareScan = this._layoutAwareScan;
|
|
|
|
const searchText = this.getTextSourceContent(textSource, 1, layoutAwareScan);
|
|
|
|
if (searchText.length === 0) { return null; }
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const dictionaryEntries = await yomichan.api.kanjiFind(searchText, optionsContext);
|
|
|
|
if (dictionaryEntries.length === 0) { return null; }
|
2020-09-06 01:43:19 +00:00
|
|
|
|
|
|
|
textSource.setEndOffset(1, layoutAwareScan);
|
2021-01-10 19:43:06 +00:00
|
|
|
const sentence = this._documentUtil.extractSentence(
|
|
|
|
textSource,
|
|
|
|
layoutAwareScan,
|
|
|
|
sentenceScanExtent,
|
2021-05-16 19:24:38 +00:00
|
|
|
sentenceTerminateAtNewlines,
|
2021-01-10 19:43:06 +00:00
|
|
|
sentenceTerminatorMap,
|
|
|
|
sentenceForwardQuoteMap,
|
|
|
|
sentenceBackwardQuoteMap
|
|
|
|
);
|
2020-09-06 01:43:19 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
return {dictionaryEntries, sentence, type: 'kanji'};
|
2020-09-06 01:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
async _searchAt(x, y, inputInfo) {
|
2020-09-06 01:43:19 +00:00
|
|
|
if (this._pendingLookup) { return; }
|
|
|
|
|
|
|
|
try {
|
2020-11-09 03:55:43 +00:00
|
|
|
const sourceInput = inputInfo.input;
|
2020-09-13 17:25:03 +00:00
|
|
|
let searchTerms = this._searchTerms;
|
|
|
|
let searchKanji = this._searchKanji;
|
|
|
|
if (sourceInput !== null) {
|
|
|
|
if (searchTerms && !sourceInput.options.searchTerms) { searchTerms = false; }
|
|
|
|
if (searchKanji && !sourceInput.options.searchKanji) { searchKanji = false; }
|
|
|
|
}
|
|
|
|
|
2020-09-06 01:43:19 +00:00
|
|
|
this._pendingLookup = true;
|
|
|
|
this._scanTimerClear();
|
|
|
|
|
|
|
|
if (typeof this._ignorePoint === 'function' && await this._ignorePoint(x, y)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const textSource = this._documentUtil.getRangeFromPoint(x, y, this._deepContentScan);
|
|
|
|
try {
|
2020-11-09 03:55:43 +00:00
|
|
|
await this._search(textSource, searchTerms, searchKanji, inputInfo);
|
2020-09-06 01:43:19 +00:00
|
|
|
} finally {
|
|
|
|
if (textSource !== null) {
|
|
|
|
textSource.cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(e);
|
2020-09-06 01:43:19 +00:00
|
|
|
} finally {
|
|
|
|
this._pendingLookup = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:25:03 +00:00
|
|
|
async _searchAtFromMouseMove(x, y, inputInfo) {
|
2020-09-06 01:43:19 +00:00
|
|
|
if (this._pendingLookup) { return; }
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
if (inputInfo.passive) {
|
2020-09-06 01:43:19 +00:00
|
|
|
if (!await this._scanTimerWait()) {
|
|
|
|
// Aborted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
await this._searchAt(x, y, inputInfo);
|
2020-09-06 01:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:13:52 +00:00
|
|
|
async _searchAtFromTouchStart(e, x, y) {
|
2020-09-06 01:43:19 +00:00
|
|
|
if (this._pendingLookup) { return; }
|
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchStart', e);
|
2020-09-11 18:13:52 +00:00
|
|
|
if (inputInfo === null) { return; }
|
|
|
|
|
2020-09-06 01:43:19 +00:00
|
|
|
const textSourceCurrentPrevious = this._textSourceCurrent !== null ? this._textSourceCurrent.clone() : null;
|
2020-09-27 15:46:37 +00:00
|
|
|
const preventScroll = inputInfo.input.options.preventTouchScrolling;
|
2020-09-06 01:43:19 +00:00
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
await this._searchAt(x, y, inputInfo);
|
2020-09-06 01:43:19 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
this._textSourceCurrent !== null &&
|
2020-10-21 00:54:26 +00:00
|
|
|
!this._textSourceCurrent.hasSameStart(textSourceCurrentPrevious)
|
2020-09-06 01:43:19 +00:00
|
|
|
) {
|
2020-09-27 15:46:37 +00:00
|
|
|
this._preventScroll = preventScroll;
|
2020-09-06 01:43:19 +00:00
|
|
|
this._preventNextContextMenu = true;
|
|
|
|
this._preventNextMouseDown = true;
|
|
|
|
}
|
|
|
|
}
|
2020-09-09 20:59:03 +00:00
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
async _searchAtFromPen(e, x, y, eventType, prevent) {
|
2020-09-13 15:33:10 +00:00
|
|
|
if (this._pendingLookup) { return; }
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
const inputInfo = this._getMatchingInputGroupFromEvent('pen', eventType, e);
|
2020-09-13 15:33:10 +00:00
|
|
|
if (inputInfo === null) { return; }
|
|
|
|
|
2020-09-13 17:25:03 +00:00
|
|
|
const {input: {options}} = inputInfo;
|
2020-09-13 15:33:10 +00:00
|
|
|
if (
|
|
|
|
(!options.scanOnPenRelease && this._penPointerReleased) ||
|
|
|
|
!(this._penPointerPressed ? options.scanOnPenPress : options.scanOnPenHover)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-27 15:46:37 +00:00
|
|
|
const preventScroll = inputInfo.input.options.preventTouchScrolling;
|
|
|
|
|
2020-11-09 03:55:43 +00:00
|
|
|
await this._searchAt(x, y, inputInfo);
|
2020-09-13 15:33:10 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
prevent &&
|
|
|
|
this._textSourceCurrent !== null
|
|
|
|
) {
|
2020-09-27 15:46:37 +00:00
|
|
|
this._preventScroll = preventScroll;
|
2020-09-13 15:33:10 +00:00
|
|
|
this._preventNextContextMenu = true;
|
|
|
|
this._preventNextMouseDown = true;
|
|
|
|
this._preventNextClick = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
_getMatchingInputGroupFromEvent(pointerType, eventType, event) {
|
2020-09-11 18:13:52 +00:00
|
|
|
const modifiers = DocumentUtil.getActiveModifiersAndButtons(event);
|
2020-11-09 03:55:43 +00:00
|
|
|
const modifierKeys = DocumentUtil.getActiveModifiers(event);
|
2021-01-17 16:43:05 +00:00
|
|
|
return this._getMatchingInputGroup(pointerType, eventType, modifiers, modifierKeys);
|
2020-09-11 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
_getMatchingInputGroup(pointerType, eventType, modifiers, modifierKeys) {
|
|
|
|
let fallbackIndex = -1;
|
2020-11-09 03:19:54 +00:00
|
|
|
const modifiersSet = new Set(modifiers);
|
2020-09-09 20:59:03 +00:00
|
|
|
for (let i = 0, ii = this._inputs.length; i < ii; ++i) {
|
|
|
|
const input = this._inputs[i];
|
2020-09-11 18:13:52 +00:00
|
|
|
const {include, exclude, types} = input;
|
2021-01-17 16:43:05 +00:00
|
|
|
if (!types.has(pointerType)) { continue; }
|
2020-11-09 03:19:54 +00:00
|
|
|
if (this._setHasAll(modifiersSet, include) && (exclude.length === 0 || !this._setHasAll(modifiersSet, exclude))) {
|
2020-09-09 20:59:03 +00:00
|
|
|
if (include.length > 0) {
|
2021-01-17 16:43:05 +00:00
|
|
|
return this._createInputInfo(input, pointerType, eventType, false, modifiers, modifierKeys);
|
|
|
|
} else if (fallbackIndex < 0) {
|
|
|
|
fallbackIndex = i;
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 16:43:05 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
fallbackIndex >= 0 ?
|
|
|
|
this._createInputInfo(this._inputs[fallbackIndex], pointerType, eventType, true, modifiers, modifierKeys) :
|
|
|
|
null
|
|
|
|
);
|
2020-09-09 20:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
_createInputInfo(input, pointerType, eventType, passive, modifiers, modifierKeys, detail) {
|
|
|
|
return {input, pointerType, eventType, passive, modifiers, modifierKeys, detail};
|
2020-11-09 03:55:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 20:59:03 +00:00
|
|
|
_setHasAll(set, values) {
|
|
|
|
for (const value of values) {
|
|
|
|
if (!set.has(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getInputArray(value) {
|
|
|
|
return (
|
|
|
|
typeof value === 'string' ?
|
|
|
|
value.split(/[,;\s]+/).map((v) => v.trim().toLowerCase()).filter((v) => v.length > 0) :
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
2020-09-11 18:13:52 +00:00
|
|
|
|
|
|
|
_getInputTypeSet({mouse, touch, pen}) {
|
|
|
|
const set = new Set();
|
|
|
|
if (mouse) { set.add('mouse'); }
|
|
|
|
if (touch) { set.add('touch'); }
|
|
|
|
if (pen) { set.add('pen'); }
|
|
|
|
return set;
|
|
|
|
}
|
2020-09-13 15:58:07 +00:00
|
|
|
|
|
|
|
_getPointerEventType(e) {
|
|
|
|
// Workaround for Firefox bug not detecting certain 'touch' events as 'pen' events.
|
|
|
|
const cachedPointerType = this._pointerIdTypeMap.get(e.pointerId);
|
|
|
|
return (typeof cachedPointerType !== 'undefined' ? cachedPointerType : e.pointerType);
|
|
|
|
}
|
2020-11-24 01:31:48 +00:00
|
|
|
|
|
|
|
_constrainTextSource(textSource, includeSelector, excludeSelector, layoutAwareScan) {
|
|
|
|
let length = textSource.text().length;
|
|
|
|
while (length > 0) {
|
|
|
|
const nodes = textSource.getNodesInRange();
|
|
|
|
if (
|
|
|
|
(includeSelector !== null && !DocumentUtil.everyNodeMatchesSelector(nodes, includeSelector)) ||
|
|
|
|
(excludeSelector !== null && DocumentUtil.anyNodeMatchesSelector(nodes, excludeSelector))
|
|
|
|
) {
|
|
|
|
--length;
|
|
|
|
textSource.setEndOffset(length, layoutAwareScan);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 19:18:18 +00:00
|
|
|
|
|
|
|
async _hasJapanese(text) {
|
|
|
|
try {
|
|
|
|
return await yomichan.api.textHasJapaneseCharacters(text);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 01:05:46 +00:00
|
|
|
|
|
|
|
_createSelectionRestoreInfo() {
|
|
|
|
const ranges = [];
|
|
|
|
const selection = window.getSelection();
|
|
|
|
for (let i = 0, ii = selection.rangeCount; i < ii; ++i) {
|
|
|
|
const range = selection.getRangeAt(i);
|
|
|
|
ranges.push(range.cloneRange());
|
|
|
|
}
|
|
|
|
return {ranges};
|
|
|
|
}
|
|
|
|
|
|
|
|
_restoreSelection(selectionRestoreInfo) {
|
|
|
|
const {ranges} = selectionRestoreInfo;
|
|
|
|
const selection = window.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
for (const range of ranges) {
|
|
|
|
try {
|
|
|
|
selection.addRange(range);
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 00:09:37 +00:00
|
|
|
|
|
|
|
_triggerClear(reason) {
|
|
|
|
this.trigger('clear', {reason});
|
|
|
|
}
|
2019-12-05 01:58:35 +00:00
|
|
|
}
|