forked from MapComplete/MapComplete
		
	Fix: note themes uses full URL now instead of Panoramax-id
This commit is contained in:
		
							parent
							
								
									4395e88390
								
							
						
					
					
						commit
						ce363dfb59
					
				
					 4 changed files with 41 additions and 32 deletions
				
			
		| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
import { ImageUploader } from "./ImageUploader"
 | 
					import { ImageUploader, UploadResult } from "./ImageUploader"
 | 
				
			||||||
import LinkImageAction from "../Osm/Actions/LinkImageAction"
 | 
					import LinkImageAction from "../Osm/Actions/LinkImageAction"
 | 
				
			||||||
import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
 | 
					import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
 | 
				
			||||||
import { OsmId, OsmTags } from "../../Models/OsmFeature"
 | 
					import { OsmId, OsmTags } from "../../Models/OsmFeature"
 | 
				
			||||||
| 
						 | 
					@ -111,44 +111,40 @@ export class ImageUploadManager {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const tags = tagsStore.data
 | 
					        const tags = tagsStore.data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const featureId = <OsmId>tags.id
 | 
					        const featureId = <OsmId>tags.id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const author = this._osmConnection.userDetails.data.name
 | 
					        const author = this._osmConnection.userDetails.data.name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const action = await this.uploadImageWithLicense(
 | 
					        const uploadResult = await this.uploadImageWithLicense(
 | 
				
			||||||
            featureId,
 | 
					            featureId,
 | 
				
			||||||
            author,
 | 
					            author,
 | 
				
			||||||
            file,
 | 
					            file,
 | 
				
			||||||
            targetKey,
 | 
					            targetKey,
 | 
				
			||||||
            tags?.data?.["_orig_theme"],
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					        if (!uploadResult) {
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        const properties = this._featureProperties.getStore(featureId)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!action) {
 | 
					        const action = new LinkImageAction(featureId, uploadResult. key,  uploadResult   . value, properties, {
 | 
				
			||||||
            return
 | 
					            theme:  tags?.data?.["_orig_theme"] ?? this._layout.id,
 | 
				
			||||||
        }
 | 
					            changeType: "add-image",
 | 
				
			||||||
        if (!isNaN(Number(featureId))) {
 | 
					 | 
				
			||||||
            // This is a map note
 | 
					 | 
				
			||||||
            const url = action._url
 | 
					 | 
				
			||||||
            await this._osmConnection.addCommentToNote(featureId, url)
 | 
					 | 
				
			||||||
            NoteCommentElement.addCommentTo(url, <UIEventSource<any>>tagsStore, {
 | 
					 | 
				
			||||||
                osmConnection: this._osmConnection,
 | 
					 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
            return
 | 
					
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        await this._changes.applyAction(action)
 | 
					        await this._changes.applyAction(action)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private async uploadImageWithLicense(
 | 
					    public async uploadImageWithLicense(
 | 
				
			||||||
        featureId: OsmId,
 | 
					        featureId: OsmId,
 | 
				
			||||||
        author: string,
 | 
					        author: string,
 | 
				
			||||||
        blob: File,
 | 
					        blob: File,
 | 
				
			||||||
        targetKey: string | undefined,
 | 
					        targetKey: string | undefined,
 | 
				
			||||||
        theme?: string,
 | 
					    ): Promise<UploadResult> {
 | 
				
			||||||
    ): Promise<LinkImageAction> {
 | 
					 | 
				
			||||||
        this.increaseCountFor(this._uploadStarted, featureId)
 | 
					        this.increaseCountFor(this._uploadStarted, featureId)
 | 
				
			||||||
        const properties = this._featureProperties.getStore(featureId)
 | 
					 | 
				
			||||||
        let key: string
 | 
					        let key: string
 | 
				
			||||||
        let value: string
 | 
					        let value: string
 | 
				
			||||||
 | 
					        let absoluteUrl: string
 | 
				
			||||||
        let location: [number, number] = undefined
 | 
					        let location: [number, number] = undefined
 | 
				
			||||||
        if (this._gps.data) {
 | 
					        if (this._gps.data) {
 | 
				
			||||||
            location = [this._gps.data.longitude, this._gps.data.latitude]
 | 
					            location = [this._gps.data.longitude, this._gps.data.latitude]
 | 
				
			||||||
| 
						 | 
					@ -157,7 +153,6 @@ export class ImageUploadManager {
 | 
				
			||||||
            const feature = this._indexedFeatures.featuresById.data.get(featureId)
 | 
					            const feature = this._indexedFeatures.featuresById.data.get(featureId)
 | 
				
			||||||
            location = GeoOperations.centerpointCoordinates(feature)
 | 
					            location = GeoOperations.centerpointCoordinates(feature)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        let absoluteUrl: string
 | 
					 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            ;({ key, value, absoluteUrl } = await this._uploader.uploadImage(blob, location, author))
 | 
					            ;({ key, value, absoluteUrl } = await this._uploader.uploadImage(blob, location, author))
 | 
				
			||||||
        } catch (e) {
 | 
					        } catch (e) {
 | 
				
			||||||
| 
						 | 
					@ -179,10 +174,8 @@ export class ImageUploadManager {
 | 
				
			||||||
            value = absoluteUrl
 | 
					            value = absoluteUrl
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        this.increaseCountFor(this._uploadFinished, featureId)
 | 
					        this.increaseCountFor(this._uploadFinished, featureId)
 | 
				
			||||||
        return new LinkImageAction(featureId, key, value, properties, {
 | 
					        return {key, absoluteUrl, value}
 | 
				
			||||||
            theme: theme ?? this._layout.id,
 | 
					
 | 
				
			||||||
            changeType: "add-image",
 | 
					 | 
				
			||||||
        })
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private getCounterFor(collection: Map<string, UIEventSource<number>>, key: string | "*") {
 | 
					    private getCounterFor(collection: Map<string, UIEventSource<number>>, key: string | "*") {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,5 +8,7 @@ export interface ImageUploader {
 | 
				
			||||||
        blob: File,
 | 
					        blob: File,
 | 
				
			||||||
        currentGps: [number,number],
 | 
					        currentGps: [number,number],
 | 
				
			||||||
        author: string
 | 
					        author: string
 | 
				
			||||||
    ): Promise<{ key: string; value: string, absoluteUrl: string }>
 | 
					    ): Promise<UploadResult>
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface UploadResult{ key: string; value: string, absoluteUrl: string }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,11 +14,14 @@
 | 
				
			||||||
  import LoginButton from "../Base/LoginButton.svelte"
 | 
					  import LoginButton from "../Base/LoginButton.svelte"
 | 
				
			||||||
  import { Translation } from "../i18n/Translation"
 | 
					  import { Translation } from "../i18n/Translation"
 | 
				
			||||||
  import Camera from "@babeard/svelte-heroicons/solid/Camera"
 | 
					  import Camera from "@babeard/svelte-heroicons/solid/Camera"
 | 
				
			||||||
 | 
					  import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
 | 
				
			||||||
 | 
					  import NoteCommentElement from "../Popup/Notes/NoteCommentElement"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  export let state: SpecialVisualizationState
 | 
					  export let state: SpecialVisualizationState
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  export let tags: UIEventSource<OsmTags>
 | 
					  export let tags: UIEventSource<OsmTags>
 | 
				
			||||||
  export let targetKey: string = undefined
 | 
					  export let targetKey: string = undefined
 | 
				
			||||||
 | 
					  export let layer: LayerConfig
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Image to show in the button
 | 
					   * Image to show in the button
 | 
				
			||||||
   * NOT the image to upload!
 | 
					   * NOT the image to upload!
 | 
				
			||||||
| 
						 | 
					@ -30,11 +33,9 @@
 | 
				
			||||||
  export let labelText: string = undefined
 | 
					  export let labelText: string = undefined
 | 
				
			||||||
  const t = Translations.t.image
 | 
					  const t = Translations.t.image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let licenseStore = state?.userRelatedState?.imageLicense ?? new ImmutableStore("CC0")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  let errors = new UIEventSource<Translation[]>([])
 | 
					  let errors = new UIEventSource<Translation[]>([])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function handleFiles(files: FileList) {
 | 
					  async function handleFiles(files: FileList) {
 | 
				
			||||||
    const errs = []
 | 
					    const errs = []
 | 
				
			||||||
    for (let i = 0; i < files.length; i++) {
 | 
					    for (let i = 0; i < files.length; i++) {
 | 
				
			||||||
      const file = files.item(i)
 | 
					      const file = files.item(i)
 | 
				
			||||||
| 
						 | 
					@ -45,7 +46,21 @@
 | 
				
			||||||
          errs.push(canBeUploaded.error)
 | 
					          errs.push(canBeUploaded.error)
 | 
				
			||||||
          continue
 | 
					          continue
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        state?.imageUploadManager.uploadImageAndApply(file, tags, targetKey)
 | 
					
 | 
				
			||||||
 | 
					        if(layer.id === "note"){
 | 
				
			||||||
 | 
					          const uploadResult = await state?.imageUploadManager.uploadImageWithLicense(file, tags, targetKey)
 | 
				
			||||||
 | 
					          if(!uploadResult){
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          const url = uploadResult.absoluteUrl
 | 
				
			||||||
 | 
					          await this._osmConnection.addCommentToNote(tags.data.id, url)
 | 
				
			||||||
 | 
					          NoteCommentElement.addCommentTo(url, <UIEventSource<any>>tags, {
 | 
				
			||||||
 | 
					            osmConnection: this._osmConnection,
 | 
				
			||||||
 | 
					          })
 | 
				
			||||||
 | 
					          return
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await state?.imageUploadManager.uploadImageAndApply(file, tags, targetKey)
 | 
				
			||||||
      } catch (e) {
 | 
					      } catch (e) {
 | 
				
			||||||
        alert(e)
 | 
					        alert(e)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1100,11 +1100,10 @@ export default class SpecialVisualizations {
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                needsUrls: [Imgur.apiUrl, ...Imgur.supportingUrls],
 | 
					                needsUrls: [Imgur.apiUrl, ...Imgur.supportingUrls],
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                constr: (state, tags, args) => {
 | 
					                constr: (state, tags, args, feature, layer) => {
 | 
				
			||||||
                    const id = tags.data[args[0] ?? "id"]
 | 
					                    const id = tags.data[args[0] ?? "id"]
 | 
				
			||||||
                    tags = state.featureProperties.getStore(id)
 | 
					                    tags = state.featureProperties.getStore(id)
 | 
				
			||||||
                    console.log("Id is", id)
 | 
					                    return new SvelteUIElement(UploadImage, { state, tags, layer  })
 | 
				
			||||||
                    return new SvelteUIElement(UploadImage, { state, tags })
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue