forked from MapComplete/MapComplete
Refactoring of image detection, fix loading wikimedia images
This commit is contained in:
parent
4da6070b28
commit
a6e8714ae0
21 changed files with 468 additions and 528 deletions
|
@ -4,29 +4,35 @@ import Combine from "../Base/Combine";
|
|||
import DeleteImage from "./DeleteImage";
|
||||
import {AttributedImage} from "./AttributedImage";
|
||||
import BaseUIElement from "../BaseUIElement";
|
||||
import Img from "../Base/Img";
|
||||
import Toggle from "../Input/Toggle";
|
||||
import {Wikimedia} from "../../Logic/ImageProviders/Wikimedia";
|
||||
import {Imgur} from "../../Logic/ImageProviders/Imgur";
|
||||
import {Mapillary} from "../../Logic/ImageProviders/Mapillary";
|
||||
import ImageProvider from "../../Logic/ImageProviders/ImageProvider";
|
||||
|
||||
export class ImageCarousel extends Toggle {
|
||||
|
||||
constructor(images: UIEventSource<{ key: string, url: string }[]>, tags: UIEventSource<any>) {
|
||||
const uiElements = images.map((imageURLS: { key: string, url: string }[]) => {
|
||||
constructor(images: UIEventSource<{ key: string, url: string, provider: ImageProvider }[]>, tags: UIEventSource<any>) {
|
||||
const uiElements = images.map((imageURLS: { key: string, url: string, provider: ImageProvider }[]) => {
|
||||
const uiElements: BaseUIElement[] = [];
|
||||
for (const url of imageURLS) {
|
||||
let image = ImageCarousel.CreateImageElement(url.url)
|
||||
if (url.key !== undefined) {
|
||||
image = new Combine([
|
||||
image,
|
||||
new DeleteImage(url.key, tags).SetClass("delete-image-marker absolute top-0 left-0 pl-3")
|
||||
]).SetClass("relative");
|
||||
|
||||
try {
|
||||
|
||||
let image = new AttributedImage(url)
|
||||
|
||||
if (url.key !== undefined) {
|
||||
image = new Combine([
|
||||
image,
|
||||
new DeleteImage(url.key, tags).SetClass("delete-image-marker absolute top-0 left-0 pl-3")
|
||||
]).SetClass("relative");
|
||||
}
|
||||
image
|
||||
.SetClass("w-full block")
|
||||
.SetStyle("min-width: 50px; background: grey;")
|
||||
uiElements.push(image);
|
||||
} catch (e) {
|
||||
console.error("Could not generate image element for", url.url, "due to", e)
|
||||
}
|
||||
image
|
||||
.SetClass("w-full block")
|
||||
.SetStyle("min-width: 50px; background: grey;")
|
||||
uiElements.push(image);
|
||||
|
||||
|
||||
}
|
||||
return uiElements;
|
||||
});
|
||||
|
@ -38,33 +44,4 @@ export class ImageCarousel extends Toggle {
|
|||
)
|
||||
this.SetClass("block w-full");
|
||||
}
|
||||
|
||||
/***
|
||||
* Creates either a 'simpleimage' or a 'wikimediaimage' based on the string
|
||||
* @param url
|
||||
* @constructor
|
||||
*/
|
||||
private static CreateImageElement(url: string): BaseUIElement {
|
||||
// @ts-ignore
|
||||
let attrSource: ImageAttributionSource = undefined;
|
||||
if (url.startsWith("File:")) {
|
||||
attrSource = Wikimedia.singleton
|
||||
} else if (url.toLowerCase().startsWith("https://commons.wikimedia.org/wiki/")) {
|
||||
attrSource = Wikimedia.singleton;
|
||||
} else if (url.toLowerCase().startsWith("https://i.imgur.com/")) {
|
||||
attrSource = Imgur.singleton
|
||||
} else if (url.toLowerCase().startsWith("https://www.mapillary.com/map/im/")) {
|
||||
attrSource = Mapillary.singleton
|
||||
} else {
|
||||
return new Img(url);
|
||||
}
|
||||
|
||||
try {
|
||||
return new AttributedImage(url, attrSource)
|
||||
} catch (e) {
|
||||
console.error("Could not create an image: ", e)
|
||||
return undefined;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue