new frontend API - getCurrentNoteContent and onNoteChange

This commit is contained in:
azivner
2018-09-03 16:05:28 +02:00
parent a3feaa13b3
commit c7a609e349
42 changed files with 817 additions and 106 deletions

View File

@@ -4,6 +4,7 @@ import utils from './utils.js';
import infoService from './info.js';
import linkService from './link.js';
import treeCache from './tree_cache.js';
import noteDetailService from './note_detail.js';
/**
* This is the main frontend API interface for scripts. It's published in the local "api" object.
@@ -191,6 +192,18 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) {
* @param {string} [noteTitle] - if not present we'll use note title
*/
this.createNoteLink = linkService.createNoteLink;
/**
* @method
* @returns {string} content of currently loaded note in the editor (HTML, code etc.)
*/
this.getCurrentNoteContent = noteDetailService.getCurrentNoteContent;
/**
* @method
* @param {function} func - callback called on note change
*/
this.onNoteChange = noteDetailService.onNoteChange;
}
export default FrontendScriptApi;

View File

@@ -49,6 +49,10 @@ const components = {
};
function getComponent(type) {
if (!type) {
type = getCurrentNote().type;
}
if (components[type]) {
return components[type];
}
@@ -93,11 +97,19 @@ async function switchToNote(noteId) {
}
}
function getCurrentNoteContent() {
return getComponent().getContent();
}
function onNoteChange(func) {
return getComponent().onNoteChange(func);
}
async function saveNote() {
const note = getCurrentNote();
note.title = $noteTitle.val();
note.content = getComponent(note.type).getContent();
note.content = getCurrentNoteContent(note);
// it's important to set the flag back to false immediatelly after retrieving title and content
// otherwise we might overwrite another change (especially async code)
@@ -107,7 +119,6 @@ async function saveNote() {
await server.put('notes/' + note.noteId, note.dto);
if (note.isProtected) {
protectedSessionHolder.touchProtectedSession();
}
@@ -549,5 +560,7 @@ export default {
refreshAttributes,
saveNote,
saveNoteIfChanged,
noteChanged
noteChanged,
getCurrentNoteContent,
onNoteChange
};

View File

@@ -35,7 +35,7 @@ async function show() {
tabindex: 100
});
codeEditor.on('change', noteDetailService.noteChanged);
onNoteChange(noteDetailService.noteChanged);
}
$noteDetailCode.show();
@@ -85,6 +85,10 @@ async function executeCurrentNote() {
infoService.showMessage("Note executed");
}
function onNoteChange(func) {
codeEditor.on('change', func);
}
$(document).bind('keydown', "ctrl+return", executeCurrentNote);
$executeScriptButton.click(executeCurrentNote);
@@ -92,5 +96,6 @@ $executeScriptButton.click(executeCurrentNote);
export default {
show,
getContent,
focus
focus,
onNoteChange
}

View File

@@ -46,5 +46,6 @@ function getFileUrl() {
export default {
show,
getContent: () => null,
focus: () => null
focus: () => null,
onNoteChange: () => null
}

View File

@@ -34,5 +34,6 @@ $renderButton.click(render);
export default {
show: render,
getContent: () => "",
focus: () => null
focus: () => null,
onNoteChange: () => null
}

View File

@@ -28,5 +28,6 @@ function show() {
export default {
getContent,
show,
focus: () => null
focus: () => null,
onNoteChange: () => null
}

View File

@@ -29,7 +29,7 @@ async function show() {
}
});
textEditor.model.document.on('change:data', noteDetailService.noteChanged);
onNoteChange(noteDetailService.noteChanged);
}
}
@@ -105,6 +105,10 @@ async function sendMarkdownDialog() {
$markdownImportTextarea.val('');
}
function onNoteChange(func) {
textEditor.model.document.on('change:data', func);
}
$markdownImportButton.click(sendMarkdownDialog);
$markdownImportDialog.bind('keydown', 'ctrl+return', sendMarkdownDialog);
@@ -115,5 +119,6 @@ export default {
show,
getEditor,
getContent,
focus
focus,
onNoteChange
}

View File

@@ -34,6 +34,7 @@ function NoteTypeModel() {
{ mime: 'application/javascript;env=backend', title: 'JavaScript backend' },
{ mime: 'application/json', title: 'JSON' },
{ mime: 'text/x-kotlin', title: 'Kotlin' },
{ mime: 'text/x-stex', title: 'LaTex' },
{ mime: 'text/x-lua', title: 'Lua' },
{ mime: 'text/x-markdown', title: 'Markdown' },
{ mime: 'text/x-objectivec', title: 'Objective C' },