@@ -742,14 +742,31 @@ $(function() {
742742 . keydown ( function ( event ) {
743743 if ( ! ( event . ctrlKey || event . metaKey ) )
744744 return ;
745- // don't conflict with text input shortcut
746- if ( document . activeElement . nodeNode == 'INPUT' || document . activeElement . nodeName == 'TEXTAREA' )
747- return ;
748- if ( String . fromCharCode ( event . which ) . toLowerCase ( ) == 'e' ) {
749- if ( $ ( '#cancel-btn:visible' ) . length === 0 ) {
750- event . preventDefault ( ) ;
751- $ ( '#mode-btn' ) . click ( ) ;
752- }
745+ switch ( String . fromCharCode ( event . which ) . toLowerCase ( ) ) {
746+ // ctrl+e or meta+e = enter edit mode
747+ case 'e' :
748+ // don't conflict with text input shortcut
749+ if ( document . activeElement . nodeNode == 'INPUT' || document . activeElement . nodeName == 'TEXTAREA' )
750+ return ;
751+ if ( $ ( '#cancel-btn:visible' ) . length === 0 ) {
752+ event . preventDefault ( ) ;
753+ $ ( '#mode-btn' ) . click ( ) ;
754+ }
755+ break ;
756+
757+ // ctrl+shift+p = toggle comment preview
758+ case 'p' :
759+ if ( event . metaKey || ! event . shiftKey )
760+ return ;
761+ if ( document . activeElement . id == 'comment' ) {
762+ event . preventDefault ( ) ;
763+ $ ( '#comment-preview-tab' ) . click ( ) ;
764+ }
765+ else if ( $ ( '#comment-preview:visible' ) . length !== 0 ) {
766+ event . preventDefault ( ) ;
767+ $ ( '#comment-edit-tab' ) . click ( ) ;
768+ }
769+ break ;
753770 }
754771 } ) ;
755772
@@ -892,6 +909,60 @@ $(function() {
892909 . prop ( 'title' , message )
893910 . show ( ) ;
894911 } ) ;
912+
913+ // comment preview
914+ var last_comment_text = '' ;
915+ $ ( '#comment-tabs li' ) . click ( function ( ) {
916+ var that = $ ( this ) ;
917+ if ( that . hasClass ( 'current' ) )
918+ return ;
919+
920+ // ensure preview's height matches the comment
921+ var comment = $ ( '#comment' ) ;
922+ var preview = $ ( '#comment-preview' ) ;
923+ var comment_height = comment [ 0 ] . offsetHeight ;
924+
925+ // change tabs
926+ $ ( '#comment-tabs li' ) . removeClass ( 'current' ) . attr ( 'aria-selected' , false ) ;
927+ $ ( '.comment-tab' ) . hide ( ) ;
928+ that . addClass ( 'current' ) . attr ( 'aria-selected' , true ) ;
929+ var tab = that . attr ( 'data-tab' ) ;
930+ $ ( '#' + tab ) . show ( ) ;
931+ var focus = that . data ( 'focus' ) ;
932+ if ( focus === '' ) {
933+ document . activeElement . blur ( ) ;
934+ }
935+ else {
936+ $ ( '#' + focus ) . focus ( ) ;
937+ }
938+
939+ // update preview
940+ preview . css ( 'height' , comment_height + 'px' ) ;
941+ if ( tab != 'comment-tab-preview' || last_comment_text == comment . val ( ) )
942+ return ;
943+ $ ( '#preview-throbber' ) . show ( ) ;
944+ preview . html ( '' ) ;
945+ bugzilla_ajax (
946+ {
947+ url : 'rest/bug/comment/render' ,
948+ type : 'POST' ,
949+ data : { text : comment . val ( ) } ,
950+ hideError : true
951+ } ,
952+ function ( data ) {
953+ $ ( '#preview-throbber' ) . hide ( ) ;
954+ preview . html ( data . html ) ;
955+ } ,
956+ function ( message ) {
957+ $ ( '#preview-throbber' ) . hide ( ) ;
958+ var container = $ ( '<div/>' ) ;
959+ container . addClass ( 'preview-error' ) ;
960+ container . text ( message ) ;
961+ preview . html ( container ) ;
962+ }
963+ ) ;
964+ last_comment_text = comment . val ( ) ;
965+ } ) ;
895966} ) ;
896967
897968function confirmUnsafeURL ( url ) {
@@ -921,6 +992,10 @@ function bugzilla_ajax(request, done_fn, error_fn) {
921992 'Bugzilla_api_token=' + encodeURIComponent ( BUGZILLA . api_token ) ;
922993 if ( request . type != 'GET' ) {
923994 request . contentType = 'application/json' ;
995+ request . processData = false ;
996+ if ( request . data && request . data . constructor === Object ) {
997+ request . data = JSON . stringify ( request . data ) ;
998+ }
924999 }
9251000 $ . ajax ( request )
9261001 . done ( function ( data ) {
@@ -936,8 +1011,10 @@ function bugzilla_ajax(request, done_fn, error_fn) {
9361011 } )
9371012 . error ( function ( data ) {
9381013 var message = data . responseJSON ? data . responseJSON . message : 'Unexpected Error' ; // all errors are unexpected :)
939- $ ( '#xhr-error' ) . html ( message ) ;
940- $ ( '#xhr-error' ) . show ( 'fast' ) ;
1014+ if ( ! request . hideError ) {
1015+ $ ( '#xhr-error' ) . html ( message ) ;
1016+ $ ( '#xhr-error' ) . show ( 'fast' ) ;
1017+ }
9411018 if ( error_fn )
9421019 error_fn ( message ) ;
9431020 } ) ;
0 commit comments