From ae6cda23401f715691bde152b61e6b8386af489e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Fri, 2 Mar 2018 09:19:05 +0100 Subject: [PATCH] Extract current text formatting helper into separate helper --- app/helpers/application_helper.rb | 8 -------- ...ws_helper.rb => text_formatting_helper.rb} | 19 ++++++++++++++++++- app/views/wiki/edit.html.erb | 1 + .../formatters/markdown/helper.rb | 4 ++++ .../formatters/plain/helper.rb | 7 +++++++ .../formatters/textile/helper.rb | 4 ++++ 6 files changed, 34 insertions(+), 9 deletions(-) rename app/helpers/{previews_helper.rb => text_formatting_helper.rb} (76%) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1c4b0b17ae..592dc6b3e6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -42,9 +42,6 @@ module ApplicationHelper include IconsHelper include AdditionalUrlHelpers - extend Forwardable - def_delegators :wiki_helper, :text_formatting_js_includes, :wikitoolbar_for, :heads_for_wiki_formatter - # Return true if user is authorized for controller/action, otherwise false def authorize_for(controller, action, project: @project) User.current.allowed_to?({ controller: controller, action: action }, project) @@ -599,11 +596,6 @@ module ApplicationHelper private - def wiki_helper - helper = OpenProject::TextFormatting::Formatters.helper_for(Setting.text_formatting) - extend helper - self - end def link_to_content_update(text, url_params = {}, html_options = {}) link_to(text, url_params, html_options) diff --git a/app/helpers/previews_helper.rb b/app/helpers/text_formatting_helper.rb similarity index 76% rename from app/helpers/previews_helper.rb rename to app/helpers/text_formatting_helper.rb index 8947b3cebf..26b10830df 100644 --- a/app/helpers/previews_helper.rb +++ b/app/helpers/text_formatting_helper.rb @@ -27,8 +27,17 @@ # See docs/COPYRIGHT.rdoc for more details. #++ -module PreviewsHelper +module TextFormattingHelper + extend Forwardable + def_delegators :current_formatting_helper, + :text_formatting_has_preview?, + :text_formatting_js_includes, + :wikitoolbar_for, + :heads_for_wiki_formatter + def preview_link(path, link_id, options = {}) + return '' unless text_formatting_has_preview? + options = { accesskey: accesskey(:preview), id: link_id, @@ -42,4 +51,12 @@ module PreviewsHelper l(:label_preview) end end + + private + + def current_formatting_helper + helper = OpenProject::TextFormatting::Formatters.helper_for(Setting.text_formatting) + extend helper + self + end end diff --git a/app/views/wiki/edit.html.erb b/app/views/wiki/edit.html.erb index 020b6309a5..3257b44a0a 100644 --- a/app/views/wiki/edit.html.erb +++ b/app/views/wiki/edit.html.erb @@ -48,6 +48,7 @@ See docs/COPYRIGHT.rdoc for more details. <%= link_to t(:button_cancel), { controller: '/wiki', action: 'show', project_id: @project, id: @page }, class: 'button' %> + <%= preview_link preview_project_wiki_path(@project, @page), 'wiki_form-preview' %> <% end %>
<% content_for :header_tags do %> diff --git a/lib/open_project/text_formatting/formatters/markdown/helper.rb b/lib/open_project/text_formatting/formatters/markdown/helper.rb index ad8998d568..b9d09a3a44 100644 --- a/lib/open_project/text_formatting/formatters/markdown/helper.rb +++ b/lib/open_project/text_formatting/formatters/markdown/helper.rb @@ -34,6 +34,10 @@ module OpenProject::TextFormatting::Formatters javascript_include_tag 'vendor/ckeditor/openproject-ckeditor.js', async: true end + def text_formatting_has_preview? + false + end + def wikitoolbar_for(field_id) tag 'op-ckeditor-form', 'textarea-selector': "##{field_id}" end diff --git a/lib/open_project/text_formatting/formatters/plain/helper.rb b/lib/open_project/text_formatting/formatters/plain/helper.rb index 5240f89298..80b0d33732 100644 --- a/lib/open_project/text_formatting/formatters/plain/helper.rb +++ b/lib/open_project/text_formatting/formatters/plain/helper.rb @@ -33,6 +33,13 @@ module OpenProject::TextFormatting::Formatters def wikitoolbar_for(_field_id) end + def text_formatting_has_preview? + false + end + + def text_formatting_js_includes + end + def heads_for_wiki_formatter end diff --git a/lib/open_project/text_formatting/formatters/textile/helper.rb b/lib/open_project/text_formatting/formatters/textile/helper.rb index d236d1b874..c8c3e5584e 100644 --- a/lib/open_project/text_formatting/formatters/textile/helper.rb +++ b/lib/open_project/text_formatting/formatters/textile/helper.rb @@ -34,6 +34,10 @@ module OpenProject::TextFormatting::Formatters # TODO Nothing to do here yet, since the js_toolbar is still part of application end + def text_formatting_has_preview? + true + end + def wikitoolbar_for(field_id) heads_for_wiki_formatter url = url_for(controller: '/help', action: 'wiki_syntax')