MapComplete/src/Logic/Osm/Actions/LinkImageAction.ts

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

59 lines
2 KiB
TypeScript
Raw Normal View History

2023-09-28 23:50:27 +02:00
import ChangeTagAction from "./ChangeTagAction"
import { Tag } from "../../Tags/Tag"
import OsmChangeAction from "./OsmChangeAction"
import { ChangeDescription } from "./ChangeDescription"
2024-12-06 01:04:58 +01:00
import { UIEventSource } from "../../UIEventSource"
export default class LinkImageAction extends OsmChangeAction {
2023-09-28 23:50:27 +02:00
private readonly _proposedKey: "image" | "mapillary" | "wiki_commons" | string
public readonly _url: string
2024-07-19 11:36:34 +02:00
private readonly _currentTags: UIEventSource<Record<string, string>>
2023-09-28 23:50:27 +02:00
private readonly _meta: { theme: string; changeType: "add-image" | "link-image" }
/**
* Adds an image-link to a feature
* @param elementId
* @param proposedKey a key which might be used, typically `image`. If the key is already used with a different URL, `key+":0"` will be used instead (or a higher number if needed)
* @param url
* @param currentTags
* @param meta
*
*/
constructor(
elementId: string,
proposedKey: "image" | "mapillary" | "wiki_commons" | string,
url: string,
2024-07-19 11:36:34 +02:00
currentTags: UIEventSource<Record<string, string>>,
meta: {
theme: string
changeType: "add-image" | "link-image"
}
) {
super(elementId, true)
2023-09-28 23:50:27 +02:00
this._proposedKey = proposedKey
this._url = url
this._currentTags = currentTags
this._meta = meta
}
protected CreateChangeDescriptions(): Promise<ChangeDescription[]> {
let key = this._proposedKey
let i = 0
const currentTags: Record<string, string> = this._currentTags.data
const url = this._url
while (currentTags[key] !== undefined && currentTags[key] !== url) {
key = this._proposedKey + ":" + i
i++
}
2023-09-28 23:50:27 +02:00
const tagChangeAction = new ChangeTagAction(
this.mainObjectId,
new Tag(key, url),
currentTags,
this._meta
)
2024-07-19 11:36:34 +02:00
this._currentTags.data[key] = url
this._currentTags.ping()
return tagChangeAction.CreateChangeDescriptions()
}
}