Skip to content

Commit

Permalink
Merge pull request ABI-Software#212 from ddjnw1yu/improve-invalid-dra…
Browse files Browse the repository at this point in the history
…w-handler

Improve UX when invalid drawing happens
  • Loading branch information
alan-wu authored Nov 21, 2024
2 parents ed578ee + f7b5382 commit e113106
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/components/FlatmapVuer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2931,6 +2931,44 @@ export default {
if (isUIDisabled) {
this.closeTooltip()
}
},
activeDrawTool: function (tool) {
let coordinates = [];
let lastClick = { x: null, y: null };
const canvas = this.$el.querySelector('.maplibregl-canvas');
const removeListeners = () => {
canvas.removeEventListener('keydown', handleKeyboardEvent);
canvas.removeEventListener('click', handleMouseEvent);
};
const handleKeyboardEvent = (event) => {
if (!['Escape', 'Enter'].includes(event.key)) return;
const isValidDraw =
(tool === 'Point' && coordinates.length === 1) ||
(tool === 'LineString' && coordinates.length >= 2) ||
(tool === 'Polygon' && coordinates.length >= 3);
if (event.key === 'Escape' || (event.key === 'Enter' && !isValidDraw)) {
this.activeDrawTool = undefined;
}
removeListeners();
};
const handleMouseEvent = (event) => {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const distance = Math.sqrt((x - lastClick.x) ** 2 + (y - lastClick.y) ** 2);
if (distance < 8) {
if (!this.isValidDrawnCreated) this.activeDrawTool = undefined;
removeListeners();
return;
}
lastClick = { x, y };
coordinates.push(lastClick);
};
if (tool) {
removeListeners();
canvas.addEventListener('keydown', handleKeyboardEvent);
canvas.addEventListener('click', handleMouseEvent);
}
}
},
mounted: function () {
Expand Down

0 comments on commit e113106

Please sign in to comment.