Skip to content

Commit 6335a8c

Browse files
authored
Fixes empty text handling with click on control (patch)
2 parents 81be934 + 1512d9f commit 6335a8c

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

cypress/e2e/text.cy.js

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ describe('Text Layer', () => {
114114
});
115115
});
116116

117+
it('place text layer and remove it with click on control', () => {
118+
cy.toolbarButton('text')
119+
.click()
120+
.closest('.button-container')
121+
.should('have.class', 'active');
122+
123+
cy.get(mapSelector).click(90, 250);
124+
125+
let textArea;
126+
cy.window().then(({ map }) => {
127+
expect(1).to.eq(map.pm.getGeomanDrawLayers().length);
128+
const textLayer = map.pm.getGeomanDrawLayers()[0];
129+
textArea = textLayer.pm.getElement();
130+
expect(textArea.value).to.eq('');
131+
});
132+
133+
cy.wait(500);
134+
cy.get(mapSelector).click(20, 20);
135+
cy.wait(500);
136+
137+
cy.window().then(({ map }) => {
138+
expect(0).to.eq(map.pm.getGeomanDrawLayers().length);
139+
});
140+
});
141+
117142
it('continue drawing', () => {
118143
cy.window().then(({ map }) => {
119144
map.pm.setGlobalOptions({ continueDrawing: true });
@@ -152,8 +177,7 @@ describe('Text Layer', () => {
152177
expect(textMap).to.eq(null);
153178
});
154179

155-
cy.get(mapSelector)
156-
.trigger('mousemove', 200, 150, { which: 1 });
180+
cy.get(mapSelector).trigger('mousemove', 200, 150, { which: 1 });
157181

158182
cy.window().then(({ map }) => {
159183
const textMap = map.pm.Draw.Text._hintMarker._map;
@@ -228,6 +252,44 @@ describe('Text Layer', () => {
228252
});
229253
});
230254

255+
it('allows to edit the Text multiple times', () => {
256+
cy.toolbarButton('text')
257+
.click()
258+
.closest('.button-container')
259+
.should('have.class', 'active');
260+
261+
cy.get(mapSelector).click(90, 250);
262+
263+
let textArea;
264+
cy.window().then(({ map }) => {
265+
expect(1).to.eq(map.pm.getGeomanDrawLayers().length);
266+
const textLayer = map.pm.getGeomanDrawLayers()[0];
267+
textArea = textLayer.pm.getElement();
268+
expect(textArea.style.width).to.eq('16px');
269+
cy.get(textArea).type('Hello World');
270+
});
271+
cy.get(mapSelector).click(100, 100);
272+
273+
cy.toolbarButton('edit').click();
274+
275+
cy.get(mapSelector).click(90, 250);
276+
cy.window().then(() => {
277+
expect(textArea.value).to.eq('Hello World');
278+
cy.get(textArea).type(' - Hello Test');
279+
});
280+
cy.get(mapSelector).click(100, 100);
281+
cy.get(mapSelector).click(90, 250);
282+
cy.window().then(() => {
283+
expect(textArea.value).to.eq('Hello World - Hello Test');
284+
cy.get(textArea).type(' - Bye');
285+
});
286+
cy.get(mapSelector).click(100, 100);
287+
cy.get(mapSelector).click(90, 250);
288+
cy.window().then(() => {
289+
expect(textArea.value).to.eq('Hello World - Hello Test - Bye');
290+
});
291+
});
292+
231293
describe('Options', () => {
232294
it('adds predefined `text`', () => {
233295
cy.window().then(({ map }) => {

src/js/Edit/L.PM.Edit.Text.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ Edit.Text = Edit.extend({
5656
L.DomEvent.off(this.textArea, 'input', this._autoResize, this);
5757
L.DomEvent.off(this.textArea, 'focus', this._focusChange, this);
5858
L.DomEvent.off(this.textArea, 'blur', this._focusChange, this);
59-
L.DomEvent.off(document, 'click', this._documentClick, this);
59+
document.removeEventListener('click', this._documentClickThis, {
60+
capture: true,
61+
});
6062

6163
this._focusChange();
6264
this.textArea.readOnly = true;
@@ -139,7 +141,11 @@ Edit.Text = Edit.extend({
139141
// we need this timeout because else the place click event is triggered here too.
140142
setTimeout(() => {
141143
if (this.enabled()) {
142-
L.DomEvent.on(document, 'click', this._documentClick, this);
144+
this._documentClickThis =
145+
this._documentClickThis || this._documentClick.bind(this);
146+
document.addEventListener('click', this._documentClickThis, {
147+
capture: true,
148+
});
143149
}
144150
}, 100);
145151
},

0 commit comments

Comments
 (0)