From 7b2a34c22ccbb9565c8c9aaee41ced9c1b64d937 Mon Sep 17 00:00:00 2001 From: Michele Tessaro Date: Fri, 20 May 2022 07:57:23 +0200 Subject: [PATCH] @minor code reformat --- .../helpers/drawio_dmsf_helper.rb | 28 +- .../helpers/drawio_settings_helper.rb | 16 +- lib/redmine_drawio/helpers/markdown_helper.rb | 24 +- lib/redmine_drawio/helpers/textile_helper.rb | 24 +- lib/redmine_drawio/hooks/macro_dialog.rb | 16 +- lib/redmine_drawio/hooks/view_hooks.rb | 248 +++--- lib/redmine_drawio/macros.rb | 714 +++++++++--------- lib/redmine_drawio/patches/string_patch.rb | 12 +- 8 files changed, 541 insertions(+), 541 deletions(-) diff --git a/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb b/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb index 384244ae..bc88b1d5 100644 --- a/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb +++ b/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb @@ -2,21 +2,21 @@ 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 + 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 end end diff --git a/lib/redmine_drawio/helpers/drawio_settings_helper.rb b/lib/redmine_drawio/helpers/drawio_settings_helper.rb index a2132e9c..d5890bbd 100644 --- a/lib/redmine_drawio/helpers/drawio_settings_helper.rb +++ b/lib/redmine_drawio/helpers/drawio_settings_helper.rb @@ -9,13 +9,13 @@ # 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' + 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 + Setting[:plugin_redmine_drawio]['drawio_svg_enabled'].present? ? true : false + end + end + end end diff --git a/lib/redmine_drawio/helpers/markdown_helper.rb b/lib/redmine_drawio/helpers/markdown_helper.rb index d43a38d4..13e4760b 100644 --- a/lib/redmine_drawio/helpers/markdown_helper.rb +++ b/lib/redmine_drawio/helpers/markdown_helper.rb @@ -30,22 +30,22 @@ def heads_for_wiki_formatter_with_drawio # Rails 5, use new `prepend` method 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();' + 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 - end - end module Redmine::WikiFormatting::Markdown::Helper prepend RedmineDrawio::Helpers::MarkdownHelper diff --git a/lib/redmine_drawio/helpers/textile_helper.rb b/lib/redmine_drawio/helpers/textile_helper.rb index cdaa76a1..5fe11d57 100644 --- a/lib/redmine_drawio/helpers/textile_helper.rb +++ b/lib/redmine_drawio/helpers/textile_helper.rb @@ -30,18 +30,18 @@ def heads_for_wiki_formatter_with_drawio # Rails 5, use new new `prepend` method 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 + 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 end end diff --git a/lib/redmine_drawio/hooks/macro_dialog.rb b/lib/redmine_drawio/hooks/macro_dialog.rb index 46f3f9c7..75302675 100644 --- a/lib/redmine_drawio/hooks/macro_dialog.rb +++ b/lib/redmine_drawio/hooks/macro_dialog.rb @@ -2,13 +2,13 @@ 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 + 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 diff --git a/lib/redmine_drawio/hooks/view_hooks.rb b/lib/redmine_drawio/hooks/view_hooks.rb index d0e86a19..6c452e12 100644 --- a/lib/redmine_drawio/hooks/view_hooks.rb +++ b/lib/redmine_drawio/hooks/view_hooks.rb @@ -4,147 +4,147 @@ 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 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 - + 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 + - 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?) + //]] + 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 - + 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 - - 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 end diff --git a/lib/redmine_drawio/macros.rb b/lib/redmine_drawio/macros.rb index e839d2fe..7f498a8b 100644 --- a/lib/redmine_drawio/macros.rb +++ b/lib/redmine_drawio/macros.rb @@ -6,18 +6,18 @@ module RedmineDrawio module Macros -Redmine::WikiFormatting::Macros.register do - desc < hilight, - 'nav' => false, - 'edit' => '_blank', - 'lightbox' => lightbox, - 'resize' => true, - 'auto-fit' => true, - 'editable' => false, - ##'zoom' => zoom, - 'page' => page, - 'layers' => if layers == '*' then nil else layers end, - 'toolbar-nohide' => (not tbautohide), - 'toolbar' => toolbar, - 'xml' => diagram - } - return RedmineDrawio::Macros.encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false) + diagram = File.read(filename, mode: 'rb') + + 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 RedmineDrawio::Macros.encapsulatePng(diagram, inlineStyle, diagramName, title, saveName, false) + else + tb = [] + tb << 'pages' unless options[:page].blank? + tb << 'layers' unless layers.blank? + tb << 'zoom' if zoom + tb << 'lightbox' if lightbox + + toolbar = if tb.empty? then nil else tb.join(' ') end + #style = if size then 'style="max-width:'+size+'px;"' else "" end + + # https://desk.draw.io/support/solutions/articles/16000042542-embed-html + # https://desk.draw.io/support/solutions/articles/16000042544-embed-mode + graphOpts = { + 'highlight' => hilight, + 'nav' => false, + 'edit' => '_blank', + 'lightbox' => lightbox, + 'resize' => true, + 'auto-fit' => true, + 'editable' => false, + ##'zoom' => zoom, + 'page' => page, + 'layers' => if layers == '*' then nil else layers end, + 'toolbar-nohide' => (not tbautohide), + 'toolbar' => toolbar, + 'xml' => diagram + } + return RedmineDrawio::Macros.encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false) + end + end end - end -end -if Redmine::Plugin.installed?(:redmine_dmsf) - Redmine::WikiFormatting::Macros.register do - desc < hilight, - 'nav' => false, - 'edit' => '_blank', - 'lightbox' => lightbox, - 'resize' => true, - 'auto-fit' => true, - 'editable' => false, - ##'zoom' => zoom, - 'page' => page, - 'layers' => if layers == '*' then nil else layers end, - 'toolbar-nohide' => (not tbautohide), - 'toolbar' => toolbar, - 'xml' => diagram - } - return RedmineDrawio::Macros.encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false) + macro :drawio_dmsf do |obj, args| + return "«Please save content first»" unless obj + 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 = 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 RedmineDrawio::Macros.svg_enabled? || not(diagramName =~ /.*\.svg$/i) + + # Add an extension, if missing + diagramName += ".png" if File.extname(diagramName.strip) == "" + diagramExt = File.extname(diagramName.strip) + + size = options[:size].to_i unless options[:size].blank? or not options[:size][/^\d+$/] + # parameters checkings + hilight = options[:hilight] unless options[:hilight].blank? + tbautohide = options[:tbautohide].to_bool unless options[:tbautohide].blank? + lightbox = options[:lightbox].to_bool unless options[:lightbox].blank? + #size = options[:fit].to_i unless options[:fit].blank? + zoom = options[:zoom].to_bool unless options[:zoom].blank? + page = options[:page].to_i unless options[:page].blank? + layers = options[:layers] unless options[:layers].blank? + + inlineStyle = "" + inlineStyle = "width:#{size}px;" if size + + if obj.is_a?(WikiContent) + container = obj.page + title = container.title + project = container.wiki.project + canEdit = User.current.allowed_to?(:edit_wiki_pages, @project) + elsif obj.is_a?(Journal) + container = obj + title = nil # not necessary + project = container.project + canEdit = container.editable_by?(User.current) + else + container = obj + title = nil # not necessary + project = container.project + canEdit = container.editable?(User.current) + end + + # Search the DMSF folder containing the diagram + folderName = File.dirname(diagramName) + 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 = RedmineDrawio::Macros.dmsf_save_name project, diagramName + else + # Diagram cannot be saved, it will become not editable + saveName = nil + end + + if file + # Document exists, get the file path + filename = file.last_revision.disk_file project + 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 = RedmineDrawio::Macros.imagePath('defaultImage'+diagramExt) + canEdit = canEdit && User.current && User.current.allowed_to?(:file_manipulation, project) + end + + diagram = File.read(filename, mode: 'rb') + + 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 RedmineDrawio::Macros.encapsulatePng(diagram, inlineStyle, diagramName, title, saveName, true) + else + tb = [] + tb << 'pages' unless options[:page].blank? + tb << 'layers' unless layers.blank? + tb << 'zoom' if zoom + tb << 'lightbox' if lightbox + + toolbar = if tb.empty? then nil else tb.join(' ') end + #style = if size then 'style="max-width:'+size+'px;"' else "" end + + graphOpts = { + 'highlight' => hilight, + 'nav' => false, + 'edit' => '_blank', + 'lightbox' => lightbox, + 'resize' => true, + 'auto-fit' => true, + 'editable' => false, + ##'zoom' => zoom, + 'page' => page, + 'layers' => if layers == '*' then nil else layers end, + 'toolbar-nohide' => (not tbautohide), + 'toolbar' => toolbar, + 'xml' => diagram + } + return RedmineDrawio::Macros.encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false) + end + end end end - end -end -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'] -end + 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'] + end -def dmsf_version - Redmine::Plugin.find(:redmine_dmsf).version -end + def dmsf_version + Redmine::Plugin.find(:redmine_dmsf).version + end -def dmsf_save_name(project, diagramName) - if Setting.plugin_redmine_dmsf['dmsf_webdav_use_project_names'] - Rails.logger.error "dmsf_version=#{RedmineDrawio::Macros.dmsf_version}" - - 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 RedmineDrawio::Macros.dmsf_version <= '1.6.0' - # DMSF 1.5.9+ can use project name as folder - "#{project.name} -#{project.id}-/#{diagramName}" - else - # With DMSF 1.6.1+ the path is changed - "#{project.name} #{project.id}/#{diagramName}" - end - else - "#{project.identifier}/#{diagramName}" - end -end + def dmsf_save_name(project, diagramName) + if Setting.plugin_redmine_dmsf['dmsf_webdav_use_project_names'] + Rails.logger.error "dmsf_version=#{RedmineDrawio::Macros.dmsf_version}" + + 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 RedmineDrawio::Macros.dmsf_version <= '1.6.0' + # DMSF 1.5.9+ can use project name as folder + "#{project.name} -#{project.id}-/#{diagramName}" + else + # With DMSF 1.6.1+ the path is changed + "#{project.name} #{project.id}/#{diagramName}" + end + else + "#{project.identifier}/#{diagramName}" + end + end -def imagePath(defaultImage) - File.expand_path(File.join(File.dirname(__FILE__), '../../spec', defaultImage)) -end + def imagePath(defaultImage) + File.expand_path(File.join(File.dirname(__FILE__), '../../spec', defaultImage)) + end -def adaptSvg(svg, size) - # Remove some scripts from the SVG (prevent some XSS issues) - localSvg = svg.sub(/(?:]*>(?:.*?)<\/script>)|(?:\\s*javascript:.*\))|(?:\\bon\w+.*?=[^>]+)|(?:src=.?(&#x?[0-9a-f]+)+)/i, '') - # Adapt SVG to make it resizable - localSvg = localSvg.sub(/]+?)(?: height="([0-9]+)px")?/, "]*>(?:.*?)<\/script>)|(?:\\s*javascript:.*\))|(?:\\bon\w+.*?=[^>]+)|(?:src=.?(&#x?[0-9a-f]+)+)/i, '') + # Adapt SVG to make it resizable + localSvg = localSvg.sub(/]+?)(?: height="([0-9]+)px")?/, "#{svg.force_encoding("UTF-8")}".html_safe - else - "".html_safe - end -end + def encapsulateSvg(svg, inlineStyle, diagramName, title, saveName, isDmsf) + dblClick = "" + tooltip = "" + style = "" + style = inlineStyle unless inlineStyle.blank? + + 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}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');\"" + tooltip = " title='Double click to edit diagram'" + style = " style='#{inlineStyle}cursor:pointer'" + end + + if Setting.plugin_redmine_drawio['drawio_svg_enabled'] + "#{svg.force_encoding("UTF-8")}".html_safe + else + "".html_safe + end + end -def encapsulatePng(png, inlineStyle, diagramName, title, saveName, isDmsf) - if saveName.nil? - return ActionController::Base.helpers.image_tag("data:image/png;charset=utf-8;base64,#{png}", - :alt => "Diagram #{diagramName}", - :class => "drawioDiagram", - :style => "#{inlineStyle}") - else - 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}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');") - end -end + def encapsulatePng(png, inlineStyle, diagramName, title, saveName, isDmsf) + if saveName.nil? + return ActionController::Base.helpers.image_tag("data:image/png;charset=utf-8;base64,#{png}", + :alt => "Diagram #{diagramName}", + :class => "drawioDiagram", + :style => "#{inlineStyle}") + else + 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}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');") + end + end -def encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, isDmsf) - randomId = 'dg_'+('a'..'z').to_a.shuffle[0,8].join - - unless saveName.nil? - # Diagram is editable, add toolbar declarations for editing - graphOpts['toolbar'] = "#{graphOpts['toolbar']} edit" - graphOpts['toolbar-buttons'] = { - 'edit' => { - 'title' => 'Edit', - 'handler' => "(function(){editDiagram($('##{randomId}'),'#{saveName}', #{isDmsf}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');})", - 'image' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAVCAQAAAADHm0dAAAAbUlEQVQoz93NoRGAMBAAwXNIZCQlUEZKoQRkOqAESqAEJBIZiURGYh/DhKjPS4bTO3Pw2TwrK2MdOhKCIAQdtkD/4KTBwEGX8aVBQQq86LDErgZfbIAKHayw4bTOvRXCZIUQM9x1CBtCZMbzi25WtlGUbURavAAAAABJRU5ErkJggg==', - } - } - end - - tag = "
".html_safe - - return tag if inlineStyle.empty? - - return "
#{tag}
".html_safe -end + def encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, isDmsf) + randomId = 'dg_'+('a'..'z').to_a.shuffle[0,8].join + + unless saveName.nil? + # Diagram is editable, add toolbar declarations for editing + graphOpts['toolbar'] = "#{graphOpts['toolbar']} edit" + graphOpts['toolbar-buttons'] = { + 'edit' => { + 'title' => 'Edit', + 'handler' => "(function(){editDiagram($('##{randomId}'),'#{saveName}', #{isDmsf}, '#{RedmineDrawio::Macros.js_safe(title)}', '#{diagramName}');})", + 'image' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAVCAQAAAADHm0dAAAAbUlEQVQoz93NoRGAMBAAwXNIZCQlUEZKoQRkOqAESqAEJBIZiURGYh/DhKjPS4bTO3Pw2TwrK2MdOhKCIAQdtkD/4KTBwEGX8aVBQQq86LDErgZfbIAKHayw4bTOvRXCZIUQM9x1CBtCZMbzi25WtlGUbURavAAAAABJRU5ErkJggg==', + } + } + end + + tag = "
".html_safe + + return tag if inlineStyle.empty? + + return "
#{tag}
".html_safe + end -def svg?(diagramName) - diagramName =~ /\.svg$/i -end + def svg?(diagramName) + diagramName =~ /\.svg$/i + end -def png?(diagramName) - diagramName =~ /\.png$/i -end + def png?(diagramName) + diagramName =~ /\.png$/i + end -def strip_non_filename_chars(filename) - # Replace directory separator from \ to / - filename = filename.gsub(/\\/, '/') - return filename.gsub(/[\x00:*?"'<>|,;]/, '_') if Gem.win_platform? - # *nix - filename.gsub(/[,;|"']/, '_') -end + def strip_non_filename_chars(filename) + # Replace directory separator from \ to / + filename = filename.gsub(/\\/, '/') + return filename.gsub(/[\x00:*?"'<>|,;]/, '_') if Gem.win_platform? + # *nix + filename.gsub(/[,;|"']/, '_') + end -def js_safe(string) - string.gsub(/'/){ %q(\') } if string - '' -end + def js_safe(string) + string.gsub(/'/){ %q(\') } if string + '' + end -def svg_enabled? - RedmineDrawio::Helpers::DrawioSettingsHelper.svg_enabled? -end + def svg_enabled? + RedmineDrawio::Helpers::DrawioSettingsHelper.svg_enabled? + end end end -end \ No newline at end of file +end diff --git a/lib/redmine_drawio/patches/string_patch.rb b/lib/redmine_drawio/patches/string_patch.rb index 11ddc626..a70b9644 100644 --- a/lib/redmine_drawio/patches/string_patch.rb +++ b/lib/redmine_drawio/patches/string_patch.rb @@ -4,12 +4,12 @@ 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 + 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