存档

文章标签 ‘tinyMCE’

Google Syntax Highlighter 与WordPress – tinyMCE 的兼容性

2010年3月24日 1 条评论

Google Syntax Highlighter 与WordPress的兼容性

Google Syntax Highlighter 与 tinyMCE  的兼容性

当我们在WordPress中写日志,要插入一段php代码的时候,在HTML视图下,可以如下

<pre name="code" class="php">

code....

</pre>

但当我们恢复到可视化的时候,代码就会变成

<pre  class="php">

code....

</pre>

此时, Google Syntax Highlighter 便识别不出来了。

因为 Google Syntax Highlighter 是通过 pre的 name 熟悉来识别的。

出现这样的情况,是因为 WordPress 采用的 tinyMCE 编辑器。而 tinyMCE编辑器默认 给 pre 没有添加 name 属性。

所以,我们只需要更改下 tinyMCE的初始化部分的代码即可。

在 wp-admin/includes/post.php 里面

找到如下代码

$initArray = array (
		'mode' => 'specific_textareas',
		'editor_selector' => 'theEditor',
		'width' => '100%',
		'theme' => 'advanced',
		'skin' => 'wp_theme',
		'theme_advanced_buttons1' => "$mce_buttons",
		'theme_advanced_buttons2' => "$mce_buttons_2",
		'theme_advanced_buttons3' => "$mce_buttons_3",
		'theme_advanced_buttons4' => "$mce_buttons_4",
		'language' => "$mce_locale",
		'spellchecker_languages' => "$mce_spellchecker_languages",
		'theme_advanced_toolbar_location' => 'top',
		'theme_advanced_toolbar_align' => 'left',
		'theme_advanced_statusbar_location' => 'bottom',
		'theme_advanced_resizing' => true,
		'theme_advanced_resize_horizontal' => false,
		'dialog_type' => 'modal',
		'relative_urls' => false,
		'remove_script_host' => false,
		'convert_urls' => false,
		'apply_source_formatting' => false,
		'remove_linebreaks' => true,
		'gecko_spellcheck' => true,
		'entities' => '38,amp,60,lt,62,gt',
		'accessibility_focus' => true,
		'tabfocus_elements' => 'major-publishing-actions',
		'media_strict' => false,
		'paste_remove_styles' => true,
		'paste_remove_spans' => true,
		'paste_strip_class_attributes' => 'all',
		'wpeditimage_disable_captions' => $no_captions,
		'plugins' => "$plugins"
	);

这个 $initArray 变量就是用来初始化 tinyMCE的。我们只需要给其添加上一个变量 即可:

‘extended_valid_elements’ => “pre[name|class]”

添加后 的完整代码如下:

$initArray = array (
		'mode' => 'specific_textareas',
		'editor_selector' => 'theEditor',
		'width' => '100%',
		'theme' => 'advanced',
		'skin' => 'wp_theme',
		'theme_advanced_buttons1' => "$mce_buttons",
		'theme_advanced_buttons2' => "$mce_buttons_2",
		'theme_advanced_buttons3' => "$mce_buttons_3",
		'theme_advanced_buttons4' => "$mce_buttons_4",
		'language' => "$mce_locale",
		'spellchecker_languages' => "$mce_spellchecker_languages",
		'theme_advanced_toolbar_location' => 'top',
		'theme_advanced_toolbar_align' => 'left',
		'theme_advanced_statusbar_location' => 'bottom',
		'theme_advanced_resizing' => true,
		'theme_advanced_resize_horizontal' => false,
		'dialog_type' => 'modal',
		'relative_urls' => false,
		'remove_script_host' => false,
		'convert_urls' => false,
		'apply_source_formatting' => false,
		'remove_linebreaks' => true,
		'gecko_spellcheck' => true,
		'entities' => '38,amp,60,lt,62,gt',
		'accessibility_focus' => true,
		'tabfocus_elements' => 'major-publishing-actions',
		'media_strict' => false,
		'paste_remove_styles' => true,
		'paste_remove_spans' => true,
		'paste_strip_class_attributes' => 'all',
		'wpeditimage_disable_captions' => $no_captions,
		'plugins' => "$plugins",
		'extended_valid_elements' => "pre[name|class]"
	);

保存后 再试试吧。
Google Syntax Highlighter 就与WordPress完美兼容了。
Google Syntax Highlighter 就与tinyMCE完美兼容了。

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

2010年1月6日 5 条评论

前几天,觉得blog的插入代码不够好用。于是,将sina的html编辑器更换为了tinyMCE。并且开发了一个简单的插入代码的功能。。。

下面就是我开发的过程。

首先,我的 tinyMCE版本是 Version: 3.2.7 (2009-09-22) 。下载地址 http://tinymce.moxiecode.com/download.php

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=\\"http://www.gosoa.com.cn/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>&nbsp;</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(\\'&lt;BR&gt;\\',\\"\\\\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插件开发示例

TinyMce的插件开发【转】

2009年12月17日 没有评论

简介:
TinyMCE 是一个基于浏览器(例如MSIE或Mozilla)的强大的所见即所得的编辑器,它使用户可以方便的编辑HTML内容。它非常灵活并且是为系统集成而设计的,比如在Intranets、CMS、LMS等系统中应用。

而在官方的TinyMCE版本上,一直没有图片上传的功能,由于项目需要,需要对在线编辑器增加上传模块。

