gpt4 book ai didi

javascript - 无法让自定义下拉菜单在 QuillJS 编辑器中工作

转载 作者:行者123 更新时间:2023-12-03 03:10:45 27 4
gpt4 key购买 nike

描述:我在 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)
}

谢谢,

最佳答案

我必须添加一些 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

关于javascript - 无法让自定义下拉菜单在 QuillJS 编辑器中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46962683/

27 4 0