Changeset 61588
- Timestamp:
- 02/04/2026 07:02:45 AM (12 hours ago)
- Location:
- trunk/src/js/_enqueues/wp
- Files:
-
- 2 edited
-
code-editor.js (modified) (9 diffs)
-
theme-plugin-editor.js (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/js/_enqueues/wp/code-editor.js
r61579 r61588 47 47 * @param {Function} settings.onUpdateErrorNotice - Callback to update error notice. 48 48 * 49 * @return { void}49 * @return {Function} Update error notice function. 50 50 */ 51 51 function configureLinting( editor, settings ) { // eslint-disable-line complexity … … 83 83 84 84 /* 85 * Note that rules must be sent in the "deprecated" lint.options property 85 * Note that rules must be sent in the "deprecated" lint.options property 86 86 * to prevent linter from complaining about unrecognized options. 87 87 * See <https://github.com/codemirror/CodeMirror/pull/4944>. … … 210 210 } 211 211 }); 212 213 return updateErrorNotice; 212 214 } 213 215 … … 262 264 * @property {object} settings - The code editor settings. 263 265 * @property {CodeMirror} codemirror - The CodeMirror instance. 266 * @property {Function} updateErrorNotice - Force update the error notice. 264 267 */ 265 268 … … 283 286 */ 284 287 wp.codeEditor.initialize = function initialize( textarea, settings ) { 285 var $textarea, codemirror, instanceSettings, instance ;288 var $textarea, codemirror, instanceSettings, instance, updateErrorNotice; 286 289 if ( 'string' === typeof textarea ) { 287 290 $textarea = $( '#' + textarea ); … … 295 298 codemirror = wp.CodeMirror.fromTextArea( $textarea[0], instanceSettings.codemirror ); 296 299 297 configureLinting( codemirror, instanceSettings );300 updateErrorNotice = configureLinting( codemirror, instanceSettings ); 298 301 299 302 instance = { 300 303 settings: instanceSettings, 301 codemirror: codemirror 304 codemirror, 305 updateErrorNotice, 302 306 }; 303 307 304 308 if ( codemirror.showHint ) { 305 codemirror.on( 'keyup', function( editor, event ) { // eslint-disable-line complexity 306 var shouldAutocomplete, isAlphaKey = /^[a-zA-Z]$/.test( event.key ), lineBeforeCursor, innerMode, token; 309 codemirror.on( 'inputRead', function( editor, change ) { 310 var shouldAutocomplete, isAlphaKey, lineBeforeCursor, innerMode, token, char; 311 312 // Only trigger autocompletion for typed input or IME composition. 313 if ( '+input' !== change.origin && ! change.origin.startsWith( '*compose' ) ) { 314 return; 315 } 316 317 // Only trigger autocompletion for single-character inputs. 318 // The text property is an array of strings, one for each line. 319 // We check that there is only one line and that line has only one character. 320 if ( 1 !== change.text.length || 1 !== change.text[0].length ) { 321 return; 322 } 323 324 char = change.text[0]; 325 isAlphaKey = /^[a-zA-Z]$/.test( char ); 326 307 327 if ( codemirror.state.completionActive && isAlphaKey ) { 308 328 return; … … 319 339 if ( 'html' === innerMode || 'xml' === innerMode ) { 320 340 shouldAutocomplete = ( 321 '<' === event.key||322 ( '/' === event.key&& 'tag' === token.type ) ||341 '<' === char || 342 ( '/' === char && 'tag' === token.type ) || 323 343 ( isAlphaKey && 'tag' === token.type ) || 324 344 ( isAlphaKey && 'attribute' === token.type ) || 325 ( '=' === event.key&& (345 ( '=' === char && ( 326 346 token.state.htmlState?.tagName || 327 347 token.state.curState?.htmlState?.tagName … … 331 351 shouldAutocomplete = 332 352 isAlphaKey || 333 ':' === event.key||334 ( ' ' === event.key&& /:\s+$/.test( lineBeforeCursor ) );353 ':' === char || 354 ( ' ' === char && /:\s+$/.test( lineBeforeCursor ) ); 335 355 } else if ( 'javascript' === innerMode ) { 336 shouldAutocomplete = isAlphaKey || '.' === event.key;356 shouldAutocomplete = isAlphaKey || '.' === char; 337 357 } else if ( 'clike' === innerMode && 'php' === codemirror.options.mode ) { 338 358 shouldAutocomplete = isAlphaKey && ( 'keyword' === token.type || 'variable' === token.type ); … … 341 361 codemirror.showHint( { completeSingle: false } ); 342 362 } 343 } );363 } ); 344 364 } 345 365 -
trunk/src/js/_enqueues/wp/theme-plugin-editor.js
r59789 r61588 3 3 */ 4 4 5 /* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1] }] */ 5 /* eslint-env es2020 */ 6 7 /* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 9, 1000] }] */ 6 8 7 9 if ( ! window.wp ) { … … 80 82 } else { 81 83 component.docsLookUpButton.prop( 'disabled', false ); 84 } 85 } ); 86 87 // Initiate saving the file when not focused in CodeMirror or when the user has syntax highlighting turned off. 88 $( window ).on( 'keydown', function( event ) { 89 if ( 90 ( event.ctrlKey || event.metaKey ) && 91 ( 's' === event.key.toLowerCase() ) && 92 ( ! component.instance || ! component.instance.codemirror.hasFocus() ) 93 ) { 94 event.preventDefault(); 95 component.form.trigger( 'submit' ); 82 96 } 83 97 } ); … … 192 206 } 193 207 208 if ( component.instance && component.instance.updateErrorNotice ) { 209 component.instance.updateErrorNotice(); 210 } 211 194 212 // Scroll to the line that has the error. 195 213 if ( component.lintErrors.length ) { … … 400 418 editor.codemirror.on( 'change', component.onChange ); 401 419 420 function onSaveShortcut() { 421 component.form.trigger( 'submit' ); 422 } 423 424 editor.codemirror.setOption( 'extraKeys', { 425 ...( editor.codemirror.getOption( 'extraKeys' ) || {} ), 426 'Ctrl-S': onSaveShortcut, 427 'Cmd-S': onSaveShortcut, 428 } ); 429 402 430 // Improve the editor accessibility. 403 431 $( editor.codemirror.display.lineDiv )
Note: See TracChangeset
for help on using the changeset viewer.