如何使用工具栏选项在Quill js上添加字体类型?

时间:2017-05-01 23:51:40

标签: javascript jquery fonts quill

我使用Quill js创建了一个富文本区域。我有工具栏的下一个选项:



new Quill('#quilljs-container', {
    modules: {
        toolbar: [
           ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
           ['blockquote', 'code-block', 'link'],

           [{ '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

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

           ['clean']                                         // remove formatting button
       ]
    },
    theme: 'snow'
});

<!-- Style -->
<link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet">

<!-- quill js container -->
<div id="quilljs-container">  </div>

<!-- Add quill js on the project -->
<script src="https://cdn.quilljs.com/1.0.0/quill.js"></script>
&#13;
&#13;
&#13;

目前,当我尝试添加更多类型时,编辑&#39;字体&#39;工具栏上的选项(例如&#39; font&#39;:[&#39; arial&#39;]),选择选项只显示&#34; Sans Serif&#34;而不是显示&#34; Arial&#34;选项。我所看到的是默认选项(&#34; Sans Serif&#34;,&#34; Serif&#34;,&#34; Monospace&#34;)以及我想要添加的自定义选项。

另外,我已经尝试了文档中显示的customizing attributors,但是使用我当前的配置,它只显示默认选项。也许我错过了什么。

我希望我对自己有疑问,有人可以帮助我。

更新:

为了更清楚一点,我想在 Quill Container configuration

之后添加更多字体
  

容器:在最简单的级别,工具栏控件可以通过简单的格式名称数组指定。

5 个答案:

答案 0 :(得分:12)

这就是你需要的。

这个过程就像你需要 4个组件

  1. selectql-font标记。这将包含新的字体选项。
  2. 将新字体添加到白名单。必须在调用Javascript中的Quill构造函数之前定义它。
  3. 在下拉列表中为每个font-family定义label。示例:font-family: "Inconsolata"
  4. 定义将在文本区域中使用的内容字体系列。按照第3个组件中的示例:.ql-font-inconsolata { font-family: "Inconsolata";}
  5. <强>更新

    我阅读文档是为了帮助您,他们提到

      

    在最简单的级别,工具栏控件可以通过一个简单的格式名称数组来指定......

    或者,您可以通过将 DOM元素选择器传递到Quill中,在 HTML 中手动创建工具栏;这就是这个答案中提出的解决方案。另一方面,文档没有提到但是在尝试了很多方法使用数组加载数据之后(没有任何成功),我注意到这是不可能的。所以,这是我的贡献以及我发布更新的原因。我为手动实现做了等价(JS和HTML)。

    可以使用 HTML JS构造函数创建自定义工具栏。构造函数将在#toolbar-container部分中以toolbar作为modules收到。

    然后,您可以仅使用HTML标记生成相同的结构。这个概念非常相似。例如:

    • 如果在 JS 中我们声明了一个这样的数组: HTML 中的['bold', 'italic', 'underline', 'strike']将是:

      <span class="ql-formats"> <button class="ql-bold"></button> <button class="ql-italic"></button> <button class="ql-underline"></button> <button class="ql-strike"></button> </span>

    • JS HTML 中的[{ 'header': 1 }, { 'header': 2 }]

      <span class="ql-formats"> <button class="ql-header" value="1"></button> <button class="ql-header" value="2"></button> </span>

    所以,这里有一个完整的示例代码片段:

    // Add fonts to whitelist
    var Font = Quill.import('formats/font');
    // We do not add Sans Serif since it is the default
    Font.whitelist = ['inconsolata', 'roboto', 'mirza', 'arial'];
    Quill.register(Font, true);
    
    
    // We can now initialize Quill with something like this:
    var quillObj = new Quill('#quilljs-container', {
      modules: {
        toolbar: '#toolbar-container'
      },
      placeholder: 'This is a font test...',
      theme: 'snow'
    });
    <!-- Style -->
    <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
    <style>
      /* Set dropdown font-families */
      
      #toolbar-container .ql-font span[data-label="Sans Serif"]::before {
        font-family: "Sans Serif";
      }
      
      #toolbar-container .ql-font span[data-label="Inconsolata"]::before {
        font-family: "Inconsolata";
      }
      
      #toolbar-container .ql-font span[data-label="Roboto"]::before {
        font-family: "Roboto";
      }
      
      #toolbar-container .ql-font span[data-label="Mirza"]::before {
        font-family: "Mirza";
      }
      
      #toolbar-container .ql-font span[data-label="Arial"]::before {
        font-family: "Arial";
      }
      /* Set content font-families */
      
      .ql-font-inconsolata {
        font-family: "Inconsolata";
      }
      
      .ql-font-roboto {
        font-family: "Roboto";
      }
      
      .ql-font-mirza {
        font-family: "Mirza";
      }
      
      .ql-font-arial {
        font-family: "Arial";
      }
      /* We do not set Sans Serif since it is the default font */
    </style>
    <link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet">
    
    
    <div id="standalone-container">
      <div id="toolbar-container">
        <span class="ql-formats">
          <select class="ql-font">
            <option selected>Sans Serif</option>
            <option value="inconsolata">Inconsolata</option>
            <option value="roboto">Roboto</option>
            <option value="mirza">Mirza</option>
            <option value="arial">Arial</option>
          </select>
          <select class="ql-size"></select>
        </span>
        <span class="ql-formats">
          <button class="ql-bold"></button>
          <button class="ql-italic"></button>
          <button class="ql-underline"></button>
          <button class="ql-strike"></button>
        </span>
        <span class="ql-formats">
          <select class="ql-color"></select>
          <select class="ql-background"></select>
        </span>
        <span class="ql-formats">
          <button class="ql-blockquote"></button>
          <button class="ql-code-block"></button>
          <button class="ql-link"></button>
        </span>
        <span class="ql-formats">
          <button class="ql-header" value="1"></button>
          <button class="ql-header" value="2"></button>
        </span>
        <span class="ql-formats">
          <button class="ql-list" value="ordered"></button>
          <button class="ql-list" value="bullet"></button>
          <button class="ql-indent" value="-1"></button>
          <button class="ql-indent" value="+1"></button>
        </span>
        <span class="ql-formats">
          <button class="ql-direction" value="rtl"></button>
          <select class="ql-align"></select>
        </span>
        <span class="ql-formats">
          <button class="ql-script" value="sub"></button>
          <button class="ql-script" value="super"></button>
        </span>
        <span class="ql-formats">
          <button class="ql-clean"></button>
        </span>
      </div>
    </div>
    <!-- quill js container -->
    <div id="quilljs-container"></div>
    <!-- Add quill js on the project -->
    <script src="https://cdn.quilljs.com/1.0.0/quill.js"></script>

