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 467d4754..5c83bae9 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 @@ -1,7 +1,9 @@ import Component from "@glimmer/component"; -import { fn } from "@ember/helper"; import { on } from "@ember/modifier"; import { action } from "@ember/object"; +import didInsert from "@ember/render-modifiers/modifiers/did-insert"; +import didUpdate from "@ember/render-modifiers/modifiers/did-update"; +import willDestroy from "@ember/render-modifiers/modifiers/will-destroy"; import { inject as service } from "@ember/service"; import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner"; import DButton from "discourse/components/d-button"; @@ -27,22 +29,54 @@ export default class AiImageCaptionContainer extends Component { const index = this.imageCaptionPopup.imageIndex; const matchingPlaceholder = this.composer.model.reply.match(IMAGE_MARKDOWN_REGEX); - const match = matchingPlaceholder[index]; - const replacement = match.replace( - IMAGE_MARKDOWN_REGEX, - `![${this.imageCaptionPopup.newCaption}|$2$3$4]($5)` - ); - this.appEvents.trigger("composer:replace-text", match, replacement); + + 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.hidePopup(); + } + + @action + resizeTextarea(target) { + const style = window.getComputedStyle(target); + + // scrollbars will show based on scrollHeight alone + // so we need to consider borders too + const borderTopWidth = parseInt(style.borderTopWidth, 10); + const borderBottomWidth = parseInt(style.borderBottomWidth, 10); + + target.scrollTop = 0; + target.style.height = `${ + target.scrollHeight + borderTopWidth + borderBottomWidth + }px`; + } + + @action + hidePopup() { this.imageCaptionPopup.showPopup = false; }