2024-02-19 12:56:28 -05:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2024-11-19 05:57:40 -05:00
|
|
|
import Service, { service } from "@ember/service";
|
2024-10-21 17:48:10 -04:00
|
|
|
import { IMAGE_MARKDOWN_REGEX } from "discourse/lib/uploads";
|
2024-02-19 12:56:28 -05:00
|
|
|
|
|
|
|
export default class ImageCaptionPopup extends Service {
|
2024-02-28 16:32:45 -05:00
|
|
|
@service composer;
|
|
|
|
@service appEvents;
|
|
|
|
|
2024-02-19 12:56:28 -05:00
|
|
|
@tracked showPopup = false;
|
|
|
|
@tracked imageIndex = null;
|
|
|
|
@tracked imageSrc = null;
|
|
|
|
@tracked newCaption = null;
|
|
|
|
@tracked loading = false;
|
2024-02-28 16:32:45 -05:00
|
|
|
@tracked popupTrigger = null;
|
2024-05-27 13:49:24 -04:00
|
|
|
@tracked showAutoCaptionLoader = false;
|
2024-02-28 16:32:45 -05:00
|
|
|
@tracked _request = null;
|
|
|
|
|
|
|
|
updateCaption() {
|
|
|
|
const matchingPlaceholder =
|
|
|
|
this.composer.model.reply.match(IMAGE_MARKDOWN_REGEX);
|
|
|
|
|
|
|
|
if (matchingPlaceholder) {
|
|
|
|
const match = matchingPlaceholder[this.imageIndex];
|
|
|
|
const replacement = match.replace(
|
|
|
|
IMAGE_MARKDOWN_REGEX,
|
|
|
|
`![${this.newCaption}|$2$3$4]($5)`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
this.appEvents.trigger("composer:replace-text", match, replacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleLoadingState(loading) {
|
|
|
|
if (loading) {
|
|
|
|
this.popupTrigger?.classList.add("disabled");
|
|
|
|
return (this.loading = true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.popupTrigger?.classList.remove("disabled");
|
|
|
|
return (this.loading = false);
|
|
|
|
}
|
2024-02-19 12:56:28 -05:00
|
|
|
}
|