]> cat aescling's git repositories - mastodon.git/commitdiff
Fix caret position after inserting emoji (#7167)
authorabcang <abcang1015@gmail.com>
Tue, 17 Apr 2018 11:50:33 +0000 (20:50 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Tue, 17 Apr 2018 11:50:33 +0000 (13:50 +0200)
app/javascript/mastodon/actions/compose.js
app/javascript/mastodon/features/compose/components/compose_form.js
app/javascript/mastodon/features/compose/containers/compose_form_container.js
app/javascript/mastodon/reducers/compose.js

index 59aa6f98d1aa887893259788a7941fb207e30032..ea9d9f359c3a67103b76e634a3dfa7ee129abaa3 100644 (file)
@@ -446,11 +446,12 @@ export function changeComposeVisibility(value) {
   };
 };
 
-export function insertEmojiCompose(position, emoji) {
+export function insertEmojiCompose(position, emoji, needsSpace) {
   return {
     type: COMPOSE_EMOJI_INSERT,
     position,
     emoji,
+    needsSpace,
   };
 };
 
index fe7bb1cb38edb115c1a00564082bbcdc31b89cf9..39eb02362d110f7603da2ac369a2a2b817ca0ea1 100644 (file)
@@ -19,6 +19,8 @@ import ImmutablePureComponent from 'react-immutable-pure-component';
 import { length } from 'stringz';
 import { countableText } from '../util/counter';
 
+const allowedAroundShortCode = '><\u0085\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d';
+
 const messages = defineMessages({
   placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
   spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Write your warning here' },
@@ -144,10 +146,13 @@ export default class ComposeForm extends ImmutablePureComponent {
   }
 
   handleEmojiPick = (data) => {
+    const { text }     = this.props;
     const position     = this.autosuggestTextarea.textarea.selectionStart;
     const emojiChar    = data.native;
-    this._restoreCaret = position + emojiChar.length + 1;
-    this.props.onPickEmoji(position, data);
+    const needsSpace   = data.custom && position > 0 && !allowedAroundShortCode.includes(text[position - 1]);
+
+    this._restoreCaret = position + emojiChar.length + 1 + (needsSpace ? 1 : 0);
+    this.props.onPickEmoji(position, data, needsSpace);
   }
 
   render () {
index ede23d36105e4053e96cf1bd412126392ac915d3..c3aa580ee307096fd8116d64d97483a84b91ec40 100644 (file)
@@ -56,8 +56,8 @@ const mapDispatchToProps = (dispatch) => ({
     dispatch(uploadCompose(files));
   },
 
-  onPickEmoji (position, data) {
-    dispatch(insertEmojiCompose(position, data));
+  onPickEmoji (position, data, needsSpace) {
+    dispatch(insertEmojiCompose(position, data, needsSpace));
   },
 
 });
index 87049ea793f828452e980761d03f2cb1848bf6b0..46d9d6c8eef3bdab0d13d51b8adff95d217d823e 100644 (file)
@@ -36,8 +36,6 @@ import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrde
 import uuid from '../uuid';
 import { me } from '../initial_state';
 
-const allowedAroundShortCode = '><\u0085\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d';
-
 const initialState = ImmutableMap({
   mounted: 0,
   sensitive: false,
@@ -137,9 +135,8 @@ const updateSuggestionTags = (state, token) => {
   });
 };
 
-const insertEmoji = (state, position, emojiData) => {
+const insertEmoji = (state, position, emojiData, needsSpace) => {
   const oldText = state.get('text');
-  const needsSpace = emojiData.custom && position > 0 && !allowedAroundShortCode.includes(oldText[position - 1]);
   const emoji = needsSpace ? ' ' + emojiData.native : emojiData.native;
 
   return state.merge({
@@ -288,7 +285,7 @@ export default function compose(state = initialState, action) {
       return state;
     }
   case COMPOSE_EMOJI_INSERT:
-    return insertEmoji(state, action.position, action.emoji);
+    return insertEmoji(state, action.position, action.emoji, action.needsSpace);
   case COMPOSE_UPLOAD_CHANGE_SUCCESS:
     return state
       .set('is_submitting', false)