当前位置: 移动技术网 > IT编程>网页制作>HTML > tinyMCE插件开发之插入html,php,sql,js代码 并代码高亮显示

tinyMCE插件开发之插入html,php,sql,js代码 并代码高亮显示

2017年12月01日  | 移动技术网IT编程  | 我要评论
下面就是我开发的过程。
首先,我的 tinymce版本是 version: 3.2.7 (2009-09-22) 。
下载地址
tinymce插入代码,需要调用 tinymce的 tinymce.execcommand('mceinsertcontent',false,value); 方法。其中参数无需改变,value 就是你要插入的内容,
比如我写了一个函数,
复制代码 代码如下:

function inserthtml(value)
{
tinymce.execcommand('mceinsertcontent',false,value);
}

后面,针对该例子,提供下载。在例子中。一共涉及到三个文件。
tinymce.html insertcode.php save.php 这三个文件。
tinymce.html 是tinymce文本框页面。
主要代码如下:
复制代码 代码如下:

<script type="text/javascript" src="//www.jb51.net/tinymce/tiny_mce.js"></script>
<script type="text/javascript">
tinymce.init({
// general options
convert_urls : false,
mode : "exact",
elements : "article_content",
//mode : "textareas",
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,wordcount",
// theme options
theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,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",
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",
// 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",
// replace values for the template plugin
template_replace_values : {
username : "some user",
staffid : "991234"
}
});
</script>
<script type="text/javascript">
function inserthtml(value)
{
tinymce.execcommand('mceinsertcontent',false,value);
}
</script>

其中js代码是初始化 tinymce。下载的例子中,并未包含 tinymce,你需要自己下载。然后 更改js代码的 src 即可。
复制代码 代码如下:

<input name="button" type="button" onclick="window.open('insertcode.php','插入代码','height=500, width=600, top=300, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')" value="点击这里插入代码" />

上面这段代码,是用来打开insertcode.php文件的。
接下来,我们来看下 insertcode。php 这个文件的代码。
首先是 js 代码
复制代码 代码如下:

<script language="javascript" src="http://www.gosoa.com.cn/js/jquery.js"></script>
<script language="javascript">
function insertcode()
{
var value = $('#postcontent').html();
var codetype = $('#codetype').val();
// window.opener.inserthtml('<textarea rows="3" cols="50" name="code" class="'+codetype+'">'+value+'</textarea>');
window.opener.inserthtml('<pre name="code" class="'+codetype+'">'+value+'</pre>');
window.close();
}
</script>

其次是 php 和 html 代码
复制代码 代码如下:

<?php
error_reporting(0);
$content = $_post['content'];
if(!empty($content))
{
    $codetype = $_post['codetype'];
    echo '<div id="postcontent">';
    $content = htmlspecialchars($content);
    echo $content;
    echo '</div>
    <input type="hidden" name="codetype" id="codetype" value="'.$codetype.'" />
    <input type="button" name="submit" value="提交" onclick="insertcode()" style="border:1px solid #000; line-height:18px; width:60px;"/>';
}else
{
?>
<div style="margin:0 auto">
<form id="form1" name="form1" method="post" action="insertcode.php">
<label>选择要插入的代码类型
<select name="codetype" id="codetype">
    <option value='php'>php</option>
    <option value='js'>js</option>
    <option value='html'>html</option>
    <option value='c'>c</option>
    <option value='asp'>asp</option>
    <option value='xml'>xml</option>
    <option value='java'>java</option>
    <option value='java'>java</option>
    <option value='csharp'>c#</option>
    <option value='sql'>sql</option>
</select>
</label>
<label>
<textarea name="content" id="content" cols="30" rows="20" style="width:600px; height:200px; border:1px dashed #333"></textarea>
</label>
<p>
<label style="padding-left:50px;">
<input type="submit" name="submit" value="提交" style="border:1px solid #000; line-height:18px; width:60px;"/>
</label>
</p>
<p> </p>
</form>
</div>
<?php
}    
?>

在insertcode.php中,insertcode() 函数用来调用 tinymce.html页面的 inserthtml()函数,并将代码插入到 tinymce.html 页面中。
代码中,我们为什么要 '+value+' 呢?
因为我们在显示页面,将会采用 syntaxhighlighter 插件来高亮显示代码。
还有一点要说明,在这里,$content = htmlspecialchars($content); 我们对于代码本身,进行了 htmlspecialchars 转义操作。这样,插入数据库的代码则会是安全的。
ok,我们再来看 save.php,该页面用来显示 提交的内容。
主要代码如下:
复制代码 代码如下:

<?
$article_content = $_post['article_content'];
function transcode($str)
{
if(empty($str))
{
return false;
}
$str = str_replace('"','"',$str);
$str = str_replace('','',$str);
$str = str_ireplace('<br>',"n",$str);
$str = str_ireplace('<pre','<pre name="code" ',$str);
return $str;
}
echo transcode($article_content);
?>
<script class="javascript" src="/tinymce/lightcode/scripts/shcore.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushcsharp.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushphp.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushjscript.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushjava.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushvb.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushsql.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushxml.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushdelphi.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushpython.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushruby.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushcss.js"></script>
<script class="javascript" src="/tinymce/lightcode/scripts/shbrushcpp.js"></script>
<script class="javascript">
dp.syntaxhighlighter.highlightall('code');
</script>

ok,完了。
^_^ ~~~
tinymce 插件开发之插代码高亮 v1.0 (支持html,php,sql,js)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网