Editor: Add CodeMirror-powered code editor with syntax highlighting, linting, and auto-completion.
* Code editor is integrated into the Theme/Plugin Editor, Additional CSS in Customizer, and Custom HTML widget. Code editor is not yet integrated into the post editor, and it may not be until accessibility concerns are addressed.
* The CodeMirror component in the Custom HTML widget is integrated in a similar way to TinyMCE being integrated into the Text widget, adopting the same approach for integrating dynamic JavaScript-initialized fields.
* Linting is performed for JS, CSS, HTML, and JSON via JSHint, CSSLint, HTMLHint, and JSONLint respectively. Linting is not yet supported for PHP.
* When user lacks `unfiltered_html` the capability, the Custom HTML widget will report any Kses-invalid elements and attributes as errors via a custom Kses rule for HTMLHint.
* When linting errors are detected, the user will be prevented from saving the code until the errors are fixed, reducing instances of broken websites.
* The placeholder value is removed from Custom CSS in favor of a fleshed-out section description which now auto-expands when the CSS field is empty. See #39892.
* The CodeMirror library is included as `wp.CodeMirror` to prevent conflicts with any existing `CodeMirror` global.
* An `wp.codeEditor.initialize()` API in JS is provided to convert a `textarea` into CodeMirror, with a `wp_enqueue_code_editor()` function in PHP to manage enqueueing the assets and settings needed to edit a given type of code.
* A user preference is added to manage whether or not "syntax highlighting" is enabled. The feature is opt-out, being enabled by default.
* Allowed file extensions in the theme and plugin editors have been updated to include formats which CodeMirror has modes for: `conf`, `css`, `diff`, `patch`, `html`, `htm`, `http`, `js`, `json`, `jsx`, `less`, `md`, `php`, `phtml`, `php3`, `php4`, `php5`, `php7`, `phps`, `scss`, `sass`, `sh`, `bash`, `sql`, `svg`, `xml`, `yml`, `yaml`, `txt`.
Props westonruter, georgestephanis, obenland, melchoyce, pixolin, mizejewski, michelleweber, afercia, grahamarmfield, samikeijonen, rianrietveld, iseulde.
See #38707.
Fixes #12423, #39892.
Built from https://develop.svn.wordpress.org/trunk@41376
git-svn-id: http://core.svn.wordpress.org/trunk@41209 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-09-13 02:08:47 -04:00
|
|
|
/* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1] }] */
|
|
|
|
|
|
|
|
if ( ! window.wp ) {
|
|
|
|
window.wp = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
wp.themePluginEditor = (function( $ ) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var component = {
|
|
|
|
l10n: {
|
|
|
|
lintError: {
|
|
|
|
singular: '',
|
|
|
|
plural: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
instance: null
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize component.
|
|
|
|
*
|
|
|
|
* @param {object} settings Settings.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
component.init = function( settings ) {
|
|
|
|
var codeEditorSettings, noticeContainer, errorNotice = [], editor;
|
|
|
|
|
|
|
|
codeEditorSettings = $.extend( {}, settings );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle tabbing to the field before the editor.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
codeEditorSettings.onTabPrevious = function() {
|
|
|
|
$( '#templateside' ).find( ':tabbable' ).last().focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle tabbing to the field after the editor.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
codeEditorSettings.onTabNext = function() {
|
|
|
|
$( '#template' ).find( ':tabbable:not(.CodeMirror-code)' ).first().focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the error notice container.
|
|
|
|
noticeContainer = $( '<div id="file-editor-linting-error"></div>' );
|
|
|
|
errorNotice = $( '<div class="inline notice notice-error"></div>' );
|
|
|
|
noticeContainer.append( errorNotice );
|
|
|
|
noticeContainer.hide();
|
|
|
|
$( 'p.submit' ).before( noticeContainer );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update error notice.
|
|
|
|
*
|
|
|
|
* @param {Array} errorAnnotations - Error annotations.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
codeEditorSettings.onUpdateErrorNotice = function onUpdateErrorNotice( errorAnnotations ) {
|
|
|
|
var message;
|
|
|
|
|
|
|
|
$( '#submit' ).prop( 'disabled', 0 !== errorAnnotations.length );
|
|
|
|
|
|
|
|
if ( 0 !== errorAnnotations.length ) {
|
|
|
|
errorNotice.empty();
|
|
|
|
if ( 1 === errorAnnotations.length ) {
|
|
|
|
message = component.l10n.singular.replace( '%d', '1' );
|
|
|
|
} else {
|
|
|
|
message = component.l10n.plural.replace( '%d', String( errorAnnotations.length ) );
|
|
|
|
}
|
|
|
|
errorNotice.append( $( '<p></p>', {
|
|
|
|
text: message
|
|
|
|
} ) );
|
|
|
|
noticeContainer.slideDown( 'fast' );
|
|
|
|
wp.a11y.speak( message );
|
|
|
|
} else {
|
|
|
|
noticeContainer.slideUp( 'fast' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
editor = wp.codeEditor.initialize( $( '#newcontent' ), codeEditorSettings );
|
|
|
|
|
2017-09-24 12:00:46 -04:00
|
|
|
// Improve the editor accessibility.
|
|
|
|
$( editor.codemirror.display.lineDiv )
|
|
|
|
.attr({
|
|
|
|
role: 'textbox',
|
|
|
|
'aria-multiline': 'true',
|
|
|
|
'aria-labelledby': 'theme-plugin-editor-label',
|
|
|
|
'aria-describedby': 'editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Focus the editor when clicking on its label.
|
|
|
|
$( '#theme-plugin-editor-label' ).on( 'click', function() {
|
|
|
|
editor.codemirror.focus();
|
|
|
|
});
|
|
|
|
|
Editor: Add CodeMirror-powered code editor with syntax highlighting, linting, and auto-completion.
* Code editor is integrated into the Theme/Plugin Editor, Additional CSS in Customizer, and Custom HTML widget. Code editor is not yet integrated into the post editor, and it may not be until accessibility concerns are addressed.
* The CodeMirror component in the Custom HTML widget is integrated in a similar way to TinyMCE being integrated into the Text widget, adopting the same approach for integrating dynamic JavaScript-initialized fields.
* Linting is performed for JS, CSS, HTML, and JSON via JSHint, CSSLint, HTMLHint, and JSONLint respectively. Linting is not yet supported for PHP.
* When user lacks `unfiltered_html` the capability, the Custom HTML widget will report any Kses-invalid elements and attributes as errors via a custom Kses rule for HTMLHint.
* When linting errors are detected, the user will be prevented from saving the code until the errors are fixed, reducing instances of broken websites.
* The placeholder value is removed from Custom CSS in favor of a fleshed-out section description which now auto-expands when the CSS field is empty. See #39892.
* The CodeMirror library is included as `wp.CodeMirror` to prevent conflicts with any existing `CodeMirror` global.
* An `wp.codeEditor.initialize()` API in JS is provided to convert a `textarea` into CodeMirror, with a `wp_enqueue_code_editor()` function in PHP to manage enqueueing the assets and settings needed to edit a given type of code.
* A user preference is added to manage whether or not "syntax highlighting" is enabled. The feature is opt-out, being enabled by default.
* Allowed file extensions in the theme and plugin editors have been updated to include formats which CodeMirror has modes for: `conf`, `css`, `diff`, `patch`, `html`, `htm`, `http`, `js`, `json`, `jsx`, `less`, `md`, `php`, `phtml`, `php3`, `php4`, `php5`, `php7`, `phps`, `scss`, `sass`, `sh`, `bash`, `sql`, `svg`, `xml`, `yml`, `yaml`, `txt`.
Props westonruter, georgestephanis, obenland, melchoyce, pixolin, mizejewski, michelleweber, afercia, grahamarmfield, samikeijonen, rianrietveld, iseulde.
See #38707.
Fixes #12423, #39892.
Built from https://develop.svn.wordpress.org/trunk@41376
git-svn-id: http://core.svn.wordpress.org/trunk@41209 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-09-13 02:08:47 -04:00
|
|
|
component.instance = editor;
|
|
|
|
};
|
|
|
|
|
|
|
|
return component;
|
|
|
|
})( jQuery );
|