From a62f00f31e46319b15319f34672a810871be9d1e Mon Sep 17 00:00:00 2001 From: Antoine M Date: Fri, 20 Mar 2026 17:08:48 +0100 Subject: [PATCH] FEATURE Introducing the component --- .../src/document-card/block.json | 54 +++ .../carhop-blocks/src/document-card/edit.js | 342 ++++++++++++++++++ .../src/document-card/editor.scss | 0 .../carhop-blocks/src/document-card/index.js | 31 ++ .../carhop-blocks/src/document-card/save.js | 39 ++ .../src/document-card/style.scss | 0 6 files changed, 466 insertions(+) create mode 100644 plugins/carhop-blocks/src/document-card/block.json create mode 100644 plugins/carhop-blocks/src/document-card/edit.js create mode 100644 plugins/carhop-blocks/src/document-card/editor.scss create mode 100644 plugins/carhop-blocks/src/document-card/index.js create mode 100644 plugins/carhop-blocks/src/document-card/save.js create mode 100644 plugins/carhop-blocks/src/document-card/style.scss diff --git a/plugins/carhop-blocks/src/document-card/block.json b/plugins/carhop-blocks/src/document-card/block.json new file mode 100644 index 0000000..3400fbb --- /dev/null +++ b/plugins/carhop-blocks/src/document-card/block.json @@ -0,0 +1,54 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "carhop-blocks/document-card", + "version": "0.1.0", + "title": "Document Card", + "category": "carhop-blocks", + "icon": "smiley", + "description": "Document Card pour la mise en forme supérieure d'éléments de contenu", + "example": {}, + "supports": { + "html": false, + "color": { + "text": true, + "background": false, + "link": false + } + }, + "textdomain": "document-card", + "editorScript": "file:./index.js", + "editorStyle": "file:./index.css", + "style": "file:./style-index.css", + "viewScript": "file:./view.js", + "attributes": { + "isInitilized": { + "type": "boolean", + "default": false + }, + "documentType": { + "type": "string", + "default": "document", + "enum": [ + "internal", + "external" + ] + }, + "documentFileSize": { + "type": "number", + "default": 0 + }, + "documentUrl": { + "type": "string", + "default": "" + }, + "documentId": { + "type": "number", + "default": 0 + }, + "UserhasValidatedExternalUrl": { + "type": "boolean", + "default": false + } + } +} \ No newline at end of file diff --git a/plugins/carhop-blocks/src/document-card/edit.js b/plugins/carhop-blocks/src/document-card/edit.js new file mode 100644 index 0000000..32c7714 --- /dev/null +++ b/plugins/carhop-blocks/src/document-card/edit.js @@ -0,0 +1,342 @@ +import { __ } from "@wordpress/i18n"; +import { + useBlockProps, + InnerBlocks, + InspectorControls, + MediaUpload, + MediaUploadCheck, + BlockControls, + MediaReplaceFlow, +} from "@wordpress/block-editor"; +import { __experimentalHStack as HStack } from "@wordpress/components"; + +import { + PanelBody, + TextControl, + Button, + Placeholder, + ToolbarButton, + Icon, + __experimentalToggleGroupControl as ToggleGroupControl, + __experimentalToggleGroupControlOption as ToggleGroupControlOption, +} from "@wordpress/components"; +import { file, link, external } from "@wordpress/icons"; + +import { MediaPlaceholder } from "@wordpress/block-editor"; +import "./editor.scss"; + +export default function Edit({ attributes, setAttributes }) { + const { + isInitilized, + documentType, + documentUrl, + documentId, + UserhasValidatedExternalUrl, + documentFileSize, + } = attributes; + + const blockProps = useBlockProps({ + className: "document-card", + }); + + const hasDocument = + (documentType === "internal" && documentId && documentUrl) || + (documentType === "external" && documentUrl && UserhasValidatedExternalUrl); + + const resetDocument = () => { + setAttributes({ + documentId: 0, + documentUrl: "", + documentFileSize: 0, + isInitilized: false, + UserhasValidatedExternalUrl: false, + }); + }; + + const setInternalDocument = (media) => { + const fileSize = media.filesizeInBytes || 0; + const fileSizeInKB = Math.round(fileSize / 1024); + + setAttributes({ + documentType: "internal", + documentFileSize: fileSizeInKB, + documentId: media.id, + documentUrl: media.url, + isInitilized: true, + }); + }; + + const setExternalDocument = (url) => { + setAttributes({ + documentType: "external", + documentFileSize: 0, + documentId: 0, + documentUrl: url, + isInitilized: true, + }); + }; + + return ( + <> + + + { + setAttributes({ documentType: value, isInitilized: true }); + resetDocument(); + }} + > + + + + {documentType === "external" && ( + <> + setExternalDocument(value)} + placeholder={__( + "https://example.com/document.pdf", + "carhop-blocks", + )} + /> + + )} + + {documentType === "internal" && ( + + { + setInternalDocument(media); + }} + allowedTypes={["application/pdf"]} + value={documentId} + render={({ open }) => ( + <> + {documentUrl ? ( +
+ + + + + {documentUrl.split("/").pop()?.split("?")[0] || + __("Document", "carhop-blocks")} + +
+ + +
+
+ ) : ( + + )} + + )} + /> +
+ )} +
+
+ +
+ {!isInitilized && ( + + + + + + + )} + + {isInitilized && + documentType === "internal" && + !documentId && + !documentUrl && ( + setInternalDocument(media)} + accept="application/pdf" + allowedTypes={["application/pdf"]} + multiple={false} + /> + )} + + {isInitilized && + documentType === "external" && + !UserhasValidatedExternalUrl && ( + + setExternalDocument(value)} + placeholder="https://example.com/document.pdf" + style={{ minWidth: 320 }} + /> + + + )} + + {hasDocument && ( + <> + + {documentType === "internal" ? ( + setInternalDocument(media)} + name={__("Remplacer le document", "carhop-blocks")} + /> + ) : null} + resetDocument()}> + {__("Supprimer le document", "carhop-blocks")} + + + +
+
+ + +
+
+ {documentType === "internal" ? "PDF" : "Document externe"} +
+ {documentFileSize > 0 && ( +
+ ( {documentFileSize} Ko ) +
+ )} +
+
+
+ + )} +
+ + ); +} diff --git a/plugins/carhop-blocks/src/document-card/editor.scss b/plugins/carhop-blocks/src/document-card/editor.scss new file mode 100644 index 0000000..e69de29 diff --git a/plugins/carhop-blocks/src/document-card/index.js b/plugins/carhop-blocks/src/document-card/index.js new file mode 100644 index 0000000..9226dae --- /dev/null +++ b/plugins/carhop-blocks/src/document-card/index.js @@ -0,0 +1,31 @@ +import { registerBlockType } from "@wordpress/blocks"; +import "./style.scss"; + +import Edit from "./edit"; +import save from "./save"; +import metadata from "./block.json"; + +registerBlockType(metadata.name, { + icon: { + src: ( + + ), + }, + edit: Edit, + save, +}); diff --git a/plugins/carhop-blocks/src/document-card/save.js b/plugins/carhop-blocks/src/document-card/save.js new file mode 100644 index 0000000..5fe3a37 --- /dev/null +++ b/plugins/carhop-blocks/src/document-card/save.js @@ -0,0 +1,39 @@ +import { useBlockProps } from "@wordpress/block-editor"; +import { InnerBlocks } from "@wordpress/block-editor"; + +export default function save({ attributes }) { + const { documentUrl, documentType, documentFileSize } = attributes; + const blockProps = useBlockProps.save({ + className: "document-card", + }); + + if (!documentUrl) { + return ( +
+ +
+ ); + } + + return ( + +
+ +
+
+ {documentType === "internal" ? "PDF" : "Document externe"} +
+ {documentFileSize > 0 && ( +
( {documentFileSize} KB )
+ )} +
+
+
+ ); +} diff --git a/plugins/carhop-blocks/src/document-card/style.scss b/plugins/carhop-blocks/src/document-card/style.scss new file mode 100644 index 0000000..e69de29