|
| 1 | +/** |
| 2 | + * Preserve dots in license post slugs within the Gutenberg editor. |
| 3 | + * |
| 4 | + * Gutenberg's cleanForSlug (in wp.url) replaces dots with dashes, |
| 5 | + * which breaks SPDX identifiers like "Apache-2.0". This script: |
| 6 | + * |
| 7 | + * 1. Watches title changes via wp.data.subscribe and corrects the |
| 8 | + * slug when the title contains dots. |
| 9 | + * 2. Listens for focusout on the slug input (capture phase) to |
| 10 | + * preserve dots when a user manually edits the slug. |
| 11 | + * |
| 12 | + * This only runs on the license post-type editor screen (enqueue |
| 13 | + * is gated in PHP), so other post types are unaffected. |
| 14 | + */ |
| 15 | +( function() { |
| 16 | + var data = window.wp && window.wp.data; |
| 17 | + if ( ! data ) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + var lastTitle = ''; |
| 22 | + var lastSlug = ''; |
| 23 | + var isUpdating = false; |
| 24 | + var pendingDottedSlug = ''; |
| 25 | + |
| 26 | + /** |
| 27 | + * Sanitize a string into a slug while preserving dots. |
| 28 | + * |
| 29 | + * @param {string} raw The raw string to slugify. |
| 30 | + * @return {string} The slugified string with dots intact. |
| 31 | + */ |
| 32 | + function slugifyWithDots( raw ) { |
| 33 | + return raw |
| 34 | + .toLowerCase() |
| 35 | + .replace( / |–|—/g, '-' ) |
| 36 | + .replace( /[^\w.\s-]/g, '' ) |
| 37 | + .replace( /[\s]+/g, '-' ) |
| 38 | + .replace( /-+/g, '-' ) |
| 39 | + .replace( /(^-+)|(-+$)/g, '' ); |
| 40 | + } |
| 41 | + |
| 42 | + /* |
| 43 | + * Capture-phase focusout on the document. |
| 44 | + * |
| 45 | + * Fires before React's synthetic onBlur, so we can read the raw |
| 46 | + * input value (with dots) before cleanForSlug strips them. |
| 47 | + * After a short delay (to let Gutenberg commit its sanitized slug), |
| 48 | + * we overwrite the store with the dotted version. |
| 49 | + */ |
| 50 | + document.addEventListener( 'focusout', function( e ) { |
| 51 | + var target = e.target; |
| 52 | + if ( ! target || 'INPUT' !== target.tagName ) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + var urlWrapper = target.closest( '.editor-post-url__input' ); |
| 57 | + if ( ! urlWrapper ) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + var val = target.value || ''; |
| 62 | + if ( val.indexOf( '.' ) === -1 ) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + pendingDottedSlug = slugifyWithDots( val ); |
| 67 | + |
| 68 | + // Let Gutenberg commit its sanitized version, then overwrite. |
| 69 | + setTimeout( function() { |
| 70 | + if ( ! pendingDottedSlug ) { |
| 71 | + return; |
| 72 | + } |
| 73 | + isUpdating = true; |
| 74 | + data.dispatch( 'core/editor' ).editPost( { slug: pendingDottedSlug } ); |
| 75 | + isUpdating = false; |
| 76 | + lastSlug = pendingDottedSlug; |
| 77 | + pendingDottedSlug = ''; |
| 78 | + }, 100 ); |
| 79 | + }, true ); |
| 80 | + |
| 81 | + /* |
| 82 | + * Store subscriber: fix title-driven slug generation. |
| 83 | + * |
| 84 | + * When the title changes and contains a dot, Gutenberg generates |
| 85 | + * a slug via cleanForSlug which strips the dot. We detect this |
| 86 | + * and dispatch the corrected dotted slug. |
| 87 | + */ |
| 88 | + data.subscribe( function() { |
| 89 | + if ( isUpdating ) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + var editor = data.select( 'core/editor' ); |
| 94 | + if ( ! editor ) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + var title = editor.getEditedPostAttribute( 'title' ) || ''; |
| 99 | + var slug = editor.getEditedPostAttribute( 'slug' ) || ''; |
| 100 | + |
| 101 | + // Title changed and contains a dot — fix the slug. |
| 102 | + if ( title !== lastTitle && title.indexOf( '.' ) !== -1 ) { |
| 103 | + lastTitle = title; |
| 104 | + |
| 105 | + var dottedSlug = slugifyWithDots( title ); |
| 106 | + |
| 107 | + if ( slug !== dottedSlug && dottedSlug.indexOf( '.' ) !== -1 ) { |
| 108 | + isUpdating = true; |
| 109 | + data.dispatch( 'core/editor' ).editPost( { slug: dottedSlug } ); |
| 110 | + isUpdating = false; |
| 111 | + lastSlug = dottedSlug; |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + lastTitle = title; |
| 117 | + lastSlug = slug; |
| 118 | + } ); |
| 119 | +}() ); |
0 commit comments