asp.net整合你的文字編輯器
阿新 • • 發佈:2019-02-07
目前,網上流行的文字編輯器很多,最有名的如:FCKEditor,CuteEditor,FreeTextBox等,當然TextBox也算一個。但是當這麼多編輯器放在一塊時,我們選擇哪一個呢?現在很多軟體已經開始嘗試做這樣的集成了,這裡給另外一種思路,讓你無限整合你的文字編輯器,而且是可擴充套件的。
需求:我們的需求是整合各種不同的文字編輯器,讓我們的系統在不需要對編輯介面進行再開發的情況下,可以更換編輯器。最好是通過配置的方式進行。
分析:為了實現更改編輯器而不修改程式碼,我們需要寫一個代理控制元件,這個控制元件的作用是代理我們的編輯器進行工作。這個代理控制元件實現我們需要的基本功能,比如:調整大小、設定或獲取文字編輯器的內容、設定樣式等。因為在統一個系統中,我們對編輯器的要求是一致的,比如我們只需要獲取內容,這時我們可以按照我們的要求設計我們的代理控制元件類(EditorControl)。在實現我們的代理控制元件前,我們必須解決對特定文字編輯器的訪問問題,等我們解決了這一問題後,我們再來實現我們的控制元件。
下一步我們要來實現我們的代理控制元件了。我們要讓這個代理控制元件代替各種各樣的編輯器,我們就需要有個介面卡,讓介面卡去代替我們訪問編輯器控制元件。我們定義好介面,至於怎麼實現,我們留給各個編輯器介面卡去實現。我們需要定義一個介面卡介面(IEditorAdapter),這個介面需要定義我們在代理控制元件總必須實現的功能介面。
publicinterface IEditorAdapter
{
///<summary>
/// Get the text editor control
///</summary>
///<returns></returns>
Control GetEditorControl();
///<summary>
/// Get or set the content of the editor control when this control post data.
///</summary>
///<returns></returns>
string Content{get;set;}
///<summary>
/// Get or set the control's width.
///</summary>
Unit Width { get;set;}
///<summary>
/// Get or set the control's height.
///</summary>
Unit Height { get;set;}
///<summary>
/// Get or set the control's style.
///</summary>
string CssClass { get;set;}
///<summary>
/// Get or set the control's ID
///</summary>
string ControlID { get;set;}
} 接下來就改實現這個介面了。對於不同的編輯器,我們需要實現專門的介面卡。這樣一來,我以後要增加一個編輯器,我只要實現一個這樣的介面卡就可以了。下面是一個簡單的介面卡的實現:
class RichTextBoxEditorAdapter : IEditorAdapter
{
private TextBox editor =null;
public RichTextBoxEditorAdapter()
{
initControl();
}
void initControl()
{
editor =new TextBox();
editor.Wrap =true;
}
IEditorAdapter 成員
}
好了,我們已經將核心的東西搞定了,下面就是搞定它的擴充套件性。這裡就需要用到工廠模式。我們可以定義一個介面卡工廠(EditorAdapterFactory),當我們的代理控制元件要訪問實際的編輯器控制元件時,我們先通過工廠尋找到介面卡,然後通過介面卡介面對獲取到的介面卡進行訪問。
///<summary>
/// The factory of editor adapter.
///</summary>
publicclass EditorAdapterFactory
{
privatestaticreadonly ILog log = LogManager.GetLogger(typeof(EditorAdapterFactory));
///<summary>
/// Get the text editor adapter
///</summary>
///<returns></returns>
publicstatic IEditorAdapter GetAdapter()
{
try
{
string typeName = Configuration.Configuration.editorAdapterConfiguration.getAdapterTypeName();
if (string.IsNullOrEmpty(typeName))
{
return getDefaultEditor();
}
else
{
Type t = Type.GetType(typeName);
if (t ==null) return getDefaultEditor();
else
{
if (t.GetInterface("IEditorAdapter") !=null)
{
log.Info("Get editor adapter:"+ t.FullName);
return (IEditorAdapter)Activator.CreateInstance(t);
}
需求:我們的需求是整合各種不同的文字編輯器,讓我們的系統在不需要對編輯介面進行再開發的情況下,可以更換編輯器。最好是通過配置的方式進行。
分析:為了實現更改編輯器而不修改程式碼,我們需要寫一個代理控制元件,這個控制元件的作用是代理我們的編輯器進行工作。這個代理控制元件實現我們需要的基本功能,比如:調整大小、設定或獲取文字編輯器的內容、設定樣式等。因為在統一個系統中,我們對編輯器的要求是一致的,比如我們只需要獲取內容,這時我們可以按照我們的要求設計我們的代理控制元件類(EditorControl)。在實現我們的代理控制元件前,我們必須解決對特定文字編輯器的訪問問題,等我們解決了這一問題後,我們再來實現我們的控制元件。
下一步我們要來實現我們的代理控制元件了。我們要讓這個代理控制元件代替各種各樣的編輯器,我們就需要有個介面卡,讓介面卡去代替我們訪問編輯器控制元件。我們定義好介面,至於怎麼實現,我們留給各個編輯器介面卡去實現。我們需要定義一個介面卡介面(IEditorAdapter),這個介面需要定義我們在代理控制元件總必須實現的功能介面。
{
///<summary>
/// Get the text editor control
///</summary>
///<returns></returns>
Control GetEditorControl();
///<summary>
/// Get or set the content of the editor control when this control post data.
///<returns></returns>
string Content{get;set;}
///<summary>
/// Get or set the control's width.
///</summary>
Unit Width { get;set;}
///<summary>
/// Get or set the control's height.
///</summary>
Unit Height
///<summary>
/// Get or set the control's style.
///</summary>
string CssClass { get;set;}
///<summary>
/// Get or set the control's ID
///</summary>
string ControlID { get;set;}
} 接下來就改實現這個介面了。對於不同的編輯器,我們需要實現專門的介面卡。這樣一來,我以後要增加一個編輯器,我只要實現一個這樣的介面卡就可以了。下面是一個簡單的介面卡的實現:
class RichTextBoxEditorAdapter : IEditorAdapter
{
private TextBox editor =null;
public RichTextBoxEditorAdapter()
{
initControl();
}
void initControl()
{
editor =new TextBox();
editor.Wrap =true;
}
IEditorAdapter 成員
}
好了,我們已經將核心的東西搞定了,下面就是搞定它的擴充套件性。這裡就需要用到工廠模式。我們可以定義一個介面卡工廠(EditorAdapterFactory),當我們的代理控制元件要訪問實際的編輯器控制元件時,我們先通過工廠尋找到介面卡,然後通過介面卡介面對獲取到的介面卡進行訪問。
///<summary>
/// The factory of editor adapter.
///</summary>
publicclass EditorAdapterFactory
{
privatestaticreadonly ILog log = LogManager.GetLogger(typeof(EditorAdapterFactory));
///<summary>
/// Get the text editor adapter
///</summary>
///<returns></returns>
publicstatic IEditorAdapter GetAdapter()
{
try
{
string typeName = Configuration.Configuration.editorAdapterConfiguration.getAdapterTypeName();
if (string.IsNullOrEmpty(typeName))
{
return getDefaultEditor();
}
else
{
Type t = Type.GetType(typeName);
if (t ==null) return getDefaultEditor();
else
{
if (t.GetInterface("IEditorAdapter") !=null)
{
log.Info("Get editor adapter:"+ t.FullName);
return (IEditorAdapter)Activator.CreateInstance(t);
}