1
+ const checkboxMarkdownPattern = / \[ [ x ] ] / g;
2
+
1
3
/**
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.
5
5
* When a checkbox value changes, the corresponding [ ] or [x] in the markdown string is set accordingly and sent to the server.
6
6
* On success it updates the raw-content on error it resets the checkbox to its original value.
7
7
*/
8
8
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' ) ;
15
12
16
- const checkboxMarkdownPattern = / \[ [ x ] ] / g;
13
+ const onChange = async ( ev , cbIndex ) => {
14
+ const $cb = $ ( ev . target ) ;
15
+ const checkboxMarkdown = $cb . is ( ':checked' ) ? '[x]' : '[ ]' ;
17
16
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 ) ) ;
20
20
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 ) ;
24
23
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' ) ;
27
27
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' ) ) ;
30
32
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
+ }
40
37
}
41
- }
42
- } ;
38
+ } ;
43
39
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
+ } ) ;
47
44
} ) ;
48
45
}
49
46
47
+ function enableAll ( $checkboxes ) { $checkboxes . removeAttr ( 'disabled' ) }
48
+ function disableAll ( $checkboxes ) { $checkboxes . attr ( 'disabled' , 'disabled' ) }
49
+
50
50
function submit ( content , url , context ) {
51
51
const csrf = window . config . csrf ;
52
52
@@ -57,7 +57,6 @@ function submit (content, url, context) {
57
57
} ) ;
58
58
}
59
59
60
-
61
60
function replaceNthMatchWith ( n , replaceWith ) {
62
61
let matchIndex = 0 ;
63
62
0 commit comments