Skip to content

Commit

Permalink
prevent the default scrolling event of spacebar when focus is on map (#…
Browse files Browse the repository at this point in the history
…724)

* prevent the default scrolling event of spacebar when focus is on map

* update keydown event listener and fix query handler bug

* update spacebar test

* update web-map.js and the tests for web-map with the changes

* small fix in web-map.js

Co-authored-by: AliyanH <aliyanulhaq@gmail.com>
  • Loading branch information
yhy0217 and AliyanH authored Jan 25, 2023
1 parent 6e54325 commit 249b22b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/mapml-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,17 @@ export class MapViewer extends HTMLElement {
}
});
// pasting layer-, links and geojson using Ctrl+V
this.parentElement.addEventListener('keydown', function (e) {
if(e.keyCode === 86 && e.ctrlKey && document.activeElement.nodeName === "MAPML-VIEWER"){
this.addEventListener('keydown', function (e) {
if(e.keyCode === 86 && e.ctrlKey){
navigator.clipboard
.readText()
.then(
(layer) => {
M._pasteLayer(document.activeElement, layer);
M._pasteLayer(this, layer);
});
} else if (e.keyCode === 32) {
e.preventDefault();
this._map.fire('keypress', {originalEvent: e});
}});
this.parentElement.addEventListener('mousedown', function (e) {
if(document.activeElement.nodeName === "MAPML-VIEWER"){
Expand Down
9 changes: 6 additions & 3 deletions src/web-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,17 @@ export class WebMap extends HTMLMapElement {
}
});
// pasting layer-, links and geojson using Ctrl+V
this.parentElement.addEventListener('keydown', function (e) {
if(e.keyCode === 86 && e.ctrlKey && document.activeElement.nodeName === "DIV"){
this.addEventListener('keydown', function (e) {
if(e.keyCode === 86 && e.ctrlKey){
navigator.clipboard
.readText()
.then(
(layer) => {
M._pasteLayer(document.activeElement.parentElement, layer);
M._pasteLayer(this, layer);
});
} else if (e.keyCode === 32) {
e.preventDefault();
this._map.fire('keypress', {originalEvent: e});
}
});
this.parentElement.addEventListener('mousedown', function (e) {
Expand Down
16 changes: 15 additions & 1 deletion test/e2e/mapml-viewer/mapml-viewer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,18 @@ test.describe("Playwright mapml-viewer Element Tests", () => {
);
expect(layerCount).toEqual(3);
});
});

test("Press spacebar when focus is on map", async () => {
// scroll to the top
await page.mouse.wheel(0,-1000);
await page.waitForTimeout(300);
await page.click("body > mapml-viewer");
await page.keyboard.press('Space');
await page.waitForTimeout(300);
const currPos = await page.$eval(
"body > mapml-viewer",
() => window.pageYOffset
);
expect(currPos).toEqual(0);
});
});
14 changes: 14 additions & 0 deletions test/e2e/web-map/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,18 @@ test.describe("Playwright web-map Element Tests", () => {
);
expect(layerCount).toEqual(3);
});

test("Press spacebar when focus is on map", async () => {
// scroll to the top
await page.mouse.wheel(0, -1000);
await page.waitForTimeout(300);
await page.click("body > map");
await page.keyboard.press('Space');
await page.waitForTimeout(300);
const currPos = await page.$eval(
"body > map",
() => window.pageYOffset
);
expect(currPos).toEqual(0);
});
});

0 comments on commit 249b22b

Please sign in to comment.