From 7925e8935c90644dc27d2dfab8bc79498520b9a1 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 24 Sep 2022 15:53:19 -0400 Subject: [PATCH 1/2] Update DocumentUtil.getRangeFromPoint to be extensible --- ext/js/dom/document-util.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ext/js/dom/document-util.js b/ext/js/dom/document-util.js index 53297084e7..fb38c7c359 100644 --- a/ext/js/dom/document-util.js +++ b/ext/js/dom/document-util.js @@ -22,7 +22,14 @@ */ class DocumentUtil { - static getRangeFromPoint(x, y, {deepContentScan, normalizeCssZoom}) { + static getRangeFromPoint(x, y, options) { + for (const handler of this._getRangeFromPointHandlers) { + const r = handler(x, y, options); + if (r !== null) { return r; } + } + + const {deepContentScan, normalizeCssZoom} = options; + const elements = this._getElementsFromPoint(x, y, deepContentScan); let imposter = null; let imposterContainer = null; @@ -62,6 +69,10 @@ class DocumentUtil { } } + static registerGetRangeFromPointHandler(handler) { + this._getRangeFromPointHandlers.push(handler); + } + /** * Extract a sentence from a document. * @param {TextSourceRange|TextSourceElement} source The text source object, either `TextSourceRange` or `TextSourceElement`. @@ -724,3 +735,5 @@ class DocumentUtil { DocumentUtil._transparentColorPattern = /rgba\s*\([^)]*,\s*0(?:\.0+)?\s*\)/; // eslint-disable-next-line no-underscore-dangle DocumentUtil._cssZoomSupported = null; +// eslint-disable-next-line no-underscore-dangle +DocumentUtil._getRangeFromPointHandlers = []; From 362a22662d13eb180b92a7f89109b1395ac56996 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 24 Sep 2022 17:14:14 -0400 Subject: [PATCH 2/2] Add documentation --- ext/js/dom/document-util.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ext/js/dom/document-util.js b/ext/js/dom/document-util.js index fb38c7c359..76f551c754 100644 --- a/ext/js/dom/document-util.js +++ b/ext/js/dom/document-util.js @@ -22,6 +22,32 @@ */ class DocumentUtil { + /** + * Options to configure how element detection is performed. + * @typedef {object} GetRangeFromPointOptions + * @property {boolean} deepContentScan Whether or deep content scanning should be performed. When deep content scanning is enabled, + * some transparent overlay elements will be ignored when looking for the element at the input position. + * @property {boolean} normalizeCssZoom Whether or not zoom coordinates should be normalized. + */ + + /** + * Scans the document for text or elements with text information at the given coordinate. + * Coordinates are provided in [client space](https://developer.mozilla.org/en-US/docs/Web/CSS/CSSOM_View/Coordinate_systems). + * @callback GetRangeFromPointHandler + * @param {number} x The x coordinate to search at. + * @param {number} y The y coordinate to search at. + * @param {GetRangeFromPointOptions} options Options to configure how element detection is performed. + * @returns {?TextSourceRange|TextSourceElement} A range for the hovered text or element, or `null` if no applicable content was found. + */ + + /** + * Scans the document for text or elements with text information at the given coordinate. + * Coordinates are provided in [client space](https://developer.mozilla.org/en-US/docs/Web/CSS/CSSOM_View/Coordinate_systems). + * @param {number} x The x coordinate to search at. + * @param {number} y The y coordinate to search at. + * @param {GetRangeFromPointOptions} options Options to configure how element detection is performed. + * @returns {?TextSourceRange|TextSourceElement} A range for the hovered text or element, or `null` if no applicable content was found. + */ static getRangeFromPoint(x, y, options) { for (const handler of this._getRangeFromPointHandlers) { const r = handler(x, y, options); @@ -69,6 +95,10 @@ class DocumentUtil { } } + /** + * Registers a custom handler for scanning for text or elements at the input position. + * @param {GetRangeFromPointHandler} handler The handler callback which will be invoked when calling `getRangeFromPoint`. + */ static registerGetRangeFromPointHandler(handler) { this._getRangeFromPointHandlers.push(handler); }