diff --git a/CHANGELOG.md b/CHANGELOG.md index 25cc2c2..8e430d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,17 +4,50 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +### [1.2.1](https://github.com/eea/volto-statistic-block/compare/1.2.0...1.2.1) - 22 May 2023 + +#### :bug: Bug Fixes + +- fix: serialize nodes as it should be done [Miu Razvan - [`ebbf6f7`](https://github.com/eea/volto-statistic-block/commit/ebbf6f7e10540a409626a19d7eb6b20e286c3b07)] + +#### :hammer_and_wrench: Others + +- update [Miu Razvan - [`88fe537`](https://github.com/eea/volto-statistic-block/commit/88fe537cde1998d9c6e02f350ec0c70db4290925)] +### [1.2.0](https://github.com/eea/volto-statistic-block/compare/1.1.0...1.2.0) - 22 May 2023 + +#### :rocket: New Features + +- feat: use slate_richtext widget instead of plain text [Miu Razvan - [`98983eb`](https://github.com/eea/volto-statistic-block/commit/98983eb93793c51e7e332ded83c0964fa4f93351)] +- feat: customize slate_richtext widget to add backward compatibility [Miu Razvan - [`aa0ebd2`](https://github.com/eea/volto-statistic-block/commit/aa0ebd2cd8e2c10d3bb5ebb134050423dac2f891)] + +#### :hammer_and_wrench: Others + +- use countup.js@2.5.0 [Miu Razvan - [`9dad099`](https://github.com/eea/volto-statistic-block/commit/9dad099fb6519688752bf4788023712bd9719671)] +- bump version [Miu Razvan - [`e886513`](https://github.com/eea/volto-statistic-block/commit/e886513e63bf480dcdb5c9e0a627b31c6b4e989d)] +- Add Sonarqube tag using industry-frontend addons list [EEA Jenkins - [`7cf6d5b`](https://github.com/eea/volto-statistic-block/commit/7cf6d5b1427a9f17bb1c740d019130e323668286)] ### [1.1.0](https://github.com/eea/volto-statistic-block/compare/1.0.1...1.1.0) - 27 March 2023 #### :hammer_and_wrench: Others -- Release 1.1.0 [Alin Voinea - [`f16c776`](https://github.com/eea/volto-statistic-block/commit/f16c776d4f6f1c00243125f33fd1739d8d7fb641)] +- Add Sonarqube tag using eea-website-frontend addons list [EEA Jenkins - [`1ecdcec`](https://github.com/eea/volto-statistic-block/commit/1ecdcec0ef98c891d9157db9592365fc10760b59)] +- Add Sonarqube tag using ims-frontend addons list [EEA Jenkins - [`e5f76cc`](https://github.com/eea/volto-statistic-block/commit/e5f76cc8f90dfa21ba1f2bb25b2491ae0e1b102d)] +- Add Sonarqube tag using advisory-board-frontend addons list [EEA Jenkins - [`439ba7f`](https://github.com/eea/volto-statistic-block/commit/439ba7f0016eb5ba86ef267309e0ddef7b81f379)] +- Add Sonarqube tag using advisory-board-frontend addons list [EEA Jenkins - [`1e72601`](https://github.com/eea/volto-statistic-block/commit/1e72601f1f9a7e8023c9130407d9ce28f1d7b6f9)] +- Add Sonarqube tag using climate-energy-frontend addons list [EEA Jenkins - [`9ab65ad`](https://github.com/eea/volto-statistic-block/commit/9ab65ad0f5b300a14590f5f260659749f4831956)] - test(Jenkins): Run tests and cypress with latest canary @plone/volto [Alin Voinea - [`75e506f`](https://github.com/eea/volto-statistic-block/commit/75e506fd7cf92296871ddf52f9b1c82a053ec931)] +- Add Sonarqube tag using cca-frontend addons list [EEA Jenkins - [`9297e9f`](https://github.com/eea/volto-statistic-block/commit/9297e9fb8a638a1564d7d7a6d2ebbea0056d775e)] +- Add Sonarqube tag using bise-frontend addons list [EEA Jenkins - [`6dde4aa`](https://github.com/eea/volto-statistic-block/commit/6dde4aae3c87bfe434a28719479e37078db11099)] +- yarn 3 [Alin Voinea - [`b4949e1`](https://github.com/eea/volto-statistic-block/commit/b4949e18f23a7965c76ef97a5790607a587987dc)] +- Add Sonarqube tag using marine-frontend addons list [EEA Jenkins - [`8820afa`](https://github.com/eea/volto-statistic-block/commit/8820afa4ab8fcebd17e90522196c1fde8b246e21)] +- Add Sonarqube tag using clms-frontend addons list [EEA Jenkins - [`8eb5b8d`](https://github.com/eea/volto-statistic-block/commit/8eb5b8de3f87152cb57faeb960233791b328c900)] +- Add Sonarqube tag using demo-kitkat-frontend addons list [EEA Jenkins - [`58f36bd`](https://github.com/eea/volto-statistic-block/commit/58f36bd4ae9dcdccbef182789957dbf0bb97da74)] +- Add Sonarqube tag using forests-frontend addons list [EEA Jenkins - [`3bb9a21`](https://github.com/eea/volto-statistic-block/commit/3bb9a2128a88c1196258722112a1957f60d332fc)] ### [1.0.1](https://github.com/eea/volto-statistic-block/compare/1.0.0...1.0.1) - 16 November 2022 #### :hammer_and_wrench: Others - test(estlint): Fix .project.eslintrc.js [Alin Voinea - [`d81878f`](https://github.com/eea/volto-statistic-block/commit/d81878fff2ba1be4446dd330560c46d8161f8bc2)] +- Add Sonarqube tag using circularity-frontend addons list [EEA Jenkins - [`a58f436`](https://github.com/eea/volto-statistic-block/commit/a58f436c5bb052fcdd13b213895c497881e48c67)] ## [1.0.0](https://github.com/eea/volto-statistic-block/compare/0.3.0...1.0.0) - 28 October 2022 #### :nail_care: Enhancements @@ -26,6 +59,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### :hammer_and_wrench: Others - Cleanup [Alin Voinea - [`6d5f4a6`](https://github.com/eea/volto-statistic-block/commit/6d5f4a6d8048689e97e567316f24902d0865de88)] +- Add Sonarqube tag using eea-website-frontend addons list [EEA Jenkins - [`5fd7ba7`](https://github.com/eea/volto-statistic-block/commit/5fd7ba7a4595e157bdb0ebb0932f1d3afd64b369)] ### [0.3.0](https://github.com/eea/volto-statistic-block/compare/0.2.0...0.3.0) - 19 September 2022 #### :nail_care: Enhancements @@ -56,6 +90,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### :hammer_and_wrench: Others - Remove useless from block title [Alin Voinea - [`a3daf0a`](https://github.com/eea/volto-statistic-block/commit/a3daf0ad63666d0cd87eef465f42f525a5a307bc)] +- Add Sonarqube tag using eea-website-frontend addons list [EEA Jenkins - [`4a9ea35`](https://github.com/eea/volto-statistic-block/commit/4a9ea35d0717941e9d96a29e46dc85717140fccf)] ### [0.1.1](https://github.com/eea/volto-statistic-block/compare/0.1.0...0.1.1) - 10 March 2022 #### :hammer_and_wrench: Others @@ -66,4 +101,5 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### :hammer_and_wrench: Others +- yarn bootstrap [Alin Voinea - [`ba85706`](https://github.com/eea/volto-statistic-block/commit/ba857067b16dc04f9bc970b0c0d4afd479bb7528)] - Initial commit [Alin Voinea - [`2e19fe2`](https://github.com/eea/volto-statistic-block/commit/2e19fe2708f89887d2d79f88696e197643ea10f4)] diff --git a/package.json b/package.json index e049108..37a11a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eeacms/volto-statistic-block", - "version": "1.1.0", + "version": "1.2.1", "description": "@eeacms/volto-statistic-block: Volto add-on", "main": "src/index.js", "author": "European Environment Agency: IDM2 A-Team", @@ -17,6 +17,9 @@ "url": "git@github.com:eea/volto-statistic-block.git" }, "addons": [], + "resolutions": { + "react-countup/countup.js": "2.5.0" + }, "dependencies": { "react-countup": "6.3.1", "react-visibility-sensor": "5.1.1" diff --git a/src/RichTextWidget.jsx b/src/RichTextWidget.jsx new file mode 100644 index 0000000..6bd0827 --- /dev/null +++ b/src/RichTextWidget.jsx @@ -0,0 +1,79 @@ +/** + * A Slate widget that uses internal JSON representation as its value + * + */ + +import React from 'react'; +import isUndefined from 'lodash/isUndefined'; +import isString from 'lodash/isString'; +import config from '@plone/volto/registry'; +import { FormFieldWrapper } from '@plone/volto/components'; +import SlateEditor from '@plone/volto-slate/editor/SlateEditor'; +import { createEmptyParagraph } from '@plone/volto-slate/utils/blocks'; + +import '@plone/volto-slate/widgets/style.css'; + +function createParagraph(text) { + return { + type: config.settings.slate.defaultBlockType, + children: [{ text }], + }; +} + +const getValue = (value) => { + if (isUndefined(value) || !isUndefined(value?.data)) { + return [createEmptyParagraph()]; + } + // Previously this was a text field + if (isString(value)) { + return [createParagraph(value)]; + } + return value; +}; + +const SlateRichTextWidget = (props) => { + const { + id, + onChange, + value, + focus, + className, + block, + placeholder, + properties, + readOnly = false, + } = props; + const [selected, setSelected] = React.useState(focus); + + return ( + +
{ + setSelected(true); + }} + onKeyDown={() => {}} + > + { + onChange(id, newValue); + }} + block={block} + selected={selected} + properties={properties} + placeholder={placeholder} + /> +
+
+ ); +}; + +export default SlateRichTextWidget; diff --git a/src/StatisticBlock/View.jsx b/src/StatisticBlock/View.jsx index 87286a7..73f6c49 100644 --- a/src/StatisticBlock/View.jsx +++ b/src/StatisticBlock/View.jsx @@ -1,11 +1,20 @@ import React from 'react'; +import isNumber from 'lodash/isNumber'; +import isNaN from 'lodash/isNaN'; import cx from 'classnames'; import CountUp from 'react-countup'; import VisibilitySensor from 'react-visibility-sensor'; import { Statistic } from 'semantic-ui-react'; import { UniversalLink } from '@plone/volto/components'; -import { serializeText } from '@eeacms/volto-statistic-block/helpers'; -import { getFieldURL } from '@eeacms/volto-statistic-block/helpers'; +import { + serializeNodes as _serializeNodes, + serializeNodesToText, +} from '@plone/volto-slate/editor/render'; +import { + getFieldURL, + serializeToNodes, + serializeNodes, +} from '@eeacms/volto-statistic-block/helpers'; import './styles.less'; @@ -65,16 +74,19 @@ const View = ({ data, mode }) => { > {items.map((item, index) => { const href = getFieldURL(item.href); - const StatisticWrapper = href ? UniversalLink : Statistic; + const StatisticWrapper = item.href ? UniversalLink : Statistic; + const valueNodes = serializeToNodes(item.value); + const valueNo = Number(serializeNodesToText(valueNodes)); + return ( - - {animation.enabled ? ( + + {animation.enabled && isNumber(valueNo) && !isNaN(valueNo) ? ( 0 ? animation.duration : 2} decimals={animation.decimals > 0 ? animation.decimals : 0} prefix={animation.prefix || ''} @@ -83,14 +95,14 @@ const View = ({ data, mode }) => { {(props) => } ) : ( - item.value + _serializeNodes(valueNodes) )} - - {item.label} + + {serializeNodes(item.label)}
- {serializeText(item.info)} + {serializeNodes(item.info)}
); diff --git a/src/StatisticBlock/schema.js b/src/StatisticBlock/schema.js index 5dfeb3a..a7c5eb1 100644 --- a/src/StatisticBlock/schema.js +++ b/src/StatisticBlock/schema.js @@ -13,9 +13,11 @@ const statisticSchema = { properties: { value: { title: 'Value', + widget: 'slate_richtext', }, label: { title: 'Label', + widget: 'slate_richtext', }, info: { title: 'Extra info', diff --git a/src/helpers.js b/src/helpers.js index f478b57..c3230f2 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,20 +1,40 @@ -import { serializeNodes } from '@plone/volto-slate/editor/render'; +import { Node } from 'slate'; import isArray from 'lodash/isArray'; import isObject from 'lodash/isObject'; import isString from 'lodash/isString'; +import config from '@plone/volto/registry'; import { isInternalURL, flattenToAppURL } from '@plone/volto/helpers'; +import { serializeNodes as _serializeNodes } from '@plone/volto-slate/editor/render'; -export const serializeText = (text) => { - return isArray(text) ? serializeNodes(text) : text; -}; +function isValidNodesArray(value) { + return isArray(value) && Node.isNodeList(value); +} + +export function createParagraph(text) { + return { + type: config.settings.slate.defaultBlockType, + children: [{ text }], + }; +} + +export function serializeToNodes(text) { + if (isValidNodesArray(text)) return text; + if (isString(text)) return [createParagraph(text)]; + return [createParagraph('')]; +} + +export function serializeNodes(value) { + const nodes = serializeToNodes(value); + return _serializeNodes(nodes); +} export const getFieldURL = (data) => { let url = data; const _isObject = data && isObject(data) && !isArray(data); if (_isObject && data['@type'] === 'URL') { - url = data['value'] || data['url'] || data['href'] || data; + url = data['value'] ?? data['url'] ?? data['href'] ?? data; } else if (_isObject) { - url = data['@id'] || data['url'] || data['href'] || data; + url = data['@id'] ?? data['url'] ?? data['href'] ?? data; } if (isArray(data)) { url = data.map((item) => getFieldURL(item)); diff --git a/src/index.js b/src/index.js index 64b7997..9a5f2ca 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,10 @@ import installStatisticBlock from './StatisticBlock'; +import RichTextWidget from './RichTextWidget'; const applyConfig = (config) => { + config.widgets.widget.slate = RichTextWidget; + config.widgets.widget.slate_richtext = RichTextWidget; + return [installStatisticBlock].reduce((acc, apply) => apply(acc), config); };