thedesk/app/js/post/emoji.js

314 lines
8.3 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//絵文字ピッカー
//最初に読み込む
$("#emoji-before").addClass("disabled");
$("#emoji-next").addClass("disabled");
//絵文字ボタンのトグル
2019-04-11 02:52:01 +10:00
function emojiToggle(reaction) {
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');
2019-04-11 13:52:38 +10:00
if (!selin) {
selin = 0;
2018-03-14 17:52:55 +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-09-01 00:35:18 +10:00
$("#right-side").css("width", "300px")
2019-09-01 22:27:18 +10:00
$("#left-side").css("width", "calc(100% - 300px)")
var width = localStorage.getItem("postbox-width");
if (width) {
width = width.replace("px", "") * 1 + 300
} else {
width = 600
}
$("#post-box").css("width", width + "px")
2019-03-13 02:51:07 +11:00
$("#suggest").html("");
2019-10-03 02:38:55 +10:00
if (!localStorage.getItem("emojis_" + acct_id)) {
2018-01-28 23:22:43 +11:00
var html =
2019-04-11 13:52:38 +10: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 {
2019-04-11 13:52:38 +10:00
emojiList('home', reaction);
2018-01-28 23:22:43 +11:00
}
} else {
2019-03-06 19:08:48 +11:00
$("#poll").addClass("hide")
2019-09-01 00:35:18 +10:00
$("#right-side").hide()
$("#right-side").css("width", "300px")
2018-01-28 23:22:43 +11:00
$("#emoji").addClass("hide")
2019-03-13 02:51:07 +11:00
$("#suggest").html("");
2019-09-01 22:27:18 +10:00
$("#left-side").css("width", "100%")
var width = localStorage.getItem("postbox-width");
if (width) {
width = width.replace("px", "") * 1
} else {
width = 300
}
$("#post-box").css("width", width + "px")
2018-01-28 23:22:43 +11:00
}
}
//絵文字リスト挿入
2019-04-11 13:52:38 +10:00
function emojiGet(parse, started) {
2019-04-23 00:16:57 +10:00
$('#emoji-list').text('Loading...');
2018-01-28 23:22:43 +11:00
var acct_id = $("#post-acct-sel").val();
var domain = localStorage.getItem("domain_" + acct_id);
2019-04-11 13:52:38 +10:00
if (localStorage.getItem("mode_" + domain) != "misskey") {
var start = "https://" + domain + "/api/v1/custom_emojis";
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
}).then(function (response) {
2019-10-31 02:30:26 +11:00
if (!response.ok) {
response.text().then(function(text) {
setLog(response.url, response.status, text);
});
}
return response.json();
2019-04-11 13:52:38 +10:00
}).catch(function (error) {
todo(error);
console.error(error);
}).then(function (json) {
if (parse == "true") {
2019-04-23 00:16:57 +10:00
$('#emoji-list').text('Parsing...');
2019-10-03 02:38:55 +10:00
var md = {
"categorized": {},
"uncategorized": []
};
var if_categorized = false
Object.keys(json).forEach(function (key) {
var emoji = json[key];
if (emoji.visible_in_picker) {
var listed = true
} else {
var listed = false
}
if (emoji.category) {
var cat = emoji.category
if (!md["categorized"][cat]) {
md["categorized"][cat] = []
}
md["categorized"][cat].push({
"shortcode": emoji.shortcode,
"url": emoji.url,
"listed": listed
})
if_categorized = true
} else {
md["uncategorized"].push({
"shortcode": emoji.shortcode,
"url": emoji.url,
"listed": listed
})
}
});
console.log(md)
2019-04-11 13:52:38 +10:00
//絵文字をマストドン公式と同順にソート
2019-10-03 02:38:55 +10:00
md["uncategorized"].sort(function (a, b) {
2019-04-11 13:52:38 +10:00
if (a.shortcode < b.shortcode) return -1;
if (a.shortcode > b.shortcode) return 1;
return 0;
});
2019-10-05 03:40:25 +10:00
Object.keys(md["categorized"]).forEach(function (key) {
md["categorized"][key].sort(function (a, b) {
if (a.shortcode < b.shortcode) return -1;
if (a.shortcode > b.shortcode) return 1;
return 0;
});
});
2019-10-03 02:38:55 +10:00
md["if_categorized"] = if_categorized
localStorage.setItem("emojis_" + acct_id, JSON.stringify(md));
2019-04-11 13:52:38 +10:00
} else {
2019-10-03 02:38:55 +10:00
localStorage.setItem("emojis_" + acct_id, JSON.stringify(md));
2019-04-11 13:52:38 +10:00
}
localStorage.setItem("emojiseek", 0);
if (!started) {
emojiList('home')
}
2019-04-11 02:52:01 +10:00
});
2019-04-11 13:52:38 +10:00
} else {
var start = "https://" + domain + "/api/meta";
fetch(start, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
}).then(function (response) {
2019-10-31 02:30:26 +11:00
if (!response.ok) {
response.text().then(function(text) {
setLog(response.url, response.status, text);
});
}
return response.json();
2019-04-11 13:52:38 +10:00
}).catch(function (error) {
todo(error);
console.error(error);
}).then(function (json) {
if (json.enableEmojiReaction) {
localStorage.setItem("emojiReaction_" + acct_id, "true");
} else {
localStorage.setItem("emojiReaction_" + acct_id, "disabled");
}
var emojis = json.emojis;
2019-10-03 02:38:55 +10:00
var md = {"uncategorized": []}
2019-04-11 13:52:38 +10:00
Object.keys(emojis).forEach(function (key) {
var emoji = emojis[key];
2019-10-03 02:38:55 +10:00
md["uncategorized"].push({
2019-04-11 13:52:38 +10:00
"shortcode": emoji.name,
2019-10-03 02:38:55 +10:00
"url": emoji.url,
"listed": true
2019-04-11 13:52:38 +10:00
})
2019-04-11 02:52:01 +10:00
});
2019-10-03 02:38:55 +10:00
md["if_categorized"] = false
2019-04-11 13:52:38 +10:00
if (parse == "true") {
2019-04-23 00:16:57 +10:00
$('#emoji-list').text('Parsing...');
2019-04-11 13:52:38 +10:00
//絵文字をマストドン公式と同順にソート
2019-10-03 02:38:55 +10:00
md["uncategorized"].sort(function (a, b) {
2019-04-11 13:52:38 +10:00
if (a.shortcode < b.shortcode) return -1;
if (a.shortcode > b.shortcode) return 1;
return 0;
});
2019-10-03 02:38:55 +10:00
localStorage.setItem("emojis_" + acct_id, JSON.stringify(md));
2019-04-11 13:52:38 +10:00
} else {
2019-10-03 02:38:55 +10:00
localStorage.setItem("emojis_" + acct_id, JSON.stringify(md));
2019-04-11 13:52:38 +10:00
}
localStorage.setItem("emojiseek", 0);
if (!started) {
emojiList('home')
}
});
2019-04-11 02:52:01 +10:00
}
2018-01-28 23:22:43 +11:00
}
//リストの描画
2019-04-11 13:52:38 +10:00
function emojiList(target, reaction) {
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();
2019-04-11 13:52:38 +10:00
if (reaction && localStorage.getItem("emojiReaction_" + acct_id) != "true") {
2019-04-11 02:52:01 +10:00
console.error("Disabled")
clear()
hide();
return false;
}
2018-01-28 23:22:43 +11:00
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 = '';
2019-10-03 02:38:55 +10:00
var raw = JSON.parse(localStorage.getItem("emojis_" + acct_id));
console.log(raw)
if (raw.if_categorized) {
var obj = [{
"divider": true,
"cat": lang.lang_emoji_uncat
}]
var cats = raw["uncategorized"]
obj = obj.concat(cats);
Object.keys(raw["categorized"]).forEach(function (key) {
var cats = raw["categorized"][key];
obj = obj.concat([{
"divider": true,
"cat": key
}]);
obj = obj.concat(cats);
});
} else {
var obj = raw["uncategorized"]
}
console.log(obj)
2018-01-28 23:22:43 +11:00
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) {
2019-10-03 02:38:55 +10:00
if(num > 0){
var ct = 1;
}
2018-01-28 23:22:43 +11:00
$("#emoji-before").addClass("disabled");
} else {
$("#emoji-before").removeClass("disabled");
}
2019-10-03 02:38:55 +10:00
if (page != 1) {
$("#emoji-next").removeClass("disabled");
} else {
$("#emoji-next").addClass("disabled");
}
2018-01-28 23:22:43 +11:00
$("#emoji-count").text(ct);
for (i = start; i < start + 126; i++) {
var emoji = obj[i];
if (emoji) {
2019-04-11 13:52:38 +10:00
if (reaction) {
2019-04-11 02:52:01 +10:00
html = html + '<a onclick="emojiReaction(\':' + emoji.shortcode +
2019-10-03 02:38:55 +10:00
':\')" class="pointer"><img src="' + emoji.url + '" width="20" title="' + emoji.shortcode + '"></a>';
2019-04-11 13:52:38 +10:00
} else {
2019-10-03 02:38:55 +10:00
if(emoji.divider){
html = html + '<p style="margin-bottom:0">'+ emoji.cat +'</p>'
}else{
if (emoji.listed) {
html = html + '<a onclick="emojiInsert(\':' + emoji.shortcode +
':\')" class="pointer"><img src="' + emoji.url + '" width="20" title="' + emoji.shortcode + '"></a>';
}
}
2019-04-11 02:52:01 +10:00
}
2018-01-28 23:22:43 +11:00
}
}
$("#emoji-list").html(html);
}
//絵文字など様々なものをテキストボックスに挿入
function emojiInsert(code, del) {
var now = $("#textarea").val();
2019-08-20 00:09:34 +10:00
var selin = $("#textarea").prop('selectionStart');
2019-04-11 13:52:38 +10:00
if (localStorage.getItem("emoji-zero-width") == "yes") {
2019-08-20 00:09:34 +10:00
var blank = "";
2019-04-11 13:52:38 +10:00
} else {
2019-08-20 00:09:34 +10:00
var blank = " ";
2019-04-11 03:59:13 +10:00
}
2019-08-20 00:09:34 +10:00
var before = now.substr(0, selin);
if (before.slice(-1) != " ") {
before = before + blank
}
var after = now.substr(selin, now.length);
if (after.slice(-1) != " ") {
after = blank + after
2018-03-14 05:31:31 +11:00
}
2019-08-20 00:09:34 +10:00
newt = before + code + after;
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 {
2019-06-16 22:52:41 +10:00
var regExp = new RegExp(del.replace(/[.*+?^=!:${}()|[\]\/\\]/g, "\\$&"), "g");
2018-02-09 03:43:11 +11:00
var now = now.replace(regExp, "");
2019-08-20 00:09:34 +10:00
$("#textarea").val(now + blank + code);
2018-01-28 23:22:43 +11:00
}
2019-04-11 13:52:38 +10:00
2018-01-28 23:22:43 +11:00
$("#textarea").focus();
}
2018-02-05 01:56:31 +11:00
//改行挿入
function brInsert(code) {
2019-04-11 13:52:38 +10:00
if (!$('#post-box').hasClass("appear")) {
localStorage.setItem("nohide", true);
2018-07-28 07:25:12 +10:00
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
}