# all files / src/js/base/module/ HintPopover.js

28.47% Statements 39/137
7.55% Branches 4/53
19.23% Functions 5/26
27.94% Lines 38/136

    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
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    2121×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    2×
    97×
    97×
    97×
    97×
    97×
    97×
    97×
    97×
    97×

    1×
    99×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     (function(global){global.wrappers['/Users/lqez/Dev/summernote/src/js/base/module/HintPopover.js']=[function(require,module,exports,__dirname,__filename){ "use strict";
    exports.__esModule = true;
    var jquery_1 = require("jquery");
    var func_1 = require("../core/func");
    var lists_1 = require("../core/lists");
    var dom_1 = require("../core/dom");
    var range_1 = require("../core/range");
    var key_1 = require("../core/key");
    var POPOVER_DIST = 5;
    var HintPopover = /** @class */ (function () {
        function HintPopover(context) {
            var _this = this;
            this.context = context;
            this.ui = jquery_1["default"].summernote.ui;
            this.$editable = context.layoutInfo.editable;
            this.options = context.options;
            this.hint = this.options.hint || [];
            this.direction = this.options.hintDirection || 'bottom';
            this.hints = jquery_1["default"].isArray(this.hint) ? this.hint : [this.hint];
            this.events = {
                'summernote.keyup': function (we, e) {
                    if (!e.isDefaultPrevented()) {
                        _this.handleKeyup(e);
                    }
                },
                'summernote.keydown': function (we, e) {
                    _this.handleKeydown(e);
                },
                'summernote.disable summernote.dialog.shown': function () {
                    _this.hide();
                }
            };
        }
        HintPopover.prototype.shouldInitialize = function () {
            return this.hints.length > 0;
        };
        HintPopover.prototype.initialize = function () {
            var _this = this;
            this.lastWordRange = null;
            this.$popover = this.ui.popover({
                className: 'note-hint-popover',
                hideArrow: true,
                direction: ''
            }).render().appendTo(this.options.container);
            this.$popover.hide();
            this.$content = this.$popover.find('.popover-content,.note-popover-content');
            this.$content.on('click', '.note-hint-item', function (e) {
                _this.$content.find('.active').removeClass('active');
                jquery_1["default"](e.currentTarget).addClass('active');
                _this.replace();
            });
        };
        HintPopover.prototype.destroy = function () {
            this.$popover.remove();
        };
        HintPopover.prototype.selectItem = function ($item) {
            this.$content.find('.active').removeClass('active');
            $item.addClass('active');
            this.$content[0].scrollTop = $item[0].offsetTop - (this.$content.innerHeight() / 2);
        };
        HintPopover.prototype.moveDown = function () {
            var $current = this.$content.find('.note-hint-item.active');
            var $next = $current.next();
            if ($next.length) {
                this.selectItem($next);
            }
            else {
                var $nextGroup = $current.parent().next();
                if (!$nextGroup.length) {
                    $nextGroup = this.$content.find('.note-hint-group').first();
                }
                this.selectItem($nextGroup.find('.note-hint-item').first());
            }
        };
        HintPopover.prototype.moveUp = function () {
            var $current = this.$content.find('.note-hint-item.active');
            var $prev = $current.prev();
            if ($prev.length) {
                this.selectItem($prev);
            }
            else {
                var $prevGroup = $current.parent().prev();
                if (!$prevGroup.length) {
                    $prevGroup = this.$content.find('.note-hint-group').last();
                }
                this.selectItem($prevGroup.find('.note-hint-item').last());
            }
        };
        HintPopover.prototype.replace = function () {
            var $item = this.$content.find('.note-hint-item.active');
            if ($item.length) {
                var node = this.nodeFromItem($item);
                // XXX: consider to move codes to editor for recording redo/undo.
                this.lastWordRange.insertNode(node);
                range_1["default"].createFromNode(node).collapse().select();
                this.lastWordRange = null;
                this.hide();
                this.context.triggerEvent('change', this.$editable.html(), this.$editable[0]);
                this.context.invoke('editor.focus');
            }
        };
        HintPopover.prototype.nodeFromItem = function ($item) {
            var hint = this.hints[$item.data('index')];
            var item = $item.data('item');
            var node = hint.content ? hint.content(item) : item;
            if (typeof node === 'string') {
                node = dom_1["default"].createText(node);
            }
            return node;
        };
        HintPopover.prototype.createItemTemplates = function (hintIdx, items) {
            var hint = this.hints[hintIdx];
            return items.map(function (item, idx) {
                var $item = jquery_1["default"]('<div class="note-hint-item"/>');
                $item.append(hint.template ? hint.template(item) : item + '');
                $item.data({
                    'index': hintIdx,
                    'item': item
                });
                return $item;
            });
        };
        HintPopover.prototype.handleKeydown = function (e) {
            if (!this.$popover.is(':visible')) {
                return;
            }
            if (e.keyCode === key_1["default"].code.ENTER) {
                e.preventDefault();
                this.replace();
            }
            else if (e.keyCode === key_1["default"].code.UP) {
                e.preventDefault();
                this.moveUp();
            }
            else if (e.keyCode === key_1["default"].code.DOWN) {
                e.preventDefault();
                this.moveDown();
            }
        };
        HintPopover.prototype.searchKeyword = function (index, keyword, callback) {
            var hint = this.hints[index];
            if (hint && hint.match.test(keyword) && hint.search) {
                var matches = hint.match.exec(keyword);
                hint.search(matches[1], callback);
            }
            else {
                callback();
            }
        };
        HintPopover.prototype.createGroup = function (idx, keyword) {
            var _this = this;
            var $group = jquery_1["default"]('<div class="note-hint-group note-hint-group-' + idx + '"/>');
            this.searchKeyword(idx, keyword, function (items) {
                items = items || [];
                if (items.length) {
                    $group.html(_this.createItemTemplates(idx, items));
                    _this.show();
                }
            });
            return $group;
        };
        HintPopover.prototype.handleKeyup = function (e) {
            var _this = this;
            if (!lists_1["default"].contains([key_1["default"].code.ENTER, key_1["default"].code.UP, key_1["default"].code.DOWN], e.keyCode)) {
                var wordRange = this.context.invoke('editor.getLastRange').getWordRange();
                var keyword_1 = wordRange.toString();
                if (this.hints.length && keyword_1) {
                    this.$content.empty();
                    var bnd = func_1["default"].rect2bnd(lists_1["default"].last(wordRange.getClientRects()));
                    if (bnd) {
                        this.$popover.hide();
                        this.lastWordRange = wordRange;
                        this.hints.forEach(function (hint, idx) {
                            if (hint.match.test(keyword_1)) {
                                _this.createGroup(idx, keyword_1).appendTo(_this.$content);
                            }
                        });
                        // select first .note-hint-item
                        this.$content.find('.note-hint-item:first').addClass('active');
                        // set position for popover after group is created
                        if (this.direction === 'top') {
                            this.$popover.css({
                                left: bnd.left,
                                top: bnd.top - this.$popover.outerHeight() - POPOVER_DIST
                            });
                        }
                        else {
                            this.$popover.css({
                                left: bnd.left,
                                top: bnd.top + bnd.height + POPOVER_DIST
                            });
                        }
                    }
                }
                else {
                    this.hide();
                }
            }
        };
        HintPopover.prototype.show = function () {
            this.$popover.show();
        };
        HintPopover.prototype.hide = function () {
            this.$popover.hide();
        };
        return HintPopover;
    }());
    exports["default"] = HintPopover;

    },'src/js/base/module/HintPopover.js',{"jquery":"/Users/lqez/Dev/summernote/node_modules/jquery/dist/jquery.js","../core/func":"/Users/lqez/Dev/summernote/src/js/base/core/func.js","../core/lists":"/Users/lqez/Dev/summernote/src/js/base/core/lists.js","../core/dom":"/Users/lqez/Dev/summernote/src/js/base/core/dom.js","../core/range":"/Users/lqez/Dev/summernote/src/js/base/core/range.js","../core/key":"/Users/lqez/Dev/summernote/src/js/base/core/key.js"}];})(this);
    //# sourceMappingURL=data:application/json;charset=utf-8;base64,