改进富文本编辑器、Editor.md

时间 2018/12/3 15:43:04 加载中...

初衷

文章的编写想改用一个支持 Markdown 语法的编辑器,于是找到了 Editor.md,链接为 https://github.com/pandao/editor.md

将 github 上的项目拉取下来,查看 examples 下面的目录,可以了解到 Editor.md 的基本使用方法。

由于拉取下来的是整个项目,所以,我提取了必要的文件来减小文件的大小。
精简后的目录有:

css
fonts
images
languages
lib
plugins
editormd.min.js
jquery.min.js
simple.html

其中的simple.html是自己写的一个小demo。

editor.md 压缩包下载
链接:https://pan.baidu.com/s/1CjMaRixQ6E2_c5Q-zpUZeg
提取码:w3d1

基本使用

拷贝

首先将精简后的editor.md目录拷贝到项目中,我的是项目的Scripts目录下。

页面部分

引入样式

  1. <link rel="stylesheet" href="css/editormd.min.css">

引入js

  1. <script src="jquery.min.js"></script>
  2. <script src="editormd.min.js"></script>

HTML

  1. <div id="test-editormd">
  2. <textarea style="display:none;"></textarea>
  3. </div>

js初始化

  1. var testEditor;
  2. $(function() {
  3. testEditor = editormd("test-editormd", {
  4. placeholder : "开始编辑...",
  5. width : "90%",
  6. height : 640,
  7. syncScrolling : "single",
  8. imageUpload: true,//图片上传功能
  9. imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
  10. imageUploadURL : "../article/uploadImage",
  11. path : "./lib/",
  12. emoji: false,
  13. toolbarIcons : function() {
  14. // Or return editormd.toolbarModes[name]; // full, simple, mini
  15. // Using "||" set icons align right.
  16. return ["undo","redo","|","bold","italic","quote","uppercase","lowercase","|","h1","h2","h3","h4","|","list-ul","list-ol","hr","|","link","image","code","code-block","table","html-entities","|","watch","preview","fullscreen","clear","|","help"]
  17. }
  18. });
  19. });

取值

  1. testEditor.getMarkdown();

赋值

  1. testEditor.setMarkdown(content);

表单编辑的时候会用到赋值功能,但调用赋值操作的时候一般是在 testEditor 初始化之后,所以此方法调用要放在 onload 事件中。

  1. var testEditor = editormd("editormd", {
  2. placeholder: "开始编辑...",
  3. width: "100%",
  4. height: 640,
  5. //syncScrolling: "single",
  6. imageUpload: true,//图片上传功能
  7. imageFormats: ["jpg", "jpeg", "gif", "png", "bmp"],
  8. imageUploadURL: "/api/File/Upload",
  9. path: "/Scripts/editor.md/lib/",
  10. emoji: false,
  11. tocm: true, // Using [TOCM]
  12. tex: true, // 开启科学公式TeX语言支持
  13. flowChart: true,
  14. sequenceDiagram: true,
  15. toolbarIcons: function () {
  16. // Or return editormd.toolbarModes[name]; // full, simple, mini
  17. // Using "||" set icons align right.
  18. return ["undo", "redo", "|", "bold", "italic", "quote", "uppercase", "lowercase", "|", "h1", "h2", "h3", "h4", "|", "list-ul", "list-ol", "hr", "|", "link", "image", "code", "code-block", "table", "html-entities", "|", "watch", "preview", "fullscreen", "clear", "|", "help"]
  19. },
  20. onload: function () {
  21. if (theEditor == 0)
  22. testEditor.setMarkdown(content);
  23. }
  24. });

Markdown展示成HTML

方式一:Markdown内容写在HTML中

引入样式

  1. <link rel="stylesheet" href="../css/editormd.preview.css" />

引入js

  1. <script src="js/jquery.min.js"></script>
  2. <script src="../lib/marked.min.js"></script>
  3. <script src="../lib/prettify.min.js"></script>
  4. <script src="../lib/raphael.min.js"></script>
  5. <script src="../lib/underscore.min.js"></script>
  6. <script src="../lib/sequence-diagram.min.js"></script>
  7. <script src="../lib/flowchart.min.js"></script>
  8. <script src="../lib/jquery.flowchart.min.js"></script>
  9. <script src="../editormd.min.js"></script>

