lazy / dynamic loading of CKEditor and Code mirror

This commit is contained in:
azivner 2018-02-19 22:02:03 -05:00
parent 131af9ab12
commit a149c6a105
4 changed files with 107 additions and 57 deletions

View File

@ -17,6 +17,15 @@ const sqlConsole = (function() {
width: $(window).width(), width: $(window).width(),
height: $(window).height(), height: $(window).height(),
open: function() { open: function() {
initEditor();
}
});
}
async function initEditor() {
if (!codeEditor) {
await requireLibrary(CODE_MIRROR);
CodeMirror.keyMap.default["Shift-Tab"] = "indentLess"; CodeMirror.keyMap.default["Shift-Tab"] = "indentLess";
CodeMirror.keyMap.default["Tab"] = "indentMore"; CodeMirror.keyMap.default["Tab"] = "indentMore";
@ -26,16 +35,15 @@ const sqlConsole = (function() {
value: "", value: "",
viewportMargin: Infinity, viewportMargin: Infinity,
indentUnit: 4, indentUnit: 4,
highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: false } highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: false}
}); });
codeEditor.setOption("mode", "text/x-sqlite"); codeEditor.setOption("mode", "text/x-sqlite");
CodeMirror.autoLoadMode(codeEditor, "sql"); CodeMirror.autoLoadMode(codeEditor, "sql");
}
codeEditor.focus(); codeEditor.focus();
} }
});
}
async function execute() { async function execute() {
const sqlQuery = codeEditor.getValue(); const sqlQuery = codeEditor.getValue();

View File

@ -124,14 +124,42 @@ const noteEditor = (function() {
isNewNoteCreated = true; isNewNoteCreated = true;
} }
function setContent(content) { async function setContent(content) {
if (currentNote.detail.type === 'text') { if (currentNote.detail.type === 'text') {
if (!editor) {
await requireLibrary(CKEDITOR);
editor = await BalloonEditor.create($noteDetail[0], {});
editor.document.on('change', noteChanged);
}
// temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49 // temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49
editor.setData(content ? content : "<p></p>"); editor.setData(content ? content : "<p></p>");
$noteDetail.show(); $noteDetail.show();
} }
else if (currentNote.detail.type === 'code') { else if (currentNote.detail.type === 'code') {
if (!codeEditor) {
await requireLibrary(CODE_MIRROR);
CodeMirror.keyMap.default["Shift-Tab"] = "indentLess";
CodeMirror.keyMap.default["Tab"] = "indentMore";
CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js';
codeEditor = CodeMirror($("#note-detail-code")[0], {
value: "",
viewportMargin: Infinity,
indentUnit: 4,
matchBrackets: true,
matchTags: { bothTags: true },
highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: false }
});
codeEditor.on('change', noteChanged);
}
$noteDetailCode.show(); $noteDetailCode.show();
// this needs to happen after the element is shown, otherwise the editor won't be refresheds // this needs to happen after the element is shown, otherwise the editor won't be refresheds
@ -196,7 +224,7 @@ const noteEditor = (function() {
$attachmentFileType.text(currentNote.detail.mime); $attachmentFileType.text(currentNote.detail.mime);
} }
else { else {
setContent(currentNote.detail.content); await setContent(currentNote.detail.content);
} }
noteChangeDisabled = false; noteChangeDisabled = false;
@ -312,34 +340,6 @@ const noteEditor = (function() {
noteTree.setNoteTitle(getCurrentNoteId(), title); noteTree.setNoteTitle(getCurrentNoteId(), title);
}); });
BalloonEditor
.create(document.querySelector('#note-detail'), {
})
.then(edit => {
editor = edit;
editor.document.on('change', noteChanged);
})
.catch(error => {
console.error(error);
});
CodeMirror.keyMap.default["Shift-Tab"] = "indentLess";
CodeMirror.keyMap.default["Tab"] = "indentMore";
CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js';
codeEditor = CodeMirror($("#note-detail-code")[0], {
value: "",
viewportMargin: Infinity,
indentUnit: 4,
matchBrackets: true,
matchTags: { bothTags: true },
highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: false }
});
codeEditor.on('change', noteChanged);
// so that tab jumps from note title (which has tabindex 1) // so that tab jumps from note title (which has tabindex 1)
$noteDetail.attr("tabindex", 2); $noteDetail.attr("tabindex", 2);
}); });

View File

@ -132,3 +132,56 @@ function formatAttribute(attr) {
return str; return str;
} }
const CKEDITOR = { "js": ["libraries/ckeditor/ckeditor.js"] };
const CODE_MIRROR = {
js: [
"libraries/codemirror/codemirror.js",
"libraries/codemirror/addon/mode/loadmode.js",
"libraries/codemirror/addon/fold/xml-fold.js",
"libraries/codemirror/addon/edit/matchbrackets.js",
"libraries/codemirror/addon/edit/matchtags.js",
"libraries/codemirror/addon/search/match-highlighter.js",
"libraries/codemirror/mode/meta.js"
],
css: [
"libraries/codemirror/codemirror.css"
]
};
async function requireLibrary(library) {
if (library.css) {
library.css.map(cssUrl => requireCss(cssUrl));
}
if (library.js) {
for (const scriptUrl of library.js) {
await requireScript(scriptUrl);
}
}
}
async function requireScript(url) {
const scripts = Array
.from(document.querySelectorAll('script'))
.map(scr => scr.src);
if (!scripts.includes(url)) {
return $.ajax({
url: url,
dataType: "script",
cache: true
})
}
}
async function requireCss(url) {
const css = Array
.from(document.querySelectorAll('link'))
.map(scr => scr.href);
if (!css.includes(url)) {
$('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', url));
}
}

View File

@ -476,8 +476,6 @@
<link href="libraries/fancytree/skin-win8/ui.fancytree.css" rel="stylesheet"> <link href="libraries/fancytree/skin-win8/ui.fancytree.css" rel="stylesheet">
<script src="libraries/fancytree/jquery.fancytree-all.min.js"></script> <script src="libraries/fancytree/jquery.fancytree-all.min.js"></script>
<script src="libraries/ckeditor/ckeditor.js"></script>
<script src="libraries/jquery.hotkeys.js"></script> <script src="libraries/jquery.hotkeys.js"></script>
<script src="libraries/jquery.fancytree.hotkeys.js"></script> <script src="libraries/jquery.fancytree.hotkeys.js"></script>
@ -485,15 +483,6 @@
<script src="libraries/knockout.min.js"></script> <script src="libraries/knockout.min.js"></script>
<script src="libraries/codemirror/codemirror.js"></script>
<link rel="stylesheet" href="libraries/codemirror/codemirror.css">
<script src="libraries/codemirror/addon/mode/loadmode.js"></script>
<script src="libraries/codemirror/addon/fold/xml-fold.js"></script>
<script src="libraries/codemirror/addon/edit/matchbrackets.js"></script>
<script src="libraries/codemirror/addon/edit/matchtags.js"></script>
<script src="libraries/codemirror/addon/search/match-highlighter.js"></script>
<script src="libraries/codemirror/mode/meta.js"></script>
<link href="stylesheets/style.css" rel="stylesheet"> <link href="stylesheets/style.css" rel="stylesheet">
<script src="javascripts/utils.js"></script> <script src="javascripts/utils.js"></script>