preview.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. });