Building a Custom Module

Quill作为编辑器的核心优势在于其丰富的API和强大的定制功能。 当您在Quill API的基础上实现功能时,将其组织为模块可能会很方便。出于本指南的目的,我们将介绍一种构建字计数器模块的方法,这是许多文字处理器中常见的功能。

注意:内部模块是Quill的功能组织的多少。您可以通过实现自己的默认模块并使用相同的名称注册它们来覆盖这些默认模块

Counting Words

单词计数器的核心只是计算编辑器中的单词数,并在某些UI中显示该值。 因此我们需要:

  1. 在Quill中监听文本更改。

  2. 计算单词数量。

  3. 显示此值。

让我们直接进入一个完整的例子吧!

  1. // Implement and register module
  2. Quill.register('modules/counter', function(quill, options) {
  3. var container = document.querySelector('#counter');
  4. quill.on('text-change', function() {
  5. var text = quill.getText();
  6. // There are a couple issues with counting words
  7. // this way but we'll fix these later
  8. container.innerText = text.split(/\s+/).length;
  9. });
  10. });
  11. // We can now initialize Quill with something like this:
  12. var quill = new Quill('#editor', {
  13. modules: {
  14. counter: true
  15. }
  16. });

注:原文这里是一个 codepen 的视图,我们可以直接通过这个链接过去。

这就是为Quill添加自定义模块所需的全部内容!函数可以被注册为模块,并且这个函数会被注入Quill对象以及相关联的option。

译者注:
这个options,指的是你在注册模块的时候自己那个模块里面自定义的一下变量,如下代码

  1. var quill = new Quill('#editor', {
  2. modules: {
  3. counter: {
  4. container: '#counter',
  5. unit: 'word'
  6. }
  7. }
  8. });

这里的options就是这个对象:

  1. {
  2. container: '#counter',
  3. unit: 'word'
  4. }

可以看codepen的那个代码,就知道是什么意思了,可以在codepen的页面下方开启console。

Using Options

模块被传递一个option对象,该对象可以用来微调所需的行为。我们可以用它来接受计数器容器的选择器,而不是硬编码的字符串。我们还可以自定义计数器来计数单词或字符:

  1. Quill.register('modules/counter', function(quill, options) {
  2. var container = document.querySelector(options.container);
  3. quill.on('text-change', function() {
  4. var text = quill.getText();
  5. if (options.unit === 'word') {
  6. container.innerText = text.split(/\s+/).length + ' words';
  7. } else {
  8. container.innerText = text.length + ' characters';
  9. }
  10. });
  11. });
  12. var quill = new Quill('#editor', {
  13. modules: {
  14. counter: {
  15. container: '#counter',
  16. unit: 'word'
  17. }
  18. }
  19. });

注:原文这里是一个 codepen 的视图,我们可以直接通过这个链接过去。

Constructors

由于任何函数都可以注册为Quill模块,我们可以将我们的计数器实现为ES5构造函数或ES6类。 这允许我们直接访问和使用模块。

  1. var Counter = function(quill, options) {
  2. this.quill = quill;
  3. this.options = options;
  4. var container = document.querySelector(options.container);
  5. var _this = this;
  6. quill.on('text-change', function() {
  7. var length = _this.calculate();
  8. container.innerText = length + ' ' + options.unit + 's';
  9. });
  10. };
  11. Counter.prototype.calculate = function() {
  12. var text = this.quill.getText();
  13. if (this.options.unit === 'word') {
  14. return text.split(/\s+/).length;
  15. } else {
  16. return text.length;
  17. }
  18. };
  19. Quill.register('modules/counter', Counter);
  20. var quill = new Quill('#editor', {
  21. modules: {
  22. counter: {
  23. container: '#counter',
  24. unit: 'word'
  25. }
  26. }
  27. });
  28. var counter = quill.getModule('counter');
  29. // We can now access calculate() directly
  30. console.log(counter.calculate(), 'words');

注:原文这里是一个 codepen 的视图,我们可以直接通过这个链接过去。

Wrapping It All Up

现在让我们抛光ES6中的模块并修复一些讨厌的错误。 这里的所有都是它的!(译者注:这是一段ES6的语法,需要babel)

  1. class Counter {
  2. constructor(quill, options) {
  3. this.quill = quill;
  4. this.options = options;
  5. this.container = document.querySelector(options.container);
  6. quill.on('text-change', this.update.bind(this));
  7. this.update(); // Account for initial contents
  8. }
  9. calculate() {
  10. let text = this.quill.getText();
  11. if (this.options.unit === 'word') {
  12. text = text.trim();
  13. // Splitting empty text returns a non-empty array
  14. return text.length > 0 ? text.split(/\s+/).length : 0;
  15. } else {
  16. return text.length;
  17. }
  18. }
  19. update() {
  20. var length = this.calculate();
  21. var label = this.options.unit;
  22. if (length !== 1) {
  23. label += 's';
  24. }
  25. this.container.innerText = length + ' ' + label;
  26. }
  27. }
  28. Quill.register('modules/counter', Counter);
  29. var quill = new Quill('#editor', {
  30. modules: {
  31. counter: {
  32. container: '#counter',
  33. unit: 'word'
  34. }
  35. }
  36. });

注:原文这里是一个 codepen 的视图,我们可以直接通过这个链接过去。