drop fall back emoji support and add routing helper class

This commit is contained in:
Alexander Meindl 2024-08-05 20:20:45 +02:00
parent b28252a8c9
commit b94e192a86
11 changed files with 51 additions and 75 deletions

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
class AdditionalsRouter
include Rails.application.routes.url_helpers
def self.default_url_options
::Mailer.default_url_options
end
end

View File

@ -3,7 +3,6 @@ account_login_bottom: ''
add_go_to_top: 0
disabled_modules:
emoji_support: 0
disable_emoji_native_support: 0
global_footer: ''
global_sidebar: ''
global_wiki_sidebar: ''

View File

@ -7,15 +7,10 @@ module Additionals
DEFAULT_MODAL_WIDTH = '350px'
GOTO_LIST = " \xc2\xbb"
LIST_SEPARATOR = "#{GOTO_LIST} ".freeze
EMOJI_ASSERT_PATH = 'plugin_assets/additionals/images/emojis'
include RedminePluginKit::PluginBase
class << self
def full_url(path = nil)
"#{Setting.protocol}://#{Setting.host_name.chomp '/'}#{path}"
end
def class_prefix(klass)
klass_name = klass.is_a?(String) ? klass : klass.name
klass_name.underscore.tr '/', '_'
@ -174,10 +169,6 @@ module Additionals
# create some side effencts
plugin_id = 'additionals'
# TODO: enable again, if fallback for emoji support
# has been implemented for mail delivery and pdf
# Additionals::Gemify.install_emoji_assets
# if plugin is already in plugins directory, use this and leave here
next if Redmine::Plugin.installed? plugin_id

View File

