| 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 |
* Tiny AI base generate class.
|
|
|
18 |
*
|
|
|
19 |
* @module tiny_aiplacement/generatebase
|
|
|
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 {loadingMessages} from 'tiny_aiplacement/loading';
|
|
|
25 |
import {getString} from 'core/str';
|
|
|
26 |
import {
|
|
|
27 |
getContextId,
|
|
|
28 |
getUserId,
|
|
|
29 |
} from 'tiny_aiplacement/options';
|
|
|
30 |
import Policy from 'core_ai/policy';
|
|
|
31 |
import PolicyModal from 'core_ai/policymodal';
|
|
|
32 |
import CustomEvents from 'core/custom_interaction_events';
|
|
|
33 |
import {isPolicyAgreed} from './options';
|
|
|
34 |
|
|
|
35 |
export default class GenerateBase {
|
|
|
36 |
modalObject;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Class constructor.
|
|
|
40 |
*
|
|
|
41 |
* @param {TinyMCE.editor} editor The tinyMCE editor instance.
|
|
|
42 |
*/
|
|
|
43 |
constructor(editor) {
|
|
|
44 |
this.editor = editor;
|
|
|
45 |
this.userid = getUserId(editor);
|
|
|
46 |
this.contextid = getContextId(editor);
|
|
|
47 |
this.responseObj = null;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Display the modal when the AI button is clicked.
|
|
|
52 |
*
|
|
|
53 |
*/
|
|
|
54 |
async displayContentModal() {
|
|
|
55 |
Policy.preconfigurePolicyState(this.userid, isPolicyAgreed(this.editor));
|
|
|
56 |
if (!await Policy.getPolicyStatus(this.userid)) {
|
|
|
57 |
const policyModal = await PolicyModal.create();
|
|
|
58 |
policyModal.getModal().on(CustomEvents.events.activate, policyModal.getActionSelector('save'), () => {
|
|
|
59 |
this.displayContentModal();
|
|
|
60 |
});
|
|
|
61 |
return;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
this.modalObject = await this.setupModal();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
getModalClass() {
|
|
|
68 |
throw new Error("Method 'getModalClass' must be implemented.");
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Set up the base text generation modal with default body content.
|
|
|
74 |
*
|
|
|
75 |
* @returns {TextModal} The image modal object.
|
|
|
76 |
*/
|
|
|
77 |
async setupModal() {
|
|
|
78 |
const modal = this.getModalClass().create({
|
|
|
79 |
templateContext: {
|
|
|
80 |
elementid: this.editor.id,
|
|
|
81 |
},
|
|
|
82 |
});
|
|
|
83 |
|
|
|
84 |
this.addContentEventListeners(modal);
|
|
|
85 |
|
|
|
86 |
return modal;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Add event listeners for the text modal.
|
|
|
91 |
*
|
|
|
92 |
* @param {Modal} modal
|
|
|
93 |
*/
|
|
|
94 |
async addContentEventListeners(modal) {
|
|
|
95 |
const modalRoot = (await modal).getRoot();
|
|
|
96 |
const root = modalRoot[0];
|
|
|
97 |
|
|
|
98 |
root.addEventListener('click', (e) => {
|
|
|
99 |
this.handleContentModalClick(e, root);
|
|
|
100 |
});
|
|
|
101 |
|
|
|
102 |
this.setupPromptArea(root);
|
|
|
103 |
this.hideLoadingSpinner(root);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
handleContentModalClick() {
|
|
|
107 |
throw new Error('Method handleContentModalClick must be implemented.');
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Hide the loading spinner.
|
|
|
112 |
*
|
|
|
113 |
* @param {Object} root The root element of the modal.
|
|
|
114 |
*/
|
|
|
115 |
hideLoadingSpinner(root) {
|
|
|
116 |
const loadingSpinnerDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_spinner"]`);
|
|
|
117 |
loadingSpinnerDiv.classList.add('hidden');
|
|
|
118 |
loadingSpinnerDiv.classList.remove('tiny-aiplacement-loading-spinner-container');
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Display the loading state in the modal.
|
|
|
123 |
*
|
|
|
124 |
* @param {HTMLElement} root - The root element of the modal.
|
|
|
125 |
* @param {HTMLElement} submitBtn - The submit button element.
|
|
|
126 |
* @param {String|null} removeClass - The class to be removed from the loading spinner div, if any.
|
|
|
127 |
*/
|
|
|
128 |
async displayLoading(root, submitBtn, removeClass = null) {
|
|
|
129 |
const loadingSpinnerDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_spinner"]`);
|
|
|
130 |
const overlayDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_overlay"]`);
|
|
|
131 |
const blurDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_blur"]`);
|
|
|
132 |
const loadingTextDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_loading_text"]`);
|
|
|
133 |
const actionButtons = root.querySelectorAll('.tiny-aiplacement-generate-footer button');
|
|
|
134 |
|
|
|
135 |
loadingMessages(loadingTextDiv);
|
|
|
136 |
|
|
|
137 |
if (removeClass) {
|
|
|
138 |
loadingSpinnerDiv.classList.remove(removeClass);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
loadingSpinnerDiv.classList.remove('hidden');
|
|
|
142 |
overlayDiv.classList.remove('hidden');
|
|
|
143 |
blurDiv.classList.add('tiny-aiplacement-blur');
|
|
|
144 |
submitBtn.innerHTML = await getString('generating', 'tiny_aiplacement');
|
|
|
145 |
|
|
|
146 |
if (actionButtons) {
|
|
|
147 |
actionButtons.forEach((button) => {
|
|
|
148 |
button.disabled = true;
|
|
|
149 |
});
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Hide the loading action in the modal.
|
|
|
155 |
*
|
|
|
156 |
* @param {Object} root The root element of the modal.
|
|
|
157 |
* @param {Object} submitBtn The submit button element.
|
|
|
158 |
*/
|
|
|
159 |
async hideLoading(root, submitBtn) {
|
|
|
160 |
const loadingSpinnerDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_spinner"]`);
|
|
|
161 |
const overlayDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_overlay"]`);
|
|
|
162 |
const blurDiv = root.querySelector(`[id="${this.editor.id}_tiny_aiplacement_blur"]`);
|
|
|
163 |
const actionButtons = root.querySelectorAll('.tiny-aiplacement-generate-footer button');
|
|
|
164 |
if (loadingSpinnerDiv) {
|
|
|
165 |
loadingSpinnerDiv.classList.add('hidden');
|
|
|
166 |
}
|
|
|
167 |
if (overlayDiv) {
|
|
|
168 |
overlayDiv.classList.add('hidden');
|
|
|
169 |
}
|
|
|
170 |
if (blurDiv) {
|
|
|
171 |
blurDiv.classList.remove('tiny-aiplacement-blur');
|
|
|
172 |
}
|
|
|
173 |
submitBtn.innerHTML = await getString('regenerate', 'tiny_aiplacement');
|
|
|
174 |
|
|
|
175 |
if (actionButtons) {
|
|
|
176 |
actionButtons.forEach((button) => {
|
|
|
177 |
button.disabled = false;
|
|
|
178 |
});
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|