-
Notifications
You must be signed in to change notification settings - Fork 1
Interactive task list checkboxes #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+582
−6
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
engine/app/javascript/controllers/coplan/checkbox_controller.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| export default class extends Controller { | ||
| static values = { planId: String, revision: Number, toggleUrl: String } | ||
| static targets = ["checkbox"] | ||
|
|
||
| toggle(event) { | ||
| const checkbox = event.target | ||
| const lineText = checkbox.dataset.lineText | ||
| if (!lineText) return | ||
| if (this.inflight) { checkbox.checked = !checkbox.checked; return } | ||
|
|
||
| const nowChecked = checkbox.checked | ||
| const oldText = lineText | ||
| const newText = nowChecked | ||
| ? lineText.replace(/([*+-]\s+)\[[ ]\]/, "$1[x]") | ||
| : lineText.replace(/([*+-]\s+)\[[xX]\]/, "$1[ ]") | ||
|
|
||
| // Optimistic UI: update immediately | ||
| checkbox.dataset.lineText = newText | ||
|
|
||
| this.inflight = true | ||
| this.#sendToggle({ checkbox, oldText, newText, nowChecked, retried: false }) | ||
| } | ||
|
|
||
| #sendToggle({ checkbox, oldText, newText, nowChecked, retried }) { | ||
| const token = document.querySelector('meta[name="csrf-token"]')?.content | ||
|
|
||
| fetch(this.toggleUrlValue, { | ||
| method: "PATCH", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "X-CSRF-Token": token, | ||
| "Accept": "application/json" | ||
| }, | ||
| body: JSON.stringify({ | ||
| old_text: oldText, | ||
| new_text: newText, | ||
| base_revision: this.revisionValue | ||
| }) | ||
| }).then(response => { | ||
| if (response.ok) { | ||
| return response.json().then(data => { | ||
| this.revisionValue = data.revision | ||
| this.inflight = false | ||
| }) | ||
| } else if (response.status === 409 && !retried) { | ||
| // Conflict: someone else edited. Update revision and retry once. | ||
| return response.json().then(data => { | ||
| if (data.current_revision) { | ||
| this.revisionValue = data.current_revision | ||
| } | ||
| this.#sendToggle({ checkbox, oldText, newText, nowChecked, retried: true }) | ||
| }) | ||
| } else { | ||
| this.#revert(checkbox, oldText, nowChecked) | ||
| } | ||
| }).catch(() => { | ||
| this.#revert(checkbox, oldText, nowChecked) | ||
| }) | ||
| } | ||
|
|
||
| #revert(checkbox, oldText, nowChecked) { | ||
| checkbox.checked = !nowChecked | ||
| checkbox.dataset.lineText = oldText | ||
| this.inflight = false | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe CoPlan::MarkdownHelper, type: :helper do | ||
| before do | ||
| helper.extend(CoPlan::MarkdownHelper) | ||
| end | ||
|
|
||
| describe "#render_markdown with task lists" do | ||
| it "renders unchecked task items as interactive checkboxes" do | ||
| html = helper.render_markdown("- [ ] Buy milk") | ||
| expect(html).to include('type="checkbox"') | ||
| expect(html).not_to include("disabled") | ||
| expect(html).to include('data-action="coplan--checkbox#toggle"') | ||
| expect(html).to include('data-coplan--checkbox-target="checkbox"') | ||
| end | ||
|
|
||
| it "renders checked task items as checked interactive checkboxes" do | ||
| html = helper.render_markdown("- [x] Buy milk") | ||
| expect(html).to include('type="checkbox"') | ||
| expect(html).to include("checked") | ||
| expect(html).not_to include("disabled") | ||
| end | ||
|
|
||
| it "sets data-line-text to the original markdown line" do | ||
| html = helper.render_markdown("- [ ] Buy milk") | ||
| expect(html).to include('data-line-text="- [ ] Buy milk"') | ||
| end | ||
|
|
||
| it "sets data-line-text for checked items" do | ||
| html = helper.render_markdown("- [x] Done task") | ||
| expect(html).to include('data-line-text="- [x] Done task"') | ||
| end | ||
|
|
||
| it "adds task-list class to parent ul" do | ||
| html = helper.render_markdown("- [ ] Item one\n- [x] Item two") | ||
| expect(html).to include('class="task-list"') | ||
| end | ||
|
|
||
| it "adds task-list-item class to li elements" do | ||
| html = helper.render_markdown("- [ ] Item") | ||
| expect(html).to include("task-list-item") | ||
| end | ||
|
|
||
| it "wraps checkbox li contents in a label for clickability" do | ||
| html = helper.render_markdown("- [ ] Click me") | ||
| doc = Nokogiri::HTML::DocumentFragment.parse(html) | ||
| label = doc.at_css("li.task-list-item label") | ||
| expect(label).to be_present | ||
| expect(label.at_css('input[type="checkbox"]')).to be_present | ||
| end | ||
|
|
||
| it "handles mixed task and non-task items" do | ||
| md = "- [ ] Task item\n- Regular item" | ||
| html = helper.render_markdown(md) | ||
| expect(html).to include('type="checkbox"') | ||
| expect(html).to include("Regular item") | ||
| end | ||
|
|
||
| it "handles multiple task lists" do | ||
| md = "- [ ] First\n- [x] Second\n\nSome text\n\n- [ ] Third" | ||
| html = helper.render_markdown(md) | ||
| doc = Nokogiri::HTML::DocumentFragment.parse(html) | ||
| checkboxes = doc.css('input[type="checkbox"]') | ||
| expect(checkboxes.length).to eq(3) | ||
| expect(checkboxes[0]["data-line-text"]).to eq("- [ ] First") | ||
| expect(checkboxes[1]["data-line-text"]).to eq("- [x] Second") | ||
| expect(checkboxes[2]["data-line-text"]).to eq("- [ ] Third") | ||
| end | ||
|
|
||
| it "does not affect regular lists" do | ||
| html = helper.render_markdown("- Item one\n- Item two") | ||
| expect(html).not_to include('type="checkbox"') | ||
| expect(html).not_to include("task-list") | ||
| end | ||
|
|
||
| it "preserves markdown-rendered wrapper class" do | ||
| html = helper.render_markdown("- [ ] Item") | ||
| expect(html).to include('class="markdown-rendered"') | ||
| end | ||
|
|
||
| it "ignores task lines inside fenced code blocks" do | ||
| md = "```\n- [ ] Fake checkbox\n```\n\n- [ ] Real checkbox" | ||
| html = helper.render_markdown(md) | ||
| doc = Nokogiri::HTML::DocumentFragment.parse(html) | ||
| checkboxes = doc.css('input[type="checkbox"]') | ||
| expect(checkboxes.length).to eq(1) | ||
| expect(checkboxes[0]["data-line-text"]).to eq("- [ ] Real checkbox") | ||
| end | ||
|
|
||
| it "preserves indentation in data-line-text for nested tasks" do | ||
| md = "- [ ] Parent\n - [ ] Nested child" | ||
| html = helper.render_markdown(md) | ||
| doc = Nokogiri::HTML::DocumentFragment.parse(html) | ||
| checkboxes = doc.css('input[type="checkbox"]') | ||
| nested = checkboxes.find { |cb| cb["data-line-text"]&.include?("Nested") } | ||
| expect(nested["data-line-text"]).to eq(" - [ ] Nested child") | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toggle_checkboxapplies client-suppliedold_text/new_textdirectly as areplace_exactoperation, so any user who can hit this endpoint can replace arbitrary unique text in the plan by crafting the JSON payload instead of only flipping[ ]/[x]on a task item. This effectively bypasses the normalupdate?guard for non-checkbox content and turns a checkbox toggle API into a general edit primitive.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 26dd248 — added server-side validation that both
old_textandnew_textmatch the task list pattern (/\A\s*[*+-]\s+\[[ xX]\]\s/). Arbitrary text replacements now get a 422.