import { createCommentVNode, defineComponent, inject, isRef, type PropType, provide, type SlotsType, watch, } from "vue"; import { componentIdSymbol, sourceIdSymbol, sourceLayerRegistry, SourceOptionProps, } from "@/lib/types"; import type { GeoJSONSource, PromoteIdSpecification } from "maplibre-gl"; import { SourceLayerRegistry } from "@/lib/lib/sourceLayer.registry"; import { SourceLib } from "@/lib/lib/source.lib"; import { useSource } from "@/lib/composable/useSource"; type DataType = GeoJSON.GeoJSON | string; /** * See [GeoJSONSource](https://maplibre.org/maplibre-gl-js/docs/API/classes/GeoJSONSource/) */ export default defineComponent({ name: "MglGeoJsonSource", props: { sourceId: { type: String as PropType, required: true, }, data: { type: [Object, String] as PropType, required: true, }, maxzoom: Number as PropType, attribution: String as PropType, buffer: Number as PropType, tolerance: Number as PropType, cluster: Boolean as PropType, clusterRadius: Number as PropType, clusterMaxZoom: Number as PropType, clusterMinPoints: Number as PropType, clusterProperties: Object as PropType, lineMetrics: Boolean as PropType, generateId: Boolean as PropType, promoteId: [Object, String] as PropType, filter: [Array, String, Object] as PropType, }, slots: Object as SlotsType<{ default: unknown }>, setup(props, { slots }) { const cid = inject(componentIdSymbol)!, source = SourceLib.getSourceRef(cid, props.sourceId), registry = new SourceLayerRegistry(), opts = { ...props, type: "geojson" }; provide(sourceIdSymbol, props.sourceId); provide(sourceLayerRegistry, registry); useSource(source, opts as SourceOptionProps, registry); watch( [isRef(props.data) ? props.data : () => props.data, source], ([v, src]) => { src?.setData( (v as DataType) || { type: "FeatureCollection", features: [] }, ); }, { immediate: true }, ); return () => [ createCommentVNode("GeoJSON Source"), source.value && slots.default ? slots.default({}) : undefined, ]; }, });