Skip to content

Commit

Permalink
Clean legacy SimpleMDE code (#17926)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wxiaoguang authored Dec 10, 2021
1 parent 719bddc commit 0a9fcf6
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 82 deletions.
2 changes: 1 addition & 1 deletion routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func FileHistory(ctx *context.Context) {
func Diff(ctx *context.Context) {
ctx.Data["PageIsDiff"] = true
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true

userName := ctx.Repo.Owner.Name
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ func CompareDiff(ctx *context.Context) {
ctx.Data["IsRepoToolbarCommits"] = true
ctx.Data["IsDiffCompare"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
setTemplateIfExists(ctx, pullRequestTemplateKey, nil, pullRequestTemplateCandidates)
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
Expand Down
8 changes: 4 additions & 4 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
ctx.Data["PageIsEdit"] = true
ctx.Data["IsNewFile"] = isNewFile
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
canCommit := renderCommitRights(ctx)

treePath := cleanUploadFileName(ctx.Repo.TreePath)
Expand Down Expand Up @@ -200,7 +200,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
ctx.Data["PageHasPosted"] = true
ctx.Data["IsNewFile"] = isNewFile
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["TreePath"] = form.TreePath
ctx.Data["TreeNames"] = treeNames
ctx.Data["TreePaths"] = treePaths
Expand Down Expand Up @@ -544,7 +544,7 @@ func DeleteFilePost(ctx *context.Context) {
func UploadFile(ctx *context.Context) {
ctx.Data["PageIsUpload"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
upload.AddUploadContext(ctx, "repo")
canCommit := renderCommitRights(ctx)
treePath := cleanUploadFileName(ctx.Repo.TreePath)
Expand Down Expand Up @@ -580,7 +580,7 @@ func UploadFilePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.UploadRepoFileForm)
ctx.Data["PageIsUpload"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
upload.AddUploadContext(ctx, "repo")
canCommit := renderCommitRights(ctx)

Expand Down
6 changes: 3 additions & 3 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ func NewIssue(ctx *context.Context) {
ctx.Data["PageIsIssueList"] = true
ctx.Data["NewIssueChooseTemplate"] = len(ctx.IssueTemplatesFromDefaultBranch()) > 0
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
title := ctx.FormString("title")
Expand Down Expand Up @@ -959,7 +959,7 @@ func NewIssuePost(ctx *context.Context) {
ctx.Data["PageIsIssueList"] = true
ctx.Data["NewIssueChooseTemplate"] = len(ctx.IssueTemplatesFromDefaultBranch()) > 0
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
upload.AddUploadContext(ctx, "comment")
Expand Down Expand Up @@ -1144,7 +1144,7 @@ func ViewIssue(ctx *context.Context) {

ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["IsProjectsEnabled"] = ctx.Repo.CanRead(unit.TypeProjects)
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
upload.AddUploadContext(ctx, "comment")
Expand Down
4 changes: 2 additions & 2 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ func ViewPullFiles(ctx *context.Context) {
setCompareContext(ctx, baseCommit, commit, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)

ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true
if ctx.Data["Assignees"], err = models.GetRepoAssignees(ctx.Repo.Repository); err != nil {
ctx.ServerError("GetAssignees", err)
Expand Down Expand Up @@ -1045,7 +1045,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
ctx.Data["IsDiffCompare"] = true
ctx.Data["IsRepoToolbarCommits"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireHighlightJS"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
Expand Down
8 changes: 4 additions & 4 deletions routers/web/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func LatestRelease(ctx *context.Context) {
func NewRelease(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
ctx.Data["PageIsReleaseList"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["tag_target"] = ctx.Repo.Repository.DefaultBranch
if tagName := ctx.FormString("tag"); len(tagName) > 0 {
Expand Down Expand Up @@ -296,7 +296,7 @@ func NewReleasePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewReleaseForm)
ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
ctx.Data["PageIsReleaseList"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true

if ctx.HasError() {
Expand Down Expand Up @@ -415,7 +415,7 @@ func EditRelease(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
ctx.Data["PageIsReleaseList"] = true
ctx.Data["PageIsEditRelease"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
upload.AddUploadContext(ctx, "release")
Expand Down Expand Up @@ -454,7 +454,7 @@ func EditReleasePost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
ctx.Data["PageIsReleaseList"] = true
ctx.Data["PageIsEditRelease"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true

tagName := ctx.Params("*")
Expand Down
8 changes: 4 additions & 4 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func WikiRaw(ctx *context.Context) {
func NewWiki(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
ctx.Data["PageIsWiki"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true

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

if ctx.HasError() {
ctx.HTML(http.StatusOK, tplWikiNew)
Expand Down Expand Up @@ -677,7 +677,7 @@ func NewWikiPost(ctx *context.Context) {
func EditWiki(ctx *context.Context) {
ctx.Data["PageIsWiki"] = true
ctx.Data["PageIsWikiEdit"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireEasyMDE"] = true

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

if ctx.HasError() {
ctx.HTML(http.StatusOK, tplWikiNew)
Expand Down
2 changes: 1 addition & 1 deletion templates/base/footer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{{template "custom/body_outer_post" .}}

{{template "base/footer_content" .}}
{{if .RequireSimpleMDE}}
{{if .RequireEasyMDE}}
<script src="{{AssetUrlPrefix}}/js/easymde.js?v={{MD5 AppVer}}"></script>
<script src="{{AssetUrlPrefix}}/vendor/plugins/codemirror/addon/mode/loadmode.js"></script>
<script src="{{AssetUrlPrefix}}/vendor/plugins/codemirror/mode/meta.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion templates/base/head.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</script>
<link rel="icon" href="{{AssetUrlPrefix}}/img/logo.svg" type="image/svg+xml">
<link rel="alternate icon" href="{{AssetUrlPrefix}}/img/favicon.png" type="image/png">
{{if .RequireSimpleMDE}}
{{if .RequireEasyMDE}}
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/easymde.css?v={{MD5 AppVer}}">
{{end}}
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/index.css?v={{MD5 AppVer}}">
Expand Down
1 change: 0 additions & 1 deletion web_src/js/easymde.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ import EasyMDE from 'easymde';
import CodeMirror from 'codemirror/lib/codemirror.js';

window.EasyMDE = EasyMDE;
window.SimpleMDE = EasyMDE;
window.CodeMirror = CodeMirror;

Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import attachTribute from '../tribute.js';

export function createCommentSimpleMDE($editArea) {
if ($editArea.length === 0) {
/**
* create an EasyMDE editor for comment
* @param textarea jQuery or HTMLElement
* @returns {null|EasyMDE}
*/
export function createCommentEasyMDE(textarea) {
if (textarea instanceof jQuery) {
textarea = textarea[0];
}
if (!textarea) {
return null;
}

const simplemde = new SimpleMDE({
const easyMDE = new window.EasyMDE({
autoDownloadFontAwesome: false,
element: $editArea[0],
element: textarea,
forceSync: true,
renderingConfig: {
singleLineBreaks: false
Expand Down Expand Up @@ -50,8 +58,9 @@ export function createCommentSimpleMDE($editArea) {
},
]
});
$(simplemde.codemirror.getInputField()).addClass('js-quick-submit');
simplemde.codemirror.setOption('extraKeys', {
const inputField = easyMDE.codemirror.getInputField();
inputField.classList.add('js-quick-submit');
easyMDE.codemirror.setOption('extraKeys', {
Enter: () => {
const tributeContainer = document.querySelector('.tribute-container');
if (!tributeContainer || tributeContainer.style.display === 'none') {
Expand All @@ -65,8 +74,25 @@ export function createCommentSimpleMDE($editArea) {
cm.execCommand('delCharBefore');
}
});
attachTribute(simplemde.codemirror.getInputField(), {mentions: true, emoji: true});
$editArea.data('simplemde', simplemde);
$(simplemde.codemirror.getInputField()).data('simplemde', simplemde);
return simplemde;
attachTribute(inputField, {mentions: true, emoji: true});

// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
inputField._data_easyMDE = easyMDE;
textarea._data_easyMDE = easyMDE;
return easyMDE;
}

/**
* get the attached EasyMDE editor created by createCommentEasyMDE
* @param el jQuery or HTMLElement
* @returns {null|EasyMDE}
*/
export function getAttachedEasyMDE(el) {
if (el instanceof jQuery) {
el = el[0];
}
if (!el) {
return null;
}
return el._data_easyMDE;
}
8 changes: 4 additions & 4 deletions web_src/js/features/comp/ImagePaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ export function initCompImagePaste($target) {
});
}

export function initSimpleMDEImagePaste(simplemde, dropzone, files) {
export function initEasyMDEImagePaste(easyMDE, dropzone, files) {
const uploadUrl = dropzone.getAttribute('data-upload-url');
simplemde.codemirror.on('paste', async (_, e) => {
easyMDE.codemirror.on('paste', async (_, e) => {
for (const img of clipboardPastedImages(e)) {
const name = img.name.substr(0, img.name.lastIndexOf('.'));
const data = await uploadFile(img, uploadUrl);
const pos = simplemde.codemirror.getCursor();
simplemde.codemirror.replaceRange(`![${name}](/attachments/${data.uuid})`, pos);
const pos = easyMDE.codemirror.getCursor();
easyMDE.codemirror.replaceRange(`![${name}](/attachments/${data.uuid})`, pos);
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
files.append(input);
}
Expand Down
23 changes: 10 additions & 13 deletions web_src/js/features/repo-issue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {htmlEscape} from 'escape-goat';
import attachTribute from './tribute.js';
import {createCommentSimpleMDE} from './comp/CommentSimpleMDE.js';
import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/CommentEasyMDE.js';
import {initCompImagePaste} from './comp/ImagePaste.js';
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';

Expand Down Expand Up @@ -213,8 +213,8 @@ export function initRepoIssueStatusButton() {
// Change status
const $statusButton = $('#status-button');
$('#comment-form textarea').on('keyup', function () {
const $simplemde = $(this).data('simplemde');
const value = ($simplemde && $simplemde.value()) ? $simplemde.value() : $(this).val();
const easyMDE = getAttachedEasyMDE(this);
const value = easyMDE?.value() || $(this).val();
$statusButton.text($statusButton.data(value.length === 0 ? 'status' : 'status-and-comment'));
});
$statusButton.on('click', () => {
Expand Down Expand Up @@ -445,22 +445,19 @@ export function initRepoPullRequestReview() {
const form = $(this).closest('.comment-code-cloud').find('.comment-form');
form.removeClass('hide');
const $textarea = form.find('textarea');
let $simplemde;
if ($textarea.data('simplemde')) {
$simplemde = $textarea.data('simplemde');
} else {
let easyMDE = getAttachedEasyMDE($textarea);
if (!easyMDE) {
attachTribute($textarea.get(), {mentions: true, emoji: true});
$simplemde = createCommentSimpleMDE($textarea);
$textarea.data('simplemde', $simplemde);
easyMDE = createCommentEasyMDE($textarea);
}
$textarea.focus();
$simplemde.codemirror.focus();
easyMDE.codemirror.focus();
assignMenuAttributes(form.find('.menu'));
});

const $reviewBox = $('.review-box');
if ($reviewBox.length === 1) {
createCommentSimpleMDE($reviewBox.find('textarea'));
createCommentEasyMDE($reviewBox.find('textarea'));
initCompImagePaste($reviewBox);
}

Expand Down Expand Up @@ -519,9 +516,9 @@ export function initRepoPullRequestReview() {
td.find("input[name='path']").val(path);
const $textarea = commentCloud.find('textarea');
attachTribute($textarea.get(), {mentions: true, emoji: true});
const $simplemde = createCommentSimpleMDE($textarea);
const easyMDE = createCommentEasyMDE($textarea);
$textarea.focus();
$simplemde.codemirror.focus();
easyMDE.codemirror.focus();
}
});
}
Expand Down
Loading

0 comments on commit 0a9fcf6

Please sign in to comment.