From adc1021ce418abe82665c0c1cbaddf5cf8cadc56 Mon Sep 17 00:00:00 2001 From: sk-ys Date: Tue, 17 May 2022 18:48:32 +0900 Subject: [PATCH] new: usr: support Redmine 5.0 (refs #110) --- after_init.rb | 2 +- lib/redmine_drawio.rb | 16 ++--- .../helpers/drawio_dmsf_helper.rb | 7 +- .../helpers/drawio_settings_helper.rb | 4 ++ lib/redmine_drawio/helpers/markdown_helper.rb | 8 ++- lib/redmine_drawio/helpers/textile_helper.rb | 8 ++- lib/redmine_drawio/hooks/macro_dialog.rb | 10 +-- lib/redmine_drawio/hooks/view_hooks.rb | 6 +- lib/redmine_drawio/macros.rb | 66 ++++++++++--------- lib/redmine_drawio/patches/string_patch.rb | 8 ++- 10 files changed, 83 insertions(+), 52 deletions(-) diff --git a/after_init.rb b/after_init.rb index 1b272d9e4..3e2797a72 100644 --- a/after_init.rb +++ b/after_init.rb @@ -1,2 +1,2 @@ # encoding: utf-8 -require_dependency 'redmine_drawio' +require File.expand_path('../lib/redmine_drawio', __FILE__) diff --git a/lib/redmine_drawio.rb b/lib/redmine_drawio.rb index b1e591c3c..62c016c93 100644 --- a/lib/redmine_drawio.rb +++ b/lib/redmine_drawio.rb @@ -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 diff --git a/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb b/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb index d2692fcea..384244ae3 100644 --- a/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb +++ b/lib/redmine_drawio/helpers/drawio_dmsf_helper.rb @@ -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)) @@ -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 diff --git a/lib/redmine_drawio/helpers/drawio_settings_helper.rb b/lib/redmine_drawio/helpers/drawio_settings_helper.rb index 0a4246da6..a2132e9c2 100644 --- a/lib/redmine_drawio/helpers/drawio_settings_helper.rb +++ b/lib/redmine_drawio/helpers/drawio_settings_helper.rb @@ -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 diff --git a/lib/redmine_drawio/helpers/markdown_helper.rb b/lib/redmine_drawio/helpers/markdown_helper.rb index 231f9560c..d43a38d48 100644 --- a/lib/redmine_drawio/helpers/markdown_helper.rb +++ b/lib/redmine_drawio/helpers/markdown_helper.rb @@ -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 @@ -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 diff --git a/lib/redmine_drawio/helpers/textile_helper.rb b/lib/redmine_drawio/helpers/textile_helper.rb index 0578361c4..cdaa76a1c 100644 --- a/lib/redmine_drawio/helpers/textile_helper.rb +++ b/lib/redmine_drawio/helpers/textile_helper.rb @@ -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 @@ -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 diff --git a/lib/redmine_drawio/hooks/macro_dialog.rb b/lib/redmine_drawio/hooks/macro_dialog.rb index 0aaaf2ca4..46f3f9c75 100644 --- a/lib/redmine_drawio/hooks/macro_dialog.rb +++ b/lib/redmine_drawio/hooks/macro_dialog.rb @@ -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 diff --git a/lib/redmine_drawio/hooks/view_hooks.rb b/lib/redmine_drawio/hooks/view_hooks.rb index a14c51ca6..d0e86a190 100644 --- a/lib/redmine_drawio/hooks/view_hooks.rb +++ b/lib/redmine_drawio/hooks/view_hooks.rb @@ -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? @@ -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 @@ -146,5 +146,5 @@ def hash_code Base64.encode64(User.current.api_key).gsub(/\n/, '').reverse! end end - + end end diff --git a/lib/redmine_drawio/macros.rb b/lib/redmine_drawio/macros.rb index b23d604aa..e839d2fe7 100644 --- a/lib/redmine_drawio/macros.rb +++ b/lib/redmine_drawio/macros.rb @@ -3,6 +3,9 @@ require 'redmine' require 'json' +module RedmineDrawio + module Macros + Redmine::WikiFormatting::Macros.register do desc < toolbar, 'xml' => diagram } - return encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false) + return RedmineDrawio::Macros.encapsulateXml(graphOpts, inlineStyle, diagramName, title, saveName, false) end end end @@ -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) == "" @@ -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 @@ -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? @@ -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'] @@ -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 @@ -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 @@ -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 @@ -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==', } } @@ -444,5 +446,9 @@ def js_safe(string) end def svg_enabled? - DrawioSettingsHelper.svg_enabled? + RedmineDrawio::Helpers::DrawioSettingsHelper.svg_enabled? end + + end + end +end \ No newline at end of file diff --git a/lib/redmine_drawio/patches/string_patch.rb b/lib/redmine_drawio/patches/string_patch.rb index 5d8b5aab0..11ddc6265 100644 --- a/lib/redmine_drawio/patches/string_patch.rb +++ b/lib/redmine_drawio/patches/string_patch.rb @@ -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)