Skip to content

Commit 6e824dc

Browse files
Jan LohageJan Lohage
authored andcommitted
Make it work for in any comment block
1 parent face387 commit 6e824dc

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

web_src/js/markdown/checkboxes.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1+
const checkboxMarkdownPattern = /\[[ x]]/g;
2+
13
/**
2-
* Affects first comment of issue page only!
3-
*
4-
* Attaches `change` handlers to the markdown rendered checkboxes.
4+
* Attaches `change` handlers to markdown rendered checkboxes in comments.
55
* When a checkbox value changes, the corresponding [ ] or [x] in the markdown string is set accordingly and sent to the server.
66
* On success it updates the raw-content on error it resets the checkbox to its original value.
77
*/
88
export default function initMarkdownCheckboxes() {
9-
const $segment = $('.page-content.issue .comment.first .segment');
10-
const $checkboxes = $segment.find('.render-content.markdown input:checkbox');
11-
const $rawContent = $segment.find('.raw-content');
12-
13-
const url = $segment.find('.edit-content-zone').data('update-url');
14-
const context = $segment.find('.edit-content-zone').data('context');
9+
$('.comment .segment').each((_, segment) => {
10+
const $segment = $(segment);
11+
const $checkboxes = $segment.find('.render-content.markdown input:checkbox');
1512

16-
const checkboxMarkdownPattern = /\[[ x]]/g;
13+
const onChange = async (ev, cbIndex) => {
14+
const $cb = $(ev.target);
15+
const checkboxMarkdown = $cb.is(':checked') ? '[x]' : '[ ]';
1716

18-
const enableAll = () => $checkboxes.removeAttr('disabled');
19-
const disableAll = () => $checkboxes.attr('disabled', 'disabled');
17+
const $rawContent = $segment.find('.raw-content');
18+
const oldContent = $rawContent.text();
19+
const newContent = oldContent.replace(checkboxMarkdownPattern, replaceNthMatchWith(cbIndex, checkboxMarkdown));
2020

21-
const onChange = async (ev, cbIndex) => {
22-
const $cb = $(ev.target);
23-
const checkboxMarkdown = $cb.is(':checked') ? '[x]' : '[ ]';
21+
if (newContent !== oldContent) {
22+
disableAll($checkboxes);
2423

25-
const oldContent = $rawContent.text();
26-
const newContent = oldContent.replace(checkboxMarkdownPattern, replaceNthMatchWith(cbIndex, checkboxMarkdown));
24+
try {
25+
const url = $segment.find('.edit-content-zone').data('update-url');
26+
const context = $segment.find('.edit-content-zone').data('context');
2727

28-
if (newContent !== oldContent) {
29-
disableAll();
28+
await submit(newContent, url, context);
29+
$rawContent.text(newContent);
30+
} catch (e) {
31+
$cb.prop('checked', !$cb.is(':checked'));
3032

31-
try {
32-
await submit(newContent, url, context);
33-
$rawContent.text(newContent);
34-
} catch (e) {
35-
$cb.prop('checked', !$cb.is(':checked'));
36-
37-
console.error(e);
38-
} finally {
39-
enableAll();
33+
console.error(e);
34+
} finally {
35+
enableAll($checkboxes);
36+
}
4037
}
41-
}
42-
};
38+
};
4339

44-
enableAll();
45-
$checkboxes.each((cbIndex, cb) => {
46-
$(cb).on('change', (ev) => onChange(ev, cbIndex));
40+
enableAll($checkboxes);
41+
$checkboxes.each((cbIndex, cb) => {
42+
$(cb).on('change', (ev) => onChange(ev, cbIndex));
43+
});
4744
});
4845
}
4946

47+
function enableAll ($checkboxes) { $checkboxes.removeAttr('disabled') }
48+
function disableAll ($checkboxes) { $checkboxes.attr('disabled', 'disabled') }
49+
5050
function submit (content, url, context) {
5151
const csrf = window.config.csrf;
5252

@@ -57,7 +57,6 @@ function submit (content, url, context) {
5757
});
5858
}
5959

60-
6160
function replaceNthMatchWith(n, replaceWith) {
6261
let matchIndex = 0;
6362

0 commit comments

Comments
 (0)