thedesk/app/js/post/suggest.js

180 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//入力時にハッシュタグと@をサジェスト
var timer = null;
var input = document.getElementById("textarea");
var prev_val = input.value;
var oldSuggest;
var suggest;
2018-04-09 00:17:33 +10:00
2018-01-28 23:22:43 +11:00
input.addEventListener("focus", function() {
2018-04-09 00:17:33 +10:00
var acct_id = $("#post-acct-sel").val();
2018-01-28 23:22:43 +11:00
$("#suggest").html("");
window.clearInterval(timer);
timer = window.setInterval(function() {
var new_val = input.value;
2019-03-13 02:51:07 +11:00
if(new_val==""){
$("#suggest").html("");
if($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")){
$("#right-side").hide()
}
return;
}
2018-01-28 23:22:43 +11:00
if (prev_val != new_val) {
2018-04-09 00:17:33 +10:00
var semoji = new_val.match(/:(\S{3,})/);
if(semoji){
var obj = JSON.parse(localStorage.getItem("emoji_" + acct_id));
2018-08-11 01:58:00 +10:00
if(!obj){
2019-01-26 14:24:26 +11:00
var ehtml=lang.lang_suggest_nodata;
2018-08-11 01:58:00 +10:00
}else{
var num = obj.length;
var ehtml="";
for (i = 0; i < num; i++) {
var emoji = obj[i];
if ( ~emoji.shortcode.indexOf(semoji[1])) {
if (emoji) {
ehtml = ehtml+'<a onclick="emojiInsert(\':' + emoji.shortcode +
': \',\':'+semoji[1]+'\')" class="pointer"><img src="' + emoji.url + '" width="20"></a>';
}
2018-04-09 00:17:33 +10:00
}
2018-08-11 01:58:00 +10:00
}
}
2019-03-13 02:51:07 +11:00
if(ehtml!=""){
$("#right-side").show()
$("#poll").addClass("hide")
$("#emoji").addClass("hide")
}else{
if($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")){
$("#right-side").hide()
}
}
2018-04-09 00:17:33 +10:00
$("#suggest").html(ehtml);
}
2018-01-28 23:22:43 +11:00
var tag = new_val.match(/#(\S{3,})/);
var acct = new_val.match(/@(\S{3,})/);
2019-04-11 02:52:01 +10:00
if (tag && tag[1]) {
2018-01-28 23:22:43 +11:00
var q = tag[1];
2018-03-14 05:31:31 +11:00
} else if (acct && acct[1]) {
2018-01-28 23:22:43 +11:00
var q = acct[1];
2018-07-22 23:03:46 +10:00
}else {
2019-03-13 02:51:07 +11:00
$("#suggest").html("");
if($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")){
$("#right-side").hide()
}
2018-01-28 23:22:43 +11:00
return;
}
var domain = localStorage.getItem("domain_" + acct_id);
2018-07-07 03:51:48 +10:00
var at = localStorage.getItem("acct_"+ acct_id + "_at");
2018-01-28 23:22:43 +11:00
suggest = "https://" + domain + "/api/v1/search?q=" + q
if (suggest != oldSuggest) {
console.log(suggest)
fetch(suggest, {
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + at
},
}).then(function(response) {
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
2019-03-13 02:51:07 +11:00
if (json.hashtags[0] && tag) {
if(tag[1]){
2018-01-28 23:22:43 +11:00
var tags = "";
Object.keys(json.hashtags).forEach(function(key4) {
var tag = json.hashtags[key4];
2019-03-13 02:51:07 +11:00
if(tag!=q){
tags = tags + '<a onclick="tagInsert(\'#' + tag + '\',\'#' + q +
'\')" class="pointer">#' + tag + '</a><br>';
}
2018-01-28 23:22:43 +11:00
});
2019-03-13 02:51:07 +11:00
$("#right-side").show()
$("#suggest").html("Tags:<br>" + tags);
$("#poll").addClass("hide")
$("#emoji").addClass("hide")
}
2018-01-28 23:22:43 +11:00
} else if (json.accounts[0] && acct[1]) {
var accts = "";
Object.keys(json.accounts).forEach(function(key3) {
var acct = json.accounts[key3];
2019-03-13 02:51:07 +11:00
if(acct.acct!=q){
accts = accts + '<a onclick="tagInsert(\'@' + acct.acct +
'\',\'@' + q + '\')" class="pointer">@' + acct.acct + '</a><br>';
}
2018-01-28 23:22:43 +11:00
});
2019-03-13 02:51:07 +11:00
$("#right-side").show()
2019-03-21 15:39:30 +11:00
$("#suggest").html(accts);
2019-03-13 02:51:07 +11:00
$("#poll").addClass("hide")
$("#emoji").addClass("hide")
2018-01-28 23:22:43 +11:00
} else {
2019-03-13 02:51:07 +11:00
if($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")){
$("#right-side").hide()
}
2018-01-28 23:22:43 +11:00
}
});
}
};
oldSuggest = suggest;
prev_value = new_val;
}, 1000);
}, false);
input.addEventListener("blur", function() {
window.clearInterval(timer);
2018-03-11 01:22:59 +11:00
favTag();
2018-01-28 23:22:43 +11:00
}, false);
2018-03-14 05:31:31 +11:00
function tagInsert(code, del) {
var now = $("#textarea").val();
var selin = $("#textarea").prop('selectionStart');
if (!del) {
} else {
var regExp = new RegExp(del, "g");
var now = now.replace(regExp, "");
selin=selin-del.length;
}
if(selin>0){
var before = now.substr(0, selin);
var after = now.substr(selin, now.length);
newt = before + " "+ code+" " + after;
}else{
newt = code+" "+now;
}
$("#textarea").val(newt);
$("#textarea").focus();
2019-03-13 02:51:07 +11:00
if($("#poll").hasClass("hide") && $("#emoji").hasClass("hide")){
$("#right-side").hide()
}
2018-03-14 05:31:31 +11:00
$("#suggest").html("");
2018-07-22 23:03:46 +10:00
}
function cgNPs(q){
suggest = "https://cg.toot.app/api/v1/search/light?q=" + q
if (suggest != oldSuggest) {
console.log(suggest)
fetch(suggest, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
}).then(function(response) {
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
console.log(json);
if (json[0]) {
var tags = "";
Object.keys(json).forEach(function(key4) {
var tag = json[key4];
tags = tags + '<a onclick="cgNP(\''+json[key4]+'\')" class="pointer">' + json[key4] + '</a> ';
});
$("#suggest").html("Cinderella NowPlaying:" + tags);
}else{
$("#suggest").html("Cinderella NowPlaying:Not Found");
}
});
}
2018-03-14 05:31:31 +11:00
}