| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Integration\Gutenberg; |
| 13 |
|
| 14 |
use WIND_PRESS; |
| 15 |
use WindPress\WindPress\Core\Runtime; |
| 16 |
use WindPress\WindPress\Utils\AssetVite; |
| 17 |
/** |
| 18 |
* @author Joshua Gugun Siagian <suabahasa@gmail.com> |
| 19 |
*/ |
| 20 |
class Editor |
| 21 |
{ |
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
\add_action('enqueue_block_editor_assets', fn() => $this->enqueue_block_editor_assets()); |
| 25 |
} |
| 26 |
public function enqueue_block_editor_assets() |
| 27 |
{ |
| 28 |
$screen = \get_current_screen(); |
| 29 |
if (\is_admin() && $screen->is_block_editor()) { |
| 30 |
\add_action('admin_head', fn() => $this->admin_head(), 1000001); |
| 31 |
} |
| 32 |
} |
| 33 |
public function admin_head() |
| 34 |
{ |
| 35 |
Runtime::get_instance()->enqueue_play_cdn(); |
| 36 |
if (\strpos($_SERVER['REQUEST_URI'], 'site-editor.php') !== \false) { |
| 37 |
// handle the canvas |
| 38 |
AssetVite::get_instance()->enqueue_asset('assets/integration/gutenberg/site-editor.js', ['handle' => WIND_PRESS::WP_OPTION . ':integration-gutenberg-site-editor', 'in-footer' => \true]); |
| 39 |
} else { |
| 40 |
// handle the canvas |
| 41 |
AssetVite::get_instance()->enqueue_asset('assets/integration/gutenberg/post-editor.js', ['handle' => WIND_PRESS::WP_OPTION . ':integration-gutenberg-post-editor', 'in-footer' => \true]); |
| 42 |
} |
| 43 |
$handle = WIND_PRESS::WP_OPTION . ':integration-gutenberg-block-editor'; |
| 44 |
AssetVite::get_instance()->enqueue_asset('assets/integration/gutenberg/block-editor.jsx', ['handle' => $handle, 'in-footer' => \true, 'dependencies' => ['wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-hooks', 'wp-i18n', 'react', 'react-dom']]); |
| 45 |
\wp_add_inline_script($handle, <<<JS |
| 46 |
document.addEventListener('DOMContentLoaded', function () { |
| 47 |
wp.hooks.addFilter('windpressgutenberg-autocomplete-items-query', 'windpressgutenberg', async (autocompleteItems, text) => { |
| 48 |
if (!window.windpress?.loaded?.module?.autocomplete) { |
| 49 |
return autocompleteItems; |
| 50 |
} |
| 51 |
|
| 52 |
const windpress_suggestions = window.wp.hooks.applyFilters('windpress.module.autocomplete', text); |
| 53 |
|
| 54 |
return [...windpress_suggestions, ...autocompleteItems]; |
| 55 |
}); |
| 56 |
}); |
| 57 |
JS |
| 58 |
, 'after'); |
| 59 |
} |
| 60 |
} |
| 61 |
|