-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Make tasklist checkboxes clickable #15791
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
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eefc914
Update content on checkbox change.
KN4CK3R 035c60e
Fixed test.
KN4CK3R a9a85a3
Replaced jQuery with vanilla javascript.
KN4CK3R 2e16e95
js improvements
silverwind a93f192
fix checkboxes after comment edit
silverwind ed87af7
add tracking dataset for bound checkboxes
silverwind 9c584e3
remove useless async
silverwind 2b718c5
Fixed comparison.
KN4CK3R 90c4d2b
Merge branch 'main' into feature-markdown-tasklist
lafriks 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
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
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import {renderMermaid} from './mermaid.js'; | ||
import {initMarkupTasklist} from './tasklist.js'; | ||
|
||
export async function renderMarkupContent() { | ||
// code that runs for all markup content | ||
export async function initMarkupContent() { | ||
await renderMermaid(document.querySelectorAll('code.language-mermaid')); | ||
} | ||
|
||
// code that only runs for comments | ||
export function initCommentContent() { | ||
initMarkupTasklist(); | ||
} |
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,62 @@ | ||
/** | ||
* Attaches `input` handlers to markdown rendered tasklist checkboxes in comments. | ||
* | ||
* When a checkbox value changes, the corresponding [ ] or [x] in the markdown string | ||
* is set accordingly and sent to the server. On success it updates the raw-content on | ||
* error it resets the checkbox to its original value. | ||
*/ | ||
|
||
const preventListener = (e) => e.preventDefault(); | ||
|
||
export function initMarkupTasklist() { | ||
for (const el of document.querySelectorAll(`.markup[data-can-edit=true]`) || []) { | ||
const container = el.parentNode; | ||
const checkboxes = el.querySelectorAll(`.task-list-item input[type=checkbox]`); | ||
|
||
for (const checkbox of checkboxes) { | ||
if (checkbox.dataset.editable) return; | ||
checkbox.dataset.editable = 'true'; | ||
checkbox.addEventListener('input', async () => { | ||
const checkboxCharacter = checkbox.checked ? 'x' : ' '; | ||
const position = parseInt(checkbox.dataset.sourcePosition) + 1; | ||
|
||
const rawContent = container.querySelector('.raw-content'); | ||
const oldContent = rawContent.textContent; | ||
const newContent = oldContent.substring(0, position) + checkboxCharacter + oldContent.substring(position + 1); | ||
if (newContent === oldContent) return; | ||
|
||
// Prevent further inputs until the request is done. This does not use the | ||
// `disabled` attribute because it causes the border to flash on click. | ||
for (const checkbox of checkboxes) { | ||
checkbox.addEventListener('click', preventListener); | ||
} | ||
|
||
try { | ||
const editContentZone = container.querySelector('.edit-content-zone'); | ||
const {updateUrl, context} = editContentZone.dataset; | ||
|
||
await $.post(updateUrl, { | ||
_csrf: window.config.csrf, | ||
content: newContent, | ||
context, | ||
}); | ||
|
||
rawContent.textContent = newContent; | ||
} catch (err) { | ||
checkbox.checked = !checkbox.checked; | ||
console.error(err); | ||
} | ||
|
||
// Enable input on checkboxes again | ||
for (const checkbox of checkboxes) { | ||
checkbox.removeEventListener('click', preventListener); | ||
} | ||
}); | ||
} | ||
|
||
// Enable the checkboxes as they are initially disabled by the markdown renderer | ||
for (const checkbox of checkboxes) { | ||
checkbox.disabled = false; | ||
} | ||
} | ||
} | ||
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.
Uh oh!
There was an error while loading. Please reload this page.