define-plugin.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Define extention plugins for Editor.md - 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>Define extention plugins for Editor.md</h1>
  14. </header>
  15. <div id="test-editormd">
  16. <textarea style="display:none;">[TOC]
  17. ### Define plugin
  18. #### Plugin directory
  19. editor.md/
  20. plugins/
  21. plugin-name/
  22. ....
  23. #### Example
  24. ```javascript
  25. (function() {
  26. var factory = function (exports) {
  27. var $ = jQuery; // if using module loader(Require.js/Sea.js).
  28. exports.customMethod = function() {
  29. //....
  30. };
  31. exports.fn.youPluginName = function() {
  32. /*
  33. var _this = this; // this == the current instance object of Editor.md
  34. var lang = this.lang;
  35. var settings = this.settings;
  36. var editor = this.editor;
  37. var cursor = cm.getCursor();
  38. var selection = cm.getSelection();
  39. cm.focus();
  40. */
  41. //....
  42. };
  43. };
  44. // CommonJS/Node.js
  45. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  46. {
  47. module.exports = factory;
  48. }
  49. else if (typeof define === "function") // AMD/CMD/Sea.js
  50. {
  51. if (define.amd) { // for Require.js
  52. define(["editormd"], function(editormd) {
  53. factory(editormd);
  54. });
  55. } else { // for Sea.js
  56. define(function(require) {
  57. var editormd = require("./../../editormd");
  58. factory(editormd);
  59. });
  60. }
  61. }
  62. else
  63. {
  64. factory(window.editormd);
  65. }
  66. })();
  67. ```
  68. #### Usage plugin
  69. ```html
  70. &lt;script src="../plugins/you-plugin-name/you-plugin-name.js"&gt;&lt;/script&gt;
  71. &lt;script&gt;
  72. editormd.customMethod();
  73. var testEditor = editormd("test-editormd", {
  74. path : '../lib/',
  75. onload : function() {
  76. this.youPluginName();
  77. this.pluginA();
  78. this.executePlugin("somePluginName", "you-plugin-name/you-plugin-name"); // load and execute plugin
  79. }
  80. });
  81. // or
  82. $("#btn").click(function(){
  83. testEditor.youPluginName();
  84. });
  85. &lt;/script&gt;
  86. ```
  87. </textarea>
  88. </div>
  89. </div>
  90. <script src="js/jquery.min.js"></script>
  91. <script src="../editormd.js"></script>
  92. <script type="text/javascript">
  93. var testEditor;
  94. editormd.fn.customMethod = function() {
  95. console.log("customMethod", this);
  96. };
  97. editormd.fn.pluginA = function() {
  98. console.log("pluginA", this);
  99. };
  100. $(function() {
  101. editormd.methodOne = function(){
  102. console.log("editormd.methodOne");
  103. };
  104. editormd.loadPlugin("../plugins/test-plugin/test-plugin", function(){
  105. editormd.testPlugin();
  106. });
  107. editormd.loadPlugin("../plugins/image-dialog/image-dialog", function(){
  108. testEditor = editormd("test-editormd", {
  109. width : "90%",
  110. height : 720,
  111. path : '../lib/',
  112. onload : function() {
  113. this.customMethod();
  114. testEditor.imageDialog();
  115. this.executePlugin("htmlEntitiesDialog", "html-entities-dialog/html-entities-dialog"); // load and execute plugin
  116. this.pluginA();
  117. }
  118. }); // execute plugin
  119. });
  120. editormd.methodOne();
  121. });
  122. </script>
  123. </body>
  124. </html>