REFACTOR: Remove .erb from emoji-picker

We can't use erb in ember-cli, and it seems the emoji groups rarely
change anyway. This commit migrates the ERB to pre-rendered javascript
that is updated via the `rake javascript:update_constants` task.
This commit is contained in:
Robin Ward
2020-11-25 10:58:19 -05:00
parent 9ec0359022
commit 059e9cb1d2
4 changed files with 1815 additions and 32 deletions

View File

@ -16,6 +16,24 @@ def library_src
"#{Rails.root}/node_modules"
end
def html_for_section(group)
icons = group["icons"].map do |icon|
class_attr = icon["diversity"] ? " class=\"diversity\"" : ""
" {{replace-emoji \":#{icon['name']}:\" (hash lazy=true#{class_attr})}}"
end
<<~SECTION
<div class="section" data-section="#{group["name"]}">
<div class="section-header">
<span class="title">{{i18n "emoji_picker.#{group["name"]}"}}</span>
</div>
<div class="section-group">
#{icons.join("\n").strip}
</div>
</div>
SECTION
end
def write_template(path, task_name, template)
header = <<~HEADER
// DO NOT EDIT THIS FILE!!!
@ -31,6 +49,18 @@ def write_template(path, task_name, template)
puts "#{basename} prettified"
end
def write_hbs_template(path, task_name, template)
header = <<~HEADER
{{!-- DO NOT EDIT THIS FILE!!! --}}
{{!-- Update it by running `rake javascript:#{task_name}` --}}
HEADER
basename = File.basename(path)
output_path = "#{Rails.root}/app/assets/javascripts/#{path}"
File.write(output_path, "#{header}\n#{template}")
puts "#{basename} created"
end
def dependencies
[
{
@ -188,6 +218,23 @@ task 'javascript:update_constants' => :environment do
write_template("pretty-text/addon/emoji/version.js", task_name, <<~JS)
export const IMAGE_VERSION = "#{Emoji::EMOJI_VERSION}";
JS
groups_json = JSON.parse(File.read("lib/emoji/groups.json"))
emoji_buttons = groups_json.map do |group|
<<~BUTTON
<button type="button" data-section="#{group["name"]}" {{action onCategorySelection "#{group["name"]}"}} class="btn btn-default category-button emoji">
{{replace-emoji ":#{group["tabicon"]}:"}}
</button>
BUTTON
end
emoji_sections = groups_json.map { |group| html_for_section(group) }
components_dir = "discourse/app/templates/components"
write_hbs_template("#{components_dir}/emoji-group-buttons.hbs", task_name, emoji_buttons.join)
write_hbs_template("#{components_dir}/emoji-group-sections.hbs", task_name, emoji_sections.join)
end
task 'javascript:update' => 'clean_up' do