| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from "@wordpress/i18n"; |
| 5 |
import { TextControl } from "@wordpress/components"; |
| 6 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 7 |
|
| 8 |
/** |
| 9 |
* Internal dependencies |
| 10 |
*/ |
| 11 |
import formFields from "./settings"; |
| 12 |
import getPenHTML from "./utils/getPenHTML"; |
| 13 |
import getPenID from "./utils/getPenID"; |
| 14 |
import getNewEditorCheck from "./utils/getNewEditorCheck"; |
| 15 |
|
| 16 |
/** |
| 17 |
* The edit function describes the structure of your block in the context of the |
| 18 |
* editor. This represents what the editor will render when the block is used. |
| 19 |
* |
| 20 |
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit |
| 21 |
* |
| 22 |
* @return {WPElement} Element to render. |
| 23 |
*/ |
| 24 |
export default function Edit({ attributes, setAttributes, isSelected }) { |
| 25 |
const { penURL, penID } = attributes; |
| 26 |
const blockProps = useBlockProps(); |
| 27 |
|
| 28 |
return ( |
| 29 |
<div {...blockProps}> |
| 30 |
{!!isSelected && formFields(attributes, setAttributes)} |
| 31 |
<TextControl |
| 32 |
style={{ |
| 33 |
textAlign: "center", |
| 34 |
border: "solid 1px rgba(100,100,100,0.25)", |
| 35 |
}} |
| 36 |
onChange={(value) => |
| 37 |
setAttributes({ |
| 38 |
penURL: value, |
| 39 |
penID: getPenID(value), |
| 40 |
isEditorURL: getNewEditorCheck(value), |
| 41 |
}) |
| 42 |
} |
| 43 |
value={penURL} |
| 44 |
placeholder={__("Enter a Pen URL...")} |
| 45 |
label={null} |
| 46 |
/> |
| 47 |
{penURL === "" || !penURL.length ? ( |
| 48 |
<div className="error"> |
| 49 |
<p style={{ textAlign: "center" }}> |
| 50 |
{__("A Pen URL is required. Please enter one in the field above.")} |
| 51 |
</p> |
| 52 |
</div> |
| 53 |
) : !penID || penID === "" || penID === null ? ( |
| 54 |
<div className="error"> |
| 55 |
<p style={{ textAlign: "center" }}>{__("The pen URL is invalid.")}</p> |
| 56 |
</div> |
| 57 |
) : ( |
| 58 |
getPenHTML(attributes) |
| 59 |
)} |
| 60 |
</div> |
| 61 |
); |
| 62 |
} |
| 63 |
|