| 1 |
import { useEffect, useRef } from "@wordpress/element"; |
| 2 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 3 |
import { select, subscribe } from "@wordpress/data"; |
| 4 |
|
| 5 |
import Inspector from "./inspector"; |
| 6 |
import CountdownPreview from "./preview"; |
| 7 |
import getCountdownStyles from "./styles"; |
| 8 |
|
| 9 |
const { duplicateBlockIdFix } = window.EBControls; |
| 10 |
|
| 11 |
const pad = (n) => String(n).padStart(2, "0"); |
| 12 |
|
| 13 |
export default function Edit(props) { |
| 14 |
const { attributes, setAttributes, clientId } = props; |
| 15 |
const { blockId, blockStyles, dueTime, resOption } = attributes; |
| 16 |
|
| 17 |
// Keep resOption in sync with the editor's preview device — without relying |
| 18 |
// on the press-bar-only `nx_style_handler` global. |
| 19 |
const deviceRef = useRef(resOption); |
| 20 |
useEffect(() => { |
| 21 |
const sel = select("core/edit-post"); |
| 22 |
const read = () => |
| 23 |
sel && sel.__experimentalGetPreviewDeviceType |
| 24 |
? sel.__experimentalGetPreviewDeviceType() |
| 25 |
: "Desktop"; |
| 26 |
const initial = read(); |
| 27 |
deviceRef.current = initial; |
| 28 |
setAttributes({ resOption: initial }); |
| 29 |
const unsubscribe = subscribe(() => { |
| 30 |
const device = read(); |
| 31 |
if (device && device !== deviceRef.current) { |
| 32 |
deviceRef.current = device; |
| 33 |
setAttributes({ resOption: device }); |
| 34 |
} |
| 35 |
}); |
| 36 |
return () => unsubscribe(); |
| 37 |
}, []); |
| 38 |
|
| 39 |
// Unique, duplicate-safe blockId used to scope this instance's styles. |
| 40 |
useEffect(() => { |
| 41 |
duplicateBlockIdFix({ |
| 42 |
BLOCK_PREFIX: "nx-countdown", |
| 43 |
blockId, |
| 44 |
setAttributes, |
| 45 |
select, |
| 46 |
clientId, |
| 47 |
}); |
| 48 |
}, []); |
| 49 |
|
| 50 |
// Sensible default due date (now + 7 days) for new blocks. |
| 51 |
useEffect(() => { |
| 52 |
if (!dueTime) { |
| 53 |
const d = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000); |
| 54 |
const local = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad( |
| 55 |
d.getDate() |
| 56 |
)}T${pad(d.getHours())}:${pad(d.getMinutes())}`; |
| 57 |
setAttributes({ dueTime: local }); |
| 58 |
} |
| 59 |
}, []); |
| 60 |
|
| 61 |
const styleObj = blockId |
| 62 |
? getCountdownStyles(blockId, attributes) |
| 63 |
: { desktop: "", tab: "", mobile: "" }; |
| 64 |
|
| 65 |
// Persist computed CSS so the PHP render_callback can echo it on the frontend. |
| 66 |
useEffect(() => { |
| 67 |
if (!blockId) return; |
| 68 |
if (JSON.stringify(blockStyles) !== JSON.stringify(styleObj)) { |
| 69 |
setAttributes({ blockStyles: styleObj }); |
| 70 |
} |
| 71 |
}, [attributes, blockId]); |
| 72 |
|
| 73 |
return ( |
| 74 |
<> |
| 75 |
<Inspector attributes={attributes} setAttributes={setAttributes} /> |
| 76 |
<div {...useBlockProps()}> |
| 77 |
<style> |
| 78 |
{` |
| 79 |
${styleObj.desktop} |
| 80 |
|
| 81 |
/* mimmikcssStart */ |
| 82 |
${resOption === "Tablet" ? styleObj.tab : " "} |
| 83 |
${resOption === "Mobile" ? styleObj.tab + styleObj.mobile : " "} |
| 84 |
/* mimmikcssEnd */ |
| 85 |
|
| 86 |
@media all and (max-width: 1024px) { |
| 87 |
/* tabcssStart */ |
| 88 |
${styleObj.tab} |
| 89 |
/* tabcssEnd */ |
| 90 |
} |
| 91 |
|
| 92 |
@media all and (max-width: 767px) { |
| 93 |
/* mobcssStart */ |
| 94 |
${styleObj.mobile} |
| 95 |
/* mobcssEnd */ |
| 96 |
} |
| 97 |
`} |
| 98 |
</style> |
| 99 |
<CountdownPreview blockId={blockId} attributes={attributes} /> |
| 100 |
</div> |
| 101 |
</> |
| 102 |
); |
| 103 |
} |
| 104 |
|