| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { Store, UIEventSource } from "../UIEventSource" | 
					
						
							|  |  |  | import BaseUIElement from "../../UI/BaseUIElement" | 
					
						
							|  |  |  | import { LicenseInfo } from "./LicenseInfo" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface ProvidedImage { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     url: string | 
					
						
							|  |  |  |     key: string | 
					
						
							| 
									
										
										
										
											2021-10-07 22:06:47 +02:00
										 |  |  |     provider: ImageProvider | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default abstract class ImageProvider { | 
					
						
							| 
									
										
										
										
											2021-10-18 22:17:41 +02:00
										 |  |  |     public abstract readonly defaultKeyPrefixes: string[] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public abstract SourceIcon(backlinkSource?: string): BaseUIElement | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Given a properies object, maps it onto _all_ the available pictures for this imageProvider | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public GetRelevantUrls( | 
					
						
							|  |  |  |         allTags: Store<any>, | 
					
						
							|  |  |  |         options?: { | 
					
						
							|  |  |  |             prefixes?: string[] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ): UIEventSource<ProvidedImage[]> { | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |         const prefixes = options?.prefixes ?? this.defaultKeyPrefixes | 
					
						
							| 
									
										
										
										
											2021-10-07 22:06:47 +02:00
										 |  |  |         if (prefixes === undefined) { | 
					
						
							| 
									
										
										
										
											2022-02-14 02:26:03 +01:00
										 |  |  |             throw "No `defaultKeyPrefixes` defined by this image provider" | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const relevantUrls = new UIEventSource< | 
					
						
							|  |  |  |             { url: string; key: string; provider: ImageProvider }[] | 
					
						
							|  |  |  |         >([]) | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |         const seenValues = new Set<string>() | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         allTags.addCallbackAndRunD((tags) => { | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |             for (const key in tags) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 if (!prefixes.some((prefix) => key.startsWith(prefix))) { | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |                     continue | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 const values = Utils.NoEmpty(tags[key]?.split(";")?.map((v) => v.trim()) ?? []) | 
					
						
							| 
									
										
										
										
											2021-10-07 22:06:47 +02:00
										 |  |  |                 for (const value of values) { | 
					
						
							|  |  |  |                     if (seenValues.has(value)) { | 
					
						
							|  |  |  |                         continue | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-10-07 22:06:47 +02:00
										 |  |  |                     seenValues.add(value) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     this.ExtractUrls(key, value).then((promises) => { | 
					
						
							| 
									
										
										
										
											2021-10-07 22:06:47 +02:00
										 |  |  |                         for (const promise of promises ?? []) { | 
					
						
							|  |  |  |                             if (promise === undefined) { | 
					
						
							|  |  |  |                                 continue | 
					
						
							|  |  |  |                             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                             promise.then((providedImage) => { | 
					
						
							| 
									
										
										
										
											2021-10-07 22:06:47 +02:00
										 |  |  |                                 if (providedImage === undefined) { | 
					
						
							|  |  |  |                                     return | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                                 relevantUrls.data.push(providedImage) | 
					
						
							|  |  |  |                                 relevantUrls.ping() | 
					
						
							|  |  |  |                             }) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         return relevantUrls | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public abstract ExtractUrls(key: string, value: string): Promise<Promise<ProvidedImage>[]> | 
					
						
							| 
									
										
										
										
											2021-10-07 22:06:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public abstract DownloadAttribution(url: string): Promise<LicenseInfo> | 
					
						
							|  |  |  | } |