MapComplete/src/UI/Popup/Notes/NoteCommentElement.ts

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

37 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Store, UIEventSource } from "../../../Logic/UIEventSource"
2022-01-08 14:08:04 +01:00
export default class NoteCommentElement {
2024-06-22 18:50:46 +02:00
/**
* Adds the comment to the _visualisation_ of the given note; doesn't _actually_ upload
* @param txt
* @param tags
* @param state
*/
2022-01-26 21:40:38 +01:00
public static addCommentTo(
txt: string,
tags: UIEventSource<Record<string, string>>,
2024-06-24 13:11:35 +02:00
state: { osmConnection: { userDetails: Store<{ name: string; uid: number }> } }
2022-01-26 21:40:38 +01:00
) {
2022-01-08 14:08:04 +01:00
const comments: any[] = JSON.parse(tags.data["comments"])
const username = state.osmConnection.userDetails.data.name
const urlRegex = /(https?:\/\/[^\s]+)/g
2022-01-26 21:40:38 +01:00
const html = txt.replace(urlRegex, function (url) {
2022-01-08 14:08:04 +01:00
return '<a href="' + url + '">' + url + "</a>"
})
2022-01-26 21:40:38 +01:00
2022-01-08 14:08:04 +01:00
comments.push({
date: new Date().toISOString(),
uid: state.osmConnection.userDetails.data.uid,
user: username,
user_url: "https://www.openstreetmap.org/user/" + username,
action: "commented",
text: txt,
html: html,
2023-12-26 22:30:27 +01:00
highlighted: true,
2022-01-08 14:08:04 +01:00
})
tags.data["comments"] = JSON.stringify(comments)
tags.ping()
}
}