Skip to content

Commit

Permalink
Process image and video on stream (#56)
Browse files Browse the repository at this point in the history
* asd

* something

* Video and image from stream implemented

* 1.0.45

* fix initial video does not show

* init media after the message
  • Loading branch information
tuseto authored Nov 7, 2023
1 parent 04aa321 commit 96ee039
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 50 deletions.
26 changes: 22 additions & 4 deletions __tests__/unit/chat-ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('ChatUi', () => {
test('that initial buttons are not appended from assistant if actions number is 10', () => {
const element = document.createElement('span');
element.appendChild = jest.fn();
sut.getLastMessageElement = jest.fn().mockReturnValue(element);
sut.getLastMessageElementConsistingMessage = jest.fn().mockReturnValue(element);
actionService.handleAction = jest.fn().mockReturnValue(document.createElement('div'));
sut.word = jest.fn();

Expand All @@ -224,7 +224,7 @@ describe('ChatUi', () => {
test('that initial buttons are appended from assistant if actions number is 10', () => {
const element = document.createElement('span');
element.appendChild = jest.fn();
sut.getLastMessageElement = jest.fn().mockReturnValue(element);
sut.getLastMessageElementConsistingMessage = jest.fn().mockReturnValue(element);
actionService.handleAction = jest.fn().mockReturnValue(document.createElement('div'));

sut.word = jest.fn();
Expand Down Expand Up @@ -476,7 +476,7 @@ describe('ChatUi', () => {

sut.historyTraverse([{ content: 'element1' }, { content: 'element2' }]);

expect(sut.initMedia).toBeCalledTimes(1);
expect(sut.initMedia).toBeCalledTimes(2);
expect(sut.appendHtml).toBeCalledTimes(2);
expect(sut.appendHtml).toBeCalledWith({ content: 'element1' }, false);
expect(sut.appendHtml).toBeCalledWith({ content: 'element2' }, true);
Expand All @@ -493,7 +493,7 @@ describe('ChatUi', () => {
expect(sut.appendHtml).toBeCalledTimes(1);
expect(sut.appendHtml).toBeCalledWith({ content: 'element2' }, true);
expect(sut.initNewLine).toBeCalledTimes(1);
expect(sut.initMedia).toBeCalledTimes(1);
expect(sut.initMedia).toBeCalledTimes(2);
});
});

Expand Down Expand Up @@ -725,5 +725,23 @@ describe('appendMedia calls the proper media markup', () => {
expect(imageMarkupSpy).toBeCalledTimes(1);
expect(videoMarkupSpy).not.toBeCalled();
});

test('set image fullscreen', () => {
document.body.innerHTML = '<img src="http://localhost/image-to-show" class="media-image"> <div class="fullscreen-background-filter"><img src="http://localhost/initial-src"></div>';

var fullScreenImageContainer = document.querySelector('.fullscreen-background-filter');
var fullScreenImage = fullScreenImageContainer.querySelector('img');

expect(fullScreenImageContainer.classList.contains("show-image")).toBeFalsy();
expect(fullScreenImage.src).toBe("http://localhost/initial-src")

document.querySelector('.media-image').click();

var fullScreenImageContainer = document.querySelector('.fullscreen-background-filter');
var fullScreenImage = fullScreenImageContainer.querySelector('img');

expect(fullScreenImageContainer.classList.contains("show-image")).toBeTruthy();
expect(fullScreenImage.src).toBe("http://localhost/image-to-show")
});
});

4 changes: 4 additions & 0 deletions __tests__/unit/socket-services.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('socket-services', () => {
getLastMessageElement: jest.fn().mockImplementationOnce(() => document.querySelector('div')),
processTextInCaseOfSquareBrackets: jest.fn(),
processTextInCaseOfCurlyBrackets: jest.fn(),
processStringInCaseOfAngleBrackets: jest.fn(),
elements: {
messageIncrementor: { appendChild: jest.fn() },
},
Expand Down Expand Up @@ -72,6 +73,7 @@ describe('socket-services', () => {
const state = {
setCtaButton: jest.fn(),
getLastMessageElement: ChatUi.getLastMessageElement,
getLastMessageElementConsistingMessage: ChatUi.getLastMessageElementConsistingMessage,
elements: { messageIncrementor: document.querySelector('#message-incrementor') },
};
jest.spyOn(input, 'show');
Expand Down Expand Up @@ -114,6 +116,7 @@ describe('socket-services', () => {
const state = {
setCtaButton: jest.fn(),
getLastMessageElement: ChatUi.getLastMessageElement,
getLastMessageElementConsistingMessage: ChatUi.getLastMessageElementConsistingMessage,
elements: {
messageIncrementor: document.querySelector('#message-incrementor'),
promptContainer: document.querySelector('#prompt-container'),
Expand Down Expand Up @@ -163,6 +166,7 @@ describe('socket-services', () => {
const state = {
setCtaButton: jest.fn(),
getLastMessageElement: ChatUi.getLastMessageElement,
getLastMessageElementConsistingMessage: ChatUi.getLastMessageElementConsistingMessage,
elements: {
messageIncrementor: document.querySelector('#message-incrementor'),
promptContainer: document.querySelector('#prompt-container'),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "1ff-chat-ui",
"version": "1.0.44",
"version": "1.0.45",
"description": "chatbot to communicate with taught ai",
"main": "src/lib/chat-ui.js",
"scripts": {
Expand Down
85 changes: 43 additions & 42 deletions src/lib/chat-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ const ChatUi = {
this.setDomContent();
this.setSocket();
this.setIntentionEvents();
this.setImageFullScreen();
},
setImageFullScreen() {
document.querySelector('body').addEventListener('click', (e) => {
console.log('document', document);
if (e.target.classList.contains('media-image')) {
const fullScreen = document.querySelector('.fullscreen-background-filter');
fullScreen.querySelector('img').src = e.target.src;
fullScreen.classList.toggle('show-image');
}
});
},
setIntentionEvents() {
intentions.on(intentionType.emailError, (response) => {
Expand Down Expand Up @@ -222,7 +233,6 @@ const ChatUi = {
historyTraverse(history) {
let counter = 1;
let isLastMessage = false;

history.forEach((data) => {
let appended = false;

Expand All @@ -235,20 +245,19 @@ const ChatUi = {
isLastMessage = true;
}
if (counter === 1) {
this.initMedia(data.content);
if (data.content.includes('^')) {
appended = true;
this.initMedia(data.content);
this.initNewLine(data, isLastMessage);
}
}
if (!appended) {
this.appendHtml(data, isLastMessage);
this.initMedia(data.content);
}

counter++;
});

this.addImageAction();
},
initNewLine(data, isLastMessage) {
const splittedContent = splitText(data.content, '^');
Expand All @@ -262,18 +271,8 @@ const ChatUi = {
this.appendHtml(newData, isLastMessage);
}
},
addImageAction() {
const images = document.querySelectorAll('.media-image');
images.forEach((img) => {
img.addEventListener('click', () => {
const fullScreen = document.querySelector('.fullscreen-background-filter');
fullScreen.classList.toggle('show-image');
});
});
},
initMedia(content) {
const link = getStringInAngleBrackets(content);

const extractedLink = link[0];
if (extractedLink) {
this.appendMedia(extractedLink);
Expand Down Expand Up @@ -387,6 +386,19 @@ const ChatUi = {
this.boldedText = '';
}
},
processStringInCaseOfAngleBrackets() {
if (this.chunk.includes('<')) {
this.answersFromStream = this.chunk;
}

if (this.answersFromStream.includes('>')) {
this.answersFromStream = this.answersFromStream.replace('<', '').replace('>', '');
console.log('this.answersFromStream', this.answersFromStream);
this.appendMedia(this.answersFromStream);
this.answersFromStream = '';
this.chunk = '';
}
},
refreshLocalStorageHistory(history) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(history));
},
Expand All @@ -412,7 +424,7 @@ const ChatUi = {
}
},
addOptions() {
const element = this.getLastMessageElement('.assistant');
const element = this.getLastMessageElementConsistingMessage('.assistant');
const answerConfig = getAnswerConfig(this.answersFromStream);
const answersContainer = document.createElement('div');
const moveBtnNumber = '10';
Expand All @@ -438,6 +450,21 @@ const ChatUi = {
this.elements.messageIncrementor.querySelectorAll(role).length - 1
];
},
getLastMessageElementConsistingMessage(role) {
const parentElements = this.elements.messageIncrementor.querySelectorAll(role);
var lastElement = null;

for (let i = parentElements.length - 1; i >= 0; i--) {
const parent = parentElements[i];
const child1 = parent.querySelector('.js-assistant-message');
if (child1) {
lastElement = parent;
break; // Exit the loop as soon as a match is found
}
}

return lastElement;
},
isFirstUserMessage() {
let history = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];

Expand Down Expand Up @@ -602,7 +629,7 @@ const ChatUi = {
this.appendHtml(data);
}

this.addImageAction();
// this.addImageAction();
if (extractedString) {
this.hideInput(extractedString);
}
Expand All @@ -626,31 +653,6 @@ const ChatUi = {
}
},

/**
* Shows the image on full screen on click
*/
fullScreenImage() {
const link = getStringInAngleBrackets(this.assistant.initialMessage.content);
const extractedLink = link[0];
const img = document.createElement('img');
const background = document.createElement('div');
const imgWrapper = document.createElement('div');
const closeMark = document.createElement('span');

closeMark.innerHTML = `&times`;
img.classList.add('media-image');
imgWrapper.classList.add('fullscreen-image-wrapper');
background.classList.add('fullscreen-background-filter');
closeMark.classList.add('close-mark');

img.src = `${extractedLink}`;
imgWrapper.appendChild(img);
imgWrapper.appendChild(closeMark);
background.appendChild(imgWrapper);
this.elements.chatbotContainer.appendChild(background);
closeMark.addEventListener('click', () => background.classList.remove('show-image'));
},

/**
*
* @param {*} extractedLink
Expand All @@ -663,7 +665,6 @@ const ChatUi = {
mediaBody.appendChild(videoMarkup(extractedLink));
} else {
mediaBody.appendChild(imageMarkup(extractedLink));
this.fullScreenImage();
}
this.elements.messageIncrementor.appendChild(mediaBody);
},
Expand Down
3 changes: 3 additions & 0 deletions src/lib/chat-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const chatMarkup = (config) => `<div class="chat-widget">
</div>
</div>
</div>
${imageFullscreen}
</div>`;

export const initiatorProfile = (config) => {
Expand Down Expand Up @@ -147,6 +148,8 @@ export const paymentButton = (translations) => `<button id="chat-pay" class="js-
<span class="payment-button__text">${translations.payButton}</span>
</button>`;

export const imageFullscreen = `<div class="fullscreen-background-filter"><div class="fullscreen-image-wrapper"><img class="media-image" src="https://www.worldatlas.com/upload/cf/93/4b/shutterstock-1385689649.jpg"><span class="close-mark">×</span></div></div>`

export const closePaymentFormButton = `<span id="payment-form-close-button" class="close-payment-form hidden">
<svg
fill="none"
Expand Down
3 changes: 2 additions & 1 deletion src/lib/socket-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export function onStreamData(data) {
this.chunk = clearCarets(data.chunk);
this.processTextInCaseOfSquareBrackets();
this.processTextInCaseOfCurlyBrackets();
this.processStringInCaseOfAngleBrackets();

!this.answersFromStream && (lastMessageElement.innerHTML += this.chunk);
lastMessageElement.addClass('cursor');
Expand All @@ -122,7 +123,7 @@ export function onStreamData(data) {
*/
export function onStreamEnd() {
window.debugMode && console.log('stream-end');
const lastMessageElement = this.getLastMessageElement('.assistant');
const lastMessageElement = this.getLastMessageElementConsistingMessage('.assistant');
const lastMessageTextContainer = lastMessageElement.querySelector('.js-assistant-message');

// in case stream ended with empty message, remove it from the DOM.
Expand Down
1 change: 1 addition & 0 deletions src/lib/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ img {
.close-mark{
position: absolute;
pointer-events: none;
top: 20px;
right: 35px;
font-size: 32px;
Expand Down

0 comments on commit 96ee039

Please sign in to comment.