Refactoring: convert qr_code to Svelte, deprecate Img.ts

This commit is contained in:
Pieter Vander Vennet 2024-08-09 14:40:07 +02:00
parent b29557a21d
commit f26265e25e
5 changed files with 65 additions and 47 deletions

View file

@ -1,11 +1,15 @@
import { Utils } from "../../Utils" import { Utils } from "../../Utils"
import BaseUIElement from "../BaseUIElement" import BaseUIElement from "../BaseUIElement"
/**
* @deprecated
*/
export default class Img extends BaseUIElement { export default class Img extends BaseUIElement {
private readonly _src: string private readonly _src: string
private readonly _rawSvg: boolean private readonly _rawSvg: boolean
private readonly _options: { readonly fallbackImage?: string } private readonly _options: { readonly fallbackImage?: string }
/** @deprecated
*/
constructor( constructor(
src: string, src: string,
rawSvg = false, rawSvg = false,

View file

@ -20,10 +20,17 @@
</script> </script>
{#if $txt} {#if $txt}
{#if cls}
<span class={cls}> <span class={cls}>
<span lang={$lang}> <span lang={$lang}>
{@html Utils.purify($txt)} {@html Utils.purify($txt)}
</span> </span>
<WeblateLink context={t?.context} /> <WeblateLink context={t?.context} />
</span> </span>
{:else}
<span lang={$lang}>
{@html Utils.purify($txt)}
</span>
<WeblateLink context={t?.context} />
{/if}
{/if} {/if}

View file

@ -11,12 +11,8 @@
import Tr from "../Base/Tr.svelte" import Tr from "../Base/Tr.svelte"
import Translations from "../i18n/Translations" import Translations from "../i18n/Translations"
import { Utils } from "../../Utils" import { Utils } from "../../Utils"
import { DocumentDuplicateIcon } from "@rgossiaux/svelte-heroicons/outline"
import ToSvelte from "../Base/ToSvelte.svelte"
import Img from "../Base/Img"
import Qr from "../../Utils/Qr" import Qr from "../../Utils/Qr"
import AccordionSingle from "../Flowbite/AccordionSingle.svelte" import AccordionSingle from "../Flowbite/AccordionSingle.svelte"
import Share from "@babeard/svelte-heroicons/solid/Share"
import Constants from "../../Models/Constants" import Constants from "../../Models/Constants"
import Copyable from "../Base/Copyable.svelte" import Copyable from "../Base/Copyable.svelte"
@ -127,9 +123,7 @@
<Copyable {state} text={linkToShare} /> <Copyable {state} text={linkToShare} />
</div> </div>
<div class="flex justify-center"> <div class="flex justify-center">
<ToSvelte <img src={new Qr(linkToShare).toImageElement(125)} style="width: 125px"/>
construct={() => new Img(new Qr(linkToShare).toImageElement(125)).SetStyle("width: 125px")}
/>
</div> </div>
<Tr t={tr.embedIntro} /> <Tr t={tr.embedIntro} />

View file

@ -0,0 +1,46 @@
<script lang="ts">
import { Store, UIEventSource } from "../../Logic/UIEventSource"
import type { SpecialVisualizationState } from "../SpecialVisualization"
import type { Feature } from "geojson"
import Loading from "../Base/Loading.svelte"
import Qr from "../../Utils/Qr"
import { GeoOperations } from "../../Logic/GeoOperations"
const smallSize = 100
const bigSize = 200
let size = new UIEventSource(smallSize)
export let state: SpecialVisualizationState
export let tags: UIEventSource<Record<string, string>>
export let feature: Feature
let [lon, lat] = GeoOperations.centerpointCoordinates(feature)
const includeLayout = window.location.pathname
.split("/")
.at(-1)
.startsWith("theme")
const layout = includeLayout
? "layout=" + state.layout.id + "&"
: ""
let id: Store<string> = tags.mapD(tags => tags.id)
let url = id.mapD(id => `${window.location.protocol}//${window.location.host}${window.location.pathname}?${layout}lat=${lat}&lon=${lon}&z=15` +
`#${id}`)
function toggleSize() {
if (size.data !== bigSize) {
size.setData(bigSize)
} else {
size.setData(smallSize)
}
}
</script>
{#if $id.startsWith("node/-")}
<!-- Not yet uploaded, doesn't have a fixed ID -->
<Loading />
{:else}
<img on:click={() => toggleSize()} src={new Qr($url).toImageElement($size)} style={`width: ${$size}px; height: ${$size}px`} />
{/if}

View file

@ -100,6 +100,8 @@ import { CombinedFetcher } from "../Logic/Web/NearbyImagesSearch"
import { And } from "../Logic/Tags/And" import { And } from "../Logic/Tags/And"
import CloseNoteButton from "./Popup/Notes/CloseNoteButton.svelte" import CloseNoteButton from "./Popup/Notes/CloseNoteButton.svelte"
import PendingChangesIndicator from "./BigComponents/PendingChangesIndicator.svelte" import PendingChangesIndicator from "./BigComponents/PendingChangesIndicator.svelte"
import Loading from "./Base/Loading"
import QrCode from "./Popup/QrCode.svelte"
class NearbyImageVis implements SpecialVisualization { class NearbyImageVis implements SpecialVisualization {
// Class must be in SpecialVisualisations due to weird cyclical import that breaks the tests // Class must be in SpecialVisualisations due to weird cyclical import that breaks the tests
@ -1686,47 +1688,12 @@ export default class SpecialVisualizations {
docs: "Generates a QR-code to share the selected object", docs: "Generates a QR-code to share the selected object",
constr( constr(
state: SpecialVisualizationState, state: SpecialVisualizationState,
tagSource: UIEventSource<Record<string, string>>, tags: UIEventSource<Record<string, string>>,
argument: string[], argument: string[],
feature: Feature feature: Feature
): BaseUIElement { ): SvelteUIElement {
const smallSize = 100 return new SvelteUIElement(QrCode , {state, tags, feature} )
const bigSize = 200
const size = new UIEventSource(smallSize)
return new VariableUiElement(
tagSource
.map((tags) => tags.id)
.map(
(id) => {
if (id.startsWith("node/-")) {
// Not yet uploaded
return undefined
} }
const [lon, lat] = GeoOperations.centerpointCoordinates(feature)
const includeLayout = window.location.pathname
.split("/")
.at(-1)
.startsWith("theme")
const layout = includeLayout
? "layout=" + state.layout.id + "&"
: ""
const url =
`${window.location.protocol}//${window.location.host}${window.location.pathname}?${layout}lat=${lat}&lon=${lon}&z=15` +
`#${id}`
return new Img(new Qr(url).toImageElement(size.data)).SetStyle(
`width: ${size.data}px`
)
},
[size]
)
).onClick(() => {
if (size.data !== bigSize) {
size.setData(bigSize)
} else {
size.setData(smallSize)
}
})
},
}, },
{ {
funcName: "direction_absolute", funcName: "direction_absolute",