-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Code Editor: Register Ctrl/Cmd + S to trigger file save in Theme/Plugin Editor #10851
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
Changes from all commits
a0cfad9
03f4950
f322df5
c2da37e
18898a3
b5f6e85
d2437dd
0d8c2df
9001dc9
bb7fac9
7c8123c
e2cb6eb
9cb5f59
2d71516
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |||||||||||||||||||||||||||||||||||
| * @output wp-admin/js/theme-plugin-editor.js | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1] }] */ | ||||||||||||||||||||||||||||||||||||
| /* eslint-env es2020 */ | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 9, 1000] }] */ | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if ( ! window.wp ) { | ||||||||||||||||||||||||||||||||||||
| window.wp = {}; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -81,6 +83,18 @@ wp.themePluginEditor = (function( $ ) { | |||||||||||||||||||||||||||||||||||
| component.docsLookUpButton.prop( 'disabled', false ); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Initiate saving the file when not focused in CodeMirror or when the user has syntax highlighting turned off. | ||||||||||||||||||||||||||||||||||||
| $( window ).on( 'keydown', function( event ) { | ||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||
| ( event.ctrlKey || event.metaKey ) && | ||||||||||||||||||||||||||||||||||||
| ( 's' === event.key.toLowerCase() ) && | ||||||||||||||||||||||||||||||||||||
| ( ! component.instance || ! component.instance.codemirror.hasFocus() ) | ||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||
| event.preventDefault(); | ||||||||||||||||||||||||||||||||||||
| component.form.trigger( 'submit' ); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||
westonruter marked this conversation as resolved.
Show resolved
Hide resolved
westonruter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
|
|
@@ -191,6 +205,10 @@ wp.themePluginEditor = (function( $ ) { | |||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if ( component.instance && component.instance.updateErrorNotice ) { | ||||||||||||||||||||||||||||||||||||
| component.instance.updateErrorNotice(); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Scroll to the line that has the error. | ||||||||||||||||||||||||||||||||||||
| if ( component.lintErrors.length ) { | ||||||||||||||||||||||||||||||||||||
| component.instance.codemirror.setCursor( component.lintErrors[0].from.line ); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -399,6 +417,16 @@ wp.themePluginEditor = (function( $ ) { | |||||||||||||||||||||||||||||||||||
| editor = wp.codeEditor.initialize( $( '#newcontent' ), codeEditorSettings ); | ||||||||||||||||||||||||||||||||||||
| editor.codemirror.on( 'change', component.onChange ); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function onSaveShortcut() { | ||||||||||||||||||||||||||||||||||||
| component.form.trigger( 'submit' ); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| editor.codemirror.setOption( 'extraKeys', { | ||||||||||||||||||||||||||||||||||||
| ...( editor.codemirror.getOption( 'extraKeys' ) || {} ), | ||||||||||||||||||||||||||||||||||||
| 'Ctrl-S': onSaveShortcut, | ||||||||||||||||||||||||||||||||||||
| 'Cmd-S': onSaveShortcut, | ||||||||||||||||||||||||||||||||||||
westonruter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
424
to
428
|
||||||||||||||||||||||||||||||||||||
| editor.codemirror.setOption( 'extraKeys', { | |
| 'Ctrl-S': onSaveShortcut, | |
| 'Cmd-S': onSaveShortcut, | |
| } ); | |
| var existingExtraKeys = editor.codemirror.getOption( 'extraKeys' ) || {}; | |
| var mergedExtraKeys = $.extend( {}, existingExtraKeys, { | |
| 'Ctrl-S': onSaveShortcut, | |
| 'Cmd-S': onSaveShortcut | |
| } ); | |
| editor.codemirror.setOption( 'extraKeys', mergedExtraKeys ); |
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.
This is a great suggestion. Existing shortcuts were indeed being lost:
| Shortcut | Command |
|---|---|
| Alt-F | findPersistent |
| Cmd-/ | toggleComment |
| Cmd-F | findPersistent |
| Ctrl-/ | toggleComment |
| Ctrl-F | findPersistent |
| Ctrl-Space | autocomplete |
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.
Addressed in 2d71516
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.
I don't know exactly what's going on in this comment, but it caught my attention.
codemirror/codemirror5#4944 was merged so this may be out of date?
Not necessary to change anything in this PR.
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.
Good callout. After merging this I intend to do an overall pass on this file and
theme-plugin-editor.jsto clean up stuff like this. I'm going to fix types and update docs. Once everything is pristine, then this should greatly help with plotting a course for v6.