答案 1 :(得分:3)

可以使用JavaScript构造函数创建自定义字体选择。

Quill Quickstart page开始,这是基本的HTML-

<!-- Include stylesheet -->
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">

<!-- Create the editor container -->
<div id="editor">
  <p>Hello World!</p>
  <p>Some initial <strong>bold</strong> text</p>
  <p><br></p>
</div>

<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>

JavaScript初始化程序中所需的代码如下:

let Font = Quill.import('formats/font');
Font.whitelist = ['times-new-roman', 'arial'];
Quill.register(Font, true);

let toolbarOptions = [
    [{ 'font': ['', 'times-new-roman', 'arial'] }],

    ['clean']                                         // remove formatting button
];

let quill = new Quill('#editor', {
    modules: {
        toolbar: toolbarOptions
      },
    theme: 'snow'
    });

您还需要在样式表中添加以下内容,其中CSS选择器中的名称就是上述JavaScript中的名称:

/* Set droplist names - -item is the currently selected font, -label is the font's appearance in the droplist*/
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value='']::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value='']::before
{
  content: 'Default';
}

.ql-snow .ql-picker.ql-font .ql-picker-label[data-value='times-new-roman']::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value='times-new-roman']::before
{
  content: 'Times New Roman';
  font-family: 'Times New Roman';
}

.ql-snow .ql-picker.ql-font .ql-picker-label[data-value='arial']::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value='arial']::before
{
  content: 'Arial';
  font-family: 'Arial';
}

