A platform for community discussion. Free, open, simple.
DEV: Conditionally show AI image caption button (#35463)
Keegan George85de78b428ae
diff --git a/app/assets/javascripts/discourse-markdown-it/src/features/image-controls.js b/app/assets/javascripts/discourse-markdown-it/src/features/image-controls.jsindex 8dcf5385503..6103eee19e5 100644--- a/app/assets/javascripts/discourse-markdown-it/src/features/image-controls.js+++ b/app/assets/javascripts/discourse-markdown-it/src/features/image-controls.js@@ -5,7 +5,12 @@ const SCALES = ["100", "75", "50"];let apiExtraButton = [];let apiExtraButtonAllowList = [];-export function addImageWrapperButton(label, btnClass, icon = null) {+export function addImageWrapperButton(+ label,+ btnClass,+ icon = null,+ includeCondition = null+) {const markup = [];markup.push(`<span class="${btnClass}">`);if (icon) {@@ -18,7 +23,10 @@ export function addImageWrapperButton(label, btnClass, icon = null) {markup.push(label);markup.push("</span>");- apiExtraButton.push(markup.join(""));+ apiExtraButton.push({+ markup: markup.join(""),+ condition: includeCondition,+ });apiExtraButtonAllowList.push(`span.${btnClass}`);apiExtraButtonAllowList.push(`svg[class=fa d-icon d-icon-${icon} svg-icon svg-string]`@@ -182,7 +190,17 @@ function ruleWithImageControls(oldRule) {result += `</span>`;result += buildImageDeleteButton();- result += apiExtraButton.join("");+ // Get upload URL from token for conditional button rendering+ const origSrcIndex = token.attrIndex("data-orig-src");+ const uploadUrl =+ origSrcIndex !== -1 ? token.attrs[origSrcIndex][1] : null;++ // Add API extra buttons with optional conditions+ apiExtraButton.forEach((button) => {+ if (!button.condition || button.condition(uploadUrl)) {+ result += button.markup;+ }+ });result += "</span></span>";diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs b/app/assets/javascripts/discourse/app/lib/plugin-api.gjsindex 1d9f9b4460d..9f83edd29ba 100644--- a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs@@ -3237,20 +3237,25 @@ class PluginApi {/*** Adds a custom button to the composer preview's image wrapper*+ * @param {string} label - The button label text+ * @param {string} btnClass - The CSS class for the button+ * @param {string} icon - The icon name (optional)+ * @param {Function} fn - The click handler function+ * @param {Function} includeCondition - Optional function that receives the image src and returns true if the button should be shown (optional)** ```* api.addComposerImageWrapperButton(* "My Custom Button",- * "custom-button-class"- * "lock"- * (event) => { console.log("Custom button clicked", event)- * });- *+ * "custom-button-class",+ * "lock",+ * (event) => { console.log("Custom button clicked", event); },+ * (imageSrc) => imageSrc.endsWith('.jpg') // Only show for JPG images+ * );* ```**/- addComposerImageWrapperButton(label, btnClass, icon, fn) {- addImageWrapperButton(label, btnClass, icon);+ addComposerImageWrapperButton(label, btnClass, icon, fn, includeCondition) {+ addImageWrapperButton(label, btnClass, icon, includeCondition);addApiImageWrapperButtonClickEvent(fn);}diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-image-preview-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-image-preview-test.jsindex 1e20a27ae8a..0fed7dd65cc 100644--- a/app/assets/javascripts/discourse/tests/acceptance/composer-image-preview-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-image-preview-test.js@@ -397,3 +397,105 @@ acceptance("Composer - Image Preview - Plugin API", function (needs) {);});});++acceptance(+ "Composer - Image Preview - Plugin API - includeCondition",+ function (needs) {+ needs.user({});+ needs.settings({ allow_uncategorized_topics: true });+ needs.site({ can_tag_topics: true });+ needs.pretender((server, helper) => {+ server.post("/uploads/lookup-urls", () => {+ return helper.response([]);+ });+ });++ needs.hooks.beforeEach(() => {+ withPluginApi((api) => {+ // Button that only shows for JPEG images+ api.addComposerImageWrapperButton(+ "JPEG Only",+ "jpeg-only-button",+ "image",+ () => {},+ (uploadUrl) => uploadUrl && uploadUrl.endsWith(".jpeg")+ );++ // Button that only shows for SVG images+ api.addComposerImageWrapperButton(+ "SVG Only",+ "svg-only-button",+ "file",+ () => {},+ (uploadUrl) => uploadUrl && uploadUrl.endsWith(".svg")+ );++ // Button without condition that should show for all images+ api.addComposerImageWrapperButton(+ "Always Show",+ "always-show-button",+ "check",+ () => {}+ );+ });+ });++ test("buttons with includeCondition only show for matching image types", async function (assert) {+ await visit("/");+ await click("#create-topic");++ // Add a JPEG image+ await fillIn(+ ".d-editor-input",+ ""+ );++ assert+ .dom(".image-wrapper .jpeg-only-button")+ .exists("JPEG button appears for JPEG image");++ assert+ .dom(".image-wrapper .svg-only-button")+ .doesNotExist("SVG button does not appear for JPEG image");++ // Replace with an SVG image+ await fillIn(+ ".d-editor-input",+ ""+ );++ assert+ .dom(".image-wrapper .svg-only-button")+ .exists("SVG button appears for SVG image");++ assert+ .dom(".image-wrapper .jpeg-only-button")+ .doesNotExist("JPEG button does not appear for SVG image");+ });++ test("button without includeCondition shows for all images", async function (assert) {+ await visit("/");+ await click("#create-topic");++ // Test with JPEG+ await fillIn(+ ".d-editor-input",+ ""+ );++ assert+ .dom(".image-wrapper .always-show-button")+ .exists("Button without condition appears for JPEG");++ // Test with SVG+ await fillIn(+ ".d-editor-input",+ ""+ );++ assert+ .dom(".image-wrapper .always-show-button")+ .exists("Button without condition appears for SVG");+ });+ }+);diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.jsindex b3e417d25ef..997043cdf78 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-image-caption.js@@ -7,6 +7,13 @@ import {popupAiCreditLimitError,} from "../lib/ai-errors";+function isAiCaptionableImage(uploadUrl) {+ if (!uploadUrl) {+ return false;+ }+ return /\.(png|jpe?g|gif|webp)$/i.test(uploadUrl);+}+export default apiInitializer((api) => {const buttonAttrs = {label: i18n("discourse_ai.ai_helper.image_caption.button_label"),@@ -87,6 +94,7 @@ export default apiInitializer((api) => {imageCaptionPopup.toggleLoadingState(false);});}- }+ },+ isAiCaptionableImage);});