Skip to content

Commit

Permalink
new: usr: support Redmine 5.0 (refs #110)
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-ys authored and Michele Tessaro committed May 20, 2022
1 parent e189bd3 commit adc1021
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 52 deletions.
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__)
16 changes: 8 additions & 8 deletions lib/redmine_drawio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/redmine_drawio/helpers/drawio_dmsf_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# encoding: UTF-8

class DMSF_helper
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))
Expand All @@ -14,4 +16,7 @@ def self.search_folder_with_path(project, parent, path)
search_folder_with_path(project, folder.id, path.drop(1))
end

end

end
end
4 changes: 4 additions & 0 deletions lib/redmine_drawio/helpers/drawio_settings_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
end
end
end
8 changes: 6 additions & 2 deletions lib/redmine_drawio/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def heads_for_wiki_formatter_with_drawio
end
else
# Rails 5, use new `prepend` method
module RedmineDrawio_markdown
module RedmineDrawio
module Helpers
module MarkdownHelper
def heads_for_wiki_formatter
super
unless @heads_for_wiki_formatter_with_drawio_included
Expand All @@ -42,8 +44,10 @@ def heads_for_wiki_formatter
end
end
end
end
end

module Redmine::WikiFormatting::Markdown::Helper
prepend RedmineDrawio_markdown
prepend RedmineDrawio::Helpers::MarkdownHelper
end
end
8 changes: 6 additions & 2 deletions lib/redmine_drawio/helpers/textile_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def heads_for_wiki_formatter_with_drawio
end
else
# Rails 5, use new new `prepend` method
module RedmineDrawio_textile
module RedmineDrawio
module Helpers
module TextileHelper
def heads_for_wiki_formatter
super
unless @heads_for_wiki_formatter_with_drawio_included
Expand All @@ -40,10 +42,12 @@ def heads_for_wiki_formatter
end
@heads_for_wiki_formatter_with_drawio_included = true
end
end
end
end
end

module Redmine::WikiFormatting::Textile::Helper
prepend RedmineDrawio_textile
prepend RedmineDrawio::Helpers::TextileHelper
end
end
10 changes: 6 additions & 4 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

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: svg_enabled? } })
locals: { svg_enabled: RedmineDrawio::Helpers::DrawioSettingsHelper.svg_enabled? } })
html.html_safe
end
end
end
end
6 changes: 3 additions & 3 deletions lib/redmine_drawio/hooks/view_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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?
Expand All @@ -12,7 +12,7 @@ def view_layouts_base_body_top(context = {})
end
end

class ViewLayoutsBaseHtmlHeadHook < Redmine::Hook::ViewListener
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
Expand Down Expand Up @@ -146,5 +146,5 @@ def hash_code
Base64.encode64(User.current.api_key).gsub(/\n/, '').reverse!
end
end

end
end
66 changes: 36 additions & 30 deletions lib/redmine_drawio/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require 'redmine'
require 'json'

module RedmineDrawio
module Macros

Redmine::WikiFormatting::Macros.register do
desc <<EOF
This macro is deprecated. Use the drawio_attach macro instead.
Expand Down Expand Up @@ -49,11 +52,11 @@
return "«Drawio diagrams are available only in issues and wiki pages»" unless obj.is_a?(WikiContent) or obj.is_a?(Issue) or obj.is_a?(Journal)

args, options = extract_macro_options(args, :size, :hilight, :tbautohide, :lightbox, :zoom, :page, :layers)
diagramName = strip_non_filename_chars(args.first)
diagramName = RedmineDrawio::Macros.strip_non_filename_chars(args.first)

return "«Please set a diagram name»".html_safe unless diagramName
return "«Only png, svg and xml diagram formats are supported»".html_safe unless diagramName =~ /.*(\.(png|svg|xml))?$/i
return "svg diagrams are disabled by the administrator" unless svg_enabled? || not(diagramName =~ /.*\.svg$/i)
return "svg diagrams are disabled by the administrator" unless RedmineDrawio::Macros.svg_enabled? || not(diagramName =~ /.*\.svg$/i)

# defalts
hilight = "#0000ff"
Expand Down Expand Up @@ -96,7 +99,7 @@
diagramExt = File.extname(diagramName.strip)

# Search attachment position
attach = container.attachments.where(filename: diagramName).last
attach = container.attachments.select { |attachment| attachment.filename == diagramName } .last

