crowi-form.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. $(function() {
  2. // preview watch
  3. var originalContent = $('#form-body').val();
  4. var prevContent = "";
  5. var watchTimer = setInterval(function() {
  6. var content = $('#form-body').val();
  7. if (prevContent != content) {
  8. var renderer = new Crowi.renderer($('#form-body').val(), $('#preview-body'));
  9. renderer.render();
  10. prevContent = content;
  11. }
  12. }, 500);
  13. // tabs handle
  14. $('textarea#form-body').on('keydown', function(event){
  15. var self = $(this)
  16. start = this.selectionStart,
  17. end = this.selectionEnd
  18. val = self.val();
  19. if (event.keyCode === 9) {
  20. // tab
  21. event.preventDefault();
  22. self.val(
  23. val.substring(0, start)
  24. + ' '
  25. + val.substring(end, val.length)
  26. );
  27. this.selectionStart = start + 4;
  28. this.selectionEnd = start + 4;
  29. } else if (event.keyCode === 27) {
  30. // escape
  31. self.blur();
  32. }
  33. });
  34. var unbindInlineAttachment = function($form) {
  35. $form.unbind('.inlineattach');
  36. };
  37. var bindInlineAttachment = function($form, attachmentOption) {
  38. var $this = $form;
  39. var editor = createEditorInstance($form);
  40. var inlineattach = new inlineAttachment(attachmentOption, editor);
  41. $form.bind({
  42. 'paste.inlineattach': function(e) {
  43. inlineattach.onPaste(e.originalEvent);
  44. },
  45. 'drop.inlineattach': function(e) {
  46. e.stopPropagation();
  47. e.preventDefault();
  48. inlineattach.onDrop(e.originalEvent);
  49. },
  50. 'dragenter.inlineattach dragover.inlineattach': function(e) {
  51. e.stopPropagation();
  52. e.preventDefault();
  53. }
  54. });
  55. };
  56. var createEditorInstance = function($form) {
  57. var $this = $form;
  58. return {
  59. getValue: function() {
  60. return $this.val();
  61. },
  62. insertValue: function(val) {
  63. inlineAttachment.util.insertTextAtCursor($this[0], val);
  64. },
  65. setValue: function(val) {
  66. $this.val(val);
  67. }
  68. };
  69. };
  70. var $inputForm = $('textarea#form-body');
  71. if ($inputForm.length > 0) {
  72. var pageId = $('#content-main').data('page-id') || 0;
  73. var attachmentOption = {
  74. uploadUrl: '/_api/attachment/page/' + pageId,
  75. extraParams: {
  76. path: location.pathname
  77. },
  78. progressText: '(Uploading file...)',
  79. urlText: "\n![file]({filename})\n"
  80. };
  81. attachmentOption.onFileUploadResponse = function(res) {
  82. var result = JSON.parse(res.response);
  83. if (result.status && result.pageCreated) {
  84. var page = result.page,
  85. pageId = page._id;
  86. $('#content-main').data('page-id', page._id);
  87. $('#page-form [name="pageForm[currentRevision]"]').val(page.revision)
  88. unbindInlineAttachment($inputForm);
  89. attachmentOption.uploadUrl = '/_api/attachment/page/' + pageId,
  90. bindInlineAttachment($inputForm, attachmentOption);
  91. }
  92. return true;
  93. };
  94. bindInlineAttachment($inputForm, attachmentOption);
  95. }
  96. $('textarea#form-body').on('dragenter dragover', function() {
  97. $(this).addClass('dragover');
  98. });
  99. $('textarea#form-body').on('drop dragleave dragend', function() {
  100. $(this).removeClass('dragover');
  101. });
  102. });