code-editor.js 11 KB

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