2020-08-14 14:19:19 +03:00
|
|
|
import { h, Component, createRef } from 'https://unpkg.com/preact?module';
|
|
|
|
import htm from 'https://unpkg.com/htm?module';
|
|
|
|
const html = htm.bind(h);
|
|
|
|
|
2020-09-26 05:34:53 +03:00
|
|
|
import { EmojiButton } from 'https://unpkg.com/@joeattardi/emoji-button@4.2.0/dist/index.js';
|
2020-09-22 06:11:09 +03:00
|
|
|
import ContentEditable, { replaceCaret } from './content-editable.js';
|
2020-08-26 21:56:01 +03:00
|
|
|
import { generatePlaceholderText, getCaretPosition, convertToText, convertOnPaste } from '../../utils/chat.js';
|
2020-09-22 06:11:09 +03:00
|
|
|
import { getLocalStorage, setLocalStorage, classNames } from '../../utils/helpers.js';
|
|
|
|
import {
|
|
|
|
URL_CUSTOM_EMOJIS,
|
|
|
|
KEY_CHAT_FIRST_MESSAGE_SENT,
|
|
|
|
CHAT_MAX_MESSAGE_LENGTH,
|
|
|
|
CHAT_CHAR_COUNT_BUFFER,
|
|
|
|
CHAT_OK_KEYCODES,
|
|
|
|
CHAT_KEY_MODIFIERS,
|
|
|
|
} from '../../utils/constants.js';
|
2020-08-17 11:12:21 +03:00
|
|
|
|
2020-08-14 14:19:19 +03:00
|
|
|
export default class ChatInput extends Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this.formMessageInput = createRef();
|
2020-08-19 10:16:35 +03:00
|
|
|
this.emojiPickerButton = createRef();
|
2020-08-14 14:19:19 +03:00
|
|
|
|
|
|
|
this.messageCharCount = 0;
|
|
|
|
|
|
|
|
this.emojiPicker = null;
|
|
|
|
|
|
|
|
this.prepNewLine = false;
|
2020-09-22 06:11:09 +03:00
|
|
|
this.modifierKeyPressed = false; // control/meta/shift/alt
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-16 04:28:29 +03:00
|
|
|
this.state = {
|
2020-08-17 11:12:21 +03:00
|
|
|
inputHTML: '',
|
2020-09-22 06:11:09 +03:00
|
|
|
inputText: '', // for counting
|
|
|
|
inputCharsLeft: CHAT_MAX_MESSAGE_LENGTH,
|
2020-08-16 04:28:29 +03:00
|
|
|
hasSentFirstChatMessage: getLocalStorage(KEY_CHAT_FIRST_MESSAGE_SENT),
|
|
|
|
};
|
|
|
|
|
2020-08-14 14:19:19 +03:00
|
|
|
this.handleEmojiButtonClick = this.handleEmojiButtonClick.bind(this);
|
|
|
|
this.handleEmojiSelected = this.handleEmojiSelected.bind(this);
|
|
|
|
this.getCustomEmojis = this.getCustomEmojis.bind(this);
|
|
|
|
|
|
|
|
this.handleMessageInputKeydown = this.handleMessageInputKeydown.bind(this);
|
|
|
|
this.handleMessageInputKeyup = this.handleMessageInputKeyup.bind(this);
|
|
|
|
this.handleMessageInputBlur = this.handleMessageInputBlur.bind(this);
|
2020-08-16 04:28:29 +03:00
|
|
|
this.handleSubmitChatButton = this.handleSubmitChatButton.bind(this);
|
|
|
|
this.handlePaste = this.handlePaste.bind(this);
|
2020-08-17 11:12:21 +03:00
|
|
|
|
|
|
|
this.handleContentEditableChange = this.handleContentEditableChange.bind(this);
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.getCustomEmojis();
|
|
|
|
}
|
|
|
|
|
|
|
|
getCustomEmojis() {
|
|
|
|
fetch(URL_CUSTOM_EMOJIS)
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network response was not ok ${response.ok}`);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(json => {
|
|
|
|
this.emojiPicker = new EmojiButton({
|
|
|
|
zIndex: 100,
|
2020-09-22 06:11:09 +03:00
|
|
|
theme: 'owncast', // see chat.css
|
2020-08-14 14:19:19 +03:00
|
|
|
custom: json,
|
|
|
|
initialCategory: 'custom',
|
|
|
|
showPreview: false,
|
2020-09-22 06:11:09 +03:00
|
|
|
emojiSize: '24px',
|
2020-08-24 05:37:06 +03:00
|
|
|
position: 'right-start',
|
|
|
|
strategy: 'absolute',
|
2020-08-14 14:19:19 +03:00
|
|
|
});
|
|
|
|
this.emojiPicker.on('emoji', emoji => {
|
|
|
|
this.handleEmojiSelected(emoji);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
// this.handleNetworkingError(`Emoji Fetch: ${error}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEmojiButtonClick() {
|
|
|
|
if (this.emojiPicker) {
|
2020-08-19 10:16:35 +03:00
|
|
|
this.emojiPicker.togglePicker(this.emojiPickerButton.current);
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEmojiSelected(emoji) {
|
2020-08-17 11:12:21 +03:00
|
|
|
const { inputHTML } = this.state;
|
2020-08-16 04:28:29 +03:00
|
|
|
let content = '';
|
2020-08-14 14:19:19 +03:00
|
|
|
if (emoji.url) {
|
|
|
|
const url = location.protocol + "//" + location.host + "/" + emoji.url;
|
|
|
|
const name = url.split('\\').pop().split('/').pop();
|
2020-08-16 04:28:29 +03:00
|
|
|
content = "<img class=\"emoji\" alt=\"" + name + "\" src=\"" + url + "\"/>";
|
2020-08-14 14:19:19 +03:00
|
|
|
} else {
|
2020-08-16 04:28:29 +03:00
|
|
|
content = emoji.emoji;
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
2020-08-16 04:28:29 +03:00
|
|
|
|
2020-08-17 11:12:21 +03:00
|
|
|
this.setState({
|
|
|
|
inputHTML: inputHTML + content,
|
|
|
|
});
|
2020-09-22 06:11:09 +03:00
|
|
|
// a hacky way add focus back into input field
|
|
|
|
setTimeout( () => {
|
|
|
|
const input = this.formMessageInput.current;
|
|
|
|
input.focus();
|
|
|
|
replaceCaret(input);
|
|
|
|
}, 100);
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// autocomplete user names
|
2020-08-26 10:51:40 +03:00
|
|
|
autoCompleteNames() {
|
2020-08-14 14:19:19 +03:00
|
|
|
const { chatUserNames } = this.props;
|
2020-08-26 10:51:40 +03:00
|
|
|
const { inputHTML } = this.state;
|
|
|
|
const position = getCaretPosition(this.formMessageInput.current);
|
|
|
|
const at = inputHTML.lastIndexOf('@', position - 1);
|
|
|
|
if (at === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
let partial = inputHTML.substring(at + 1, position).trim();
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
if (partial === this.suggestion) {
|
|
|
|
partial = this.partial;
|
|
|
|
} else {
|
|
|
|
this.partial = partial;
|
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
const possibilities = chatUserNames.filter(function (username) {
|
|
|
|
return username.toLowerCase().startsWith(partial.toLowerCase());
|
|
|
|
});
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
if (this.completionIndex === undefined || ++this.completionIndex >= possibilities.length) {
|
|
|
|
this.completionIndex = 0;
|
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
if (possibilities.length > 0) {
|
|
|
|
this.suggestion = possibilities[this.completionIndex];
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-17 11:12:21 +03:00
|
|
|
this.setState({
|
|
|
|
inputHTML: inputHTML.substring(0, at + 1) + this.suggestion + ' ' + inputHTML.substring(position),
|
|
|
|
})
|
2020-08-26 10:51:40 +03:00
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
return true;
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMessageInputKeydown(event) {
|
2020-08-16 04:28:29 +03:00
|
|
|
const formField = this.formMessageInput.current;
|
2020-08-17 11:12:21 +03:00
|
|
|
|
|
|
|
let textValue = formField.innerText.trim(); // get this only to count chars
|
2020-09-22 06:11:09 +03:00
|
|
|
const newStates = {};
|
|
|
|
let numCharsLeft = CHAT_MAX_MESSAGE_LENGTH - textValue.length;
|
|
|
|
const key = event && event.key;
|
2020-08-26 10:51:40 +03:00
|
|
|
|
|
|
|
if (key === 'Enter') {
|
|
|
|
if (!this.prepNewLine) {
|
|
|
|
this.sendMessage();
|
|
|
|
event.preventDefault();
|
|
|
|
this.prepNewLine = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 06:11:09 +03:00
|
|
|
// allow key presses such as command/shift/meta, etc even when message length is full later.
|
|
|
|
if (CHAT_KEY_MODIFIERS.includes(key)) {
|
|
|
|
this.modifierKeyPressed = true;
|
|
|
|
}
|
2020-08-26 10:51:40 +03:00
|
|
|
if (key === 'Control' || key === 'Shift') {
|
|
|
|
this.prepNewLine = true;
|
|
|
|
}
|
|
|
|
if (key === 'Tab') {
|
|
|
|
if (this.autoCompleteNames()) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// value could have been changed, update char count
|
|
|
|
textValue = formField.innerText.trim();
|
2020-09-22 06:11:09 +03:00
|
|
|
numCharsLeft = CHAT_MAX_MESSAGE_LENGTH - textValue.length;
|
2020-08-26 10:51:40 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-09-22 06:11:09 +03:00
|
|
|
if (numCharsLeft <= 0 && !CHAT_OK_KEYCODES.includes(key)) {
|
|
|
|
newStates.inputText = textValue;
|
|
|
|
this.setState(newStates);
|
|
|
|
if (!this.modifierKeyPressed) {
|
2020-08-26 10:51:40 +03:00
|
|
|
event.preventDefault(); // prevent typing more
|
|
|
|
}
|
2020-09-22 06:11:09 +03:00
|
|
|
return;
|
2020-08-26 10:51:40 +03:00
|
|
|
}
|
2020-09-22 06:11:09 +03:00
|
|
|
newStates.inputText = textValue;
|
|
|
|
this.setState(newStates);
|
2020-08-26 10:51:40 +03:00
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
handleMessageInputKeyup(event) {
|
2020-09-22 06:11:09 +03:00
|
|
|
const formField = this.formMessageInput.current;
|
|
|
|
const textValue = formField.innerText.trim(); // get this only to count chars
|
|
|
|
|
|
|
|
const { key } = event;
|
|
|
|
|
|
|
|
if (key === 'Control' || key === 'Shift') {
|
2020-08-26 10:57:15 +03:00
|
|
|
this.prepNewLine = false;
|
2020-08-26 10:51:40 +03:00
|
|
|
}
|
2020-09-22 06:11:09 +03:00
|
|
|
if (CHAT_KEY_MODIFIERS.includes(key)) {
|
|
|
|
this.modifierKeyPressed = false;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
inputCharsLeft: CHAT_MAX_MESSAGE_LENGTH - textValue.length,
|
|
|
|
});
|
2020-08-26 10:51:40 +03:00
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
2020-08-26 10:51:40 +03:00
|
|
|
handleMessageInputBlur(event) {
|
|
|
|
this.prepNewLine = false;
|
2020-09-22 06:11:09 +03:00
|
|
|
this.modifierKeyPressed = false;
|
2020-08-14 14:19:19 +03:00
|
|
|
}
|
|
|
|
|
2020-08-16 04:28:29 +03:00
|
|
|
handlePaste(event) {
|
2020-09-22 06:11:09 +03:00
|
|
|
// don't allow paste if too much text already
|
|
|
|
if (CHAT_MAX_MESSAGE_LENGTH - this.state.inputText.length < 0) {
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
2020-08-26 21:56:01 +03:00
|
|
|
convertOnPaste(event);
|
2020-09-22 06:11:09 +03:00
|
|
|
this.handleMessageInputKeydown(event);
|
2020-08-16 04:28:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmitChatButton(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.sendMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage() {
|
|
|
|
const { handleSendMessage } = this.props;
|
2020-09-22 06:11:09 +03:00
|
|
|
const { hasSentFirstChatMessage, inputHTML, inputText } = this.state;
|
|
|
|
if (CHAT_MAX_MESSAGE_LENGTH - inputText.length < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-26 21:56:01 +03:00
|
|
|
const message = convertToText(inputHTML);
|
2020-08-16 04:28:29 +03:00
|
|
|
const newStates = {
|
2020-08-17 11:12:21 +03:00
|
|
|
inputHTML: '',
|
2020-09-22 06:11:09 +03:00
|
|
|
inputText: '',
|
|
|
|
inputCharsLeft: CHAT_MAX_MESSAGE_LENGTH,
|
2020-08-16 04:28:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
handleSendMessage(message);
|
|
|
|
|
|
|
|
if (!hasSentFirstChatMessage) {
|
|
|
|
newStates.hasSentFirstChatMessage = true;
|
|
|
|
setLocalStorage(KEY_CHAT_FIRST_MESSAGE_SENT, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear things out.
|
|
|
|
this.setState(newStates);
|
2020-08-17 11:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleContentEditableChange(event) {
|
|
|
|
this.setState({ inputHTML: event.target.value });
|
2020-08-16 04:28:29 +03:00
|
|
|
}
|
2020-08-14 14:19:19 +03:00
|
|
|
|
|
|
|
render(props, state) {
|
2020-09-22 06:11:09 +03:00
|
|
|
const { hasSentFirstChatMessage, inputCharsLeft, inputHTML } = state;
|
2020-08-16 04:28:29 +03:00
|
|
|
const { inputEnabled } = props;
|
2020-08-14 14:19:19 +03:00
|
|
|
const emojiButtonStyle = {
|
2020-09-22 06:11:09 +03:00
|
|
|
display: this.emojiPicker && inputCharsLeft > 0 ? 'block' : 'none',
|
2020-08-14 14:19:19 +03:00
|
|
|
};
|
2020-09-22 06:11:09 +03:00
|
|
|
const extraClasses = classNames({
|
|
|
|
'display-count': inputCharsLeft <= CHAT_CHAR_COUNT_BUFFER,
|
|
|
|
});
|
2020-08-16 04:28:29 +03:00
|
|
|
const placeholderText = generatePlaceholderText(inputEnabled, hasSentFirstChatMessage);
|
2020-08-14 14:19:19 +03:00
|
|
|
return (
|
|
|
|
html`
|
2020-09-22 06:11:09 +03:00
|
|
|
<div id="message-input-container" class="relative shadow-md bg-gray-900 border-t border-gray-700 border-solid p-4 z-20 ${extraClasses}">
|
|
|
|
|
|
|
|
<div
|
|
|
|
id="message-input-wrap"
|
|
|
|
class="flex flex-row justify-end appearance-none w-full bg-gray-200 border border-black-500 rounded py-2 px-2 pr-12 my-2 overflow-auto">
|
|
|
|
<${ContentEditable}
|
|
|
|
id="message-input"
|
|
|
|
class="appearance-none block w-full bg-transparent text-sm text-gray-700 h-full focus:outline-none"
|
|
|
|
|
|
|
|
placeholderText=${placeholderText}
|
|
|
|
innerRef=${this.formMessageInput}
|
|
|
|
html=${inputHTML}
|
|
|
|
disabled=${!inputEnabled}
|
|
|
|
onChange=${this.handleContentEditableChange}
|
|
|
|
onKeyDown=${this.handleMessageInputKeydown}
|
|
|
|
onKeyUp=${this.handleMessageInputKeyup}
|
|
|
|
onBlur=${this.handleMessageInputBlur}
|
|
|
|
|
|
|
|
onPaste=${this.handlePaste}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div id="message-form-actions" class="absolute flex flex-col w-10 justify-end items-center">
|
2020-08-19 10:16:35 +03:00
|
|
|
<button
|
|
|
|
ref=${this.emojiPickerButton}
|
|
|
|
id="emoji-button"
|
2020-09-22 06:11:09 +03:00
|
|
|
class="text-3xl leading-3 cursor-pointer text-purple-600"
|
2020-08-19 10:16:35 +03:00
|
|
|
type="button"
|
|
|
|
style=${emojiButtonStyle}
|
|
|
|
onclick=${this.handleEmojiButtonClick}
|
2020-08-24 05:37:06 +03:00
|
|
|
disabled=${!inputEnabled}
|
2020-09-22 06:11:09 +03:00
|
|
|
><img src="../../../img/smiley.png" /></button>
|
2020-08-19 10:16:35 +03:00
|
|
|
|
2020-09-22 06:11:09 +03:00
|
|
|
<span id="message-form-warning" class="text-red-600 text-xs">${inputCharsLeft}/${CHAT_MAX_MESSAGE_LENGTH}</span>
|
2020-08-19 10:16:35 +03:00
|
|
|
</div>
|
2020-08-14 14:19:19 +03:00
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2020-08-26 10:57:15 +03:00
|
|
|
}
|