Skip to content

Commit 0a9fcf6

Browse files
authored
Clean legacy SimpleMDE code (#17926)
Since we are using EasyMDE now, we do not need to keep the SimpleMDE code anymore. This PR removes all legacy SimpleMDE code, and makes some related changes: * `createCommentEasyMDE` can accept native DOM element, and it doesn't need `jQuery.data` to store EasyMDE editor object (as discussed about the frontend guideline). * introduce `getAttachedEasyMDE` to get the attached EasyMDE editor object, it's easier to find all the usage of EasyMDE. * rename variable names from `$simplemde` to `easyMDE`, the `$` was incorrect because it is a EasyMDE editor, not a jQuery object. With this PR, it will be easier to do more refactoring or replacing EasyMDE with other editors.
1 parent 719bddc commit 0a9fcf6

16 files changed

+104
-82
lines changed

routers/web/repo/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func FileHistory(ctx *context.Context) {
253253
func Diff(ctx *context.Context) {
254254
ctx.Data["PageIsDiff"] = true
255255
ctx.Data["RequireHighlightJS"] = true
256-
ctx.Data["RequireSimpleMDE"] = true
256+
ctx.Data["RequireEasyMDE"] = true
257257
ctx.Data["RequireTribute"] = true
258258

259259
userName := ctx.Repo.Owner.Name

routers/web/repo/compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ func CompareDiff(ctx *context.Context) {
761761
ctx.Data["IsRepoToolbarCommits"] = true
762762
ctx.Data["IsDiffCompare"] = true
763763
ctx.Data["RequireTribute"] = true
764-
ctx.Data["RequireSimpleMDE"] = true
764+
ctx.Data["RequireEasyMDE"] = true
765765
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
766766
setTemplateIfExists(ctx, pullRequestTemplateKey, nil, pullRequestTemplateCandidates)
767767
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled

routers/web/repo/editor.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
6969
ctx.Data["PageIsEdit"] = true
7070
ctx.Data["IsNewFile"] = isNewFile
7171
ctx.Data["RequireHighlightJS"] = true
72-
ctx.Data["RequireSimpleMDE"] = true
72+
ctx.Data["RequireEasyMDE"] = true
7373
canCommit := renderCommitRights(ctx)
7474

7575
treePath := cleanUploadFileName(ctx.Repo.TreePath)
@@ -200,7 +200,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
200200
ctx.Data["PageHasPosted"] = true
201201
ctx.Data["IsNewFile"] = isNewFile
202202
ctx.Data["RequireHighlightJS"] = true
203-
ctx.Data["RequireSimpleMDE"] = true
203+
ctx.Data["RequireEasyMDE"] = true
204204
ctx.Data["TreePath"] = form.TreePath
205205
ctx.Data["TreeNames"] = treeNames
206206
ctx.Data["TreePaths"] = treePaths
@@ -544,7 +544,7 @@ func DeleteFilePost(ctx *context.Context) {
544544
func UploadFile(ctx *context.Context) {
545545
ctx.Data["PageIsUpload"] = true
546546
ctx.Data["RequireTribute"] = true
547-
ctx.Data["RequireSimpleMDE"] = true
547+
ctx.Data["RequireEasyMDE"] = true
548548
upload.AddUploadContext(ctx, "repo")
549549
canCommit := renderCommitRights(ctx)
550550
treePath := cleanUploadFileName(ctx.Repo.TreePath)
@@ -580,7 +580,7 @@ func UploadFilePost(ctx *context.Context) {
580580
form := web.GetForm(ctx).(*forms.UploadRepoFileForm)
581581
ctx.Data["PageIsUpload"] = true
582582
ctx.Data["RequireTribute"] = true
583-
ctx.Data["RequireSimpleMDE"] = true
583+
ctx.Data["RequireEasyMDE"] = true
584584
upload.AddUploadContext(ctx, "repo")
585585
canCommit := renderCommitRights(ctx)
586586

routers/web/repo/issue.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ func NewIssue(ctx *context.Context) {
786786
ctx.Data["PageIsIssueList"] = true
787787
ctx.Data["NewIssueChooseTemplate"] = len(ctx.IssueTemplatesFromDefaultBranch()) > 0
788788
ctx.Data["RequireHighlightJS"] = true
789-
ctx.Data["RequireSimpleMDE"] = true
789+
ctx.Data["RequireEasyMDE"] = true
790790
ctx.Data["RequireTribute"] = true
791791
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
792792
title := ctx.FormString("title")
@@ -959,7 +959,7 @@ func NewIssuePost(ctx *context.Context) {
959959
ctx.Data["PageIsIssueList"] = true
960960
ctx.Data["NewIssueChooseTemplate"] = len(ctx.IssueTemplatesFromDefaultBranch()) > 0
961961
ctx.Data["RequireHighlightJS"] = true
962-
ctx.Data["RequireSimpleMDE"] = true
962+
ctx.Data["RequireEasyMDE"] = true
963963
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
964964
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
965965
upload.AddUploadContext(ctx, "comment")
@@ -1144,7 +1144,7 @@ func ViewIssue(ctx *context.Context) {
11441144

11451145
ctx.Data["RequireHighlightJS"] = true
11461146
ctx.Data["RequireTribute"] = true
1147-
ctx.Data["RequireSimpleMDE"] = true
1147+
ctx.Data["RequireEasyMDE"] = true
11481148
ctx.Data["IsProjectsEnabled"] = ctx.Repo.CanRead(unit.TypeProjects)
11491149
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
11501150
upload.AddUploadContext(ctx, "comment")

routers/web/repo/pull.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ func ViewPullFiles(ctx *context.Context) {
701701
setCompareContext(ctx, baseCommit, commit, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
702702

703703
ctx.Data["RequireHighlightJS"] = true
704-
ctx.Data["RequireSimpleMDE"] = true
704+
ctx.Data["RequireEasyMDE"] = true
705705
ctx.Data["RequireTribute"] = true
706706
if ctx.Data["Assignees"], err = models.GetRepoAssignees(ctx.Repo.Repository); err != nil {
707707
ctx.ServerError("GetAssignees", err)
@@ -1045,7 +1045,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
10451045
ctx.Data["IsDiffCompare"] = true
10461046
ctx.Data["IsRepoToolbarCommits"] = true
10471047
ctx.Data["RequireTribute"] = true
1048-
ctx.Data["RequireSimpleMDE"] = true
1048+
ctx.Data["RequireEasyMDE"] = true
10491049
ctx.Data["RequireHighlightJS"] = true
10501050
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
10511051
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled

routers/web/repo/release.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func LatestRelease(ctx *context.Context) {
262262
func NewRelease(ctx *context.Context) {
263263
ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
264264
ctx.Data["PageIsReleaseList"] = true
265-
ctx.Data["RequireSimpleMDE"] = true
265+
ctx.Data["RequireEasyMDE"] = true
266266
ctx.Data["RequireTribute"] = true
267267
ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
268268
if tagName := ctx.FormString("tag"); len(tagName) > 0 {
@@ -296,7 +296,7 @@ func NewReleasePost(ctx *context.Context) {
296296
form := web.GetForm(ctx).(*forms.NewReleaseForm)
297297
ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
298298
ctx.Data["PageIsReleaseList"] = true
299-
ctx.Data["RequireSimpleMDE"] = true
299+
ctx.Data["RequireEasyMDE"] = true
300300
ctx.Data["RequireTribute"] = true
301301

302302
if ctx.HasError() {
@@ -415,7 +415,7 @@ func EditRelease(ctx *context.Context) {
415415
ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
416416
ctx.Data["PageIsReleaseList"] = true
417417
ctx.Data["PageIsEditRelease"] = true
418-
ctx.Data["RequireSimpleMDE"] = true
418+
ctx.Data["RequireEasyMDE"] = true
419419
ctx.Data["RequireTribute"] = true
420420
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
421421
upload.AddUploadContext(ctx, "release")
@@ -454,7 +454,7 @@ func EditReleasePost(ctx *context.Context) {
454454
ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
455455
ctx.Data["PageIsReleaseList"] = true
456456
ctx.Data["PageIsEditRelease"] = true
457-
ctx.Data["RequireSimpleMDE"] = true
457+
ctx.Data["RequireEasyMDE"] = true
458458
ctx.Data["RequireTribute"] = true
459459

460460
tagName := ctx.Params("*")

routers/web/repo/wiki.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ func WikiRaw(ctx *context.Context) {
622622
func NewWiki(ctx *context.Context) {
623623
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
624624
ctx.Data["PageIsWiki"] = true
625-
ctx.Data["RequireSimpleMDE"] = true
625+
ctx.Data["RequireEasyMDE"] = true
626626

627627
if !ctx.Repo.Repository.HasWiki() {
628628
ctx.Data["title"] = "Home"
@@ -639,7 +639,7 @@ func NewWikiPost(ctx *context.Context) {
639639
form := web.GetForm(ctx).(*forms.NewWikiForm)
640640
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
641641
ctx.Data["PageIsWiki"] = true
642-
ctx.Data["RequireSimpleMDE"] = true
642+
ctx.Data["RequireEasyMDE"] = true
643643

644644
if ctx.HasError() {
645645
ctx.HTML(http.StatusOK, tplWikiNew)
@@ -677,7 +677,7 @@ func NewWikiPost(ctx *context.Context) {
677677
func EditWiki(ctx *context.Context) {
678678
ctx.Data["PageIsWiki"] = true
679679
ctx.Data["PageIsWikiEdit"] = true
680-
ctx.Data["RequireSimpleMDE"] = true
680+
ctx.Data["RequireEasyMDE"] = true
681681

682682
if !ctx.Repo.Repository.HasWiki() {
683683
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
@@ -697,7 +697,7 @@ func EditWikiPost(ctx *context.Context) {
697697
form := web.GetForm(ctx).(*forms.NewWikiForm)
698698
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
699699
ctx.Data["PageIsWiki"] = true
700-
ctx.Data["RequireSimpleMDE"] = true
700+
ctx.Data["RequireEasyMDE"] = true
701701

702702
if ctx.HasError() {
703703
ctx.HTML(http.StatusOK, tplWikiNew)

templates/base/footer.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{{template "custom/body_outer_post" .}}
1313

1414
{{template "base/footer_content" .}}
15-
{{if .RequireSimpleMDE}}
15+
{{if .RequireEasyMDE}}
1616
<script src="{{AssetUrlPrefix}}/js/easymde.js?v={{MD5 AppVer}}"></script>
1717
<script src="{{AssetUrlPrefix}}/vendor/plugins/codemirror/addon/mode/loadmode.js"></script>
1818
<script src="{{AssetUrlPrefix}}/vendor/plugins/codemirror/mode/meta.js"></script>

templates/base/head.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</script>
5757
<link rel="icon" href="{{AssetUrlPrefix}}/img/logo.svg" type="image/svg+xml">
5858
<link rel="alternate icon" href="{{AssetUrlPrefix}}/img/favicon.png" type="image/png">
59-
{{if .RequireSimpleMDE}}
59+
{{if .RequireEasyMDE}}
6060
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/easymde.css?v={{MD5 AppVer}}">
6161
{{end}}
6262
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/index.css?v={{MD5 AppVer}}">

web_src/js/easymde.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ import EasyMDE from 'easymde';
33
import CodeMirror from 'codemirror/lib/codemirror.js';
44

55
window.EasyMDE = EasyMDE;
6-
window.SimpleMDE = EasyMDE;
76
window.CodeMirror = CodeMirror;
87

web_src/js/features/comp/CommentSimpleMDE.js web_src/js/features/comp/CommentEasyMDE.js

+36-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import attachTribute from '../tribute.js';
22

3-
export function createCommentSimpleMDE($editArea) {
4-
if ($editArea.length === 0) {
3+
/**
4+
* create an EasyMDE editor for comment
5+
* @param textarea jQuery or HTMLElement
6+
* @returns {null|EasyMDE}
7+
*/
8+
export function createCommentEasyMDE(textarea) {
9+
if (textarea instanceof jQuery) {
10+
textarea = textarea[0];
11+
}
12+
if (!textarea) {
513
return null;
614
}
715

8-
const simplemde = new SimpleMDE({
16+
const easyMDE = new window.EasyMDE({
917
autoDownloadFontAwesome: false,
10-
element: $editArea[0],
18+
element: textarea,
1119
forceSync: true,
1220
renderingConfig: {
1321
singleLineBreaks: false
@@ -50,8 +58,9 @@ export function createCommentSimpleMDE($editArea) {
5058
},
5159
]
5260
});
53-
$(simplemde.codemirror.getInputField()).addClass('js-quick-submit');
54-
simplemde.codemirror.setOption('extraKeys', {
61+
const inputField = easyMDE.codemirror.getInputField();
62+
inputField.classList.add('js-quick-submit');
63+
easyMDE.codemirror.setOption('extraKeys', {
5564
Enter: () => {
5665
const tributeContainer = document.querySelector('.tribute-container');
5766
if (!tributeContainer || tributeContainer.style.display === 'none') {
@@ -65,8 +74,25 @@ export function createCommentSimpleMDE($editArea) {
6574
cm.execCommand('delCharBefore');
6675
}
6776
});
68-
attachTribute(simplemde.codemirror.getInputField(), {mentions: true, emoji: true});
69-
$editArea.data('simplemde', simplemde);
70-
$(simplemde.codemirror.getInputField()).data('simplemde', simplemde);
71-
return simplemde;
77+
attachTribute(inputField, {mentions: true, emoji: true});
78+
79+
// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
80+
inputField._data_easyMDE = easyMDE;
81+
textarea._data_easyMDE = easyMDE;
82+
return easyMDE;
83+
}
84+
85+
/**
86+
* get the attached EasyMDE editor created by createCommentEasyMDE
87+
* @param el jQuery or HTMLElement
88+
* @returns {null|EasyMDE}
89+
*/
90+
export function getAttachedEasyMDE(el) {
91+
if (el instanceof jQuery) {
92+
el = el[0];
93+
}
94+
if (!el) {
95+
return null;
96+
}
97+
return el._data_easyMDE;
7298
}

web_src/js/features/comp/ImagePaste.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ export function initCompImagePaste($target) {
7676
});
7777
}
7878

79-
export function initSimpleMDEImagePaste(simplemde, dropzone, files) {
79+
export function initEasyMDEImagePaste(easyMDE, dropzone, files) {
8080
const uploadUrl = dropzone.getAttribute('data-upload-url');
81-
simplemde.codemirror.on('paste', async (_, e) => {
81+
easyMDE.codemirror.on('paste', async (_, e) => {
8282
for (const img of clipboardPastedImages(e)) {
8383
const name = img.name.substr(0, img.name.lastIndexOf('.'));
8484
const data = await uploadFile(img, uploadUrl);
85-
const pos = simplemde.codemirror.getCursor();
86-
simplemde.codemirror.replaceRange(`![${name}](/attachments/${data.uuid})`, pos);
85+
const pos = easyMDE.codemirror.getCursor();
86+
easyMDE.codemirror.replaceRange(`![${name}](/attachments/${data.uuid})`, pos);
8787
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
8888
files.append(input);
8989
}

web_src/js/features/repo-issue.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {htmlEscape} from 'escape-goat';
22
import attachTribute from './tribute.js';
3-
import {createCommentSimpleMDE} from './comp/CommentSimpleMDE.js';
3+
import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/CommentEasyMDE.js';
44
import {initCompImagePaste} from './comp/ImagePaste.js';
55
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
66

@@ -213,8 +213,8 @@ export function initRepoIssueStatusButton() {
213213
// Change status
214214
const $statusButton = $('#status-button');
215215
$('#comment-form textarea').on('keyup', function () {
216-
const $simplemde = $(this).data('simplemde');
217-
const value = ($simplemde && $simplemde.value()) ? $simplemde.value() : $(this).val();
216+
const easyMDE = getAttachedEasyMDE(this);
217+
const value = easyMDE?.value() || $(this).val();
218218
$statusButton.text($statusButton.data(value.length === 0 ? 'status' : 'status-and-comment'));
219219
});
220220
$statusButton.on('click', () => {
@@ -445,22 +445,19 @@ export function initRepoPullRequestReview() {
445445
const form = $(this).closest('.comment-code-cloud').find('.comment-form');
446446
form.removeClass('hide');
447447
const $textarea = form.find('textarea');
448-
let $simplemde;
449-
if ($textarea.data('simplemde')) {
450-
$simplemde = $textarea.data('simplemde');
451-
} else {
448+
let easyMDE = getAttachedEasyMDE($textarea);
449+
if (!easyMDE) {
452450
attachTribute($textarea.get(), {mentions: true, emoji: true});
453-
$simplemde = createCommentSimpleMDE($textarea);
454-
$textarea.data('simplemde', $simplemde);
451+
easyMDE = createCommentEasyMDE($textarea);
455452
}
456453
$textarea.focus();
457-
$simplemde.codemirror.focus();
454+
easyMDE.codemirror.focus();
458455
assignMenuAttributes(form.find('.menu'));
459456
});
460457

461458
const $reviewBox = $('.review-box');
462459
if ($reviewBox.length === 1) {
463-
createCommentSimpleMDE($reviewBox.find('textarea'));
460+
createCommentEasyMDE($reviewBox.find('textarea'));
464461
initCompImagePaste($reviewBox);
465462
}
466463

@@ -519,9 +516,9 @@ export function initRepoPullRequestReview() {
519516
td.find("input[name='path']").val(path);
520517
const $textarea = commentCloud.find('textarea');
521518
attachTribute($textarea.get(), {mentions: true, emoji: true});
522-
const $simplemde = createCommentSimpleMDE($textarea);
519+
const easyMDE = createCommentEasyMDE($textarea);
523520
$textarea.focus();
524-
$simplemde.codemirror.focus();
521+
easyMDE.codemirror.focus();
525522
}
526523
});
527524
}

0 commit comments

Comments
 (0)