Two augmentations, so TypeScript actually knows about the custom elements:
- HTMLElementTagNameMap (src/index.ts): createElement/querySelector now
infer the exact component class for all 24 tags instead of HTMLElement.
- Per-component addEventListener/removeEventListener overrides, typing
`leaflet:<name>` events against the real Leaflet event payload
(PopupEvent, DragEndEvent, LeafletMouseEvent, etc.) while still accepting
ordinary DOM events normally, and rejecting event names that component
doesn't fire.
src/core/event-types.ts holds reusable event-name -> payload-type
fragments (mirroring shared-props.ts's fragment reuse), composed per
family: MapEvents, MarkerEvents, PathEvents, TileLayerEvents,
DivOverlayLayerEvents, GroupEvents. LeafletAddEventListener<T>/
LeafletRemoveEventListener<T> in with-props.ts are type-only
intersection-of-overloads helpers applied via `declare addEventListener:
...`, the same pattern every component already uses for `declare readonly
leafletObject?: X` -- zero runtime cost. Deliberately no generic `string`
fallback overload: a fallback would silently accept unrecognized
`leaflet:*` names too, defeating the point.
Fixed a real bug found while building this: #forwardEvents was
dispatching the raw pre-merge `data` Leaflet passes to fire(), missing
type/target/sourceTarget that Leaflet's own fire() merges in before
notifying real .on() listeners. Typing `detail` against Leaflet's actual
event interfaces would have been dishonest otherwise, so the merge now
matches Leaflet's own Evented#fire.
Also added 'line-updated' to the existing internal-event HTMLElementEventMap
augmentation in register.ts (needed once addEventListener got overridden
on polygon/polyline, which use it internally) and cleaned up ~35 now-
redundant `as HTMLElement & {...}` casts across the test suite that the
tag name map makes unnecessary.
89 lines
3.1 KiB
TypeScript
89 lines
3.1 KiB
TypeScript
import { ImageOverlay, SVGOverlay, VideoOverlay, latLngBounds } from 'leaflet';
|
|
import { describe, expect, it } from 'vitest';
|
|
import '../../src/components/leaflet-map.ts';
|
|
import '../../src/components/leaflet-image-overlay.ts';
|
|
import '../../src/components/leaflet-video-overlay.ts';
|
|
import '../../src/components/leaflet-svg-overlay.ts';
|
|
|
|
const BOUNDS: [[number, number], [number, number]] = [
|
|
[40.71, -74.24],
|
|
[40.78, -74.12],
|
|
];
|
|
|
|
describe('leaflet-image-overlay', () => {
|
|
it('creates an ImageOverlay with a live bounds getter (url has no Leaflet getter to read back)', () => {
|
|
const el = document.createElement('leaflet-image-overlay');
|
|
el.setAttribute('url', 'https://example.com/a.png');
|
|
el.setAttribute('bounds', JSON.stringify(BOUNDS));
|
|
document.body.append(el);
|
|
|
|
expect(el.leafletObject).toBeInstanceOf(ImageOverlay);
|
|
expect(el.url).toBe('https://example.com/a.png');
|
|
expect(el.bounds).toEqual(BOUNDS);
|
|
|
|
el.leafletObject?.setBounds(
|
|
latLngBounds([
|
|
[1, 2],
|
|
[3, 4],
|
|
]),
|
|
);
|
|
// el.bounds reads live via getBounds()
|
|
expect(el.bounds).toEqual([
|
|
[1, 2],
|
|
[3, 4],
|
|
]);
|
|
el.remove();
|
|
});
|
|
});
|
|
|
|
describe('leaflet-video-overlay', () => {
|
|
it('creates a VideoOverlay and applies the newly added options', () => {
|
|
const el = document.createElement('leaflet-video-overlay');
|
|
el.setAttribute('url', 'https://example.com/a.mp4');
|
|
el.setAttribute('bounds', JSON.stringify(BOUNDS));
|
|
el.setAttribute('z-index', '5');
|
|
el.setAttribute('class-name', 'my-video');
|
|
el.setAttribute('keep-aspect-ratio', 'false');
|
|
el.setAttribute('error-overlay-url', 'https://example.com/error.png');
|
|
document.body.append(el);
|
|
|
|
expect(el.leafletObject).toBeInstanceOf(VideoOverlay);
|
|
expect(el.leafletObject?.options.zIndex).toBe(5);
|
|
expect(el.leafletObject?.options.className).toBe('my-video');
|
|
expect(el.leafletObject?.options.keepAspectRatio).toBe(false);
|
|
expect(el.leafletObject?.options.errorOverlayUrl).toBe('https://example.com/error.png');
|
|
el.remove();
|
|
});
|
|
|
|
it('applies playback attributes directly to the underlying <video> element once it exists', () => {
|
|
// The <video> element is only created in onAdd(), so the overlay needs to
|
|
// actually be added to a map before getElement() returns anything.
|
|
const map = document.createElement('leaflet-map');
|
|
document.body.append(map);
|
|
|
|
const el = document.createElement('leaflet-video-overlay');
|
|
el.setAttribute('url', 'https://example.com/a.mp4');
|
|
el.setAttribute('bounds', JSON.stringify(BOUNDS));
|
|
el.setAttribute('muted', '');
|
|
el.setAttribute('loop', '');
|
|
map.append(el);
|
|
|
|
const video = el.getElement();
|
|
expect(video?.muted).toBe(true);
|
|
expect(video?.loop).toBe(true);
|
|
map.remove();
|
|
});
|
|
});
|
|
|
|
describe('leaflet-svg-overlay', () => {
|
|
it('creates an SVGOverlay with a live bounds getter', () => {
|
|
const el = document.createElement('leaflet-svg-overlay');
|
|
el.setAttribute('bounds', JSON.stringify(BOUNDS));
|
|
document.body.append(el);
|
|
|
|
expect(el.leafletObject).toBeInstanceOf(SVGOverlay);
|
|
expect(el.bounds).toEqual(BOUNDS);
|
|
el.remove();
|
|
});
|
|
});
|