Custom control development with TMS WEB Core for Visual Studio Code, an intern reports
At tmssoftware.com we love to have interns. I believe it is a fantastic experience to learn young aspiring software developers about Delphi, its eco-system, its amazing productivity and at the same time, absorb a lot of energy, get new fresh ideas and get to know what drives and motivates the next generation of software developers.
Today, we want to give the word to our intern, and very soon colleague, Bradley who did a lot of stunning work this school year and tells in his first blog post about building his first custom control for TMS WEB Core. Nothing but an amazing and exciting story! So, with pleasure, I give the word to Bradley:
A first custom web control
Having only one year experience using Delphi and TMS WEB Core, I was assigned the (at first sight) daunting task of developing my first ever custom web control, namely a chat box control. I had only been developing web applications since I joined tmssoftware.com as an intern, so clearly writing a component was something completely new. Using the available documentation and the experience of my mentor Bruno I quickly obtained the necessary skills for developing a TMS WEB Core custom control, the TWebChatBox control.
First steps
Starting the chatbox implementation
After I was comfortable enough with the principle of custom controls. I’ve started building the chat box component. As there were a ton of features on the requirements list, I had actually no idea where to start. I’ve focused first of all on a working way of sending/receiving messages. When I had that going, I started focusing on adding a basic design, followed up by adding the necessary properties for manipulating said chat box.
I think implementing collections as a way of managing messages was the only thing I really struggled with. Having never implemented anything like this, figuring out how to implement the chat message bubbles at collection item level was a challenge. If you add a message to your collection, a chat message bubble will be generated and displayed in the chat control. The collection item will also keep a reference of the chat message bubble so that you can manipulate it later on.
FChatMessage := TJSHTMLElement(document.createElement('SPAN')); FMessageInfo := TJSHTMLElement(document.createElement('DIV')); FTimeStamp := TJSHTMLElement(document.createElement('P')); FChatSender := TJSHTMLElement(document.createElement('P')); FImageArea := TJSHTMLElement(document.createElement('DIV')); FMessageArea := TJSHTMLElement(document.createElement('DIV')); FImage := TJSHTMLElement(document.createElement('IMG'));
These chat bubbles are, after being made, displayed on the chat display which is nothing more than a flexbox DIV.
Underneath the chat display we have our input area. This area contains several elements like an input for the message, a send button and an emoji picker.
FArea := TJSHTMLElement(document.createElement('DIV')); FInputDisplay := TJSHTMLElement(document.createElement('DIV')); FInputTextBox := TJSHtmlElement(document.createElement('TEXTAREA')); FSendButton := TJSHTMLElement(document.createElement('BUTTON')); FInputDisplay.appendChild(FInputTextBox); FArea.appendChild(FInputDisplay); FArea.appendChild(FSendButton); ElementHandle.appendChild(FArea); FSendButton.addEventListener('click', @HandleSendMessageButtonClick); FInputTextBox.addEventListener('keyup', @HandleSendMessageInputEnter);
There are 2 event listeners. You can press the send button for sending messages. But pressing enter will also send your message. When sending/receiving messages the chat box will auto scroll with all messages except if you scroll up.
Another feature that the chat box has, is an auto scroll. when you send a message the chat box will automatically scroll down as long as the user hasn’t scrolled up. To check if a user has scrolled up is nothing more than using this function:
function TChatBox.isScrolledToBottom: Boolean; begin Result := (FChatDisplay.scrollHeight – FChatDisplay.clientHeight <= FChatDisplay.scrollTop + 1); end;
To effectively scroll down you use this procedure:
procedure TChatBox.scrollToBottom; begin FChatDisplay.scrollTop := FChatDisplay.scrollHeight; end;
The Emoji Picker is an external JavaScript library. The emoji picker is added by instantiating the EmojiPicker class and then use the generateElements function and giving the input textbox as a parameter.
asm (()=> { var picker = new EmojiPicker(); picker.generateElements(element); })(); end;
This library will add a dropdown button linked to your input textbox and will allow you to choose an emoji.
Typing some of the mostly used emoji shortcuts in the textbox will also change to an emoji when sending the message. The same applies for hyperlinks that are converted into clickable links in the chat bubble.
Conclusion
Thank you
Thank you Bradley for a great collaboration during the internship. We look forward to go to the next level as a colleague in the tms family in July! The year went by fast and let's see who will take on the challenge to become an intern at tmssoftware.com in the next school year!