2016-02-04 07:05:14 +08:00
|
|
|
import {DOMUtils, ContenteditableExtension} from 'nylas-exports'
|
|
|
|
import EmojiActions from './emoji-actions'
|
|
|
|
import EmojiPicker from './emoji-picker'
|
|
|
|
const emoji = require('node-emoji');
|
|
|
|
const emojis = Object.keys(emoji.emoji).sort();
|
|
|
|
|
|
|
|
class EmojisComposerExtension extends ContenteditableExtension {
|
|
|
|
|
2016-02-04 09:40:28 +08:00
|
|
|
static onContentChanged = ({editor}) => {
|
|
|
|
const sel = editor.currentSelection()
|
|
|
|
const {emojiOptions, triggerWord} = EmojisComposerExtension._findEmojiOptions(sel);
|
2016-02-04 07:05:14 +08:00
|
|
|
if (sel.anchorNode && sel.isCollapsed) {
|
|
|
|
if (emojiOptions.length > 0) {
|
2016-02-04 09:40:28 +08:00
|
|
|
const offset = sel.anchorOffset;
|
2016-02-04 07:05:14 +08:00
|
|
|
if (!DOMUtils.closest(sel.anchorNode, "n1-emoji-autocomplete")) {
|
2016-02-04 09:40:28 +08:00
|
|
|
editor.select(sel.anchorNode,
|
2016-02-13 03:41:11 +08:00
|
|
|
sel.anchorOffset - triggerWord.length - 1,
|
2016-02-04 09:40:28 +08:00
|
|
|
sel.focusNode,
|
|
|
|
sel.focusOffset).wrapSelection("n1-emoji-autocomplete");
|
|
|
|
editor.select(sel.anchorNode,
|
|
|
|
offset,
|
|
|
|
sel.anchorNode,
|
|
|
|
offset);
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
2016-02-04 09:40:28 +08:00
|
|
|
} else {
|
2016-02-04 07:05:14 +08:00
|
|
|
if (DOMUtils.closest(sel.anchorNode, "n1-emoji-autocomplete")) {
|
|
|
|
editor.unwrapNodeAndSelectAll(DOMUtils.closest(sel.anchorNode, "n1-emoji-autocomplete"));
|
|
|
|
editor.select(sel.anchorNode,
|
2016-02-13 03:41:11 +08:00
|
|
|
sel.anchorOffset + triggerWord.length + 1,
|
2016-02-04 07:05:14 +08:00
|
|
|
sel.focusNode,
|
2016-02-13 03:41:11 +08:00
|
|
|
sel.focusOffset + triggerWord.length + 1);
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 09:40:28 +08:00
|
|
|
} else {
|
2016-02-04 07:05:14 +08:00
|
|
|
if (DOMUtils.closest(sel.anchorNode, "n1-emoji-autocomplete")) {
|
|
|
|
editor.unwrapNodeAndSelectAll(DOMUtils.closest(sel.anchorNode, "n1-emoji-autocomplete"));
|
|
|
|
editor.select(sel.anchorNode,
|
|
|
|
sel.anchorOffset + triggerWord.length,
|
|
|
|
sel.focusNode,
|
|
|
|
sel.focusOffset + triggerWord.length);
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 06:54:43 +08:00
|
|
|
};
|
2016-02-04 07:05:14 +08:00
|
|
|
|
|
|
|
static toolbarComponentConfig = ({toolbarState}) => {
|
2016-02-04 09:40:28 +08:00
|
|
|
const sel = toolbarState.selectionSnapshot;
|
2016-02-04 07:05:14 +08:00
|
|
|
if (sel) {
|
2016-02-04 09:40:28 +08:00
|
|
|
const {emojiOptions} = EmojisComposerExtension._findEmojiOptions(sel);
|
2016-02-04 07:05:14 +08:00
|
|
|
if (emojiOptions.length > 0 && !toolbarState.dragging && !toolbarState.doubleDown) {
|
2016-02-04 09:40:28 +08:00
|
|
|
const locationRefNode = DOMUtils.closest(sel.anchorNode,
|
|
|
|
"n1-emoji-autocomplete");
|
2016-02-13 03:41:11 +08:00
|
|
|
if (!locationRefNode) return null;
|
|
|
|
const selectedEmoji = locationRefNode.getAttribute("selectedEmoji");
|
2016-02-04 07:05:14 +08:00
|
|
|
return {
|
|
|
|
component: EmojiPicker,
|
|
|
|
props: {emojiOptions,
|
|
|
|
selectedEmoji},
|
|
|
|
locationRefNode: locationRefNode,
|
2016-02-04 09:40:28 +08:00
|
|
|
width: EmojisComposerExtension._emojiPickerWidth(emojiOptions),
|
2016-02-13 03:41:11 +08:00
|
|
|
height: EmojisComposerExtension._emojiPickerHeight(emojiOptions),
|
2016-02-25 03:25:31 +08:00
|
|
|
hidePointer: true,
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2016-02-18 06:54:43 +08:00
|
|
|
};
|
2016-02-04 07:05:14 +08:00
|
|
|
|
|
|
|
static editingActions = () => {
|
|
|
|
return [{
|
|
|
|
action: EmojiActions.selectEmoji,
|
2016-02-04 09:40:28 +08:00
|
|
|
callback: EmojisComposerExtension._onSelectEmoji,
|
2016-02-04 07:05:14 +08:00
|
|
|
}]
|
2016-02-18 06:54:43 +08:00
|
|
|
};
|
2016-02-04 07:05:14 +08:00
|
|
|
|
|
|
|
static onKeyDown = ({editor, event}) => {
|
2016-02-04 09:40:28 +08:00
|
|
|
const sel = editor.currentSelection()
|
|
|
|
const {emojiOptions} = EmojisComposerExtension._findEmojiOptions(sel);
|
2016-02-04 07:05:14 +08:00
|
|
|
if (emojiOptions.length > 0) {
|
2016-02-04 09:40:28 +08:00
|
|
|
if (event.key === "ArrowDown" || event.key === "ArrowRight" ||
|
|
|
|
event.key === "ArrowUp" || event.key === "ArrowLeft") {
|
2016-02-04 07:05:14 +08:00
|
|
|
event.preventDefault();
|
2016-02-04 09:40:28 +08:00
|
|
|
const moveToNext = (event.key === "ArrowDown" || event.key === "ArrowRight")
|
|
|
|
const emojiNameNode = DOMUtils.closest(sel.anchorNode, "n1-emoji-autocomplete");
|
|
|
|
const selectedEmoji = emojiNameNode.getAttribute("selectedEmoji");
|
2016-02-04 07:05:14 +08:00
|
|
|
if (selectedEmoji) {
|
2016-02-04 09:40:28 +08:00
|
|
|
const emojiIndex = emojiOptions.indexOf(selectedEmoji);
|
2016-02-04 07:05:14 +08:00
|
|
|
if (emojiIndex < emojiOptions.length - 1 && moveToNext) {
|
|
|
|
emojiNameNode.setAttribute("selectedEmoji", emojiOptions[emojiIndex + 1]);
|
2016-02-04 09:40:28 +08:00
|
|
|
} else if (emojiIndex > 0 && !moveToNext) {
|
2016-02-04 07:05:14 +08:00
|
|
|
emojiNameNode.setAttribute("selectedEmoji", emojiOptions[emojiIndex - 1]);
|
2016-02-04 09:40:28 +08:00
|
|
|
} else {
|
|
|
|
const index = moveToNext ? 0 : emojiOptions.length - 1;
|
2016-02-04 07:05:14 +08:00
|
|
|
emojiNameNode.setAttribute("selectedEmoji", emojiOptions[index]);
|
|
|
|
}
|
2016-02-04 09:40:28 +08:00
|
|
|
} else {
|
|
|
|
const index = moveToNext ? 1 : emojiOptions.length - 1;
|
2016-02-04 07:05:14 +08:00
|
|
|
emojiNameNode.setAttribute("selectedEmoji", emojiOptions[index]);
|
|
|
|
}
|
2016-02-04 09:40:28 +08:00
|
|
|
} else if (event.key === "Enter") {
|
2016-02-04 07:05:14 +08:00
|
|
|
event.preventDefault();
|
2016-02-04 09:40:28 +08:00
|
|
|
const emojiNameNode = DOMUtils.closest(sel.anchorNode, "n1-emoji-autocomplete");
|
|
|
|
let selectedEmoji = emojiNameNode.getAttribute("selectedEmoji");
|
2016-02-04 07:05:14 +08:00
|
|
|
if (!selectedEmoji) selectedEmoji = emojiOptions[0];
|
|
|
|
EmojisComposerExtension._onSelectEmoji({editor: editor,
|
|
|
|
actionArg: {emojiChar: emoji.get(selectedEmoji)}});
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 06:54:43 +08:00
|
|
|
};
|
2016-02-04 07:05:14 +08:00
|
|
|
|
|
|
|
static _findEmojiOptions(sel) {
|
|
|
|
if (sel.anchorNode &&
|
|
|
|
sel.anchorNode.nodeValue &&
|
|
|
|
sel.anchorNode.nodeValue.length > 0 &&
|
|
|
|
sel.isCollapsed) {
|
2016-02-13 03:41:11 +08:00
|
|
|
const words = sel.anchorNode.nodeValue.substring(0, sel.anchorOffset);
|
|
|
|
let index = words.lastIndexOf(":");
|
|
|
|
let lastWord = "";
|
|
|
|
if (index !== -1 && words.lastIndexOf(" ") < index) {
|
|
|
|
lastWord = words.substring(index + 1, sel.anchorOffset);
|
|
|
|
} else {
|
2016-02-04 09:40:28 +08:00
|
|
|
const {text} = EmojisComposerExtension._getTextUntilSpace(sel.anchorNode, sel.anchorOffset);
|
2016-02-13 03:41:11 +08:00
|
|
|
index = text.lastIndexOf(":");
|
|
|
|
if (index !== -1 && text.lastIndexOf(" ") < index) {
|
|
|
|
lastWord = text.substring(index + 1);
|
|
|
|
} else {
|
|
|
|
return {triggerWord: "", emojiOptions: []};
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
2016-02-13 03:41:11 +08:00
|
|
|
}
|
|
|
|
if (lastWord.length > 0) {
|
|
|
|
return {triggerWord: lastWord, emojiOptions: EmojisComposerExtension._findMatches(lastWord)};
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
|
|
|
return {triggerWord: lastWord, emojiOptions: []};
|
|
|
|
}
|
|
|
|
return {triggerWord: "", emojiOptions: []};
|
|
|
|
}
|
|
|
|
|
|
|
|
static _onSelectEmoji = ({editor, actionArg}) => {
|
2016-02-04 09:40:28 +08:00
|
|
|
const emojiChar = actionArg.emojiChar;
|
2016-02-04 07:05:14 +08:00
|
|
|
if (!emojiChar) return null;
|
2016-02-04 09:40:28 +08:00
|
|
|
const sel = editor.currentSelection()
|
2016-02-04 07:05:14 +08:00
|
|
|
if (sel.anchorNode &&
|
|
|
|
sel.anchorNode.nodeValue &&
|
|
|
|
sel.anchorNode.nodeValue.length > 0 &&
|
2016-02-13 03:41:11 +08:00
|
|
|
sel.isCollapsed) {
|
|
|
|
const words = sel.anchorNode.nodeValue.substring(0, sel.anchorOffset);
|
|
|
|
let index = words.lastIndexOf(":");
|
|
|
|
let lastWord = words.substring(index + 1, sel.anchorOffset);
|
|
|
|
if (index !== -1 && words.lastIndexOf(" ") < index) {
|
|
|
|
editor.select(sel.anchorNode,
|
|
|
|
sel.anchorOffset - lastWord.length - 1,
|
|
|
|
sel.focusNode,
|
|
|
|
sel.focusOffset);
|
|
|
|
} else {
|
2016-02-04 09:40:28 +08:00
|
|
|
const {text, textNode} = EmojisComposerExtension._getTextUntilSpace(sel.anchorNode, sel.anchorOffset);
|
2016-02-13 03:41:11 +08:00
|
|
|
index = text.lastIndexOf(":");
|
|
|
|
lastWord = text.substring(index + 1);
|
2016-02-04 09:40:28 +08:00
|
|
|
const offset = textNode.nodeValue.lastIndexOf(":");
|
|
|
|
editor.select(textNode,
|
|
|
|
offset,
|
|
|
|
sel.focusNode,
|
|
|
|
sel.focusOffset);
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
|
|
|
editor.insertText(emojiChar);
|
|
|
|
}
|
2016-02-18 06:54:43 +08:00
|
|
|
};
|
2016-02-04 07:05:14 +08:00
|
|
|
|
|
|
|
static _emojiPickerWidth(emojiOptions) {
|
2016-02-04 09:40:28 +08:00
|
|
|
let maxLength = 0;
|
|
|
|
for (const emojiOption of emojiOptions) {
|
|
|
|
if (emojiOption.length > maxLength) {
|
|
|
|
maxLength = emojiOption.length;
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 09:40:28 +08:00
|
|
|
const WIDTH_PER_CHAR = 8;
|
2016-02-25 03:25:31 +08:00
|
|
|
return (maxLength + 8) * WIDTH_PER_CHAR;
|
2016-02-04 07:05:14 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 03:41:11 +08:00
|
|
|
static _emojiPickerHeight(emojiOptions) {
|
2016-02-25 02:07:27 +08:00
|
|
|
const HEIGHT_PER_EMOJI = 25;
|
2016-02-13 03:41:11 +08:00
|
|
|
if (emojiOptions.length < 5) {
|
|
|
|
return emojiOptions.length * HEIGHT_PER_EMOJI + 20;
|
|
|
|
}
|
2016-02-25 02:07:27 +08:00
|
|
|
return 5 * HEIGHT_PER_EMOJI + 23;
|
2016-02-13 03:41:11 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 07:05:14 +08:00
|
|
|
static _getTextUntilSpace(node, offset) {
|
2016-02-04 09:40:28 +08:00
|
|
|
let text = node.nodeValue.substring(0, offset);
|
|
|
|
let prevTextNode = DOMUtils.previousTextNode(node);
|
2016-02-04 07:05:14 +08:00
|
|
|
if (!prevTextNode) return {text: text, textNode: node};
|
|
|
|
while (prevTextNode) {
|
2016-02-04 09:40:28 +08:00
|
|
|
if (prevTextNode.nodeValue.indexOf(" ") === -1 &&
|
|
|
|
prevTextNode.nodeValue.indexOf(":") === -1) {
|
2016-02-04 07:05:14 +08:00
|
|
|
text = prevTextNode.nodeValue + text;
|
|
|
|
prevTextNode = DOMUtils.previousTextNode(prevTextNode);
|
2016-02-04 09:40:28 +08:00
|
|
|
} else {
|
|
|
|
text = prevTextNode.nodeValue.trim() + text;
|
2016-02-04 07:05:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {text: text, textNode: prevTextNode};
|
|
|
|
}
|
|
|
|
|
|
|
|
static _findMatches(word) {
|
2016-02-04 09:40:28 +08:00
|
|
|
const emojiOptions = []
|
2016-02-04 07:05:14 +08:00
|
|
|
for (const curEmoji of emojis) {
|
2016-02-04 09:40:28 +08:00
|
|
|
if (word === curEmoji.substring(0, word.length)) {
|
2016-02-04 07:05:14 +08:00
|
|
|
emojiOptions.push(curEmoji);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return emojiOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EmojisComposerExtension;
|