wp/wp-admin/js/code-editor.js
changeset 16 a86126ab1dd4
parent 9 177826044cd9
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    37 
    37 
    38 	/**
    38 	/**
    39 	 * Configure linting.
    39 	 * Configure linting.
    40 	 *
    40 	 *
    41 	 * @param {CodeMirror} editor - Editor.
    41 	 * @param {CodeMirror} editor - Editor.
    42 	 * @param {object}     settings - Code editor settings.
    42 	 * @param {Object}     settings - Code editor settings.
    43 	 * @param {object}     settings.codeMirror - Settings for CodeMirror.
    43 	 * @param {Object}     settings.codeMirror - Settings for CodeMirror.
    44 	 * @param {Function}   settings.onChangeLintingErrors - Callback for when there are changes to linting errors.
    44 	 * @param {Function}   settings.onChangeLintingErrors - Callback for when there are changes to linting errors.
    45 	 * @param {Function}   settings.onUpdateErrorNotice - Callback to update error notice.
    45 	 * @param {Function}   settings.onUpdateErrorNotice - Callback to update error notice.
    46 	 *
    46 	 *
    47 	 * @returns {void}
    47 	 * @return {void}
    48 	 */
    48 	 */
    49 	function configureLinting( editor, settings ) { // eslint-disable-line complexity
    49 	function configureLinting( editor, settings ) { // eslint-disable-line complexity
    50 		var currentErrorAnnotations = [], previouslyShownErrorAnnotations = [];
    50 		var currentErrorAnnotations = [], previouslyShownErrorAnnotations = [];
    51 
    51 
    52 		/**
    52 		/**
    53 		 * Call the onUpdateErrorNotice if there are new errors to show.
    53 		 * Call the onUpdateErrorNotice if there are new errors to show.
    54 		 *
    54 		 *
    55 		 * @returns {void}
    55 		 * @return {void}
    56 		 */
    56 		 */
    57 		function updateErrorNotice() {
    57 		function updateErrorNotice() {
    58 			if ( settings.onUpdateErrorNotice && ! _.isEqual( currentErrorAnnotations, previouslyShownErrorAnnotations ) ) {
    58 			if ( settings.onUpdateErrorNotice && ! _.isEqual( currentErrorAnnotations, previouslyShownErrorAnnotations ) ) {
    59 				settings.onUpdateErrorNotice( currentErrorAnnotations, editor );
    59 				settings.onUpdateErrorNotice( currentErrorAnnotations, editor );
    60 				previouslyShownErrorAnnotations = currentErrorAnnotations;
    60 				previouslyShownErrorAnnotations = currentErrorAnnotations;
    62 		}
    62 		}
    63 
    63 
    64 		/**
    64 		/**
    65 		 * Get lint options.
    65 		 * Get lint options.
    66 		 *
    66 		 *
    67 		 * @returns {object} Lint options.
    67 		 * @return {Object} Lint options.
    68 		 */
    68 		 */
    69 		function getLintOptions() { // eslint-disable-line complexity
    69 		function getLintOptions() { // eslint-disable-line complexity
    70 			var options = editor.getOption( 'lint' );
    70 			var options = editor.getOption( 'lint' );
    71 
    71 
    72 			if ( ! options ) {
    72 			if ( ! options ) {
    77 				options = {};
    77 				options = {};
    78 			} else if ( _.isObject( options ) ) {
    78 			} else if ( _.isObject( options ) ) {
    79 				options = $.extend( {}, options );
    79 				options = $.extend( {}, options );
    80 			}
    80 			}
    81 
    81 
    82 			// Note that rules must be sent in the "deprecated" lint.options property to prevent linter from complaining about unrecognized options. See <https://github.com/codemirror/CodeMirror/pull/4944>.
    82 			/*
       
    83 			 * Note that rules must be sent in the "deprecated" lint.options property 
       
    84 			 * to prevent linter from complaining about unrecognized options.
       
    85 			 * See <https://github.com/codemirror/CodeMirror/pull/4944>.
       
    86 			 */
    83 			if ( ! options.options ) {
    87 			if ( ! options.options ) {
    84 				options.options = {};
    88 				options.options = {};
    85 			}
    89 			}
    86 
    90 
    87 			// Configure JSHint.
    91 			// Configure JSHint.
   207 
   211 
   208 	/**
   212 	/**
   209 	 * Configure tabbing.
   213 	 * Configure tabbing.
   210 	 *
   214 	 *
   211 	 * @param {CodeMirror} codemirror - Editor.
   215 	 * @param {CodeMirror} codemirror - Editor.
   212 	 * @param {object}     settings - Code editor settings.
   216 	 * @param {Object}     settings - Code editor settings.
   213 	 * @param {object}     settings.codeMirror - Settings for CodeMirror.
   217 	 * @param {Object}     settings.codeMirror - Settings for CodeMirror.
   214 	 * @param {Function}   settings.onTabNext - Callback to handle tabbing to the next tabbable element.
   218 	 * @param {Function}   settings.onTabNext - Callback to handle tabbing to the next tabbable element.
   215 	 * @param {Function}   settings.onTabPrevious - Callback to handle tabbing to the previous tabbable element.
   219 	 * @param {Function}   settings.onTabPrevious - Callback to handle tabbing to the previous tabbable element.
   216 	 *
   220 	 *
   217 	 * @returns {void}
   221 	 * @return {void}
   218 	 */
   222 	 */
   219 	function configureTabbing( codemirror, settings ) {
   223 	function configureTabbing( codemirror, settings ) {
   220 		var $textarea = $( codemirror.getTextArea() );
   224 		var $textarea = $( codemirror.getTextArea() );
   221 
   225 
   222 		codemirror.on( 'blur', function() {
   226 		codemirror.on( 'blur', function() {
   261 	 * Initialize Code Editor (CodeMirror) for an existing textarea.
   265 	 * Initialize Code Editor (CodeMirror) for an existing textarea.
   262 	 *
   266 	 *
   263 	 * @since 4.9.0
   267 	 * @since 4.9.0
   264 	 *
   268 	 *
   265 	 * @param {string|jQuery|Element} textarea - The HTML id, jQuery object, or DOM Element for the textarea that is used for the editor.
   269 	 * @param {string|jQuery|Element} textarea - The HTML id, jQuery object, or DOM Element for the textarea that is used for the editor.
   266 	 * @param {object}                [settings] - Settings to override defaults.
   270 	 * @param {Object}                [settings] - Settings to override defaults.
   267 	 * @param {Function}              [settings.onChangeLintingErrors] - Callback for when the linting errors have changed.
   271 	 * @param {Function}              [settings.onChangeLintingErrors] - Callback for when the linting errors have changed.
   268 	 * @param {Function}              [settings.onUpdateErrorNotice] - Callback for when error notice should be displayed.
   272 	 * @param {Function}              [settings.onUpdateErrorNotice] - Callback for when error notice should be displayed.
   269 	 * @param {Function}              [settings.onTabPrevious] - Callback to handle tabbing to the previous tabbable element.
   273 	 * @param {Function}              [settings.onTabPrevious] - Callback to handle tabbing to the previous tabbable element.
   270 	 * @param {Function}              [settings.onTabNext] - Callback to handle tabbing to the next tabbable element.
   274 	 * @param {Function}              [settings.onTabNext] - Callback to handle tabbing to the next tabbable element.
   271 	 * @param {object}                [settings.codemirror] - Options for CodeMirror.
   275 	 * @param {Object}                [settings.codemirror] - Options for CodeMirror.
   272 	 * @param {object}                [settings.csslint] - Rules for CSSLint.
   276 	 * @param {Object}                [settings.csslint] - Rules for CSSLint.
   273 	 * @param {object}                [settings.htmlhint] - Rules for HTMLHint.
   277 	 * @param {Object}                [settings.htmlhint] - Rules for HTMLHint.
   274 	 * @param {object}                [settings.jshint] - Rules for JSHint.
   278 	 * @param {Object}                [settings.jshint] - Rules for JSHint.
   275 	 *
   279 	 *
   276 	 * @returns {CodeEditorInstance} Instance.
   280 	 * @return {CodeEditorInstance} Instance.
   277 	 */
   281 	 */
   278 	wp.codeEditor.initialize = function initialize( textarea, settings ) {
   282 	wp.codeEditor.initialize = function initialize( textarea, settings ) {
   279 		var $textarea, codemirror, instanceSettings, instance;
   283 		var $textarea, codemirror, instanceSettings, instance;
   280 		if ( 'string' === typeof textarea ) {
   284 		if ( 'string' === typeof textarea ) {
   281 			$textarea = $( '#' + textarea );
   285 			$textarea = $( '#' + textarea );
   322 						isAlphaKey ||
   326 						isAlphaKey ||
   323 						':' === event.key ||
   327 						':' === event.key ||
   324 						' ' === event.key && /:\s+$/.test( lineBeforeCursor );
   328 						' ' === event.key && /:\s+$/.test( lineBeforeCursor );
   325 				} else if ( 'javascript' === innerMode ) {
   329 				} else if ( 'javascript' === innerMode ) {
   326 					shouldAutocomplete = isAlphaKey || '.' === event.key;
   330 					shouldAutocomplete = isAlphaKey || '.' === event.key;
   327 				} else if ( 'clike' === innerMode && 'application/x-httpd-php' === codemirror.options.mode ) {
   331 				} else if ( 'clike' === innerMode && 'php' === codemirror.options.mode ) {
   328 					shouldAutocomplete = 'keyword' === token.type || 'variable' === token.type;
   332 					shouldAutocomplete = 'keyword' === token.type || 'variable' === token.type;
   329 				}
   333 				}
   330 				if ( shouldAutocomplete ) {
   334 				if ( shouldAutocomplete ) {
   331 					codemirror.showHint( { completeSingle: false } );
   335 					codemirror.showHint( { completeSingle: false } );
   332 				}
   336 				}