Skip to content

Commit

Permalink
Merge pull request #263 from maykinmedia/emoji
Browse files Browse the repository at this point in the history
Emoji
  • Loading branch information
alextreme authored Jun 15, 2022
2 parents dad04d7 + 29459bb commit 99eeedc
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 60 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@joeattardi/emoji-button": "^4.6.4",
"@tarekraafat/autocomplete.js": "^10.2.6",
"bem.js": "^1.0.10",
"emojibase-data": "^7.0.1",
"flatpickr": "^4.6.9",
"gumshoejs": "^5.1.2",
"html-minifier": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n button_tags form_tags messages_tags %}
{% load i18n button_tags form_tags messages_tags icon_tags %}

<section class="messages">
<header class="messages__header">
Expand Down Expand Up @@ -45,7 +45,16 @@ <h4 class="p p--muted">{{ day.text }}</h4>
{% textarea form.content %}
<div class="group-input__toolbar">
{% input form.file icon="attach_file" no_label=True no_help=True extra_classes="message-file" %}
{% button class="emoji-button" icon="tag_faces" text=_('Selecteer emoji') hide_text=True type="button" %}
<div class="emoji">
{% button class="emoji__button" icon="tag_faces" text=_('Selecteer emoji') hide_text=True type="button" %}
<div class="emoji__popup">
<div class="emoji__header">
<input class="emoji__search input input--bordered" type="search" placeholder="{% trans "Zoek emoji" %}">
{% icon icon="close" %}
</div>
<div class="emoji__emojis"></div>
</div>
</div>
</div>
</div>
{% form_actions primary_text=_("Verzenden") primary_icon='arrow_forward' %}
Expand Down
103 changes: 48 additions & 55 deletions src/open_inwoner/js/components/emoji-button/index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,61 @@
import { EmojiButton } from '@joeattardi/emoji-button'
import emojis from 'emojibase-data/nl/data.json'

/** @type {NodeList} All the emoji buttons. */
const EMOJI_BUTTONS = document.querySelectorAll('.emoji-button')
const emojiElements = document.querySelectorAll('.emoji')