if canEdit
# Diagram and document are editable
Expand All @@ -118,17 +121,17 @@
if attach
filename = attach.diskfile
else
filename = imagePath('defaultImage'+diagramExt)
filename = RedmineDrawio::Macros.imagePath('defaultImage'+diagramExt)
end

diagram = File.read(filename, mode: 'rb')

if svg? diagramName
return encapsulateSvg(adaptSvg(diagram, size), inlineStyle, diagramName, title, saveName, false)
elsif png? diagramName
if RedmineDrawio::Macros.svg? diagramName
return RedmineDrawio::Macros.encapsulateSvg(RedmineDrawio::Macros.adaptSvg(diagram, size), inlineStyle, diagramName, title, saveName, false)
elsif RedmineDrawio::Macros.png? diagramName
# if png, encode image and remove newlines (required by Internet Explorer)
diagram = Base64.encode64(diagram).gsub("\n", '')
return encapsulatePng(diagram, inlineStyle, diagramName, title, saveName, false)
return RedmineDrawio::Macros.encapsulatePng(diagram, inlineStyle, diagramName, title, saveName, false)
else
tb = []
tb << 'pages' unless options[:page].blank?
Expand Down Expand Up @@ -156,7 +159,7 @@
'toolbar' => toolbar,
'xml' => diagram
}
return encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false)
return RedmineDrawio::Macros.encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false)
end
end
end
Expand Down Expand Up @@ -201,11 +204,11 @@
return "«Drawio diagrams are available only in issues and wiki pages»" unless obj.is_a?(WikiContent) or obj.is_a?(Issue) or obj.is_a?(Journal)

args, options = extract_macro_options(args, :size, :hilight, :tbautohide, :lightbox, :zoom, :page, :layers)
diagramName = strip_non_filename_chars(args.first).force_encoding("UTF-8")
diagramName = RedmineDrawio::Macros.strip_non_filename_chars(args.first).force_encoding("UTF-8")

return "«Please set a diagram name»".html_safe unless diagramName
return "«Only png and svg diagram formats are supported»".html_safe unless diagramName =~ /.*(\.(png|svg))?$/i
return "svg diagrams are disabled by the administrator" unless svg_enabled? || not(diagramName =~ /.*\.svg$/i)
return "svg diagrams are disabled by the administrator" unless RedmineDrawio::Macros.svg_enabled? || not(diagramName =~ /.*\.svg$/i)

# Add an extension, if missing
diagramName += ".png" if File.extname(diagramName.strip) == ""
Expand Down Expand Up @@ -243,14 +246,14 @@

# Search the DMSF folder containing the diagram
folderName = File.dirname(diagramName)
folder = DMSF_helper.deep_folder_search(project, folderName)
folder = RedmineDrawio::Helpers::DrawioDmsfHelper.deep_folder_search(project, folderName)

# Search the document in DMSF
file = DmsfFile.find_file_by_name project, folder, File.basename(diagramName)

if canEdit
# Diagram and document are editable
saveName = dmsf_save_name project, diagramName
saveName = RedmineDrawio::Macros.dmsf_save_name project, diagramName
else
# Diagram cannot be saved, it will become not editable
saveName = nil
Expand All @@ -262,18 +265,18 @@
canEdit = canEdit && User.current && User.current.allowed_to?(:file_manipulation, file.project)
else
# Document does not exists: use a predefined diagram to start editing
filename = imagePath('defaultImage'+diagramExt)
filename = RedmineDrawio::Macros.imagePath('defaultImage'+diagramExt)
canEdit = canEdit && User.current && User.current.allowed_to?(:file_manipulation, project)
end

diagram = File.read(filename, mode: 'rb')

if svg? diagramName
return encapsulateSvg(adaptSvg(diagram, size), inlineStyle, diagramName, title, saveName, true)
elsif png? diagramName
if RedmineDrawio::Macros.svg? diagramName
return RedmineDrawio::Macros.encapsulateSvg(RedmineDrawio::Macros.adaptSvg(diagram, size), inlineStyle, diagramName, title, saveName, true)
elsif RedmineDrawio::Macros.png? diagramName
# if png, encode image and remove newlines (required by Internet Explorer)
diagram = Base64.encode64(diagram).gsub("\n", '')
return encapsulatePng(diagram, inlineStyle, diagramName, title, saveName, true)
return RedmineDrawio::Macros.encapsulatePng(diagram, inlineStyle, diagramName, title, saveName, true)
else
tb = []
tb << 'pages' unless options[:page].blank?
Expand All @@ -299,14 +302,13 @@
'toolbar' => toolbar,
'xml' => diagram
}
return encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false)
return RedmineDrawio::Macros.encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false)
end
end
end
end

