Skip to content

Add Interactive Block Playground package #220

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

Merged
merged 19 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
78be11c
initial commit
michalczaplinski Apr 28, 2023
d18a4fc
Add "private": "true" to block playground package.json
michalczaplinski May 1, 2023
9aee561
Update block playground license
michalczaplinski May 1, 2023
466c708
block playground: remove main from package.json
michalczaplinski May 1, 2023
4b46046
block playground: update vite config
michalczaplinski May 1, 2023
1242f0b
block playground: Use new Playground API
michalczaplinski May 1, 2023
ae3bfb7
block playground: Rename to `dev:interactivity` in package.json
michalczaplinski May 1, 2023
e09c1a4
block playground: update tsconfigs
michalczaplinski May 1, 2023
720b704
block playground: rename package to @wp-playground/interactive-block-…
michalczaplinski May 1, 2023
7f89795
block playground: rename main.ts to index.ts
michalczaplinski May 1, 2023
9bf1b84
block playground: remove reference to base tsconfig.json
michalczaplinski May 1, 2023
2f26bf9
block playground: Use a blueprint
michalczaplinski May 1, 2023
bbbe6ca
block playground: Remove unused login import
michalczaplinski May 1, 2023
c32b84f
block playground: Remove uneeded stuff from vite config
michalczaplinski May 1, 2023
335be4f
block playground: Add virtual module
michalczaplinski May 1, 2023
431d3df
block playground: formatting
michalczaplinski May 2, 2023
2f6ff6a
block playground: format the readme
michalczaplinski May 2, 2023
1f96f70
Update packages/playground/interactive-block-playground/src/index.ts
michalczaplinski May 9, 2023
ed5c11f
Allow installing plugins/themes from github release pages
michalczaplinski May 9, 2023
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build:website": "nx build playground-website",
"deploy": "gh-pages -d dist/docs -t true",
"dev": "nx dev playground-website",
"dev:interactivity": "nx dev interactive-block-playground",
"lint": "nx run-many --all --target=lint",
"predeploy": "nx build docs-site",
"prepublishOnly": "npm run build",
Expand Down
5 changes: 5 additions & 0 deletions packages/playground/interactive-block-playground/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Interactive Block Playground