HTML

  1. <div id="test-editormd-view">
  2. <textarea style="display:none;" name="test-editormd-markdown-doc">###Hello world!</textarea>
  3. </div>

js初始化

  1. var testEditormdView = editormd.markdownToHTML("test-editormd-view", {
  2. htmlDecode: "style,script,iframe", // you can filter tags decode
  3. emoji: false,
  4. taskList: true,
  5. tocm: true,
  6. tex: true,
  7. flowChart: true,
  8. sequenceDiagram: true,
  9. });

方式二:Markdown内容通过js赋值转化

方式二不同的是 HTMLjs 部分

HTML

  1. <div id="test-editormd-view">
  2. <textarea style="display:none;" name="test-editormd-markdown-doc"></textarea>
  3. </div>

js初始化

  1. $.get("test.md", function(markdown) {
  2. var testEditormdView = editormd.markdownToHTML("test-editormd-view", {
  3. markdown: markdown,
  4. htmlDecode: "style,script,iframe", // you can filter tags decode
  5. emoji: false,
  6. taskList: true,
  7. tocm: true,
  8. tex: true,
  9. flowChart: true,
  10. sequenceDiagram: true,
  11. });
  12. }

图片上传

图片上传在上面的初始化已经提及到了,按这样的配置就可以了。

  1. imageUpload: true,//图片上传功能
  2. imageFormats: ["jpg", "jpeg", "gif", "png", "bmp"],
  3. imageUploadURL: "/api/File/Upload",

后台接口返回值需要按他给的来

  1. {
  2. "success" : 0, /*1-成功,0-失败*/
  3. "message" : "XXXXX", /*消息*/
  4. "url" : "http://XXXX/1.png" /*图片地址*/
  5. }

这里提供一个 C# 版本的接口

C#

  1. public class FileController : ApiController
  2. {
  3. [HttpPost]
  4. public IHttpActionResult Upload()
  5. {
  6. HttpFileCollection files = HttpContext.Current.Request.Files;
  7. HttpPostedFile file = files[0];
  8. if (file == null)
  9. {
  10. return Ok(new { success = 0, message = "没有文件" });
  11. }
  12. else if (!CheckFileType(file))
  13. {
  14. return Ok(new { success = 0, message = "文件类型不对" });
  15. }
  16. else if (!CheckFileSize(file))
  17. {
  18. return Ok(new { success = 0, message = "文件大于请小于20M" });
  19. }
  20. string extension = file.FileName.Substring(file.FileName.LastIndexOf("."));
  21. string theFile = Path.GetFileName(MD5Helper.MD5Value(file.FileName))+extension;
  22. string folderPath = HttpContext.Current.Request.MapPath("~/Upload");
  23. if (!Directory.Exists(folderPath))
  24. Directory.CreateDirectory(folderPath);
  25. var fileName = Path.Combine(folderPath, theFile);
  26. try
  27. {
  28. file.SaveAs(fileName);
  29. string path = "/upload/" + theFile;
  30. return Ok(new { success = 1, message = "", url = path });
  31. }
  32. catch (Exception ex)
  33. {
  34. return Ok(new { success = 1, message = ex.Message });
  35. }
  36. }
  37. private bool CheckFileType(HttpPostedFile file)
  38. {
  39. string extension = file.FileName.Substring(file.FileName.LastIndexOf("."));
  40. List<string> extens = new List<string>() { ".jpg", ".jpeg", ".gif", ".png", ".bmp" };
  41. return extens.Contains(extension);
  42. }
  43. private bool CheckFileSize(HttpPostedFile file)
  44. {
  45. var size = file.ContentLength / 1024 / 1024;
  46. if (size > 200) //如果大于20M
  47. return false;
  48. else
  49. return true;
  50. }
  51. }
版权说明
作者:SQBER
文章来源:http://blog.sqber.com/articles/improve-the-editor.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。