通过选择器将 tinyMCE 连接到您的 jsf 组件
在下面的示例中,您加载了一个包含函数“loadMyEditor()”的自己的 javascript 文件“loadhtmleditor.js”。这个函数在body onload中调用。在这个 javascript 函数中,您通过选择器而不是通过模式将 tinyMCE 连接到您的 jsf 组件。在选择器参数中,您编写要连接的组件的 html-id。请注意,在此参数中,由 jsf 生成的 html-id 中的 ':' 分隔符必须用两个反斜杠屏蔽!在此示例中:选择器:“#myForm\\:myHtmlText”
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<script src="resources/tiny_mce/tiny_mce.js" type="text/javascript"></script>
<script src="resources/loadhtmleditor.js" type="text/javascript"></script>
</h:head>
<h:body onload="loadMyEditor();">
<h:form id="myForm">
<h:inputTextarea id="myHtmlText" value="#{bean.htmlText}">
</h:inputTextarea>
</h:form>
</h:body>
</html>
加载htmleditor.js:
function loadMyEditor()
{
tinyMCE.init({
// General options
selector : "#myForm\\:myHtmlText",
theme : "advanced",
plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
// Theme options
theme_advanced_buttons1 :"bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,|,bullist,numlist,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,preview,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,styleprops,spellchecker,|,insertfile,insertimage",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
// Skin options
skin : "o2k7",
skin_variant : "silver",
content_css : "css/example.css",
});
}
这应该足以将您的 jsf bean 绑定到 tinyMCE!
补充:
如果您想在用户更改 html 内容时通过 ajax 做出反应,您可以像这样扩展 tinyMCE 编辑器的创建:
加载htmleditor.js:
function loadMyEditor()
{
tinyMCE.init({
// General options
selector : "#myForm\\:myHtmlText",
theme : "advanced",
plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
// Theme options
theme_advanced_buttons1 :"bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,|,bullist,numlist,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,preview,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,styleprops,spellchecker,|,insertfile,insertimage",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
// Skin options
skin : "o2k7",
skin_variant : "silver",
init_instance_callback: function (editor)
{
editor.on('Change', function(e){myFunctionEditorChanged(editor);});
editor.on('Paste', function(e){myFunctionEditorPasted(editor);});
},
content_css : "css/example.css"
});
}
function myFunctionEditorChanged(editor)
{
tinyMCE.triggerSave(); // to be shure, that the changed content is in your jsf-component...
// do something to trigger an ajax request handeling the changed html content.
}
function myFunctionEditorPasted(editor)
{
tinyMCE.triggerSave(); // to be shure, that the changed content is in your jsf-component...
// do something to trigger an ajax request handeling the changed html content.
}
通过这种方式,您可以对用户通过键盘或粘贴内容输入到 html 编辑器的所有更改做出反应。当用户通过 tinyMCE 的菜单更改 html 内容时,我没有找到获取通知的方法。(例如,当用户将某些单词更改为粗体时......)