Add emoji autosuggest (#5053)
* Add emoji autosuggest Some credit goes to glitch-soc/mastodon#149 * Remove server-side shortcode->unicode conversion * Insert shortcode when suggestion is custom emoji * Remove remnant of server-side emojis * Update style of autosuggestions * Fix wrong emoji filenames generated in autosuggest item * Do not lazy load emoji picker, as that no longer works * Fix custom emoji autosuggest * Fix multiple "Custom" categories getting added to emoji index, only add oncemain
parent
66126f3021
commit
1e02ba111a
@ -1,24 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module EmojiHelper
|
||||
def emojify(text)
|
||||
return text if text.blank?
|
||||
|
||||
text.gsub(emoji_pattern) do |match|
|
||||
emoji = Emoji.instance.unicode($1) # rubocop:disable Style/PerlBackrefs
|
||||
|
||||
if emoji
|
||||
emoji
|
||||
else
|
||||
match
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def emoji_pattern
|
||||
@emoji_pattern ||=
|
||||
/(?<=[^[:alnum:]:]|\n|^)
|
||||
(#{Emoji.instance.names.map { |name| Regexp.escape(name) }.join('|')})
|
||||
(?=[^[:alnum:]:]|$)/x
|
||||
end
|
||||
end
|
@ -0,0 +1,37 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { unicodeMapping } from '../emojione_light';
|
||||
|
||||
const assetHost = process.env.CDN_HOST || '';
|
||||
|
||||
export default class AutosuggestEmoji extends React.PureComponent {
|
||||
|
||||
static propTypes = {
|
||||
emoji: PropTypes.object.isRequired,
|
||||
};
|
||||
|
||||
render () {
|
||||
const { emoji } = this.props;
|
||||
let url;
|
||||
|
||||
if (emoji.custom) {
|
||||
url = emoji.imageUrl;
|
||||
} else {
|
||||
const [ filename ] = unicodeMapping[emoji.native];
|
||||
url = `${assetHost}/emoji/${filename}.svg`;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='autosuggest-emoji'>
|
||||
<img
|
||||
className='emojione'
|
||||
src={url}
|
||||
alt={emoji.native || emoji.colons}
|
||||
/>
|
||||
|
||||
{emoji.colons}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'singleton'
|
||||
|
||||
class Emoji
|
||||
include Singleton
|
||||
|
||||
def initialize
|
||||
data = Oj.load(File.open(Rails.root.join('lib', 'assets', 'emoji.json')))
|
||||
|
||||
@map = {}
|
||||
|
||||
data.each do |_, emoji|
|
||||
keys = [emoji['shortname']] + emoji['aliases']
|
||||
unicode = codepoint_to_unicode(emoji['unicode'])
|
||||
|
||||
keys.each do |key|
|
||||
@map[key] = unicode
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def unicode(shortcode)
|
||||
@map[shortcode]
|
||||
end
|
||||
|
||||
def names
|
||||
@map.keys
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def codepoint_to_unicode(codepoint)
|
||||
if codepoint.include?('-')
|
||||
codepoint.split('-').map(&:hex).pack('U*')
|
||||
else
|
||||
[codepoint.hex].pack('U')
|
||||
end
|
||||
end
|
||||
end
|
File diff suppressed because one or more lines are too long
@ -1,20 +0,0 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe EmojiHelper, type: :helper do
|
||||
describe '#emojify' do
|
||||
it 'converts shortcodes to unicode' do
|
||||
text = ':book: Book'
|
||||
expect(emojify(text)).to eq '📖 Book'
|
||||
end
|
||||
|
||||
it 'converts composite emoji shortcodes to unicode' do
|
||||
text = ':couple_ww:'
|
||||
expect(emojify(text)).to eq '👩❤👩'
|
||||
end
|
||||
|
||||
it 'does not convert shortcodes that are part of a string into unicode' do
|
||||
text = ':see_no_evil::hear_no_evil::speak_no_evil:'
|
||||
expect(emojify(text)).to eq text
|
||||
end
|
||||
end
|
||||
end
|
@ -1,15 +0,0 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Emoji do
|
||||
describe '#unicode' do
|
||||
it 'returns a unicode for a shortcode' do
|
||||
expect(Emoji.instance.unicode(':joy:')).to eq '😂'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#names' do
|
||||
it 'returns an array' do
|
||||
expect(Emoji.instance.names).to be_an Array
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue