A few days ago, I felt that the insertion code of the blog was not easy to use. So, sina's html editor was replaced with tinyMCE. And a simple insertion of code has been developed. . . The following is my development process.
First, my tinyMCE version is Version: 3.2.7 (2009-09-22).
tinyMCE insertion code requires calling tinyMCE.execCommand('mceInsertContent',false,value); method of tinyMCE. The parameters do not need to be changed, value is what you want to insert.
For example, I wrote a function,
The code copy is as follows:
function InsertHTML(value)
{
tinyMCE.execCommand('mceInsertContent',false,value);
}
Later, for this example, a download is provided. In the example. There are three documents involved in total.
tinyMCE.html insertcode.php save.php these three files.
tinyMCE.html is the tinyMCE text box page.
The main code is as follows:
The code copy is as follows:
<script type=text/javascript src=http://www.vevb.com/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,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>
where js code is to initialize tinyMCE. The download example does not include tinyMCE, you need to download it yourself. Then change the src of the js code.
The code copy is as follows:
<input name=button type=button onclick=window.open('insertcode.php','insert code','height=500, width=600, top=300, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no') value=Click here to insert the code/>
The above code is used to open the insertcode.php file.
Next, let's take a look at insertcode. The code of this file.
First of all, the js code
The code copy is as follows:
<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>
Next is PHP and html code
The code copy is as follows:
<?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=Submit 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 the type of code to insert
<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=Submit style=border:1px solid #000; line-height:18px; width:60px;/>
</label>
</p>
<p> </p>
</form>
</div>
<?php
}
?>
In insertcode.php, the insertcode() function is used to call the insertHTMl() function of the tinyMCE.html page and insert the code into the tinyMCE.html page.
In the code, why do we need '+value+'?
Because we are displaying the page, we will use the SyntaxHighlighter plug-in to highlight the code.
Another point to be explained, here, $content = htmlspecialchars($content); We performed htmlspecialchars escape operation for the code itself. In this way, the code inserted into the database will be safe.
OK, let's look at save.php again, this page is used to display the submitted content.
The main code is as follows:
The code copy is as follows:
<?
$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, it's over.