custom-toolbar.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>自定义工具栏 - Editor.md examples</title>
  6. <link rel="stylesheet" href="css/style.css" />
  7. <link rel="stylesheet" href="../css/editormd.css" />
  8. <link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" />
  9. </head>
  10. <body>
  11. <div id="layout">
  12. <header>
  13. <h1>自定义工具栏</h1>
  14. <p>Custom toolbar (icons handler)</p>
  15. </header>
  16. <div id="test-editormd">
  17. <textarea style="display:none;">### Custom toolbar
  18. ```javascript
  19. $(function() {
  20. var testEditor = editormd("test-editormd", {
  21. width: "90%",
  22. height: 640,
  23. path : '../lib/',
  24. toolbarIcons : function() {
  25. // Or return editormd.toolbarModes[name]; // full, simple, mini
  26. // Using "||" set icons align right.
  27. return ["undo", "redo", "|", "bold", "hr", "|", "preview", "watch", "|", "fullscreen", "info", "testIcon", "testIcon2", "file", "faicon", "||", "watch", "fullscreen", "preview", "testIcon"]
  28. },
  29. toolbarIconsClass : {
  30. testIcon : "fa-gears" // 指定一个FontAawsome的图标类
  31. },
  32. toolbarIconTexts : {
  33. testIcon2 : "测试按钮" // 如果没有图标,则可以这样直接插入内容,可以是字符串或HTML标签
  34. },
  35. // 用于增加自定义工具栏的功能,可以直接插入HTML标签,不使用默认的元素创建图标
  36. toolbarCustomIcons : {
  37. file : "&lt;input type="file" accept=".md" /&gt;",
  38. faicon : "&lt;i class="fa fa-star" onclick="alert('faicon');"&gt;&lt;/i&gt;"
  39. },
  40. // 自定义工具栏按钮的事件处理
  41. toolbarHandlers : {
  42. /**
  43. * @param {Object} cm CodeMirror对象
  44. * @param {Object} icon 图标按钮jQuery元素对象
  45. * @param {Object} cursor CodeMirror的光标对象,可获取光标所在行和位置
  46. * @param {String} selection 编辑器选中的文本
  47. */
  48. testIcon : function(cm, icon, cursor, selection) {
  49. //var cursor = cm.getCursor(); //获取当前光标对象,同cursor参数
  50. //var selection = cm.getSelection(); //获取当前选中的文本,同selection参数
  51. // 替换选中文本,如果没有选中文本,则直接插入
  52. cm.replaceSelection("[" + selection + ":testIcon]");
  53. // 如果当前没有选中的文本,将光标移到要输入的位置
  54. if(selection === "") {
  55. cm.setCursor(cursor.line, cursor.ch + 1);
  56. }
  57. // this == 当前editormd实例
  58. console.log("testIcon =>", this, cm, icon, cursor, selection);
  59. },
  60. testIcon2 : function(cm, icon, cursor, selection) {
  61. cm.replaceSelection("[" + selection + ":testIcon2](" + icon.html() + ")");
  62. console.log("testIcon2 =>", this, icon.html());
  63. }
  64. },
  65. lang : {
  66. toolbar : {
  67. file : "上传文件",
  68. testIcon : "自定义按钮testIcon", // 自定义按钮的提示文本,即title属性
  69. testIcon2 : "自定义按钮testIcon2",
  70. undo : "撤销 (Ctrl+Z)"
  71. }
  72. },
  73. onload : function(){
  74. $("[type=\"file\"]").bind("change", function(){
  75. alert($(this).val());
  76. testEditor.cm.replaceSelection($(this).val());
  77. console.log($(this).val(), testEditor);
  78. });
  79. }
  80. });
  81. });
  82. ```
  83. </textarea>
  84. </div>
  85. </div>
  86. <script src="js/jquery.min.js"></script>
  87. <script src="../editormd.js"></script>
  88. <!-- 多语言和自定义工具栏的整合测试 -->
  89. <script src="../languages/en.js"></script>
  90. <script type="text/javascript">
  91. $(function() {
  92. var testEditor = editormd("test-editormd", {
  93. width: "90%",
  94. height: 640,
  95. path : '../lib/',
  96. watch : false,
  97. toolbarIcons : function() {
  98. // Or return editormd.toolbarModes[name]; // full, simple, mini
  99. // Using "||" set icons align right.
  100. return ["undo", "redo", "|", "bold", "hr", "|", "preview", "watch", "|", "fullscreen", "info", "testIcon", "testIcon2", "file", "faicon", "||", "watch", "fullscreen", "preview", "testIcon", "file"]
  101. },
  102. // toolbarIcons : "full", // You can also use editormd.toolbarModes[name] default list, values: full, simple, mini.
  103. toolbarIconsClass : {
  104. testIcon : "fa-gears" // 指定一个FontAawsome的图标类
  105. },
  106. toolbarIconTexts : {
  107. testIcon2 : "测试按钮Test button" // 如果没有图标,则可以这样直接插入内容,可以是字符串或HTML标签
  108. },
  109. // 用于增加自定义工具栏的功能,可以直接插入HTML标签,不使用默认的元素创建图标
  110. toolbarCustomIcons : {
  111. file : "<input type=\"file\" accept=\".md\" />",
  112. faicon : "<i class=\"fa fa-star\" onclick=\"alert('faicon');\"></i>"
  113. },
  114. // 自定义工具栏按钮的事件处理
  115. toolbarHandlers : {
  116. /**
  117. * @param {Object} cm CodeMirror对象
  118. * @param {Object} icon 图标按钮jQuery元素对象
  119. * @param {Object} cursor CodeMirror的光标对象,可获取光标所在行和位置
  120. * @param {String} selection 编辑器选中的文本
  121. */
  122. testIcon : function(cm, icon, cursor, selection) {
  123. //var cursor = cm.getCursor(); //获取当前光标对象,同cursor参数
  124. //var selection = cm.getSelection(); //获取当前选中的文本,同selection参数
  125. // 替换选中文本,如果没有选中文本,则直接插入
  126. cm.replaceSelection("[" + selection + ":testIcon]");
  127. // 如果当前没有选中的文本,将光标移到要输入的位置
  128. if(selection === "") {
  129. cm.setCursor(cursor.line, cursor.ch + 1);
  130. }
  131. // this == 当前editormd实例
  132. console.log("testIcon =>", this, cm, icon, cursor, selection);
  133. },
  134. testIcon2 : function(cm, icon, cursor, selection) {
  135. cm.replaceSelection("[" + selection + ":testIcon2](" + icon.html() + ")");
  136. console.log("testIcon2 =>", this, icon.html());
  137. }
  138. },
  139. lang : {
  140. toolbar : {
  141. file : "上传文件",
  142. testIcon : "自定义按钮testIcon", // 自定义按钮的提示文本,即title属性
  143. testIcon2 : "自定义按钮testIcon2",
  144. undo : "撤销 (Ctrl+Z)"
  145. }
  146. },
  147. onload : function(){
  148. $("[type=\"file\"]").bind("change", function(){
  149. alert($(this).val());
  150. testEditor.cm.replaceSelection($(this).val());
  151. console.log($(this).val(), testEditor);
  152. });
  153. }
  154. });
  155. });
  156. </script>
  157. </body>
  158. </html>