From 187dcecf5db22b57a476ad9de84452496baa0e4f Mon Sep 17 00:00:00 2001 From: Clemence Kyara Date: Wed, 23 Oct 2024 17:03:46 +0300 Subject: [PATCH 1/6] Update configs to work with @commons-ui/payload --- apps/climatemappedafrica/eslint.webpack.config.js | 1 - apps/climatemappedafrica/jest.config.js | 2 ++ apps/climatemappedafrica/jsconfig.json | 1 + apps/climatemappedafrica/next.config.js | 1 + apps/climatemappedafrica/tsconfig.json | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/climatemappedafrica/eslint.webpack.config.js b/apps/climatemappedafrica/eslint.webpack.config.js index 4242830fc..7e30d0408 100644 --- a/apps/climatemappedafrica/eslint.webpack.config.js +++ b/apps/climatemappedafrica/eslint.webpack.config.js @@ -19,7 +19,6 @@ module.exports = { resolve: { alias: { "@/climatemappedafrica": path.resolve(__dirname, "src/"), - content: path.resolve(__dirname, "content/"), }, extensions: [".js"], }, diff --git a/apps/climatemappedafrica/jest.config.js b/apps/climatemappedafrica/jest.config.js index 69bb7f133..70265641e 100644 --- a/apps/climatemappedafrica/jest.config.js +++ b/apps/climatemappedafrica/jest.config.js @@ -11,6 +11,8 @@ module.exports = { "/../../packages/commons-ui-core/src/$1", "^@/commons-ui/next/(.*)$": "/../../packages/commons-ui-next/src/$1", + "^@/commons-ui/payload/(.*)$": + "/../../packages/commons-ui-payload/src/$1", }, transformIgnorePatterns: ["/node_modules/(?!camelcase-keys)"], }; diff --git a/apps/climatemappedafrica/jsconfig.json b/apps/climatemappedafrica/jsconfig.json index d997fd29f..c2777f419 100644 --- a/apps/climatemappedafrica/jsconfig.json +++ b/apps/climatemappedafrica/jsconfig.json @@ -5,6 +5,7 @@ "@/climatemappedafrica/*": ["./src/*"], "@/commons-ui/core/*": ["../../packages/commons-ui-core/src/*"], "@/commons-ui/next/*": ["../../packages/commons-ui-next/src/*"], + "@/commons-ui/payload/*": ["../../packages/commons-ui-payload/src/*"], "@/hurumap/core/*": ["../../packages/hurumap-core/src/*"], "@/hurumap/next/*": ["../../packages/hurumap-next/src/*"] } diff --git a/apps/climatemappedafrica/next.config.js b/apps/climatemappedafrica/next.config.js index ed809bcc4..9bab1d826 100644 --- a/apps/climatemappedafrica/next.config.js +++ b/apps/climatemappedafrica/next.config.js @@ -28,6 +28,7 @@ module.exports = { transpilePackages: [ "@commons-ui/core", "@commons-ui/next", + "@commons-ui/payload", "@hurumap/core", "@hurumap/next", ], diff --git a/apps/climatemappedafrica/tsconfig.json b/apps/climatemappedafrica/tsconfig.json index f15e19bc5..c73d18f31 100644 --- a/apps/climatemappedafrica/tsconfig.json +++ b/apps/climatemappedafrica/tsconfig.json @@ -18,6 +18,7 @@ "@/climatemappedafrica/*": ["./src/*"], "@/commons-ui/core/*": ["../../packages/commons-ui-core/src/*"], "@/commons-ui/next/*": ["../../packages/commons-ui-next/src/*"], + "@/commons-ui/payload/*": ["../../packages/commons-ui-payload/src/*"], "@/hurumap/core/*": ["../../packages/hurumap-core/src/*"], "@/hurumap/next/*": ["../../packages/hurumap-next/src/*"] } From 866603bd52ed23542a7718137fd16d0d0a801814 Mon Sep 17 00:00:00 2001 From: Clemence Kyara Date: Wed, 23 Oct 2024 17:04:50 +0300 Subject: [PATCH 2/6] Implement extra features in RichText + textAlign, + indent, + media, --- .../commons-ui-payload/src/RichText/Media.js | 42 ++++++++++ .../src/RichText/RichText.js | 84 +++++++++++++++---- packages/commons-ui-payload/src/index.js | 2 + .../src/utils/image-from-media.js | 9 ++ .../commons-ui-payload/src/utils/index.js | 3 + 5 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 packages/commons-ui-payload/src/RichText/Media.js create mode 100644 packages/commons-ui-payload/src/utils/image-from-media.js create mode 100644 packages/commons-ui-payload/src/utils/index.js diff --git a/packages/commons-ui-payload/src/RichText/Media.js b/packages/commons-ui-payload/src/RichText/Media.js new file mode 100644 index 000000000..04e47b2a3 --- /dev/null +++ b/packages/commons-ui-payload/src/RichText/Media.js @@ -0,0 +1,42 @@ +import { Figure, RichTypography } from "@commons-ui/next"; +import React from "react"; + +import { imageFromMedia } from "@/commons-ui/payload/utils"; + +const Media = React.forwardRef(function Media(props, ref) { + const { CaptionProps, ImageProps, media, caption, sx, ...others } = props; + const image = imageFromMedia(media); + + return ( +
+ + {caption} + +
+ ); +}); + +export default Media; diff --git a/packages/commons-ui-payload/src/RichText/RichText.js b/packages/commons-ui-payload/src/RichText/RichText.js index dd1c236bd..235570600 100644 --- a/packages/commons-ui-payload/src/RichText/RichText.js +++ b/packages/commons-ui-payload/src/RichText/RichText.js @@ -4,10 +4,22 @@ import { Box } from "@mui/material"; import React, { Fragment } from "react"; import { Text } from "slate"; +import Media from "./Media"; + const DEFAULT_PROPS = { html: false, }; +const mostIndentedNode = (node) => { + let level = 1; + let indentedNode = node; + while (indentedNode.children?.type === "indent") { + level += 1; + indentedNode = indentedNode.children; + } + return { level, indentedNode }; +}; + const serialize = (children, props) => children?.map((node, i) => { if (Text.isText(node)) { @@ -21,6 +33,12 @@ const serialize = (children, props) => if (node.italic) { text = {text}; } + if (node.strikethrough) { + text = {text}; + } + if (node.underline) { + text = {text}; + } // Handle other leaf types here... @@ -30,75 +48,109 @@ const serialize = (children, props) => if (!node) { return null; } - // TODO(kilemensi): handle node.type === indent + const TypographyProps = { ...DEFAULT_PROPS, ...props.TypographyProps }; switch (node.type) { case "h1": return ( - + {serialize(node.children)} ); case "h2": return ( - + {serialize(node.children)} ); case "h3": return ( - + {serialize(node.children)} ); case "h4": return ( - + {serialize(node.children)} ); case "h5": return ( - + {serialize(node.children)} ); case "h6": return ( - + {serialize(node.children)} ); - case "quote": - return
{serialize(node.children)}
; + case "indent": { + const { level, indentedNode } = mostIndentedNode(node); + // TODO(kilemensi): Can we pass `level` to children & use `text-indent`? + return ( + + {serialize(indentedNode.children, props)} + + ); + } case "link": + // We use RichTypography instead of Link directly just incase props + // such as LinkProps, etc. were passed in. + // TODO(kilemensi): Figure out a better way so that we can use Link directly return ( - + {serialize(node.children)} ); - default: + case "quote": + return
{serialize(node.children)}
; + case "upload": + if (node.relationTo === "media") { + const { caption } = node.fields; + + return ( + + ); + } + // TODO(kilemensi): Do we need to handle other types of uploads such as documents? + return null; + default: { + const { textAlign, type: component } = node; return ( {serialize(node.children, props)} ); + } } }); const RichText = React.forwardRef(function RichText(props, ref) { - const { elements, variant, typographyProps, ...other } = props; + const { elements, variant, MediaProps, TypographyProps, ...other } = props; if (!elements?.length) { return null; } return ( - {serialize(elements, typographyProps)} + {serialize(elements, { MediaProps, TypographyProps })} ); }); diff --git a/packages/commons-ui-payload/src/index.js b/packages/commons-ui-payload/src/index.js index ace4dbf6b..409a9c1a0 100644 --- a/packages/commons-ui-payload/src/index.js +++ b/packages/commons-ui-payload/src/index.js @@ -1,3 +1,5 @@ /* eslint-disable import/prefer-default-export */ +export * from "./utils"; + export { default as RichText } from "./RichText"; diff --git a/packages/commons-ui-payload/src/utils/image-from-media.js b/packages/commons-ui-payload/src/utils/image-from-media.js new file mode 100644 index 000000000..f1c380434 --- /dev/null +++ b/packages/commons-ui-payload/src/utils/image-from-media.js @@ -0,0 +1,9 @@ +function imageFromMedia(media, options) { + const { alt, url } = media || {}; + if (!url?.length) { + return null; + } + return { alt, src: url, ...options }; +} + +export default imageFromMedia; diff --git a/packages/commons-ui-payload/src/utils/index.js b/packages/commons-ui-payload/src/utils/index.js new file mode 100644 index 000000000..8490ce803 --- /dev/null +++ b/packages/commons-ui-payload/src/utils/index.js @@ -0,0 +1,3 @@ +export { default as imageFromMedia } from "./image-from-media"; + +export default undefined; From 9a886a1ebf6a77e97c1bcba5439f57492c386006 Mon Sep 17 00:00:00 2001 From: Clemence Kyara Date: Wed, 23 Oct 2024 17:06:39 +0300 Subject: [PATCH 3/6] Update components to use RichText --- .../src/components/Footer/index.js | 2 +- .../Panel/DesktopPanel/DesktopPanel.js | 1 - .../src/components/Hero/Hero.snap.js | 1 + .../src/components/RichHeader/RichHeader.js | 4 +-- .../src/components/Summary/Summary.js | 35 +++++++++++-------- .../src/components/Summary/Summary.snap.js | 4 +-- .../src/components/Summary/useStyles.js | 32 ----------------- 7 files changed, 26 insertions(+), 53 deletions(-) delete mode 100644 apps/climatemappedafrica/src/components/Summary/useStyles.js diff --git a/apps/climatemappedafrica/src/components/Footer/index.js b/apps/climatemappedafrica/src/components/Footer/index.js index 57e721b1f..aa58eba96 100644 --- a/apps/climatemappedafrica/src/components/Footer/index.js +++ b/apps/climatemappedafrica/src/components/Footer/index.js @@ -69,7 +69,7 @@ function Footer(props) { }, })} elements={description} - typographyProps={{ + TypographyProps={{ LinkProps: { color: "text.secondary", sx: { diff --git a/apps/climatemappedafrica/src/components/HURUmap/Panel/DesktopPanel/DesktopPanel.js b/apps/climatemappedafrica/src/components/HURUmap/Panel/DesktopPanel/DesktopPanel.js index 94a6ab2c7..4dcb508f8 100644 --- a/apps/climatemappedafrica/src/components/HURUmap/Panel/DesktopPanel/DesktopPanel.js +++ b/apps/climatemappedafrica/src/components/HURUmap/Panel/DesktopPanel/DesktopPanel.js @@ -14,7 +14,6 @@ function DesktopPanel({ sx, ...props }) { setValue(undefined); }; const handleValueChange = useCallback((newValue) => { - console.log("BOOM H: ", newValue); // toggle value if the same setValue((oldValue) => (oldValue !== newValue ? newValue : undefined)); }, []); diff --git a/apps/climatemappedafrica/src/components/Hero/Hero.snap.js b/apps/climatemappedafrica/src/components/Hero/Hero.snap.js index 4fc9fb1e1..df36df3f0 100644 --- a/apps/climatemappedafrica/src/components/Hero/Hero.snap.js +++ b/apps/climatemappedafrica/src/components/Hero/Hero.snap.js @@ -76,6 +76,7 @@ exports[` renders unchanged 1`] = ` />