Skip to content

Commit

Permalink
fix: applying suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjmpb committed Sep 16, 2024
1 parent 57f2421 commit 72350de
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lms/static/js/edxnotes/plugins/llm_summarize.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@
transform: rotate(360deg);
}
`;
document.head.appendChild(style);
let annotator = this.annotator;
document.head.appendChild(style);
this.modifyDom(this.annotator);

const summarizeButton = document.getElementById('summarizeButton');

summarizeButton.addEventListener('click', function(ev) {
annotator.editor.element[0].setAttribute('is_summarizing', true);
});
Expand All @@ -96,10 +98,15 @@
},
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);

function toggleLoader() {
const saveButton = document.querySelector('.annotator-controls .annotator-save');
const loaderWrapper = document.querySelector('.summarize-loader-wrapper');
editor.fields[0].element.children[0].classList.toggle('d-none');
loaderWrapper.classList.toggle('d-none');
saveButton.disabled = !saveButton.disabled;
}
const textAreaWrapper = editor.fields[0].element;
const request = new Request('/pearson-core/llm-assistance/api/v0/summarize-text', {
method: 'POST',
headers: {
Expand All @@ -110,6 +117,9 @@
text_to_summarize: annotation.quote,
}),
});

editor.fields[1].element.children[0].value = 'ai_summary';
toggleLoader(editor);
fetch(request)
.then((response) => {
toggleLoader();
Expand All @@ -123,32 +133,26 @@
alert(error.message);
editor.hide();
});

function toggleLoader() {
const saveButton = document.querySelector('.annotator-controls .annotator-save');
const loaderWrapper = document.querySelector('.summarize-loader-wrapper');
editor.fields[0].element.children[0].classList.toggle('d-none');
loaderWrapper.classList.toggle('d-none');
saveButton.disabled = !saveButton.disabled;
}
},
cleanupSummarize: function(editor) {
const textAreaWrapper = editor.fields[0].element;
const loaderWrapper = document.querySelector('.summarize-loader-wrapper');

textAreaWrapper.children[0].value = '';
textAreaWrapper.children[1].value = '';
editor.element[0].setAttribute('is_summarizing', 'false');
const loaderWrapper = document.querySelector('.summarize-loader-wrapper');
loaderWrapper.classList.add('d-none');
},
modifyDom: function(annotator) {
const textAreaWrapper = annotator.editor.fields[0].element;

annotator.adder[0].children[0].id = 'annotateButton';
annotator.adder[0].children[0].innerHTML = '<i class="fa fa-pencil" aria-hidden="true"></i>';
annotator.adder[0].innerHTML += `
<button class="summarize-button" id="summarizeButton" title="${gettext('Summarize text using AI.')}">
<i class="fa fa-star" aria-hidden="true"></i>
</button>
`;
const textAreaWrapper = annotator.editor.fields[0].element;
// Style is being defined here since classes styling not working
// for this element.
textAreaWrapper.innerHTML += `
Expand Down

0 comments on commit 72350de

Please sign in to comment.