-
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
Add CodeMirror to Additional CSS / Custom HTML block #60155
Open
okmttdhr
wants to merge
23
commits into
trunk
Choose a base branch
from
add/codemirror
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+715
−48
Open
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f23e475
Add codemirror to Additional CSS
okmttdhr 7b38263
Add codemirror to Custom HTML block
okmttdhr b2ceddd
Fix styles
okmttdhr 14678ea
Add focus change effect
okmttdhr 421f2d4
Add codemirror as restrictedImport
okmttdhr 4decdff
Ensure minimum lines to be full-width
okmttdhr a3e89a9
Add indenting with tab
okmttdhr 9394fc9
Add instructions to focus out of the editor
okmttdhr 15e2486
Align styles with the existing ones
okmttdhr 59a81b5
Fix lint
okmttdhr 4c6262c
Extract EditorView component
okmttdhr ee88fcd
Fix incorrect use of var()
okmttdhr a6519f2
Reuse EditorView component
okmttdhr 57f4770
Fix wrong package version
okmttdhr dff73f6
Fix typo in variable name
okmttdhr e7aa264
Remove unnecessary condition in useEffect
okmttdhr fb5ba29
Use @\mixins
okmttdhr a106416
Add VisuallyHidden component to hide editor instructions
okmttdhr 30ae96e
Fix editor height isn't calculated properly
okmttdhr af8af11
Use BaseControl for Additional CSS
okmttdhr b0054fc
Utilize useInstanceId
okmttdhr ab79885
Switch the editor's light/dark mode
okmttdhr 6a4b77d
Merge branch 'trunk' into add/codemirror
okmttdhr 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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. |
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
Oops, something went wrong.
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.
Importing these individual packages will create four webpack chunks, and the loading will take more time than necessary, because the next load starts only after the previous one has finished.
Better to move the
EditorView
code into a separate module and load it all at once:Then there will be just one
code-mirror-view
chunk that's loaded on demand. There's a bit of trouble with thelang-css
andlang-html
modules. Do we want to bundle them both in a single chunk? That depends on how big they are.Or the dynamic module can contain the React component that shows the editor: the
<div>
together with theuseRef
and theuseEffect
(this time the effect doesn't do dynamicimport
). Then it can be used like:It would be also a good idea to not load the CodeMirror editor not when the
AdvancedPanel
is rendered, that will load it too often. Load it only after clicking on the CSS field, only after the user shows a clear intent to edit.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.
Thank you for your review/insights, @jsnajdr! 😄
Not exactly, in terms of
import
. Fundamentally, based on my understanding of how modules are waited for and executed, the following codewould be roughly equivalent to the following:
At least, Webpack behaves in this manner, even for
import()
. Here’s a screenshot showing that the requests for chunks run in parallel when using the original approach.However, this does not seem to be stated explicitly in the spec of
import()
, and my tests confirmed that native dynamic imports execute requests sequentially both at the top level and within function scopes. I’ll address this later in this comment.This also applies even if we:
React.lazy()
import {} from 'codemirror'
because we are splitting the vendor's chunk.
This approach does not create a single chunk, even with all logic extracted into a component and using static import (although we can tweak chunks further by using the optimization.splitChunks option).
Thus, the requests for the “one-component approach” looked like this, showing no difference from the original approach:
Given the above, I propose the following;
Promise.all([import(), import(), …])
rather than fetching a single larger chunk at once. This ensures that requests run in parallel, even after transitioning to native dynamic import.For now, parallel chunk requests seem to be more efficient.
I don’t think it’s a good idea to load all
lang-*
modules in this component because we may want to support additional language modes in the future. (Even with the “parallel requests” strategy, this could potentially max out the browser's limit on simultaneous requests.)On a slow 3G network, this delay could be around 8 seconds. Making users wait that long for syntax highlighting isn't ideal, IMO.
I'd consider more eager loading strategies (if that doesn't interfere with user interaction). For instance, for Additional CSS, we could load the chunks when the three-dots menu is opened, or for a Custom HTML block, load them when the block appears in the search results. Alternatively,
prefetch
might be a viable approach. WDYT?Once the chunks are loaded, subsequent network requests are unnecessary, even if users access Additional CSS multiple times. Thus, “loading them too often” shouldn’t pose an issue, in my opinion. We just need to be careful about how it affects users’ actual interactions/experiences.
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.
We should consider not doing this. After all, currently Gutenberg doesn't do it on purpose, but only because it's a webpack default and we didn't do any dynamic chunks until now.
In my block lazy loading experiment I disabled it, see this comment: https://github.com/WordPress/gutenberg/pull/55585/files#r1383350711
It helps to keep a nice file structure with human-comprehensible names.