-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Asynchronously load tinyMCE the first time a classic block is edited #21684
Closed
sarayourfriend
wants to merge
15
commits into
WordPress:master
from
sarayourfriend:add/lazy-classic-block
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f334112
Add LazyLoad component
sarayourfriend a2e071e
WIP: Lazy load tineMCE for the classic block
sarayourfriend bac6771
Only print TinyMCE scripts outside of the block editor screen
sarayourfriend 99c7af2
Inline a window function for TinyMCE i18n init
sarayourfriend 26a3b7c
Add script and style REST APIs
spacedmonkey 288519c
Fix PHP
spacedmonkey d57e03f
Fix lints
spacedmonkey 594cf96
Update lib/class-wp-rest-dependencies-controller.php
spacedmonkey a33ed9e
Allow passing multiple dependency handles
sarayourfriend f706903
Use the actual object's handles rather than duplicating its initializ…
sarayourfriend 0d55d9a
Use dependencies REST API to fetch style and script URIs
sarayourfriend 7d370f1
Guarantee script execution order
sarayourfriend 3980857
Fetch TinyMCE translations from REST API
sarayourfriend 6f0575d
Prevent onLoaded for previously loaded deps
sarayourfriend 0f90624
Add document to LazyLoad component
sarayourfriend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* TinyMCE i18n controller. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Endpoint for retrieving TinyMCE translations needed to initialize the library | ||
* on the frontend. Used by the classic block's edit LazyLoad wrapper. | ||
* | ||
* Class WP_REST_TinyMCE_I18n_Controller | ||
* | ||
* @see WP_REST_Controller | ||
*/ | ||
class WP_REST_TinyMCE_I18n_Controller extends WP_REST_Controller { | ||
|
||
/** | ||
* Register routes. | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( | ||
'wp/v2', | ||
'/tinymce-i18n', | ||
[ | ||
[ | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => [ $this, 'get_translations' ], | ||
'permission_callback' => [ $this, 'check_permissions' ], | ||
'args' => [], | ||
], | ||
'schema' => array( $this, 'get_item_schema' ), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Get the translations for the current user's locale. | ||
* | ||
* _WP_Editors is not included by default so we need to require it | ||
* if the class isn't available. We should probably refactor all the | ||
* TinyMCE translation files out of _WP_Editors. This shouldn't be | ||
* too hard considering they're already all static functions. | ||
* | ||
* @param WP_REST_Request $request Request. | ||
* | ||
* @return array | ||
*/ | ||
public function get_translations( $request ) { | ||
if ( ! class_exists( '_WP_Editors', false ) ) { | ||
require ABSPATH . WPINC . '/class-wp-editor.php'; | ||
} | ||
|
||
$mce_locale = _WP_Editors::get_mce_locale(); | ||
$json_only = true; | ||
$baseurl = _WP_Editors::get_baseurl(); | ||
|
||
return [ | ||
"translations" => _WP_Editors::wp_mce_translation( $mce_locale, $json_only ), | ||
"locale" => $mce_locale, | ||
"locale_script_handle" => "$baseurl/langs/$mce_locale.js", | ||
]; | ||
} | ||
|
||
/** | ||
* This endpoint is only mean to be used from the editor so it can be safely | ||
* restricted only to users that can edit posts. | ||
* | ||
* @param WP_REST_Request $request Request. | ||
* | ||
* @return bool|true|WP_Error | ||
*/ | ||
public function check_permissions( $request ) { | ||
return current_user_can( 'edit_posts' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { BACKSPACE, DELETE, F10 } from '@wordpress/keycodes'; | |
* Internal dependencies | ||
*/ | ||
import LazyLoad from './lazy'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
const { wp } = window; | ||
|
||
|
@@ -225,12 +226,17 @@ class ClassicEdit extends Component { | |
const LazyClassicEdit = ( props ) => ( | ||
<LazyLoad | ||
scripts={ [ 'wp-tinymce' ] } | ||
onLoaded={ () => | ||
new Promise( ( resolve ) => { | ||
window.wpMceTranslation(); | ||
resolve(); | ||
} ) | ||
} | ||
onLoaded={ async () => { | ||
const { | ||
translations, | ||
locale, | ||
locale_script_handle: localeScriptHandle, | ||
} = await apiFetch( { path: '/wp/v2/tinymce-i18n' } ); | ||
|
||
const { tinymce } = window; | ||
tinymce.addI18n( locale, JSON.stringify( translations ) ); | ||
tinymce.ScriptLoader.markDone( localeScriptHandle ); | ||
} } | ||
Comment on lines
+229
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gziolo I think these changes will be interesting to you 😊 cc @griffbrad because we'd also discussed using an endpoint to retrieve the translation json. |
||
placeholder={ <div>Loading...</div> } | ||
> | ||
<ClassicEdit { ...props } /> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still POCing, needs tests and an actually correct schema!