Skip to content

Commit 121977c

Browse files
blueworrybearlunny
authored andcommitted
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
1 parent 61db834 commit 121977c

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

routers/repo/issue.go

+1
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ func ViewIssue(ctx *context.Context) {
673673
ctx.Data["RequireHighlightJS"] = true
674674
ctx.Data["RequireDropzone"] = true
675675
ctx.Data["RequireTribute"] = true
676+
ctx.Data["RequireSimpleMDE"] = true
676677
renderAttachmentSettings(ctx)
677678

678679
if err = issue.LoadAttributes(); err != nil {

web_src/js/index.js

+58-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let csrf;
1414
let suburl;
1515
let previewFileModes;
1616
let simpleMDEditor;
17+
const commentMDEditors = {};
1718
let codeMirrorEditor;
1819

1920
// Disable Dropzone auto-discover because it's manually initialized
@@ -304,11 +305,27 @@ function initImagePaste(target) {
304305
});
305306
}
306307

308+
function initSimpleMDEImagePaste(simplemde, files) {
309+
simplemde.codemirror.on('paste', (_, event) => {
310+
retrieveImageFromClipboardAsBlob(event, (img) => {
311+
const name = img.name.substr(0, img.name.lastIndexOf('.'));
312+
uploadFile(img, (res) => {
313+
const data = JSON.parse(res);
314+
const pos = simplemde.codemirror.getCursor();
315+
simplemde.codemirror.replaceRange(`![${name}](${suburl}/attachments/${data.uuid})`, pos);
316+
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
317+
files.append(input);
318+
});
319+
});
320+
});
321+
}
322+
307323
function initCommentForm() {
308324
if ($('.comment.form').length === 0) {
309325
return;
310326
}
311327

328+
setCommentSimpleMDE($('.comment.form textarea'));
312329
initBranchSelector();
313330
initCommentPreviewTab($('.comment.form'));
314331
initImagePaste($('.comment.form textarea'));
@@ -836,6 +853,7 @@ function initRepository() {
836853
const $renderContent = $segment.find('.render-content');
837854
const $rawContent = $segment.find('.raw-content');
838855
let $textarea;
856+
let $simplemde;
839857

840858
// Setup new form
841859
if ($editContentZone.html().length === 0) {
@@ -920,8 +938,10 @@ function initRepository() {
920938
$tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
921939
$editContentForm.find('.write.segment').attr('data-tab', $editContentZone.data('write'));
922940
$editContentForm.find('.preview.segment').attr('data-tab', $editContentZone.data('preview'));
923-
941+
$simplemde = setCommentSimpleMDE($textarea);
942+
commentMDEditors[$editContentZone.data('write')] = $simplemde;
924943
initCommentPreviewTab($editContentForm);
944+
initSimpleMDEImagePaste($simplemde, $files);
925945

926946
$editContentZone.find('.cancel.button').click(() => {
927947
$renderContent.show();
@@ -968,15 +988,18 @@ function initRepository() {
968988
});
969989
} else {
970990
$textarea = $segment.find('textarea');
991+
$simplemde = commentMDEditors[$editContentZone.data('write')];
971992
}
972993

973994
// Show write/preview tab and copy raw content as needed
974995
$editContentZone.show();
975996
$renderContent.hide();
976997
if ($textarea.val().length === 0) {
977998
$textarea.val($rawContent.text());
999+
$simplemde.value($rawContent.text());
9781000
}
9791001
$textarea.focus();
1002+
$simplemde.codemirror.focus();
9801003
event.preventDefault();
9811004
});
9821005

@@ -1442,6 +1465,40 @@ function setSimpleMDE($editArea) {
14421465
return true;
14431466
}
14441467

1468+
function setCommentSimpleMDE($editArea) {
1469+
const simplemde = new SimpleMDE({
1470+
autoDownloadFontAwesome: false,
1471+
element: $editArea[0],
1472+
forceSync: true,
1473+
renderingConfig: {
1474+
singleLineBreaks: false
1475+
},
1476+
indentWithTabs: false,
1477+
tabSize: 4,
1478+
spellChecker: false,
1479+
toolbar: ['bold', 'italic', 'strikethrough', '|',
1480+
'heading-1', 'heading-2', 'heading-3', 'heading-bigger', 'heading-smaller', '|',
1481+
'code', 'quote', '|',
1482+
'unordered-list', 'ordered-list', '|',
1483+
'link', 'image', 'table', 'horizontal-rule', '|',
1484+
'clean-block']
1485+
});
1486+
simplemde.codemirror.setOption('extraKeys', {
1487+
Enter: () => {
1488+
if (!(issuesTribute.isActive || emojiTribute.isActive)) {
1489+
return CodeMirror.Pass;
1490+
}
1491+
},
1492+
Backspace: (cm) => {
1493+
cm.getInputField().trigger('input');
1494+
cm.execCommand('delCharBefore');
1495+
}
1496+
});
1497+
issuesTribute.attach(simplemde.codemirror.getInputField());
1498+
emojiTribute.attach(simplemde.codemirror.getInputField());
1499+
return simplemde;
1500+
}
1501+
14451502
function setCodeMirror($editArea) {
14461503
if (simpleMDEditor) {
14471504
simpleMDEditor.toTextArea();

0 commit comments

Comments
 (0)