Feature/#91 watch2gether display chat message character count #93

Merged
Serraniel merged 12 commits from feature/#91-watch2gether-display-chat-message-character-count into develop 2020-10-29 18:09:12 +01:00
Showing only changes of commit 542d328323 - Show all commits

View file

@ -4,18 +4,20 @@ import { v4 as uuidv4 } from 'uuid';
export function init() { export function init() {
// UPS // runAfterLoad is not what we want...wait for runAfterLocationChange.... // UPS // runAfterLoad is not what we want...wait for runAfterLocationChange....
Serraniel commented 2020-10-25 14:00:01 +01:00 (Migrated from github.com)
Review
  • This is blocked until the changes made in #19 are merged.

However, the rest of the changes should be ready for a first review. But it may be possible this code doesn´t work yet as I cannot test it in the current state.

- [x] This is blocked until the changes made in #19 are merged. However, the rest of the changes should be ready for a first review. But it may be possible this code doesn´t work yet as I cannot test it in the current state.
Serraniel commented 2020-10-28 22:36:42 +01:00 (Migrated from github.com)
Review

Blocking item was merges.

Blocking item was merges.
core.runAfterLoad(() => { core.runAfterLocationChange(() => {
manipulateChatInput(); manipulateChatInput();
}, "^/watch2gether/.*$"); }, "^/watch2gether/.*$");
} }
function manipulateChatInput() { function manipulateChatInput() {
let textarea = document.querySelector('.chat-input textarea'); let textarea = document.querySelector('.chat-input textarea');
// avoid duplicate registration // avoid duplicate registration
if (typeof textarea.dataset.charCounterId !== 'undefined') { if (typeof textarea.dataset.charCounterId !== 'undefined') {
return; return;
} }
addCharCounter(textarea);
} }
function addCharCounter(textarea) { function addCharCounter(textarea) {
@ -23,23 +25,39 @@ function addCharCounter(textarea) {
let controlRow = chatDiv.children[1]; // row with controls let controlRow = chatDiv.children[1]; // row with controls
let btn = controlRow.querySelector('button'); // find send button let btn = controlRow.querySelector('button'); // find send button
let counterSpan = document.createElement('span'); // create span for counter let charCounterSpan = document.createElement('span'); // create span for counter
counterSpan.classList.add('awp-w2g-chatCounter'); charCounterSpan.classList.add('awp-w2g-chatCounter');
// id and "connection" // id and "connection"
let counterId = `awp-${v4()}` let counterId = `awp-${uuidv4()}`
counterSpan.id = counterId; charCounterSpan.id = counterId;
textarea.dataset.charCounterId = counterId; textarea.dataset.charCounterId = counterId;
btn.parentElement.inserBefore(counterSpan, btn); // and insert in front of the button btn.parentElement.insertBefore(charCounterSpan, btn); // and insert in front of the button
updateCharCounter(textarea, charCounterSpan);
textarea.addEventListener('keypress keyup', () => { textarea.addEventListener('keyup', () => {
let current = textarea.value.length; console.log('TRIGGER')
let max = textarea.maxLength; updateCharCounter(textarea, charCounterSpan)
counterSpan.innerText = `${current} / ${max}`;
// animation if at max
counterSpan.classList.toggle('awp-w2g-chatCounter-max', current >= max);
}); });
}
function updateCharCounter(textarea, charCounterSpan) {
const SHAKE_CLASS = 'awp-w2g-chatCounter-max';
// reset class
charCounterSpan.classList.remove(SHAKE_CLASS);
let current = textarea.value.length;
let max = textarea.maxLength;
charCounterSpan.innerText = `${current} / ${max}`;
// animation if at max
// this need to be delayed because removing and adding it too fast again will prevent the browsers to replay the animation
if (current >= max) {
setTimeout(() => {
charCounterSpan.classList.add(SHAKE_CLASS);
}, 100);
}
} }