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

✨ Block Support Example #1565

Merged
merged 17 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion .github/workflows/nextjs-bundle-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ jobs:
analyze:
strategy:
matrix:
next-dir: ['examples/next/faustwp-getting-started']
next-dir: [
'examples/next/faustwp-getting-started',
'examples/next/block-support'
]

runs-on: ubuntu-latest
steps:
Expand Down
5 changes: 5 additions & 0 deletions examples/next/block-support/.env.local.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Your WordPress site URL
NEXT_PUBLIC_WORDPRESS_URL=https://faustexample.wpengine.com

# Plugin secret found in WordPress Settings->Faust
# FAUST_SECRET_KEY=YOUR_PLUGIN_SECRET
2 changes: 2 additions & 0 deletions examples/next/block-support/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
possibleTypes.json
globalStylesheet.css
3 changes: 3 additions & 0 deletions examples/next/block-support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @faustwp/block-support-example

Example showcasing Faust.js block editor support.
12 changes: 12 additions & 0 deletions examples/next/block-support/faust.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { setConfig } from '@faustwp/core';
import templates from './wp-templates';
import possibleTypes from './possibleTypes.json';

/**
* @type {import('@faustwp/core').FaustConfig}
**/
export default setConfig({
templates,
experimentalPlugins: [],
possibleTypes,
});
18 changes: 18 additions & 0 deletions examples/next/block-support/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { withFaust, getWpHostname } = require('@faustwp/core');

/**
* @type {import('next').NextConfig}
**/
module.exports = withFaust({
reactStrictMode: true,
sassOptions: {
includePaths: ['node_modules'],
},
images: {
domains: [getWpHostname()],
},
i18n: {
locales: ['en'],
defaultLocale: 'en',
},
});
30 changes: 30 additions & 0 deletions examples/next/block-support/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@faustwp/block-support-example",
"private": true,
"version": "0.1.0",
"scripts": {
"predev": "faust generatePossibleTypes && faust generateGlobalStylesheet",
"prebuild": "faust generatePossibleTypes && faust generateGlobalStylesheet",
Comment on lines +6 to +7
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"dev": "faust dev",
"build": "faust build",
"start": "faust start"
},
"dependencies": {
"@apollo/client": "^3.6.6",
"@faustwp/blocks": "2.0.0",
"@faustwp/cli": "1.0.1",
"@faustwp/core": "1.1.0",
"@wordpress/base-styles": "^4.26.0",
"@wordpress/block-library": "^7.19.0",
"classnames": "^2.3.1",
"graphql": "^16.6.0",
"next": "^12.1.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"sass": "^1.54.9"
},
"engines": {
"node": ">=16",
"npm": ">=8"
}
}
16 changes: 16 additions & 0 deletions examples/next/block-support/pages/[...wordpressNode].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getWordPressProps, WordPressTemplate } from '@faustwp/core';

export default function Page(props) {
return <WordPressTemplate {...props} />;
}

export function getStaticProps(ctx) {
return getWordPressProps({ ctx });
}

export async function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
};
}
24 changes: 24 additions & 0 deletions examples/next/block-support/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import '../faust.config';
import React from 'react';
import { useRouter } from 'next/router';
import { WordPressBlocksProvider, fromThemeJson } from '@faustwp/blocks';
import { FaustProvider } from '@faustwp/core';
import blocks from '../wp-blocks';
// import '../styles/blocks.scss'; // <-- Import block styles from WordPress.
import '../globalStylesheet.css'; // <-- Generated by @faustwp/cli

export default function MyApp({ Component, pageProps }) {
const router = useRouter();

return (
<FaustProvider pageProps={pageProps}>
<WordPressBlocksProvider
config={{
blocks,
theme: null,
}}>
<Component {...pageProps} key={router.asPath} />
</WordPressBlocksProvider>
</FaustProvider>
);
}
4 changes: 4 additions & 0 deletions examples/next/block-support/pages/api/faust/[[...route]].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '../../../faust.config';
import { apiRouter } from '@faustwp/core';

export default apiRouter;
9 changes: 9 additions & 0 deletions examples/next/block-support/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getWordPressProps, WordPressTemplate } from '@faustwp/core';

