code-editor.js 11 KB

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