所以我不得不为一个asignmment制作一个Quill界面。必须有一个单词的计数器,但我发现我还需要一个所有字符的计数器。那么将这个字符计数器添加到我的项目中的最佳方法是什么。
class Counter {
constructor(quill, options) {
this.quill = quill;
this.options = options;
this.container = document.querySelector(options.container);
quill.on('text-change', this.update.bind(this));
this.update(); // Account for initial contents
}
calculate() {
let text = this.quill.getText();
if (this.options.unit === 'word') {
text = text.trim();
// Splitting empty text returns a non-empty array
return text.length > 0 ? text.split(/\s+/).length : 0;
} else {
return text.length;
}
}
update() {
var length = this.calculate();
var label = this.options.unit;
if (length !== 1) {
label += 's';
}
this.container.innerText = length + ' ' + label;
}
}
Quill.register('modules/counter', Counter);
var quill = new Quill('#editor', {
modules: {
toolbar: toolbarOptions,
counter: {
container: '#counter',
unit: 'word'
}
},
theme: 'snow'
});
该指南有一个编写模块的教程,该模块根据配置选项计算单词或字符:https://quilljs.com/guides/building-a-custom-module/#using-options
var quill = new Quill('#editor', {
modules: {
counter: {
container: '#counter',
unit: 'character'
}
}
});
但是:Kua zxsw指出