Skip to content

Commit

Permalink
Merge pull request #172 from Geoportail-Luxembourg/GSLUX-765-fix-drawing
Browse files Browse the repository at this point in the history
GSLUX-765: FIX drawing
  • Loading branch information
AlitaBernachot authored Nov 19, 2024
2 parents 921e184 + 1cd311e commit c8a2aea
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 21 deletions.
12 changes: 12 additions & 0 deletions cypress/e2e/draw/draw-feat-line.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ describe('Draw "Line"', () => {
.should('contain.text', 'Changer le sens de la ligne')
})
})

describe('When using Escape key when drawing a line', () => {
it('ends the drawing and adds a new line', () => {
cy.get('*[data-cy="featItemName"]').should('have.length', 1)
cy.get('button[data-cy="drawLineButton"]').click()
cy.get('button[data-cy="drawLineButton"]').click()
cy.get('div.ol-viewport').click(250, 250, { force: true })
cy.get('div.ol-viewport').click(300, 300, { force: true })
cy.get('body').type('{esc}')
cy.get('*[data-cy="featItemName"]').should('have.length', 2)
})
})
})
12 changes: 2 additions & 10 deletions cypress/e2e/layers-selection/layers-selection-catalog.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ describe('Catalogue', () => {
const layers = (<AUTWindowOlMap>window).olMap
.getLayers()
.getArray()
.filter(
(l: any) =>
// regex match
!/feature(Edit)?Layer/.exec(l.get('cyLayerType'))
)
.filter((l: any) => !/feature(Edit)?Layer/.exec(l.get('cyLayerType')))
expect(layers[0].get('id')).to.eq(556)
})
cy.get('[data-cy="catalog"]')
Expand All @@ -85,11 +81,7 @@ describe('Catalogue', () => {
const layers = (<AUTWindowOlMap>window).olMap
.getLayers()
.getArray()
.filter(
(l: any) =>
// regex match
!/feature(Edit)?Layer/.exec(l.get('cyLayerType'))
)
.filter((l: any) => !/feature(Edit)?Layer/.exec(l.get('cyLayerType')))
expect(layers[0].get('id')).to.eq(359)
expect(layers[1].get('id')).to.eq(353)
})
Expand Down
9 changes: 3 additions & 6 deletions src/assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,18 @@
}

.lux-btn-grey {
@apply lux-btn py-[3px] px-[8px];
@apply lux-btn py-[2px] px-[9px] border-[1px] border-gray-500 border-solid rounded-[2.5px] focus:text-gray-800 focus:border-gray-500;
background-color: rgb(239, 239, 239);
border-style: outset;
border-color: rgb(51, 51, 51);
color: rgb(51, 51, 51);
}

.lux-btn-grey:hover {
background-color: rgb(233, 233, 233);
@apply bg-gray-300;
color: rgb(51, 51, 51);
}

.lux-btn-grey.pressed {
background-color: rgb(133, 168, 233);
color: rgb(51, 51, 181);
@apply bg-gray-400;
}

.lux-close-cross {
Expand Down
5 changes: 3 additions & 2 deletions src/components/draw/feature-edit-linestyle-item.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { useTranslation } from 'i18next-vue'
import { DrawnFeature } from '@/services/draw/drawn-feature'
import { capitalize } from '@/services/utils'
const { t } = useTranslation()
Expand All @@ -17,11 +18,11 @@ function onClickChangeLineStyle(linestyle: string) {

<template>
<button
:class="`lux-btn-grey rounded${
:class="`lux-btn-grey${
props.feature.featureStyle.linestyle === props.linestyle ? ' pressed' : ''
}`"
@click="onClickChangeLineStyle(props.linestyle)"
>
{{ t(props.linestyle) }}
{{ t(capitalize(props.linestyle)) }}
</button>
</template>
27 changes: 25 additions & 2 deletions src/components/draw/feature-sub-wrapper.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
<script setup lang="ts">
import { inject } from 'vue'
import { inject, ref, watchEffect } from 'vue'
import MapPopup from '@/components/map/map-popup.vue'
import useDrawnFeatures from '@/composables/draw/drawn-features.composable'
import { DrawnFeature } from '@/services/draw/drawn-feature'
import { debounce } from '@/services/utils'
defineProps<{
isDocked: boolean
}>()
const { getFeatCoordinates } = useDrawnFeatures()
const emit = defineEmits(['closePopup'])
const feature: DrawnFeature | undefined = inject('feature')
const popupAnchor = feature ? getFeatCoordinates(feature) : undefined
const popupAnchor: any = ref(getUnreactiveCoords())
function onClosePopup() {
emit('closePopup')
}
/**
* Update the anchor coords when user modfies the geom,
* but with a debounce, preventing the modale to moves at each update of the geom
*/
const onChangeGeom = debounce(() => {
popupAnchor.value = getUnreactiveCoords()
}, 1000)
/**
* Get an unreactive value for coords, otherwise, anchor is updated
* each time the geom is modified by the user
*/
function getUnreactiveCoords() {
return JSON.parse(JSON.stringify(getFeatCoordinates(feature!)))
}
watchEffect(() => {
if (feature?.getGeometry() && getFeatCoordinates(feature!)) {
onChangeGeom()
}
})
</script>

<template>
Expand Down
18 changes: 17 additions & 1 deletion src/composables/draw/draw-interaction.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import useDrawnFeatures from './drawn-features.composable'
import drawTooltip from './draw-tooltip'

export default function useDrawInteraction(options: Options) {
const { addFeature } = useDrawnFeatures()
const drawInteraction = new Draw(options)
drawInteraction.setActive(false)
const map = useMap().getOlMap()
Expand All @@ -16,13 +17,28 @@ export default function useDrawInteraction(options: Options) {
listen(drawInteraction, 'drawend', event => {
onDrawEnd(event as DrawEvent)
})
const { addFeature } = useDrawnFeatures()
listen(document, 'keyup', event => {
onKeyUp(event as KeyboardEvent)
})

function onDrawEnd(event: DrawEvent) {
drawTooltip.remove()
addFeature(event.feature)
event.stopPropagation()
}

/**
* Deactivate this interaction if the ESC key is pressed.
* @param event
*/
function onKeyUp(event: KeyboardEvent) {
if (event.key === 'Escape') {
drawTooltip.remove()
drawInteraction.finishDrawing()
drawInteraction.setActive(false)
}
}

return {
drawInteraction,
}
Expand Down
9 changes: 9 additions & 0 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,12 @@ export function downloadFile(
document.body.removeChild(a)
URL.revokeObjectURL(url)
}

/**
* Capitalized (only) the first letter of the given string
* @param str The string to capitalize
* @returns The capitalized string
*/
export function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}

0 comments on commit c8a2aea

Please sign in to comment.