Making WordPress.org

Changeset 12713


Ignore:
Timestamp:
07/05/2023 10:45:32 AM (17 months ago)
Author:
amieiro
Message:

Translate: Sync remote repo (gp-translation-helpers)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/js/editor.js

    r12673 r12713  
    1 /* global $gp, $gp_translation_helpers_editor, wpApiSettings, $gp_comment_feedback_settings, $gp_editor_options, fetch, TextDecoderStream, URL, URLSearchParams, window */
     1/* global document, $gp, $gp_translation_helpers_editor, wpApiSettings, $gp_comment_feedback_settings, $gp_editor_options, fetch, TextDecoderStream, URL, URLSearchParams, window */
    22/* eslint camelcase: "off" */
    33jQuery( function( $ ) {
     4    /**
     5     * Stores (caches) the content of the translation helpers, to avoid making the query another time.
     6     *
     7     * @type {Array}
     8     */
     9    // eslint-disable-next-line prefer-const
     10    let translationHelpersCache = [];
    411    let focusedRowId = '';
    512    // When a user clicks on a sidebar tab, the visible tab and div changes.
     
    2027        const rowId = tr.attr( 'row' );
    2128        const translation_status = tr.find( '.panel-header' ).find( 'span' ).html();
     29        const nextEditor = $gp.editor.current.nextAll( 'tr.editor' ).first();
    2230        const chatgpt_review_status = JSON.parse( window.localStorage.getItem( 'translate-details-state' ) );
    2331        const chatgpt_review_enabled = ( chatgpt_review_status && 'open' === chatgpt_review_status[ 'details-chatgpt' ] ) || ! chatgpt_review_status;
     
    2836        focusedRowId = rowId;
    2937        loadTabsAndDivs( tr );
     38
     39        if ( nextEditor.length ) {
     40            cacheTranslationHelpersForARow( nextEditor );
     41        }
     42
    3043        if ( chatgpt_review_enabled && $gp_comment_feedback_settings.openai_key && $gp_editor_options.can_approve && ( 'waiting' === translation_status || 'fuzzy' === translation_status ) ) {
    3144            fetchOpenAIReviewResponse( rowId, tr, false );
    3245        } else {
    33             if ( ! $gp_comment_feedback_settings.openai_key ) {
    34                 tr.find( '.details-chatgpt' ).hide();
    35             }
    36             tr.find( '.openai-review' ).hide();
     46            tr.find( '.details-chatgpt, .openai-review' ).hide();
    3747        }
    3848    } );
     
    194204    }
    195205
     206    $( document ).ready( function() {
     207        // Gets the translation helpers for the first row and caches them.
     208        const firstEditor = $( '#translations' ).find( 'tr.editor' ).first();
     209        cacheTranslationHelpersForARow( firstEditor );
     210    } );
     211
    196212    /**
    197213     * Hides all tabs and show one of them, the last clicked.
     
    237253
    238254    /**
    239      * Load the content in the tabs (header tab and content) for the opened row.
     255     * Loads the content in the tabs (header tab and content) for the opened row.
    240256     *
    241257     * @param {Object} element The element that triggers the action.
    242258     */
    243259    function loadTabsAndDivs( element ) {
    244         const originalId = element.closest( 'tr' ).attr( 'id' ).substring( 7 );
    245         const requestUrl = $gp_translation_helpers_editor.translation_helper_url + originalId + '?nohc';
    246         $.getJSON( requestUrl, function( data ) {
     260        const rowId = element.closest( 'tr.editor' ).attr( 'id' ).substring( 7 );
     261        const requestUrl = $gp_translation_helpers_editor.translation_helper_url + rowId + '?nohc';
     262        if ( translationHelpersCache[ rowId ] !== undefined ) {
     263            updateDataInTabs( translationHelpersCache[ rowId ], rowId );
     264        } else {
     265            $.getJSON( requestUrl, function( data ) {
     266                translationHelpersCache[ rowId ] = data;
     267                updateDataInTabs( data, rowId );
     268            } );
     269        }
     270    }
     271
     272    /**
     273     * Updates the content of the tabs and divs.
     274     *
     275     * @param {Object} data       The content to update the tabs and divs.
     276     * @param {number} originalId The id of the original string to translate.
     277     *
     278     * @return {void}
     279     */
     280    function updateDataInTabs( data, originalId ) {
     281        if ( data[ 'helper-translation-discussion-' + originalId ] !== undefined ) {
    247282            $( '[data-tab="sidebar-tab-discussion-' + originalId + '"]' ).html( 'Discussion (' + data[ 'helper-translation-discussion-' + originalId ].count + ')' );
    248283            $( '#sidebar-div-discussion-' + originalId ).html( data[ 'helper-translation-discussion-' + originalId ].content );
     284        }
     285        if ( data[ 'helper-history-' + originalId ] !== undefined ) {
    249286            $( '[data-tab="sidebar-tab-history-' + originalId + '"]' ).html( 'History (' + data[ 'helper-history-' + originalId ].count + ')' );
    250287            $( '#sidebar-div-history-' + originalId ).html( data[ 'helper-history-' + originalId ].content );
     288        }
     289        if ( data[ 'helper-other-locales-' + originalId ] !== undefined ) {
    251290            $( '[data-tab="sidebar-tab-other-locales-' + originalId + '"]' ).html( 'Other locales (' + data[ 'helper-other-locales-' + originalId ].count + ')' );
    252291            $( '#sidebar-div-other-locales-' + originalId ).html( data[ 'helper-other-locales-' + originalId ].content );
    253292            add_copy_button( '#sidebar-div-other-locales-' + originalId );
    254         } );
     293        }
     294    }
     295
     296    /**
     297     * Caches the translation helpers for a row.
     298     *
     299     * @param {Object} editor The editor row.
     300     *
     301     * @return {void}
     302     */
     303    function cacheTranslationHelpersForARow( editor ) {
     304        const rowId = editor.attr( 'row' );
     305        const requestUrl = $gp_translation_helpers_editor.translation_helper_url + rowId + '?nohc';
     306        if ( ! rowId ) {
     307            return;
     308        }
     309
     310        if ( translationHelpersCache[ rowId ] === undefined ) {
     311            // Store a string with a space to avoid making the same request another time.
     312            translationHelpersCache[ rowId ] = ' ';
     313            $.getJSON( requestUrl, function( data ) {
     314                translationHelpersCache[ rowId ] = data;
     315                updateDataInTabs( data, rowId );
     316            } );
     317        }
    255318    }
    256319
Note: See TracChangeset for help on using the changeset viewer.