Edit File by line
/home/barbar84/public_h.../wp-admin/js
File: code-editor.js
/**
[0] Fix | Delete
* @output wp-admin/js/code-editor.js
[1] Fix | Delete
*/
[2] Fix | Delete
[3] Fix | Delete
if ( 'undefined' === typeof window.wp ) {
[4] Fix | Delete
/**
[5] Fix | Delete
* @namespace wp
[6] Fix | Delete
*/
[7] Fix | Delete
window.wp = {};
[8] Fix | Delete
}
[9] Fix | Delete
if ( 'undefined' === typeof window.wp.codeEditor ) {
[10] Fix | Delete
/**
[11] Fix | Delete
* @namespace wp.codeEditor
[12] Fix | Delete
*/
[13] Fix | Delete
window.wp.codeEditor = {};
[14] Fix | Delete
}
[15] Fix | Delete
[16] Fix | Delete
( function( $, wp ) {
[17] Fix | Delete
'use strict';
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Default settings for code editor.
[21] Fix | Delete
*
[22] Fix | Delete
* @since 4.9.0
[23] Fix | Delete
* @type {object}
[24] Fix | Delete
*/
[25] Fix | Delete
wp.codeEditor.defaultSettings = {
[26] Fix | Delete
codemirror: {},
[27] Fix | Delete
csslint: {},
[28] Fix | Delete
htmlhint: {},
[29] Fix | Delete
jshint: {},
[30] Fix | Delete
onTabNext: function() {},
[31] Fix | Delete
onTabPrevious: function() {},
[32] Fix | Delete
onChangeLintingErrors: function() {},
[33] Fix | Delete
onUpdateErrorNotice: function() {}
[34] Fix | Delete
};
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Configure linting.
[38] Fix | Delete
*
[39] Fix | Delete
* @param {CodeMirror} editor - Editor.
[40] Fix | Delete
* @param {Object} settings - Code editor settings.
[41] Fix | Delete
* @param {Object} settings.codeMirror - Settings for CodeMirror.
[42] Fix | Delete
* @param {Function} settings.onChangeLintingErrors - Callback for when there are changes to linting errors.
[43] Fix | Delete
* @param {Function} settings.onUpdateErrorNotice - Callback to update error notice.
[44] Fix | Delete
*
[45] Fix | Delete
* @return {void}
[46] Fix | Delete
*/
[47] Fix | Delete
function configureLinting( editor, settings ) { // eslint-disable-line complexity
[48] Fix | Delete
var currentErrorAnnotations = [], previouslyShownErrorAnnotations = [];
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Call the onUpdateErrorNotice if there are new errors to show.
[52] Fix | Delete
*
[53] Fix | Delete
* @return {void}
[54] Fix | Delete
*/
[55] Fix | Delete
function updateErrorNotice() {
[56] Fix | Delete
if ( settings.onUpdateErrorNotice && ! _.isEqual( currentErrorAnnotations, previouslyShownErrorAnnotations ) ) {
[57] Fix | Delete
settings.onUpdateErrorNotice( currentErrorAnnotations, editor );
[58] Fix | Delete
previouslyShownErrorAnnotations = currentErrorAnnotations;
[59] Fix | Delete
}
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Get lint options.
[64] Fix | Delete
*
[65] Fix | Delete
* @return {Object} Lint options.
[66] Fix | Delete
*/
[67] Fix | Delete
function getLintOptions() { // eslint-disable-line complexity
[68] Fix | Delete
var options = editor.getOption( 'lint' );
[69] Fix | Delete
[70] Fix | Delete
if ( ! options ) {
[71] Fix | Delete
return false;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
if ( true === options ) {
[75] Fix | Delete
options = {};
[76] Fix | Delete
} else if ( _.isObject( options ) ) {
[77] Fix | Delete
options = $.extend( {}, options );
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/*
[81] Fix | Delete
* Note that rules must be sent in the "deprecated" lint.options property
[82] Fix | Delete
* to prevent linter from complaining about unrecognized options.
[83] Fix | Delete
* See <https://github.com/codemirror/CodeMirror/pull/4944>.
[84] Fix | Delete
*/
[85] Fix | Delete
if ( ! options.options ) {
[86] Fix | Delete
options.options = {};
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
// Configure JSHint.
[90] Fix | Delete
if ( 'javascript' === settings.codemirror.mode && settings.jshint ) {
[91] Fix | Delete
$.extend( options.options, settings.jshint );
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
// Configure CSSLint.
[95] Fix | Delete
if ( 'css' === settings.codemirror.mode && settings.csslint ) {
[96] Fix | Delete
$.extend( options.options, settings.csslint );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
// Configure HTMLHint.
[100] Fix | Delete
if ( 'htmlmixed' === settings.codemirror.mode && settings.htmlhint ) {
[101] Fix | Delete
options.options.rules = $.extend( {}, settings.htmlhint );
[102] Fix | Delete
[103] Fix | Delete
if ( settings.jshint ) {
[104] Fix | Delete
options.options.rules.jshint = settings.jshint;
[105] Fix | Delete
}
[106] Fix | Delete
if ( settings.csslint ) {
[107] Fix | Delete
options.options.rules.csslint = settings.csslint;
[108] Fix | Delete
}
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
// Wrap the onUpdateLinting CodeMirror event to route to onChangeLintingErrors and onUpdateErrorNotice.
[112] Fix | Delete
options.onUpdateLinting = (function( onUpdateLintingOverridden ) {
[113] Fix | Delete
return function( annotations, annotationsSorted, cm ) {
[114] Fix | Delete
var errorAnnotations = _.filter( annotations, function( annotation ) {
[115] Fix | Delete
return 'error' === annotation.severity;
[116] Fix | Delete
} );
[117] Fix | Delete
[118] Fix | Delete
if ( onUpdateLintingOverridden ) {
[119] Fix | Delete
onUpdateLintingOverridden.apply( annotations, annotationsSorted, cm );
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
// Skip if there are no changes to the errors.
[123] Fix | Delete
if ( _.isEqual( errorAnnotations, currentErrorAnnotations ) ) {
[124] Fix | Delete
return;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
currentErrorAnnotations = errorAnnotations;
[128] Fix | Delete
[129] Fix | Delete
if ( settings.onChangeLintingErrors ) {
[130] Fix | Delete
settings.onChangeLintingErrors( errorAnnotations, annotations, annotationsSorted, cm );
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/*
[134] Fix | Delete
* Update notifications when the editor is not focused to prevent error message
[135] Fix | Delete
* from overwhelming the user during input, unless there are now no errors or there
[136] Fix | Delete
* were previously errors shown. In these cases, update immediately so they can know
[137] Fix | Delete
* that they fixed the errors.
[138] Fix | Delete
*/
[139] Fix | Delete
if ( ! editor.state.focused || 0 === currentErrorAnnotations.length || previouslyShownErrorAnnotations.length > 0 ) {
[140] Fix | Delete
updateErrorNotice();
[141] Fix | Delete
}
[142] Fix | Delete
};
[143] Fix | Delete
})( options.onUpdateLinting );
[144] Fix | Delete
[145] Fix | Delete
return options;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
editor.setOption( 'lint', getLintOptions() );
[149] Fix | Delete
[150] Fix | Delete
// Keep lint options populated.
[151] Fix | Delete
editor.on( 'optionChange', function( cm, option ) {
[152] Fix | Delete
var options, gutters, gutterName = 'CodeMirror-lint-markers';
[153] Fix | Delete
if ( 'lint' !== option ) {
[154] Fix | Delete
return;
[155] Fix | Delete
}
[156] Fix | Delete
gutters = editor.getOption( 'gutters' ) || [];
[157] Fix | Delete
options = editor.getOption( 'lint' );
[158] Fix | Delete
if ( true === options ) {
[159] Fix | Delete
if ( ! _.contains( gutters, gutterName ) ) {
[160] Fix | Delete
editor.setOption( 'gutters', [ gutterName ].concat( gutters ) );
[161] Fix | Delete
}
[162] Fix | Delete
editor.setOption( 'lint', getLintOptions() ); // Expand to include linting options.
[163] Fix | Delete
} else if ( ! options ) {
[164] Fix | Delete
editor.setOption( 'gutters', _.without( gutters, gutterName ) );
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
// Force update on error notice to show or hide.
[168] Fix | Delete
if ( editor.getOption( 'lint' ) ) {
[169] Fix | Delete
editor.performLint();
[170] Fix | Delete
} else {
[171] Fix | Delete
currentErrorAnnotations = [];
[172] Fix | Delete
updateErrorNotice();
[173] Fix | Delete
}
[174] Fix | Delete
} );
[175] Fix | Delete
[176] Fix | Delete
// Update error notice when leaving the editor.
[177] Fix | Delete
editor.on( 'blur', updateErrorNotice );
[178] Fix | Delete
[179] Fix | Delete
// Work around hint selection with mouse causing focus to leave editor.
[180] Fix | Delete
editor.on( 'startCompletion', function() {
[181] Fix | Delete
editor.off( 'blur', updateErrorNotice );
[182] Fix | Delete
} );
[183] Fix | Delete
editor.on( 'endCompletion', function() {
[184] Fix | Delete
var editorRefocusWait = 500;
[185] Fix | Delete
editor.on( 'blur', updateErrorNotice );
[186] Fix | Delete
[187] Fix | Delete
// Wait for editor to possibly get re-focused after selection.
[188] Fix | Delete
_.delay( function() {
[189] Fix | Delete
if ( ! editor.state.focused ) {
[190] Fix | Delete
updateErrorNotice();
[191] Fix | Delete
}
[192] Fix | Delete
}, editorRefocusWait );
[193] Fix | Delete
});
[194] Fix | Delete
[195] Fix | Delete
/*
[196] Fix | Delete
* Make sure setting validities are set if the user tries to click Publish
[197] Fix | Delete
* while an autocomplete dropdown is still open. The Customizer will block
[198] Fix | Delete
* saving when a setting has an error notifications on it. This is only
[199] Fix | Delete
* necessary for mouse interactions because keyboards will have already
[200] Fix | Delete
* blurred the field and cause onUpdateErrorNotice to have already been
[201] Fix | Delete
* called.
[202] Fix | Delete
*/
[203] Fix | Delete
$( document.body ).on( 'mousedown', function( event ) {
[204] Fix | Delete
if ( editor.state.focused && ! $.contains( editor.display.wrapper, event.target ) && ! $( event.target ).hasClass( 'CodeMirror-hint' ) ) {
[205] Fix | Delete
updateErrorNotice();
[206] Fix | Delete
}
[207] Fix | Delete
});
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Configure tabbing.
[212] Fix | Delete
*
[213] Fix | Delete
* @param {CodeMirror} codemirror - Editor.
[214] Fix | Delete
* @param {Object} settings - Code editor settings.
[215] Fix | Delete
* @param {Object} settings.codeMirror - Settings for CodeMirror.
[216] Fix | Delete
* @param {Function} settings.onTabNext - Callback to handle tabbing to the next tabbable element.
[217] Fix | Delete
* @param {Function} settings.onTabPrevious - Callback to handle tabbing to the previous tabbable element.
[218] Fix | Delete
*
[219] Fix | Delete
* @return {void}
[220] Fix | Delete
*/
[221] Fix | Delete
function configureTabbing( codemirror, settings ) {
[222] Fix | Delete
var $textarea = $( codemirror.getTextArea() );
[223] Fix | Delete
[224] Fix | Delete
codemirror.on( 'blur', function() {
[225] Fix | Delete
$textarea.data( 'next-tab-blurs', false );
[226] Fix | Delete
});
[227] Fix | Delete
codemirror.on( 'keydown', function onKeydown( editor, event ) {
[228] Fix | Delete
var tabKeyCode = 9, escKeyCode = 27;
[229] Fix | Delete
[230] Fix | Delete
// Take note of the ESC keypress so that the next TAB can focus outside the editor.
[231] Fix | Delete
if ( escKeyCode === event.keyCode ) {
[232] Fix | Delete
$textarea.data( 'next-tab-blurs', true );
[233] Fix | Delete
return;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
// Short-circuit if tab key is not being pressed or the tab key press should move focus.
[237] Fix | Delete
if ( tabKeyCode !== event.keyCode || ! $textarea.data( 'next-tab-blurs' ) ) {
[238] Fix | Delete
return;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
// Focus on previous or next focusable item.
[242] Fix | Delete
if ( event.shiftKey ) {
[243] Fix | Delete
settings.onTabPrevious( codemirror, event );
[244] Fix | Delete
} else {
[245] Fix | Delete
settings.onTabNext( codemirror, event );
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
// Reset tab state.
[249] Fix | Delete
$textarea.data( 'next-tab-blurs', false );
[250] Fix | Delete
[251] Fix | Delete
// Prevent tab character from being added.
[252] Fix | Delete
event.preventDefault();
[253] Fix | Delete
});
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* @typedef {object} wp.codeEditor~CodeEditorInstance
[258] Fix | Delete
* @property {object} settings - The code editor settings.
[259] Fix | Delete
* @property {CodeMirror} codemirror - The CodeMirror instance.
[260] Fix | Delete
*/
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Initialize Code Editor (CodeMirror) for an existing textarea.
[264] Fix | Delete
*
[265] Fix | Delete
* @since 4.9.0
[266] Fix | Delete
*
[267] Fix | Delete
* @param {string|jQuery|Element} textarea - The HTML id, jQuery object, or DOM Element for the textarea that is used for the editor.
[268] Fix | Delete
* @param {Object} [settings] - Settings to override defaults.
[269] Fix | Delete
* @param {Function} [settings.onChangeLintingErrors] - Callback for when the linting errors have changed.
[270] Fix | Delete
* @param {Function} [settings.onUpdateErrorNotice] - Callback for when error notice should be displayed.
[271] Fix | Delete
* @param {Function} [settings.onTabPrevious] - Callback to handle tabbing to the previous tabbable element.
[272] Fix | Delete
* @param {Function} [settings.onTabNext] - Callback to handle tabbing to the next tabbable element.
[273] Fix | Delete
* @param {Object} [settings.codemirror] - Options for CodeMirror.
[274] Fix | Delete
* @param {Object} [settings.csslint] - Rules for CSSLint.
[275] Fix | Delete
* @param {Object} [settings.htmlhint] - Rules for HTMLHint.
[276] Fix | Delete
* @param {Object} [settings.jshint] - Rules for JSHint.
[277] Fix | Delete
*
[278] Fix | Delete
* @return {CodeEditorInstance} Instance.
[279] Fix | Delete
*/
[280] Fix | Delete
wp.codeEditor.initialize = function initialize( textarea, settings ) {
[281] Fix | Delete
var $textarea, codemirror, instanceSettings, instance;
[282] Fix | Delete
if ( 'string' === typeof textarea ) {
[283] Fix | Delete
$textarea = $( '#' + textarea );
[284] Fix | Delete
} else {
[285] Fix | Delete
$textarea = $( textarea );
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
instanceSettings = $.extend( {}, wp.codeEditor.defaultSettings, settings );
[289] Fix | Delete
instanceSettings.codemirror = $.extend( {}, instanceSettings.codemirror );
[290] Fix | Delete
[291] Fix | Delete
codemirror = wp.CodeMirror.fromTextArea( $textarea[0], instanceSettings.codemirror );
[292] Fix | Delete
[293] Fix | Delete
configureLinting( codemirror, instanceSettings );
[294] Fix | Delete
[295] Fix | Delete
instance = {
[296] Fix | Delete
settings: instanceSettings,
[297] Fix | Delete
codemirror: codemirror
[298] Fix | Delete
};
[299] Fix | Delete
[300] Fix | Delete
if ( codemirror.showHint ) {
[301] Fix | Delete
codemirror.on( 'keyup', function( editor, event ) { // eslint-disable-line complexity
[302] Fix | Delete
var shouldAutocomplete, isAlphaKey = /^[a-zA-Z]$/.test( event.key ), lineBeforeCursor, innerMode, token;
[303] Fix | Delete
if ( codemirror.state.completionActive && isAlphaKey ) {
[304] Fix | Delete
return;
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
// Prevent autocompletion in string literals or comments.
[308] Fix | Delete
token = codemirror.getTokenAt( codemirror.getCursor() );
[309] Fix | Delete
if ( 'string' === token.type || 'comment' === token.type ) {
[310] Fix | Delete
return;
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
innerMode = wp.CodeMirror.innerMode( codemirror.getMode(), token.state ).mode.name;
[314] Fix | Delete
lineBeforeCursor = codemirror.doc.getLine( codemirror.doc.getCursor().line ).substr( 0, codemirror.doc.getCursor().ch );
[315] Fix | Delete
if ( 'html' === innerMode || 'xml' === innerMode ) {
[316] Fix | Delete
shouldAutocomplete =
[317] Fix | Delete
'<' === event.key ||
[318] Fix | Delete
'/' === event.key && 'tag' === token.type ||
[319] Fix | Delete
isAlphaKey && 'tag' === token.type ||
[320] Fix | Delete
isAlphaKey && 'attribute' === token.type ||
[321] Fix | Delete
'=' === token.string && token.state.htmlState && token.state.htmlState.tagName;
[322] Fix | Delete
} else if ( 'css' === innerMode ) {
[323] Fix | Delete
shouldAutocomplete =
[324] Fix | Delete
isAlphaKey ||
[325] Fix | Delete
':' === event.key ||
[326] Fix | Delete
' ' === event.key && /:\s+$/.test( lineBeforeCursor );
[327] Fix | Delete
} else if ( 'javascript' === innerMode ) {
[328] Fix | Delete
shouldAutocomplete = isAlphaKey || '.' === event.key;
[329] Fix | Delete
} else if ( 'clike' === innerMode && 'php' === codemirror.options.mode ) {
[330] Fix | Delete
shouldAutocomplete = 'keyword' === token.type || 'variable' === token.type;
[331] Fix | Delete
}
[332] Fix | Delete
if ( shouldAutocomplete ) {
[333] Fix | Delete
codemirror.showHint( { completeSingle: false } );
[334] Fix | Delete
}
[335] Fix | Delete
});
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
// Facilitate tabbing out of the editor.
[339] Fix | Delete
configureTabbing( codemirror, settings );
[340] Fix | Delete
[341] Fix | Delete
return instance;
[342] Fix | Delete
};
[343] Fix | Delete
[344] Fix | Delete
})( window.jQuery, window.wp );
[345] Fix | Delete
[346] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function