using System;
using System.Text;
using Zhaizj.Framework.Web.Context;
namespace Zhaizj.Framework {
///
/// 在 web 中使用的富文本编辑器
///
public class Editor {
///
/// 工具栏类型
///
public enum ToolbarType {
///
/// 基本按钮
///
Basic,
///
/// 全部按钮
///
Full
}
private String _content;
private String _controlName;
private String _height;
private String _editorPath;
private ToolbarType _Toolbar;
private String _width;
private Boolean _isUnique = false;
private String _jsVersion;
private String _uploadUrl;
private String _mypicsUrl;
///
/// 图片上传网址
///
public String UploadUrl {
get { return _uploadUrl; }
set { _uploadUrl = value; }
}
///
/// 当前用户的所有图片的网址
///
public String MyPicsUrl {
get { return _mypicsUrl; }
set { _mypicsUrl = value; }
}
public Boolean IsUnique {
get { return _isUnique; }
set { _isUnique = value; }
}
///
/// 编辑器名称
///
public String ControlName {
get { return _controlName; }
}
///
/// 编辑器文件(js、css、图片等)所在路径
///
public String EditorPath {
get { return _editorPath; }
}
public String RelativePath {
get { return strUtil.TrimStart( this.EditorPath, sys.Path.Js ); }
}
private String FrameId {
get { return String.Format( "Zhaizj.FrameworkEditor_{0}_frame", ControlName ); }
}
///
/// 高度
///
public String Height {
get { return _height; }
}
///
/// 工具栏类型
///
public ToolbarType Toolbar {
get { return _Toolbar; }
set { _Toolbar = value; }
}
///
/// 宽度,默认是父容器的100%
///
public String Width {
get { return _width; }
}
/////
///// 编辑器变量名称(js中使用)
/////
//public String EditVarName {
// get { return String.Format( "{0}Editor", ControlName.Replace( ".", "" ) ); }
//}
///
/// 需要编辑的内容(html格式)
///
public String Content {
get {
if (strUtil.HasText( _content )) {
//return _content.Replace( "'", "′" ).Replace( "\n", "" ).Replace( "\r", "" );
return strUtil.EncodeTextarea( _content );
}
return String.Empty;
}
}
///
/// 编辑器名称。在其他页面和编辑器页面进行交互的时候,需要这个名称。
///
public String EditVarName {
get { return String.Format( "{0}Editor", ControlName.Replace( ".", "" ) ); }
}
///
/// 设置图片上传网址
///
///
public void AddUploadUrl( MvcContext ctx ) {
Object objuploadUrl = ctx.GetItem( "editorUploadUrl" );
Object objmypicsUrl = ctx.GetItem( "editorMyPicsUrl" );
this.UploadUrl = objuploadUrl == null ? "" : objuploadUrl.ToString();
this.MyPicsUrl = objmypicsUrl == null ? "" : objmypicsUrl.ToString();
}
private Editor( String controlName, String content, String width, String height, String editorPath, ToolbarType toolbarType ) {
_controlName = String.Format( "{0}", controlName );
_content = content;
_width = width;
_height = height;
_editorPath = editorPath;
_Toolbar = toolbarType;
}
///
/// 创建编辑器(页面中第一个)
///
///
///
///
///
///
///
///
public static Editor NewOne( String controlName, String content, String height, String editorPath, String jsVersion, ToolbarType toolbarType ) {
Editor result = new Editor( controlName, content, "100%", height, editorPath, toolbarType );
result._isUnique = true;
result._jsVersion = jsVersion;
return result;
}
private String Render() {
StringBuilder builder = new StringBuilder();
builder.AppendFormat( "
", this.ControlName.Replace( ".", "_" ) + "Editor" );
builder.AppendFormat( "", this.ControlName, this.Content );
builder.Append( "" );
builder.Append( "
" );
return builder.ToString();
}
///
/// 编辑器生成的js和html内容
///
///
public override String ToString() {
return Render();
}
private String getJsPath() {
String result = strUtil.TrimEnd( this.EditorPath, "/" );
return strUtil.TrimEnd( result.ToLower(), "editor" );
}
private String getJsVersionString() {
return strUtil.HasText( _jsVersion ) ? "?v=" + _jsVersion : "";
}
}
}