Add fonts in Quill editor Angular 2+

2k views Asked by At

I am using quill editor in angular I am unable to add multiple fonts in angular 2+ I am using following code snippet

QuillModule.forRoot({
      modules: {
        toolbar: [
          ['blockquote', 'code-block'],

          // [{ 'header': 1 }, { 'header': 2 }],               // custom button values
          [{ 'list': 'ordered' }, { 'list': 'bullet' }],
          // [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
          [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
          [{ 'direction': 'rtl' }],                         // text direction

          [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
          [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

          [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
          [{ 'font': [] }],
          [{ 'align': [] }],

          ['clean'],                                         // remove formatting button

          ['link', 'image']                         // link and image, video
        ]
      },

    })

when ever I add fonts in fonts list I get same font all the time. I followed this link as well but its not for angular I suppose How to add font types on Quill js with toolbar options?

can anyone help me?

2

There are 2 answers

0
Kavinda Senarathne On

A select tag with a ql-font class. This will contain the new font options.Add the new fonts to the whitelist. This has to be defined before you call the Quill constructor in Javascript.Define the font-family for each label in the dropdown. Example:

font-family: "Inconsolata"

Define the content font-families that will be used in the text area. Following the example in the 3rd component: .ql-font-inconsolata { font-family: "Inconsolata";}

1
Abolfazl Almas On

I solved my problem like this.

First, you must define your fonts in the whitelist.

import Quill from 'quill';

const FontAttributor = Quill.import('attributors/class/font');
FontAttributor.whitelist = [
  'IRANSans',
  'roboto',
  'cursive',
  'fantasy',
  'monospace'
];
Quill.register(FontAttributor, true);

Then you have to add the fonts to the config inside your component.

quillConfig: QuillModules = {
    toolbar: {
      container: [
        ['bold', 'italic', 'underline', 'strike'],
        [{ size: ['small', false, 'large', 'huge'] }],
        [{ font: ['IRANSans', 'roboto', 'cursive', 'fantasy', 'monospace'] }],
        [{ align: [] }],
        ['clean'],
        ['link'],
      ]
    }
  };

By applying these changes, your fonts will be added to the list of fonts and will get the data-value attribute.

enter image description here

After that, you need to add a series of styles to the app.

.ql-snow .ql-picker-options .ql-picker-item {
  font-family: #{attr(data-value)};
  &::before {
    content: attr(data-value) !important;
  }
}

.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
  content: attr(data-value) !important;
}

Finally, to apply the font-family on the text inside the editor, you can use sass,sccs like this.

$text-editor-fonts: 'IRANSans', 'roboto', 'cursive', 'fantasy', 'monospace';

@each $text-editor-font in $text-editor-fonts {
  .ql-font-#{$text-editor-font} {
    font-family: $text-editor-font;
  }
}