Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a button that adds a string with HTML tags and classes?

Based on this link, I tried to add a new button, which allows me to have a string inserted at cursor position. It works so far, however I had to change this line:

this.quill.insertText(cursorPosition, "★");

to:

this.quill.clipboard.dangerouslyPasteHTML(cursorPosition, '<i>★</i>');

as I wanted to be able to paste HTML tag with the string.

Now, in the next step, I want to add a class with the HTML tag:

this.quill.clipboard.dangerouslyPasteHTML(cursorPosition, '<i class="my-icon">★</i>');

However, whenever I click the button now, the class is just completely discarded. In fact, instead of the tag i, I get the tag em, which works for what I want to do (italics), but it's still annoying.

How can I make sure the tags and class stay the same when I click the button? All I want to do is to click a button and have a little string enclosed in a span with a class to appear in the editor. How can I achieve this?

like image 260
noClue Avatar asked Apr 16 '18 10:04

noClue


People also ask

How to add text in button HTML?

Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.). That is not possible with a button created with the <input> element! Tip: Always specify the type attribute for a <button> element, to tell browsers what type of button it is.


1 Answers

Update: 24-Apr-18

So it took a lot of time to actually figure this out and get it working. It is possible to do this by create your own Blot and overriding the italic blot

let Inline = Quill.import('blots/inline');

class Icon extends Inline {
  static create(value) {
    let node = super.create(value);
    if (value) {
      console.log(value)
      node.setAttribute('class', value);
    }
    return node;
  }

  static formats(domNode) {
    console.log(domNode)
    return domNode.getAttribute("class");
  }

  format(name, value) {
    console.log(name, value)
    if (name !== this.statics.blotName || !value) return super.format(name, value);
    if (value){
      this.domNode.setAttribute('class', value);
    }
  }
}
Icon.blotName = 'icon';
Icon.tagName = 'span';
Quill.register(Icon);

var BackgroundClass = Quill.import('attributors/class/background');
var ColorClass = Quill.import('attributors/class/color');
var SizeStyle = Quill.import('attributors/style/size');
Quill.register(BackgroundClass, true);
Quill.register(ColorClass, true);
Quill.register(SizeStyle, true);


var quill = new Quill('#editor-container', {
  modules: {
    toolbar: '#toolbar-container'
  },
  placeholder: 'Compose an epic...',
  theme: 'snow'
});


var customButton = document.querySelector('#custom-button');
customButton.addEventListener('click', function() {
  quill.insertText(quill.getSelection().index, "★\n", 'icon', 'fa fa-icon fa-icon-green');
});

Here is a codepen for the same

Working

Original Answer

QuillJS is rich text editor and not a HTML WYSIWYG editor. So you cannot get your own custom html inside the same

https://codepen.io/anon/pen/ZyEjrQ

In the above code pen, click source, try your code and click source again

Source view

Source view

Source view -> UI View -> Source view

Converted source view

If you try to remove the clipboard matchers, then you will nearly break the rich text functionality.

So either you leave this requirement or you pick a different editor for your job. Some threads you should look at

https://github.com/quilljs/quill/issues/1856

https://github.com/quilljs/quill/issues/1733

https://github.com/quilljs/quill/issues/1657#issuecomment-325737074

like image 55
Tarun Lalwani Avatar answered Sep 20 '22 21:09

Tarun Lalwani