213 lines
6.6 KiB
C#
213 lines
6.6 KiB
C#
using System;
|
|
using System.Text;
|
|
using Zhaizj.Framework.Web.Context;
|
|
|
|
namespace Zhaizj.Framework {
|
|
|
|
/// <summary>
|
|
/// 在 web 中使用的富文本编辑器
|
|
/// </summary>
|
|
public class Editor {
|
|
|
|
/// <summary>
|
|
/// 工具栏类型
|
|
/// </summary>
|
|
public enum ToolbarType {
|
|
|
|
/// <summary>
|
|
/// 基本按钮
|
|
/// </summary>
|
|
Basic,
|
|
|
|
/// <summary>
|
|
/// 全部按钮
|
|
/// </summary>
|
|
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;
|
|
|
|
/// <summary>
|
|
/// 图片上传网址
|
|
/// </summary>
|
|
public String UploadUrl {
|
|
get { return _uploadUrl; }
|
|
set { _uploadUrl = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前用户的所有图片的网址
|
|
/// </summary>
|
|
public String MyPicsUrl {
|
|
get { return _mypicsUrl; }
|
|
set { _mypicsUrl = value; }
|
|
}
|
|
|
|
public Boolean IsUnique {
|
|
get { return _isUnique; }
|
|
set { _isUnique = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑器名称
|
|
/// </summary>
|
|
public String ControlName {
|
|
get { return _controlName; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑器文件(js、css、图片等)所在路径
|
|
/// </summary>
|
|
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 ); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 高度
|
|
/// </summary>
|
|
public String Height {
|
|
get { return _height; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 工具栏类型
|
|
/// </summary>
|
|
public ToolbarType Toolbar {
|
|
get { return _Toolbar; }
|
|
set { _Toolbar = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 宽度,默认是父容器的100%
|
|
/// </summary>
|
|
public String Width {
|
|
get { return _width; }
|
|
}
|
|
|
|
///// <summary>
|
|
///// 编辑器变量名称(js中使用)
|
|
///// </summary>
|
|
//public String EditVarName {
|
|
// get { return String.Format( "{0}Editor", ControlName.Replace( ".", "" ) ); }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 需要编辑的内容(html格式)
|
|
/// </summary>
|
|
public String Content {
|
|
get {
|
|
if (strUtil.HasText( _content )) {
|
|
//return _content.Replace( "'", "′" ).Replace( "\n", "" ).Replace( "\r", "" );
|
|
return strUtil.EncodeTextarea( _content );
|
|
}
|
|
return String.Empty;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑器名称。在其他页面和编辑器页面进行交互的时候,需要这个名称。
|
|
/// </summary>
|
|
public String EditVarName {
|
|
get { return String.Format( "{0}Editor", ControlName.Replace( ".", "" ) ); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置图片上传网址
|
|
/// </summary>
|
|
/// <param name="ctx"></param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建编辑器(页面中第一个)
|
|
/// </summary>
|
|
/// <param name="controlName"></param>
|
|
/// <param name="content"></param>
|
|
/// <param name="height"></param>
|
|
/// <param name="editorPath"></param>
|
|
/// <param name="jsVersion"></param>
|
|
/// <param name="toolbarType"></param>
|
|
/// <returns></returns>
|
|
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( "<div id=\"{0}\">", this.ControlName.Replace( ".", "_" ) + "Editor" );
|
|
|
|
builder.AppendFormat( "<textarea id=\"{0}\" name=\"{0}\" class=\"editor-textarea\" style=\"display:none;height:" + this.Height + ";\">{1}</textarea>", this.ControlName, this.Content );
|
|
|
|
builder.Append( "<script>_run(function(){require([\"" + RelativePath + "editor\"],function(){" );
|
|
builder.AppendLine();
|
|
builder.Append( "window." + EditVarName + "=new Zhaizj.Framework.editor( {editorPath:'" + this.EditorPath + "', height:'" + this.Height + "', name:'" + this.ControlName + "', content:'', toolbarType:'" + this.Toolbar.ToString().ToLower() + "', uploadUrl:'" + this.UploadUrl + "', mypicsUrl:'" + this.MyPicsUrl + "' } );" + EditVarName + ".render();" );
|
|
builder.AppendLine();
|
|
builder.Append( "})});</script>" );
|
|
|
|
builder.Append( "</div>" );
|
|
|
|
return builder.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑器生成的js和html内容
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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 : "";
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|