Skip to content

Commit 5a2c042

Browse files
committed
fix: listen only when possible
1 parent 773b769 commit 5a2c042

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/control/modify.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ModifyControl extends Control {
2525
* @param {Object} [options.deleteInteractionOptions] Options for the delete interaction.
2626
* @param {Object} [options.deselectInteractionOptions] Options for the deselect interaction. Default: features are deselected on click on map.
2727
*/
28-
constructor(options) {
28+
constructor(options = {}) {
2929
super({
3030
title: 'Modify geometry',
3131
className: 'ole-control-modify',
@@ -376,14 +376,14 @@ class ModifyControl extends Control {
376376
}
377377
super.setMap(map);
378378
this.addListeners();
379-
this.map.addInteraction(this.deselectInteraction);
380-
this.map.addInteraction(this.deleteInteraction);
381-
this.map.addInteraction(this.selectModify);
379+
this.map?.addInteraction(this.deselectInteraction);
380+
this.map?.addInteraction(this.deleteInteraction);
381+
this.map?.addInteraction(this.selectModify);
382382
// For the default behvior it's very important to add selectMove after selectModify.
383383
// It will avoid single/dbleclick mess.
384-
this.map.addInteraction(this.selectMove);
385-
this.map.addInteraction(this.moveInteraction);
386-
this.map.addInteraction(this.modifyInteraction);
384+
this.map?.addInteraction(this.selectMove);
385+
this.map?.addInteraction(this.moveInteraction);
386+
this.map?.addInteraction(this.modifyInteraction);
387387
}
388388

389389
/**

src/editor.test.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { expect, test, describe, beforeEach } from 'vitest';
33
import Map from 'ol/Map';
44
import Editor from './editor';
55
import CAD from './control/cad';
6+
import ModifyControl from './control/modify';
67

78
describe('editor', () => {
89
let map;
910
let editor;
1011
let cad;
12+
let modify;
1113

1214
beforeEach(() => {
1315
// In the test we use pixel as coordinates.
@@ -16,6 +18,7 @@ describe('editor', () => {
1618
});
1719
editor = new Editor(map);
1820
cad = new CAD();
21+
modify = new ModifyControl();
1922
});
2023

2124
test('adds a control', () => {
@@ -46,14 +49,20 @@ describe('editor', () => {
4649
});
4750

4851
test('is removed', () => {
52+
editor.addControl(modify);
4953
editor.addControl(cad);
50-
cad.activate();
51-
expect(cad.getActive()).toBe(true);
52-
expect(editor.controls.getArray()[0]).toBe(cad);
53-
expect(editor.activeControls.getArray()[0]).toBe(cad);
54+
modify.activate();
55+
expect(modify.getActive()).toBe(true);
56+
expect(editor.controls.getArray()[0]).toBe(modify);
57+
expect(editor.controls.getArray()[1]).toBe(cad);
58+
expect(editor.activeControls.getArray()[0]).toBe(modify);
59+
expect(editor.activeControls.getArray()[0]).toBe(modify);
5460
editor.remove();
5561
expect(editor.controls.getLength()).toBe(0);
5662
expect(editor.activeControls.getLength()).toBe(0);
63+
expect(modify.map).toBe();
64+
expect(modify.editor).toBe();
65+
expect(modify.getActive()).toBe(false);
5766
expect(cad.map).toBe();
5867
expect(cad.editor).toBe();
5968
expect(cad.getActive()).toBe(false);

0 commit comments

Comments
 (0)