private

class << self
def drawio_url
return '//embed.diagrams.net' if Setting.plugin_redmine_drawio['drawio_service_url'].to_s.strip.empty?
Setting.plugin_redmine_drawio['drawio_service_url']
Expand All @@ -318,12 +320,12 @@ def dmsf_version

def dmsf_save_name(project, diagramName)
if Setting.plugin_redmine_dmsf['dmsf_webdav_use_project_names']
Rails.logger.error "dmsf_version=#{dmsf_version}"
Rails.logger.error "dmsf_version=#{RedmineDrawio::Macros.dmsf_version}"

if dmsf_version <= '1.5.8'
if RedmineDrawio::Macros.dmsf_version <= '1.5.8'
# Prior to DMSF 1.5.9 project names cannot be used for folder names
"#{project.id}/#{diagramName}"
elsif dmsf_version <= '1.6.0'
elsif RedmineDrawio::Macros.dmsf_version <= '1.6.0'
# DMSF 1.5.9+ can use project name as folder
"#{project.name} -#{project.id}-/#{diagramName}"
else
Expand Down Expand Up @@ -372,7 +374,7 @@ def encapsulateSvg(svg, inlineStyle, diagramName, title, saveName, isDmsf)

unless saveName.nil?
tagType = if Setting.plugin_redmine_drawio['drawio_svg_enabled'] then 'svg' else 'img' end
dblClick = " ondblclick=\"editDiagram($(this).find('#{tagType}')[0],'#{saveName}',#{isDmsf}, '#{js_safe(title)}', '#{diagramName}');\""
dblClick = " ondblclick=\"editDiagram($(this).find('#{tagType}')[0],'#{saveName}',#{isDmsf}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');\""
tooltip = " title='Double click to edit diagram'"
style = " style='#{inlineStyle}cursor:pointer'"
end
Expand All @@ -386,17 +388,17 @@ def encapsulateSvg(svg, inlineStyle, diagramName, title, saveName, isDmsf)

def encapsulatePng(png, inlineStyle, diagramName, title, saveName, isDmsf)
if saveName.nil?
return image_tag("data:image/png;charset=utf-8;base64,#{png}",
return ActionController::Base.helpers.image_tag("data:image/png;charset=utf-8;base64,#{png}",
:alt => "Diagram #{diagramName}",
:class => "drawioDiagram",
:style => "#{inlineStyle}")
else
return image_tag("data:image/png;charset=utf-8;base64,#{png}",
return ActionController::Base.helpers.image_tag("data:image/png;charset=utf-8;base64,#{png}",
:alt => "Diagram #{diagramName}",
:title => "Double click to edit diagram",
:class => "drawioDiagram",
:style => "#{inlineStyle}cursor:pointer;",
:ondblclick => "editDiagram(this,'#{saveName}', #{isDmsf}, '#{js_safe(title)}', '#{diagramName}');")
:ondblclick => "editDiagram(this,'#{saveName}', #{isDmsf}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');")
end
end

Expand All @@ -409,7 +411,7 @@ def encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, isDmsf)
graphOpts['toolbar-buttons'] = {
'edit' => {
'title' => 'Edit',
'handler' => "(function(){editDiagram($('##{randomId}'),'#{saveName}', #{isDmsf}, '#{js_safe(title)}', '#{diagramName}');})",
'handler' => "(function(){editDiagram($('##{randomId}'),'#{saveName}', #{isDmsf}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');})",
'image' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAVCAQAAAADHm0dAAAAbUlEQVQoz93NoRGAMBAAwXNIZCQlUEZKoQRkOqAESqAEJBIZiURGYh/DhKjPS4bTO3Pw2TwrK2MdOhKCIAQdtkD/4KTBwEGX8aVBQQq86LDErgZfbIAKHayw4bTOvRXCZIUQM9x1CBtCZMbzi25WtlGUbURavAAAAABJRU5ErkJggg==',
}
}
Expand Down Expand Up @@ -444,5 +446,9 @@ def js_safe(string)
end

def svg_enabled?
DrawioSettingsHelper.svg_enabled?
RedmineDrawio::Helpers::DrawioSettingsHelper.svg_enabled?
end

end
end
end
8 changes: 7 additions & 1 deletion 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
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 adc1021

Please sign in to comment.