Add SimpleMDE and Fix Image Paste for Issue/Comment Editor (#9197)
* update #9132 and #8834 - add SimpleMDE for issue and fix image paste for comment editor * attache tribute to simplemde * update #9197 force simplemde file input event when backspace press
This commit is contained in:
		
					parent
					
						
							
								61db834904
							
						
					
				
			
			
				commit
				
					
						121977c36f
					
				
			
		
					 2 changed files with 59 additions and 1 deletions
				
			
		| 
						 | 
					@ -673,6 +673,7 @@ func ViewIssue(ctx *context.Context) {
 | 
				
			||||||
	ctx.Data["RequireHighlightJS"] = true
 | 
						ctx.Data["RequireHighlightJS"] = true
 | 
				
			||||||
	ctx.Data["RequireDropzone"] = true
 | 
						ctx.Data["RequireDropzone"] = true
 | 
				
			||||||
	ctx.Data["RequireTribute"] = true
 | 
						ctx.Data["RequireTribute"] = true
 | 
				
			||||||
 | 
						ctx.Data["RequireSimpleMDE"] = true
 | 
				
			||||||
	renderAttachmentSettings(ctx)
 | 
						renderAttachmentSettings(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err = issue.LoadAttributes(); err != nil {
 | 
						if err = issue.LoadAttributes(); err != nil {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,6 +14,7 @@ let csrf;
 | 
				
			||||||
let suburl;
 | 
					let suburl;
 | 
				
			||||||
let previewFileModes;
 | 
					let previewFileModes;
 | 
				
			||||||
let simpleMDEditor;
 | 
					let simpleMDEditor;
 | 
				
			||||||
 | 
					const commentMDEditors = {};
 | 
				
			||||||
let codeMirrorEditor;
 | 
					let codeMirrorEditor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Disable Dropzone auto-discover because it's manually initialized
 | 
					// Disable Dropzone auto-discover because it's manually initialized
 | 
				
			||||||
| 
						 | 
					@ -304,11 +305,27 @@ function initImagePaste(target) {
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function initSimpleMDEImagePaste(simplemde, files) {
 | 
				
			||||||
 | 
					  simplemde.codemirror.on('paste', (_, event) => {
 | 
				
			||||||
 | 
					    retrieveImageFromClipboardAsBlob(event, (img) => {
 | 
				
			||||||
 | 
					      const name = img.name.substr(0, img.name.lastIndexOf('.'));
 | 
				
			||||||
 | 
					      uploadFile(img, (res) => {
 | 
				
			||||||
 | 
					        const data = JSON.parse(res);
 | 
				
			||||||
 | 
					        const pos = simplemde.codemirror.getCursor();
 | 
				
			||||||
 | 
					        simplemde.codemirror.replaceRange(``, pos);
 | 
				
			||||||
 | 
					        const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
 | 
				
			||||||
 | 
					        files.append(input);
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function initCommentForm() {
 | 
					function initCommentForm() {
 | 
				
			||||||
  if ($('.comment.form').length === 0) {
 | 
					  if ($('.comment.form').length === 0) {
 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  setCommentSimpleMDE($('.comment.form textarea'));
 | 
				
			||||||
  initBranchSelector();
 | 
					  initBranchSelector();
 | 
				
			||||||
  initCommentPreviewTab($('.comment.form'));
 | 
					  initCommentPreviewTab($('.comment.form'));
 | 
				
			||||||
  initImagePaste($('.comment.form textarea'));
 | 
					  initImagePaste($('.comment.form textarea'));
 | 
				
			||||||
| 
						 | 
					@ -836,6 +853,7 @@ function initRepository() {
 | 
				
			||||||
      const $renderContent = $segment.find('.render-content');
 | 
					      const $renderContent = $segment.find('.render-content');
 | 
				
			||||||
      const $rawContent = $segment.find('.raw-content');
 | 
					      const $rawContent = $segment.find('.raw-content');
 | 
				
			||||||
      let $textarea;
 | 
					      let $textarea;
 | 
				
			||||||
 | 
					      let $simplemde;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Setup new form
 | 
					      // Setup new form
 | 
				
			||||||
      if ($editContentZone.html().length === 0) {
 | 
					      if ($editContentZone.html().length === 0) {
 | 
				
			||||||
| 
						 | 
					@ -920,8 +938,10 @@ function initRepository() {
 | 
				
			||||||
        $tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
 | 
					        $tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
 | 
				
			||||||
        $editContentForm.find('.write.segment').attr('data-tab', $editContentZone.data('write'));
 | 
					        $editContentForm.find('.write.segment').attr('data-tab', $editContentZone.data('write'));
 | 
				
			||||||
        $editContentForm.find('.preview.segment').attr('data-tab', $editContentZone.data('preview'));
 | 
					        $editContentForm.find('.preview.segment').attr('data-tab', $editContentZone.data('preview'));
 | 
				
			||||||
 | 
					        $simplemde = setCommentSimpleMDE($textarea);
 | 
				
			||||||
 | 
					        commentMDEditors[$editContentZone.data('write')] = $simplemde;
 | 
				
			||||||
        initCommentPreviewTab($editContentForm);
 | 
					        initCommentPreviewTab($editContentForm);
 | 
				
			||||||
 | 
					        initSimpleMDEImagePaste($simplemde, $files);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $editContentZone.find('.cancel.button').click(() => {
 | 
					        $editContentZone.find('.cancel.button').click(() => {
 | 
				
			||||||
          $renderContent.show();
 | 
					          $renderContent.show();
 | 
				
			||||||
| 
						 | 
					@ -968,6 +988,7 @@ function initRepository() {
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        $textarea = $segment.find('textarea');
 | 
					        $textarea = $segment.find('textarea');
 | 
				
			||||||
 | 
					        $simplemde = commentMDEditors[$editContentZone.data('write')];
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Show write/preview tab and copy raw content as needed
 | 
					      // Show write/preview tab and copy raw content as needed
 | 
				
			||||||
| 
						 | 
					@ -975,8 +996,10 @@ function initRepository() {
 | 
				
			||||||
      $renderContent.hide();
 | 
					      $renderContent.hide();
 | 
				
			||||||
      if ($textarea.val().length === 0) {
 | 
					      if ($textarea.val().length === 0) {
 | 
				
			||||||
        $textarea.val($rawContent.text());
 | 
					        $textarea.val($rawContent.text());
 | 
				
			||||||
 | 
					        $simplemde.value($rawContent.text());
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      $textarea.focus();
 | 
					      $textarea.focus();
 | 
				
			||||||
 | 
					      $simplemde.codemirror.focus();
 | 
				
			||||||
      event.preventDefault();
 | 
					      event.preventDefault();
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1442,6 +1465,40 @@ function setSimpleMDE($editArea) {
 | 
				
			||||||
  return true;
 | 
					  return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function setCommentSimpleMDE($editArea) {
 | 
				
			||||||
 | 
					  const simplemde = new SimpleMDE({
 | 
				
			||||||
 | 
					    autoDownloadFontAwesome: false,
 | 
				
			||||||
 | 
					    element: $editArea[0],
 | 
				
			||||||
 | 
					    forceSync: true,
 | 
				
			||||||
 | 
					    renderingConfig: {
 | 
				
			||||||
 | 
					      singleLineBreaks: false
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    indentWithTabs: false,
 | 
				
			||||||
 | 
					    tabSize: 4,
 | 
				
			||||||
 | 
					    spellChecker: false,
 | 
				
			||||||
 | 
					    toolbar: ['bold', 'italic', 'strikethrough', '|',
 | 
				
			||||||
 | 
					      'heading-1', 'heading-2', 'heading-3', 'heading-bigger', 'heading-smaller', '|',
 | 
				
			||||||
 | 
					      'code', 'quote', '|',
 | 
				
			||||||
 | 
					      'unordered-list', 'ordered-list', '|',
 | 
				
			||||||
 | 
					      'link', 'image', 'table', 'horizontal-rule', '|',
 | 
				
			||||||
 | 
					      'clean-block']
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  simplemde.codemirror.setOption('extraKeys', {
 | 
				
			||||||
 | 
					    Enter: () => {
 | 
				
			||||||
 | 
					      if (!(issuesTribute.isActive || emojiTribute.isActive)) {
 | 
				
			||||||
 | 
					        return CodeMirror.Pass;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    Backspace: (cm) => {
 | 
				
			||||||
 | 
					      cm.getInputField().trigger('input');
 | 
				
			||||||
 | 
					      cm.execCommand('delCharBefore');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  issuesTribute.attach(simplemde.codemirror.getInputField());
 | 
				
			||||||
 | 
					  emojiTribute.attach(simplemde.codemirror.getInputField());
 | 
				
			||||||
 | 
					  return simplemde;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function setCodeMirror($editArea) {
 | 
					function setCodeMirror($editArea) {
 | 
				
			||||||
  if (simpleMDEditor) {
 | 
					  if (simpleMDEditor) {
 | 
				
			||||||
    simpleMDEditor.toTextArea();
 | 
					    simpleMDEditor.toTextArea();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue