Skip to content

Commit

Permalink
Squash. Previous HEAD=f6dc45a BASE=23a572a
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Apr 11, 2023
1 parent a45faf6 commit 011ea63
Show file tree
Hide file tree
Showing 65 changed files with 7,659 additions and 865 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
main-branch-name: 'trunk'
- run: npm install -g yarn nx
- run: yarn install --frozen-lockfile

- run: nx format:check || npx prettier --check .
- run: nx affected --target=typecheck
- run: nx affected --target=lint --parallel=3
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ You can connect to the Playground using the JavaScript client. Here's an example
<script type="module">
import { connectPlayground, login } from '@wp-playground/client';
const client = await connectPlayground(
document.getElementById('wp'),
{ loadRemote: 'https://playground.wordpress.net/remote.html' }
);
const client = await connectPlayground(document.getElementById('wp'), { loadRemote: 'https://playground.wordpress.net/remote.html' });
await client.isReady();
await login(client, 'admin', 'password');
await client.goTo('/wp-admin/post-new.php');
Expand Down
34 changes: 29 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,25 @@
},
"private": true,
"dependencies": {
"@codemirror/autocomplete": "6.4.2",
"@codemirror/commands": "6.2.2",
"@codemirror/lang-css": "6.1.1",
"@codemirror/lang-html": "6.4.2",
"@codemirror/lang-php": "6.0.1",
"@codemirror/lang-sql": "6.4.0",
"@codemirror/language": "6.6.0",
"@codemirror/state": "6.2.0",
"@codemirror/theme-one-dark": "6.1.1",
"@codemirror/view": "6.9.3",
"classnames": "^2.3.2",
"comlink": "^4.4.1",
"file-saver": "^2.0.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react": "18",
"react-dom": "18",
"react-modal": "^3.16.1"
},
"devDependencies": {
"@codemirror/lang-javascript": "6.1.4",
"@convex-dev/typedoc-plugin-markdown": "3.14.0",
"@knodes/typedoc-plugin-pages": "0.23.1",
"@nrwl/devkit": "^15.8.6",
Expand All @@ -68,14 +79,23 @@
"@types/file-saver": "^2.0.5",
"@types/jest": "^29.4.0",
"@types/node": "18.14.2",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@types/react": "18",
"@types/react-dom": "18",
"@types/react-modal": "^3.13.1",
"@types/wordpress__block-editor": "11.5.0",
"@types/wordpress__blocks": "12.5.0",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"@vitejs/plugin-react": "^3.1.0",
"@vitest/coverage-c8": "~0.25.8",
"@vitest/ui": "^0.25.8",
"@wordpress/block-editor": "11.6.0",
"@wordpress/blocks": "12.6.0",
"@wordpress/components": "23.6.0",
"@wordpress/core-data": "6.6.0",
"@wordpress/data": "8.6.0",
"@wordpress/element": "5.6.0",
"@wordpress/notices": "3.29.0",
"dts-bundle-generator": "^7.2.0",
"esbuild": "^0.17.5",
"esbuild-plugin-copy": "^2.1.0",
Expand All @@ -88,17 +108,20 @@
"eslint-plugin-react-hooks": "4.6.0",
"gh-pages": "5.0.0",
"glob": "^9.3.0",
"i": "0.3.7",
"jest": "^29.4.1",
"jest-environment-jsdom": "^29.4.1",
"jest-environment-node": "^29.4.1",
"jsdom": "~20.0.3",
"jsonc-eslint-parser": "^2.1.0",
"lerna": "6.6.1",
"nx": "15.8.6",
"parcel": "2.8.3",
"prettier": "^2.6.2",
"react-test-renderer": "18.2.0",
"rimraf": "^4.4.0",
"rollup-plugin-dts": "^5.2.0",
"rollup-plugin-external-globals": "0.7.3",
"rollup-plugin-ts": "^3.2.0",
"ts-jest": "^29.0.5",
"ts-node": "10.9.1",
Expand All @@ -110,7 +133,8 @@
"vite": "^4.0.1",
"vite-plugin-dts": "~1.7.1",
"vite-plugin-eslint": "^1.8.1",
"vite-plugin-static-copy": "^0.13.1",
"vite-plugin-external": "1.2.8",
"vite-plugin-static-copy": "0.13.1",
"vite-plugin-wasm": "^3.2.2",
"vite-tsconfig-paths": "^4.0.2",
"vitepress": "1.0.0-alpha.60",
Expand Down
18 changes: 18 additions & 0 deletions packages/interactive-code-block/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
11 changes: 11 additions & 0 deletions packages/interactive-code-block/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# interactive-code-block

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build interactive-code-block` to build the library.

## Running unit tests

Run `nx test interactive-code-block` to execute the unit tests via [Jest](https://jestjs.io).
5 changes: 5 additions & 0 deletions packages/interactive-code-block/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@wp-playground/interactive-code-block",
"version": "0.0.1",
"type": "module"
}
40 changes: 40 additions & 0 deletions packages/interactive-code-block/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "interactive-code-block",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/interactive-code-block/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nrwl/vite:build",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/interactive-code-block"
}
},
"dev": {
"executor": "@nrwl/vite:build",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/interactive-code-block",
"watch": true,
"minify": false
}
},
"test": {
"executor": "@nrwl/vite:test",
"outputs": ["coverage/packages/interactive-code-block"],
"options": {
"passWithNoTests": true,
"reportsDirectory": "../../coverage/packages/interactive-code-block"
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/interactive-code-block/**/*.ts"]
}
}
},
"tags": []
}
115 changes: 115 additions & 0 deletions packages/interactive-code-block/public/block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

add_action('init', function () {
$asset_file = require plugin_dir_path(__FILE__) . 'index.asset.php';
foreach ($asset_file['dependencies'] as $dep) {
if (wp_script_is($dep, 'registered')) {
$deps[] = $dep;
}
}
// Register block scripts
$scripts = [
'interactive-code-block' => $asset_file['files']['view'],
'interactive-code-block-editor' => $asset_file['files']['editor'],
];
foreach ($scripts as $script => $file) {
wp_register_script(
$script,
plugins_url($file, __FILE__),
$deps,
'' // Version is included in the file name
);
}

// Load block scripts as ES modules
add_filter('script_loader_tag', function ($tag, $handle, $src) use ($scripts) {
if (!array_key_exists($handle, $scripts)) {
return $tag;
}

// Remove any query string from the URL
$new_url = current(explode('?', $src));
$tag = '<script type="module" src="' . esc_url($new_url) . '"></script>';
return $tag;
}, 10, 3);

$asset_file = require plugin_dir_path(__FILE__) . 'index.asset.php';
$deps = [];
foreach ($asset_file['dependencies'] as $dep) {
if (wp_style_is($dep, 'registered')) {
$deps[] = $dep;
}
}

// Register block styles
$styles = [
'interactive-code-block' => 'view',
'interactive-code-block-editor' => 'editor',
];
foreach ($styles as $style => $file) {
wp_register_style(
$style,
plugins_url('assets/' . $file . '.css', __FILE__),
$deps,
$asset_file['version']
);
}

// Register the block itself
register_block_type_from_metadata(__DIR__ . '/assets/block.json', [
'render_callback' => function ($attributes) {
if (!isset($attributes['code'])) {
return '';
}

$execution_scripts = get_option(LCS_Execution_Scripts_Endpoint::OPTION_KEY, []);
$script_id = $attributes['executionScript'];
$script = $execution_scripts[$script_id] ?? '';

$all_libraries = lcs_get_libraries_list();
$library_ids = array_unique(
array_merge(
$attributes['libraries'],
$script['libraries'] ?? []
)
);
$libraries = array_values(array_filter($all_libraries, function ($library) use ($library_ids) {
return in_array($library['id'], $library_ids);
}));

// The base64_decode is to prevent incorrect escaping by Gutenberg
// E.g. representing <h1 title="&lt;div&gt; &quot;html&quot;"> seems
// to be a problem for Gutenberg – it will store it as
// &lt;h1 title=&quot;&amp;lt;div&amp;gt; &amp;quot;html&amp;quot;&quot;$gt;
return '
<div
class="wp-playground-interactive-code-snippet"
data-code="' . esc_attr($attributes['code']) . '"
data-file-type="' . esc_attr($attributes['fileType']) . '"
data-libraries="' . esc_attr(json_encode($libraries)) . '"
data-execution-script="' . esc_attr(json_encode($script)) . '"
data-execution-script-id="' . esc_attr(json_encode($script_id)) . '"
data-cached-output="' . esc_attr($attributes['cachedOutput']) . '"
data-show-cached-output="' . esc_attr($attributes['showCachedOutput']) . '"
>
<b>Loading interactive code snippet...</b>
</div>
';
}
// Render a static code snippet while the interactive one is loading:
// Useful only for PHP and only if the PHP version >= webhost version.
// ' . highlight_string(base64_decode($attributes['code']), true) . '
]);
});


function lce_editor_init($hook)
{
global $current_screen;
if (!$current_screen->is_block_editor()) {
return;
}

lce_preload_endpoints_data();
}
add_action('admin_enqueue_scripts', 'lce_editor_init');
Loading

0 comments on commit 011ea63

Please sign in to comment.