/**
* Allows selecting emojis.
*/
class EmojiButtonSelector {
/**
* Constructor method.
* @param {HTMLElement} node
*/
class Emoji {
constructor(node) {
/** @type {HTMLElement} */
this.node = node

/** @type {EmojiButton} */
this.picker = this.getPicker()

this.bindEvents()
this.content = document.getElementById('id_content')
this.search = node.querySelector('.emoji__search')
this.button = node.querySelector('.emoji__button')
this.popup = node.querySelector('.emoji__popup')
this.container = node.querySelector('.emoji__emojis')
this.close = this.popup.querySelector('.material-icons')
this.populatePopup()
this.addListeners()
}

/**
* Binds events to callbacks.
*/
bindEvents() {
this.node.addEventListener('click', () =>
this.getPicker().togglePicker(this.node)
)
this.getPicker().on('emoji', this.onEmoji.bind(this))
addListeners() {
this.button.addEventListener('click', (event) => {
event.preventDefault()
this.popup.classList.toggle('emoji__popup--open')
})
document.addEventListener('keyup', (event) => {
if (event.key === 'Escape') {
this.popup.classList.remove('emoji__popup--open')
}
})
this.close.addEventListener('click', (event) => {
this.popup.classList.remove('emoji__popup--open')
})
this.search.addEventListener('keyup', this.searchEmoji.bind(this))
}

/**
* Returns the input to add emoji's to.
* @return {HTMLElement}
*/
getInput() {
return this.node.parentElement?.parentElement?.querySelector(
'input, textarea'
)
}

/**
* Returns/instantiates the picker instances.
* @return {EmojiButton}
*/
getPicker() {
if (!this.picker) {
this.picker = new EmojiButton()
}

return this.picker
searchEmoji(event) {
const searchValue = event.currentTarget.value.toUpperCase()
document.querySelectorAll('.emoji__emoji-button').forEach((button) => {
const label = button.dataset.label.toUpperCase()
if (label.includes(searchValue)) {
button.classList.remove('emoji__emoji-button--hidden')
} else {
button.classList.add('emoji__emoji-button--hidden')
}
})
}

/**
* Gets called when an emoji is selected.
* @param {Object} selection
*/
onEmoji(selection) {
const input = this.getInput()
const emoji = selection.emoji
input.value += emoji
populatePopup() {
emojis.forEach((emoji) => {
const emojiButton = document.createElement('div')
emojiButton.classList.add('emoji__emoji-button')
emojiButton.append(emoji.emoji)
emojiButton.title = emoji.label
emojiButton.setAttribute('data-label', emoji.label)
emojiButton.addEventListener('click', (event) => {
this.content.append(emoji.emoji)
})
this.container.append(emojiButton)
})
}
}

// Start1
;[...EMOJI_BUTTONS].forEach((node) => new EmojiButtonSelector(node))
;[...emojiElements].forEach((emojiElement) => new Emoji(emojiElement))
2 changes: 1 addition & 1 deletion src/open_inwoner/js/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import './confirmation'
import './datepicker'
import './dropdown'
import './dropdown'
// import './emoji-button'
import './emoji-button'
import './header'
import './map'
import './message-file'
Expand Down
48 changes: 48 additions & 0 deletions src/open_inwoner/scss/components/Emoji/Emoji.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.emoji {
&__header {
display: grid;
grid-template-columns: 1fr 25px;
align-items: center;
padding: 8px 0 0 8px;
gap: 8px;
}

&__search {
// grid-column: 1 / span 8;
min-width: 0;
}

.material-icons {
cursor: pointer;
}

&__emoji-button {
cursor: pointer;

&--hidden {
display: none;
}
}

&__popup {
display: none;
position: absolute;
background-color: #fff;
border: 1px solid #000;
width: 260px;

&--open {
display: block;
}
}

&__emojis {
display: grid;
gap: var(--spacing-medium);
grid-template-columns: repeat(8, 1fr);
padding: var(--spacing-medium);
max-height: 200px;
overflow: auto;
text-align: center;
}
}
4 changes: 4 additions & 0 deletions src/open_inwoner/scss/components/Form/Input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
font-family: var(--font-family-body);
font-size: var(--font-size-body);
background-color: var(--color-white);

&.input--bordered {
border: 1px solid var(--color-mute);
}
}

textarea.input {
Expand Down
5 changes: 5 additions & 0 deletions src/open_inwoner/scss/components/MessageFile/MessageFile.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
padding: var(--spacing-small);
}

&__init {
display: none;
}

&__delete {
cursor: pointer;
}
Expand All @@ -34,6 +38,7 @@

.label {
cursor: pointer;
flex-direction: row;

*[class*='icon'] {
top: var(--spacing-tiny);
Expand Down
6 changes: 5 additions & 1 deletion src/open_inwoner/scss/components/Messages/_Messages.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@
}

.message {
max-width: 50%;
max-width: 85%;
align-self: baseline;

@media (min-width: 768px) {
max-width: 50%;
}
}

.message--ours {
Expand Down
1 change: 1 addition & 0 deletions src/open_inwoner/scss/components/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@
@import './Typography/LinkList.scss';
@import './Typography/P.scss';
@import './modal/modal';
@import './Emoji/Emoji.scss';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<input type="{{ widget.type }}" name="{{ widget.name }}" data-init="{{ widget.value|default:'' }}"{% include "django/forms/widgets/attrs.html" %}>
<label>
<input type="text" name="{{ widget.init_name }}" id="{{ widget.init_id }}" {% if widget.value %} value="{{ widget.value.instance.uuid }}"{% endif %} class="message-file__init" style="display: none">
<input type="text" name="{{ widget.init_name }}" id="{{ widget.init_id }}" {% if widget.value %} value="{{ widget.value.instance.uuid }}"{% endif %} class="message-file__init">
</label>

0 comments on commit 99eeedc

Please sign in to comment.