WordPress/wp-admin/js/theme-plugin-editor.js
Andrea Fercia d1948f623a Accessibility: CodeMirror editing areas minor improvements.
- properly labels all the code editor areas (Theme/Plugin, Custom HTML widget, Additional CSS), whether CodeMirror is enabled or disabled
- adds `role="textbox"` and `aria-multiline="true"` to the CodeMirror editing area to allow assistive technologies properly identify it as a textarea
- standardizes the "keyboard trap" help text across the admin and keeps it as a list for better readability
- use the Help text elements as target for `aria-describedby`, to make screen readers read out the help text when focusing the editors
- fixes the `aria-expanded` attribute usage in the Customizer "Additional CSS" help toggle
- moves focus to the CodeMirror editing area when clicking on the associated label
- in the Plugin editor screen: changes a `<big>` element to `<h2>` for better semantics and consistency with the Theme editor screen
- also, removes a few textdomain leftovers, see `better-code-editing` and `default`

Props westonruter, melchoyce, afercia.
Fixes #41872.

Built from https://develop.svn.wordpress.org/trunk@41586


git-svn-id: http://core.svn.wordpress.org/trunk@41419 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-09-24 16:00:46 +00:00

105 lines
2.6 KiB
JavaScript

/* 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 );
// 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();
});
component.instance = editor;
};
return component;
})( jQuery );