Bootstrap-3-Typeahead icon indicating copy to clipboard operation
Bootstrap-3-Typeahead copied to clipboard

Typeahead on content-editable div

Open vkocjancic opened this issue 7 years ago • 1 comments

Hi.

I have a div where users can alter div content. This is done with help of contenteditable attribute. I implemented typeahead on insert and it works fine, unless:

  • you decide to select text and delete it. After that it does not work anymore
  • if you have already selected some value from typeahead it will not work even if you delete value.

vkocjancic avatar Jul 08 '17 21:07 vkocjancic

You can modify the following code to solve this problem. (bootstrap3-typeahead.js At line 563)

  input: function (e) {
            if(this.$element[0].tagName === 'DIV'){
                 this.$element.val(this.$element.text());
            }
            var currentValue = this.$element.val() || this.$element.text();
            if (this.value !== currentValue) {
                this.value = currentValue;
                this.lookup();
            }
        },

and at the line 77 (Focus moves to the end)

    select: function () {
      var val = this.$menu.find('.active').data('value');
      this.$element.data('active', val);
      if (this.autoSelect || val) {
        var newVal = this.updater(val);
        // Updater can be set to any random functions via "options" parameter in constructor above.
        // Add null check for cases when updater returns void or undefined.
        if (!newVal) {
          newVal = '';
        }
        this.$element
          .val(this.displayText(newVal) || newVal)
          .text(this.displayText(newVal) || newVal)
          .change();

          var textbox = this.$element[0];
          var sel = window.getSelection();
          var range = document.createRange();
          range.selectNodeContents(textbox);
          range.collapse(false);
          sel.removeAllRanges();
          sel.addRange(range);

        this.afterSelect(newVal);
      }
      return this.hide();
    },

trent8464 avatar Mar 14 '19 11:03 trent8464