RedMine自定义--新增复制问题标题和链接按钮

前言:Redmine本身的复制链接按钮只能复制问题的链接详情,复制出来的格式是:ip.xxxx/issues/200   这次自定义出来一个按钮,希望可以复制问题的标题和链接,这样发送问题给别人时能先知道这个问题大概是什么

 

一:首先找到redmine菜单栏的代码路径,在:redmine/app/views/issues的_action_menu.html.erb文件

源代码是这样的:

<div class="contextual">
<%= link_to l(:button_edit), edit_issue_path(@issue),
            :onclick => 'showAndScrollTo("update", "issue_notes"); return false;',
            :class => 'icon icon-edit', :accesskey => accesskey(:edit) if @issue.editable? %>
<%= link_to l(:button_log_time), new_issue_time_entry_path(@issue),
            :class => 'icon icon-time-add' if User.current.allowed_to?(:log_time, @project) %>
<%= watcher_link(@issue, User.current) %>
<%= link_to l(:button_copy), project_copy_issue_path(@project, @issue),
            :class => 'icon icon-copy' if User.current.allowed_to?(:copy_issues, @project) && Issue.allowed_target_projects.any? %>
<%= actions_dropdown do %>
  <%= copy_object_url_link(issue_url(@issue, only_path: false)) %>
  <%= link_to l(:button_delete), issue_path(@issue),
              :data => {:confirm => issues_destroy_confirmation_message(@issue)},
              :method => :delete, :class => 'icon icon-del' if @issue.deletable? %>
<% end %>
</div>

 

可以看到里面有一个复制链接的操作,我们在里面插入一个按钮即可,优化完的代码如下:

<div class="contextual">

<!-- 添加复制按钮,改成按钮元素 -->
  <%= button_to '复制链接和标题', '#', method: :get, id: 'copy-issue-link', class: 'btn btn-primary icon icon-copy', type: 'button' %>

<%= link_to l(:button_edit), edit_issue_path(@issue),
            :onclick => 'showAndScrollTo("update", "issue_notes"); return false;',
            :class => 'icon icon-edit', :accesskey => accesskey(:edit) if @issue.editable? %>
<%= link_to l(:button_log_time), new_issue_time_entry_path(@issue),
            :class => 'icon icon-time-add' if User.current.allowed_to?(:log_time, @project) %>
<%= watcher_link(@issue, User.current) %>
<%= link_to l(:button_copy), project_copy_issue_path(@project, @issue),
            :class => 'icon icon-copy' if User.current.allowed_to?(:copy_issues, @project) && Issue.allowed_target_projects.any? %>
<%= actions_dropdown do %>
  <%= copy_object_url_link(issue_url(@issue, only_path: false)) %>
  <%= link_to l(:button_delete), issue_path(@issue),
              :data => {:confirm => issues_destroy_confirmation_message(@issue)},
              :method => :delete, :class => 'icon icon-del' if @issue.deletable? %>
<% end %>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
  // 获取复制按钮
  const copyButton = document.getElementById('copy-issue-link');

  if (copyButton) {
    copyButton.addEventListener('click', function(event) {
      event.preventDefault(); // 防止默认行为

      // 获取问题的标题和 URL
      const issueTitle = "<%= @issue.subject %>";
      const issueUrl = "<%= issue_url(@issue, only_path: false) %>";

      // 使用换行符将标题和 URL 分开
      const textToCopy = `${issueTitle}\n${issueUrl}`;

      // 创建一个临时的文本区域
      const tempTextArea = document.createElement('textarea');
      tempTextArea.value = textToCopy;

      // 将文本区域添加到文档中
      document.body.appendChild(tempTextArea);

      // 选中文本区域中的文本
      tempTextArea.select();
      tempTextArea.setSelectionRange(0, 99999); // 对于移动设备

      // 执行复制命令
      document.execCommand('copy');

      // 移除临时文本区域
      document.body.removeChild(tempTextArea);

  
    });
  }

});

</script>

使用JS添加一个按钮,前面html直接给按钮文本写死,不需要考虑多语言的问题,用关键字issue.subject获取问题的标题,修改完代码以后,redmine服务重启一次即可生效

复制出来的效果是:

【问题】xxxx问题标题
http://xxx.xxx.xx.xxx:3000/issues/200

 

 

posted @ 2024-11-19 10:22  Hiro-D  阅读(8)  评论(0编辑  收藏  举报