通过xajax给fckeditor增加自动保存草稿功能

本文介绍如何使用xajax 0.5.4beta版本为FCKeditor 2.4.3添加自动保存功能,并提供了实现代码示例。

使用的xajax版本是0.5.4beta,fckeditor版本是2.4.3,通过xajax给fckeditor增加自动保存草稿功能,

fckeditor自动保存草稿演示地址
http://xajax.dayanmei.com/demo/xajax2fckeditor/

xajax给fckeditor增加自动保存草稿下载(xajax和fckeditor请自行下载)
http://xajax.dayanmei.com/download/xajax2fckeditor.rar

<?php

session_start
();

define('ROOT_DIR',str_replace("//","/",dirname(__FILE__)));

$cachedir './cache/';



//清除超过1天的记录文件

function clearcache($dir,$date 1) {

     
$now time();

     
$time $date 60 60 24;

     if(!
is_dir($dir)){

          die(
'error :dir' $dir ' is not exist'); 

          }

     
$handle opendir($dir);

     while(
false !== ($filename readdir($handle))) {

          if(
$filename == '.' OR $filename == '..' OR $filename == ''){

               continue;

               }

          if((
$now filemtime($dir.$filename)) > $time){

               @
unlink($dir.$filename);

               }

     }

}



function 
xml_escape($string) {

     return 
str_replace(array('&','"',"'",'<','>'),

          array(
'&amp;','&quot;','&apos;','&lt;','&gt;'),

          
$string     

     
);

}



//执行清理

clearcache($cachedir);



require_once(
'./xajax/xajax.inc.php');



$xajax = new xajax();

$xajax->registerFunction('autosave');

$xajax->registerFunction('loadcache');

$xajax->processRequest();

echo 
$xajax->getJavascript('./xajax');



//定义载入数据函数

function loadcache() {

     global 
$cachedir;

     
$filename $cachedir session_id().'.txt';

     if(
file_exists($filename)){

          
$content '';

          
$handle file($filename);

          foreach(
$handle as $key=>$val) {

               
$content .= $val;

          }

          }else{

          
$content '';

          }

     
$content xml_escape($content);

     
$obj = new xajaxResponse();

     
$obj->call("SetContents('$content')");

     
//$obj->alert($content);

     
$obj->assign('autosavemsg','innerHTML',"成功载入数据");

     return 
$obj;

}



//定义自动保存函数

function autosave($content) {

     global 
$cachedir;

     
$obj = new xajaxResponse();

     
$content mysql_escape_string($content);

     
$filename $cachedir session_id() . ".txt";

     
$fp fopen($filename,"w+");

     
fwrite($fp,$content);

     
fclose($fp);

     if(
file_exists($filename)){

          
$obj->assign("autosavemsg","innerHTML","成功保存数据...");

          }

     
//$obj->alert($content);

     
return $obj;

}



function 
editor($editorid,$content '') {

     
//载入编辑器

     
require_once('../../includes/fckeditor/fckeditor.php');

     
$editor = new fckeditor($editorid);

     
$editor->BasePath '../../includes/fckeditor/';

     
$editor->Value $content;

     return 
$editor->createhtml();

}



?>

<!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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

<meta name="Description" content="" />

</head>

<style type="text/css">

<!--

a {

     cursor: pointer;

}

-->

</style>

<body>



<form name="f1" id="f1" method="post" action="" enctype="multipart/form-data">

<?php

//载入编辑器

echo editor('content');

?>

<script language="javascript">

<!--

// 自动保存时间间隔

var AutoSaveTime=90000;

// 计时器对象

var AutoSaveTimer;

// 首先设置一次自动保存状态

SetAutoSave();

// 设置自动保存状态函数

function SetAutoSave() {

     AutoSaveTimer=setInterval("GetContents('content')",AutoSaveTime);

}



//获取内容

function GetContents(contentid) {

     var oEditor = FCKeditorAPI.GetInstance(contentid) ;

     content = oEditor.GetXHTML( true );

     xajax_autosave(content);

}



//设置编辑器内容

function SetContents(content)

{

     // Get the editor instance that we want to interact with.

     var oEditor = FCKeditorAPI.GetInstance('content') ;

     // Set the editor contents (replace the actual one).

     oEditor.SetHTML( content ) ;

}

//-->

</script>

</form>

<div id="autosavemsg">

    

</div>



<a href="#" onclick="xajax_loadcache();" title="">载入自动保存内容< /a> <a href="#" onclick="GetContents('content');" title="">手动保存 </a>

</body>

</html>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值