Text scanner refactor (#515)

* Mark functions as private

* Mark fields as private

* Add getter/setters for publicly used fields

* Replace public static functions with private non-static functions

* Update comment
This commit is contained in:
toasted-nutbread 2020-05-07 19:45:08 -04:00 committed by GitHub
parent 33bd9682ea
commit b972f8cbf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,27 +24,27 @@
class TextScanner extends EventDispatcher { class TextScanner extends EventDispatcher {
constructor(node, ignoreElements, ignorePoints) { constructor(node, ignoreElements, ignorePoints) {
super(); super();
this.node = node; this._node = node;
this.ignoreElements = ignoreElements; this._ignoreElements = ignoreElements;
this.ignorePoints = ignorePoints; this._ignorePoints = ignorePoints;
this.ignoreNodes = null; this._ignoreNodes = null;
this.scanTimerPromise = null; this._causeCurrent = null;
this.causeCurrent = null; this._scanTimerPromise = null;
this.textSourceCurrent = null; this._textSourceCurrent = null;
this.textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
this.pendingLookup = false; this._pendingLookup = false;
this.options = null; this._options = null;
this.enabled = false; this._enabled = false;
this.eventListeners = new EventListenerCollection(); this._eventListeners = new EventListenerCollection();
this.primaryTouchIdentifier = null; this._primaryTouchIdentifier = null;
this.preventNextContextMenu = false; this._preventNextContextMenu = false;
this.preventNextMouseDown = false; this._preventNextMouseDown = false;
this.preventNextClick = false; this._preventNextClick = false;
this.preventScroll = false; this._preventScroll = false;
this._canClearSelection = true; this._canClearSelection = true;
} }
@ -57,26 +57,38 @@ class TextScanner extends EventDispatcher {
this._canClearSelection = value; this._canClearSelection = value;
} }
onMouseOver(e) { get ignoreNodes() {
if (this.ignoreElements().includes(e.target)) { return this._ignoreNodes;
this.scanTimerClear(); }
set ignoreNodes(value) {
this._ignoreNodes = value;
}
get causeCurrent() {
return this._causeCurrent;
}
_onMouseOver(e) {
if (this._ignoreElements().includes(e.target)) {
this._scanTimerClear();
} }
} }
onMouseMove(e) { _onMouseMove(e) {
this.scanTimerClear(); this._scanTimerClear();
if (this.pendingLookup || DOM.isMouseButtonDown(e, 'primary')) { if (this._pendingLookup || DOM.isMouseButtonDown(e, 'primary')) {
return; return;
} }
const modifiers = DOM.getActiveModifiers(e); const modifiers = DOM.getActiveModifiers(e);
this.trigger('activeModifiersChanged', {modifiers}); this.trigger('activeModifiersChanged', {modifiers});
const scanningOptions = this.options.scanning; const scanningOptions = this._options.scanning;
const scanningModifier = scanningOptions.modifier; const scanningModifier = scanningOptions.modifier;
if (!( if (!(
TextScanner.isScanningModifierPressed(scanningModifier, e) || this._isScanningModifierPressed(scanningModifier, e) ||
(scanningOptions.middleMouse && DOM.isMouseButtonDown(e, 'auxiliary')) (scanningOptions.middleMouse && DOM.isMouseButtonDown(e, 'auxiliary'))
)) { )) {
return; return;
@ -84,7 +96,7 @@ class TextScanner extends EventDispatcher {
const search = async () => { const search = async () => {
if (scanningModifier === 'none') { if (scanningModifier === 'none') {
if (!await this.scanTimerWait()) { if (!await this._scanTimerWait()) {
// Aborted // Aborted
return; return;
} }
@ -96,112 +108,110 @@ class TextScanner extends EventDispatcher {
search(); search();
} }
onMouseDown(e) { _onMouseDown(e) {
if (this.preventNextMouseDown) { if (this._preventNextMouseDown) {
this.preventNextMouseDown = false; this._preventNextMouseDown = false;
this.preventNextClick = true; this._preventNextClick = true;
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
return false; return false;
} }
if (DOM.isMouseButtonDown(e, 'primary')) { if (DOM.isMouseButtonDown(e, 'primary')) {
this.scanTimerClear(); this._scanTimerClear();
this.clearSelection(false); this.clearSelection(false);
} }
} }
onMouseOut() { _onMouseOut() {
this.scanTimerClear(); this._scanTimerClear();
} }
onClick(e) { _onClick(e) {
if (this.preventNextClick) { if (this._preventNextClick) {
this.preventNextClick = false; this._preventNextClick = false;
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
return false; return false;
} }
} }
onAuxClick() { _onAuxClick() {
this.preventNextContextMenu = false; this._preventNextContextMenu = false;
} }
onContextMenu(e) { _onContextMenu(e) {
if (this.preventNextContextMenu) { if (this._preventNextContextMenu) {
this.preventNextContextMenu = false; this._preventNextContextMenu = false;
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
return false; return false;
} }
} }
onTouchStart(e) { _onTouchStart(e) {
if (this.primaryTouchIdentifier !== null || e.changedTouches.length === 0) { if (this._primaryTouchIdentifier !== null || e.changedTouches.length === 0) {
return; return;
} }
this.preventScroll = false; this._preventScroll = false;
this.preventNextContextMenu = false; this._preventNextContextMenu = false;
this.preventNextMouseDown = false; this._preventNextMouseDown = false;
this.preventNextClick = false; this._preventNextClick = false;
const primaryTouch = e.changedTouches[0]; const primaryTouch = e.changedTouches[0];
if (DOM.isPointInSelection(primaryTouch.clientX, primaryTouch.clientY, window.getSelection())) { if (DOM.isPointInSelection(primaryTouch.clientX, primaryTouch.clientY, window.getSelection())) {
return; return;
} }
this.primaryTouchIdentifier = primaryTouch.identifier; this._primaryTouchIdentifier = primaryTouch.identifier;
if (this.pendingLookup) { if (this._pendingLookup) {
return; return;
} }
const textSourceCurrentPrevious = this.textSourceCurrent !== null ? this.textSourceCurrent.clone() : null; const textSourceCurrentPrevious = this._textSourceCurrent !== null ? this._textSourceCurrent.clone() : null;
this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchStart') this.searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchStart')
.then(() => { .then(() => {
if ( if (
this.textSourceCurrent === null || this._textSourceCurrent === null ||
this.textSourceCurrent.equals(textSourceCurrentPrevious) this._textSourceCurrent.equals(textSourceCurrentPrevious)
) { ) {
return; return;
} }
this.preventScroll = true; this._preventScroll = true;
this.preventNextContextMenu = true; this._preventNextContextMenu = true;
this.preventNextMouseDown = true; this._preventNextMouseDown = true;
}); });
} }
onTouchEnd(e) { _onTouchEnd(e) {
if ( if (
this.primaryTouchIdentifier === null || this._primaryTouchIdentifier === null ||
TextScanner.getTouch(e.changedTouches, this.primaryTouchIdentifier) === null this._getTouch(e.changedTouches, this._primaryTouchIdentifier) === null
) { ) {
return; return;
} }
this.primaryTouchIdentifier = null; this._primaryTouchIdentifier = null;
this.preventScroll = false; this._preventScroll = false;
this.preventNextClick = false; this._preventNextClick = false;
// Don't revert context menu and mouse down prevention, // Don't revert context menu and mouse down prevention, since these events can occur after the touch has ended.
// since these events can occur after the touch has ended. // I.e. this._preventNextContextMenu and this._preventNextMouseDown should not be assigned to false.
// this.preventNextContextMenu = false;
// this.preventNextMouseDown = false;
} }
onTouchCancel(e) { _onTouchCancel(e) {
this.onTouchEnd(e); this._onTouchEnd(e);
} }
onTouchMove(e) { _onTouchMove(e) {
if (!this.preventScroll || !e.cancelable || this.primaryTouchIdentifier === null) { if (!this._preventScroll || !e.cancelable || this._primaryTouchIdentifier === null) {
return; return;
} }
const primaryTouch = TextScanner.getTouch(e.changedTouches, this.primaryTouchIdentifier); const primaryTouch = this._getTouch(e.changedTouches, this._primaryTouchIdentifier);
if (primaryTouch === null) { if (primaryTouch === null) {
return; return;
} }
@ -215,99 +225,99 @@ class TextScanner extends EventDispatcher {
throw new Error('Override me'); throw new Error('Override me');
} }
async scanTimerWait() { async _scanTimerWait() {
const delay = this.options.scanning.delay; const delay = this._options.scanning.delay;
const promise = promiseTimeout(delay, true); const promise = promiseTimeout(delay, true);
this.scanTimerPromise = promise; this._scanTimerPromise = promise;
try { try {
return await promise; return await promise;
} finally { } finally {
if (this.scanTimerPromise === promise) { if (this._scanTimerPromise === promise) {
this.scanTimerPromise = null; this._scanTimerPromise = null;
} }
} }
} }
scanTimerClear() { _scanTimerClear() {
if (this.scanTimerPromise !== null) { if (this._scanTimerPromise !== null) {
this.scanTimerPromise.resolve(false); this._scanTimerPromise.resolve(false);
this.scanTimerPromise = null; this._scanTimerPromise = null;
} }
} }
setEnabled(enabled) { setEnabled(enabled) {
this.eventListeners.removeAllEventListeners(); this._eventListeners.removeAllEventListeners();
this.enabled = enabled; this._enabled = enabled;
if (this.enabled) { if (this._enabled) {
this.hookEvents(); this._hookEvents();
} else { } else {
this.clearSelection(true); this.clearSelection(true);
} }
} }
hookEvents() { _hookEvents() {
const eventListenerInfos = this.getMouseEventListeners(); const eventListenerInfos = this._getMouseEventListeners();
if (this.options.scanning.touchInputEnabled) { if (this._options.scanning.touchInputEnabled) {
eventListenerInfos.push(...this.getTouchEventListeners()); eventListenerInfos.push(...this._getTouchEventListeners());
} }
for (const [node, type, listener, options] of eventListenerInfos) { for (const [node, type, listener, options] of eventListenerInfos) {
this.eventListeners.addEventListener(node, type, listener, options); this._eventListeners.addEventListener(node, type, listener, options);
} }
} }
getMouseEventListeners() { _getMouseEventListeners() {
return [ return [
[this.node, 'mousedown', this.onMouseDown.bind(this)], [this._node, 'mousedown', this._onMouseDown.bind(this)],
[this.node, 'mousemove', this.onMouseMove.bind(this)], [this._node, 'mousemove', this._onMouseMove.bind(this)],
[this.node, 'mouseover', this.onMouseOver.bind(this)], [this._node, 'mouseover', this._onMouseOver.bind(this)],
[this.node, 'mouseout', this.onMouseOut.bind(this)] [this._node, 'mouseout', this._onMouseOut.bind(this)]
]; ];
} }
getTouchEventListeners() { _getTouchEventListeners() {
return [ return [
[this.node, 'click', this.onClick.bind(this)], [this._node, 'click', this._onClick.bind(this)],
[this.node, 'auxclick', this.onAuxClick.bind(this)], [this._node, 'auxclick', this._onAuxClick.bind(this)],
[this.node, 'touchstart', this.onTouchStart.bind(this)], [this._node, 'touchstart', this._onTouchStart.bind(this)],
[this.node, 'touchend', this.onTouchEnd.bind(this)], [this._node, 'touchend', this._onTouchEnd.bind(this)],
[this.node, 'touchcancel', this.onTouchCancel.bind(this)], [this._node, 'touchcancel', this._onTouchCancel.bind(this)],
[this.node, 'touchmove', this.onTouchMove.bind(this), {passive: false}], [this._node, 'touchmove', this._onTouchMove.bind(this), {passive: false}],
[this.node, 'contextmenu', this.onContextMenu.bind(this)] [this._node, 'contextmenu', this._onContextMenu.bind(this)]
]; ];
} }
setOptions(options) { setOptions(options) {
this.options = options; this._options = options;
} }
async searchAt(x, y, cause) { async searchAt(x, y, cause) {
try { try {
this.scanTimerClear(); this._scanTimerClear();
if (this.pendingLookup) { if (this._pendingLookup) {
return; return;
} }
for (const ignorePointFn of this.ignorePoints) { for (const ignorePointFn of this._ignorePoints) {
if (await ignorePointFn(x, y)) { if (await ignorePointFn(x, y)) {
return; return;
} }
} }
const textSource = docRangeFromPoint(x, y, this.options.scanning.deepDomScan); const textSource = docRangeFromPoint(x, y, this._options.scanning.deepDomScan);
try { try {
if (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) { if (this._textSourceCurrent !== null && this._textSourceCurrent.equals(textSource)) {
return; return;
} }
this.pendingLookup = true; this._pendingLookup = true;
const result = await this.onSearchSource(textSource, cause); const result = await this.onSearchSource(textSource, cause);
if (result !== null) { if (result !== null) {
this.causeCurrent = cause; this._causeCurrent = cause;
this.setCurrentTextSource(textSource); this.setCurrentTextSource(textSource);
} }
this.pendingLookup = false; this._pendingLookup = false;
} finally { } finally {
if (textSource !== null) { if (textSource !== null) {
textSource.cleanup(); textSource.cleanup();
@ -323,11 +333,11 @@ class TextScanner extends EventDispatcher {
clonedTextSource.setEndOffset(length); clonedTextSource.setEndOffset(length);
if (this.ignoreNodes !== null && clonedTextSource.range) { if (this._ignoreNodes !== null && clonedTextSource.range) {
length = clonedTextSource.text().length; length = clonedTextSource.text().length;
while (clonedTextSource.range && length > 0) { while (clonedTextSource.range && length > 0) {
const nodes = TextSourceRange.getNodesInRange(clonedTextSource.range); const nodes = TextSourceRange.getNodesInRange(clonedTextSource.range);
if (!TextSourceRange.anyNodeMatchesSelector(nodes, this.ignoreNodes)) { if (!TextSourceRange.anyNodeMatchesSelector(nodes, this._ignoreNodes)) {
break; break;
} }
--length; --length;
@ -340,31 +350,31 @@ class TextScanner extends EventDispatcher {
clearSelection(passive) { clearSelection(passive) {
if (!this._canClearSelection) { return; } if (!this._canClearSelection) { return; }
if (this.textSourceCurrent !== null) { if (this._textSourceCurrent !== null) {
if (this.textSourceCurrentSelected) { if (this._textSourceCurrentSelected) {
this.textSourceCurrent.deselect(); this._textSourceCurrent.deselect();
} }
this.textSourceCurrent = null; this._textSourceCurrent = null;
this.textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
} }
this.trigger('clearSelection', {passive}); this.trigger('clearSelection', {passive});
} }
getCurrentTextSource() { getCurrentTextSource() {
return this.textSourceCurrent; return this._textSourceCurrent;
} }
setCurrentTextSource(textSource) { setCurrentTextSource(textSource) {
this.textSourceCurrent = textSource; this._textSourceCurrent = textSource;
if (this.options.scanning.selectText) { if (this._options.scanning.selectText) {
this.textSourceCurrent.select(); this._textSourceCurrent.select();
this.textSourceCurrentSelected = true; this._textSourceCurrentSelected = true;
} else { } else {
this.textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
} }
} }
static isScanningModifierPressed(scanningModifier, mouseEvent) { _isScanningModifierPressed(scanningModifier, mouseEvent) {
switch (scanningModifier) { switch (scanningModifier) {
case 'alt': return mouseEvent.altKey; case 'alt': return mouseEvent.altKey;
case 'ctrl': return mouseEvent.ctrlKey; case 'ctrl': return mouseEvent.ctrlKey;
@ -375,7 +385,7 @@ class TextScanner extends EventDispatcher {
} }
} }
static getTouch(touchList, identifier) { _getTouch(touchList, identifier) {
for (const touch of touchList) { for (const touch of touchList) {
if (touch.identifier === identifier) { if (touch.identifier === identifier) {
return touch; return touch;