code-editor.js 11 KB

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