thedesk/app/js/post/suggest.js

263 lines
7.4 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//入力時にハッシュタグと@をサジェスト
2019-11-09 00:52:54 +11:00
var timer = null
2018-01-28 23:22:43 +11:00
2019-11-09 00:52:54 +11:00
var input = document.getElementById('textarea')
2018-01-28 23:22:43 +11:00
2019-11-09 00:52:54 +11:00
var prev_val = input.value
var oldSuggest
var suggest
2018-04-09 00:17:33 +10:00
2019-11-04 03:09:43 +11:00
input.addEventListener(
2019-11-09 00:52:54 +11:00
'focus',
2019-11-04 03:09:43 +11:00
function() {
2019-11-09 00:52:54 +11:00
localStorage.removeItem('cursor')
var acct_id = $('#post-acct-sel').val()
$('#suggest').html('')
window.clearInterval(timer)
2019-11-04 03:09:43 +11:00
timer = window.setInterval(function() {
2019-11-09 00:52:54 +11:00
var new_val = input.value
if (new_val == '') {
$('#suggest').html('')
if ($('#poll').hasClass('hide') && $('#emoji').hasClass('hide')) {
$('#right-side').hide()
$('#right-side').css('width', '300px')
$('#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 00:35:18 +10:00
}
2019-11-09 00:52:54 +11:00
$('#post-box').css('width', width + 'px')
2019-11-04 03:09:43 +11:00
}
2019-11-09 00:52:54 +11:00
return
2019-11-04 03:09:43 +11:00
}
if (prev_val != new_val) {
2019-11-09 00:52:54 +11:00
var tag = new_val.match(/#(\S{3,})/)
var acct = new_val.match(/@(\S{3,})/)
2019-11-04 03:09:43 +11:00
if (tag && tag[1]) {
2019-11-09 00:52:54 +11:00
var q = tag[1]
2019-11-04 03:09:43 +11:00
} else if (acct && acct[1]) {
2019-11-09 00:52:54 +11:00
var q = acct[1]
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
$('#suggest').html('')
if ($('#poll').hasClass('hide') && $('#emoji').hasClass('hide')) {
$('#right-side').hide()
$('#right-side').css('width', '300px')
$('#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 00:35:18 +10:00
}
2019-11-09 00:52:54 +11:00
$('#post-box').css('width', width + 'px')
2019-09-01 00:35:18 +10:00
}
2019-11-09 00:52:54 +11:00
return
2019-03-13 02:51:07 +11:00
}
2019-11-09 00:52:54 +11:00
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
suggest = 'https://' + domain + '/api/v2/search?q=' + q
2019-11-04 03:09:43 +11:00
if (suggest != oldSuggest) {
2019-11-09 00:52:54 +11:00
console.log('Try to get suggest at ' + suggest)
2019-11-04 03:09:43 +11:00
fetch(suggest, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2019-11-04 03:09:43 +11:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json',
Authorization: 'Bearer ' + at
2019-11-04 03:09:43 +11:00
}
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:09:43 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:09:43 +11: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:09:43 +11:00
})
.then(function(json) {
2019-11-09 00:52:54 +11:00
console.log(['Search', json])
2019-11-04 03:09:43 +11:00
//ハッシュタグ
if (json.hashtags[0] && tag) {
if (tag[1]) {
2019-11-09 00:52:54 +11:00
var tags = []
2019-11-04 03:09:43 +11:00
Object.keys(json.hashtags).forEach(function(key4) {
2019-11-09 00:52:54 +11:00
var tag = json.hashtags[key4]
var his = tag.history
var uses =
his[0].uses * 1 +
his[1].uses * 1 +
his[2].uses * 1 +
his[3].uses * 1 +
his[4].uses * 1 +
his[5].uses * 1 +
his[6].uses * 1
tagHTML = `<br><a onclick="tagInsert('#${escapeHTML(tag.name)}','#${escapeHTML(tag.name)}')" class="pointer">#${escapeHTML(tag.name)}</a>&nbsp;${uses}toot(s)`
2019-11-04 03:09:43 +11:00
var item = {
uses: uses,
html: tagHTML
2019-11-09 00:52:54 +11:00
}
tags.push(item)
})
var num_a = -1
var num_b = 1
2019-11-04 03:09:43 +11:00
tags = tags.sort(function(a, b) {
2019-11-09 00:52:54 +11:00
var x = a['uses']
var y = b['uses']
if (x > y) return num_a
if (x < y) return num_b
return 0
})
var ins = ''
var nev = false
2019-11-04 03:09:43 +11:00
Object.keys(tags).forEach(function(key7) {
2019-11-09 00:52:54 +11:00
ins = ins + tags[key7].html
2019-11-04 03:09:43 +11:00
if (key7 <= 0 && !nev) {
2019-11-09 00:52:54 +11:00
ins = ins + '<br>'
nev = true
2019-11-04 03:09:43 +11:00
}
2019-11-09 00:52:54 +11:00
})
$('#suggest').html(ins)
$('#right-side').show()
$('#right-side').css('width', '200px')
$('#left-side').css('width', 'calc(100% - 200px)')
var width = localStorage.getItem('postbox-width')
2019-11-04 03:09:43 +11:00
if (width) {
2019-11-09 00:52:54 +11:00
width = width.replace('px', '') * 1 + 200
2019-11-04 03:09:43 +11:00
} else {
2019-11-09 00:52:54 +11:00
width = 500
2019-11-04 03:09:43 +11:00
}
2019-11-09 00:52:54 +11:00
$('#post-box').css('width', width + 'px')
$('#poll').addClass('hide')
$('#emoji').addClass('hide')
2019-07-29 01:42:17 +10:00
}
2019-11-04 03:09:43 +11:00
} else if (json.accounts[0] && acct[1]) {
2019-11-09 00:52:54 +11:00
var accts = ''
2019-11-04 03:09:43 +11:00
Object.keys(json.accounts).forEach(function(key3) {
2019-11-09 00:52:54 +11:00
var acct = json.accounts[key3]
2019-11-04 03:09:43 +11:00
if (acct.acct != q) {
//Instance Actorって…
2019-11-09 00:52:54 +11:00
if (acct.username.indexOf('.') < 0) {
accts = accts + `<a onclick="tagInsert('@${acct.acct}','@${q}')" class="pointer">@${acct.acct}</a><br>`
2019-11-04 03:09:43 +11:00
}
}
2019-11-09 00:52:54 +11:00
})
$('#right-side').show()
$('#right-side').css('width', '200px')
$('#left-side').css('width', 'calc(100% - 200px)')
var width = localStorage.getItem('postbox-width')
2019-11-04 03:09:43 +11:00
if (width) {
2019-11-09 00:52:54 +11:00
width = width.replace('px', '') * 1 + 200
2019-11-04 03:09:43 +11:00
} else {
2019-11-09 00:52:54 +11:00
width = 500
2019-07-29 01:52:12 +10:00
}
2019-11-09 00:52:54 +11:00
$('#post-box').css('width', width + 'px')
$('#suggest').html(accts)
$('#poll').addClass('hide')
$('#emoji').addClass('hide')
2019-09-01 22:27:18 +10:00
} else {
2019-11-09 00:52:54 +11:00
if ($('#poll').hasClass('hide') && $('#emoji').hasClass('hide')) {
$('#right-side').hide()
$('#right-side').css('width', '300px')
$('#left-side').css('width', '100%')
var width = localStorage.getItem('postbox-width')
2019-11-04 03:09:43 +11:00
if (width) {
2019-11-09 00:52:54 +11:00
width = width.replace('px', '') * 1
2019-11-04 03:09:43 +11:00
} else {
2019-11-09 00:52:54 +11:00
width = 300
2019-11-04 03:09:43 +11:00
}
2019-11-09 00:52:54 +11:00
$('#post-box').css('width', width + 'px')
2019-09-01 01:02:34 +10:00
}
2019-03-13 02:51:07 +11:00
}
2019-11-09 00:52:54 +11:00
})
2019-11-04 03:09:43 +11:00
}
2018-01-28 23:22:43 +11:00
}
2019-11-09 00:52:54 +11:00
oldSuggest = suggest
prev_value = new_val
}, 1000)
2019-11-04 03:09:43 +11:00
},
false
2019-11-09 00:52:54 +11:00
)
2018-01-28 23:22:43 +11:00
2019-11-04 03:09:43 +11:00
input.addEventListener(
2019-11-09 00:52:54 +11:00
'blur',
2019-11-04 03:09:43 +11:00
function() {
2019-11-09 00:52:54 +11:00
window.clearInterval(timer)
favTag()
2019-11-04 03:09:43 +11:00
},
false
2019-11-09 00:52:54 +11:00
)
2018-03-14 05:31:31 +11:00
function tagInsert(code, del) {
2019-11-09 00:52:54 +11:00
var now = $('#textarea').val()
var selin = $('#textarea').prop('selectionStart')
2018-03-14 05:31:31 +11:00
if (!del) {
} else {
2019-11-09 00:52:54 +11:00
var regExp = new RegExp(del.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&'), 'g')
var now = now.replace(regExp, '')
selin = selin - del.length
2018-03-14 05:31:31 +11:00
}
2019-05-19 17:39:30 +10:00
if (selin > 0) {
2019-11-09 00:52:54 +11:00
var before = now.substr(0, selin)
var after = now.substr(selin, now.length)
newt = before + ' ' + code + ' ' + after
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
newt = code + ' ' + now
2018-03-14 05:31:31 +11:00
}
2019-11-09 00:52:54 +11:00
$('#textarea').val(newt)
$('#textarea').focus()
if ($('#poll').hasClass('hide') && $('#emoji').hasClass('hide')) {
$('#right-side').hide()
$('#right-side').css('width', '300px')
$('#left-side').css('width', '50%')
var width = localStorage.getItem('postbox-width').replace('px', '') * 1
2019-09-01 00:35:18 +10:00
if (!width) {
2019-11-09 00:52:54 +11:00
width = 300
2019-09-01 00:35:18 +10:00
}
2019-11-09 00:52:54 +11:00
$('#post-box').css('width', width + 'px')
2019-03-13 02:51:07 +11:00
}
2019-11-09 00:52:54 +11:00
$('#suggest').html('')
2018-07-22 23:03:46 +10:00
}
2019-05-19 17:39:30 +10:00
function cgNPs(q) {
2019-11-09 00:52:54 +11:00
suggest = 'https://cg.toot.app/api/v1/search/light?q=' + q
2019-05-19 17:39:30 +10:00
if (suggest != oldSuggest) {
2019-11-09 00:52:54 +11:00
console.log('Try to get suggest at ' + suggest)
2019-05-19 17:39:30 +10:00
fetch(suggest, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2019-05-19 17:39:30 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json'
2018-07-22 23:03:46 +10:00
}
2019-11-04 03:09:43 +11:00
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:09:43 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:09:43 +11: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:09:43 +11:00
})
.then(function(json) {
if (json[0]) {
2019-11-09 00:52:54 +11:00
var tags = ''
2019-11-04 03:09:43 +11:00
Object.keys(json).forEach(function(key4) {
2019-11-09 00:52:54 +11:00
var tag = json[key4]
tags = tags + `<a onclick="cgNp('${json[key4]}')" class="pointer">${escapeHTML(json[key4])}</a>`
})
$('#suggest').html('Cinderella NowPlaying:' + tags)
2019-11-04 03:09:43 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#suggest').html('Cinderella NowPlaying:Not Found')
2019-11-04 03:09:43 +11:00
}
2019-11-09 00:52:54 +11:00
})
2019-05-19 17:39:30 +10:00
}
2019-11-04 03:09:43 +11:00
}