Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page Builder Elements #1931

Merged
merged 24 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
45b094b
fix: add `graphqlApiUrl: cloudfront.getDistributionUrl("/graphql")`
adrians5j Sep 15, 2021
dc744fb
Merge branch 'next' of github.com:webiny/webiny-js into next
adrians5j Sep 19, 2021
ad71434
wip: initial commit
adrians5j Sep 21, 2021
c3cdcc9
fix: replace {dev} with dev
adrians5j Sep 21, 2021
e409fa5
fix: use `@webiny/app-page-builder-elements`
adrians5j Sep 21, 2021
9f57f5d
chore: update yarn.lock
adrians5j Sep 21, 2021
cdf4245
wip: create page elements / styles handlers
adrians5j Sep 24, 2021
01d12fb
wip: perform breakpoints refactor
adrians5j Sep 24, 2021
d933972
wip: before settings refactor
adrians5j Sep 24, 2021
d8adcec
wip: getCss / getThemeCss refactor
adrians5j Sep 25, 2021
8ab459f
feat: add ability to customize link
adrians5j Sep 26, 2021
444e7ca
fix: refactor styles functions
adrians5j Sep 26, 2021
8ee0f9e
fix: rename back to app-page-builder-elements
adrians5j Sep 26, 2021
f0a536b
fix: add poc files
adrians5j Sep 26, 2021
71fda68
fix: fix dependencies
adrians5j Sep 26, 2021
603a60a
fix: fix dependencies
adrians5j Sep 26, 2021
b68a1fe
fix: simplify link argument
adrians5j Sep 27, 2021
5878236
chore: clean up redundant files
adrians5j Sep 27, 2021
a16aee6
chore: update yarn.lock
adrians5j Sep 27, 2021
7841326
chore: clean up redundant files
adrians5j Sep 27, 2021
10c136f
chore: clean up redundant files
adrians5j Sep 27, 2021
e1e45de
chore: update yarn.lock
adrians5j Sep 27, 2021
563aa3f
chore: format code
adrians5j Sep 27, 2021
d179938
chore: remove unused variable
adrians5j Sep 27, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/admin/code/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import "./App.scss";

