Changeset 12584
- Timestamp:
- 05/12/2023 08:05:57 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js
r12581 r12584 1 1 ( function( $ ){ 2 /** 3 * Stores the originalId of the translations for which OpenAI has already been queried, 4 * to avoid making the query another time. 5 * 6 * @type {array} 7 */ 8 var OpenAITMSuggestionRequested = []; 9 10 /** 11 * Stores the originalId of the translations for which DeepL has already been queried, 12 * to avoid making the query another time. 13 * 14 * @type {array} 15 */ 16 var DeeplTMSuggestionRequested = []; 17 2 18 function fetchSuggestions( $container, apiUrl, originalId, translationId, nonce ) { 3 19 var xhr = $.ajax( { … … 16 32 if ( response.success ) { 17 33 $container.append( response.data ); 34 removeNoSuggestionsMessage( $container ); 35 copyTranslationMemoryToSidebarTab(); 18 36 } else { 19 37 $container.append( $( '<span/>', { 'text': 'Error while loading suggestions.' } ) ); … … 30 48 xhr.always( function() { 31 49 $container.removeClass( 'fetching' ); 32 removeNoSuggestionsMessage( $container ); 33 } ); 50 } ); 51 } 52 53 /** 54 * Copies the translation memory to the sidebar tab and adds the number of items in the TM to the tab. 55 * 56 * @return {void} 57 */ 58 function copyTranslationMemoryToSidebarTab(){ 59 var divSidebarWithTM = $gp.editor.current.find( '.meta.translation-memory' ).first(); 60 var divId = divSidebarWithTM.attr( 'data-row-id' ); 61 var TMcontainer = $gp.editor.current.find( '.suggestions__translation-memory' ); 62 if ( !TMcontainer.length ) { 63 return; 64 } 65 if ( !TMcontainer.hasClass( 'initialized' ) ) { 66 return; 67 } 68 69 var itemsInTM = TMcontainer.find( '.translation-suggestion.with-tooltip.translation' ).length; 70 itemsInTM += TMcontainer.find( '.translation-suggestion.with-tooltip.deepl' ).length; 71 itemsInTM += TMcontainer.find( '.translation-suggestion.with-tooltip.openai' ).length; 72 $( '[data-tab="sidebar-tab-translation-memory-' + divId + '"]' ).html( 'TM (' + itemsInTM + ')' ); 73 $( '#sidebar-div-translation-memory-' + divId ).html( TMcontainer.html() ); 34 74 } 35 75 … … 71 111 } 72 112 113 if ( !$gp.editor.current.find('translation-suggestion.with-tooltip.translation').first() ) { 114 return; 115 } 116 73 117 $container.addClass( 'fetching' ); 74 118 … … 86 130 **/ 87 131 function maybeFetchOpenAISuggestions() { 88 maybeFetchExternalSuggestions( gpTranslationSuggestions.get_external_translations.get_openai_translations, window.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL );132 maybeFetchExternalSuggestions( 'OpenAI', gpTranslationSuggestions.get_external_translations.get_openai_translations, window.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL ); 89 133 } 90 134 … … 95 139 **/ 96 140 function maybeFetchDeeplSuggestions() { 97 maybeFetchExternalSuggestions( gpTranslationSuggestions.get_external_translations.get_deepl_translations, window.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL );141 maybeFetchExternalSuggestions( 'DeepL', gpTranslationSuggestions.get_external_translations.get_deepl_translations, window.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL ); 98 142 } 99 143 … … 101 145 * Gets the suggestions from an external service. 102 146 * 103 * @param getExternalSuggestions 104 * @param apiUrl 105 */ 106 function maybeFetchExternalSuggestions( getExternalSuggestions, apiUrl ) { 147 * @param type The type of the external service: OpenAI or DeepL. 148 * @param getExternalSuggestions Whether to get the suggestions from the external service. 149 * @param apiUrl The URL of the API. 150 * 151 * @return {void} 152 */ 153 function maybeFetchExternalSuggestions( type, getExternalSuggestions, apiUrl ) { 107 154 var $container = $gp.editor.current.find( '.suggestions__translation-memory' ); 108 155 if ( !$container.length ) { … … 112 159 return; 113 160 } 114 115 161 var originalId = $gp.editor.current.original_id; 116 162 var translationId = $gp.editor.current.translation_id; 117 163 var nonce = $container.data( 'nonce' ); 118 164 165 if( true === wasRequestMade( type, originalId ) ) { 166 return; 167 } 168 119 169 fetchSuggestions( $container, apiUrl, originalId, translationId, nonce ); 170 } 171 172 /** 173 * Checks if the request was already made for this originalId and type. 174 * 175 * @param type The type of the external service: OpenAI or DeepL. 176 * @param originalId The original ID. 177 * 178 * @returns {boolean} Whether the request was already made. 179 */ 180 function wasRequestMade( type, originalId ) { 181 if ('OpenAI' === type) { 182 if ( originalId in OpenAITMSuggestionRequested ) { 183 return true; 184 } else { 185 OpenAITMSuggestionRequested[originalId] = true; 186 } 187 } 188 if ('DeepL' === type) { 189 if ( originalId in DeeplTMSuggestionRequested ) { 190 return true; 191 } else { 192 DeeplTMSuggestionRequested[originalId] = true; 193 } 194 } 195 return false; 120 196 } 121 197
Note: See TracChangeset
for help on using the changeset viewer.