|
1 | | -jQuery(function ($) { |
2 | | - let getUsers; |
3 | | - let tribute = new Tribute({ |
4 | | - values: function (search, cb) { |
5 | | - getUsernames(search, (users) => cb(users)); |
6 | | - }, |
7 | | - lookup: function (users, mentionText) { |
8 | | - if (users.key.includes(mentionText)) { |
9 | | - return users.key + " (" + users.name + ")"; |
10 | | - } else if (users.name.includes(mentionText)) { |
11 | | - return users.name + " (" + users.key + ")"; |
12 | | - } else if (users.user_nicename.includes(mentionText)) { |
13 | | - return users.user_nicename + " (" + users.name + ")"; |
| 1 | +jQuery( |
| 2 | + function ($) { |
| 3 | + const mentionMap = {}; |
| 4 | + let tribute = new Tribute( |
| 5 | + { |
| 6 | + values: function (search, cb) { |
| 7 | + let data = { |
| 8 | + action: "cmt_mntn_get_users", |
| 9 | + term: search, |
| 10 | + }; |
| 11 | + |
| 12 | + $.ajax( |
| 13 | + { |
| 14 | + url: Comment_Mention.ajaxurl, // Replace with your API endpoint |
| 15 | + method: 'GET', |
| 16 | + data: data, // Send current text as the search term |
| 17 | + dataType: 'json', |
| 18 | + success: function(response) { |
| 19 | + // Format the response for Tribute.js |
| 20 | + // const results = response.data; |
| 21 | + const results = response.data.map( |
| 22 | + function(item) { |
| 23 | + mentionMap[item.key] = item.name; |
| 24 | + return item; |
| 25 | + } |
| 26 | + ); |
| 27 | + cb( results ); // Pass formatted results to Tribute.js |
| 28 | + }, |
| 29 | + error: function(xhr, status, error) { |
| 30 | + console.error( "Error fetching data:", error ); |
| 31 | + cb( [] ); // Pass empty array on error |
| 32 | + } |
| 33 | + } |
| 34 | + ); |
| 35 | + }, |
| 36 | + lookup: function (users, mentionText) { |
| 37 | + if (users.key.includes( mentionText )) { |
| 38 | + return users.key + " (" + users.name + ")"; |
| 39 | + } else if (users.name.includes( mentionText )) { |
| 40 | + return users.name + " (" + users.key + ")"; |
| 41 | + } else if (users.user_nicename.includes( mentionText )) { |
| 42 | + return users.user_nicename + " (" + users.name + ")"; |
| 43 | + } |
| 44 | + }, |
| 45 | + selectTemplate: function(item) { |
| 46 | + if ( 'undefined' !== typeof( Comment_Mention.mention_by_fullname ) && '1' === Comment_Mention.mention_by_fullname ) { |
| 47 | + return '@' + item.original.name; // Use `name` in textarea1 |
| 48 | + } else { |
| 49 | + return '@' + item.original.key; // Use `name` in textarea1 |
| 50 | + } |
| 51 | + } |
14 | 52 | } |
15 | | - }, |
16 | | - }); |
| 53 | + ); |
| 54 | + |
| 55 | + tribute.attach( $( "#commentform textarea" ) ); |
| 56 | + tribute.attach( $( ".bbp-topic-form form textarea" ) ); |
| 57 | + tribute.attach( $( ".bbp-reply-form form textarea" ) ); |
17 | 58 |
|
18 | | - tribute.attach($("#commentform textarea")); |
19 | | - tribute.attach($(".bbp-topic-form form textarea")); |
20 | | - tribute.attach($(".bbp-reply-form form textarea")); |
| 59 | + if ( $( '#main_comment' ).length > 0 ) { |
| 60 | + $( '#comment' ).on( 'tribute-replaced', cmt_mntn_sync_usernames ); |
| 61 | + $( '#comment' ).on( 'input', cmt_mntn_sync_usernames ); |
| 62 | + } |
21 | 63 |
|
22 | | - function getUsernames(search, cb) { |
23 | | - let data = { |
24 | | - action: "cmt_mntn_get_users", |
25 | | - term: search, |
26 | | - }; |
| 64 | + function cmt_mntn_sync_usernames() { |
27 | 65 |
|
28 | | - getUsers = $.ajax({ |
29 | | - url: Comment_Mention.ajaxurl, |
30 | | - data: data, |
31 | | - method: "GET", |
32 | | - beforeSend: function () { |
33 | | - if (getUsers != null) { |
34 | | - getUsers.abort(); |
| 66 | + let content = $( '#comment' ).val(); |
| 67 | + |
| 68 | + Object.keys( mentionMap ).forEach( |
| 69 | + name => { |
| 70 | + const nameMention = '@' + mentionMap[name]; // Mention format in textarea1 (name) |
| 71 | + const usernameMention = '@' + name; // Mention format in textarea2 (username) |
| 72 | + content = content.split( nameMention ).join( usernameMention ); |
35 | 73 | } |
36 | | - }, |
37 | | - success: function (response) { |
38 | | - var usernames = response.data; |
39 | | - cb(usernames); |
40 | | - }, |
41 | | - }); |
| 74 | + ); |
| 75 | + |
| 76 | + $( '#main_comment' ).val( content ); |
| 77 | + } |
42 | 78 | } |
43 | | -}); |
| 79 | +); |
0 commit comments