/****************************************************
Set the font-family content used for the HTML content.
*****************************************************/
.ql-font-arial {
  font-family: 'Arial';
}

.ql-font-times-new-roman {
  font-family: 'Times New Roman';
}

答案 2 :(得分:3)

必须对@Thomas的答案进行一些更新,但是下面的代码可以在Quill v1.3.6中很好地实现自动化。如果您有一小段字体,那么所需的CSS太简单了;但是如果您有更大和/或动态的列表,则需要按以下步骤进行自动化:

// Specify Quill fonts
var fontList = ['Arial', 'Courier', 'Garamond', 'Tahoma', 'Times New Roman', 'Verdana'];
var fontNames = fontList.map(font => getFontName(font));
var fonts = Quill.import('attributors/class/font');
fonts.whitelist = fontNames;
Quill.register(fonts, true);

// Add fonts to CSS style
var fontStyles = "";
fontList.forEach(function(font) {
    var fontName = getFontName(font);
    fontStyles += ".ql-snow .ql-picker.ql-font .ql-picker-label[data-value=" + fontName + "]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value=" + fontName + "]::before {" +
        "content: '" + font + "';" +
        "font-family: '" + font + "', sans-serif;" +
        "}" +
        ".ql-font-" + fontName + "{" +
        " font-family: '" + font + "', sans-serif;" +
        "}";
});

// Configure Quill editor options
var toolbarOptions = [
    [{ 'font': fonts.whitelist }],
    ['bold', 'italic', 'underline'],
    ['clean'] 
];

var quill;
$(function() {
    // Append the CSS stylesheet to the page
    var node = document.createElement('style');
    node.innerHTML = fontStyles;
    document.body.appendChild(node);

    quill = new Quill('#editor', {
    theme: 'snow',
    modules: {
        toolbar: toolbarOptions
    } 
    });
});

// Generate code-friendly font names
function getFontName(font) {
    return font.toLowerCase().replace(/\s/g, "-");
}

与Quill文档相关的链接为https://quilljs.com/guides/how-to-customize-quill/#customizing-attributors,尽管此处提供的示例并不完整。

答案 3 :(得分:1)

可接受的答案通常很棒,但要注意的是,formats/font实际上是错误的,不适用于经过测试的 Quill 1.3.7

对我有用的是:

let fonts = Quill.import("formats/font");

let fonts = Quill.import("attributors/style/font");
fonts.whitelist = ["initial", "sans-serif", "serif", "monospace"];
Quill.register(fonts, true);

答案 4 :(得分:0)

FWIW,我从Steve B复制了代码,并使代码更通用。这样,您无需进行所有CSS规则的复制粘贴等操作……只需在字体数组中指定要添加的字体即可。

// specify the fonts you would 
var fonts = ['Arial', 'Courier', 'Garamond', 'Tahoma', 'Times New Roman', 'Verdana'];
// generate code friendly names
function getFontName(font) {
    return font.toLowerCase().replace(/\s/g, "-");
}
var fontNames = fonts.map(font => getFontName(font));
// add fonts to style
var fontStyles = "";
fonts.forEach(function(font) {
    var fontName = getFontName(font);
    fontStyles += ".ql-snow .ql-picker.ql-font .ql-picker-label[data-value=" + fontName + "]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value=" + fontName + "]::before {" +
        "content: '" + font + "';" +
        "font-family: '" + font + "', sans-serif;" +
        "}" +
        ".ql-font-" + fontName + "{" +
        " font-family: '" + font + "', sans-serif;" +
        "}";
});
var node = document.createElement('style');
node.innerHTML = fontStyles;
document.body.appendChild(node);

var toolbarOptions = [
    ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
    ['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': fontNames }],
    [{ 'align': [] }],

    ['clean']                                         // remove formatting button
];

// Add fonts to whitelist
var Font = Quill.import('formats/font');
Font.whitelist = fontNames;
Quill.register(Font, true);

var quill = new Quill('#editor', {
    modules: {
        toolbar: toolbarOptions
    },
    theme: 'snow'
});