Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :blob-emotes: #526

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
//= require moment.min
//= require fullcalendar.min
//= require jquery.multi-select
//= require emotes
7 changes: 7 additions & 0 deletions app/views/layouts/_emote_handler.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
<% if member_signed_in? %>
$(renderEmoteTags);
<% else %>
$(removeEmoteTags);
<% end %>
</script>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<%= render 'layouts/url_helpers' %>
<%= javascript_include_tag "application" %>
<%= javascript_pack_tag "application" %>
<%= render 'layouts/emote_handler' %>
<%= csrf_meta_tags %>
</head>

Expand Down
59 changes: 59 additions & 0 deletions vendor/assets/javascripts/emotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Adds selected emotes to all pages.
* Allows copy-paste from ABTech Slack
*
* Usage:
* -----
* Just use the emotes in any text :blobeyes:
*/


let emotes = {
':party-blob:': 'https://emojis.slackmojis.com/emojis/images/1643514770/7808/party-blob.gif?1643514770',
':blobsob:': 'https://emojis.slackmojis.com/emojis/images/1643514690/6921/blob_sob.png?1643514690',
':blobtoiletspin:': 'https://github.com/thomplinds/custom-slack-emojis/blob/main/images/blobtoiletflush.gif?raw=true',
':blobsad:': 'https://emojis.slackmojis.com/emojis/images/1643514688/6904/blob_sad.png?1643514688',
':blob-spin:': 'https://emojis.slackmojis.com/emojis/images/1643515247/12652/blobspin.png?1643515247',
':blob-go:': 'https://emojis.slackmojis.com/emojis/images/1643514683/6858/blob_go.png?1643514683',
':blob_excited:': 'https://emojis.slackmojis.com/emojis/images/1643514936/9579/blob_excited.gif?1643514936',
':blobeyes:': 'https://emojis.slackmojis.com/emojis/images/1643514682/6848/blob_eyes.png?1643514682',
':blobfearful:': 'https://emojis.slackmojis.com/emojis/images/1643514682/6850/blob_fearful.png?1643514682',
};

function renderEmoteTags() {
for (const [emote_text, url] of Object.entries(emotes)) {
let text_nodes = $(`:contains("${emote_text}")`).contents();

// Filter to text (type 3) nodes that definitely contain it
// (:contains doesn't guarantee this: https://stackoverflow.com/a/29418265)
text_nodes = text_nodes.filter(function() {
return this.nodeType === 3 && this.textContent.indexOf(emote_text) > -1;
});

// replace the emote text with the img
text_nodes.replaceWith(function() {
return this.nodeValue.replace(RegExp(emote_text, "g"),
// .emote-inline doesn't actually do anything
// height should end up roughly true line height
// alt text shows if image is not found and allows copy-paste
`<img class="emote-inline" style="height:1.4em; margin-bottom:-0.2em;" src="${url}" alt="${emote_text}"/>`
);
});
}
}

function removeEmoteTags() {
for (const [emote_text, url] of Object.entries(emotes)) {
let text_nodes = $(`:contains("${emote_text}")`).contents();

// Filter to text (type 3) nodes that definitely contain it
// (:contains doesn't guarantee this: https://stackoverflow.com/a/29418265)
text_nodes = text_nodes.filter(function() {
return this.nodeType === 3 && this.textContent.indexOf(emote_text) > -1;
});

// remove emote tags
text_nodes.replaceWith(function() {
return this.nodeValue.replace(RegExp(emote_text, "g"), "");
});
}
}