From 6a30b06a55e5a4b96b400f356c2b9ec192ac529c Mon Sep 17 00:00:00 2001 From: Keegan George Date: Wed, 28 Feb 2024 13:32:45 -0800 Subject: [PATCH] DEV: Cancel popup should abort request (#497) --- .../ai-image-caption-container.gjs | 27 +++-------- .../discourse/services/image-caption-popup.js | 35 +++++++++++++- .../initializers/ai-image-caption.js | 48 +++++++++---------- .../system/ai_helper/ai_image_caption_spec.rb | 9 ++++ .../components/ai_caption_popup.rb | 8 ++++ 5 files changed, 81 insertions(+), 46 deletions(-) diff --git a/assets/javascripts/discourse/connectors/editor-preview/ai-image-caption-container.gjs b/assets/javascripts/discourse/connectors/editor-preview/ai-image-caption-container.gjs index 5c83bae9..0ca46547 100644 --- a/assets/javascripts/discourse/connectors/editor-preview/ai-image-caption-container.gjs +++ b/assets/javascripts/discourse/connectors/editor-preview/ai-image-caption-container.gjs @@ -11,12 +11,9 @@ import DTextarea from "discourse/components/d-textarea"; import autoFocus from "discourse/modifiers/auto-focus"; import icon from "discourse-common/helpers/d-icon"; import i18n from "discourse-common/helpers/i18n"; -import { IMAGE_MARKDOWN_REGEX } from "../../lib/utilities"; export default class AiImageCaptionContainer extends Component { @service imageCaptionPopup; - @service appEvents; - @service composer; @action updateCaption(event) { @@ -26,22 +23,7 @@ export default class AiImageCaptionContainer extends Component { @action saveCaption() { - const index = this.imageCaptionPopup.imageIndex; - const matchingPlaceholder = - this.composer.model.reply.match(IMAGE_MARKDOWN_REGEX); - - if (matchingPlaceholder) { - const match = matchingPlaceholder[index]; - const replacement = match.replace( - IMAGE_MARKDOWN_REGEX, - `![${this.imageCaptionPopup.newCaption}|$2$3$4]($5)` - ); - - if (match) { - this.appEvents.trigger("composer:replace-text", match, replacement); - } - } - + this.imageCaptionPopup.updateCaption(); this.hidePopup(); } @@ -63,6 +45,11 @@ export default class AiImageCaptionContainer extends Component { @action hidePopup() { this.imageCaptionPopup.showPopup = false; + if (this.imageCaptionPopup._request) { + this.imageCaptionPopup._request.abort(); + this.imageCaptionPopup._request = null; + this.imageCaptionPopup.toggleLoadingState(false); + } }