Skip to:
Content

BuddyPress.org

Changeset 10053


Ignore:
Timestamp:
08/18/2015 10:49:31 PM (9 years ago)
Author:
djpaul
Message:

Activity: fix @mentions in wp-admin post editor.

If the screen loads with the text editor visible, bp_mentions is attached to the text editor as expected, but loadMentionsInTinyMCE uses up its tries and gives up trying to attach the iframe, so when you switch to the visual editor, bp_mentions didn't work.

This change removes the looped loadMentionsInTinyMCE approach and instead hooks into TinyMCE configuration to specify an init callback, where we then load bp_mentions.

Fixes #6487 (branch). Props dcavins, imath, DJPaul.

Location:
branches/2.3/src/bp-activity
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.3/src/bp-activity/bp-activity-cssjs.php

    r9819 r10053  
    4242    }
    4343
     44    // If the script has been enqueued, let's attach our mentions TinyMCE init callback.
     45    add_filter( 'tiny_mce_before_init', 'bp_add_mentions_on_tinymce_init', 10, 2 );
     46
    4447    /**
    4548     * Fires at the end of the Activity Mentions script.
     
    5457add_action( 'bp_enqueue_scripts', 'bp_activity_mentions_script' );
    5558add_action( 'bp_admin_enqueue_scripts', 'bp_activity_mentions_script' );
     59
     60/**
     61 * Bind the mentions listener to a wp_editor instance when TinyMCE initializes.
     62 *
     63 * @since BuddyPress (2.3.3)
     64 *
     65 * @param array  $settings   An array with TinyMCE config.
     66 * @param string $editor_id Unique editor identifier, e.g. 'content'.
     67 *
     68 * @return array  $mceInit   An array with TinyMCE config.
     69 */
     70function bp_add_mentions_on_tinymce_init( $settings, $editor_id ) {
     71    // We only apply the mentions init to the visual post editor in the WP dashboard.
     72    if ( 'content' === $editor_id ) {
     73        $settings['init_instance_callback'] = 'window.bp.mentions.tinyMCEinit';
     74    }
     75
     76    return $settings;
     77}
  • branches/2.3/src/bp-activity/js/mentions.js

    r9819 r10053  
    1 (function( $, undefined ) {
     1/* global bp */
     2
     3window.bp = window.bp || {};
     4
     5( function( bp, $, undefined ) {
    26    var mentionsQueryCache = [],
    37        mentionsItem;
     8
     9    bp.mentions       = bp.mentions || {};
     10    bp.mentions.users = window.bp.mentions.users || [];
     11
     12    if ( typeof window.BP_Suggestions === 'object' ) {
     13        bp.mentions.users = window.BP_Suggestions.friends || bp.mentions.users;
     14    }
    415
    516    /**
     
    229240
    230241    $( document ).ready(function() {
    231         var loadMentionsInTinyMCE,
    232             loadAttempts = 0,
    233             users        = [];
    234 
    235         if ( typeof window.BP_Suggestions === 'object' ) {
    236             users = window.BP_Suggestions.friends || users;
    237         }
    238 
    239         // Dashboard post 'visual' editor.
    240         loadMentionsInTinyMCE = function() {
    241             if ( loadAttempts < 4 || ! $( 'body' ).hasClass( 'wp-admin' ) ) {
    242                 loadAttempts++;
    243 
    244                 if ( typeof window.tinyMCE === 'undefined' || window.tinyMCE.activeEditor === null || typeof window.tinyMCE.activeEditor === 'undefined' ) {
    245                     setTimeout( loadMentionsInTinyMCE, 500 );
    246                     return;
    247                 }
    248             }
    249 
     242        // Activity/reply, post comments, dashboard post 'text' editor.
     243        $( '.bp-suggestions, #comments form textarea, .wp-editor-area' ).bp_mentions( bp.mentions.users );
     244    });
     245
     246    bp.mentions.tinyMCEinit = function() {
     247        if ( typeof window.tinyMCE === 'undefined' || window.tinyMCE.activeEditor === null || typeof window.tinyMCE.activeEditor === 'undefined' ) {
     248            return;
     249        } else {
    250250            $( window.tinyMCE.activeEditor.contentDocument.activeElement )
    251251                .atwho( 'setIframe', $( '#content_ifr' )[0] )
    252                 .bp_mentions( users );
    253         };
    254 
    255         // Activity/reply, post comments, dashboard post 'text' editor.
    256         $( '.bp-suggestions, #comments form textarea, .wp-editor-area' ).bp_mentions( users );
    257 
    258         // Dashboard post 'visual' editor.
    259         loadMentionsInTinyMCE();
    260     });
    261 })( jQuery );
     252                .bp_mentions( bp.mentions.users );
     253        }
     254    };
     255})( bp, jQuery );
Note: See TracChangeset for help on using the changeset viewer.