-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from BhargavBhandari90/mention-by-name
Add filter and script for pro plugin
- Loading branch information
Showing
5 changed files
with
93 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,79 @@ | ||
jQuery(function ($) { | ||
let getUsers; | ||
let tribute = new Tribute({ | ||
values: function (search, cb) { | ||
getUsernames(search, (users) => cb(users)); | ||
}, | ||
lookup: function (users, mentionText) { | ||
if (users.key.includes(mentionText)) { | ||
return users.key + " (" + users.name + ")"; | ||
} else if (users.name.includes(mentionText)) { | ||
return users.name + " (" + users.key + ")"; | ||
} else if (users.user_nicename.includes(mentionText)) { | ||
return users.user_nicename + " (" + users.name + ")"; | ||
jQuery( | ||
function ($) { | ||
const mentionMap = {}; | ||
let tribute = new Tribute( | ||
{ | ||
values: function (search, cb) { | ||
let data = { | ||
action: "cmt_mntn_get_users", | ||
term: search, | ||
}; | ||
|
||
$.ajax( | ||
{ | ||
url: Comment_Mention.ajaxurl, // Replace with your API endpoint | ||
method: 'GET', | ||
data: data, // Send current text as the search term | ||
dataType: 'json', | ||
success: function(response) { | ||
// Format the response for Tribute.js | ||
// const results = response.data; | ||
const results = response.data.map( | ||
function(item) { | ||
mentionMap[item.key] = item.name; | ||
return item; | ||
} | ||
); | ||
cb( results ); // Pass formatted results to Tribute.js | ||
}, | ||
error: function(xhr, status, error) { | ||
console.error( "Error fetching data:", error ); | ||
cb( [] ); // Pass empty array on error | ||
} | ||
} | ||
); | ||
}, | ||
lookup: function (users, mentionText) { | ||
if (users.key.includes( mentionText )) { | ||
return users.key + " (" + users.name + ")"; | ||
} else if (users.name.includes( mentionText )) { | ||
return users.name + " (" + users.key + ")"; | ||
} else if (users.user_nicename.includes( mentionText )) { | ||
return users.user_nicename + " (" + users.name + ")"; | ||
} | ||
}, | ||
selectTemplate: function(item) { | ||
if ( 'undefined' !== typeof( Comment_Mention.mention_by_fullname ) && '1' === Comment_Mention.mention_by_fullname ) { | ||
return '@' + item.original.name; // Use `name` in textarea1 | ||
} else { | ||
return '@' + item.original.key; // Use `name` in textarea1 | ||
} | ||
} | ||
} | ||
}, | ||
}); | ||
); | ||
|
||
tribute.attach( $( "#commentform textarea" ) ); | ||
tribute.attach( $( ".bbp-topic-form form textarea" ) ); | ||
tribute.attach( $( ".bbp-reply-form form textarea" ) ); | ||
|
||
tribute.attach($("#commentform textarea")); | ||
tribute.attach($(".bbp-topic-form form textarea")); | ||
tribute.attach($(".bbp-reply-form form textarea")); | ||
if ( $( '#main_comment' ).length > 0 ) { | ||
$( '#comment' ).on( 'tribute-replaced', cmt_mntn_sync_usernames ); | ||
$( '#comment' ).on( 'input', cmt_mntn_sync_usernames ); | ||
} | ||
|
||
function getUsernames(search, cb) { | ||
let data = { | ||
action: "cmt_mntn_get_users", | ||
term: search, | ||
}; | ||
function cmt_mntn_sync_usernames() { | ||
|
||
getUsers = $.ajax({ | ||
url: Comment_Mention.ajaxurl, | ||
data: data, | ||
method: "GET", | ||
beforeSend: function () { | ||
if (getUsers != null) { | ||
getUsers.abort(); | ||
let content = $( '#comment' ).val(); | ||
|
||
Object.keys( mentionMap ).forEach( | ||
name => { | ||
const nameMention = '@' + mentionMap[name]; // Mention format in textarea1 (name) | ||
const usernameMention = '@' + name; // Mention format in textarea2 (username) | ||
content = content.split( nameMention ).join( usernameMention ); | ||
} | ||
}, | ||
success: function (response) { | ||
var usernames = response.data; | ||
cb(usernames); | ||
}, | ||
}); | ||
); | ||
|
||
$( '#main_comment' ).val( content ); | ||
} | ||
} | ||
}); | ||
); |