Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load EasyMDE/CodeMirror dynamically, remove RequireEasyMDE #18069

Merged
merged 9 commits into from
Jan 5, 2022
Merged
1 change: 0 additions & 1 deletion routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ func FileHistory(ctx *context.Context) {
func Diff(ctx *context.Context) {
ctx.Data["PageIsDiff"] = true
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true

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

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

Expand Down
3 changes: 0 additions & 3 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ func NewIssue(ctx *context.Context) {
ctx.Data["PageIsIssueList"] = true
ctx.Data["NewIssueChooseTemplate"] = len(ctx.IssueTemplatesFromDefaultBranch()) > 0
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
title := ctx.FormString("title")
Expand Down Expand Up @@ -962,7 +961,6 @@ func NewIssuePost(ctx *context.Context) {
ctx.Data["PageIsIssueList"] = true
ctx.Data["NewIssueChooseTemplate"] = len(ctx.IssueTemplatesFromDefaultBranch()) > 0
ctx.Data["RequireHighlightJS"] = 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 @@ -1147,7 +1145,6 @@ func ViewIssue(ctx *context.Context) {

ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireTribute"] = 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
2 changes: 0 additions & 2 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ func ViewPullFiles(ctx *context.Context) {
setCompareContext(ctx, baseCommit, commit, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)

ctx.Data["RequireHighlightJS"] = 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 @@ -1088,7 +1087,6 @@ func CompareAndPullRequestPost(ctx *context.Context) {
ctx.Data["IsDiffCompare"] = true
ctx.Data["IsRepoToolbarCommits"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireHighlightJS"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
Expand Down
4 changes: 0 additions & 4 deletions routers/web/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ 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["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 +295,6 @@ 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["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true

if ctx.HasError() {
Expand Down Expand Up @@ -415,7 +413,6 @@ func EditRelease(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
ctx.Data["PageIsReleaseList"] = true
ctx.Data["PageIsEditRelease"] = 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 +451,6 @@ func EditReleasePost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.release.edit_release")
ctx.Data["PageIsReleaseList"] = true
ctx.Data["PageIsEditRelease"] = true
ctx.Data["RequireEasyMDE"] = true
ctx.Data["RequireTribute"] = true

tagName := ctx.Params("*")
Expand Down
4 changes: 0 additions & 4 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,6 @@ 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["RequireEasyMDE"] = true

if !ctx.Repo.Repository.HasWiki() {
ctx.Data["title"] = "Home"
Expand All @@ -639,7 +638,6 @@ 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["RequireEasyMDE"] = true

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

if !ctx.Repo.Repository.HasWiki() {
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
Expand All @@ -697,7 +694,6 @@ 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["RequireEasyMDE"] = true

if ctx.HasError() {
ctx.HTML(http.StatusOK, tplWikiNew)
Expand Down
8 changes: 0 additions & 8 deletions templates/base/footer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
{{template "custom/body_outer_post" .}}

{{template "base/footer_content" .}}
{{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>
<script>
CodeMirror.modeURL = '{{AssetUrlPrefix}}/vendor/plugins/codemirror/mode/%N/%N.js';
</script>
{{end}}

<!-- Third-party libraries -->
{{if .RequireU2F}}
Expand Down
3 changes: 0 additions & 3 deletions templates/base/head.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
</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 .RequireEasyMDE}}
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/easymde.css?v={{MD5 AppVer}}">
{{end}}
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/index.css?v={{MD5 AppVer}}">
<noscript>
<style>
Expand Down
7 changes: 0 additions & 7 deletions web_src/js/easymde.js

This file was deleted.

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

const {appSubUrl} = window.config;

function loadScript(url) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.async = true;
script.addEventListener('load', () => {
resolve();
});
script.addEventListener('error', (e) => {
reject(e.error);
});
document.body.appendChild(script);
script.src = url;
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
});
}

export async function importEasyMDE() {
// for CodeMirror: the plugins should be loaded dynamically
// https://github.com/codemirror/CodeMirror/issues/5484
// https://github.com/codemirror/CodeMirror/issues/4838

const [{default: EasyMDE}, {default: CodeMirror}] = await Promise.all([
import(/* webpackChunkName: "easymde" */'easymde'),
import(/* webpackChunkName: "codemirror" */'codemirror'),
import(/* webpackChunkName: "easymde" */'easymde/dist/easymde.min.css'),
]);

// CodeMirror plugins must be loaded by a "Plain browser env"
window.CodeMirror = CodeMirror;
await Promise.all([
loadScript(`${appSubUrl}/assets/vendor/plugins/codemirror/addon/mode/loadmode.js`),
loadScript(`${appSubUrl}/assets/vendor/plugins/codemirror/mode/meta.js`),
]);

// the loadmode.js/meta.js would set the modeURL/modeInfo properties, so we check it to make sure our loading works
if (!CodeMirror.modeURL || !CodeMirror.modeInfo) {
throw new Error('failed to load plugins for CodeMirror');
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
}

CodeMirror.modeURL = `${appSubUrl}/assets/vendor/plugins/codemirror/mode/%N/%N.js`;
return EasyMDE;
}

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

const easyMDE = new window.EasyMDE({
const EasyMDE = await importEasyMDE();
const easyMDE = new EasyMDE({
autoDownloadFontAwesome: false,
element: textarea,
forceSync: true,
renderingConfig: {
singleLineBreaks: false
singleLineBreaks: false,
6543 marked this conversation as resolved.
Show resolved Hide resolved
},
indentWithTabs: false,
tabSize: 4,
Expand Down Expand Up @@ -56,7 +101,7 @@ export function createCommentEasyMDE(textarea) {
className: 'fa fa-file',
title: 'Revert to simple textarea',
},
]
],
});
const inputField = easyMDE.codemirror.getInputField();
inputField.classList.add('js-quick-submit');
Expand All @@ -72,13 +117,12 @@ export function createCommentEasyMDE(textarea) {
cm.getInputField().trigger('input');
}
cm.execCommand('delCharBefore');
}
},
});
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;
setAttachedEasyMDE(inputField, easyMDE);
setAttachedEasyMDE(textarea, easyMDE);
return easyMDE;
}

Expand All @@ -96,3 +140,14 @@ export function getAttachedEasyMDE(el) {
}
return el._data_easyMDE;
}

function setAttachedEasyMDE(el, easyMDE) {
if (el instanceof jQuery) {
el = el[0];
}
if (!el) {
return;
}
// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
el._data_easyMDE = easyMDE;
}
13 changes: 7 additions & 6 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 {createCommentEasyMDE, getAttachedEasyMDE} from './comp/CommentEasyMDE.js';
import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/EasyMDE.js';
import {initCompImagePaste} from './comp/ImagePaste.js';
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';

Expand Down Expand Up @@ -439,16 +439,17 @@ export function initRepoPullRequestReview() {
$(`#show-outdated-${id}`).removeClass('hide');
});

$(document).on('click', 'button.comment-form-reply', function (e) {
$(document).on('click', 'button.comment-form-reply', async function (e) {
e.preventDefault();
Copy link
Member

@silverwind silverwind Dec 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIFE needed for this preventDefault? I think but I think the code may as well work with removal of this line it as button click should not have a default action.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, it would submit a form?


$(this).hide();
const form = $(this).closest('.comment-code-cloud').find('.comment-form');
form.removeClass('hide');
const $textarea = form.find('textarea');
let easyMDE = getAttachedEasyMDE($textarea);
if (!easyMDE) {
attachTribute($textarea.get(), {mentions: true, emoji: true});
easyMDE = createCommentEasyMDE($textarea);
await attachTribute($textarea.get(), {mentions: true, emoji: true});
easyMDE = await createCommentEasyMDE($textarea);
}
$textarea.focus();
easyMDE.codemirror.focus();
Expand Down Expand Up @@ -515,8 +516,8 @@ export function initRepoPullRequestReview() {
td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed');
td.find("input[name='path']").val(path);
const $textarea = commentCloud.find('textarea');
attachTribute($textarea.get(), {mentions: true, emoji: true});
const easyMDE = createCommentEasyMDE($textarea);
await attachTribute($textarea.get(), {mentions: true, emoji: true});
const easyMDE = await createCommentEasyMDE($textarea);
$textarea.focus();
easyMDE.codemirror.focus();
}
Expand Down
5 changes: 3 additions & 2 deletions web_src/js/features/repo-legacy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/CommentEasyMDE.js';
import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/EasyMDE.js';
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
import {initCompImagePaste, initEasyMDEImagePaste} from './comp/ImagePaste.js';
import {
Expand Down Expand Up @@ -256,6 +256,7 @@ export function initRepoCommentForm() {

async function onEditContent(event) {
event.preventDefault();

$(this).closest('.dropdown').find('.menu').toggle('visible');
const $segment = $(this).closest('.header').next();
const $editContentZone = $segment.find('.edit-content-zone');
Expand Down Expand Up @@ -341,7 +342,7 @@ async function onEditContent(event) {
$tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
$editContentForm.find('.write').attr('data-tab', $editContentZone.data('write'));
$editContentForm.find('.preview').attr('data-tab', $editContentZone.data('preview'));
easyMDE = createCommentEasyMDE($textarea);
easyMDE = await createCommentEasyMDE($textarea);

initCompMarkupContentPreviewTab($editContentForm);
if ($dropzone.length === 1) {
Expand Down
18 changes: 10 additions & 8 deletions web_src/js/features/repo-release.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import attachTribute from './tribute.js';
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
import {initEasyMDEImagePaste} from './comp/ImagePaste.js';
import {createCommentEasyMDE} from './comp/CommentEasyMDE.js';
import {createCommentEasyMDE} from './comp/EasyMDE.js';

export function initRepoRelease() {
$(document).on('click', '.remove-rel-attach', function() {
Expand All @@ -19,11 +19,13 @@ export function initRepoReleaseEditor() {
return false;
}

const $textarea = $editor.find('textarea');
attachTribute($textarea.get(), {mentions: false, emoji: true});
const $files = $editor.parent().find('.files');
const easyMDE = createCommentEasyMDE($textarea);
initCompMarkupContentPreviewTab($editor);
const dropzone = $editor.parent().find('.dropzone')[0];
initEasyMDEImagePaste(easyMDE, dropzone, $files);
(async () => {
const $textarea = $editor.find('textarea');
await attachTribute($textarea.get(), {mentions: false, emoji: true});
const $files = $editor.parent().find('.files');
const easyMDE = await createCommentEasyMDE($textarea);
initCompMarkupContentPreviewTab($editor);
const dropzone = $editor.parent().find('.dropzone')[0];
initEasyMDEImagePaste(easyMDE, dropzone, $files);
})();
}
Loading