Built with [WordPress Playground](https://github.com/WordPress/wordpress-playground).

https://user-images.githubusercontent.com/5417266/233141638-b8143576-fb56-462d-9abb-fce117ba84ba.mov
48 changes: 48 additions & 0 deletions packages/playground/interactive-block-playground/build-zips.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const chokidar = require('chokidar');
const archiver = require('archiver');
const fs = require('fs');

const helloFolderPath = './hello';
const outputZipPath = './zips/hello.zip';

// Function to zip the 'hello' folder and save it as 'hello.zip'
function zipHelloFolder() {
const output = fs.createWriteStream(outputZipPath);
const archive = archiver('zip', {
zlib: { level: 9 }, // Sets the compression level.
});

output.on('close', () => {
console.log(`hello.zip has been created.`);
});

archive.on('error', (err) => {
throw err;
});

archive.pipe(output);

archive.directory(helloFolderPath, false);

archive.finalize();
}

// Watch the 'hello' folder for changes
const watcher = chokidar.watch(helloFolderPath, {
persistent: true,
ignoreInitial: true,
});

watcher
.on('add', (path) => {
console.log(`File ${path} has been added.`);
zipHelloFolder();
})
.on('change', (path) => {
console.log(`File ${path} has been changed.`);
zipHelloFolder();
})
.on('unlink', (path) => {
console.log(`File ${path} has been removed.`);
zipHelloFolder();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "hello/log-block",
"version": "0.1.0",
"title": "Hello - Log block",
"category": "text",
"icon": "heart",
"description": "",
"textdomain": "hello",
"supports": {
"interactivity": true
},
"editorScript": "file:./index.js",
"render": "file:./render.php",
"viewScript": "file:./view.js",
"style": "file:./style.css"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-element'), 'version' => '3cf3fb8a26c0120e24db');
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { registerBlockType } = wp.blocks;
const { useBlockProps } = wp.blockEditor;
const { createElement } = wp.element;

const Edit = () => {
return createElement(
'p',
useBlockProps(),
'Hello World! (from the editor).'
);
};

registerBlockType('hello/log-block', {
edit: Edit,
save: () => null,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
$wrapper_attributes = get_block_wrapper_attributes();

?>

<div
<?php echo $wrapper_attributes; ?>
>
<button
data-wp-on.click="actions.hello.log"
>
HELLO
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
body > :not(.wp-site-blocks) {
display: none;
}

.wp-site-blocks > :not(main) {
display: none;
}

main > :not(.entry-content) {
display: none;
}

.entry-content {
position: absolute;
top: 0;
left: 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => '8ff3477eebdb6c7f40ea');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Disclaimer: Importing the `store` using a global is just a temporary solution.
const { store } = window.__experimentalInteractivity;

store({
actions: {
hello: {
log: () => {
console.log('hello!');
},
},
},
});
61 changes: 61 additions & 0 deletions packages/playground/interactive-block-playground/hello/hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Plugin Name: Hello
* Version: 0.1.1
* Requires at least: 6.0
* Requires PHP: 5.6
* Description: Plugin that demoes the usage of the Interactivity API.
* Author: WordPress Team
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: hello
*
* @package hello
*/

add_action( 'init', 'register_hello_block' );

function register_hello_block() {
if ( file_exists( __DIR__ . '/blocks/hello' ) ) {
register_block_type( __DIR__ . '/blocks/hello');
};
}


add_action( 'wp_enqueue_scripts', 'hello_auto_inject_interactivity_dependency' );

function add_hello_script_dependency( $handle, $dep, $in_footer ) {
global $wp_scripts;

$script = $wp_scripts->query( $handle, 'registered' );
if ( ! $script ) {
return false;
}

if ( ! in_array( $dep, $script->deps, true ) ) {
$script->deps[] = $dep;

if ( $in_footer ) {
// move script to the footer
$wp_scripts->add_data( $handle, 'group', 1 );
}
}

return true;
}

function hello_auto_inject_interactivity_dependency() {
$registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();

foreach ( $registered_blocks as $name => $block ) {
$has_interactivity_support = $block->supports['interactivity'] ?? false;

if ( ! $has_interactivity_support ) {
continue;
}
foreach ( $block->view_script_handles as $handle ) {
add_hello_script_dependency( $handle, 'wp-directive-runtime', true );
}
}
}
17 changes: 17 additions & 0 deletions packages/playground/interactive-block-playground/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.editors {
display: grid;
grid-template-rows: 35px 2fr;
grid-auto-flow: column;
grid-gap: 20px;
justify-content: start;
}

textarea {
width: 387px;
height: 240px;
font-family: monospace;
}

.iframe-container {
position: relative;
}
28 changes: 28 additions & 0 deletions packages/playground/interactive-block-playground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Interactivity API - Interactive docs</title>
<link rel="stylesheet" href="./index.css" />
</head>
<body>
<div class="iframe-container">
<iframe
class="hidden"
id="playground"
src="https://playground.wordpress.net/remote.html"
style="width: 800px; height: 500px"
></iframe>
<div class="iframe-spinner"></div>
</div>
<div class="editors">
<h4>render.php</h4>
<textarea id="render.php" spellcheck="false"></textarea>
<h4>view.js</h4>
<textarea id="view.js" spellcheck="false"></textarea>
</div>
<script type="module" src="/src/index.ts"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions packages/playground/interactive-block-playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@wp-playground/interactive-block-playground",
"version": "1.0.0",
"description": "",
"scripts": {
"build-zips": "node build-zips.js"
},
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"type": "module",
"private": "true"
}
62 changes: 62 additions & 0 deletions packages/playground/interactive-block-playground/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "interactive-block-playground",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/playground/interactive-block-playground/src",
"projectType": "application",
"implicitDependencies": ["playground-remote"],
"targets": {
"build:standalone": {
"executor": "@nrwl/vite:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/packages/playground/interactive-block-playground"
},
"configurations": {
"development": {
"mode": "development"
},
"production": {
"mode": "production"
}
}
},
"dev": {
"executor": "nx:run-commands",
"options": {
"commands": [
"nx dev playground-remote --configuration=development-for-website",
"nx dev:standalone interactive-block-playground --hmr --output-style=stream-without-prefixes"
],
"parallel": true,
"color": true
}
},
"dev:standalone": {
"executor": "@nrwl/vite:dev-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "interactive-block-playground:build"
},
"configurations": {
"development": {
"buildTarget": "interactive-block-playground:build:standalone:development",
"hmr": true
},
"production": {
"buildTarget": "interactive-block-playground:build:standalone:production",
"hmr": false
}
}
},
"typecheck": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": [
"yarn tsc -p packages/playground/interactive-block-playground/tsconfig.json --noEmit"
]
}
}
},
"tags": ["scope:web-client"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { remotePlaygroundOrigin } from 'virtual:interactive-block-playground-config';
Loading