MapComplete/src/UI/Image/LinkableImage.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
2.9 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { UIEventSource } from "../../Logic/UIEventSource"
import type { OsmTags } from "../../Models/OsmFeature"
import type { SpecialVisualizationState } from "../SpecialVisualization"
import type { P4CPicture } from "../../Logic/Web/NearbyImagesSearch"
import AllImageProviders from "../../Logic/ImageProviders/AllImageProviders"
import LinkImageAction from "../../Logic/Osm/Actions/LinkImageAction"
import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction"
import { Tag } from "../../Logic/Tags/Tag"
import type { Feature } from "geojson"
import Translations from "../i18n/Translations"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import type { ProvidedImage } from "../../Logic/ImageProviders/ImageProvider"
import AttributedImage from "./AttributedImage.svelte"
import SpecialTranslation from "../Popup/TagRendering/SpecialTranslation.svelte"
export let tags: UIEventSource<OsmTags>
2023-09-21 15:29:34 +02:00
export let state: SpecialVisualizationState
export let image: P4CPicture
export let feature: Feature
export let layer: LayerConfig
2023-09-28 23:50:27 +02:00
export let linkable = true
let targetValue = Object.values(image.osmTags)[0]
let isLinked = new UIEventSource(Object.values(tags.data).some((v) => targetValue === v))
2023-09-28 23:50:27 +02:00
const t = Translations.t.image.nearby
const providedImage: ProvidedImage = {
url: image.thumbUrl ?? image.pictureUrl,
url_hd: image.pictureUrl,
2023-12-19 23:02:02 +01:00
key: undefined,
provider: AllImageProviders.byName(image.provider),
date: new Date(image.date),
id: Object.values(image.osmTags)[0],
}
2023-09-28 23:50:27 +02:00
function applyLink(isLinked :boolean) {
console.log("Applying linked image", isLinked, targetValue)
2023-09-21 15:29:34 +02:00
const currentTags = tags.data
const key = Object.keys(image.osmTags)[0]
const url = targetValue
if (isLinked) {
2023-11-23 16:00:14 +01:00
const action = new LinkImageAction(currentTags.id, key, url, tags, {
theme: tags.data._orig_theme ?? state.layout.id,
2023-09-21 15:29:34 +02:00
changeType: "link-image",
})
state.changes.applyAction(action)
} else {
for (const k in currentTags) {
2023-09-21 15:29:34 +02:00
const v = currentTags[k]
if (v === url) {
2023-09-21 15:29:34 +02:00
const action = new ChangeTagAction(currentTags.id, new Tag(k, ""), currentTags, {
2023-12-03 04:49:28 +01:00
theme: tags.data._orig_theme ?? state.layout.id,
2023-09-21 15:29:34 +02:00
changeType: "remove-image",
})
state.changes.applyAction(action)
}
}
}
}
isLinked.addCallback(isLinked => applyLink(isLinked))
</script>
2023-09-21 15:29:34 +02:00
<div class="flex w-fit shrink-0 flex-col">
2023-12-19 23:02:02 +01:00
<div class="cursor-zoom-in" on:click={() => state.previewedImage.setData(providedImage)}>
2023-12-21 01:46:18 +01:00
<AttributedImage
image={providedImage}
imgClass="max-h-64 w-auto"
previewedImage={state.previewedImage}
/>
</div>
{#if linkable}
<label>
<input bind:checked={$isLinked} type="checkbox" />
<SpecialTranslation t={t.link} {tags} {state} {layer} {feature} />
</label>
{/if}
</div>