thedesk/app/js/post/emoji.js

152 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//絵文字ピッカー
//最初に読み込む
$("#emoji-before").addClass("disabled");
$("#emoji-next").addClass("disabled");
//絵文字ボタンのトグル
2018-09-20 01:10:53 +10:00
function emojiToggle() {
2018-01-28 23:22:43 +11:00
var acct_id = $("#post-acct-sel").val();
2018-03-14 05:31:31 +11:00
var selin = $("#textarea").prop('selectionStart');
2018-03-14 17:52:55 +11:00
if(!selin){
selin=0;
}
2018-03-14 05:31:31 +11:00
localStorage.setItem("cursor", selin);
2019-03-06 19:08:48 +11:00
2018-01-28 23:22:43 +11:00
if ($("#emoji").hasClass("hide")) {
$("#emoji").removeClass("hide")
2019-03-06 19:08:48 +11:00
$("#right-side").show()
2019-03-13 02:51:07 +11:00
$("#suggest").html("");
2018-01-28 23:22:43 +11:00
if (!localStorage.getItem("emoji_" + acct_id)) {
var html =
2019-01-26 14:24:26 +11:00
'<button class="btn waves-effect green" style="width:100%; padding:0; margin-top:0;" onclick="emojiGet(\'true\');">'+lang.lang_emoji_get+'</button>';
2018-01-28 23:22:43 +11:00
$("#emoji-list").html(html);
} else {
emojiList('home');
}
} else {
2019-03-06 19:08:48 +11:00
$("#poll").addClass("hide")
2018-01-28 23:22:43 +11:00
$("#emoji").addClass("hide")
2019-03-13 02:51:07 +11:00
$("#suggest").html("");
2019-03-06 19:08:48 +11:00
$("#right-side").hide()
2018-01-28 23:22:43 +11:00
}
}
//絵文字リスト挿入
function emojiGet(parse) {
$('#emoji-list').html('Loading...');
var acct_id = $("#post-acct-sel").val();
var domain = localStorage.getItem("domain_" + acct_id);
var start = "https://" + domain + "/api/v1/custom_emojis";
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
}).then(function(response) {
return response.json();
}).catch(function(error) {
todo(error);
console.error(error);
}).then(function(json) {
if (parse == "true") {
$('#emoji-list').html('Parsing...');
//絵文字をマストドン公式と同順にソート
json.sort(function(a, b) {
if (a.shortcode < b.shortcode) return -1;
if (a.shortcode > b.shortcode) return 1;
return 0;
});
localStorage.setItem("emoji_" + acct_id, JSON.stringify(json));
} else {
localStorage.setItem("emoji_" + acct_id, JSON.stringify(json));
}
localStorage.setItem("emojiseek", 0);
emojiList('home')
});
}
//リストの描画
function emojiList(target) {
2019-01-26 14:24:26 +11:00
$("#now-emoji").text(lang.lang_emoji_custom);
2018-01-28 23:22:43 +11:00
var acct_id = $("#post-acct-sel").val();
var start = localStorage.getItem("emojiseek");
if (target == "next") {
var start = start * 1 + 127;
localStorage.setItem("emojiseek", start);
} else if (target == "before") {
var start = start - 127;
localStorage.setItem("emojiseek", start);
} else {
var start = 0;
localStorage.getItem("emojiseek", 0)
}
var html = '';
var obj = JSON.parse(localStorage.getItem("emoji_" + acct_id));
var num = obj.length;
if (num < start) {
var start = 0;
localStorage.setItem("emojiseek", start);
}
var page = Math.ceil(num / 126);
$("#emoji-sum").text(page);
var ct = Math.ceil(start / 126);
2019-03-08 05:19:26 +11:00
if (ct === 0) {
2018-01-28 23:22:43 +11:00
var ct = 1;
$("#emoji-before").addClass("disabled");
} else {
$("#emoji-before").removeClass("disabled");
}
$("#emoji-next").removeClass("disabled");
$("#emoji-count").text(ct);
for (i = start; i < start + 126; i++) {
var emoji = obj[i];
if (emoji) {
html = html + '<a onclick="emojiInsert(\':' + emoji.shortcode +
2018-02-13 06:16:46 +11:00
': \')" class="pointer"><img src="' + emoji.url + '" width="20"></a>';
2018-01-28 23:22:43 +11:00
}
}
$("#emoji-list").html(html);
}
//絵文字など様々なものをテキストボックスに挿入
function emojiInsert(code, del) {
var now = $("#textarea").val();
2018-03-14 05:31:31 +11:00
var selin = localStorage.getItem("cursor");
console.log(selin);
var now = $("#textarea").val();
if(selin>0){
var before = now.substr(0, selin);
var after = now.substr(selin, now.length);
newt = before + " "+ code+" " + after;
}else{
newt = code+" ";
}
2018-01-28 23:22:43 +11:00
if (!del) {
2018-03-14 05:31:31 +11:00
$("#textarea").val(newt);
2018-04-09 00:17:33 +10:00
//emoji();
2018-01-28 23:22:43 +11:00
} else {
var regExp = new RegExp(del, "g");
2018-02-09 03:43:11 +11:00
var now = now.replace(regExp, "");
2018-01-28 23:22:43 +11:00
$("#textarea").val(now + " " + code);
}
2018-05-12 04:12:25 +10:00
2018-01-28 23:22:43 +11:00
$("#textarea").focus();
2018-05-12 04:12:25 +10:00
var selin = $("#textarea").prop('selectionStart');
if(!selin){
selin=0;
}
localStorage.setItem("cursor", selin);
2018-01-28 23:22:43 +11:00
}
2018-02-05 01:56:31 +11:00
//改行挿入
function brInsert(code) {
2018-07-28 07:25:12 +10:00
if(!$('#post-box').hasClass("appear")){
localStorage.setItem("nohide",true);
show();
}
2018-02-05 01:56:31 +11:00
var now = $("#textarea").val();
$("#textarea").val(now + code);
$("#textarea").focus();
2018-03-20 15:55:25 +11:00
}