@@ -7,6 +7,7 @@ import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkd
7
7
import { toAbsoluteUrl } from '../utils.js' ;
8
8
import { initDropzone } from './common-global.js' ;
9
9
import { POST , GET } from '../modules/fetch.js' ;
10
+ import { showErrorToast } from '../modules/toast.js' ;
10
11
11
12
const { appSubUrl} = window . config ;
12
13
@@ -602,85 +603,69 @@ export function initRepoIssueWipToggle() {
602
603
} ) ;
603
604
}
604
605
605
- async function pullrequest_targetbranch_change ( update_url ) {
606
- const targetBranch = $ ( '#pull-target-branch' ) . data ( 'branch' ) ;
607
- const $branchTarget = $ ( '#branch_target' ) ;
608
- if ( targetBranch === $branchTarget . text ( ) ) {
609
- window . location . reload ( ) ;
610
- return false ;
611
- }
612
- try {
613
- await POST ( update_url , { data : new URLSearchParams ( { target_branch : targetBranch } ) } ) ;
614
- } catch ( error ) {
615
- console . error ( error ) ;
616
- } finally {
617
- window . location . reload ( ) ;
618
- }
619
- }
620
-
621
606
export function initRepoIssueTitleEdit ( ) {
622
- // Edit issue title
623
- const $issueTitle = $ ( '#issue-title' ) ;
624
- const $editInput = $ ( '#edit-title-input input' ) ;
625
-
626
- const editTitleToggle = function ( ) {
627
- toggleElem ( $issueTitle ) ;
628
- toggleElem ( '.not-in-edit' ) ;
629
- toggleElem ( '#edit-title-input' ) ;
630
- toggleElem ( '#pull-desc' ) ;
631
- toggleElem ( '#pull-desc-edit' ) ;
632
- toggleElem ( '.in-edit' ) ;
633
- toggleElem ( '.new-issue-button' ) ;
634
- document . getElementById ( 'issue-title-wrapper' ) ?. classList . toggle ( 'edit-active' ) ;
635
- $editInput [ 0 ] . focus ( ) ;
636
- $editInput [ 0 ] . select ( ) ;
637
- return false ;
638
- } ;
639
-
640
- $ ( '#edit-title' ) . on ( 'click' , editTitleToggle ) ;
641
- $ ( '#cancel-edit-title' ) . on ( 'click' , editTitleToggle ) ;
642
- $ ( '#save-edit-title' ) . on ( 'click' , editTitleToggle ) . on ( 'click' , async function ( ) {
643
- const pullrequest_target_update_url = this . getAttribute ( 'data-target-update-url' ) ;
644
- if ( ! $editInput . val ( ) . length || $editInput . val ( ) === $issueTitle . text ( ) ) {
645
- $editInput . val ( $issueTitle . text ( ) ) ;
646
- await pullrequest_targetbranch_change ( pullrequest_target_update_url ) ;
647
- } else {
648
- try {
649
- const params = new URLSearchParams ( ) ;
650
- params . append ( 'title' , $editInput . val ( ) ) ;
651
- const response = await POST ( this . getAttribute ( 'data-update-url' ) , { data : params } ) ;
652
- const data = await response . json ( ) ;
653
- $editInput . val ( data . title ) ;
654
- $issueTitle . text ( data . title ) ;
655
- if ( pullrequest_target_update_url ) {
656
- await pullrequest_targetbranch_change ( pullrequest_target_update_url ) ; // it will reload the window
657
- } else {
658
- window . location . reload ( ) ;
607
+ const issueTitleDisplay = document . querySelector ( '#issue-title-display' ) ;
608
+ const issueTitleEditor = document . querySelector ( '#issue-title-editor' ) ;
609
+ if ( ! issueTitleEditor ) return ;
610
+
611
+ const issueTitleInput = issueTitleEditor . querySelector ( 'input' ) ;
612
+ const oldTitle = issueTitleInput . getAttribute ( 'data-old-title' ) ;
613
+ issueTitleDisplay . querySelector ( '#issue-title-edit-show' ) . addEventListener ( 'click' , ( ) => {
614
+ hideElem ( issueTitleDisplay ) ;
615
+ hideElem ( '#pull-desc-display' ) ;
616
+ showElem ( issueTitleEditor ) ;
617
+ showElem ( '#pull-desc-editor' ) ;
618
+ if ( ! issueTitleInput . value . trim ( ) ) {
619
+ issueTitleInput . value = oldTitle ;
620
+ }
621
+ issueTitleInput . focus ( ) ;
622
+ } ) ;
623
+ issueTitleEditor . querySelector ( '.ui.cancel.button' ) . addEventListener ( 'click' , ( ) => {
624
+ hideElem ( issueTitleEditor ) ;
625
+ hideElem ( '#pull-desc-editor' ) ;
626
+ showElem ( issueTitleDisplay ) ;
627
+ showElem ( '#pull-desc-display' ) ;
628
+ } ) ;
629
+ const editSaveButton = issueTitleEditor . querySelector ( '.ui.primary.button' ) ;
630
+ editSaveButton . addEventListener ( 'click' , async ( ) => {
631
+ const prTargetUpdateUrl = editSaveButton . getAttribute ( 'data-target-update-url' ) ;
632
+ const newTitle = issueTitleInput . value . trim ( ) ;
633
+ try {
634
+ if ( newTitle && newTitle !== oldTitle ) {
635
+ const resp = await POST ( editSaveButton . getAttribute ( 'data-update-url' ) , { data : new URLSearchParams ( { title : newTitle } ) } ) ;
636
+ if ( ! resp . ok ) {
637
+ throw new Error ( `Failed to update issue title: ${ resp . statusText } ` ) ;
659
638
}
660
- } catch ( error ) {
661
- console . error ( error ) ;
662
639
}
640
+ if ( prTargetUpdateUrl ) {
641
+ const newTargetBranch = document . querySelector ( '#pull-target-branch' ) . getAttribute ( 'data-branch' ) ;
642
+ const oldTargetBranch = document . querySelector ( '#branch_target' ) . textContent ;
643
+ if ( newTargetBranch !== oldTargetBranch ) {
644
+ const resp = await POST ( prTargetUpdateUrl , { data : new URLSearchParams ( { target_branch : newTargetBranch } ) } ) ;
645
+ if ( ! resp . ok ) {
646
+ throw new Error ( `Failed to update PR target branch: ${ resp . statusText } ` ) ;
647
+ }
648
+ }
649
+ }
650
+ window . location . reload ( ) ;
651
+ } catch ( error ) {
652
+ console . error ( error ) ;
653
+ showErrorToast ( error . message ) ;
663
654
}
664
- return false ;
665
655
} ) ;
666
656
}
667
657
668
658
export function initRepoIssueBranchSelect ( ) {
669
- const changeBranchSelect = function ( ) {
670
- const $selectionTextField = $ ( '#pull-target-branch' ) ;
671
-
672
- const baseName = $selectionTextField . data ( 'basename' ) ;
673
- const branchNameNew = $ ( this ) . data ( 'branch' ) ;
674
- const branchNameOld = $selectionTextField . data ( 'branch' ) ;
675
-
676
- // Replace branch name to keep translation from HTML template
677
- $selectionTextField . html ( $selectionTextField . html ( ) . replace (
678
- `${ baseName } :${ branchNameOld } ` ,
679
- `${ baseName } :${ branchNameNew } ` ,
680
- ) ) ;
681
- $selectionTextField . data ( 'branch' , branchNameNew ) ; // update branch name in setting
682
- } ;
683
- $ ( '#branch-select > .item' ) . on ( 'click' , changeBranchSelect ) ;
659
+ document . querySelector ( '#branch-select' ) ?. addEventListener ( 'click' , ( e ) => {
660
+ const el = e . target . closest ( '.item[data-branch]' ) ;
661
+ if ( ! el ) return ;
662
+ const pullTargetBranch = document . querySelector ( '#pull-target-branch' ) ;
663
+ const baseName = pullTargetBranch . getAttribute ( 'data-basename' ) ;
664
+ const branchNameNew = el . getAttribute ( 'data-branch' ) ;
665
+ const branchNameOld = pullTargetBranch . getAttribute ( 'data-branch' ) ;
666
+ pullTargetBranch . textContent = pullTargetBranch . textContent . replace ( `${ baseName } :${ branchNameOld } ` , `${ baseName } :${ branchNameNew } ` ) ;
667
+ pullTargetBranch . setAttribute ( 'data-branch' , branchNameNew ) ;
668
+ } ) ;
684
669
}
685
670
686
671
export function initSingleCommentEditor ( $commentForm ) {
0 commit comments