Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 2280853

Browse files
authored
fix: DEV-2480: convenient text region offsets (#646)
When `start` is on the last char or `end` is on the first char of text node, find better to fit to have first char for `start` or last char for `end`.
1 parent 35503f3 commit 2280853

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/utils/feature-flags.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export const FF_DEV_2186 = "ff_front_dev_2186_comments_for_update";
5959

6060
export const FF_DEV_2458 = "ff_front_dev_2458_comments_for_skip_250522_short";
6161

62+
export const FF_DEV_2480 = "ff_dev_2480_convenient_offsets_270522_short";
63+
6264
function getFeatureFlags() {
6365
return window.APP_SETTINGS?.feature_flags || {
6466
// ff_front_DEV_1713_audio_ui_150222_short: true,

src/utils/selection-tools.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FF_DEV_2480, isFF } from "./feature-flags";
12
import { clamp, isDefined } from "./utilities";
23

34
const isTextNode = node => node && node.nodeType === Node.TEXT_NODE;
@@ -275,11 +276,13 @@ const applyTextGranularity = (selection, granularity) => {
275276
* @param {HTMLElement} commonContainer
276277
* @param {HTMLElement} node
277278
* @param {number} offset
279+
* @param {string} direction forward, backward, forward-next, backward-next
280+
* "-next" when we need to skip node if it's a text node
278281
*/
279-
const textNodeLookup = (commonContainer, node, offset, direction) => {
282+
const textNodeLookup = (commonContainer, node, offset, direction = "forward") => {
280283
const startNode = node === commonContainer ? node.childNodes[offset] : node;
281284

282-
if (isTextNode(startNode)) return startNode;
285+
if (isTextNode(startNode) && !direction.endsWith("next")) return startNode;
283286

284287
const walker = commonContainer.ownerDocument.createTreeWalker(commonContainer, NodeFilter.SHOW_ALL);
285288
let currentNode = walker.nextNode();
@@ -290,7 +293,9 @@ const textNodeLookup = (commonContainer, node, offset, direction) => {
290293
currentNode = walker.nextNode();
291294
}
292295

293-
if (currentNode && direction === "backward") return lastTextNode;
296+
if (currentNode && direction.startsWith("backward")) return lastTextNode;
297+
298+
if (direction === "forward-next") currentNode = walker.nextNode();
294299

295300
while (currentNode) {
296301
if (isTextNode(currentNode)) return currentNode;
@@ -312,10 +317,27 @@ const fixRange = range => {
312317
range.setStart(startContainer, 0);
313318
}
314319

320+
if (isFF(FF_DEV_2480) && startContainer.wholeText.length === startOffset) {
321+
do {
322+
startContainer = textNodeLookup(commonContainer, startContainer, startOffset, "forward-next");
323+
} while (/^\s*$/.test(startContainer.wholeText));
324+
range.setStart(startContainer, 0);
325+
}
326+
315327
if (!isTextNode(endContainer)) {
328+
let isIncluded = false;
329+
316330
endContainer = textNodeLookup(commonContainer, endContainer, endOffset, "backward");
317331
if (!endContainer) return null;
318-
const isIncluded = !!range.toString().match(endContainer.wholeText)?.length;
332+
333+
if (isFF(FF_DEV_2480)) {
334+
while (/^\s*$/.test(endContainer.wholeText)) {
335+
endContainer = textNodeLookup(commonContainer, endContainer, endOffset, "backward-next");
336+
}
337+
isIncluded = range.toString().trimEnd().endsWith(endContainer.wholeText.trimEnd());
338+
} else {
339+
isIncluded = range.toString().includes(endContainer.wholeText);
340+
}
319341

320342
range.setEnd(endContainer, isIncluded ? endContainer.length : 0);
321343
}

0 commit comments

Comments
 (0)