@trackunit/react-map-draw 0.0.39 → 0.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +95 -0
- package/index.esm.js +94 -1
- package/package.json +3 -3
- package/src/index.d.ts +1 -0
- package/src/pointerCapture.d.ts +33 -0
package/index.cjs.js
CHANGED
|
@@ -332,10 +332,105 @@ const loadTerraDrawEngine = async (options) => {
|
|
|
332
332
|
return createTerraDrawEngine(options);
|
|
333
333
|
};
|
|
334
334
|
|
|
335
|
+
/** How far inside the map edge a replayed release lands, so it is inside the map's bounds. */
|
|
336
|
+
const EDGE_INSET_PX = 2;
|
|
337
|
+
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
338
|
+
const createPointerCapture = ({ resolveElement, resolveBounds }) => {
|
|
339
|
+
let element = null;
|
|
340
|
+
const onPointerDown = (event) => {
|
|
341
|
+
if (element === null || !event.isPrimary || event.button !== 0)
|
|
342
|
+
return;
|
|
343
|
+
try {
|
|
344
|
+
element.setPointerCapture(event.pointerId);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
// The pointer is already gone (a touch that ended before the handler ran); nothing to capture.
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
const replayInsideMap = (event, target) => {
|
|
351
|
+
const bounds = resolveBounds();
|
|
352
|
+
const left = bounds.left + EDGE_INSET_PX;
|
|
353
|
+
const right = bounds.right - EDGE_INSET_PX;
|
|
354
|
+
const top = bounds.top + EDGE_INSET_PX;
|
|
355
|
+
const bottom = bounds.bottom - EDGE_INSET_PX;
|
|
356
|
+
const inside = event.clientX >= left && event.clientX <= right && event.clientY >= top && event.clientY <= bottom;
|
|
357
|
+
if (inside || typeof PointerEvent !== "function")
|
|
358
|
+
return;
|
|
359
|
+
target.dispatchEvent(new PointerEvent("pointerup", {
|
|
360
|
+
bubbles: true,
|
|
361
|
+
cancelable: true,
|
|
362
|
+
pointerId: event.pointerId,
|
|
363
|
+
pointerType: event.pointerType,
|
|
364
|
+
isPrimary: event.isPrimary,
|
|
365
|
+
button: event.button,
|
|
366
|
+
buttons: 0,
|
|
367
|
+
clientX: clamp(event.clientX, left, right),
|
|
368
|
+
clientY: clamp(event.clientY, top, bottom),
|
|
369
|
+
}));
|
|
370
|
+
};
|
|
371
|
+
const onPointerEnd = (event) => {
|
|
372
|
+
if (element === null || !element.hasPointerCapture(event.pointerId))
|
|
373
|
+
return;
|
|
374
|
+
if (event.type === "pointerup")
|
|
375
|
+
replayInsideMap(event, element);
|
|
376
|
+
try {
|
|
377
|
+
element.releasePointerCapture(event.pointerId);
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
// Already released by the browser.
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
const detach = () => {
|
|
384
|
+
if (element === null)
|
|
385
|
+
return;
|
|
386
|
+
element.removeEventListener("pointerdown", onPointerDown);
|
|
387
|
+
element.removeEventListener("pointerup", onPointerEnd);
|
|
388
|
+
element.removeEventListener("pointercancel", onPointerEnd);
|
|
389
|
+
element = null;
|
|
390
|
+
};
|
|
391
|
+
return {
|
|
392
|
+
setEnabled: enabled => {
|
|
393
|
+
if (!enabled) {
|
|
394
|
+
detach();
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (element !== null)
|
|
398
|
+
return;
|
|
399
|
+
const next = resolveElement();
|
|
400
|
+
if (next === null)
|
|
401
|
+
return;
|
|
402
|
+
element = next;
|
|
403
|
+
element.addEventListener("pointerdown", onPointerDown);
|
|
404
|
+
element.addEventListener("pointerup", onPointerEnd);
|
|
405
|
+
element.addEventListener("pointercancel", onPointerEnd);
|
|
406
|
+
},
|
|
407
|
+
};
|
|
408
|
+
};
|
|
409
|
+
/**
|
|
410
|
+
* Wraps an engine so the capture runs only while a drawing mode is active and is
|
|
411
|
+
* released together with the engine.
|
|
412
|
+
*/
|
|
413
|
+
const withPointerCapture = (engine, options) => {
|
|
414
|
+
const capture = createPointerCapture(options);
|
|
415
|
+
return {
|
|
416
|
+
...engine,
|
|
417
|
+
setMode: mode => {
|
|
418
|
+
engine.setMode(mode);
|
|
419
|
+
capture.setEnabled(mode !== "idle");
|
|
420
|
+
},
|
|
421
|
+
destroy: () => {
|
|
422
|
+
capture.setEnabled(false);
|
|
423
|
+
engine.destroy();
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
|
|
335
428
|
exports.DEFAULT_DRAW_STYLE = DEFAULT_DRAW_STYLE;
|
|
336
429
|
exports.DRAW_COORDINATE_PRECISION = DRAW_COORDINATE_PRECISION;
|
|
337
430
|
exports.DrawController = DrawController;
|
|
431
|
+
exports.createPointerCapture = createPointerCapture;
|
|
338
432
|
exports.drawValuesEqual = drawValuesEqual;
|
|
339
433
|
exports.isDrawablePolygonFeature = isDrawablePolygonFeature;
|
|
340
434
|
exports.loadTerraDrawEngine = loadTerraDrawEngine;
|
|
341
435
|
exports.normalizeDrawValue = normalizeDrawValue;
|
|
436
|
+
exports.withPointerCapture = withPointerCapture;
|
package/index.esm.js
CHANGED
|
@@ -330,4 +330,97 @@ const loadTerraDrawEngine = async (options) => {
|
|
|
330
330
|
return createTerraDrawEngine(options);
|
|
331
331
|
};
|
|
332
332
|
|
|
333
|
-
|
|
333
|
+
/** How far inside the map edge a replayed release lands, so it is inside the map's bounds. */
|
|
334
|
+
const EDGE_INSET_PX = 2;
|
|
335
|
+
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
336
|
+
const createPointerCapture = ({ resolveElement, resolveBounds }) => {
|
|
337
|
+
let element = null;
|
|
338
|
+
const onPointerDown = (event) => {
|
|
339
|
+
if (element === null || !event.isPrimary || event.button !== 0)
|
|
340
|
+
return;
|
|
341
|
+
try {
|
|
342
|
+
element.setPointerCapture(event.pointerId);
|
|
343
|
+
}
|
|
344
|
+
catch {
|
|
345
|
+
// The pointer is already gone (a touch that ended before the handler ran); nothing to capture.
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
const replayInsideMap = (event, target) => {
|
|
349
|
+
const bounds = resolveBounds();
|
|
350
|
+
const left = bounds.left + EDGE_INSET_PX;
|
|
351
|
+
const right = bounds.right - EDGE_INSET_PX;
|
|
352
|
+
const top = bounds.top + EDGE_INSET_PX;
|
|
353
|
+
const bottom = bounds.bottom - EDGE_INSET_PX;
|
|
354
|
+
const inside = event.clientX >= left && event.clientX <= right && event.clientY >= top && event.clientY <= bottom;
|
|
355
|
+
if (inside || typeof PointerEvent !== "function")
|
|
356
|
+
return;
|
|
357
|
+
target.dispatchEvent(new PointerEvent("pointerup", {
|
|
358
|
+
bubbles: true,
|
|
359
|
+
cancelable: true,
|
|
360
|
+
pointerId: event.pointerId,
|
|
361
|
+
pointerType: event.pointerType,
|
|
362
|
+
isPrimary: event.isPrimary,
|
|
363
|
+
button: event.button,
|
|
364
|
+
buttons: 0,
|
|
365
|
+
clientX: clamp(event.clientX, left, right),
|
|
366
|
+
clientY: clamp(event.clientY, top, bottom),
|
|
367
|
+
}));
|
|
368
|
+
};
|
|
369
|
+
const onPointerEnd = (event) => {
|
|
370
|
+
if (element === null || !element.hasPointerCapture(event.pointerId))
|
|
371
|
+
return;
|
|
372
|
+
if (event.type === "pointerup")
|
|
373
|
+
replayInsideMap(event, element);
|
|
374
|
+
try {
|
|
375
|
+
element.releasePointerCapture(event.pointerId);
|
|
376
|
+
}
|
|
377
|
+
catch {
|
|
378
|
+
// Already released by the browser.
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
const detach = () => {
|
|
382
|
+
if (element === null)
|
|
383
|
+
return;
|
|
384
|
+
element.removeEventListener("pointerdown", onPointerDown);
|
|
385
|
+
element.removeEventListener("pointerup", onPointerEnd);
|
|
386
|
+
element.removeEventListener("pointercancel", onPointerEnd);
|
|
387
|
+
element = null;
|
|
388
|
+
};
|
|
389
|
+
return {
|
|
390
|
+
setEnabled: enabled => {
|
|
391
|
+
if (!enabled) {
|
|
392
|
+
detach();
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (element !== null)
|
|
396
|
+
return;
|
|
397
|
+
const next = resolveElement();
|
|
398
|
+
if (next === null)
|
|
399
|
+
return;
|
|
400
|
+
element = next;
|
|
401
|
+
element.addEventListener("pointerdown", onPointerDown);
|
|
402
|
+
element.addEventListener("pointerup", onPointerEnd);
|
|
403
|
+
element.addEventListener("pointercancel", onPointerEnd);
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
/**
|
|
408
|
+
* Wraps an engine so the capture runs only while a drawing mode is active and is
|
|
409
|
+
* released together with the engine.
|
|
410
|
+
*/
|
|
411
|
+
const withPointerCapture = (engine, options) => {
|
|
412
|
+
const capture = createPointerCapture(options);
|
|
413
|
+
return {
|
|
414
|
+
...engine,
|
|
415
|
+
setMode: mode => {
|
|
416
|
+
engine.setMode(mode);
|
|
417
|
+
capture.setEnabled(mode !== "idle");
|
|
418
|
+
},
|
|
419
|
+
destroy: () => {
|
|
420
|
+
capture.setEnabled(false);
|
|
421
|
+
engine.destroy();
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
export { DEFAULT_DRAW_STYLE, DRAW_COORDINATE_PRECISION, DrawController, createPointerCapture, drawValuesEqual, isDrawablePolygonFeature, loadTerraDrawEngine, normalizeDrawValue, withPointerCapture };
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-map-draw",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.41",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=24.x"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@trackunit/react-map-adapter-shared": "0.0.
|
|
11
|
-
"@trackunit/geo-json-utils": "1.21.
|
|
10
|
+
"@trackunit/react-map-adapter-shared": "0.0.203",
|
|
11
|
+
"@trackunit/geo-json-utils": "1.21.36",
|
|
12
12
|
"es-toolkit": "^1.52.0",
|
|
13
13
|
"terra-draw": "^1.36.0"
|
|
14
14
|
},
|
package/src/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export type { DrawEngine, DrawEngineLoader } from "./drawEngine";
|
|
|
3
3
|
export { DEFAULT_DRAW_STYLE, DRAW_COORDINATE_PRECISION, type DrawStyle } from "./drawStyle";
|
|
4
4
|
export { drawValuesEqual, isDrawablePolygonFeature, normalizeDrawValue } from "./drawValue";
|
|
5
5
|
export { loadTerraDrawEngine, type TerraDrawEngineOptions, type TerraDrawProviderAdapter } from "./loadTerraDrawEngine";
|
|
6
|
+
export { createPointerCapture, withPointerCapture, type PointerCapture, type PointerCaptureOptions, } from "./pointerCapture";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { DrawEngine } from "./drawEngine";
|
|
2
|
+
/**
|
|
3
|
+
* Keeps a drawing gesture on the map element from press to release (ADR 0030).
|
|
4
|
+
*
|
|
5
|
+
* Terra Draw ends a drag, and counts a click, only when the pointerup target is the map
|
|
6
|
+
* element it listens on and the release position is inside the map. Without capture the
|
|
7
|
+
* release lands on whatever is under the cursor at that moment: a control button, the page
|
|
8
|
+
* outside the map, a marker that is not ours to make inert. Capturing the pointer on
|
|
9
|
+
* pointerdown retargets the release to the map element, and a release outside the map is
|
|
10
|
+
* replayed at the nearest point inside it, so a corner dragged past the edge stops at the
|
|
11
|
+
* edge instead of following the cursor forever. This runs only while a drawing mode is
|
|
12
|
+
* active, so clicks on markers keep working in idle; Terra Draw's own
|
|
13
|
+
* `forwardMapElementEvents` captures unconditionally and would break them. Both
|
|
14
|
+
* provider adapters listen on one element (Google's event layer, Mapbox's canvas) and
|
|
15
|
+
* share the same release rule, so both loaders wrap their engine with
|
|
16
|
+
* `withPointerCapture`.
|
|
17
|
+
*/
|
|
18
|
+
export type PointerCapture = Readonly<{
|
|
19
|
+
/** Attaches the capture listeners to the resolved element, or removes them. */
|
|
20
|
+
setEnabled: (enabled: boolean) => void;
|
|
21
|
+
}>;
|
|
22
|
+
export type PointerCaptureOptions = Readonly<{
|
|
23
|
+
/** The element Terra Draw listens on; `null` when it cannot be found. */
|
|
24
|
+
resolveElement: () => HTMLElement | null;
|
|
25
|
+
/** The map's box in viewport coordinates; a release outside it is replayed on its edge. */
|
|
26
|
+
resolveBounds: () => DOMRect;
|
|
27
|
+
}>;
|
|
28
|
+
export declare const createPointerCapture: ({ resolveElement, resolveBounds }: PointerCaptureOptions) => PointerCapture;
|
|
29
|
+
/**
|
|
30
|
+
* Wraps an engine so the capture runs only while a drawing mode is active and is
|
|
31
|
+
* released together with the engine.
|
|
32
|
+
*/
|
|
33
|
+
export declare const withPointerCapture: (engine: DrawEngine, options: PointerCaptureOptions) => DrawEngine;
|