Skip to content

Commit 17527d8

Browse files
Merge pull request #20138 from calixteman/bug1976597
[Editor] Highlight text on a selectionchange event which hasn't been triggered by the pointer or the keyboard (bug 1976597)
2 parents e5922f2 + a81e991 commit 17527d8

File tree

2 files changed

+95
-14
lines changed

2 files changed

+95
-14
lines changed

src/display/editor/tools.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ class AnnotationEditorUIManager {
637637

638638
#isEnabled = false;
639639

640+
#isPointerDown = false;
641+
640642
#isWaiting = false;
641643

642644
#keyboardManagerAC = null;
@@ -857,6 +859,20 @@ class AnnotationEditorUIManager {
857859
evt => this.updateParams(evt.type, evt.value),
858860
{ signal }
859861
);
862+
window.addEventListener(
863+
"pointerdown",
864+
() => {
865+
this.#isPointerDown = true;
866+
},
867+
{ capture: true, signal }
868+
);
869+
window.addEventListener(
870+
"pointerup",
871+
() => {
872+
this.#isPointerDown = false;
873+
},
874+
{ capture: true, signal }
875+
);
860876
this.#addSelectionListener();
861877
this.#addDragAndDropListeners();
862878
this.#addKeyboardManager();
@@ -1299,22 +1315,30 @@ class AnnotationEditorUIManager {
12991315
: null;
13001316
activeLayer?.toggleDrawing();
13011317

1302-
const ac = new AbortController();
1303-
const signal = this.combinedSignal(ac);
1318+
if (this.#isPointerDown) {
1319+
const ac = new AbortController();
1320+
const signal = this.combinedSignal(ac);
13041321

1305-
const pointerup = e => {
1306-
if (e.type === "pointerup" && e.button !== 0) {
1307-
// Do nothing on right click.
1308-
return;
1309-
}
1310-
ac.abort();
1322+
const pointerup = e => {
1323+
if (e.type === "pointerup" && e.button !== 0) {
1324+
// Do nothing on right click.
1325+
return;
1326+
}
1327+
ac.abort();
1328+
activeLayer?.toggleDrawing(true);
1329+
if (e.type === "pointerup") {
1330+
this.#onSelectEnd("main_toolbar");
1331+
}
1332+
};
1333+
window.addEventListener("pointerup", pointerup, { signal });
1334+
window.addEventListener("blur", pointerup, { signal });
1335+
} else {
1336+
// Here neither the shift key nor the pointer is down and we've
1337+
// something in the selection: we can be in the case where the user is
1338+
// using a screen reader (see bug 1976597).
13111339
activeLayer?.toggleDrawing(true);
1312-
if (e.type === "pointerup") {
1313-
this.#onSelectEnd("main_toolbar");
1314-
}
1315-
};
1316-
window.addEventListener("pointerup", pointerup, { signal });
1317-
window.addEventListener("blur", pointerup, { signal });
1340+
this.#onSelectEnd("main_toolbar");
1341+
}
13181342
}
13191343
}
13201344

test/integration/highlight_editor_spec.mjs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,4 +2801,61 @@ describe("Highlight Editor", () => {
28012801
);
28022802
});
28032803
});
2804+
2805+
describe("Highlight the selection but without a mouse or a keyboard", () => {
2806+
let pages;
2807+
2808+
beforeEach(async () => {
2809+
pages = await loadAndWait(
2810+
"tracemonkey.pdf",
2811+
".annotationEditorLayer",
2812+
null,
2813+
null,
2814+
{ highlightEditorColors: "red=#AB0000" }
2815+
);
2816+
});
2817+
2818+
afterEach(async () => {
2819+
await closePages(pages);
2820+
});
2821+
2822+
it("must highlight with red color", async () => {
2823+
await Promise.all(
2824+
pages.map(async ([browserName, page]) => {
2825+
await switchToHighlight(page);
2826+
2827+
await page.evaluate(() => {
2828+
// Take the first span which contains "Trace-based Just-in-Time..."
2829+
const root = document.querySelector(
2830+
".page[data-page-number='1'] > .textLayer"
2831+
);
2832+
const iter = document.createNodeIterator(
2833+
root,
2834+
NodeFilter.SHOW_TEXT
2835+
);
2836+
const textnode = iter.nextNode();
2837+
const selection = document.getSelection();
2838+
const range = document.createRange();
2839+
range.selectNodeContents(textnode);
2840+
selection.removeAllRanges();
2841+
selection.addRange(range);
2842+
});
2843+
2844+
await page.waitForSelector(`${getEditorSelector(0)}`);
2845+
await page.waitForSelector(
2846+
`.page[data-page-number = "1"] svg.highlightOutline.selected`
2847+
);
2848+
2849+
const usedColor = await page.evaluate(() => {
2850+
const highlight = document.querySelector(
2851+
`.page[data-page-number = "1"] .canvasWrapper > svg.highlight`
2852+
);
2853+
return highlight.getAttribute("fill");
2854+
});
2855+
2856+
expect(usedColor).withContext(`In ${browserName}`).toEqual("#AB0000");
2857+
})
2858+
);
2859+
});
2860+
});
28042861
});

0 commit comments

Comments
 (0)