You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1 KiB

  1. /**
  2. * preview plugin
  3. * @param selector
  4. * @param target_selector
  5. */
  6. function Preview(selector, target_selector) {
  7. // get input element
  8. function get_input($e) {
  9. return $e.find(".js-preview-input").eq(0);
  10. }
  11. // get result html container element
  12. function get_container($t) {
  13. if ($t.hasClass("js-preview-container")) {
  14. return $t
  15. }
  16. return $t.find(".js-preview-container").eq(0);
  17. }
  18. var $e = $(selector);
  19. var $t = $(target_selector);
  20. var $ipt = get_input($t);
  21. if (!$ipt.length) {
  22. console.log("[preview]: no preview input");
  23. return
  24. }
  25. var $cnt = get_container($t);
  26. if (!$cnt.length) {
  27. console.log("[preview]: no preview container");
  28. return
  29. }
  30. // call api via ajax
  31. $e.on("click", function () {
  32. $.post("/api/v1/markdown", {
  33. text: $ipt.val()
  34. }, function (html) {
  35. $cnt.html(html);
  36. })
  37. });
  38. console.log("[preview]: init preview @", selector, "&", target_selector);
  39. }
  40. $.fn.extend({
  41. markdown_preview: function (target) {
  42. Preview(this, target);
  43. }
  44. });