@ -50,14 +50,6 @@ module Additionals
end
def emoji_tag(emoji, _emoji_code)
if Additionals.setting? :disable_emoji_native_support
emoji_tag_fallback emoji
else
emoji_tag_native emoji
end
end
def emoji_tag_native(emoji)
return unless emoji
data = {
@ -69,17 +61,6 @@ module Additionals
ActionController::Base.helpers.content_tag 'additionals-emoji', emoji.codepoints, options
end
def emoji_tag_fallback(emoji)
ActionController::Base.helpers.image_tag emoji_image_path(emoji),
title: emoji.description,
class: 'inline_emojify'
end
def emoji_image_path(emoji, local: false)
base_url = local ? '/' : Additionals.full_url
File.join base_url, Additionals::EMOJI_ASSERT_PATH, emoji.image_name
end
def with_emoji?(text)
text.match? emoji_pattern
end

View File

@ -3,28 +3,6 @@
module Additionals
class Gemify
class << self
# install emoji fallback assets from gem (without asset pipeline)
def install_emoji_assets
Additionals.debug 'install_emoji_assets'
return Rails.logger.error 'TanukiEmoji class for emoji not found' unless defined? TanukiEmoji
source_image_path = TanukiEmoji.images_path
target_image_path = File.join Dir.pwd, 'public', Additionals::EMOJI_ASSERT_PATH
begin
FileUtils.mkdir_p target_image_path
rescue StandardError => e
raise "Could not create directory #{target_image_path}: " + e.message
end
Dir["#{source_image_path}/*"].each do |file|
target = File.join target_image_path, file.gsub(source_image_path, '')
FileUtils.cp file, target unless File.exist?(target) && FileUtils.identical?(file, target)
rescue StandardError => e
raise "Could not copy #{file} to #{target}: " + e.message
end
end
# install assets from gem (without asset pipline)
def install_assets(plugin_id = 'additionals')
return unless Gem.loaded_specs[plugin_id]

View File

@ -45,7 +45,7 @@ module Additionals
def emoji_unicode_element_unicode_filter(text)
text.gsub emoji_unicode_pattern do |moji|
emoji = TanukiEmoji.find_by_codepoints moji # rubocop: disable Rails/DynamicFindBy
emoji_tag_native emoji
emoji_tag emoji
end
end

View File

@ -36,6 +36,10 @@ module Additionals
EnabledModule.create project: project, name: 'issue_tracking'
end
end
def emoji_heart_tag
'<additionals-emoji title="heavy black heart" data-name="heart" data-unicode-version="1.1">❤</additionals-emoji>'
end
end
module PluginFixturesLoader

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
require File.expand_path '../../test_helper', __FILE__
class AdditionalsRouterTest < Additionals::TestCase
include Redmine::I18n
def test_url_for
router = AdditionalsRouter.new
assert_equal 'http://localhost:3000/', router.url_for(:home)
end
def test_routing_url
router = AdditionalsRouter.new
assert_equal 'http://localhost:3000/projects/1', router.project_url(1)
end
def test_routing_path
router = AdditionalsRouter.new
assert_equal '/projects/1', router.project_path(1)
end
end

View File

@ -33,7 +33,7 @@ module WikiFormatting
</code></pre>
HTML
expected = <<~HTML
<img title="heavy black heart" class="inline_emojify" src="http://localhost:3000/#{Additionals::EMOJI_ASSERT_PATH}/emoji_u2764.png">
#{emoji_heart_tag}
<pre><code>
def foo
:heart:
@ -42,8 +42,7 @@ module WikiFormatting
HTML
with_plugin_settings 'additionals', legacy_smiley_support: 0,
emoji_support: 1,
disable_emoji_native_support: 1 do
emoji_support: 1 do
assert_equal expected, emoji_filter(input)
end
end

View File

@ -24,32 +24,27 @@ module WikiFormatting
def test_emojies
with_plugin_settings 'additionals', legacy_smiley_support: 0,
emoji_support: 1,
disable_emoji_native_support: 1 do
emoji_support: 1 do
text = 'A test with a :heart: emoji'
assert_equal '<p>A test with a <img title="heavy black heart" class="inline_emojify"' \
" src=\"http://localhost:3000/#{Additionals::EMOJI_ASSERT_PATH}/emoji_u2764.png\" /> emoji</p>",
assert_equal "<p>A test with a #{emoji_heart_tag} emoji</p>",
@formatter.new(text).to_html.strip
end
end
def test_smilies_and_emojies
with_plugin_settings 'additionals', legacy_smiley_support: 1,
emoji_support: 1,
disable_emoji_native_support: 1 do
emoji_support: 1 do
text = ':heart: and :)'
assert_equal '<p><img title="heavy black heart" class="inline_emojify"' \
" src=\"http://localhost:3000/#{Additionals::EMOJI_ASSERT_PATH}/emoji_u2764.png\" />" \
assert_equal "<p>#{emoji_heart_tag}" \
' and <span class="additionals smiley smiley-smiley" title=":)"></span></p>',
@formatter.new(text).to_html.strip
text = ' :) and :heart:'
assert_equal '<p><span class="additionals smiley smiley-smiley" title=":)"></span> and' \
' <img title="heavy black heart" class="inline_emojify"' \
" src=\"http://localhost:3000/#{Additionals::EMOJI_ASSERT_PATH}/emoji_u2764.png\" /></p>",
" #{emoji_heart_tag}</p>",
@formatter.new(text).to_html.strip
end
end

View File

@ -28,13 +28,11 @@ module WikiFormatting
def test_emojies
with_plugin_settings 'additionals', legacy_smiley_support: 0,
emoji_support: 1,
disable_emoji_native_support: 1 do
emoji_support: 1 do
@formatter::RULES.delete :inline_smileys
str = 'A test with a :heart: emoji and a :) smiley'
@to_test[str] = 'A test with a <img title="heavy black heart" class="inline_emojify"' \
" src=\"http://localhost:3000/#{Additionals::EMOJI_ASSERT_PATH}/emoji_u2764.png\" /> emoji and a :) smiley"
@to_test[str] = "A test with a #{emoji_heart_tag} emoji and a :) smiley"
assert_html_output @to_test
end
@ -42,17 +40,14 @@ module WikiFormatting
def test_smilies_and_emojies
with_plugin_settings 'additionals', legacy_smiley_support: 1,
emoji_support: 1,
disable_emoji_native_support: 1 do
emoji_support: 1 do
# this is required, because inline_smileys are activated with controller action
@formatter::RULES << :inline_smileys
@to_test[':heart: and :)'] = '<img title="heavy black heart" class="inline_emojify"' \
" src=\"http://localhost:3000/#{Additionals::EMOJI_ASSERT_PATH}/emoji_u2764.png\" />" \
@to_test[':heart: and :)'] = "#{emoji_heart_tag}" \
' and <span class="additionals smiley smiley-smiley" title=":)"></span>'
@to_test[':) and :heart:'] = '<span class="additionals smiley smiley-smiley" title=":)"></span> and' \
' <img title="heavy black heart" class="inline_emojify"' \
" src=\"http://localhost:3000/#{Additionals::EMOJI_ASSERT_PATH}/emoji_u2764.png\" />"
" #{emoji_heart_tag}"
assert_html_output @to_test
end