Skip to content

Commit

Permalink
fix: listen only when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
oterral committed May 14, 2024
1 parent 773b769 commit 5a2c042
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/control/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ModifyControl extends Control {
* @param {Object} [options.deleteInteractionOptions] Options for the delete interaction.
* @param {Object} [options.deselectInteractionOptions] Options for the deselect interaction. Default: features are deselected on click on map.
*/
constructor(options) {
constructor(options = {}) {
super({
title: 'Modify geometry',
className: 'ole-control-modify',
Expand Down Expand Up @@ -376,14 +376,14 @@ class ModifyControl extends Control {
}
super.setMap(map);
this.addListeners();
this.map.addInteraction(this.deselectInteraction);
this.map.addInteraction(this.deleteInteraction);
this.map.addInteraction(this.selectModify);
this.map?.addInteraction(this.deselectInteraction);
this.map?.addInteraction(this.deleteInteraction);
this.map?.addInteraction(this.selectModify);
// For the default behvior it's very important to add selectMove after selectModify.
// It will avoid single/dbleclick mess.
this.map.addInteraction(this.selectMove);
this.map.addInteraction(this.moveInteraction);
this.map.addInteraction(this.modifyInteraction);
this.map?.addInteraction(this.selectMove);
this.map?.addInteraction(this.moveInteraction);
this.map?.addInteraction(this.modifyInteraction);
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { expect, test, describe, beforeEach } from 'vitest';
import Map from 'ol/Map';
import Editor from './editor';
import CAD from './control/cad';
import ModifyControl from './control/modify';

describe('editor', () => {
let map;
let editor;
let cad;
let modify;

beforeEach(() => {
// In the test we use pixel as coordinates.
Expand All @@ -16,6 +18,7 @@ describe('editor', () => {
});
editor = new Editor(map);
cad = new CAD();
modify = new ModifyControl();
});

test('adds a control', () => {
Expand Down Expand Up @@ -46,14 +49,20 @@ describe('editor', () => {
});

test('is removed', () => {
editor.addControl(modify);
editor.addControl(cad);
cad.activate();
expect(cad.getActive()).toBe(true);
expect(editor.controls.getArray()[0]).toBe(cad);
expect(editor.activeControls.getArray()[0]).toBe(cad);
modify.activate();
expect(modify.getActive()).toBe(true);
expect(editor.controls.getArray()[0]).toBe(modify);
expect(editor.controls.getArray()[1]).toBe(cad);
expect(editor.activeControls.getArray()[0]).toBe(modify);
expect(editor.activeControls.getArray()[0]).toBe(modify);
editor.remove();
expect(editor.controls.getLength()).toBe(0);
expect(editor.activeControls.getLength()).toBe(0);
expect(modify.map).toBe();
expect(modify.editor).toBe();
expect(modify.getActive()).toBe(false);
expect(cad.map).toBe();
expect(cad.editor).toBe();
expect(cad.getActive()).toBe(false);
Expand Down

0 comments on commit 5a2c042

Please sign in to comment.