【发布时间】:2020-05-19 15:28:32
【问题描述】:
我正在使用 PrimeFaces 7 及其 TextEditor 组件,该组件在内部使用免费和开源的编辑器 Quill
我需要添加一个自定义HTML按钮,在选择时,将其插入在TextEditor中的游标的当前位置上选择的单词 - (在Quill)
中可以在 Quill 编辑器中添加自定义按钮并将 EventListener 附加到它们,如下所示:
https://quilljs.com/docs/modules/toolbar/
(请看上面页面的这部分):
var customButton = document.querySelector('#custom-button');
customButton.addEventListener('click', function() {
console.log('Clicked!');
});
还有一个由 Quill 编辑器提供的 API,用于在给定位置插入文本,请参见此处:
https://quilljs.com/docs/api/#content
看看这个方法:
`quill.insertText(0, 'Hello', 'bold', true);`
但是,我确实错过了一些事情:
1.) 自定义按钮的定义应在工具栏div中完成,如下代码所示:
`<div id="toolbar">
<!-- But you can also add your own -->
<button id="custom-button"></button>`
但是:当使用现成的 PrimeFaces 文本编辑器组件时,我该怎么做呢?
我在本地试过这个:
jQuery(document).ready(function () {
jQuery(document).ready(function () {
var customButton = document.getElementById("resultsFormId:quillToolbarId_toolbar:custButId");
if (customButton!=null){
return;
}
customButton = document.createElement("button");
customButton.innerHTML = 'Params';
customButton.id="resultsFormId:quillToolbarId_toolbar:custButId";
var qlTollbar = document.getElementById("resultsFormId:quillToolbarId_toolbar");
qlTollbar.appendChild(customButton);
customButton.addEventListener('click', function() {
Quill.insertText(0, 'Hello', 'bold', true);
console.log('Clicked!');
});
});
});
并且可以说:
1.1) 自定义按钮被插入到 Quill 工具栏上(不是很好和样式,但它在那里)
1.2) 当我单击自定义按钮时,首先会执行 EventListener。这没关系,但在这里:
Quill.insertText(0, 'Hello', 'bold', true);
我需要引用表示 Quill 编辑器的 js 对象,而不是 Quill.insertText()。 === > 你能帮忙吗?
1.3) 在 Point (1.2) 的 EventListener 被执行后,我的整个代码在
jQuery(document).ready(function () {
jQuery(document).ready(function () {
再次执行,未找到customButton的id,并再次重新创建。 ===> 我可以避免吗?
2.) 在特定位置插入文本的代码中,我需要在用户单击(选择自定义按钮上的选项)之前获取光标的最后一个位置。
我该怎么做?
【问题讨论】:
-
我认为您需要提出功能请求或提交类似primefaces.github.io/primefaces/8_0/#/components/… 的 PR
-
或者检查覆盖一些javascript文件是否有效...
-
@Kukeltje 我做了一个小测试,将我的结果放在上面的问题中。你能帮我点(1.2)和(1.3)吗(见我上面编辑过的问题)谢谢!
-
您是否阅读了精美的手册?关于定制的 PrimeFaces (展示和文档)?至少在技术上添加自定义按钮的问题的一部分,使大量的 javascript 变得多余。而且您在此处有效地添加了多个问题,难以发表评论。 1.2 1.3 和 2 不会因为这个而被我解决,因为它们纯粹是 Quill 并且与 PF 无关。最好分成多个,但让我们先关注 1.1。这可以更简单地完成。通过添加相关类等,样式可能由您决定......
-
加两次
jQuery(document).ready(function ()有什么意义?这不是你双触发的问题吗?
标签: javascript primefaces quill