-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCKEditorMVC.cs
65 lines (60 loc) · 3.09 KB
/
CKEditorMVC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Text;
using System.Web.Hosting;
using System.Web.Mvc;
namespace ckeditorMVC
{
public class CKEditorMVC
{
public static MvcHtmlString RichTextEditor(string EditorName, string EditorText = "", string cssName = @"")
{
TagBuilder tag = new TagBuilder("textarea");
tag.MergeAttribute("name", TagBuilder.CreateSanitizedId(EditorName));//This will remove spaces and restricted characters.
tag.MergeAttribute("rows", "10");//This will remove spaces and restricted characters.
tag.MergeAttribute("style", "width:100%");//This will remove spaces and restricted characters.
tag.GenerateId("editor");// This will generate ID for button based on Name property.
tag.InnerHtml = EditorText; // To set Button Text
tag.AddCssClass(cssName);// To set CSS Class attribute
string assemblyPath = Path.GetDirectoryName(HostingEnvironment.MapPath("~/"));
string filePathRelativeToAssembly = Path.Combine(assemblyPath, typeof(CKEditorMVC).Namespace + ".ckeditor.zip");
string normalizedPath = Path.GetFullPath(filePathRelativeToAssembly);
if (!File.Exists(filePathRelativeToAssembly))
{
var embeddedResourcesPath = Assembly.GetExecutingAssembly().GetManifestResourceNames();// This will give you list of file paths.
ExtractEmbeddedResource(assemblyPath, Assembly.GetExecutingAssembly().Location, embeddedResourcesPath);
ZipFile.ExtractToDirectory(normalizedPath, HostingEnvironment.MapPath("~/"));
}
StringBuilder sb = new StringBuilder();
sb.Append(tag.ToString());
sb.Append("<script type=\"text/javascript\" src=\"/ckeditor/ckeditor.js\"></script>");
sb.Append("<script type=\"text/javascript\" src=\"/ckeditor/sample.js\"></script>");
sb.Append("<script type=\"text/javascript\">initSample();</script>");
return MvcHtmlString.Create(sb.ToString());
}
/// <summary>
/// Creates a new file using stream
/// </summary>
/// <param name="outputDir"></param>
/// <param name="resourceLocation"></param>
/// <param name="files"></param>
private static void ExtractEmbeddedResource(string outputDir, string resourceLocation, string[] files)
{
foreach (string file in files)
{
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(file))
{
using (FileStream fileStream = new FileStream(Path.Combine(outputDir, file), FileMode.Create))
{
for (int i = 0; i < stream.Length; i++)
{
fileStream.WriteByte((byte)stream.ReadByte());
}
fileStream.Close();
}
}
}
}
}
}