aboutsummaryrefslogtreecommitdiff
path: root/ext/js/dom
diff options
context:
space:
mode:
authorKuuuube <61125188+Kuuuube@users.noreply.github.com>2024-05-14 13:02:33 -0400
committerGitHub <noreply@github.com>2024-05-14 17:02:33 +0000
commit266c8c3a3f0d6f8c39832bfdc0cc88b88c2b710b (patch)
tree360198d216a15b828714a40435a6139b28c28d2b /ext/js/dom
parentba288277a5e73026f7b0c07d993aa00a5e66c332 (diff)
Make Scan selected text lazy and add Scan text at selection (#915)
* Add Scan selected text lazy * Remove extra spaces * Improve naming * Use disallowExpandSelection instead of "lazy" * Use "keyword arguments" for _scanSelectedText * Revert Use "keyword arguments" for _scanSelectedText
Diffstat (limited to 'ext/js/dom')
-rw-r--r--ext/js/dom/text-source-range.js24
1 files changed, 19 insertions, 5 deletions
diff --git a/ext/js/dom/text-source-range.js b/ext/js/dom/text-source-range.js
index 2012af7a..68ba208a 100644
--- a/ext/js/dom/text-source-range.js
+++ b/ext/js/dom/text-source-range.js
@@ -42,8 +42,9 @@ export class TextSourceRange {
* @param {?DOMRect} cachedSourceRect A cached `DOMRect` representing the rect of the `imposterSourceElement`,
* which can be used after the imposter element is removed from the page.
* Must not be `null` if imposterElement is specified.
+ * @param {boolean} disallowExpandSelection
*/
- constructor(range, rangeStartOffset, content, imposterElement, imposterSourceElement, cachedRects, cachedSourceRect) {
+ constructor(range, rangeStartOffset, content, imposterElement, imposterSourceElement, cachedRects, cachedSourceRect, disallowExpandSelection) {
/** @type {Range} */
this._range = range;
/** @type {number} */
@@ -58,6 +59,8 @@ export class TextSourceRange {
this._cachedRects = cachedRects;
/** @type {?DOMRect} */
this._cachedSourceRect = cachedSourceRect;
+ /** @type {boolean} */
+ this._disallowExpandSelection = disallowExpandSelection;
}
/**
@@ -104,7 +107,8 @@ export class TextSourceRange {
this._imposterElement,
this._imposterSourceElement,
this._cachedRects,
- this._cachedSourceRect
+ this._cachedSourceRect,
+ this._disallowExpandSelection
);
}
@@ -145,7 +149,8 @@ export class TextSourceRange {
}
const state = new DOMTextScanner(node, offset, !layoutAwareScan, layoutAwareScan).seek(length);
this._range.setEnd(state.node, state.offset);
- this._content = (fromEnd ? this._content + state.content : state.content);
+ const expandedContent = fromEnd ? this._content + state.content : state.content;
+ this._content = this._disallowExpandSelection ? expandedContent : this._content;
return length - state.remainder;
}
@@ -252,7 +257,16 @@ export class TextSourceRange {
* @returns {TextSourceRange} A new instance of the class corresponding to the range.
*/
static create(range) {
- return new TextSourceRange(range, range.startOffset, range.toString(), null, null, null, null);
+ return new TextSourceRange(range, range.startOffset, range.toString(), null, null, null, null, true);
+ }
+
+ /**
+ * Creates a new instance for a given range without expanding the search.
+ * @param {Range} range The source range.
+ * @returns {TextSourceRange} A new instance of the class corresponding to the range.
+ */
+ static createLazy(range) {
+ return new TextSourceRange(range, range.startOffset, range.toString(), null, null, null, null, false);
}
/**
@@ -265,7 +279,7 @@ export class TextSourceRange {
static createFromImposter(range, imposterElement, imposterSourceElement) {
const cachedRects = convertMultipleRectZoomCoordinates(range.getClientRects(), range.startContainer);
const cachedSourceRect = convertRectZoomCoordinates(imposterSourceElement.getBoundingClientRect(), imposterSourceElement);
- return new TextSourceRange(range, range.startOffset, range.toString(), imposterElement, imposterSourceElement, cachedRects, cachedSourceRect);
+ return new TextSourceRange(range, range.startOffset, range.toString(), imposterElement, imposterSourceElement, cachedRects, cachedSourceRect, true);
}
/**