From 1f8626ce28144f2762666d637ac2b4b2e4f9e603 Mon Sep 17 00:00:00 2001 From: JC Date: Tue, 18 Jun 2024 05:08:38 +0800 Subject: [PATCH] WDSBT-20 - Add block template --- inc/block-template/README.md | 26 ++++++++ inc/block-template/block/edit.js.mustache | 22 +++++++ inc/block-template/block/editor.scss.mustache | 9 +++ inc/block-template/block/index.js.mustache | 33 ++++++++++ inc/block-template/block/render.php.mustache | 35 +++++++++++ inc/block-template/block/style.scss.mustache | 10 +++ inc/block-template/block/view.js.mustache | 12 ++++ inc/block-template/index.js | 43 +++++++++++++ inc/block-template/plugin/$slug.php.mustache | 49 +++++++++++++++ .../plugin/.editorconfig.mustache | 18 ++++++ inc/block-template/plugin/.eslintrc.mustache | 3 + inc/block-template/plugin/.gitignore.mustache | 30 +++++++++ inc/block-template/plugin/readme.txt.mustache | 61 +++++++++++++++++++ 13 files changed, 351 insertions(+) create mode 100644 inc/block-template/README.md create mode 100644 inc/block-template/block/edit.js.mustache create mode 100644 inc/block-template/block/editor.scss.mustache create mode 100644 inc/block-template/block/index.js.mustache create mode 100644 inc/block-template/block/render.php.mustache create mode 100644 inc/block-template/block/style.scss.mustache create mode 100644 inc/block-template/block/view.js.mustache create mode 100644 inc/block-template/index.js create mode 100644 inc/block-template/plugin/$slug.php.mustache create mode 100644 inc/block-template/plugin/.editorconfig.mustache create mode 100644 inc/block-template/plugin/.eslintrc.mustache create mode 100644 inc/block-template/plugin/.gitignore.mustache create mode 100644 inc/block-template/plugin/readme.txt.mustache diff --git a/inc/block-template/README.md b/inc/block-template/README.md new file mode 100644 index 0000000..9d4a07d --- /dev/null +++ b/inc/block-template/README.md @@ -0,0 +1,26 @@ +# WDS Block Template + +This template is configured to generate a block that is ready for block registration using the [`@wordpress/create-block`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/) tool. + +## Usage + +Run the following in the terminal of your choice: + +`npx @wordpress/create-block --template ../../inc/block-template --no-plugin` + + +## Structure + +Once the command has completed, the following structure will be created: + +``` text +📁src +└── 📁blocks + └── 📁{example-block} + └── block.json + └── edit.js + └── editor.scss + └── index.js + └── render.php + └── style.scss +``` diff --git a/inc/block-template/block/edit.js.mustache b/inc/block-template/block/edit.js.mustache new file mode 100644 index 0000000..d421066 --- /dev/null +++ b/inc/block-template/block/edit.js.mustache @@ -0,0 +1,22 @@ +/** + * WordPress Dependencies + */ +import { __ } from '@wordpress/i18n'; +import { useBlockProps } from '@wordpress/block-editor'; + +/** + * Internal Dependencies + */ +import './editor.scss'; + +/** + * The edit function describes the structure of your block in the context of the + * editor. This represents what the editor will render when the block is used. + * + * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit + */ +export default function Edit() { + return ( +

{__('{{title}} – hello from the editor!', '{{textdomain}}')}

