<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12677] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js: Translate: Preload the suggestions</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/12677">12677</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/12677","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>amieiro</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-06-26 08:52:54 +0000 (Mon, 26 Jun 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Translate: Preload the suggestions

See https://github.com/WordPress/wordpress.org/pull/141 </pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsjstranslationsuggestionsjs">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsjstranslationsuggestionsjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js   2023-06-23 02:42:27 UTC (rev 12676)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js     2023-06-26 08:52:54 UTC (rev 12677)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,21 +1,66 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* global $gp */
</ins><span class="cx" style="display: block; padding: 0 10px"> ( function( $ ){
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Stores the originalId of the translations for which OpenAI has already been queried,
-        * to avoid making the query another time.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Stores (caches) the translation memory (TM) suggestions that has already been queried,
+        * to avoid making the query another time. The key is the originalId.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @type {array}
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        var TMSuggestionRequested = [];
+
+       /**
+        * Stores (caches) the OpenAI suggestions that has already been queried,
+        * to avoid making the query another time. The key is the originalId.
+        *
+        * @type {array}
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         var OpenAITMSuggestionRequested = [];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Stores the originalId of the translations for which DeepL has already been queried,
-        * to avoid making the query another time.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Stores (caches) the DeepL suggestions that has already been queried,
+        * to avoid making the query another time. The key is the originalId.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @type {array}
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        var DeeplTMSuggestionRequested = [];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function fetchSuggestions( $container, apiUrl, originalId, translationId, nonce ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Stores (caches) the "Other Languages" suggestions that has already been queried,
+        * to avoid making the query another time. The key is the originalId.
+        *
+        * @type {array}
+        */
+       var OtherLanguagesSuggestionRequested = [];
+
+       /**
+        * Requests the suggestions from an external system, stores them in the cache and appended them to the container.
+        * If the suggestions are already in the cache, they are appended to the container.
+        *
+        * @param {object} $container    The container where the suggestions will be stored.
+        * @param {string} apiUrl        The URL of the API.
+        * @param {number} originalId    The ID of the original string.
+        * @param {number} translationId The ID of the translation string.
+        * @param {string} nonce         The nonce to use to make the request.
+        * @param {string} type          The type of suggestions to fetch: TM, OpenAI, DeepL, OL (Other Languages).
+        *
+        * @return {void}
+        */
+       function fetchSuggestions( $container, apiUrl, originalId, translationId, nonce, type ) {
+               var cachedSuggestion = getTheSuggestionFromTheCache( type, originalId );
+               if ( cachedSuggestion ) {
+                       $container.removeClass( 'fetching' );
+                       $container.find( '.suggestions__loading-indicator' ).remove();
+                       if ( isThisTypeOfSuggestionInTheContainer( $container, type ) ) {
+                               return;
+                       }
+
+                       $container.append( cachedSuggestion );
+                       removeNoSuggestionsMessage( $container );
+                       copyTranslationMemoryToSidebarTab( $container );
+                       return;
+               }
+               // Store a string with a space to avoid making the same request another time.
+               storeTheSuggestionInTheCache( type, originalId, ' ' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 var xhr = $.ajax( {
</span><span class="cx" style="display: block; padding: 0 10px">                        url: apiUrl,
</span><span class="cx" style="display: block; padding: 0 10px">                        data: {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,8 +76,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $container.find( '.suggestions__loading-indicator' ).remove();
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( response.success ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $container.append( response.data );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                storeTheSuggestionInTheCache( type, originalId, response.data );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 removeNoSuggestionsMessage( $container );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                copyTranslationMemoryToSidebarTab();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         copyTranslationMemoryToSidebarTab( $container );
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                $container.append( $( '<span/>', { 'text': 'Error while loading suggestions.' } ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -51,14 +97,101 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Gets the suggestions for the first row and stores them in the local cache.
+        *
+        * @return {void}
+        */
+       function getSuggestionsForTheFirstRow() {
+               var firstEditor = $( '#translations' ).find( '.editor' ).first();
+               var row_id = firstEditor.closest( 'tr' ).attr( 'row' );
+               if ( ! row_id ) {
+                       return;
+               }
+               firstEditor.row_id = row_id;
+               firstEditor.original_id = $gp.editor.original_id_from_row_id( row_id );
+               firstEditor.translation_id = $gp.editor.translation_id_from_row_id( row_id );
+               maybeFetchTranslationMemorySuggestions( firstEditor );
+               maybeFetchOpenAISuggestions( firstEditor );
+               maybeFetchDeeplSuggestions( firstEditor );
+               maybeFetchOtherLanguageSuggestions( firstEditor );
+       }
+
+       /**
+        * Gets a suggestion from the local cache.
+        *
+        * @param {string} type       The type of suggestions to fetch: TM, OpenAI, DeepL, OL (Other Languages).
+        * @param {number} originalId The ID of the original string.
+        *
+        * @return {string|boolean}  The suggestion if it is in the cache, false otherwise.
+        */
+       function getTheSuggestionFromTheCache( type, originalId ) {
+
+               switch ( type ) {
+                       case 'TM':
+                               if ( ! ( originalId in TMSuggestionRequested ) ) {
+                                       return false;
+                               }
+                               return TMSuggestionRequested[ originalId ];
+                               break;
+                       case 'OpenAI':
+                               if ( ! ( originalId in OpenAITMSuggestionRequested ) ) {
+                                       return false;
+                               }
+                               return OpenAITMSuggestionRequested[ originalId ];
+                               break;
+                       case 'DeepL':
+                               if ( ! ( originalId in DeeplTMSuggestionRequested ) ) {
+                                       return false;
+                               }
+                               return DeeplTMSuggestionRequested[ originalId ];
+                               break;
+                       case 'OL':
+                               if ( ! ( originalId in OtherLanguagesSuggestionRequested ) ) {
+                                       return false;
+                               }
+                               return OtherLanguagesSuggestionRequested[ originalId ];
+                               break;
+               }
+       }
+
+       /**
+        * Stores the suggestion in the local cache (JavaScript variables).
+        *
+        * @param {string} type       The type of suggestions to fetch: TM, OpenAI, DeepL, OL (Other Languages).
+        * @param {number} originalId The ID of the original string.
+        * @param {string} suggestion The suggestion to store.
+        *
+        * @return {void}
+        */
+       function storeTheSuggestionInTheCache( type, originalId, suggestion ) {
+               switch (type) {
+                       case 'TM':
+                               TMSuggestionRequested[ originalId ] = suggestion;
+                               break;
+                       case 'OpenAI':
+                               OpenAITMSuggestionRequested[ originalId ] = suggestion;
+                               break;
+                       case 'DeepL':
+                               DeeplTMSuggestionRequested[ originalId ] = suggestion;
+                               break;
+                       case 'OL':
+                               OtherLanguagesSuggestionRequested[ originalId ] = suggestion;
+                               break;
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Copies the translation memory to the sidebar tab and adds the number of items in the TM to the tab.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param {object} $container The container where the suggestions are stored.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return {void}
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function copyTranslationMemoryToSidebarTab(){
-           var divSidebarWithTM = $gp.editor.current.find( '.meta.translation-memory' ).first();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function copyTranslationMemoryToSidebarTab( $container ){
+               var editor = $container.closest( '.editor' );
+           var divSidebarWithTM = editor.find( '.meta.translation-memory' ).first();
</ins><span class="cx" style="display: block; padding: 0 10px">                 var divId = divSidebarWithTM.attr( 'data-row-id' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                var TMcontainer = $gp.editor.current.find( '.suggestions__translation-memory' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         var TMcontainer = editor.find( '.suggestions__translation-memory' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( !TMcontainer.length ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -71,9 +204,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Add the suggestion from the translation memory to the translation textarea if
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Adds the suggestion from the translation memory to the translation textarea if
</ins><span class="cx" style="display: block; padding: 0 10px">          * the suggestion has 100% of accuracy.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param {string} data The HTML response from the TM.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return {void}
</span><span class="cx" style="display: block; padding: 0 10px">         **/
</span><span class="cx" style="display: block; padding: 0 10px">        function addSuggestionToTranslation( data ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -97,8 +232,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function maybeFetchTranslationMemorySuggestions() {
-               var $container = $gp.editor.current.find( '.suggestions__translation-memory' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Fetches the suggestions from the translation memory.
+        *
+        * @param {object} editor The editor object.
+        *
+        * @return {void}
+        */
+       function maybeFetchTranslationMemorySuggestions( editor ) {
+               var $container = editor.find( '.suggestions__translation-memory' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( !$container.length ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -107,48 +249,53 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !$gp.editor.current.find('translation-suggestion.with-tooltip.translation').first() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( !editor.find('translation-suggestion.with-tooltip.translation').first() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $container.addClass( 'fetching' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                var originalId = $gp.editor.current.original_id;
-               var translationId = $gp.editor.current.translation_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         var originalId = editor.original_id;
+               var translationId = editor.translation_id;
</ins><span class="cx" style="display: block; padding: 0 10px">                 var nonce = $container.data( 'nonce' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                fetchSuggestions( $container, window.WPORG_TRANSLATION_MEMORY_API_URL, originalId, translationId, nonce );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         fetchSuggestions( $container, window.WPORG_TRANSLATION_MEMORY_API_URL, originalId, translationId, nonce, 'TM' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Gets the suggestions from the OpenAI API.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param {object} editor The editor object.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return {void}
</span><span class="cx" style="display: block; padding: 0 10px">         **/
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function maybeFetchOpenAISuggestions() {
-               maybeFetchExternalSuggestions( 'OpenAI', gpTranslationSuggestions.get_external_translations.get_openai_translations, window.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function maybeFetchOpenAISuggestions( editor ) {
+               maybeFetchExternalSuggestions( editor, 'OpenAI', gpTranslationSuggestions.get_external_translations.get_openai_translations, window.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Gets the suggestions from the DeepL API.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param {object} editor The editor object.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return {void}
</span><span class="cx" style="display: block; padding: 0 10px">         **/
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function maybeFetchDeeplSuggestions() {
-               maybeFetchExternalSuggestions( 'DeepL', gpTranslationSuggestions.get_external_translations.get_deepl_translations, window.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function maybeFetchDeeplSuggestions( editor ) {
+               maybeFetchExternalSuggestions( editor, 'DeepL', gpTranslationSuggestions.get_external_translations.get_deepl_translations, window.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Gets the suggestions from an external service.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param type                                   The type of the external service: OpenAI or DeepL.
-        * @param getExternalSuggestions Whether to get the suggestions from the external service.
-        * @param apiUrl                                 The URL of the API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param {object}  editor                 The editor.
+        * @param {string}  type                   The type of the external service: OpenAI or DeepL.
+        * @param {boolean} getExternalSuggestions Whether to get the suggestions from the external service.
+        * @param {string}  apiUrl                 The URL of the API.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return {void}
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function maybeFetchExternalSuggestions( type, getExternalSuggestions, apiUrl ) {
-               var $container = $gp.editor.current.find( '.suggestions__translation-memory' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function maybeFetchExternalSuggestions( editor, type, getExternalSuggestions, apiUrl ) {
+               var $container = editor.find( '.suggestions__translation-memory' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( !$container.length ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -155,45 +302,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( true !== getExternalSuggestions ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                var originalId = $gp.editor.current.original_id;
-               var translationId = $gp.editor.current.translation_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         var originalId = editor.original_id;
+               var translationId = editor.translation_id;
</ins><span class="cx" style="display: block; padding: 0 10px">                 var nonce = $container.data( 'nonce' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if( true === wasRequestMade( type, originalId ) ) {
-                       return;
-               }
-
-               fetchSuggestions( $container, apiUrl, originalId, translationId, nonce );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         fetchSuggestions( $container, apiUrl, originalId, translationId, nonce, type );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Checks if the request was already made for this originalId and type.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Gets the suggestions from other languages.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param type            The type of the external service: OpenAI or DeepL.
-        * @param originalId  The original ID.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param {object} editor The editor object.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @returns {boolean} Whether the request was already made.
-        */
-       function wasRequestMade( type, originalId ) {
-               if ('OpenAI' === type) {
-                       if ( originalId in OpenAITMSuggestionRequested ) {
-                               return true;
-                       } else {
-                               OpenAITMSuggestionRequested[originalId] = true;
-                       }
-               }
-               if ('DeepL' === type) {
-                       if ( originalId in DeeplTMSuggestionRequested ) {
-                               return true;
-                       } else {
-                               DeeplTMSuggestionRequested[originalId] = true;
-                       }
-               }
-               return false;
-       }
-
-       function maybeFetchOtherLanguageSuggestions() {
-               var $container = $gp.editor.current.find( '.suggestions__other-languages' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return {void}
+        **/
+       function maybeFetchOtherLanguageSuggestions( editor ) {
+               var $container = editor.find( '.suggestions__other-languages' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! $container.length ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -204,11 +328,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $container.addClass( 'fetching' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                var originalId = $gp.editor.current.original_id;
-               var translationId = $gp.editor.current.translation_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         var originalId = editor.original_id;
+               var translationId = editor.translation_id;
</ins><span class="cx" style="display: block; padding: 0 10px">                 var nonce = $container.data( 'nonce' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                fetchSuggestions( $container, window.WPORG_OTHER_LANGUAGES_API_URL, originalId , translationId,  nonce );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         fetchSuggestions( $container, window.WPORG_OTHER_LANGUAGES_API_URL, originalId , translationId,  nonce, 'OL' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -216,7 +340,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * This is needed because the suggestions are loaded asynchronously.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param $container
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param {object} $container The container where the suggestions are stored.
+        *
+        * @return {void}
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        function removeNoSuggestionsMessage( $container ) {
</span><span class="cx" style="display: block; padding: 0 10px">                var hasSuggestions = $container.find( '.translation-suggestion' ).length > 0;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,7 +349,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( hasSuggestions ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $container.find( '.no-suggestions' ).hide();
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $container = removeNoSuggestionsDuplicateMessage( $container );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 removeNoSuggestionsDuplicateMessage( $container );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -230,8 +356,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Removes duplicate "No suggestions" messages.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param $container
-        * @returns {*|jQuery}
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param {object} $container The container where the suggestions are stored.
+        *
+        * @return {void}
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        function removeNoSuggestionsDuplicateMessage( $container ) {
</span><span class="cx" style="display: block; padding: 0 10px">                var $html = $($container);
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -247,8 +374,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $(this).remove();
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                });
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               return $html.prop('outerHTML');
</del><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px">        function copySuggestion( event ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'A' === event.target.tagName ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -273,14 +398,54 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $activeTextarea[0].dispatchEvent( event );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Checks if the suggestions of a certain type are in the container.
+        *
+        * @param {object} container The container.
+        * @param {string} type The type of the suggestions: OpenAI or DeepL.
+        *
+        * @return {boolean}
+        */
+       function isThisTypeOfSuggestionInTheContainer( container, type ) {
+               switch ( type ) {
+                       case 'TM':
+                               if ( container.find( '.translation-suggestion.with-tooltip.translation' ).length > 0 ) {
+                                       return true;
+                               }
+                               break;
+                       case 'OpenAI':
+                               if ( container.find( '.translation-suggestion.with-tooltip.openai' ).length > 0 ) {
+                                       return true;
+                               }
+                               break;
+                       case 'DeepL':
+                               if ( container.find( '.translation-suggestion.with-tooltip.deepl' ).length > 0 ) {
+                                       return true;
+                               }
+                               break;
+               }
+               return false;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $gp.editor.show = ( function( original ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        original.apply( $gp.editor, arguments );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        maybeFetchTranslationMemorySuggestions();
-                       maybeFetchOpenAISuggestions();
-                       maybeFetchDeeplSuggestions();
-                       maybeFetchOtherLanguageSuggestions();
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 maybeFetchTranslationMemorySuggestions( $gp.editor.current );
+                       maybeFetchOpenAISuggestions( $gp.editor.current );
+                       maybeFetchDeeplSuggestions( $gp.editor.current );
+                       maybeFetchOtherLanguageSuggestions( $gp.editor.current );
+                       var nextEditor = $gp.editor.current.nextAll('tr.editor' ).first();
+                       if ( nextEditor.length ) {
+                               var row_id = nextEditor.closest( 'tr' ).attr( 'row' );
+                               nextEditor.row_id = row_id;
+                               nextEditor.original_id = $gp.editor.original_id_from_row_id( row_id );
+                               nextEditor.translation_id = $gp.editor.translation_id_from_row_id( row_id );
+                               maybeFetchTranslationMemorySuggestions( nextEditor );
+                               maybeFetchOpenAISuggestions( nextEditor );
+                               maybeFetchDeeplSuggestions( nextEditor );
+                               maybeFetchOtherLanguageSuggestions( nextEditor );
+                       }
+               };
</ins><span class="cx" style="display: block; padding: 0 10px">         })( $gp.editor.show );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $gp.editor.install_hooks = ( function( original ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -289,7 +454,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $( $gp.editor.table )
</span><span class="cx" style="display: block; padding: 0 10px">                                .on( 'click', '.translation-suggestion', copySuggestion );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $( document ).ready( function() {
+                               getSuggestionsForTheFirstRow();
+                       });
+               };
</ins><span class="cx" style="display: block; padding: 0 10px">         })( $gp.editor.install_hooks );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> })( jQuery );
</span></span></pre>
</div>
</div>

</body>
</html>