Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ function restore(map: MapLibre.Map): void {
replaced = []
}

function mergeCredits(controls: MapLibre.AttributionControl[]): string[] | undefined {
const credits = [...new Set(controls.flatMap(existing => existing.options.customAttribution ?? []))]
return credits.length ? credits : undefined
}

const control = useMapLibreResource<MapLibre.AttributionControl>({
create({ maplibre, map }) {
// `_controls` is the list that `hasControl()` reads. `filter` copies it before removal.
replaced = map._controls.filter((existing): existing is MapLibre.AttributionControl => existing instanceof maplibre.AttributionControl)
// The component options override the map's `attributionControl` options.
// Credits the map already configured stay unless the component sets its own.
// Credits from every replaced control stay unless the component sets its own.
const inherited = replaced[0]?.options
const options = inherited || props.options
? { ...inherited, ...props.options, customAttribution: props.options?.customAttribution ?? inherited?.customAttribution }
? { ...inherited, ...props.options, customAttribution: props.options?.customAttribution ?? mergeCredits(replaced) }
: undefined
const instance = new maplibre.AttributionControl(options)
for (const existing of replaced)
Expand Down
18 changes: 17 additions & 1 deletion test/nuxt-runtime/maplibre-controls.nuxt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ describe('mapLibre attribution control', () => {
global: provideMap(maplibre, map),
})
await nextTick()
expect(controlsOf(map, 'attribution')[0]!.options).toEqual({ compact: false, customAttribution: 'Data: Hobart City Council' })
expect(controlsOf(map, 'attribution')[0]!.options).toEqual({ compact: false, customAttribution: ['Data: Hobart City Council'] })
inheriting.unmount()

const overriding = mount(ScriptMapLibreAttributionControl, {
Expand All @@ -318,6 +318,22 @@ describe('mapLibre attribution control', () => {
overriding.unmount()
})

it('keeps the credits of every replaced attribution control', async () => {
const maplibre = createMapLibre()
const { map } = mapWithDefaultAttribution(maplibre, { compact: true, customAttribution: 'MapLibre' })
map.addControl(new (maplibre.AttributionControl as any)({ customAttribution: ['Data: Hobart City Council', 'MapLibre'] }))
const wrapper = mount(ScriptMapLibreAttributionControl, {
global: provideMap(maplibre, map),
})
await nextTick()

const [mounted] = controlsOf(map, 'attribution')
expect(controlsOf(map, 'attribution')).toHaveLength(1)
expect((mounted!.options as { customAttribution: unknown }).customAttribution).toEqual(['MapLibre', 'Data: Hobart City Council'])
wrapper.unmount()
expect(controlsOf(map, 'attribution')).toHaveLength(2)
})

it('restores the map default after consumer code removed the component control', async () => {
const maplibre = createMapLibre()
const { map, builtIn } = mapWithDefaultAttribution(maplibre)
Expand Down
Loading