You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
// Tracks an ordered set of vertices contributed by <leaflet-line> children,
|
|
// built entirely from the leaflet-line-sync/leaflet-line-remove events they
|
|
// fire (see register.ts) -- never by querying the DOM or reading a child's
|
|
// property directly. Shared by leaflet-polygon and leaflet-polyline.
|
|
//
|
|
// Order matters (it's the vertex sequence), so membership changes are
|
|
// inserted at their actual document position via compareDocumentPosition --
|
|
// that's a structural question about the tree, not a read of child state,
|
|
// and there's no other sane source of truth for "which vertex comes first."
|
|
export type LatLngTuple = [number, number];
|
|
|
|
export class VertexTracker {
|
|
#vertices = new Map<HTMLElement, LatLngTuple>();
|
|
#order: HTMLElement[] = [];
|
|
|
|
sync(element: HTMLElement, latlng: LatLngTuple): void {
|
|
if (!this.#vertices.has(element)) this.#insertOrdered(element);
|
|
this.#vertices.set(element, latlng);
|
|
}
|
|
|
|
remove(element: HTMLElement): void {
|
|
this.#vertices.delete(element);
|
|
this.#order = this.#order.filter((el) => el !== element);
|
|
}
|
|
|
|
coords(): LatLngTuple[] {
|
|
return this.#order.map((el) => this.#vertices.get(el)!);
|
|
}
|
|
|
|
#insertOrdered(el: HTMLElement): void {
|
|
const idx = this.#order.findIndex(
|
|
(existing) => (existing.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_PRECEDING) !== 0,
|
|
);
|
|
if (idx === -1) this.#order.push(el);
|
|
else this.#order.splice(idx, 0, el);
|
|
}
|
|
}
|