Skip to content

Commit

Permalink
style: expand annotation textarea and apply responsiveness for differ…
Browse files Browse the repository at this point in the history
…ent devices.
  • Loading branch information
Serafin-dev committed Oct 25, 2024
1 parent cdba011 commit 2df1983
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions lms/static/js/edxnotes/plugins/llm_summarize.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
// Overrides of annotatorjs HTML/CSS to add summarize button.
const style = document.createElement('style');
style.innerHTML = `
form.annotator-widget {
width: fit-content;
}
@media (max-width: 768px) {
.annotator-invert-x .annotator-widget{
left: 0px !important;
}
.annotator-outer {
left: 10px !important;
}
.annotator-widget textarea{
max-height: 380px;
}
}
.annotator-adder::before {
content: '';
width: 10px;
Expand Down Expand Up @@ -106,6 +122,8 @@
loaderWrapper.classList.toggle('d-none');
saveButton.disabled = !saveButton.disabled;
}

const container = document.querySelector('#main');
const textAreaWrapper = editor.fields[0].element;
const request = new Request('/pearson-core/llm-assistance/api/v0/summarize-text', {
method: 'POST',
Expand All @@ -122,27 +140,61 @@
editor.fields[1].element.children[0].value = 'ai_summary';
toggleLoader(editor);
fetch(request)
.then((response) => {
toggleLoader();
if (response.ok) return response.json();
throw new Error(gettext('There was an error while summarizing the content.'));
})
.then((data) => {
textAreaWrapper.children[0].value = data.summary;
})
.catch((error) => {
alert(error.message);
editor.hide();
.then((response) => {
toggleLoader();
if (response.ok) return response.json();
throw new Error(gettext('There was an error while summarizing the content.'));
})
.then((data) => {
const annotatorEditor = editor.element[0]
const annotatorForm = annotatorEditor.children[0]
const containerWidth = container.offsetWidth;
const containerPadding = window.getComputedStyle(container).getPropertyValue('padding');
const controlsHeight = annotatorForm.children[1].offsetHeight
const editorLeft = parseInt(annotatorEditor.style.getPropertyValue('left'))
const editorTop = parseInt(annotatorEditor.style.getPropertyValue('top'))
const tagFieldHeight = editor.fields[1].element.offsetHeight
const textArea = textAreaWrapper.children[0]

textArea.value = data.summary;
textArea.setAttribute('style', `
background-color: #f7f7f7 !important;
border-radius: 5px;
font-size: 12px !important;
width: ${containerWidth - editorLeft - parseInt(containerPadding)}px !important;
height: auto;
overflow-y: auto;
`);

textArea.style.height = `${textArea.scrollHeight}px`;

if (annotatorForm.offsetHeight > editorTop){
textArea.style.maxHeight = `${editorTop - controlsHeight - tagFieldHeight - 10}px`;
}
textArea.setAttribute('tabindex', '-1');
textArea.scrollIntoView({behavior: 'smooth', block: 'start'});

const resizeObserver = new ResizeObserver(() => {
const containerWidth = container.offsetWidth;
textArea.style.width = `${containerWidth - editorLeft - parseInt(containerPadding)}px`;
});
resizeObserver.observe(container);
})
.catch((error) => {
alert(error.message);
editor.hide();
});
},
cleanupSummarize: function(editor) {
const textAreaWrapper = editor.fields[0].element;
const textArea = textAreaWrapper.children[0]
const loaderWrapper = document.querySelector('.summarize-loader-wrapper');

textAreaWrapper.children[0].value = '';
textArea.value = '';
textAreaWrapper.children[1].value = '';
editor.options.isSummarizing = false;
loaderWrapper.classList.add('d-none');
textArea.removeAttribute('style');
},
modifyDom: function(annotator) {
const textAreaWrapper = annotator.editor.fields[0].element;
Expand Down

0 comments on commit 2df1983

Please sign in to comment.