Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * AI Modal for Tiny.
18
 *
19
 * @module      tiny_aiplacement/mediaimage
20
 * @copyright   2024 Matt Porritt <matt.porritt@moodle.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import {getString} from 'core/str';
25
import MediaImage from 'tiny_media/image';
26
import Notification from 'core/notification';
27
import {prefetchStrings} from 'core/prefetch';
28
import {MAX_LENGTH_ALT} from 'tiny_media/imagehelpers';
29
 
30
prefetchStrings('core_ai', [
31
    'contentwatermark',
32
]);
33
 
34
export default class AiMediaImage extends MediaImage {
35
    constructor(editor, url, alt) {
36
        super(editor); // Call the parent class constructor
37
        this.generatedImageUrl = url;
38
        this.altText = alt;
39
        getString('contentwatermark', 'core_ai').then((watermark) => {
40
            this.watermark = watermark;
41
            return;
42
        }).catch(Notification.exception);
43
    }
44
 
45
    getSelectedImage() {
46
        const imgElement = document.createElement('img');
47
 
48
        // Set attributes for the img element
49
        imgElement.src = this.generatedImageUrl;
50
        imgElement.alt = this.truncateAltText(this.altText);
51
 
52
        return imgElement;
53
    }
54
 
55
    /**
56
     * Truncate the alt text if it is longer than the maximum length.
57
     * @param {String} altText The alt text
58
     * @return {string} The truncated alt text
59
     */
60
    truncateAltText(altText) {
61
        const maximumAltTextLength = MAX_LENGTH_ALT;
62
        const watermark = ' - ' + this.watermark;
63
        const ellipsis = '...';
64
 
65
        // Append the watermark to the alt text.
66
        if (altText.length + watermark.length <= maximumAltTextLength) {
67
            altText = altText + watermark;
68
        } else {
69
            const remainingLength = maximumAltTextLength - watermark.length - ellipsis.length;
70
            altText = altText.substring(0, remainingLength) + ellipsis + watermark;
71
        }
72
        return altText;
73
    }
74
}