_form.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. {% if req.form.errors %}
  2. <div class="alert alert-danger">
  3. <ul>
  4. {% for error in req.form.errors %}
  5. <li>{{ error }}</li>
  6. {% endfor %}
  7. </ul>
  8. </div>
  9. {% endif %}
  10. <div id="form-box" class="row">
  11. <form action="{{ path }}/edit" id="page-form" method="post" class="col-md-6">
  12. <textarea name="pageForm[body]" class="form-control form-body-height" id="form-body">{% if pageForm.body %}{{ pageForm.body }}{% elseif not revision.body %}# {{ path|path2name }}{% else %}{{ revision.body }}{% endif %}</textarea>
  13. <input type="hidden" name="pageForm[format]" value="markdown" id="form-format">
  14. <input type="hidden" name="pageForm[currentRevision]" value="{{ pageForm.currentRevision|default(revision._id.toString()) }}">
  15. <div class="form-submit-group form-group form-inline">
  16. {#<button class="btn btn-default">
  17. <i class="fa fa-file-text"></i>
  18. ファイルを追加 ...
  19. </button>#}
  20. <div class="pull-right form-inline">
  21. <select name="pageForm[grant]" class="form-control">
  22. {% for grantId, grantLabel in consts.pageGrants %}
  23. <option value="{{ grantId }}" {% if pageForm.grant|default(page.grant) == grantId %}selected{% endif %}>{{ grantLabel }}</option>
  24. {% endfor %}
  25. </select>
  26. <input type="submit" class="btn btn-primary" id="edit-form-submit" value="ページを更新" />
  27. </div>
  28. </div>
  29. </form>
  30. <div class="col-md-6 hidden-sm hidden-xs">
  31. <div id="preview-body" class="wiki preview-body">
  32. </div>
  33. </div>
  34. <div class="file-module hidden">
  35. </div>
  36. <script type="text/javascript">
  37. $(function() {
  38. // preview watch
  39. var originalContent = $('#form-body').val();
  40. var prevContent = "";
  41. var watchTimer = setInterval(function() {
  42. var content = $('#form-body').val();
  43. if (prevContent != content) {
  44. var renderer = new Crowi.renderer($('#form-body').val(), $('#preview-body'));
  45. renderer.render();
  46. prevContent = content;
  47. }
  48. }, 500);
  49. // tabs handle
  50. $('textarea#form-body').on('keydown', function(event){
  51. var self = $(this)
  52. start = this.selectionStart,
  53. end = this.selectionEnd
  54. val = self.val();
  55. if (event.keyCode === 9) {
  56. // tab
  57. event.preventDefault();
  58. self.val(
  59. val.substring(0, start)
  60. + ' '
  61. + val.substring(end, val.length)
  62. );
  63. this.selectionStart = start + 4;
  64. this.selectionEnd = start + 4;
  65. } else if (event.keyCode === 27) {
  66. // escape
  67. self.blur();
  68. }
  69. });
  70. var unbindInlineAttachment = function($form) {
  71. console.log('unbind inlineattach.');
  72. $form.unbind('.inlineattach');
  73. };
  74. var bindInlineAttachment = function($form, attachmentOption) {
  75. console.log('bind inline with options:', attachmentOption);
  76. var $this = $form;
  77. var editor = createEditorInstance($form);
  78. var inlineattach = new inlineAttachment(attachmentOption, editor);
  79. $form.bind({
  80. 'paste.inlineattach': function(e) {
  81. inlineattach.onPaste(e.originalEvent);
  82. },
  83. 'drop.inlineattach': function(e) {
  84. e.stopPropagation();
  85. e.preventDefault();
  86. inlineattach.onDrop(e.originalEvent);
  87. },
  88. 'dragenter.inlineattach dragover.inlineattach': function(e) {
  89. e.stopPropagation();
  90. e.preventDefault();
  91. }
  92. });
  93. };
  94. var createEditorInstance = function($form) {
  95. var $this = $form;
  96. return {
  97. getValue: function() {
  98. return $this.val();
  99. },
  100. insertValue: function(val) {
  101. inlineAttachment.util.insertTextAtCursor($this[0], val);
  102. },
  103. setValue: function(val) {
  104. $this.val(val);
  105. }
  106. };
  107. };
  108. var $inputForm = $('textarea#form-body');
  109. if ($inputForm.length > 0) {
  110. var pageId = $('#content-main').data('page-id') || 0;
  111. var attachmentOption = {
  112. uploadUrl: '/_api/attachment/page/' + pageId,
  113. extraParams: {
  114. path: location.pathname
  115. },
  116. progressText: '(Uploading file...)',
  117. urlText: "\n![file]({filename})\n"
  118. };
  119. attachmentOption.onFileUploadResponse = function(res) {
  120. var result = JSON.parse(res.response);
  121. console.log("onFileUploadResponse", result);
  122. if (result.status && result.pageCreated) {
  123. var page = result.page,
  124. pageId = page._id;
  125. $('#content-main').data('page-id', page._id);
  126. $('#page-form [name="pageForm[currentRevision]"]').val(page.revision)
  127. unbindInlineAttachment($inputForm);
  128. attachmentOption.uploadUrl = '/_api/attachment/page/' + pageId,
  129. bindInlineAttachment($inputForm, attachmentOption);
  130. }
  131. return true;
  132. };
  133. bindInlineAttachment($inputForm, attachmentOption);
  134. }
  135. $('textarea#form-body').on('dragenter dragover', function() {
  136. $(this).addClass('dragover');
  137. });
  138. $('textarea#form-body').on('drop dragleave dragend', function() {
  139. $(this).removeClass('dragover');
  140. });
  141. });
  142. </script>
  143. </div>