1
- import $ from 'jquery' ;
2
1
import { svg } from '../svg.ts' ;
3
2
import { showErrorToast } from '../modules/toast.ts' ;
4
3
import { GET , POST } from '../modules/fetch.ts' ;
5
- import { showElem } from '../utils/dom.ts' ;
4
+ import { createElementFromHTML , showElem } from '../utils/dom.ts' ;
6
5
import { parseIssuePageInfo } from '../utils.ts' ;
6
+ import { fomanticQuery } from '../modules/fomantic/base.ts' ;
7
7
8
- let i18nTextEdited ;
9
- let i18nTextOptions ;
10
- let i18nTextDeleteFromHistory ;
11
- let i18nTextDeleteFromHistoryConfirm ;
8
+ let i18nTextEdited : string ;
9
+ let i18nTextOptions : string ;
10
+ let i18nTextDeleteFromHistory : string ;
11
+ let i18nTextDeleteFromHistoryConfirm : string ;
12
12
13
- function showContentHistoryDetail ( issueBaseUrl , commentId , historyId , itemTitleHtml ) {
14
- let $dialog = $ ( '.content-history-detail-dialog' ) ;
15
- if ( $dialog . length ) return ;
13
+ function showContentHistoryDetail ( issueBaseUrl : string , commentId : string , historyId : string , itemTitleHtml : string ) {
14
+ let elDetailDialog = document . querySelector ( '.content-history-detail-dialog' ) ;
15
+ if ( elDetailDialog ) return ;
16
16
17
- $dialog = $ ( `
17
+ elDetailDialog = createElementFromHTML ( `
18
18
<div class="ui modal content-history-detail-dialog">
19
19
${ svg ( 'octicon-x' , 16 , 'close icon inside' ) }
20
20
<div class="header tw-flex tw-items-center tw-justify-between">
@@ -29,8 +29,11 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
29
29
</div>
30
30
<div class="comment-diff-data is-loading"></div>
31
31
</div>` ) ;
32
- $dialog . appendTo ( $ ( 'body' ) ) ;
33
- $dialog . find ( '.dialog-header-options' ) . dropdown ( {
32
+ document . body . append ( elDetailDialog ) ;
33
+ const elOptionsDropdown = elDetailDialog . querySelector ( '.ui.dropdown.dialog-header-options' ) ;
34
+ const $fomanticDialog = fomanticQuery ( elDetailDialog ) ;
35
+ const $fomanticDropdownOptions = fomanticQuery ( elOptionsDropdown ) ;
36
+ $fomanticDropdownOptions . dropdown ( {
34
37
showOnFocus : false ,
35
38
allowReselection : true ,
36
39
async onChange ( _value , _text , $item ) {
@@ -46,7 +49,7 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
46
49
const resp = await response . json ( ) ;
47
50
48
51
if ( resp . ok ) {
49
- $dialog . modal ( 'hide' ) ;
52
+ $fomanticDialog . modal ( 'hide' ) ;
50
53
} else {
51
54
showErrorToast ( resp . message ) ;
52
55
}
@@ -60,10 +63,10 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
60
63
}
61
64
} ,
62
65
onHide ( ) {
63
- $ ( this ) . dropdown ( 'clear' , true ) ;
66
+ $fomanticDropdownOptions . dropdown ( 'clear' , true ) ;
64
67
} ,
65
68
} ) ;
66
- $dialog . modal ( {
69
+ $fomanticDialog . modal ( {
67
70
async onShow ( ) {
68
71
try {
69
72
const params = new URLSearchParams ( ) ;
@@ -74,43 +77,46 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
74
77
const response = await GET ( url ) ;
75
78
const resp = await response . json ( ) ;
76
79
77
- const commentDiffData = $dialog . find ( '.comment-diff-data' ) [ 0 ] ;
78
- commentDiffData ? .classList . remove ( 'is-loading' ) ;
80
+ const commentDiffData = elDetailDialog . querySelector ( '.comment-diff-data' ) ;
81
+ commentDiffData . classList . remove ( 'is-loading' ) ;
79
82
commentDiffData . innerHTML = resp . diffHtml ;
80
83
// there is only one option "item[data-option-item=delete]", so the dropdown can be entirely shown/hidden.
81
84
if ( resp . canSoftDelete ) {
82
- showElem ( $dialog . find ( '.dialog-header-options' ) ) ;
85
+ showElem ( elOptionsDropdown ) ;
83
86
}
84
87
} catch ( error ) {
85
88
console . error ( 'Error:' , error ) ;
86
89
}
87
90
} ,
88
91
onHidden ( ) {
89
- $dialog . remove ( ) ;
92
+ $fomanticDialog . remove ( ) ;
90
93
} ,
91
94
} ) . modal ( 'show' ) ;
92
95
}
93
96
94
- function showContentHistoryMenu ( issueBaseUrl , $item , commentId ) {
95
- const $headerLeft = $item . find ( '.comment-header-left' ) ;
97
+ function showContentHistoryMenu ( issueBaseUrl : string , elCommentItem : Element , commentId : string ) {
98
+ const elHeaderLeft = elCommentItem . querySelector ( '.comment-header-left' ) ;
96
99
const menuHtml = `
97
100
<div class="ui dropdown interact-fg content-history-menu" data-comment-id="${ commentId } ">
98
101
• ${ i18nTextEdited } ${ svg ( 'octicon-triangle-down' , 14 , 'dropdown icon' ) }
99
102
<div class="menu">
100
103
</div>
101
104
</div>` ;
102
105
103
- $headerLeft . find ( `.content-history-menu` ) . remove ( ) ;
104
- $headerLeft . append ( $ ( menuHtml ) ) ;
105
- $headerLeft . find ( '.dropdown' ) . dropdown ( {
106
+ elHeaderLeft . querySelector ( `.ui.dropdown.content-history-menu` ) ?. remove ( ) ; // remove the old one if exists
107
+ elHeaderLeft . append ( createElementFromHTML ( menuHtml ) ) ;
108
+
109
+ const elDropdown = elHeaderLeft . querySelector ( '.ui.dropdown.content-history-menu' ) ;
110
+ const $fomanticDropdown = fomanticQuery ( elDropdown ) ;
111
+ $fomanticDropdown . dropdown ( {
106
112
action : 'hide' ,
107
113
apiSettings : {
108
114
cache : false ,
109
115
url : `${ issueBaseUrl } /content-history/list?comment_id=${ commentId } ` ,
110
116
} ,
111
117
saveRemoteData : false ,
112
118
onHide ( ) {
113
- $ ( this ) . dropdown ( 'change values' , null ) ;
119
+ $fomanticDropdown . dropdown ( 'change values' , null ) ;
114
120
} ,
115
121
onChange ( value , itemHtml , $item ) {
116
122
if ( value && ! $item . find ( '[data-history-is-deleted=1]' ) . length ) {
@@ -124,9 +130,9 @@ export async function initRepoIssueContentHistory() {
124
130
const issuePageInfo = parseIssuePageInfo ( ) ;
125
131
if ( ! issuePageInfo . issueNumber ) return ;
126
132
127
- const $itemIssue = $ ( '.repository.issue .timeline-item.comment.first' ) ; // issue(PR) main content
128
- const $comments = $ ( '.repository.issue .comment-list .comment' ) ; // includes: issue(PR) comments, review comments, code comments
129
- if ( ! $itemIssue . length && ! $comments . length ) return ;
133
+ const elIssueDescription = document . querySelector ( '.repository.issue .timeline-item.comment.first' ) ; // issue(PR) main content
134
+ const elComments = document . querySelectorAll ( '.repository.issue .comment-list .comment' ) ; // includes: issue(PR) comments, review comments, code comments
135
+ if ( ! elIssueDescription && ! elComments . length ) return ;
130
136
131
137
const issueBaseUrl = `${ issuePageInfo . repoLink } /issues/${ issuePageInfo . issueNumber } ` ;
132
138
@@ -139,13 +145,13 @@ export async function initRepoIssueContentHistory() {
139
145
i18nTextDeleteFromHistoryConfirm = resp . i18n . textDeleteFromHistoryConfirm ;
140
146
i18nTextOptions = resp . i18n . textOptions ;
141
147
142
- if ( resp . editedHistoryCountMap [ 0 ] && $itemIssue . length ) {
143
- showContentHistoryMenu ( issueBaseUrl , $itemIssue , '0' ) ;
148
+ if ( resp . editedHistoryCountMap [ 0 ] && elIssueDescription ) {
149
+ showContentHistoryMenu ( issueBaseUrl , elIssueDescription , '0' ) ;
144
150
}
145
151
for ( const [ commentId , _editedCount ] of Object . entries ( resp . editedHistoryCountMap ) ) {
146
152
if ( commentId === '0' ) continue ;
147
- const $itemComment = $ ( `#issuecomment-${ commentId } ` ) ;
148
- showContentHistoryMenu ( issueBaseUrl , $itemComment , commentId ) ;
153
+ const elIssueComment = document . querySelector ( `#issuecomment-${ commentId } ` ) ;
154
+ if ( elIssueComment ) showContentHistoryMenu ( issueBaseUrl , elIssueComment , commentId ) ;
149
155
}
150
156
} catch ( error ) {
151
157
console . error ( 'Error:' , error ) ;
0 commit comments