export default function Page(props) {
return <WordPressTemplate {...props} />;
}

export function getStaticProps(ctx) {
return getWordPressProps({ ctx });
}
5 changes: 5 additions & 0 deletions examples/next/block-support/pages/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WordPressTemplate } from '@faustwp/core';

export default function Preview(props) {
return <WordPressTemplate {...props} />;
}
13 changes: 13 additions & 0 deletions examples/next/block-support/styles/blocks.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// WordPress block styles.

@use 'sass:math';

@import '@wordpress/base-styles/mixins';
@import '@wordpress/base-styles/colors.native';
@import '@wordpress/base-styles/z-index';
@import '@wordpress/base-styles/default-custom-properties';
@import '@wordpress/base-styles/colors';
@import '@wordpress/base-styles/variables';
@import '@wordpress/base-styles/breakpoints';
@import '@wordpress/block-library/src/style';
@import '@wordpress/block-library/src/theme';
15 changes: 15 additions & 0 deletions examples/next/block-support/wp-blocks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CoreBlocks } from '@faustwp/blocks';

export default {
CoreParagraph: CoreBlocks.CoreParagraph,
CoreColumns: CoreBlocks.CoreColumns,
CoreColumn: CoreBlocks.CoreColumn,
CoreCode: CoreBlocks.CoreCode,
CoreQuote: CoreBlocks.CoreQuote,
CoreImage: CoreBlocks.CoreImage,
CoreSeparator: CoreBlocks.CoreSeparator,
CoreList: CoreBlocks.CoreList,
CoreButton: CoreBlocks.CoreButton,
CoreButtons: CoreBlocks.CoreButtons,
CoreHeading: CoreBlocks.CoreHeading,
};
69 changes: 69 additions & 0 deletions examples/next/block-support/wp-templates/front-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { gql } from '@apollo/client';
import { flatListToHierarchical } from '@faustwp/core';
import { WordPressBlocksViewer } from '@faustwp/blocks';
import blocks from '../wp-blocks';

export default function Component({ loading, data }) {
// Loading state for previews.
if (loading) {
return <>Loading...</>;
}

const { title, editorBlocks } = data?.page ?? { title: '' };
const blockList = flatListToHierarchical(editorBlocks, { childrenKey: 'innerBlocks' });

return (
<div className='is-layout-constrained'>
<h1>{title}</h1>
<WordPressBlocksViewer blocks={blockList} />
</div>
);
}

Component.query = gql`
${blocks.CoreParagraph.fragments.entry}
${blocks.CoreColumns.fragments.entry}
${blocks.CoreColumn.fragments.entry}
${blocks.CoreCode.fragments.entry}
${blocks.CoreButtons.fragments.entry}
${blocks.CoreButton.fragments.entry}
${blocks.CoreQuote.fragments.entry}
${blocks.CoreImage.fragments.entry}
${blocks.CoreSeparator.fragments.entry}
${blocks.CoreList.fragments.entry}
${blocks.CoreHeading.fragments.entry}
query GetPage(
$databaseId: ID!
$asPreview: Boolean = false
) {
page(id: $databaseId, idType: DATABASE_ID, asPreview: $asPreview) {
title
content
editorBlocks(flat: false) {
name
__typename
renderedHtml
id: clientId
parentId: parentClientId
...${blocks.CoreParagraph.fragments.key}
...${blocks.CoreColumns.fragments.key}
...${blocks.CoreColumn.fragments.key}
...${blocks.CoreCode.fragments.key}
...${blocks.CoreButtons.fragments.key}
...${blocks.CoreButton.fragments.key}
...${blocks.CoreQuote.fragments.key}
...${blocks.CoreImage.fragments.key}
...${blocks.CoreSeparator.fragments.key}
...${blocks.CoreList.fragments.key}
...${blocks.CoreHeading.fragments.key}
}
}
}
`;

Component.variables = ({ databaseId }, ctx) => {
return {
databaseId,
asPreview: ctx?.asPreview,
};
};
5 changes: 5 additions & 0 deletions examples/next/block-support/wp-templates/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import frontPage from './front-page';

export default {
'front-page': frontPage,
};
Loading
Loading