From 27cb6b1c4de8acc34d210bf443bfad08b64692bf Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 23 Jan 2018 20:45:34 -0500 Subject: [PATCH] added possibility of executing javascript --- public/javascripts/note_editor.js | 13 ++++++++++++- public/javascripts/note_type.js | 13 ++++++++++++- views/index.ejs | 5 +++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/public/javascripts/note_editor.js b/public/javascripts/note_editor.js index 73b64749c..50362b1f0 100644 --- a/public/javascripts/note_editor.js +++ b/public/javascripts/note_editor.js @@ -196,6 +196,14 @@ const noteEditor = (function() { return currentNote ? currentNote.detail.type : null; } + function executeScript() { + if (getCurrentNoteType() === 'code') { + const script = codeEditor.getValue(); + + eval(script); + } + } + $(document).ready(() => { noteTitleEl.on('input', () => { noteChanged(); @@ -234,6 +242,8 @@ const noteEditor = (function() { noteDetailEl.attr("tabindex", 2); }); + $(document).bind('keydown', "ctrl+return", executeScript); + setInterval(saveNoteIfChanged, 5000); return { @@ -249,6 +259,7 @@ const noteEditor = (function() { getCurrentNoteId, newNoteCreated, getEditor, - focus + focus, + executeScript }; })(); \ No newline at end of file diff --git a/public/javascripts/note_type.js b/public/javascripts/note_type.js index 726080682..3cd764cdc 100644 --- a/public/javascripts/note_type.js +++ b/public/javascripts/note_type.js @@ -1,6 +1,7 @@ "use strict"; const noteType = (function() { + const executeScriptButton = $("#execute-script-button"); const noteTypeModel = new NoteTypeModel(); function NoteTypeModel() { @@ -74,6 +75,8 @@ const noteType = (function() { + '/mime/' + encodeURIComponent(self.mime())); await noteEditor.reload(); + + self.updateExecuteScriptButtonVisibility(); } this.selectText = function() { @@ -96,6 +99,10 @@ const noteType = (function() { save(); }; + + this.updateExecuteScriptButtonVisibility = function() { + executeScriptButton.toggle(self.mime() === 'application/javascript'); + } } ko.applyBindings(noteTypeModel, document.getElementById('note-type')); @@ -105,6 +112,10 @@ const noteType = (function() { setNoteType: type => noteTypeModel.type(type), getNoteMime: () => noteTypeModel.mime(), - setNoteMime: mime => noteTypeModel.mime(mime) + setNoteMime: mime => { + noteTypeModel.mime(mime); + + noteTypeModel.updateExecuteScriptButtonVisibility(); + } }; })(); \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index 8c3152b2f..4fa48dbe6 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -95,6 +95,11 @@ + +