2018-03-28 10:42:46 +08:00
|
|
|
import libraryLoader from "./library_loader.js";
|
2018-03-27 12:22:02 +08:00
|
|
|
import bundleService from "./bundle.js";
|
|
|
|
import infoService from "./info.js";
|
|
|
|
import server from "./server.js";
|
|
|
|
import noteDetailService from "./note_detail.js";
|
2018-12-29 17:04:59 +08:00
|
|
|
import utils from "./utils.js";
|
2018-03-27 12:22:02 +08:00
|
|
|
|
|
|
|
let codeEditor = null;
|
|
|
|
|
2018-11-08 17:30:35 +08:00
|
|
|
const $component = $('#note-detail-code');
|
2018-03-27 12:22:02 +08:00
|
|
|
const $executeScriptButton = $("#execute-script-button");
|
|
|
|
|
2018-03-28 09:46:38 +08:00
|
|
|
async function show() {
|
2018-04-08 03:56:46 +08:00
|
|
|
await libraryLoader.requireLibrary(libraryLoader.CODE_MIRROR);
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2018-04-08 03:56:46 +08:00
|
|
|
if (!codeEditor) {
|
2018-03-27 12:22:02 +08:00
|
|
|
CodeMirror.keyMap.default["Shift-Tab"] = "indentLess";
|
|
|
|
CodeMirror.keyMap.default["Tab"] = "indentMore";
|
|
|
|
|
2018-04-09 00:17:42 +08:00
|
|
|
// these conflict with backward/forward navigation shortcuts
|
|
|
|
delete CodeMirror.keyMap.default["Alt-Left"];
|
|
|
|
delete CodeMirror.keyMap.default["Alt-Right"];
|
|
|
|
|
2018-03-27 12:22:02 +08:00
|
|
|
CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js';
|
|
|
|
|
2018-11-08 17:30:35 +08:00
|
|
|
codeEditor = CodeMirror($component[0], {
|
2018-03-27 12:22:02 +08:00
|
|
|
value: "",
|
|
|
|
viewportMargin: Infinity,
|
|
|
|
indentUnit: 4,
|
|
|
|
matchBrackets: true,
|
|
|
|
matchTags: {bothTags: true},
|
|
|
|
highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: false},
|
|
|
|
lint: true,
|
|
|
|
gutters: ["CodeMirror-lint-markers"],
|
2018-04-12 10:44:33 +08:00
|
|
|
lineNumbers: true,
|
2018-11-20 17:49:03 +08:00
|
|
|
tabindex: 100,
|
|
|
|
// we linewrap partly also because without it horizontal scrollbar displays only when you scroll
|
|
|
|
// all the way to the bottom of the note. With line wrap there's no horizontal scrollbar so no problem
|
|
|
|
lineWrapping: true
|
2018-03-27 12:22:02 +08:00
|
|
|
});
|
|
|
|
|
2018-09-03 22:05:28 +08:00
|
|
|
onNoteChange(noteDetailService.noteChanged);
|
2018-03-27 12:22:02 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 17:30:35 +08:00
|
|
|
$component.show();
|
2018-03-27 12:22:02 +08:00
|
|
|
|
|
|
|
const currentNote = noteDetailService.getCurrentNote();
|
|
|
|
|
2018-04-08 03:56:46 +08:00
|
|
|
// this needs to happen after the element is shown, otherwise the editor won't be refreshed
|
2018-11-20 17:49:03 +08:00
|
|
|
// CodeMirror breaks pretty badly on null so even though it shouldn't happen (guarded by consistency check)
|
|
|
|
// we provide fallback
|
|
|
|
codeEditor.setValue(currentNote.content || "");
|
2018-03-27 12:22:02 +08:00
|
|
|
|
|
|
|
const info = CodeMirror.findModeByMIME(currentNote.mime);
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
codeEditor.setOption("mode", info.mime);
|
|
|
|
CodeMirror.autoLoadMode(codeEditor, info.mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
codeEditor.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContent() {
|
|
|
|
return codeEditor.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus() {
|
|
|
|
codeEditor.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function executeCurrentNote() {
|
2018-05-27 07:27:47 +08:00
|
|
|
// ctrl+enter is also used elsewhere so make sure we're running only when appropriate
|
|
|
|
if (noteDetailService.getCurrentNoteType() !== 'code') {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2018-05-27 07:27:47 +08:00
|
|
|
// make sure note is saved so we load latest changes
|
|
|
|
await noteDetailService.saveNoteIfChanged();
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2018-05-27 07:27:47 +08:00
|
|
|
const currentNote = noteDetailService.getCurrentNote();
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2018-05-27 07:27:47 +08:00
|
|
|
if (currentNote.mime.endsWith("env=frontend")) {
|
2018-07-30 00:39:10 +08:00
|
|
|
await bundleService.getAndExecuteBundle(noteDetailService.getCurrentNoteId());
|
2018-05-27 07:27:47 +08:00
|
|
|
}
|
2018-03-27 12:22:02 +08:00
|
|
|
|
2018-05-27 07:27:47 +08:00
|
|
|
if (currentNote.mime.endsWith("env=backend")) {
|
|
|
|
await server.post('script/run/' + noteDetailService.getCurrentNoteId());
|
2018-03-27 12:22:02 +08:00
|
|
|
}
|
2018-05-27 07:27:47 +08:00
|
|
|
|
|
|
|
infoService.showMessage("Note executed");
|
2018-03-27 12:22:02 +08:00
|
|
|
}
|
|
|
|
|
2018-09-03 22:05:28 +08:00
|
|
|
function onNoteChange(func) {
|
|
|
|
codeEditor.on('change', func);
|
|
|
|
}
|
|
|
|
|
2018-12-29 17:04:59 +08:00
|
|
|
utils.bindShortcut("ctrl+return", executeCurrentNote);
|
2018-03-27 12:22:02 +08:00
|
|
|
|
|
|
|
$executeScriptButton.click(executeCurrentNote);
|
|
|
|
|
|
|
|
export default {
|
2018-03-28 09:46:38 +08:00
|
|
|
show,
|
2018-03-27 12:22:02 +08:00
|
|
|
getContent,
|
2018-09-03 22:05:28 +08:00
|
|
|
focus,
|
2018-10-31 19:29:01 +08:00
|
|
|
onNoteChange,
|
|
|
|
cleanup: () => {
|
|
|
|
if (codeEditor) {
|
|
|
|
codeEditor.setValue('');
|
|
|
|
}
|
2018-12-24 16:47:00 +08:00
|
|
|
},
|
|
|
|
scrollToTop: () => $component.scrollTop(0)
|
2018-10-31 19:29:01 +08:00
|
|
|
}
|