Skip to content

Commit

Permalink
Merge branch 'release-v1.4.0'
Browse files Browse the repository at this point in the history
Michele Tessaro committed May 20, 2022
2 parents 50f334a + d9c7e61 commit 1cbcea0
Showing 13 changed files with 587 additions and 545 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Changelog


## v1.4.0 (2022-05-20)

### New

* Support Redmine 5.0 (refs #110) [sk-ys]


## v1.3.1 (2022-05-14)

### Changes

* Updated changelog. [Michele Tessaro]

### Fix

* Fixed mxgraph viewer patch (refs #105) [Michele Tessaro]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ Before submit an issue please read carefully the `README.md` file (this page): m

## Requirements

- Requires Redmine v2.6+. Tested with Redmine v3.1.4, v3.2.4, v3.3.3, v3.4.0, v4.0.4 as well as Easy Redmine 2016.05.07.
- Requires Redmine v2.6+. Tested with Redmine v3.1.4, v3.2.4, v3.3.3, v3.4.0, v4.0.4, v5.0.1 as well as Easy Redmine 2016.05.07.

## Installation

2 changes: 1 addition & 1 deletion after_init.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# encoding: utf-8
require_dependency 'redmine_drawio'
require File.expand_path('../lib/redmine_drawio', __FILE__)
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
name 'Redmine Drawio plugin'
author 'Michele Tessaro'
description 'Wiki macro plugin for inserting drawio diagrams into Wiki pages and Issues'
version '1.3.1'
version '1.4.0'
url 'https://github.com/mikitex70/redmine_drawio'
author_url 'https://github.com/mikitex70'

16 changes: 8 additions & 8 deletions lib/redmine_drawio.rb
Original file line number Diff line number Diff line change
@@ -3,20 +3,20 @@
# Drawio libraries

# Patches
require 'redmine_drawio/patches/string_patch'
require File.expand_path('../redmine_drawio/patches/string_patch', __FILE__)

# Helpers
require 'redmine_drawio/helpers/drawio_dmsf_helper'
require 'redmine_drawio/helpers/drawio_settings_helper'
require 'redmine_drawio/helpers/textile_helper'
require 'redmine_drawio/helpers/markdown_helper'
require File.expand_path('../redmine_drawio/helpers/drawio_dmsf_helper', __FILE__)
require File.expand_path('../redmine_drawio/helpers/drawio_settings_helper', __FILE__)
require File.expand_path('../redmine_drawio/helpers/textile_helper', __FILE__)
require File.expand_path('../redmine_drawio/helpers/markdown_helper', __FILE__)

# Hooks
require 'redmine_drawio/hooks/view_hooks'
require 'redmine_drawio/hooks/macro_dialog'
require File.expand_path('../redmine_drawio/hooks/view_hooks', __FILE__)
require File.expand_path('../redmine_drawio/hooks/macro_dialog', __FILE__)

# Macros
require 'redmine_drawio/macros'
require File.expand_path('../redmine_drawio/macros', __FILE__)


module RedmineDrawio
29 changes: 17 additions & 12 deletions lib/redmine_drawio/helpers/drawio_dmsf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# encoding: UTF-8

class DMSF_helper
def self.deep_folder_search(project, folderPath)
return nil if folderPath == "."
search_folder_with_path(project, nil, folderPath.split(File::SEPARATOR))
end

def self.search_folder_with_path(project, parent, path)
folder = DmsfFolder.visible.where(:project_id => project.id, :dmsf_folder_id => parent, :title => path[0]).first
module RedmineDrawio
module Helpers
class DrawioDmsfHelper
def self.deep_folder_search(project, folderPath)
return nil if folderPath == "."
search_folder_with_path(project, nil, folderPath.split(File::SEPARATOR))
end

def self.search_folder_with_path(project, parent, path)
folder = DmsfFolder.visible.where(:project_id => project.id, :dmsf_folder_id => parent, :title => path[0]).first

return folder if path.length == 1

search_folder_with_path(project, folder.id, path.drop(1))
end

end

return folder if path.length == 1

search_folder_with_path(project, folder.id, path.drop(1))
end

end
14 changes: 9 additions & 5 deletions lib/redmine_drawio/helpers/drawio_settings_helper.rb
Original file line number Diff line number Diff line change
@@ -8,10 +8,14 @@
# @note The table check is necessary for running tests since the table is not
# available when loading this code first.
#
module DrawioSettingsHelper
def self.svg_enabled?
return false unless ActiveRecord::Base.connection.data_source_exists? 'settings'
module RedmineDrawio
module Helpers
module DrawioSettingsHelper
def self.svg_enabled?
return false unless ActiveRecord::Base.connection.data_source_exists? 'settings'

Setting[:plugin_redmine_drawio]['drawio_svg_enabled'].present? ? true : false
end
Setting[:plugin_redmine_drawio]['drawio_svg_enabled'].present? ? true : false
end
end
end
end
26 changes: 15 additions & 11 deletions lib/redmine_drawio/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
@@ -28,22 +28,26 @@ def heads_for_wiki_formatter_with_drawio
end
else
# Rails 5, use new `prepend` method
module RedmineDrawio_markdown
def heads_for_wiki_formatter
super
unless @heads_for_wiki_formatter_with_drawio_included
# This code is executed only once and inserts a javascript code
# that patches the jsToolBar adding the new buttons.
# After that, all editors in the page will get the new buttons.
content_for :header_tags do
javascript_tag 'if(typeof(Drawio) !== "undefined") Drawio.initToolbar();'
module RedmineDrawio
module Helpers
module MarkdownHelper
def heads_for_wiki_formatter
super
unless @heads_for_wiki_formatter_with_drawio_included
# This code is executed only once and inserts a javascript code
# that patches the jsToolBar adding the new buttons.
# After that, all editors in the page will get the new buttons.
content_for :header_tags do
javascript_tag 'if(typeof(Drawio) !== "undefined") Drawio.initToolbar();'
end
@heads_for_wiki_formatter_with_drawio_included = true
end
end
@heads_for_wiki_formatter_with_drawio_included = true
end
end
end

module Redmine::WikiFormatting::Markdown::Helper
prepend RedmineDrawio_markdown
prepend RedmineDrawio::Helpers::MarkdownHelper
end
end
26 changes: 15 additions & 11 deletions lib/redmine_drawio/helpers/textile_helper.rb
Original file line number Diff line number Diff line change
@@ -28,22 +28,26 @@ def heads_for_wiki_formatter_with_drawio
end
else
# Rails 5, use new new `prepend` method
module RedmineDrawio_textile
def heads_for_wiki_formatter
super
unless @heads_for_wiki_formatter_with_drawio_included
# This code is executed only once and inserts a javascript code
# that patches the jsToolBar adding the new buttons.
# After that, all editors in the page will get the new buttons.
content_for :header_tags do
javascript_tag 'if(typeof(Drawio) !== "undefined") Drawio.initToolbar();'
module RedmineDrawio
module Helpers
module TextileHelper
def heads_for_wiki_formatter
super
unless @heads_for_wiki_formatter_with_drawio_included
# This code is executed only once and inserts a javascript code
# that patches the jsToolBar adding the new buttons.
# After that, all editors in the page will get the new buttons.
content_for :header_tags do
javascript_tag 'if(typeof(Drawio) !== "undefined") Drawio.initToolbar();'
end
@heads_for_wiki_formatter_with_drawio_included = true
end
end
@heads_for_wiki_formatter_with_drawio_included = true
end
end
end

module Redmine::WikiFormatting::Textile::Helper
prepend RedmineDrawio_textile
prepend RedmineDrawio::Helpers::TextileHelper
end
end
18 changes: 10 additions & 8 deletions lib/redmine_drawio/hooks/macro_dialog.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# encoding: UTF-8

class RedmineDrawioHookListener < Redmine::Hook::ViewListener
include DrawioSettingsHelper

def view_layouts_base_body_bottom(context = {})
html = context[:controller].send(:render_to_string,
{ partial: 'redmine_drawio/macro_dialog',
locals: { svg_enabled: svg_enabled? } })
html.html_safe
module RedmineDrawio
module Hooks
class MacroDialog < Redmine::Hook::ViewListener
def view_layouts_base_body_bottom(context = {})
html = context[:controller].send(:render_to_string,
{ partial: 'redmine_drawio/macro_dialog',
locals: { svg_enabled: RedmineDrawio::Helpers::DrawioSettingsHelper.svg_enabled? } })
html.html_safe
end
end
end
end
250 changes: 125 additions & 125 deletions lib/redmine_drawio/hooks/view_hooks.rb
Original file line number Diff line number Diff line change
@@ -3,148 +3,148 @@
require 'base64'

module RedmineDrawio
module Hooks
class ViewLayoutsBaseBodyTop < Redmine::Hook::ViewListener
def view_layouts_base_body_top(context = {})
return unless User.current.admin? && !Setting.rest_api_enabled?

class ViewLayoutsBaseBodyTop < Redmine::Hook::ViewListener
def view_layouts_base_body_top(context = {})
return unless User.current.admin? && !Setting.rest_api_enabled?

context[:controller].send(:render_to_string, { partial: 'redmine_drawio/hooks/api_not_enabled_warning' })
context[:controller].send(:render_to_string, { partial: 'redmine_drawio/hooks/api_not_enabled_warning' })
end
end
end

class ViewLayoutsBaseHtmlHeadHook < Redmine::Hook::ViewListener

# This method will add the necessary CSS and JS scripts to the page header.
# The scripts are loaded before the 'jstoolbar-textile.min.js' is loaded so
# the toolbar cannot be patched.
# A second step is required: the textile_helper.rb inserts a small Javascript
# fragment after the jstoolbar-textile is loaded, which pathes the jsToolBar
# object.
def view_layouts_base_html_head(context={})
# loading XML viewer library, only if necessary
header = <<-EOF
<script type="text/javascript">//<![CDATA[
$(function() {
if($(".mxgraph").length) {
var script = document.createElement('script');
script.src = 'https://viewer.diagrams.net/js/viewer-static.min.js';
document.head.append(script);
class ViewHooks < Redmine::Hook::ViewListener

# This method will add the necessary CSS and JS scripts to the page header.
# The scripts are loaded before the 'jstoolbar-textile.min.js' is loaded so
# the toolbar cannot be patched.
# A second step is required: the textile_helper.rb inserts a small Javascript
# fragment after the jstoolbar-textile is loaded, which pathes the jsToolBar
# object.
def view_layouts_base_html_head(context={})
# loading XML viewer library, only if necessary
header = <<-EOF
<script type="text/javascript">//<![CDATA[
$(function() {
if($(".mxgraph").length) {
var script = document.createElement('script');
script.src = 'https://viewer.diagrams.net/js/viewer-static.min.js';
document.head.append(script);
}
});
//]]</script>
EOF

if Setting.plugin_redmine_drawio['drawio_mathjax']
# Some MathJax tuning:
# * set regexp for classes to ignore, for to no apply MathJax to wrong elements
# * MathJax context menu (enabled, maybe is better to disable it?)
inline = <<-EOF
<script type="text/x-mathjax-config">//<![CDATA[
MathJax.Hub.Config({
/*menuSettings: {
context: "Browser"
},*/
tex2jax: {
//inlineMath: [['$', '$'], ['\\(', '\\)']],
ignoreClass: "no-mathjax|error|warning|notice"
},
asciimath2jax: {
ignoreClass: "no-mathjax|error|warning|notice"
}
});
//]]</script>
EOF

if Setting.plugin_redmine_drawio['drawio_mathjax']
# Some MathJax tuning:
# * set regexp for classes to ignore, for to no apply MathJax to wrong elements
# * MathJax context menu (enabled, maybe is better to disable it?)
//]]</script>
EOF
header << inline
header << javascript_include_tag("#{mathjax_url}?config=TeX-MML-AM_HTMLorMML")
end

return header unless editable?(context)

if Setting.plugin_redmine_drawio['drawio_service_url'].to_s.strip.empty?
drawio_url = '//embed.diagrams.net'
else
drawio_url = Setting.plugin_redmine_drawio['drawio_service_url']
end

inline = <<-EOF
<script type="text/x-mathjax-config">//<![CDATA[
MathJax.Hub.Config({
/*menuSettings: {
context: "Browser"
},*/
tex2jax: {
//inlineMath: [['$', '$'], ['\\(', '\\)']],
ignoreClass: "no-mathjax|error|warning|notice"
},
asciimath2jax: {
ignoreClass: "no-mathjax|error|warning|notice"
}
});
//]]</script>
<script type=\"text/javascript\">//<![CDATA[
var Drawio = {
settings: {
redmineUrl: '#{redmine_url}',
hashCode : '#{hash_code}',
drawioUrl : '#{drawio_url}',
DMSF : #{dmsf_enabled? context},
isEasyRedmine: #{easyredmine?}
}
};
//]]></script>
EOF

header << inline
header << javascript_include_tag("#{mathjax_url}?config=TeX-MML-AM_HTMLorMML")
end

return header unless editable?(context)

if Setting.plugin_redmine_drawio['drawio_service_url'].to_s.strip.empty?
drawio_url = '//embed.diagrams.net'
else
drawio_url = Setting.plugin_redmine_drawio['drawio_service_url']
header << stylesheet_link_tag("drawioEditor.css" , :plugin => "redmine_drawio", :media => "screen")
header << javascript_include_tag("encoding-indexes.js", :plugin => "redmine_drawio")
header << javascript_include_tag("encoding.min.js", :plugin => "redmine_drawio")
header << javascript_include_tag("drawioEditor.js", :plugin => "redmine_drawio")
header << javascript_include_tag("lang/drawio_jstoolbar-en.js", :plugin => "redmine_drawio")
header << javascript_include_tag("lang/drawio_jstoolbar-#{current_language.to_s.downcase}.js", :plugin => "redmine_drawio") if lang_supported? current_language.to_s.downcase
header << javascript_include_tag("drawio_jstoolbar.js", :plugin => "redmine_drawio") unless ckeditor_enabled?
header
end

inline = <<-EOF
<script type=\"text/javascript\">//<![CDATA[
var Drawio = {
settings: {
redmineUrl: '#{redmine_url}',
hashCode : '#{hash_code}',
drawioUrl : '#{drawio_url}',
DMSF : #{dmsf_enabled? context},
isEasyRedmine: #{easyredmine?}
}
};
//]]></script>
EOF
private

header << inline
header << stylesheet_link_tag("drawioEditor.css" , :plugin => "redmine_drawio", :media => "screen")
header << javascript_include_tag("encoding-indexes.js", :plugin => "redmine_drawio")
header << javascript_include_tag("encoding.min.js", :plugin => "redmine_drawio")
header << javascript_include_tag("drawioEditor.js", :plugin => "redmine_drawio")
header << javascript_include_tag("lang/drawio_jstoolbar-en.js", :plugin => "redmine_drawio")
header << javascript_include_tag("lang/drawio_jstoolbar-#{current_language.to_s.downcase}.js", :plugin => "redmine_drawio") if lang_supported? current_language.to_s.downcase
header << javascript_include_tag("drawio_jstoolbar.js", :plugin => "redmine_drawio") unless ckeditor_enabled?
header
end

private

def editable?(context)
return false unless context[:controller]
return true if context[:controller].is_a?(WikiController) && User.current.allowed_to?(:edit_wiki_pages, context[:project])
return false unless context[:controller].is_a?(IssuesController)
def editable?(context)
return false unless context[:controller]
return true if context[:controller].is_a?(WikiController) && User.current.allowed_to?(:edit_wiki_pages, context[:project])
return false unless context[:controller].is_a?(IssuesController)

if context[:issue].nil?
return true if context[:journal].nil?
context[:journal].editable_by?(User.current)
else
context[:issue].editable?(User.current)
if context[:issue].nil?
return true if context[:journal].nil?
context[:journal].editable_by?(User.current)
else
context[:issue].editable?(User.current)
end
end
end

# Returns the context path of Redmine installation (usually '/' or '/redmine/').
def redmine_url
rootUrl = ActionController::Base.relative_url_root

# Returns the context path of Redmine installation (usually '/' or '/redmine/').
def redmine_url
rootUrl = ActionController::Base.relative_url_root

return rootUrl+'/' if rootUrl != nil
return rootUrl+'/' if rootUrl != nil

return '/'
end

def dmsf_enabled?(context)
return false unless Redmine::Plugin.installed? :redmine_dmsf
return false unless context[:project] && context[:project].module_enabled?('dmsf')
true
end

def ckeditor_enabled?
Setting.text_formatting == "CKEditor"
end

def easyredmine?
Redmine::Plugin.installed?(:easy_redmine)
end

def lang_supported? lang
return false if lang == 'en' # English is always loaded, avoid double load
File.exist? "#{File.expand_path('../../../../assets/javascripts/lang', __FILE__)}/drawio_jstoolbar-#{lang}.js"
end

def mathjax_url
url = Setting.plugin_redmine_drawio['drawio_mathjax_url']
url = '//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js' unless url.present?
url
end
return '/'
end
def dmsf_enabled?(context)
return false unless Redmine::Plugin.installed? :redmine_dmsf
return false unless context[:project] && context[:project].module_enabled?('dmsf')
true
end
def ckeditor_enabled?
Setting.text_formatting == "CKEditor"
end
def easyredmine?
Redmine::Plugin.installed?(:easy_redmine)
end
def lang_supported? lang
return false if lang == 'en' # English is always loaded, avoid double load
File.exist? "#{File.expand_path('../../../../assets/javascripts/lang', __FILE__)}/drawio_jstoolbar-#{lang}.js"
end
def mathjax_url
url = Setting.plugin_redmine_drawio['drawio_mathjax_url']
url = '//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js' unless url.present?
url
end

def hash_code
return '' unless Setting.rest_api_enabled?
def hash_code
return '' unless Setting.rest_api_enabled?

Base64.encode64(User.current.api_key).gsub(/\n/, '').reverse!
Base64.encode64(User.current.api_key).gsub(/\n/, '').reverse!
end
end
end

end
716 changes: 361 additions & 355 deletions lib/redmine_drawio/macros.rb

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions lib/redmine_drawio/patches/string_patch.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# encoding: UTF-8

# Add to_bool method to String class; this makes source more readable
class String
def to_bool
return true if self =~ (/^(true|t|yes|y|1)$/i)
return false if self.empty? || self =~ (/^(false|f|no|n|0)$/i)

raise ArgumentError.new "invalid value: #{self}"
end
module RedmineDrawio
module Patches
module StringPatch
def to_bool
return true if self =~ (/^(true|t|yes|y|1)$/i)
return false if self.empty? || self =~ (/^(false|f|no|n|0)$/i)

raise ArgumentError.new "invalid value: #{self}"
end
end
end
end

String.prepend(RedmineDrawio::Patches::StringPatch)

0 comments on commit 1cbcea0

Please sign in to comment.