Project

General

Profile

Feature #39130 » 0001-WIP.patch

Marius BĂLTEANU, 2025-07-28 22:55

View differences:

app/controllers/issues_controller.rb
def update
return unless update_issue_from_params
if params[:sourcepos].present?
@issue.update_task_list_item(:description, params[:sourcepos], params[:checked])
end
attachments = params[:attachments] || params.dig(:issue, :uploads)
if @issue.attachments_addable?
@issue.save_attachments(attachments)
app/controllers/wiki_controller.rb
@section = params[:section].to_i
@section_hash = params[:section_hash]
@content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(@section, @text, @section_hash)
elsif params[:sourcepos].present?
@content.update_task_list_item(:text, params[:sourcepos], params[:checked])
else
@content.version = content_params[:version] if content_params[:version]
@content.text = @text
app/javascript/controllers/task_list_controller.js
import {Controller} from '@hotwired/stimulus'
export default class extends Controller {
// Define the 'updateUrl' value that we passed from the HTML
static values = {
updateUrl: String,
};
connect() {
// Find all checkboxes within this controller's scope and enable them.
this.element.querySelectorAll('input[type="checkbox"].task-list-item-checkbox').forEach(checkbox => {
checkbox.disabled = false;
checkbox.addEventListener('change', (event) => {
const isChecked = event.target.checked;
const sourcePosition = checkbox.parentElement.dataset.sourcepos;
if (!sourcePosition) {
console.error('Task item is missing data-sourcepos attribute.');
// Optionally revert the checkbox and show an error
event.target.checked = !isChecked;
return;
}
this.updateTaskListItem(this.updateUrlValue, sourcePosition, isChecked);
});
});
}
async updateTaskListItem(updateUrl, sourcePosition, isChecked) {
// You need to get the CSRF token for Rails
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;
try {
const response = await fetch(updateUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-CSRF-Token': csrfToken,
},
body: JSON.stringify({
sourcepos: sourcePosition,
checked: isChecked,
}),
});
if (!response.ok) {
// Handle server error: revert the checkbox state and alert the user
throw new Error();
}
console.log('Task updated successfully!');
} catch (error) {
console.error('Error:', error);
// Revert the checkbox state
}
}
}
app/models/concerns/task_list_updatable.rb
# app/models/concerns/task_list_updatable.rb
module TaskListUpdatable
extend ActiveSupport::Concern
# Updates a task list item in a text attribute based on its position.
# This method modifies the attribute on the object in memory; it does not save the record.
#
# @param attribute_name [Symbol] The name of the text attribute (e.g., :description).
# @param source_pos [String] The position of the task item, e.g., "10:1-10:15".
# @param checked_value [Object] The value from params indicating the new checked state.
def update_task_list_item(attribute_name, source_pos, checked_value)
text = public_send(attribute_name)
Rails.logger.debug "*****"*88
Rails.logger.debug text.inspect
return if text.blank? || source_pos.blank?
# The line number from sourcepos is 1-based.
line_number = source_pos.to_s.split(':').first.to_i
return if line_number <= 0
lines = text.lines
# The lines array is 0-based, so we subtract 1.
target_line = lines[line_number - 1]
return unless target_line
# Robustly determine the checked state from params.
is_checked = [true, 'true', '1'].include?(checked_value)
if is_checked
# Change first '[ ]' to '[x]' on the line.
target_line.sub!(/\[\s\]/, '[x]')
else
# Change first '[x]' or '[X]' to '[ ]' on the line.
target_line.sub!(/\[x\]/i, '[ ]')
end
public_send(:"#{attribute_name}=", lines.join)
end
end
app/models/issue.rb
before_save :set_parent_id
include Redmine::NestedSet::IssueNestedSet
include Redmine::Reaction::Reactable
include TaskListUpdatable
belongs_to :project
belongs_to :tracker
app/models/wiki_content.rb
class WikiContent < ApplicationRecord
self.locking_column = 'version'
include TaskListUpdatable
belongs_to :page, :class_name => 'WikiPage'
belongs_to :author, :class_name => 'User'
has_many :versions, :class_name => 'WikiContentVersion', :dependent => :delete_all
app/views/issues/show.html.erb
</div>
<p><strong><%=l(:field_description)%></strong></p>
<div id="issue_description_wiki" class="wiki" data-quote-reply-target="content">
<div id="issue_description_wiki" class="wiki"
data-quote-reply-target="content"
data-controller="task-list"
data-task-list-id-value="<%= edit_issue_path(@issue.id) %>">
<%= textilizable @issue, :description, :attachments => @issue.attachments %>
</div>
</div>
app/views/wiki/_content.html.erb
<div class="wiki wiki-page">
<div class="wiki wiki-page"
data-controller="task-list"
data-task-list-update-url-value="<%= project_wiki_page_path(:id => @page.title) %>">
<%= textilizable content, :text, :attachments => content.page.attachments,
:edit_section_links => (@sections_editable && {:controller => 'wiki', :action => 'edit', :project_id => @page.project, :id => @page.title}) %>
</div>
lib/redmine/wiki_formatting/common_mark/formatter.rb
github_pre_lang: false,
hardbreaks: Redmine::Configuration['common_mark_enable_hardbreaks'] == true,
tasklist_classes: true,
sourcepos: true,
}.freeze,
commonmarker_plugins: {
syntax_highlighter: nil
lib/redmine/wiki_formatting/common_mark/markdown_filter.rb
def call
html = Commonmarker.to_html(@text, options: {
extension: extensions,
extension: extensions,
render: render_options,
parse: parse_options
}, plugins: plugins)
}, plugins: plugins)
html.rstrip!
html
lib/redmine/wiki_formatting/common_mark/sanitization_filter.rb
# allow `id` in li element for footnotes
# allow `class` in li element for task list items
allowlist[:attributes]["li"] = %w(id class)
allowlist[:attributes]["li"] = %w(id class data-sourcepos)
allowlist[:transformers].push lambda{|env|
node = env[:node]
return unless node.name == "li"
    (1-1/1)