From e345b3a479dbfd8cfc88fce59570e5128d3025a2 Mon Sep 17 00:00:00 2001 From: cutls Date: Sat, 16 Nov 2019 22:02:42 +0900 Subject: [PATCH] Nice insert emoji and tags yeah --- app/js/post/emoji.js | 49 ++++++++++++++++++++++++------------------ app/js/post/suggest.js | 49 ++++++++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/app/js/post/emoji.js b/app/js/post/emoji.js index 8735aa4c..75ee099c 100644 --- a/app/js/post/emoji.js +++ b/app/js/post/emoji.js @@ -284,32 +284,39 @@ function emojiList(target, reaction) { //絵文字など様々なものをテキストボックスに挿入 function emojiInsert(code, del) { - var now = $('#textarea').val() - var selin = $('#textarea').prop('selectionStart') if (localStorage.getItem('emoji-zero-width') == 'yes') { - var blank = '​' + var blankBefore = '​' + var blankAfter = '​' } else { - var blank = ' ' + var blankBefore = ' ' + var blankAfter = ' ' } - 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 - } - newt = before + code + after - if (!del) { - $('#textarea').val(newt) - //emoji(); + var textarea = document.querySelector('#textarea') + var sentence = textarea.value + var len = sentence.length + var pos = textarea.selectionStart + if (del) { + var delLen = del.length } else { - var regExp = new RegExp(del.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&'), 'g') - var now = now.replace(regExp, '') - $('#textarea').val(now + blank + code) + var delLen = 0 } - - $('#textarea').focus() + 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 + } else { + var word = blankBefore + code + blankAfter + } + sentence = before + word + after + textarea.value = sentence } //改行挿入 function brInsert(code) { diff --git a/app/js/post/suggest.js b/app/js/post/suggest.js index 8f248ff4..59cb452d 100644 --- a/app/js/post/suggest.js +++ b/app/js/post/suggest.js @@ -97,7 +97,9 @@ input.addEventListener( his[4].uses * 1 + his[5].uses * 1 + his[6].uses * 1 - tagHTML = `
#${escapeHTML(tag.name)} ${uses}toot(s)` + tagHTML = `
#${escapeHTML(tag.name)} ${uses}toot(s)` var item = { uses: uses, @@ -144,7 +146,9 @@ input.addEventListener( if (acct.acct != q) { //Instance Actorって… if (acct.username.indexOf('.') < 0) { - accts = accts + `@${acct.acct}
` + accts = + accts + + `@${acct.acct}
` } } }) @@ -194,23 +198,34 @@ input.addEventListener( false ) function tagInsert(code, del) { - var now = $('#textarea').val() - var selin = $('#textarea').prop('selectionStart') - if (!del) { + var blankBefore = ' ' + var blankAfter = ' ' + var textarea = document.querySelector('#textarea') + var sentence = textarea.value + var len = sentence.length + var pos = textarea.selectionStart + if (del) { + var delLen = del.length } else { - var regExp = new RegExp(del.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&'), 'g') - var now = now.replace(regExp, '') - selin = selin - del.length + var delLen = 0 } - if (selin > 0) { - var before = now.substr(0, selin) - var after = now.substr(selin, now.length) - newt = before + ' ' + code + ' ' + after + var before = sentence.substr(0, pos - del) + 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 { - newt = code + ' ' + now + var word = blankBefore + code + blankAfter } - $('#textarea').val(newt) - $('#textarea').focus() + sentence = before + word + after + textarea.value = sentence if ($('#poll').hasClass('hide') && $('#emoji').hasClass('hide')) { $('#right-side').hide() $('#right-side').css('width', '300px') @@ -251,7 +266,9 @@ function cgNPs(q) { var tags = '' Object.keys(json).forEach(function(key4) { var tag = json[key4] - tags = tags + `${escapeHTML(json[key4])}` + tags = + tags + + `${escapeHTML(json[key4])}` }) $('#suggest').html('Cinderella NowPlaying:' + tags) } else {