无法在QuillJS编辑器中获取自定义下拉列表

时间:2017-10-26 19:51:21

标签: javascript rich-text-editor quill

说明:我在“Quill Editor”工具栏中创建下拉列表时遇到问题。任何帮助,将不胜感激。理想情况下,我希望下拉列表显示在工具栏中,并在编辑器中将选择选项文本添加为​​文本。

测试用例:https://codepen.io/Graphettion/pen/OxezbO

HTML

<div id="editor-toolbar">
  <select class="ql-emailVars">
    <option value="1">Account Url</option>
    <option value="2">First Name</option>
    <option value="3">Login</option>
    <option value="4">Org Name</option>
    <option value="5">Support Email</option>
  </select>
</div>
<div id="editor"></div>
<div class="text-output"></div>
<div class="html-output"></div>

JS

const quill = new Quill('#editor', {
  modules: {
    toolbar: {
      container: "#editor-toolbar",
        handlers: {
          "emailVars": emailVars
        }
    }
  },
  theme: 'snow'
});

// Add Custom Dropdown to Toolbar
function emailVars(args) {
  const value = args[0];
  const cursorPosition = this.quill.getSelection().index
  this.quill.insertText(cursorPosition, value)
  this.quill.setSelection(cursorPosition + value.length)
}

谢谢,

2 个答案:

答案 0 :(得分:0)

我必须添加一些CSS才能在工具栏上显示它,而某些JS则将其插入到文本编辑器中。

CSS

.ql-picker.ql-emailVars {
  width: 120px;
}

.ql-picker.ql-emailVars .ql-picker-item::before, 
.ql-picker.ql-emailVars .ql-picker-label::before {
  content: 'Custom'
}

.ql-picker.ql-emailVars [data-value="1"]::before {
  content: 'Account Url'
}

.ql-picker.ql-emailVars [data-value="2"]::before {
  content: 'First Name'
}

.ql-picker.ql-emailVars [data-value="3"]::before {
  content: 'Login'
}

.ql-picker.ql-emailVars [data-value="4"]::before {
  content: 'Org Name'
}

.ql-picker.ql-emailVars [data-value="5"]::before {
  content: 'Support Email'
}

JS

const quill = new Quill('#editor', {
  modules: {
    toolbar: {
      container: "#editor-toolbar",
      handlers: {
        "emailVars": emailVars
      }
    }
  },
  theme: 'snow'
});

// Add Custom Dropdown to Toolbar
function emailVars(args) {
  const value = args[0]
  const cursorPosition = this.quill.getSelection().index
  if (value == 1) {
    this.quill.insertText(cursorPosition, "{AccountURL}")
  } else if (value == 2) {
    this.quill.insertText(cursorPosition, "{FirstName}")
  } else if (value == 3) {
    this.quill.insertText(cursorPosition, "{Login}")
  } else if (value == 4) {
    this.quill.insertText(cursorPosition, "{OrganizationName}")
  } else if (value == 5) {
    this.quill.insertText(cursorPosition, "{SupportEmail}")
  } else {
    this.quill.insertText(cursorPosition, "Please add an email variable.")
  }

  this.quill.setSelection(cursorPosition + value.length)
}

https://codepen.io/Graphettion/pen/OxezbO

答案 1 :(得分:0)

我也有这个麻烦。不幸的是,Quill中有一个硬代码。只有默认的工具栏选择具有CSS规则,用于显示void addToBitsAtPosition(uint8_t location, uint64_t *bits, uint8_t to_be_added) { union { uint64_t u64; uint8_t u8[8]; } t; t.u64 = *bits; t.u8[location] = to_be_added; *bits = t.u64; }

的文本
options

但是最好的方法是为所有自定义选择添加此.ql-snow .ql-picker.ql-font .ql-picker-item[data-label]:not([data-label=''])::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-label]:not([data-label=''])::before { content: attr(data-label); } ,因为在我的情况下content: attr(data-label);设置正确,并且可以从CSS data-label进行访问