thedesk/app/js/post/emoji.js

357 lines
9.2 KiB
JavaScript
Raw Normal View History

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