PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.1.0
FrontBlocks for Gutenberg/GeneratePress v1.1.0
trunk 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 ci-artifacts
frontblocks / assets / counter / frontblocks-counter.js
frontblocks / assets / counter Last commit date
frontblocks-counter-runtime.js 8 months ago frontblocks-counter.css 8 months ago frontblocks-counter.js 8 months ago
frontblocks-counter.js
131 lines
1 "use strict";
2
3 var createHigherOrderComponent = wp.compose.createHigherOrderComponent;
4 var _wp$element = wp.element,
5 Fragment = _wp$element.Fragment,
6 useEffect = _wp$element.useEffect;
7 var InspectorControls = wp.blockEditor.InspectorControls;
8 var _wp$components = wp.components,
9 PanelBody = _wp$components.PanelBody,
10 ToggleControl = _wp$components.ToggleControl,
11 TextControl = _wp$components.TextControl;
12 var _wp$data = wp.data,
13 select = _wp$data.select,
14 dispatch = _wp$data.dispatch;
15 var __ = wp.i18n.__;
16 wp.hooks.addFilter('blocks.registerBlockType', 'frontblocks/add-counter-attribute', function (settings, name) {
17 if (name === 'generateblocks/text' || name === 'generateblocks/headline') {
18 settings.attributes = Object.assign(settings.attributes, {
19 isCounterActive: {
20 type: 'boolean',
21 default: false
22 },
23 animationDuration: {
24 type: 'number',
25 default: 2000
26 },
27 finalNumber: {
28 type: 'string',
29 default: ''
30 },
31 numberPrefix: {
32 type: 'string',
33 default: ''
34 },
35 numberSuffix: {
36 type: 'string',
37 default: ''
38 }
39 });
40 }
41 return settings;
42 });
43 var withHeadlineCounterControl = createHigherOrderComponent(function (BlockEdit) {
44 return function (props) {
45 if (props.name !== BLOCK_NAME) {
46 return /*#__PURE__*/React.createElement(BlockEdit, props);
47 }
48 var attributes = props.attributes,
49 setAttributes = props.setAttributes,
50 clientId = props.clientId;
51 var isCounterActive = attributes.isCounterActive,
52 animationDuration = attributes.animationDuration,
53 finalNumber = attributes.finalNumber,
54 numberPrefix = attributes.numberPrefix,
55 numberSuffix = attributes.numberSuffix;
56 var durationInSeconds = animationDuration / 1000;
57 useEffect(function () {
58 if (isCounterActive) {
59 var block = select('core/block-editor').getBlock(clientId);
60 if (!block) return;
61 var formattedNumber = "".concat(numberPrefix).concat(finalNumber).concat(numberSuffix);
62 if (finalNumber && block.attributes.content !== formattedNumber) {
63 dispatch('core/block-editor').updateBlockAttributes(clientId, {
64 content: formattedNumber
65 });
66 }
67 }
68 }, [isCounterActive, finalNumber, numberPrefix, numberSuffix, clientId]);
69 return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(BlockEdit, props), /*#__PURE__*/React.createElement(InspectorControls, null, /*#__PURE__*/React.createElement(PanelBody, {
70 title: __('Frontblocks - Counter Effect', 'frontblocks'),
71 initialOpen: false
72 }, /*#__PURE__*/React.createElement(ToggleControl, {
73 label: __('Activate Counter Effect', 'frontblocks'),
74 help: isCounterActive ? __('The number in the headline will animate on scroll.', 'frontblocks') : __('Enable to activate counting animation.', 'frontblocks'),
75 checked: isCounterActive,
76 onChange: function onChange(val) {
77 return setAttributes({
78 isCounterActive: val
79 });
80 }
81 }), isCounterActive && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TextControl, {
82 label: __('Final Number', 'frontblocks'),
83 value: finalNumber,
84 onChange: function onChange(val) {
85 return setAttributes({
86 finalNumber: val
87 });
88 },
89 help: __('The number to count up to (e.g.: 100).', 'frontblocks')
90 }), /*#__PURE__*/React.createElement(TextControl, {
91 label: __('Number Prefix', 'frontblocks'),
92 value: numberPrefix,
93 onChange: function onChange(val) {
94 return setAttributes({
95 numberPrefix: val
96 });
97 },
98 help: __('Text to display before the number (e.g.: $, €).', 'frontblocks')
99 }), /*#__PURE__*/React.createElement(TextControl, {
100 label: __('Number Suffix', 'frontblocks'),
101 value: numberSuffix,
102 onChange: function onChange(val) {
103 return setAttributes({
104 numberSuffix: val
105 });
106 },
107 help: __('Text to display after the number (e.g.: %, +).', 'frontblocks')
108 }), /*#__PURE__*/React.createElement(TextControl, {
109 label: __('Animation Duration (seconds)', 'frontblocks'),
110 value: durationInSeconds,
111 onChange: function onChange(val) {
112 var seconds = parseFloat(val);
113 var milliseconds = isNaN(seconds) ? 0 : seconds * 1000;
114 setAttributes({
115 animationDuration: milliseconds
116 });
117 },
118 type: "number",
119 step: "0.1",
120 help: __('Time in seconds for the animation.', 'frontblocks')
121 })), /*#__PURE__*/React.createElement("p", {
122 style: {
123 marginTop: '10px',
124 fontSize: '12px',
125 color: '#777'
126 }
127 }, /*#__PURE__*/React.createElement("small", null, __("Make sure the text begins with a number (e.g., '123', '+500', or '€100').", 'frontblocks'))))));
128 };
129 }, 'withHeadlineCounterControl');
130 wp.hooks.addFilter('editor.BlockEdit', 'frontblocks/headline-counter-control', withHeadlineCounterControl);
131