Skip to content

Commit

Permalink
[NEW] Only Load CodeMirror code when it is needed (RocketChat#15351)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSchoeler authored and ggazzo committed Sep 13, 2019
1 parent aaa6d91 commit 212621f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/ui-admin/client/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@
<div class="title">
{{label}}
</div>
{{> CodeMirror name=_id options=getEditorOptions code=value }}
{{setEditorOnBlur _id}}
{{> CodeMirror name=_id options=getEditorOptions code=value editorOnBlur=setEditorOnBlur}}

<div class="buttons">
<button class="rc-button rc-button--primary button-fullscreen">{{_ "Full_Screen"}}</button>
<button class="rc-button rc-button--primary button-restore">{{_ "Exit_Full_Screen"}}</button>
Expand Down
6 changes: 3 additions & 3 deletions app/ui-admin/client/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ Template.admin.helpers({
readOnly,
};
},
setEditorOnBlur(_id) {
Meteor.defer(function() {
setEditorOnBlur() {
return function(_id) {
if (!$(`.code-mirror-box[data-editor-id="${ _id }"] .CodeMirror`)[0]) {
return;
}
Expand All @@ -308,7 +308,7 @@ Template.admin.helpers({
const onChangeDelayed = _.debounce(onChange, 500);
codeMirror.on('change', onChangeDelayed);
codeMirror.changeAdded = true;
});
};
},
assetAccept(fileConstraints) {
if (fileConstraints.extensions && fileConstraints.extensions.length) {
Expand Down
2 changes: 1 addition & 1 deletion app/ui/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import './lib/iframeCommands';
import './lib/menu';
import './lib/Modernizr';
import './lib/parentTemplate';
import './lib/codeMirror/codeMirror';
import './lib/codeMirror';
import './lib/textarea-cursor';
import './views/cmsPage.html';
import './views/404/roomNotFound.html';
Expand Down
3 changes: 0 additions & 3 deletions app/ui/client/lib/codeMirror/codeMirror.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import './codeMirrorComponent.html';
import './codeMirrorComponent.js';

import 'codemirror/addon/fold/brace-fold.js';
import 'codemirror/addon/fold/comment-fold.js';
import 'codemirror/addon/fold/foldcode.js';
Expand Down
17 changes: 13 additions & 4 deletions app/ui/client/lib/codeMirror/codeMirrorComponent.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import { Template } from 'meteor/templating';
import 'codemirror/lib/codemirror.css';
import CodeMirror from 'codemirror/lib/codemirror.js';
import './codeMirrorComponent.html';

const CodeMirrors = {};

Template.CodeMirror.rendered = function() {
Template.CodeMirror.onRendered(async function() {
const CodeMirror = await import('codemirror/lib/codemirror.js');

await import('./codeMirror');
await import('codemirror/lib/codemirror.css');


const options = this.data.options || { lineNumbers: true };
const textarea = this.find('textarea');
const editor = CodeMirror.fromTextArea(textarea, options);

CodeMirrors[this.data.id || 'code-mirror-textarea'] = editor;
if (this.data && this.data.editorOnBlur) {
this.data.editorOnBlur(this.data.name);
}

const self = this;
editor.on('change', function(doc) {
Expand All @@ -34,7 +42,8 @@ Template.CodeMirror.rendered = function() {
Meteor.defer(function() {
editor.refresh();
});
};
});


Template.CodeMirror.destroyed = function() {
delete CodeMirrors[this.data.id || 'code-mirror-textarea'];
Expand Down
1 change: 1 addition & 0 deletions app/ui/client/lib/codeMirror/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './codeMirrorComponent';

0 comments on commit 212621f

Please sign in to comment.