external editor support

This commit is contained in:
Mo Bitar
2017-02-15 13:01:19 -06:00
parent 18ad8cd5dd
commit 979b5eb2a8
15 changed files with 581 additions and 312 deletions

View File

@@ -16,50 +16,9 @@ angular.module('app.frontend')
link:function(scope, elem, attrs, ctrl) {
/**
* Insert 4 spaces when a tab key is pressed,
* only used when inside of the text editor.
* If the shift key is pressed first, this event is
* not fired.
*/
var handleTab = function (event) {
if (!event.shiftKey && event.which == 9) {
event.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
var spaces = " ";
// Insert 4 spaces
this.value = this.value.substring(0, start)
+ spaces + this.value.substring(end);
// Place cursor 4 spaces away from where
// the tab key was pressed
this.selectionStart = this.selectionEnd = start + 4;
}
}
var handler = function(event) {
if (event.ctrlKey || event.metaKey) {
switch (String.fromCharCode(event.which).toLowerCase()) {
case 's':
event.preventDefault();
$timeout(function(){
ctrl.saveNote(event);
});
break;
case 'e':
event.preventDefault();
$timeout(function(){
ctrl.clickedEditNote();
})
break;
case 'm':
event.preventDefault();
$timeout(function(){
ctrl.toggleMarkdown();
})
break;
case 'o':
event.preventDefault();
$timeout(function(){
@@ -70,14 +29,6 @@ angular.module('app.frontend')
}
};
window.addEventListener('keydown', handler);
var element = document.getElementById("note-text-editor");
element.addEventListener('keydown', handleTab);
scope.$on('$destroy', function(){
window.removeEventListener('keydown', handler);
})
scope.$watch('ctrl.note', function(note, oldNote){
if(note) {
ctrl.setNote(note, oldNote);
@@ -88,7 +39,21 @@ angular.module('app.frontend')
}
}
})
.controller('EditorCtrl', function ($sce, $timeout, authManager, markdownRenderer, $rootScope, extensionManager, syncManager) {
.controller('EditorCtrl', function ($sce, $timeout, authManager, markdownRenderer, $rootScope, extensionManager, syncManager, modelManager) {
window.addEventListener("message", function(){
console.log("App received message:", event);
if(event.data.status) {
this.postNoteToExternalEditor();
} else {
var id = event.data.id;
var text = event.data.text;
if(this.note.uuid == id) {
this.note.text = text;
this.changesMade();
}
}
}.bind(this), false);
this.setNote = function(note, oldNote) {
this.editorMode = 'edit';
@@ -96,6 +61,13 @@ angular.module('app.frontend')
this.showMenu = false;
this.loadTagsString();
if(note.editorUrl) {
this.customEditor = this.editorForUrl(note.editorUrl);
this.postNoteToExternalEditor();
} else {
this.customEditor = null;
}
if(note.safeText().length == 0 && note.dummy) {
this.focusTitle(100);
}
@@ -109,6 +81,28 @@ angular.module('app.frontend')
}
}
this.selectedEditor = function(editor) {
this.showEditorMenu = false;
if(editor.default) {
this.customEditor = null;
} else {
this.customEditor = editor;
}
this.note.editorUrl = editor.url;
}.bind(this)
this.editorForUrl = function(url) {
var editors = modelManager.itemsForContentType("SN|Editor");
return editors.filter(function(editor){return editor.url == url})[0];
}
this.postNoteToExternalEditor = function() {
var externalEditorElement = document.getElementById("editor-iframe");
if(externalEditorElement) {
externalEditorElement.contentWindow.postMessage({text: this.note.text, id: this.note.uuid}, '*');
}
}
this.hasAvailableExtensions = function() {
return extensionManager.extensionsInContextOfItem(this.note).length > 0;
}