文件结构:
css TinyMCE样式表文件

images 存放按纽等图像文件

langs TinyMCE语言包

editor_plugin.js TinyMCE插件调用时的主文件

popup.php TinyMCE上传插件的用户对口框

popup.script.php 处理popup.php的脚本文件

开发概述

对于TinyMCE插件的开发,我觉得用MVC的观点描述是十分合适的,M层是TinyMCE的核心文件,我们只是调用即可;C层如editor_plugin.js、对话框的JS逻辑,V层应该就是用户对口框的设计。

开发步骤

一 制作与tinymce通讯的连接器:editor_plugin.js

1 复制插件开发模版

在tiny_mce/plugins里面有一个_template的文件夹,它可以说是插件开发的一个通用的模版文件,将它复制到tiny_mce/plugins文件夹,并命名为你的插件名称如upload

2 选择需要的回调函数

打开editor_plugin.js文件,保留tinyMCE.importPluginLanguagePack,及 TinyMCE_TemplatePlugin类的getInfo、getControlHTML、execCommand、 handleNodeChange函数,以及最后的tinyMCE.addPlugin函数,以上函数的作用分别是:

tinyMCE.importPluginLanguagePack:加载语言包(Line:11)

getInfo:插件版本信息(Line:34)

getControlHTML与handleNodeChange:生成按纽(Line:68 130)

execCommand:当TinyMCE加载此插件时,执行此函数,在这里主要是找开一个对话窗口(Line:88)

tinyMCE.addPlugin:增加一个插件(Line:238)

3 修改插件名称
将里面所有小写的late改成你刚才命名的插件名,注意,原98-102行只须更改98行template['file']后的路径。

第二个要修改的是:var TinyMCE_TemplatePlugin(原26行)与tinyMCE.addPlugin(原238行),这里的作用是生成一个插件,名字一致即可

4 加载语言包
tinyMCE.importPluginLanguagePack:与lang文件夹对应,根据需要选择

5 创建版本信息
这个主要是为后期插件升级做的备注,主要作用就是***到此一游,做个记号

6 生成编辑按纽
getControlHTML : function(cn) {
 switch (cn) {
   case “template”:
     return tinyMCE.getButtonHTML(cn, ‘lang_template_desc’, ‘{$pluginurl}/images/template.gif’, ‘mceTemplate’, true);
 }
 return “”;
},

变更如下:其中mceUpload是execCommand调用的命令,得保持一致
getControlHTML : function(cn) {
 switch (cn) {
   case “upload”:
     return tinyMCE.getButtonHTML(cn, ‘lang_upload_desc’, ‘{$pluginurl}/images/template.gif’, ‘mceUpload’, true);
 }
 return “”;
},

handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
 // Select template button if parent node is a strong or b
 if (node.parentNode.nodeName == “STRONG” || node.parentNode.nodeName == “B”) {
  tinyMCE.switchClass(editor_id + ‘_template’, ‘mceButtonSelected’);
  return true;
 }

 // Deselect template button
 tinyMCE.switchClass(editor_id + ‘_template’, ‘mceButtonNormal’);
},//如果这是最后一个函数,记得去掉“,”号
这个函数的主要作用是编辑器中按纽对用户操作的响应,由于我们只做上传,不需要再编辑上传的图像。故更改如下:

handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
tinyMCE.switchClass(editor_id + ‘_upload’, ‘mceButtonNormal’);
}

7 调出图形对口窗口
execCommand : function(editor_id, element, command, user_interface, value) {
 switch (command) {
  case “mceTemplate”:
  if (user_interface) {
   var template = new Array();
   template['file'] = ‘../../plugins/template/popup.htm’; template['width'] = 300;
   template['height'] = 200;
   tinyMCE.openWindow(template, {editor_id : editor_id, some_custom_arg : “somecustomdata”});
   tinyMCE.triggerNodeChange(false);
  } else {
   alert(“execCommand: mceTemplate gets called from popup.”);
  }
  return true;
 }
 return false;
},

在这个代码中,case “mceTemplate”的作用是当用户选择执行这个命令时调用,与我们在生成按纽的时候,最后设的’mceUpload’保持一致。
调用一个对话框的主要函数是:tinyMCE.openWindow,具体参数请参考随附近上传的手册
execCommand : function(editor_id, element, command, user_interface, value) {
 switch (command) {
  case “mceUpload”:
  var template = new Array();
  template['file'] = ‘../../plugins/upload/popup.php’;
  template['width'] = 480;
  template['height'] = 380;
  tinyMCE.openWindow(template, {editor_id : editor_id});
  tinyMCE.triggerNodeChange(false);
  return true;
 }
 return false;
},

二 制作与用户对话的对口窗口View:popup.php
1 设计页面与实现图片上传:这一步在网上有很多的资料,请大家google一下即可
2 向编辑窗口插入值:tinyMCE.execCommand(‘mceInsertContent’,true,html);
html为返回内容的变量,如”

三 前台调用
1 加载插件
plugins : “upload “,

2 生成按纽
theme_advanced_buttons1_add_before : ” upload”,

结束语
这几天一直在看TinyMCE关于插件开发的资料,在网上却很少有开发插件方面的教程,因此也花了些时间写了一个小教程,希望对大家有帮助,也希望能与大家多交流交流。

分类: 财富人生 标签: ,