77 *
88 * @package phpMyFAQ
99 * @author Thorsten Rinne <[email protected] > 10- * @copyright 2023 phpMyFAQ Team
10+ * @copyright 2023-2024 phpMyFAQ Team
1111 * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
1212 * @link https://www.phpmyfaq.de
1313 * @since 2023-03-05
@@ -21,7 +21,7 @@ export const handleMarkdownForm = () => {
2121 // Store the height of the textarea
2222 if ( answer ) {
2323 if ( answerHeight !== 'undefined' ) {
24- answer . style . height = parseInt ( answerHeight ) ;
24+ answer . style . height = answerHeight ;
2525 }
2626
2727 answer . addEventListener ( 'mouseup' , ( event ) => {
@@ -33,33 +33,35 @@ export const handleMarkdownForm = () => {
3333 if ( markdownTabs ) {
3434 const tab = document . querySelector ( 'a[data-markdown-tab="preview"]' ) ;
3535
36- tab . addEventListener ( 'shown.bs.tab' , ( event ) => {
36+ tab . addEventListener ( 'shown.bs.tab' , async ( ) => {
3737 const preview = document . getElementById ( 'markdown-preview' ) ;
3838 preview . style . height = answer . style . height ;
3939
40- fetch ( window . location . pathname + 'api/content/markdown' , {
41- method : 'POST' ,
42- headers : {
43- Accept : 'application/json, text/plain, */*' ,
44- 'Content-Type' : 'application/json' ,
45- } ,
46- body : JSON . stringify ( {
47- text : answer . value ,
48- } ) ,
49- } )
50- . then ( async ( response ) => {
51- if ( response . ok ) {
52- return response . json ( ) ;
53- }
54- throw new Error ( 'Network response was not ok: ' , { cause : { response } } ) ;
55- } )
56- . then ( ( response ) => {
57- preview . innerHTML = response . success ;
58- } )
59- . catch ( async ( error ) => {
60- const errorMessage = await error . cause . response . json ( ) ;
61- console . error ( errorMessage ) ;
40+ try {
41+ const response = await fetch ( window . location . pathname + 'api/content/markdown' , {
42+ method : 'POST' ,
43+ headers : {
44+ Accept : 'application/json, text/plain, */*' ,
45+ 'Content-Type' : 'application/json' ,
46+ } ,
47+ body : JSON . stringify ( {
48+ text : answer . value ,
49+ } ) ,
6250 } ) ;
51+
52+ if ( ! response . ok ) {
53+ throw new Error ( 'Network response was not ok' ) ;
54+ }
55+
56+ const responseData = await response . json ( ) ;
57+ preview . innerHTML = responseData . success ;
58+ } catch ( error ) {
59+ if ( error instanceof Error ) {
60+ console . error ( error ) ;
61+ } else {
62+ console . error ( 'Unknown error:' , error ) ;
63+ }
64+ }
6365 } ) ;
6466 }
6567} ;
0 commit comments