export const App = () => (
<ApolloProvider client={createApolloClient({ uri: process.env.REACT_APP_GRAPHQL_API_URL })}>
{/*
{/*
<SecurityProvider> is a generic provider of identity information. 3rd party identity providers (like Cognito,
Okta, Auth0) will handle the authentication, and set the information about the user into this provider,
so other parts of the system have a centralized place to fetch user information from.
Expand Down Expand Up @@ -81,8 +81,8 @@ export const App = () => (
<CmsProvider createApolloClient={createApolloClient}>
{/*
<Routes/> is a helper component that loads all "route" plugins, sorts them
in the correct "path" order and renders using the <Switch> component,
so only the matching route is rendered.
in the correct "path" order and renders using the <Switch> component,
so only the matching route is rendered.
*/}
<Routes />
</CmsProvider>
Expand Down
1 change: 1 addition & 0 deletions packages/app-page-builder-elements/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../.babel.react")({ path: __dirname });
21 changes: 21 additions & 0 deletions packages/app-page-builder-elements/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Webiny

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions packages/app-page-builder-elements/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @webiny/app-page-builder-elements
[![](https://img.shields.io/npm/dw/@webiny/app-page-builder-elements.svg)](https://www.npmjs.com/package/@webiny/app-page-builder-elements)
[![](https://img.shields.io/npm/v/@webiny/app-page-builder-elements.svg)](https://www.npmjs.com/package/@webiny/app-page-builder-elements)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

A React component that renders pages created with Webiny Page Builder.

## Install
```
npm install --save @webiny/app-page-builder-elements
```

Or if you prefer yarn:
```
yarn add @webiny/app-page-builder-elements
```
55 changes: 55 additions & 0 deletions packages/app-page-builder-elements/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@webiny/app-page-builder-elements",
"version": "5.14.0",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/webiny/webiny-js.git"
},
"author": {
"name": "Webiny Ltd",
"email": "info@webiny.com",
"url": "https://www.webiny.com"
},
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.5.5",
"@emotion/css": "^11.1.3"
},
"peerDependencies": {
"react": "16.14.0"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.8.3",
"@types/react": "^16.9.56",
"@types/resize-observer-browser": "^0.1.4",
"@webiny/cli": "^5.14.0",
"@webiny/project-utils": "^5.14.0",
"babel-plugin-emotion": "^9.2.8",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-named-asset-import": "^1.0.0-next.3e165448",
"execa": "^5.0.0",
"rimraf": "^3.0.2",
"ttypescript": "^1.5.12",
"typescript": "^4.1.3"
},
"publishConfig": {
"access": "public",
"directory": "dist"
},
"scripts": {
"build": "yarn webiny run build",
"watch": "yarn webiny run watch"
},
"svgo": {
"plugins": {
"removeViewBox": false
}
},
"gitHead": "8476da73b653c89cc1474d968baf55c1b0ae0e5f"
}
11 changes: 11 additions & 0 deletions packages/app-page-builder-elements/src/components/Content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import { Content as ContentType } from "~/types";
import { Element } from "./Element";

export interface Props {
content: ContentType;
}

export const Content: React.FC<Props> = props => {
return <Element element={props.content} />;
};
28 changes: 28 additions & 0 deletions packages/app-page-builder-elements/src/components/Element.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";
import { Element as ElementType } from "~/types";
import { usePageElements } from "~/hooks/usePageElements";
import ErrorBoundary from "./ErrorBoundary";

export interface Props {
element: ElementType;
}

export const Element: React.FC<Props> = props => {
const { renderers } = usePageElements();

const { element } = props;
if (!element) {
return null;
}

const ElementRenderer = renderers[element.type];
if (!ElementRenderer) {
return null;
}

return (
<ErrorBoundary>
<ElementRenderer {...props} />
</ErrorBoundary>
);
};
17 changes: 17 additions & 0 deletions packages/app-page-builder-elements/src/components/Elements.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import { Element as ElementType } from "~/types";
import { Element } from "./Element";

export interface Props {
element: ElementType;
}

export const Elements: React.FC<Props> = props => {
return (
<>
{props.element.elements.map(element => (
<Element key={element.id} element={element} />
))}
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from "react";

class ErrorBoundary extends React.Component<any, { hasError: boolean }> {
constructor(props) {
super(props);
this.state = { hasError: false };
}

static getDerivedStateFromError() {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}

componentDidCatch(error, errorInfo) {
console.log("An error occurred while rendering a page element:");
console.log(error, errorInfo);
}

render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}

return this.props.children;
}
}

export default ErrorBoundary;
138 changes: 138 additions & 0 deletions packages/app-page-builder-elements/src/contexts/PageElements.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import React, { createContext, useCallback } from "react";
import {
Breakpoint,
StylesObjects,
PageElementsContextValue,
PageElementsProviderProps
} from "~/types";
import { css, cx, CSSObject } from "@emotion/css";

export const PageElementsContext = createContext(null);

// Detect if we're working with a per-breakpoint object, or just a set of regular CSS properties.
const isPerBreakpointStylesObject = ({
breakpoints,
styles
}: {
breakpoints: Record<string, Breakpoint>;
styles: StylesObjects;
}): boolean => {
for (const breakpointName in breakpoints) {
if (styles[breakpointName]) {
return true;
}
}
return false;
};

const assignStyles = (args: {
breakpoints: Record<string, Breakpoint>;
styles: StylesObjects;
assignTo?: CSSObject;
}) => {
const { breakpoints, styles = {}, assignTo = {} } = args;
if (isPerBreakpointStylesObject({ breakpoints, styles })) {
for (const breakpointName in breakpoints) {
const breakpoint = breakpoints[breakpointName];
if (styles && styles[breakpointName]) {
if (!assignTo[breakpoint.mediaQuery]) {
assignTo[breakpoint.mediaQuery] = {};
}
Object.assign(assignTo[breakpoint.mediaQuery], styles[breakpointName]);
}
}
} else {
Object.assign(assignTo, styles);
}

return assignTo;
};

export const PageElementsProvider: React.FC<PageElementsProviderProps> = ({
children,
theme,
renderers = {},
modifiers
}) => {
const getElementStyles = useCallback<PageElementsContextValue["getElementStyles"]>(element => {
const styles = {};

for (const modifierName in modifiers.styles) {
assignStyles({
breakpoints: theme.breakpoints,
assignTo: styles,
styles: modifiers.styles[modifierName]({
element,
theme,
renderers,
modifiers
})
});
}

return [styles];
}, []);

const getElementClassNames = useCallback<PageElementsContextValue["getElementClassNames"]>(
element => {
return getElementStyles(element).map(item => css(item));
},
[]
);

const getThemeStyles = useCallback<PageElementsContextValue["getThemeStyles"]>(getStyles => {
let themeStyles = {};
try {
themeStyles = getStyles(theme);
} catch (e) {
// Do nothing.
console.warn("Could not load theme styles:");
console.log(e);
}

const styles = assignStyles({
breakpoints: theme.breakpoints,
styles: themeStyles
});

return [styles];
}, []);

const getThemeClassNames = useCallback<PageElementsContextValue["getThemeClassNames"]>(
getStyles => {
const styles = getThemeStyles(getStyles);
return styles.map(item => css(item));
},
[]
);

const getStyles = useCallback<PageElementsContextValue["getStyles"]>(styles => {
return [styles];
}, []);

const getClassNames = useCallback<PageElementsContextValue["getClassNames"]>(customStyles => {
const styles = getStyles(customStyles);
return styles.map(item => css(item));
}, []);

const value: PageElementsContextValue = {
theme,
renderers,
modifiers,
getElementStyles,
getElementClassNames,
getThemeStyles,
getThemeClassNames,
getStyles,
getClassNames,
combineClassNames: cx
};

return <PageElementsContext.Provider value={value}>{children}</PageElementsContext.Provider>;
};

export const PageElementsConsumer = ({ children }) => (
<PageElementsContext.Consumer>
{props => React.cloneElement(children, props)}
</PageElementsContext.Consumer>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useContext } from "react";
import { PageElementsContext } from "~/contexts/PageElements";
import { PageElementsContextValue } from "~/types";

export function usePageElements(): PageElementsContextValue {
return useContext(PageElementsContext);
}
Loading