Overhaul of theme

This commit is contained in:
Dan Chadwick
2024-08-25 12:32:35 -04:00
parent f63bdddde4
commit 7304c4b0d7
58 changed files with 818 additions and 569 deletions

View File

@@ -0,0 +1,5 @@
name: Auto Alt
description: 'AI generated alt text on media items.'
type: module
package: Media
core_version_requirement: ^10 || ^11

View File

@@ -0,0 +1,9 @@
auto_alt:
css:
theme:
css/styles.css: { }
js:
js/autoAlt.js: { }
dependencies:
- core/once
- core/drupal

View File

@@ -0,0 +1,12 @@
<?php
function auto_alt_form_alter(&$form, &$form_state, $form_id) {
if ($form_id !== 'media_image_edit_form') {
return;
}
$form['auto_alt'] = [
'#type' => 'markup',
'#markup' => '<span id="autoalt">🪄</span>'
];
$form['#attached']['library'][] = 'auto_alt/auto_alt';
}

View File

@@ -0,0 +1,7 @@
auto_alt.content:
path: '/ai/alt-text-generator'
defaults:
_controller: '\Drupal\auto_alt\Controller\AltTextGenerator::generate'
_title: ''
requirements:
_permission: 'access content'

View File

@@ -0,0 +1,19 @@
.form-managed-file__meta-items {
position: relative;
}
#autoalt {
position: absolute;
top: 46px;
right: 3px;
padding: 8px;
background: 'whitesmoke';
transform: translateY(-50%);
border-radius: 0 5px 5px 0;
cursor: pointer;
transition: background .2s ease;
}
#autoalt:hover {
background: lightgray;
}

View File

@@ -0,0 +1,32 @@
(function (Drupal, once) {
Drupal.behaviors.autoAltBehavior = {
attach: function (context, settings) {
once('autoAltBehavior', '#edit-field-media-image-0-alt', context).forEach(function (element) {
// Move the wand to where it should be.
let wand = context.getElementById("autoalt");
let mediaMeta = context.querySelector('.form-managed-file__meta-items');
mediaMeta.append(wand);
wand.addEventListener('click', generateAltText);
});
async function generateAltText() {
let imageUrl = document.querySelector('.image-preview__img-wrapper img').src;
const url = "/ai/alt-text-generator?";
try {
const response = await fetch(url + new URLSearchParams({
image: imageUrl,
}).toString());
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const json = await response.json();
let altInput = document.getElementById('edit-field-media-image-0-alt');
altInput.value = json;
} catch (error) {
console.error(error.message);
}
}
}
};
})(Drupal, once);

View File

@@ -0,0 +1,41 @@
<?php
namespace Drupal\auto_alt\Controller;
use \Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Generate Alt Text for an image.
*/
class AltTextGenerator extends ControllerBase {
/**
* Returns a renderable array for a test page.
*
* return []
*/
public function generate() {
// Get the url to the image from the request.
$image_url = \Drupal::request()->get('image');
if (!$image_url) {
throw new NotFoundHttpException();
}
$image_contents = file_get_contents($image_url);
$encoded_image = base64_encode($image_contents);
// Now send this to chat gpt / ai to generate alt text.
$response = [
"This is some alt text for an image.",
"A new string for alt text",
"Another string about alt text",
];
$text_to_return = rand(0,2);
$json_response = new JsonResponse();
$json_response->headers->set('Content-Type', 'application/json');
$json_response->setData($response[$text_to_return]);
return $json_response;
}
}