]> source.dussan.org Git - redmine.git/commitdiff
Make languages in Highlighted code button in toolbar customizable (#32528).
authorGo MAEDA <maeda@farend.jp>
Tue, 14 Jan 2020 14:38:48 +0000 (14:38 +0000)
committerGo MAEDA <maeda@farend.jp>
Tue, 14 Jan 2020 14:38:48 +0000 (14:38 +0000)
Patch by Mizuki ISHIKAWA.

git-svn-id: http://svn.redmine.org/redmine/trunk@19429 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/user_preference.rb
app/views/users/_preferences.html.erb
config/locales/en.yml
lib/redmine/wiki_formatting/markdown/helper.rb
lib/redmine/wiki_formatting/textile/helper.rb
public/javascripts/jstoolbar/jstoolbar.js
test/integration/layout_test.rb
test/unit/user_preference_test.rb

index 597ac9c901c54632ceaaabe9a03a2758ba3b7515..3f0d9743c8bb5b3560e48f555406bf2aa1023bb2 100644 (file)
@@ -35,9 +35,11 @@ class UserPreference < ActiveRecord::Base
     'no_self_notified',
     'textarea_font',
     'recently_used_projects',
-    'history_default_tab')
+    'history_default_tab',
+    'toolbar_language_options')
 
   TEXTAREA_FONT_OPTIONS = ['monospace', 'proportional']
+  DEFAULT_TOOLBAR_LANGUAGE_OPTIONS = %w[c cpp csharp css diff go groovy html java javascript objc perl php python r ruby sass scala shell sql swift xml yaml]
 
   def initialize(attributes=nil, *args)
     super
@@ -98,6 +100,15 @@ class UserPreference < ActiveRecord::Base
   def history_default_tab; self[:history_default_tab]; end
   def history_default_tab=(value); self[:history_default_tab]=value; end
 
+  def toolbar_language_options
+    self[:toolbar_language_options].presence || DEFAULT_TOOLBAR_LANGUAGE_OPTIONS.join(',')
+  end
+
+  def toolbar_language_options=(value)
+    languages = value.to_s.delete(' ').split(',').select{|lang| Redmine::SyntaxHighlighting.language_supported?(lang) }.compact
+    self[:toolbar_language_options] = languages.join(',')
+  end
+
   # Returns the names of groups that are displayed on user's page
   # Example:
   #   preferences.my_page_groups
index 6397c3a38e57c977e41c57227c8efac033d2beea..3734c3064760040f8878473c247b3c90822f600e 100644 (file)
@@ -6,4 +6,5 @@
 <p><%= pref_fields.select :textarea_font, textarea_font_options %></p>
 <p><%= pref_fields.text_field :recently_used_projects, :size => 2 %></p>
 <p><%= pref_fields.select :history_default_tab, history_default_tab_options %></p>
+<p><%= pref_fields.text_area :toolbar_language_options, :rows => 4 %></p>
 <% end %>
index 031c7ba8757cea785f96e407619f18743676ea0c..3df365c090a59460f7dd787e0bf400045990337c 100644 (file)
@@ -389,6 +389,7 @@ en:
   field_recently_used_projects: Number of recently used projects in jump box
   field_history_default_tab: Issue's history default tab
   field_unique_id: Unique ID
+  field_toolbar_language_options: Code highlighting toolbar languages
 
   setting_app_title: Application title
   setting_welcome_text: Welcome text
index 6b5a1dfc3788d8a0754f3ae24f060d01e9f2ff8b..aafa90e3cff0de2f41dd72292aebf212b07b0993 100644 (file)
@@ -33,11 +33,13 @@ module Redmine
 
         def heads_for_wiki_formatter
           unless @heads_for_wiki_formatter_included
+            toolbar_language_options = User.current && User.current.pref.toolbar_language_options
             content_for :header_tags do
               javascript_include_tag('jstoolbar/jstoolbar') +
               javascript_include_tag('jstoolbar/markdown') +
               javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language.to_s.downcase}") +
-              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};") +
+              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};" +
+                             "var userHlLanguages = #{(toolbar_language_options.nil? ? UserPreference::DEFAULT_TOOLBAR_LANGUAGE_OPTIONS : toolbar_language_options.split(',')).to_json};") +
               stylesheet_link_tag('jstoolbar')
             end
             @heads_for_wiki_formatter_included = true
index e3865fca5d7e219ab0cdfeac053c87c68f164f5d..9b45fd38869660f49b3139b03a36b95120fe38e4 100644 (file)
@@ -34,11 +34,13 @@ module Redmine
 
         def heads_for_wiki_formatter
           unless @heads_for_wiki_formatter_included
+            toolbar_language_options = User.current && User.current.pref.toolbar_language_options
             content_for :header_tags do
               javascript_include_tag('jstoolbar/jstoolbar') +
               javascript_include_tag('jstoolbar/textile') +
               javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language.to_s.downcase}") +
-              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};") +
+              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};" +
+                             "var userHlLanguages = #{(toolbar_language_options.nil? ? UserPreference::DEFAULT_TOOLBAR_LANGUAGE_OPTIONS : toolbar_language_options.split(',')).to_json};") +
               stylesheet_link_tag('jstoolbar')
             end
             @heads_for_wiki_formatter_included = true
index dccff99521015b7a2f28bd1e866bdd23f924233e..4f5ab73060a8b11db94fcdb282e866a4113fec4f 100644 (file)
@@ -453,7 +453,7 @@ jsToolBar.prototype.resizeDragStop = function(event) {
 
 /* Code highlighting menu */
 jsToolBar.prototype.precodeMenu = function(fn){
-  var hlLanguages = ["c", "cpp", "csharp", "css", "diff", "go", "groovy", "html", "java", "javascript", "objc", "perl", "php", "python", "r", "ruby", "sass", "scala", "shell", "sql", "swift", "xml", "yaml"];
+  var hlLanguages = window.userHlLanguages;
   var menu = $("<ul style='position:absolute;'></ul>");
   for (var i = 0; i < hlLanguages.length; i++) {
     $("<li></li>").text(hlLanguages[i]).appendTo(menu).mousedown(function(){
index 7b5e9325f3ef91783dcfed6a6980928a47c58634..f152251d5831761c5e6c7f28278411b70f7384cd 100644 (file)
@@ -66,6 +66,7 @@ class LayoutTest < Redmine::IntegrationTest
 
     get '/projects/ecookbook/issues/new'
     assert_select 'head script[src^=?]', '/javascripts/jstoolbar/jstoolbar.js?'
+    assert_include "var userHlLanguages = #{UserPreference::DEFAULT_TOOLBAR_LANGUAGE_OPTIONS.to_json};", response.body
   end
 
   def test_calendar_header_tags
index 8eb3490d8bc73edf019a483c17ddc8c9ba0869e7..cc6c5d48cd9366e923cf414720f389e6fee5036f 100644 (file)
@@ -110,4 +110,11 @@ class UserPreferenceTest < ActiveSupport::TestCase
     up.save!
     assert_equal ['documents'], up.my_page_settings.keys
   end
+
+  def test_toolbar_language_options_setter_should_remove_except_supported_languages
+    up = User.find(2).pref
+    # bar is not a supported language
+    up.toolbar_language_options = 'ruby,cpp,bar,c'
+    assert_equal 'ruby,cpp,c', up.toolbar_language_options
+  end
 end