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