+ ); +} diff --git a/inc/block-template/block/editor.scss.mustache b/inc/block-template/block/editor.scss.mustache new file mode 100644 index 0000000..cd69c16 --- /dev/null +++ b/inc/block-template/block/editor.scss.mustache @@ -0,0 +1,9 @@ +/** + * The following styles get applied inside the editor only. + * + * Replace them with your own styles or remove the file completely. + */ + +.wp-block-{{namespace}}-{{slug}} { + border: 1px dotted var(--wp--preset--color--grey-400); +} diff --git a/inc/block-template/block/index.js.mustache b/inc/block-template/block/index.js.mustache new file mode 100644 index 0000000..54380ea --- /dev/null +++ b/inc/block-template/block/index.js.mustache @@ -0,0 +1,33 @@ +/** + * Registers a new block provided a unique name and an object defining its behavior. + * + * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/ + */ +import { registerBlockType } from '@wordpress/blocks'; + +/** + * Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files. + * All files containing `style` keyword are bundled together. The code used + * gets applied both to the front of your site and to the editor. + * + * @see https://www.npmjs.com/package/@wordpress/scripts#using-css + */ +import './style.scss'; + +/** + * Internal dependencies + */ +import Edit from './edit'; +import metadata from './block.json'; + +/** + * Every block starts by registering a new block type definition. + * + * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/ + */ +registerBlockType(metadata.name, { + /** + * @see ./edit.js + */ + edit: Edit, +}); diff --git a/inc/block-template/block/render.php.mustache b/inc/block-template/block/render.php.mustache new file mode 100644 index 0000000..6984a9c --- /dev/null +++ b/inc/block-template/block/render.php.mustache @@ -0,0 +1,35 @@ + +

+ data-wp-interactive="{{slug}}" + +> + +

+{{/isInteractiveVariant}} +{{#isDynamicVariant}} +?> +

> + +

+{{/isDynamicVariant}} \ No newline at end of file diff --git a/inc/block-template/block/style.scss.mustache b/inc/block-template/block/style.scss.mustache new file mode 100644 index 0000000..9c9e7da --- /dev/null +++ b/inc/block-template/block/style.scss.mustache @@ -0,0 +1,10 @@ +/** + * The following styles get applied both on the front of your site + * and in the editor. + * + * Replace them with your own styles or remove the file completely. + */ + +.wp-block-{{namespace}}-{{slug}} { + border: 1px dotted var(--wp--preset--color--grey-400); +} diff --git a/inc/block-template/block/view.js.mustache b/inc/block-template/block/view.js.mustache new file mode 100644 index 0000000..2711962 --- /dev/null +++ b/inc/block-template/block/view.js.mustache @@ -0,0 +1,12 @@ +{{#isInteractiveVariant}} +/** + * WordPress dependencies + */ +import { store } from '@wordpress/interactivity'; + +store( '{{slug}}', { + state: {}, + actions: {}, + callbacks: {}, +} ); +{{/isInteractiveVariant}} diff --git a/inc/block-template/index.js b/inc/block-template/index.js new file mode 100644 index 0000000..ae36cad --- /dev/null +++ b/inc/block-template/index.js @@ -0,0 +1,43 @@ +/** + * Dependencies + */ +const { join } = require('path'); + +module.exports = { + defaultValues: { + transformer: (view) => { + const { + variantVars: { isInteractiveVariant }, + } = view; + return { + ...view, + requiresAtLeast: isInteractiveVariant ? '6.5' : '6.1', + }; + }, + author: 'WebDevStudios', + category: 'wds-blocks', + dashicon: 'pets', + description: 'A custom block created by the create-block for the theme', + namespace: 'wdsbt', + render: 'file:./render.php', + version: '1.0.0', + customPackageJSON: { + prettier: '@wordpress/prettier-config', + }, + }, + variants: { + dynamic: {}, + interactive: { + viewScriptModule: 'file:./view.js', + customScripts: { + build: 'wp-scripts build --experimental-modules', + start: 'wp-scripts start --experimental-modules', + }, + supports: { + interactive: true, + }, + }, + }, + pluginTemplatesPath: join(__dirname, 'plugin'), + blockTemplatesPath: join(__dirname, 'block'), +}; diff --git a/inc/block-template/plugin/$slug.php.mustache b/inc/block-template/plugin/$slug.php.mustache new file mode 100644 index 0000000..d04ada7 --- /dev/null +++ b/inc/block-template/plugin/$slug.php.mustache @@ -0,0 +1,49 @@ +