Nice insert emoji and tags yeah

This commit is contained in:
cutls 2019-11-16 22:02:42 +09:00
parent fe32723ad5
commit e345b3a479
2 changed files with 61 additions and 37 deletions

View File

@ -284,32 +284,39 @@ function emojiList(target, reaction) {
//絵文字など様々なものをテキストボックスに挿入 //絵文字など様々なものをテキストボックスに挿入
function emojiInsert(code, del) { function emojiInsert(code, del) {
var now = $('#textarea').val()
var selin = $('#textarea').prop('selectionStart')
if (localStorage.getItem('emoji-zero-width') == 'yes') { if (localStorage.getItem('emoji-zero-width') == 'yes') {
var blank = '' var blankBefore = ''
var blankAfter = ''
} else { } else {
var blank = ' ' var blankBefore = ' '
var blankAfter = ' '
} }
var before = now.substr(0, selin) var textarea = document.querySelector('#textarea')
if (before.slice(-1) != ' ') { var sentence = textarea.value
before = before + blank var len = sentence.length
} var pos = textarea.selectionStart
var after = now.substr(selin, now.length) if (del) {
if (after.slice(-1) != ' ') { var delLen = del.length
after = blank + after
}
newt = before + code + after
if (!del) {
$('#textarea').val(newt)
//emoji();
} else { } else {
var regExp = new RegExp(del.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&'), 'g') var delLen = 0
var now = now.replace(regExp, '')
$('#textarea').val(now + blank + code)
} }
var before = sentence.substr(0, pos - delLen)
$('#textarea').focus() 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
} else {
var word = blankBefore + code + blankAfter
}
sentence = before + word + after
textarea.value = sentence
} }
//改行挿入 //改行挿入
function brInsert(code) { function brInsert(code) {

View File

@ -97,7 +97,9 @@ input.addEventListener(
his[4].uses * 1 + his[4].uses * 1 +
his[5].uses * 1 + his[5].uses * 1 +
his[6].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)` tagHTML = `<br><a onclick="tagInsert('#${escapeHTML(
tag.name
)}','#${q}')" class="pointer">#${escapeHTML(tag.name)}</a>&nbsp;${uses}toot(s)`
var item = { var item = {
uses: uses, uses: uses,
@ -144,7 +146,9 @@ input.addEventListener(
if (acct.acct != q) { if (acct.acct != q) {
//Instance Actorって… //Instance Actorって…
if (acct.username.indexOf('.') < 0) { if (acct.username.indexOf('.') < 0) {
accts = accts + `<a onclick="tagInsert('@${acct.acct}','@${q}')" class="pointer">@${acct.acct}</a><br>` accts =
accts +
`<a onclick="tagInsert('@${acct.acct}','@${q}')" class="pointer">@${acct.acct}</a><br>`
} }
} }
}) })
@ -194,23 +198,34 @@ input.addEventListener(
false false
) )
function tagInsert(code, del) { function tagInsert(code, del) {
var now = $('#textarea').val() var blankBefore = ' '
var selin = $('#textarea').prop('selectionStart') var blankAfter = ' '
if (!del) { var textarea = document.querySelector('#textarea')
var sentence = textarea.value
var len = sentence.length
var pos = textarea.selectionStart
if (del) {
var delLen = del.length
} else { } else {
var regExp = new RegExp(del.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&'), 'g') var delLen = 0
var now = now.replace(regExp, '')
selin = selin - del.length
} }
if (selin > 0) { var before = sentence.substr(0, pos - del)
var before = now.substr(0, selin) var last = before.substr(-1, 1)
var after = now.substr(selin, now.length) if (last == ' ') blankBefore = ''
newt = before + ' ' + code + ' ' + after 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
} else { } else {
newt = code + ' ' + now var word = blankBefore + code + blankAfter
} }
$('#textarea').val(newt) sentence = before + word + after
$('#textarea').focus() textarea.value = sentence
if ($('#poll').hasClass('hide') && $('#emoji').hasClass('hide')) { if ($('#poll').hasClass('hide') && $('#emoji').hasClass('hide')) {
$('#right-side').hide() $('#right-side').hide()
$('#right-side').css('width', '300px') $('#right-side').css('width', '300px')
@ -251,7 +266,9 @@ function cgNPs(q) {
var tags = '' var tags = ''
Object.keys(json).forEach(function(key4) { Object.keys(json).forEach(function(key4) {
var tag = json[key4] var tag = json[key4]
tags = tags + `<a onclick="cgNp('${json[key4]}')" class="pointer">${escapeHTML(json[key4])}</a>` tags =
tags +
`<a onclick="cgNp('${json[key4]}')" class="pointer">${escapeHTML(json[key4])}</a>`
}) })
$('#suggest').html('Cinderella NowPlaying:' + tags) $('#suggest').html('Cinderella NowPlaying:' + tags)
} else { } else {