| 1 |
import { |
| 2 |
TextareaControl, |
| 3 |
Button, |
| 4 |
Flex, |
| 5 |
Notice, |
| 6 |
Modal, |
| 7 |
} from '@wordpress/components'; |
| 8 |
import { useState } from '@wordpress/element'; |
| 9 |
import { __ } from '@wordpress/i18n'; |
| 10 |
import HtmlParserFooter from './HtmlParserFooter'; |
| 11 |
|
| 12 |
const { |
| 13 |
parseHTMLtoBlocks, |
| 14 |
getFormInnerFields, |
| 15 |
} = JetFormBuilderParser; |
| 16 |
|
| 17 |
export default function HtmlParserModal( {setShowModal} ) { |
| 18 |
const [ html, setHtml ] = useState( '' ); |
| 19 |
const [ formHTML, setFormHTML ] = useState( '' ); |
| 20 |
const [ isParsing, setParsing ] = useState( false ); |
| 21 |
const [ error, setError ] = useState( '' ); |
| 22 |
|
| 23 |
const handleSubmit = () => { |
| 24 |
setParsing( true ); |
| 25 |
setError( '' ); |
| 26 |
|
| 27 |
try { |
| 28 |
const innerHTML = getFormInnerFields( html ); |
| 29 |
|
| 30 |
const styledHTML = ` |
| 31 |
<div class="jet-form-builder-html-parser-preview"> |
| 32 |
<style> |
| 33 |
.jet-form-builder-html-parser-preview { |
| 34 |
pointer-events: none; |
| 35 |
} |
| 36 |
.jet-form-builder-html-parser-preview textarea, |
| 37 |
.jet-form-builder-html-parser-preview input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]), |
| 38 |
.jet-form-builder-html-parser-preview select { |
| 39 |
display: block; |
| 40 |
width: 100% !important; |
| 41 |
max-width: 100% !important; |
| 42 |
margin-bottom: 10px; |
| 43 |
} |
| 44 |
.jet-form-builder-html-parser-preview label { |
| 45 |
display:block; |
| 46 |
margin-bottom:5px; |
| 47 |
} |
| 48 |
.jet-form-builder-html-parser-preview br { |
| 49 |
display:none; |
| 50 |
} |
| 51 |
.jet-form-builder-html-parser-preview input[type="submit"], |
| 52 |
.jet-form-builder-html-parser-preview button { |
| 53 |
display:block; |
| 54 |
border: none; |
| 55 |
background-color: #0071a1; |
| 56 |
color: #fff; |
| 57 |
padding: 10px 20px; |
| 58 |
cursor: pointer; |
| 59 |
} |
| 60 |
</style> |
| 61 |
${innerHTML} |
| 62 |
</div> |
| 63 |
`; |
| 64 |
|
| 65 |
setFormHTML( styledHTML ); |
| 66 |
} catch ( err ) { |
| 67 |
console.error( err ); |
| 68 |
setError( |
| 69 |
err?.message || |
| 70 |
__( 'Failed to parse HTML.', 'jet-form-builder' ) |
| 71 |
); |
| 72 |
} finally { |
| 73 |
setParsing( false ); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
return ( |
| 78 |
<Modal |
| 79 |
style={ { width: '60vw' } } |
| 80 |
onRequestClose={ () => setShowModal( false ) } |
| 81 |
title={ __( 'Import HTML Form', 'jet-form-builder' ) } |
| 82 |
className="jfb-html-parser-modal" |
| 83 |
> |
| 84 |
{ error && ( |
| 85 |
<Notice |
| 86 |
status="error" |
| 87 |
isDismissible |
| 88 |
onRemove={ () => setError( '' ) } |
| 89 |
> |
| 90 |
{ error } |
| 91 |
</Notice> |
| 92 |
) } |
| 93 |
|
| 94 |
{ Boolean( formHTML.length ) ? ( |
| 95 |
<> |
| 96 |
<div |
| 97 |
dangerouslySetInnerHTML={ { __html: formHTML } } |
| 98 |
style={ { |
| 99 |
padding: '2em 1em', |
| 100 |
backgroundColor: '#f6f7f7', |
| 101 |
marginBottom: '1em', |
| 102 |
} } |
| 103 |
/> |
| 104 |
|
| 105 |
<HtmlParserFooter |
| 106 |
clearHTML={ () => setFormHTML( '' ) } |
| 107 |
formHTML={ formHTML } |
| 108 |
rawHTML={ html } |
| 109 |
setShowModal={ setShowModal } |
| 110 |
/> |
| 111 |
</> |
| 112 |
) : ( |
| 113 |
<> |
| 114 |
<TextareaControl |
| 115 |
label={ __( 'Paste your HTML here', 'jet-form-builder' ) } |
| 116 |
value={ html } |
| 117 |
onChange={ setHtml } |
| 118 |
rows={ 7 } |
| 119 |
/> |
| 120 |
|
| 121 |
<ul style={ { |
| 122 |
listStyle: 'disc', |
| 123 |
paddingInlineStart: '1em', |
| 124 |
} }> |
| 125 |
<li>{ __( |
| 126 |
'You can use any HTML code that contains form elements, but only headings, form controls, and buttons will be parsed.', |
| 127 |
'jet-form-builder', |
| 128 |
) }</li> |
| 129 |
<li>{ __( |
| 130 |
`All the code you paste here will be parsed into a single Form, even if it contains more than one <form> tag.`, |
| 131 |
'jet-form-builder', |
| 132 |
) }</li> |
| 133 |
<li>{ __( |
| 134 |
`The parser can only understand form elements and structure; it can't parse styling or behavior.`, |
| 135 |
'jet-form-builder', |
| 136 |
) }</li> |
| 137 |
</ul> |
| 138 |
|
| 139 |
<Flex justify="flex-end" style={ { marginTop: '1em' } }> |
| 140 |
<Button |
| 141 |
isPrimary |
| 142 |
isBusy={ isParsing } |
| 143 |
disabled={ isParsing || !html.trim().length } |
| 144 |
onClick={ handleSubmit } |
| 145 |
> |
| 146 |
{ isParsing |
| 147 |
? __( 'Parsing...', 'jet-form-builder' ) |
| 148 |
: __( 'Parse HTML', 'jet-form-builder' ) } |
| 149 |
</Button> |
| 150 |
</Flex> |
| 151 |
</> |
| 152 |
) } |
| 153 |
</Modal> |
| 154 |
); |
| 155 |
} |
| 156 |
|