Skip to content

Commit

Permalink
update go-gitea#9132 and go-gitea#8834 - add SimpleMDE for issue and …
Browse files Browse the repository at this point in the history
…fix image paste for comment editor
  • Loading branch information
blueworrybear committed Nov 28, 2019
1 parent f5bd088 commit d707d1f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion public/js/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireDropzone"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireSimpleMDE"] = true
renderAttachmentSettings(ctx)

if err = issue.LoadAttributes(); err != nil {
Expand Down
46 changes: 45 additions & 1 deletion web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let csrf;
let suburl;
let previewFileModes;
let simpleMDEditor;
const commentMDEditors = {};
let codeMirrorEditor;

// Disable Dropzone auto-discover because it's manually initialized
Expand Down Expand Up @@ -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(`![${name}](${suburl}/attachments/${data.uuid})`, pos);
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
files.append(input);
});
});
});
}

function initCommentForm() {
if ($('.comment.form').length === 0) {
return;
}

setCommentSimpleMDE($('.comment.form textarea'));
initBranchSelector();
initCommentPreviewTab($('.comment.form'));
initImagePaste($('.comment.form textarea'));
Expand Down Expand Up @@ -797,6 +814,7 @@ function initRepository() {
const $renderContent = $segment.find('.render-content');
const $rawContent = $segment.find('.raw-content');
let $textarea;
let $simplemde;

// Setup new form
if ($editContentZone.html().length === 0) {
Expand Down Expand Up @@ -881,8 +899,10 @@ function initRepository() {
$tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
$editContentForm.find('.write.segment').attr('data-tab', $editContentZone.data('write'));
$editContentForm.find('.preview.segment').attr('data-tab', $editContentZone.data('preview'));

$simplemde = setCommentSimpleMDE($textarea);
commentMDEditors[$editContentZone.data('write')] = $simplemde;
initCommentPreviewTab($editContentForm);
initSimpleMDEImagePaste($simplemde, $files);

$editContentZone.find('.cancel.button').click(() => {
$renderContent.show();
Expand Down Expand Up @@ -929,15 +949,18 @@ function initRepository() {
});
} else {
$textarea = $segment.find('textarea');
$simplemde = commentMDEditors[$editContentZone.data('write')];
}

// Show write/preview tab and copy raw content as needed
$editContentZone.show();
$renderContent.hide();
if ($textarea.val().length === 0) {
$textarea.val($rawContent.text());
$simplemde.value($rawContent.text());
}
$textarea.focus();
$simplemde.codemirror.focus();
event.preventDefault();
});

Expand Down Expand Up @@ -1398,6 +1421,27 @@ function setSimpleMDE($editArea) {
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']
});
return simplemde;
}

function setCodeMirror($editArea) {
if (simpleMDEditor) {
simpleMDEditor.toTextArea();
Expand Down

0 comments on commit d707d1f

Please sign in to comment.