您的位置:首页 > Web前端 > JQuery

Tinymce、smarty 、jquery的融合及首行缩进功能的插件实现

2009-01-15 18:32 453 查看

引入方式

在系统中引入的方式为js引入方式。

//jquery基类引入
<script language="javascript" src="http://{$smarty.server.SERVER_NAME}/webroot/js/jquery.js"></script>
//jquery日历插件引入
<script language="javascript" src="http://{$smarty.server.SERVER_NAME}/webroot/js/cal/ui.datepicker.js"></script>
//jquery文件上传插件引入
<script language="javascript" src="http://{$smarty.server.SERVER_NAME}/webroot/js/ajaxfileupload.js"></script>
//tinymce引入
<script language="javascript" src="http://{$smarty.server.SERVER_NAME}/webroot/js/tiny_mce/tiny_mce.js"></script>

和其他js控件一样引入方式一样,都是用js引入方式实现的。

tinymce的初始化

1. js初始化

<!--smarty的不执行标记//->
{literal}
<script language="javascript">
tinyMCE.init({
// General options
debug : false,//错误报告开关
mode : "exact",//用特定的模式
elements : "webeditor_demo1",//容器的id值,将来要在页面中替换的
theme : "advanced",//皮肤
//加载的插件列表
plugins : "safari,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,stageindent,example",

//Language package
language : "zh",//语言设定

// 操作框上的功能显示
theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,stageindent,|,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,example",
//样式设定
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,

// Example content CSS (should be your site CSS)
content_css : "css/content.css",

//config the link to blank
//valid_elements :"a[href|target=_blank]",

// Drop lists for link/image/media/template dialogs
template_external_list_url : "lists/template_list.js",
external_link_list_url : "lists/link_list.js",
external_image_list_url : "lists/image_list.js",
media_external_list_url : "lists/media_list.js"
});
</script>
{/literal}
tinymce的初始化要和jquery的引入的位置一致,而不是写在jquery的$(document).ready(..)里写的。 一定要用exact模式,默认模式下,是将tinymce显示在每个textarea中,定义了id之后就不管是什么标签了,如div,span 都可以。

2. 页面内的初始化
html代码
<form action="./interface/responsetinymce" method="post">
<div id="webeditor_demo1"></div>
</form>
除了要定义一个id和elements 想对应的容器之外,就是必须要定义一个form 。它的action定义要发送的服务器端地址,他的method定义数据的发送方式。

插件开发

在3.x的tinymce中在tiny_mce/plugins/example有一个插件开发的例子,只要修改程序框架中的代码为要完成的功能即可。现在主要讲一下“首行缩进”功能的实现。

1.文件
在自己的插件目录下有俩个文件:
a. editor_plugin.js
b. editor_plugin_src.js

第一个文件是压缩后的js文件,第二个是自己开发的文件。tinymce推荐的压缩工具是 jstrim 。Url是http://javascriptcompressor.com/。

2.基本原理

<p style="text-indent:24px;">
This page shows all available buttons and plugins that are included in the TinyMCE core package.There are more examples on how to use TinyMCE in the <a href="http://wiki.moxiecode.com/examples/tinymce/">Wiki</a>.
</p>
实现方法是通过给段落标签加入style属性来实现的。
3.tinymce的实现

ed.addCommand('mceStageindent', function() {
//基本变量设置
var ed = tinyMCE.activeEditor, d = ed.dom, s = ed.selection, e, iv, iu , st;
st= s.getNode().style;
var allinline=d.select('p');
var bfontSize=document.body.currentStyle?document.body.currentStyle['fontSize']:
document.defaultView.getComputedStyle(document.body,false)['fontSize'];
iu=/[a-z%]+$/i.exec(bfontSize);//获得文字的大小
iv=parseInt(bfontSize)*2;//俩个文字的宽度

//实现在选取段落时候缩进段落,不选取则缩进所有段落。
if (s.getContent() != "") {
if (st.textIndent == "" || st.textIndent == "undefined") {
st.textIndent = iv+iu;
}
else {
st.textIndent = '';
}
}else{
if (st.textIndent == "" || st.textIndent == "undefined") {
d.setStyle(allinline, 'text-indent', iv+iu);
}else{
d.setStyle(allinline, 'text-indent', '');
}
}
}
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: