all files / editor-manager/plugin/ emoji-picker-action.js

100% Statements 50/50
93.33% Branches 14/15
100% Functions 8/8
100% Lines 50/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73   1892× 1892×   1892× 1892×   1747× 1747×   77× 77× 77×                               1747×        
define(["require", "exports", "./../../common/constant", "./inserthtml"], function (require, exports, EVENTS, inserthtml_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var EmojiPickerAction = (function () {
        function EmojiPickerAction(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        EmojiPickerAction.prototype.addEventListener = function () {
            this.parent.observer.on(EVENTS.EMOJI_PICKER_ACTIONS, this.emojiInsert, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        EmojiPickerAction.prototype.removeEventListener = function () {
            this.parent.observer.off(EVENTS.EMOJI_PICKER_ACTIONS, this.emojiInsert);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        EmojiPickerAction.prototype.emojiInsert = function (args) {
            var node = document.createTextNode(args.value);
            var selection = this.parent.currentDocument.getSelection();
            var range = selection.getRangeAt(0);
            var cursorPos = range.startOffset;
            for (var i = cursorPos - 1; i >= cursorPos - 15; i--) {
                var prevChar_1 = selection.focusNode.textContent.substring(i - 1, i);
                var isPrevSpace_1 = /:$/.test(prevChar_1);
                if (isPrevSpace_1) {
                    this.beforeApplyFormat(true);
                    break;
                }
            }
            var colon = /:$/.test(selection.focusNode.textContent.charAt(cursorPos - 1));
            var prevChar = selection.focusNode.textContent.charAt(cursorPos - 2);
            var isPrevSpace = /\s/.test(prevChar);
            if (colon && (isPrevSpace || selection.focusOffset === 1)) {
                this.beforeApplyFormat(true);
            }
            inserthtml_1.InsertHtml.Insert(this.parent.currentDocument, node, this.parent.editableElement);
            Eif (args.callBack) {
                args.callBack({
                    requestType: args.subCommand,
                    editorMode: 'HTML',
                    event: args.event,
                    range: this.parent.nodeSelection.getRange(this.parent.currentDocument),
                    elements: this.parent.nodeSelection.getSelectedNodes(this.parent.currentDocument)
                });
            }
        };
        EmojiPickerAction.prototype.beforeApplyFormat = function (isBlockFormat) {
            var range1 = this.parent.nodeSelection.getRange(this.parent.currentDocument);
            var node = this.parent.nodeSelection.getNodeCollection(range1)[0];
            var blockNewLine = !(node.parentElement.innerHTML.replace(/&nbsp;|<br>/g, '').trim() === ':' || node.textContent.trim().indexOf('/') === 0);
            var startNode = node;
            if (blockNewLine && isBlockFormat) {
                while (startNode !== this.parent.editableElement) {
                    startNode = startNode.parentElement;
                }
            }
            var startPoint = range1.startOffset;
            while (this.parent.nodeSelection.getRange(document).toString().indexOf(':') === -1) {
                this.parent.nodeSelection.setSelectionText(document, node, node, startPoint, range1.endOffset);
                startPoint--;
            }
            var range2 = this.parent.nodeSelection.getRange(this.parent.currentDocument);
            var node2 = this.parent.nodeCutter.GetSpliceNode(range2, node);
            node2.parentNode.removeChild(node2);
        };
        EmojiPickerAction.prototype.destroy = function () {
            this.removeEventListener();
        };
        return EmojiPickerAction;
    }());
    exports.EmojiPickerAction = EmojiPickerAction;
});