Skip to content

Commit 099185a

Browse files
authored
SVG and unlimited images (#47)
* svg, image style config, automatic autogeneration * remove version * works with unlimited images
1 parent 44f153b commit 099185a

File tree

4 files changed

+239
-144
lines changed

4 files changed

+239
-144
lines changed
Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(function ($) {
22
$(document).ready(function () {
33
/**
4-
* Ensure the correct alt field is wrapped properly.
4+
* Wrap ALT fields in a container if not already done.
55
*/
66
function wrapAltTextFields() {
7-
$("input[name^='field_image'][name$='[alt]']").each(function () {
7+
$("input[name$='[alt]']").each(function () {
88
var $altField = $(this).closest(".form-item");
99
if (!$altField.parent().hasClass("ai-alt-field-wrapper")) {
1010
$altField.wrap('<div class="ai-alt-field-wrapper"></div>');
@@ -13,77 +13,70 @@
1313
}
1414

1515
/**
16-
* Automatically trigger alt text generation on page load if needed.
16+
* Generate alt text for each item that was flagged in Backdrop.settings.openaiAlt.
17+
* (We only add items with empty alt in the PHP code, so we won't overwrite existing alt.)
1718
*/
18-
function triggerAutoGenerationIfNeeded() {
19+
function triggerAutoGenerationForAll() {
1920
if (Backdrop.settings.openaiAlt) {
20-
var fid = Backdrop.settings.openaiAlt.fid;
21-
var fieldName = Backdrop.settings.openaiAlt.field_name;
22-
var delta = Backdrop.settings.openaiAlt.delta;
23-
24-
// Trigger AJAX call to generate Alt text
25-
$.ajax({
26-
url: Backdrop.settings.basePath + "openai-alt/generate-alt-text",
27-
type: "POST",
28-
data: { fid: fid, field_name: fieldName, delta: delta },
29-
success: function (response) {
30-
if (response.status === "success" && response.alt_text) {
31-
var $altField = $("input[name='" + fieldName + "[und][" + delta + "][alt]']");
32-
$altField.val(response.alt_text).trigger("change");
33-
34-
// Ensure Backdrop recognizes the update
35-
setTimeout(function () {
36-
Backdrop.attachBehaviors();
37-
}, 500);
38-
}
39-
},
40-
error: function (xhr, status, error) {
41-
console.error("❌ AJAX request failed:", status, error);
42-
},
21+
$.each(Backdrop.settings.openaiAlt, function (key, item) {
22+
// item = { fid, field_name, delta, target_id, ... }
23+
if (item.fid && item.field_name !== undefined && item.delta !== undefined) {
24+
generateAltText(item.fid, item.field_name, item.delta);
25+
}
4326
});
4427
}
4528
}
4629

4730
/**
48-
* Handle AJAX responses for file uploads & alt text generation.
31+
* Helper: Actually call the endpoint, then place alt text in the correct input.
4932
*/
50-
$(document).ajaxComplete(function (event, xhr, settings) {
51-
if (settings.url.includes("file/ajax")) {
52-
setTimeout(function () {
53-
if (Backdrop.settings.openaiAlt) {
54-
triggerAutoGenerationIfNeeded();
55-
}
56-
}, 500);
57-
}
58-
59-
// Handling response from alt text generation
60-
if (settings.url.includes("openai-alt/generate-alt-text")) {
61-
try {
62-
var response = JSON.parse(xhr.responseText);
33+
function generateAltText(fid, fieldName, delta) {
34+
$.ajax({
35+
url: Backdrop.settings.basePath + "openai-alt/generate-alt-text",
36+
type: "POST",
37+
data: { fid: fid, field_name: fieldName, delta: delta },
38+
success: function (response) {
6339
if (response.status === "success" && response.alt_text) {
64-
var $altField = $("input[name^='field_image'][name$='[alt]']");
65-
$altField.val(response.alt_text).trigger("change");
40+
// 1) Insert the result into the <input> for this field/delta.
41+
var selector = "input[name='" + fieldName + "[und][" + delta + "][alt]']";
42+
$(selector).val(response.alt_text).trigger("change");
6643

67-
// Ensure Backdrop recognizes the update
68-
setTimeout(function () {
69-
Backdrop.attachBehaviors();
70-
}, 500);
44+
// 2) Remove this item from openaiAlt so it won't auto-regenerate again.
45+
var key = fieldName + ":" + delta;
46+
if (Backdrop.settings.openaiAlt && Backdrop.settings.openaiAlt[key]) {
47+
delete Backdrop.settings.openaiAlt[key];
48+
}
7149
}
72-
} catch (e) {
73-
console.error("Error processing OpenAI alt text response:", e);
7450
}
51+
});
52+
}
53+
54+
/**
55+
* After any AJAX that includes "file/ajax" (i.e. new image added),
56+
* wait a moment, then try generating alt text for newly added items.
57+
*/
58+
$(document).ajaxComplete(function (event, xhr, settings) {
59+
if (settings.url.includes("file/ajax")) {
60+
setTimeout(triggerAutoGenerationForAll, 500);
7561
}
7662
});
7763

7864
/**
79-
* Handle image removal events.
65+
* Optional: If you remove an image, you might want to remove
66+
* that item from openaiAlt. Only do so if you have data attributes
67+
* for the remove button. Otherwise, leaving them won't matter
68+
* because we only run alt generation for items with an empty alt
69+
* (and that item is gone entirely anyway).
8070
*/
8171
$(document).on("click", ".file-remove-button", function () {
82-
Backdrop.settings.openaiAlt = null; // Reset settings to allow new uploads
72+
// For a multi-value field, you might parse the delta from $(this).data('delta').
73+
// For now, we can just do:
74+
// Backdrop.settings.openaiAlt = null;
75+
// But that nixes *all* items. So be careful.
8376
});
8477

85-
// **Run initial setup**
78+
// On page load, wrap all existing ALT fields, then generate for any that need it.
8679
wrapAltTextFields();
87-
triggerAutoGenerationIfNeeded(); // Ensure auto-generation is triggered if needed
80+
triggerAutoGenerationForAll();
8881
});
8982
})(jQuery);

modules/openai_alt/js/openai_alt_autogenerate_ckeditor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ $(document).ready(function () {
6262
});
6363

6464
function triggerAutoGenerate(fid, src, wrapperId) {
65-
/*$.ajax({
65+
$.ajax({
6666
url: Backdrop.settings.basePath + 'openai-alt/ckeditor-autogenerate',
6767
type: 'POST',
6868
data: {
@@ -81,5 +81,5 @@ function triggerAutoGenerate(fid, src, wrapperId) {
8181
error: function (xhr, status, error) {
8282
console.error("❌ AJAX request failed:", status, error);
8383
}
84-
});*/
84+
});
8585
}

modules/openai_alt/openai_alt.info

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,3 @@ configure = admin/config/openai/openai-alt
77
dependencies[] = openai
88
dpendencies[] = image_effects
99
dependencies[] = image_effects_coloractions
10-
11-
; Added by Backdrop CMS packaging script on 2025-01-12
12-
project = openai
13-
version = 1.x-1.0.2
14-
timestamp = 1736733485

0 commit comments

Comments
 (0)