| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var __ = wp.i18n.__; |
| 4 |
var registerBlockType = wp.blocks.registerBlockType; |
| 5 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 6 |
var PanelBody = wp.components.PanelBody; |
| 7 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 8 |
var SelectControl = wp.components.SelectControl; |
| 9 |
var ToggleControl = wp.components.ToggleControl; |
| 10 |
var RangeControl = wp.components.RangeControl; |
| 11 |
var TextControl = wp.components.TextControl; |
| 12 |
|
| 13 |
var _ddTC, _ddTV; |
| 14 |
function _tc() { return _ddTC || (_ddTC = window.bkbgTypographyControl); } |
| 15 |
function _tv(t, p) { return (_ddTV || (_ddTV = window.bkbgTypoCssVars)) ? _ddTV(t, p) : {}; } |
| 16 |
|
| 17 |
var FORMAT_MAP = { |
| 18 |
'long-date': { opts: { weekday:'long', year:'numeric', month:'long', day:'numeric' } }, |
| 19 |
'short-date': { opts: { year:'numeric', month:'short', day:'numeric' } }, |
| 20 |
'numeric-date': { opts: { year:'numeric', month:'2-digit', day:'2-digit' } }, |
| 21 |
'year-only': null, |
| 22 |
'time-12': { opts: { hour:'numeric', minute:'2-digit', hour12: true } }, |
| 23 |
'time-24': { opts: { hour:'2-digit', minute:'2-digit', hour12: false } }, |
| 24 |
'datetime': { opts: { year:'numeric', month:'short', day:'numeric', hour:'numeric', minute:'2-digit' } } |
| 25 |
}; |
| 26 |
|
| 27 |
function formatDate( format, tz ) { |
| 28 |
var now = new Date(); |
| 29 |
if ( format === 'year-only' ) return String( now.getFullYear() ); |
| 30 |
var entry = FORMAT_MAP[ format ]; |
| 31 |
if ( !entry ) return now.toLocaleDateString(); |
| 32 |
try { |
| 33 |
var opts = Object.assign( {}, entry.opts ); |
| 34 |
if ( tz ) opts.timeZone = tz; |
| 35 |
return now.toLocaleString( navigator.language || 'en', opts ); |
| 36 |
} catch (e) { |
| 37 |
return now.toLocaleString(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
var TIMEZONES = [ |
| 42 |
'UTC','America/New_York','America/Chicago','America/Denver','America/Los_Angeles', |
| 43 |
'America/Sao_Paulo','Europe/London','Europe/Paris','Europe/Berlin','Europe/Moscow', |
| 44 |
'Asia/Dubai','Asia/Kolkata','Asia/Tokyo','Asia/Shanghai','Australia/Sydney' |
| 45 |
]; |
| 46 |
|
| 47 |
registerBlockType( 'blockenberg/dynamic-date', { |
| 48 |
edit: function ( props ) { |
| 49 |
var attrs = props.attributes; |
| 50 |
var setAttr = props.setAttributes; |
| 51 |
var state = wp.element.useState( null ); |
| 52 |
var displayed = wp.element.useState( formatDate( attrs.format, attrs.timezone === 'custom' ? attrs.customTz : undefined ) ); |
| 53 |
|
| 54 |
/* Recompute whenever attrs change */ |
| 55 |
var tz = attrs.timezone === 'custom' ? attrs.customTz : undefined; |
| 56 |
var preview = formatDate( attrs.format, tz ); |
| 57 |
|
| 58 |
var wrapStyle = Object.assign({ |
| 59 |
'--bkdd-color': attrs.textColor || 'inherit', |
| 60 |
'--bkdd-fs': attrs.fontSize + 'px', |
| 61 |
'--bkdd-fw': attrs.fontWeight, |
| 62 |
'--bkdd-lh': attrs.lineHeight, |
| 63 |
'--bkdd-align': attrs.align |
| 64 |
}, _tv(attrs.typoText || {}, '--bkbg-dd-txt-')); |
| 65 |
|
| 66 |
return el( 'div', null, |
| 67 |
el( InspectorControls, null, |
| 68 |
el( PanelBody, { title: __( 'Date Settings', 'blockenberg' ), initialOpen: true }, |
| 69 |
el( SelectControl, { |
| 70 |
label: __( 'Format', 'blockenberg' ), |
| 71 |
value: attrs.format, |
| 72 |
options: [ |
| 73 |
{ label: 'Long Date (Monday, Jan 1, 2024)', value: 'long-date' }, |
| 74 |
{ label: 'Short Date (Jan 1, 2024)', value: 'short-date' }, |
| 75 |
{ label: 'Numeric Date (01/01/2024)', value: 'numeric-date' }, |
| 76 |
{ label: 'Year Only (2024)', value: 'year-only' }, |
| 77 |
{ label: 'Time 12h (1:30 PM)', value: 'time-12' }, |
| 78 |
{ label: 'Time 24h (13:30)', value: 'time-24' }, |
| 79 |
{ label: 'Date & Time', value: 'datetime' } |
| 80 |
], |
| 81 |
onChange: function (v) { setAttr({ format: v }); } |
| 82 |
} ), |
| 83 |
el( SelectControl, { |
| 84 |
label: __( 'Timezone', 'blockenberg' ), |
| 85 |
value: attrs.timezone, |
| 86 |
options: [ |
| 87 |
{ label: 'Site Timezone', value: 'site' }, |
| 88 |
{ label: 'UTC', value: 'utc' }, |
| 89 |
{ label: 'Custom', value: 'custom' } |
| 90 |
], |
| 91 |
onChange: function (v) { setAttr({ timezone: v }); } |
| 92 |
} ), |
| 93 |
attrs.timezone === 'custom' && el( SelectControl, { |
| 94 |
label: __( 'Custom Timezone', 'blockenberg' ), |
| 95 |
value: attrs.customTz, |
| 96 |
options: TIMEZONES.map( function (tz) { return { label: tz, value: tz }; } ), |
| 97 |
onChange: function (v) { setAttr({ customTz: v }); } |
| 98 |
} ), |
| 99 |
el( ToggleControl, { |
| 100 |
label: __( 'Live Clock (auto-update)', 'blockenberg' ), |
| 101 |
checked: attrs.liveClock, |
| 102 |
onChange: function (v) { setAttr({ liveClock: v }); }, |
| 103 |
__nextHasNoMarginBottom: true |
| 104 |
} ) |
| 105 |
), |
| 106 |
el( PanelBody, { title: __( 'Text & Style', 'blockenberg' ), initialOpen: false }, |
| 107 |
el( TextControl, { |
| 108 |
label: __( 'Prefix Text', 'blockenberg' ), |
| 109 |
value: attrs.prefix, |
| 110 |
onChange: function (v) { setAttr({ prefix: v }); } |
| 111 |
} ), |
| 112 |
el( TextControl, { |
| 113 |
label: __( 'Suffix Text', 'blockenberg' ), |
| 114 |
value: attrs.suffix, |
| 115 |
onChange: function (v) { setAttr({ suffix: v }); } |
| 116 |
} ), |
| 117 |
el( SelectControl, { |
| 118 |
label: __( 'HTML Tag', 'blockenberg' ), |
| 119 |
value: attrs.tag, |
| 120 |
options: [ |
| 121 |
{ label: 'span', value: 'span' }, |
| 122 |
{ label: 'p', value: 'p' }, |
| 123 |
{ label: 'div', value: 'div' } |
| 124 |
], |
| 125 |
onChange: function (v) { setAttr({ tag: v }); } |
| 126 |
} ), |
| 127 |
el( SelectControl, { |
| 128 |
label: __( 'Alignment', 'blockenberg' ), |
| 129 |
value: attrs.align, |
| 130 |
options: [ |
| 131 |
{ label: 'Left', value: 'left' }, |
| 132 |
{ label: 'Center', value: 'center' }, |
| 133 |
{ label: 'Right', value: 'right' } |
| 134 |
], |
| 135 |
onChange: function (v) { setAttr({ align: v }); } |
| 136 |
} ), |
| 137 |
), |
| 138 |
|
| 139 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 140 |
_tc() && el(_tc(), { label: __('Text'), typo: attrs.typoText || {}, onChange: function (v) { setAttr({ typoText: v }); }, defaultSize: attrs.fontSize || 16 }) |
| 141 |
), |
| 142 |
el( PanelBody, { title: __( 'Color', 'blockenberg' ), initialOpen: false }, |
| 143 |
el( PanelColorSettings, { |
| 144 |
title: __( 'Color', 'blockenberg' ), |
| 145 |
colorSettings: [ |
| 146 |
{ value: attrs.textColor, onChange: function(v){setAttr({textColor:v||''});}, label: __('Text Color') } |
| 147 |
] |
| 148 |
} ) |
| 149 |
) |
| 150 |
), |
| 151 |
el( 'div', { className: 'bkdd-wrap', style: wrapStyle }, |
| 152 |
el( attrs.tag || 'span', { className: 'bkdd-output' }, |
| 153 |
attrs.prefix && el( 'span', { className: 'bkdd-prefix' }, attrs.prefix ), |
| 154 |
el( 'span', { className: 'bkdd-date' }, preview ), |
| 155 |
attrs.suffix && el( 'span', { className: 'bkdd-suffix' }, attrs.suffix ) |
| 156 |
) |
| 157 |
) |
| 158 |
); |
| 159 |
}, |
| 160 |
save: function ( props ) { |
| 161 |
var attrs = props.attributes; |
| 162 |
return el( 'div', { |
| 163 |
className: 'bkdd-wrap', |
| 164 |
style: Object.assign({ |
| 165 |
'--bkdd-color': attrs.textColor || 'inherit', |
| 166 |
'--bkdd-fs': attrs.fontSize + 'px', |
| 167 |
'--bkdd-fw': attrs.fontWeight, |
| 168 |
'--bkdd-lh': attrs.lineHeight, |
| 169 |
'--bkdd-align': attrs.align |
| 170 |
}, _tv(attrs.typoText || {}, '--bkbg-dd-txt-')), |
| 171 |
'data-format': attrs.format, |
| 172 |
'data-tz': attrs.timezone === 'custom' ? attrs.customTz : attrs.timezone, |
| 173 |
'data-live': attrs.liveClock ? '1' : '0' |
| 174 |
}, |
| 175 |
el( attrs.tag || 'span', { className: 'bkdd-output' }, |
| 176 |
attrs.prefix && el( 'span', { className: 'bkdd-prefix' }, attrs.prefix ), |
| 177 |
el( 'span', { className: 'bkdd-date' }, '' ), |
| 178 |
attrs.suffix && el( 'span', { className: 'bkdd-suffix' }, attrs.suffix ) |
| 179 |
) |
| 180 |
); |
| 181 |
} |
| 182 |
} ); |
| 183 |
} )(); |
| 184 |
|