2018-03-28 09:36:01 +08:00
|
|
|
import bundleService from "./bundle.js";
|
|
|
|
import server from "./server.js";
|
|
|
|
import noteDetailService from "./note_detail.js";
|
2018-05-27 07:27:47 +08:00
|
|
|
import noteDetailCodeService from "./note_detail_code.js";
|
2018-03-28 09:36:01 +08:00
|
|
|
|
2018-05-27 07:27:47 +08:00
|
|
|
const $noteDetailCode = $('#note-detail-code');
|
2018-03-28 09:36:01 +08:00
|
|
|
const $noteDetailRender = $('#note-detail-render');
|
2018-05-27 07:27:47 +08:00
|
|
|
const $toggleEditButton = $('#toggle-edit-button');
|
|
|
|
const $renderButton = $('#render-button');
|
|
|
|
|
|
|
|
let codeEditorInitialized;
|
2018-03-28 09:36:01 +08:00
|
|
|
|
2018-03-28 09:46:38 +08:00
|
|
|
async function show() {
|
2018-05-27 07:27:47 +08:00
|
|
|
codeEditorInitialized = false;
|
|
|
|
|
2018-03-28 09:36:01 +08:00
|
|
|
$noteDetailRender.show();
|
|
|
|
|
2018-05-27 07:27:47 +08:00
|
|
|
await render();
|
|
|
|
}
|
|
|
|
|
|
|
|
$toggleEditButton.click(() => {
|
|
|
|
if ($noteDetailCode.is(":visible")) {
|
|
|
|
$noteDetailCode.hide();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!codeEditorInitialized) {
|
|
|
|
noteDetailCodeService.show();
|
|
|
|
|
|
|
|
codeEditorInitialized = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$noteDetailCode.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$renderButton.click(render);
|
|
|
|
|
|
|
|
async function render() {
|
|
|
|
// ctrl+enter is also used elsewhere so make sure we're running only when appropriate
|
|
|
|
if (noteDetailService.getCurrentNoteType() !== 'render') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codeEditorInitialized) {
|
|
|
|
await noteDetailService.saveNoteIfChanged();
|
|
|
|
}
|
|
|
|
|
2018-03-28 09:36:01 +08:00
|
|
|
const bundle = await server.get('script/bundle/' + noteDetailService.getCurrentNoteId());
|
|
|
|
|
|
|
|
$noteDetailRender.html(bundle.html);
|
|
|
|
|
|
|
|
await bundleService.executeBundle(bundle);
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:27:47 +08:00
|
|
|
$(document).bind('keydown', "ctrl+return", render);
|
|
|
|
|
2018-03-28 09:36:01 +08:00
|
|
|
export default {
|
2018-03-28 09:46:38 +08:00
|
|
|
show,
|
2018-05-27 07:27:47 +08:00
|
|
|
getContent: noteDetailCodeService.getContent,
|
2018-03-28 09:36:01 +08:00
|
|
|
focus: () => null
|
|
|
|
}
|