diff --git a/lms/static/js/edxnotes/plugins/llm_summarize.js b/lms/static/js/edxnotes/plugins/llm_summarize.js index 6f812511118b..c8d7e83bc143 100644 --- a/lms/static/js/edxnotes/plugins/llm_summarize.js +++ b/lms/static/js/edxnotes/plugins/llm_summarize.js @@ -68,9 +68,86 @@ background-color: white; border: 1px solid gray; } + + .summarize-loader-wrapper { + display: none; + } + + .summarize-loader-wrapper.show { + display: block + } + // Defining content loader since fontawesome icons don't work + // inside the annotatorjs modal. + .loader { + width: 5em !important; + border-color: red !important; + } + @keyframes rotation { + 0% { + transform: rotate(0deg); + } + 100% { + transform: rotate(360deg); + } `; document.head.appendChild(style); - var annotator = this.annotator; + this.modifyDom(this.annotator); + const summarizeButton = document.getElementById('summarizeButton'); + summarizeButton.addEventListener('click', function(ev) { + document.querySelector('.annotator-outer.annotator-editor').setAttribute('is_summarizing', true); + }); + this.annotator.subscribe('annotationEditorShown', this.handleSummarize); + this.annotator.subscribe('annotationEditorHidden', this.cleanupSummarize); + }, + handleSummarize: function (editor, annotation) { + if (editor.element[0].getAttribute('is_summarizing') !== 'true') return; + const textAreaWrapper = editor.fields[0].element; + editor.fields[1].element.children[0].value = 'ai_summary'; + toggleLoader(editor); + + const request = new Request("/pearson-core/llm-assistance/api/v0/summarize-text", { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': $.cookie('csrftoken') + }, + body: JSON.stringify({ + text_to_summarize: annotation.quote + }) + }); + fetch(request) + .then((response) => { + if (response.ok) return response.json(); + throw new Error(gettext('There was an error while summarizing the content.')); + }) + .then((data) => { + toggleLoader(); + textAreaWrapper.children[0].value = data.summary; + }) + .catch((error) => { + console.error('Error during summarization:', error); + editor.hide(); + }); + + function toggleLoader() { + const saveButton = document.querySelector('.annotator-controls .annotator-save'); + const loaderWrapper = document.querySelector('.summarize-loader-wrapper'); + const textArea = editor.fields[0].element.children[0]; + if (textArea.style.display === 'none') textArea.style.display = 'block'; + else textArea.style.display = 'none'; + loaderWrapper.classList.toggle('show'); + saveButton.disabled = !saveButton.disabled; + } + }, + cleanupSummarize: function(editor) { + const textAreaWrapper = editor.fields[0].element; + textAreaWrapper.children[0].value = ''; + textAreaWrapper.children[1].value = ''; + editor.element[0].setAttribute('is_summarizing', 'false'); + const loaderWrapper = document.querySelector('.summarize-loader-wrapper'); + loaderWrapper.classList.remove('show'); + }, + modifyDom: function(annotator) { annotator.adder[0].children[0].id = 'annotateButton'; annotator.adder[0].children[0].innerHTML = ''; annotator.adder[0].innerHTML += ` @@ -78,7 +155,26 @@ `; - }, + const textAreaWrapper = annotator.editor.fields[0].element; + textAreaWrapper.innerHTML += ` +
+ + + + ${gettext('Summarizing...')} + +
`; + } }); }); }).call(this, define || RequireJS.define);