link-dialog.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*!
  2. * Link dialog plugin for Editor.md
  3. *
  4. * @file link-dialog.js
  5. * @author pandao
  6. * @version 1.2.1
  7. * @updateTime 2015-06-09
  8. * {@link https://github.com/pandao/editor.md}
  9. * @license MIT
  10. */
  11. (function() {
  12. var factory = function (exports) {
  13. var pluginName = "link-dialog";
  14. exports.fn.linkDialog = function() {
  15. var _this = this;
  16. var cm = this.cm;
  17. var editor = this.editor;
  18. var settings = this.settings;
  19. var selection = cm.getSelection();
  20. var lang = this.lang;
  21. var linkLang = lang.dialog.link;
  22. var classPrefix = this.classPrefix;
  23. var dialogName = classPrefix + pluginName, dialog;
  24. cm.focus();
  25. if (editor.find("." + dialogName).length > 0)
  26. {
  27. dialog = editor.find("." + dialogName);
  28. dialog.find("[data-url]").val("http://");
  29. dialog.find("[data-title]").val(selection);
  30. this.dialogShowMask(dialog);
  31. this.dialogLockScreen();
  32. dialog.show();
  33. }
  34. else
  35. {
  36. var dialogHTML = "<div class=\"" + classPrefix + "form\">" +
  37. "<label>" + linkLang.url + "</label>" +
  38. "<input type=\"text\" value=\"http://\" data-url />" +
  39. "<br/>" +
  40. "<label>" + linkLang.urlTitle + "</label>" +
  41. "<input type=\"text\" value=\"" + selection + "\" data-title />" +
  42. "<br/>" +
  43. "</div>";
  44. dialog = this.createDialog({
  45. title : linkLang.title,
  46. width : 380,
  47. height : 211,
  48. content : dialogHTML,
  49. mask : settings.dialogShowMask,
  50. drag : settings.dialogDraggable,
  51. lockScreen : settings.dialogLockScreen,
  52. maskStyle : {
  53. opacity : settings.dialogMaskOpacity,
  54. backgroundColor : settings.dialogMaskBgColor
  55. },
  56. buttons : {
  57. enter : [lang.buttons.enter, function() {
  58. var url = this.find("[data-url]").val();
  59. var title = this.find("[data-title]").val();
  60. if (url === "http://" || url === "")
  61. {
  62. alert(linkLang.urlEmpty);
  63. return false;
  64. }
  65. /*if (title === "")
  66. {
  67. alert(linkLang.titleEmpty);
  68. return false;
  69. }*/
  70. var str = "[" + title + "](" + url + " \"" + title + "\")";
  71. if (title == "")
  72. {
  73. str = "[" + url + "](" + url + ")";
  74. }
  75. cm.replaceSelection(str);
  76. this.hide().lockScreen(false).hideMask();
  77. return false;
  78. }],
  79. cancel : [lang.buttons.cancel, function() {
  80. this.hide().lockScreen(false).hideMask();
  81. return false;
  82. }]
  83. }
  84. });
  85. }
  86. };
  87. };
  88. // CommonJS/Node.js
  89. if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
  90. {
  91. module.exports = factory;
  92. }
  93. else if (typeof define === "function") // AMD/CMD/Sea.js
  94. {
  95. if (define.amd) { // for Require.js
  96. define(["editormd"], function(editormd) {
  97. factory(editormd);
  98. });
  99. } else { // for Sea.js
  100. define(function(require) {
  101. var editormd = require("./../../editormd");
  102. factory(editormd);
  103. });
  104. }
  105. }
  106. else
  107. {
  108. factory(window.editormd);
  109. }
  110. })();